Properly regenerate gcc/configure.
[official-gcc.git] / gcc / ada / sem_ch6.adb
blob01c6e26b50c555bc3b0f28b42b16b7d8d90e7b16
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 if Ekind (Scope (Designator)) = E_Protected_Type then
227 Error_Msg_N
228 ("abstract subprogram not allowed in protected type", N);
230 -- Issue a warning if the abstract subprogram is neither a dispatching
231 -- operation nor an operation that overrides an inherited subprogram or
232 -- predefined operator, since this most likely indicates a mistake.
234 elsif Warn_On_Redundant_Constructs
235 and then not Is_Dispatching_Operation (Designator)
236 and then not Present (Overridden_Operation (Designator))
237 and then (not Is_Operator_Symbol_Name (Chars (Designator))
238 or else Scop /= Scope (Etype (First_Formal (Designator))))
239 then
240 Error_Msg_N
241 ("abstract subprogram is not dispatching or overriding?r?", N);
242 end if;
244 Generate_Reference_To_Formals (Designator);
245 Check_Eliminated (Designator);
247 if Has_Aspects (N) then
248 Analyze_Aspect_Specifications (N, Designator);
249 end if;
250 end Analyze_Abstract_Subprogram_Declaration;
252 ---------------------------------
253 -- Analyze_Expression_Function --
254 ---------------------------------
256 procedure Analyze_Expression_Function (N : Node_Id) is
257 Loc : constant Source_Ptr := Sloc (N);
258 LocX : constant Source_Ptr := Sloc (Expression (N));
259 Expr : constant Node_Id := Expression (N);
260 Spec : constant Node_Id := Specification (N);
262 Def_Id : Entity_Id;
264 Prev : Entity_Id;
265 -- If the expression is a completion, Prev is the entity whose
266 -- declaration is completed. Def_Id is needed to analyze the spec.
268 New_Body : Node_Id;
269 New_Spec : Node_Id;
270 Ret : Node_Id;
272 begin
273 -- This is one of the occasions on which we transform the tree during
274 -- semantic analysis. If this is a completion, transform the expression
275 -- function into an equivalent subprogram body, and analyze it.
277 -- Expression functions are inlined unconditionally. The back-end will
278 -- determine whether this is possible.
280 Inline_Processing_Required := True;
282 -- Create a specification for the generated body. Types and defauts in
283 -- the profile are copies of the spec, but new entities must be created
284 -- for the unit name and the formals.
286 New_Spec := New_Copy_Tree (Spec);
287 Set_Defining_Unit_Name (New_Spec,
288 Make_Defining_Identifier (Sloc (Defining_Unit_Name (Spec)),
289 Chars (Defining_Unit_Name (Spec))));
291 if Present (Parameter_Specifications (New_Spec)) then
292 declare
293 Formal_Spec : Node_Id;
294 Def : Entity_Id;
296 begin
297 Formal_Spec := First (Parameter_Specifications (New_Spec));
299 -- Create a new formal parameter at the same source position
301 while Present (Formal_Spec) loop
302 Def := Defining_Identifier (Formal_Spec);
303 Set_Defining_Identifier (Formal_Spec,
304 Make_Defining_Identifier (Sloc (Def),
305 Chars => Chars (Def)));
306 Next (Formal_Spec);
307 end loop;
308 end;
309 end if;
311 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
313 -- If there are previous overloadable entities with the same name,
314 -- check whether any of them is completed by the expression function.
316 if Present (Prev) and then Is_Overloadable (Prev) then
317 Def_Id := Analyze_Subprogram_Specification (Spec);
318 Prev := Find_Corresponding_Spec (N);
319 end if;
321 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
323 New_Body :=
324 Make_Subprogram_Body (Loc,
325 Specification => New_Spec,
326 Declarations => Empty_List,
327 Handled_Statement_Sequence =>
328 Make_Handled_Sequence_Of_Statements (LocX,
329 Statements => New_List (Ret)));
331 -- If the expression completes a generic subprogram, we must create a
332 -- separate node for the body, because at instantiation the original
333 -- node of the generic copy must be a generic subprogram body, and
334 -- cannot be a expression function. Otherwise we just rewrite the
335 -- expression with the non-generic body.
337 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
338 Insert_After (N, New_Body);
340 -- Propagate any aspects or pragmas that apply to the expression
341 -- function to the proper body when the expression function acts
342 -- as a completion.
344 if Has_Aspects (N) then
345 Move_Aspects (N, To => New_Body);
346 end if;
348 Relocate_Pragmas_To_Body (New_Body);
350 Rewrite (N, Make_Null_Statement (Loc));
351 Set_Has_Completion (Prev, False);
352 Analyze (N);
353 Analyze (New_Body);
354 Set_Is_Inlined (Prev);
356 -- If the expression function is a completion, the previous declaration
357 -- must come from source. We know already that appears in the current
358 -- scope. The entity itself may be internally created if within a body
359 -- to be inlined.
361 elsif Present (Prev) and then Comes_From_Source (Parent (Prev)) then
362 Set_Has_Completion (Prev, False);
364 -- An expression function that is a completion freezes the
365 -- expression. This means freezing the return type, and if it is
366 -- an access type, freezing its designated type as well.
368 -- Note that we cannot defer this freezing to the analysis of the
369 -- expression itself, because a freeze node might appear in a nested
370 -- scope, leading to an elaboration order issue in gigi.
372 Freeze_Before (N, Etype (Prev));
374 if Is_Access_Type (Etype (Prev)) then
375 Freeze_Before (N, Designated_Type (Etype (Prev)));
376 end if;
378 -- For navigation purposes, indicate that the function is a body
380 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
381 Rewrite (N, New_Body);
383 -- Correct the parent pointer of the aspect specification list to
384 -- reference the rewritten node.
386 if Has_Aspects (N) then
387 Set_Parent (Aspect_Specifications (N), N);
388 end if;
390 -- Propagate any pragmas that apply to the expression function to the
391 -- proper body when the expression function acts as a completion.
392 -- Aspects are automatically transfered because of node rewriting.
394 Relocate_Pragmas_To_Body (N);
395 Analyze (N);
397 -- Prev is the previous entity with the same name, but it is can
398 -- be an unrelated spec that is not completed by the expression
399 -- function. In that case the relevant entity is the one in the body.
400 -- Not clear that the backend can inline it in this case ???
402 if Has_Completion (Prev) then
403 Set_Is_Inlined (Prev);
405 -- The formals of the expression function are body formals,
406 -- and do not appear in the ali file, which will only contain
407 -- references to the formals of the original subprogram spec.
409 declare
410 F1 : Entity_Id;
411 F2 : Entity_Id;
413 begin
414 F1 := First_Formal (Def_Id);
415 F2 := First_Formal (Prev);
417 while Present (F1) loop
418 Set_Spec_Entity (F1, F2);
419 Next_Formal (F1);
420 Next_Formal (F2);
421 end loop;
422 end;
424 else
425 Set_Is_Inlined (Defining_Entity (New_Body));
426 end if;
428 -- If this is not a completion, create both a declaration and a body, so
429 -- that the expression can be inlined whenever possible.
431 else
432 -- An expression function that is not a completion is not a
433 -- subprogram declaration, and thus cannot appear in a protected
434 -- definition.
436 if Nkind (Parent (N)) = N_Protected_Definition then
437 Error_Msg_N
438 ("an expression function is not a legal protected operation", N);
439 end if;
441 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
443 -- Correct the parent pointer of the aspect specification list to
444 -- reference the rewritten node.
446 if Has_Aspects (N) then
447 Set_Parent (Aspect_Specifications (N), N);
448 end if;
450 Analyze (N);
451 Set_Is_Inlined (Defining_Entity (N));
453 -- Establish the linkages between the spec and the body. These are
454 -- used when the expression function acts as the prefix of attribute
455 -- 'Access in order to freeze the original expression which has been
456 -- moved to the generated body.
458 Set_Corresponding_Body (N, Defining_Entity (New_Body));
459 Set_Corresponding_Spec (New_Body, Defining_Entity (N));
461 -- To prevent premature freeze action, insert the new body at the end
462 -- of the current declarations, or at the end of the package spec.
463 -- However, resolve usage names now, to prevent spurious visibility
464 -- on later entities. Note that the function can now be called in
465 -- the current declarative part, which will appear to be prior to
466 -- the presence of the body in the code. There are nevertheless no
467 -- order of elaboration issues because all name resolution has taken
468 -- place at the point of declaration.
470 declare
471 Decls : List_Id := List_Containing (N);
472 Par : constant Node_Id := Parent (Decls);
473 Id : constant Entity_Id := Defining_Entity (N);
475 begin
476 -- If this is a wrapper created for in an instance for a formal
477 -- subprogram, insert body after declaration, to be analyzed when
478 -- the enclosing instance is analyzed.
480 if GNATprove_Mode
481 and then Is_Generic_Actual_Subprogram (Defining_Entity (N))
482 then
483 Insert_After (N, New_Body);
485 else
486 if Nkind (Par) = N_Package_Specification
487 and then Decls = Visible_Declarations (Par)
488 and then Present (Private_Declarations (Par))
489 and then not Is_Empty_List (Private_Declarations (Par))
490 then
491 Decls := Private_Declarations (Par);
492 end if;
494 Insert_After (Last (Decls), New_Body);
495 Push_Scope (Id);
496 Install_Formals (Id);
498 -- Preanalyze the expression for name capture, except in an
499 -- instance, where this has been done during generic analysis,
500 -- and will be redone when analyzing the body.
502 declare
503 Expr : constant Node_Id := Expression (Ret);
505 begin
506 Set_Parent (Expr, Ret);
508 if not In_Instance then
509 Preanalyze_Spec_Expression (Expr, Etype (Id));
510 end if;
511 end;
513 End_Scope;
514 end if;
515 end;
516 end if;
518 -- If the return expression is a static constant, we suppress warning
519 -- messages on unused formals, which in most cases will be noise.
521 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
522 Is_OK_Static_Expression (Expr));
523 end Analyze_Expression_Function;
525 ----------------------------------------
526 -- Analyze_Extended_Return_Statement --
527 ----------------------------------------
529 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
530 begin
531 Check_Compiler_Unit ("extended return statement", N);
532 Analyze_Return_Statement (N);
533 end Analyze_Extended_Return_Statement;
535 ----------------------------
536 -- Analyze_Function_Call --
537 ----------------------------
539 procedure Analyze_Function_Call (N : Node_Id) is
540 Actuals : constant List_Id := Parameter_Associations (N);
541 Func_Nam : constant Node_Id := Name (N);
542 Actual : Node_Id;
544 begin
545 Analyze (Func_Nam);
547 -- A call of the form A.B (X) may be an Ada 2005 call, which is
548 -- rewritten as B (A, X). If the rewriting is successful, the call
549 -- has been analyzed and we just return.
551 if Nkind (Func_Nam) = N_Selected_Component
552 and then Name (N) /= Func_Nam
553 and then Is_Rewrite_Substitution (N)
554 and then Present (Etype (N))
555 then
556 return;
557 end if;
559 -- If error analyzing name, then set Any_Type as result type and return
561 if Etype (Func_Nam) = Any_Type then
562 Set_Etype (N, Any_Type);
563 return;
564 end if;
566 -- Otherwise analyze the parameters
568 if Present (Actuals) then
569 Actual := First (Actuals);
570 while Present (Actual) loop
571 Analyze (Actual);
572 Check_Parameterless_Call (Actual);
573 Next (Actual);
574 end loop;
575 end if;
577 Analyze_Call (N);
578 end Analyze_Function_Call;
580 -----------------------------
581 -- Analyze_Function_Return --
582 -----------------------------
584 procedure Analyze_Function_Return (N : Node_Id) is
585 Loc : constant Source_Ptr := Sloc (N);
586 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
587 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
589 R_Type : constant Entity_Id := Etype (Scope_Id);
590 -- Function result subtype
592 procedure Check_Limited_Return (Expr : Node_Id);
593 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
594 -- limited types. Used only for simple return statements.
595 -- Expr is the expression returned.
597 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
598 -- Check that the return_subtype_indication properly matches the result
599 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
601 --------------------------
602 -- Check_Limited_Return --
603 --------------------------
605 procedure Check_Limited_Return (Expr : Node_Id) is
606 begin
607 -- Ada 2005 (AI-318-02): Return-by-reference types have been
608 -- removed and replaced by anonymous access results. This is an
609 -- incompatibility with Ada 95. Not clear whether this should be
610 -- enforced yet or perhaps controllable with special switch. ???
612 -- A limited interface that is not immutably limited is OK.
614 if Is_Limited_Interface (R_Type)
615 and then
616 not (Is_Task_Interface (R_Type)
617 or else Is_Protected_Interface (R_Type)
618 or else Is_Synchronized_Interface (R_Type))
619 then
620 null;
622 elsif Is_Limited_Type (R_Type)
623 and then not Is_Interface (R_Type)
624 and then Comes_From_Source (N)
625 and then not In_Instance_Body
626 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
627 then
628 -- Error in Ada 2005
630 if Ada_Version >= Ada_2005
631 and then not Debug_Flag_Dot_L
632 and then not GNAT_Mode
633 then
634 Error_Msg_N
635 ("(Ada 2005) cannot copy object of a limited type "
636 & "(RM-2005 6.5(5.5/2))", Expr);
638 if Is_Limited_View (R_Type) then
639 Error_Msg_N
640 ("\return by reference not permitted in Ada 2005", Expr);
641 end if;
643 -- Warn in Ada 95 mode, to give folks a heads up about this
644 -- incompatibility.
646 -- In GNAT mode, this is just a warning, to allow it to be
647 -- evilly turned off. Otherwise it is a real error.
649 -- In a generic context, simplify the warning because it makes
650 -- no sense to discuss pass-by-reference or copy.
652 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
653 if Inside_A_Generic then
654 Error_Msg_N
655 ("return of limited object not permitted in Ada 2005 "
656 & "(RM-2005 6.5(5.5/2))?y?", Expr);
658 elsif Is_Limited_View (R_Type) then
659 Error_Msg_N
660 ("return by reference not permitted in Ada 2005 "
661 & "(RM-2005 6.5(5.5/2))?y?", Expr);
662 else
663 Error_Msg_N
664 ("cannot copy object of a limited type in Ada 2005 "
665 & "(RM-2005 6.5(5.5/2))?y?", Expr);
666 end if;
668 -- Ada 95 mode, compatibility warnings disabled
670 else
671 return; -- skip continuation messages below
672 end if;
674 if not Inside_A_Generic then
675 Error_Msg_N
676 ("\consider switching to return of access type", Expr);
677 Explain_Limited_Type (R_Type, Expr);
678 end if;
679 end if;
680 end Check_Limited_Return;
682 -------------------------------------
683 -- Check_Return_Subtype_Indication --
684 -------------------------------------
686 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
687 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
689 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
690 -- Subtype given in the extended return statement (must match R_Type)
692 Subtype_Ind : constant Node_Id :=
693 Object_Definition (Original_Node (Obj_Decl));
695 R_Type_Is_Anon_Access : constant Boolean :=
696 Ekind_In (R_Type,
697 E_Anonymous_Access_Subprogram_Type,
698 E_Anonymous_Access_Protected_Subprogram_Type,
699 E_Anonymous_Access_Type);
700 -- True if return type of the function is an anonymous access type
701 -- Can't we make Is_Anonymous_Access_Type in einfo ???
703 R_Stm_Type_Is_Anon_Access : constant Boolean :=
704 Ekind_In (R_Stm_Type,
705 E_Anonymous_Access_Subprogram_Type,
706 E_Anonymous_Access_Protected_Subprogram_Type,
707 E_Anonymous_Access_Type);
708 -- True if type of the return object is an anonymous access type
710 procedure Error_No_Match (N : Node_Id);
711 -- Output error messages for case where types do not statically
712 -- match. N is the location for the messages.
714 --------------------
715 -- Error_No_Match --
716 --------------------
718 procedure Error_No_Match (N : Node_Id) is
719 begin
720 Error_Msg_N
721 ("subtype must statically match function result subtype", N);
723 if not Predicates_Match (R_Stm_Type, R_Type) then
724 Error_Msg_Node_2 := R_Type;
725 Error_Msg_NE
726 ("\predicate of& does not match predicate of&",
727 N, R_Stm_Type);
728 end if;
729 end Error_No_Match;
731 -- Start of processing for Check_Return_Subtype_Indication
733 begin
734 -- First, avoid cascaded errors
736 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
737 return;
738 end if;
740 -- "return access T" case; check that the return statement also has
741 -- "access T", and that the subtypes statically match:
742 -- if this is an access to subprogram the signatures must match.
744 if R_Type_Is_Anon_Access then
745 if R_Stm_Type_Is_Anon_Access then
747 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
748 then
749 if Base_Type (Designated_Type (R_Stm_Type)) /=
750 Base_Type (Designated_Type (R_Type))
751 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
752 then
753 Error_No_Match (Subtype_Mark (Subtype_Ind));
754 end if;
756 else
757 -- For two anonymous access to subprogram types, the
758 -- types themselves must be type conformant.
760 if not Conforming_Types
761 (R_Stm_Type, R_Type, Fully_Conformant)
762 then
763 Error_No_Match (Subtype_Ind);
764 end if;
765 end if;
767 else
768 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
769 end if;
771 -- If the return object is of an anonymous access type, then report
772 -- an error if the function's result type is not also anonymous.
774 elsif R_Stm_Type_Is_Anon_Access
775 and then not R_Type_Is_Anon_Access
776 then
777 Error_Msg_N ("anonymous access not allowed for function with "
778 & "named access result", Subtype_Ind);
780 -- Subtype indication case: check that the return object's type is
781 -- covered by the result type, and that the subtypes statically match
782 -- when the result subtype is constrained. Also handle record types
783 -- with unknown discriminants for which we have built the underlying
784 -- record view. Coverage is needed to allow specific-type return
785 -- objects when the result type is class-wide (see AI05-32).
787 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
788 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
789 and then
790 Covers
791 (Base_Type (R_Type),
792 Underlying_Record_View (Base_Type (R_Stm_Type))))
793 then
794 -- A null exclusion may be present on the return type, on the
795 -- function specification, on the object declaration or on the
796 -- subtype itself.
798 if Is_Access_Type (R_Type)
799 and then
800 (Can_Never_Be_Null (R_Type)
801 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
802 Can_Never_Be_Null (R_Stm_Type)
803 then
804 Error_No_Match (Subtype_Ind);
805 end if;
807 -- AI05-103: for elementary types, subtypes must statically match
809 if Is_Constrained (R_Type)
810 or else Is_Access_Type (R_Type)
811 then
812 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
813 Error_No_Match (Subtype_Ind);
814 end if;
815 end if;
817 -- All remaining cases are illegal
819 -- Note: previous versions of this subprogram allowed the return
820 -- value to be the ancestor of the return type if the return type
821 -- was a null extension. This was plainly incorrect.
823 else
824 Error_Msg_N
825 ("wrong type for return_subtype_indication", Subtype_Ind);
826 end if;
827 end Check_Return_Subtype_Indication;
829 ---------------------
830 -- Local Variables --
831 ---------------------
833 Expr : Node_Id;
835 -- Start of processing for Analyze_Function_Return
837 begin
838 Set_Return_Present (Scope_Id);
840 if Nkind (N) = N_Simple_Return_Statement then
841 Expr := Expression (N);
843 -- Guard against a malformed expression. The parser may have tried to
844 -- recover but the node is not analyzable.
846 if Nkind (Expr) = N_Error then
847 Set_Etype (Expr, Any_Type);
848 Expander_Mode_Save_And_Set (False);
849 return;
851 else
852 -- The resolution of a controlled [extension] aggregate associated
853 -- with a return statement creates a temporary which needs to be
854 -- finalized on function exit. Wrap the return statement inside a
855 -- block so that the finalization machinery can detect this case.
856 -- This early expansion is done only when the return statement is
857 -- not part of a handled sequence of statements.
859 if Nkind_In (Expr, N_Aggregate,
860 N_Extension_Aggregate)
861 and then Needs_Finalization (R_Type)
862 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
863 then
864 Rewrite (N,
865 Make_Block_Statement (Loc,
866 Handled_Statement_Sequence =>
867 Make_Handled_Sequence_Of_Statements (Loc,
868 Statements => New_List (Relocate_Node (N)))));
870 Analyze (N);
871 return;
872 end if;
874 Analyze_And_Resolve (Expr, R_Type);
875 Check_Limited_Return (Expr);
876 end if;
878 -- RETURN only allowed in SPARK as the last statement in function
880 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
881 and then
882 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
883 or else Present (Next (N)))
884 then
885 Check_SPARK_05_Restriction
886 ("RETURN should be the last statement in function", N);
887 end if;
889 else
890 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
892 -- Analyze parts specific to extended_return_statement:
894 declare
895 Obj_Decl : constant Node_Id :=
896 Last (Return_Object_Declarations (N));
897 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
898 HSS : constant Node_Id := Handled_Statement_Sequence (N);
900 begin
901 Expr := Expression (Obj_Decl);
903 -- Note: The check for OK_For_Limited_Init will happen in
904 -- Analyze_Object_Declaration; we treat it as a normal
905 -- object declaration.
907 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
908 Analyze (Obj_Decl);
910 Check_Return_Subtype_Indication (Obj_Decl);
912 if Present (HSS) then
913 Analyze (HSS);
915 if Present (Exception_Handlers (HSS)) then
917 -- ???Has_Nested_Block_With_Handler needs to be set.
918 -- Probably by creating an actual N_Block_Statement.
919 -- Probably in Expand.
921 null;
922 end if;
923 end if;
925 -- Mark the return object as referenced, since the return is an
926 -- implicit reference of the object.
928 Set_Referenced (Defining_Identifier (Obj_Decl));
930 Check_References (Stm_Entity);
932 -- Check RM 6.5 (5.9/3)
934 if Has_Aliased then
935 if Ada_Version < Ada_2012 then
937 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
938 -- Can it really happen (extended return???)
940 Error_Msg_N
941 ("aliased only allowed for limited return objects "
942 & "in Ada 2012??", N);
944 elsif not Is_Limited_View (R_Type) then
945 Error_Msg_N
946 ("aliased only allowed for limited return objects", N);
947 end if;
948 end if;
949 end;
950 end if;
952 -- Case of Expr present
954 if Present (Expr)
956 -- Defend against previous errors
958 and then Nkind (Expr) /= N_Empty
959 and then Present (Etype (Expr))
960 then
961 -- Apply constraint check. Note that this is done before the implicit
962 -- conversion of the expression done for anonymous access types to
963 -- ensure correct generation of the null-excluding check associated
964 -- with null-excluding expressions found in return statements.
966 Apply_Constraint_Check (Expr, R_Type);
968 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
969 -- type, apply an implicit conversion of the expression to that type
970 -- to force appropriate static and run-time accessibility checks.
972 if Ada_Version >= Ada_2005
973 and then Ekind (R_Type) = E_Anonymous_Access_Type
974 then
975 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
976 Analyze_And_Resolve (Expr, R_Type);
978 -- If this is a local anonymous access to subprogram, the
979 -- accessibility check can be applied statically. The return is
980 -- illegal if the access type of the return expression is declared
981 -- inside of the subprogram (except if it is the subtype indication
982 -- of an extended return statement).
984 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
985 if not Comes_From_Source (Current_Scope)
986 or else Ekind (Current_Scope) = E_Return_Statement
987 then
988 null;
990 elsif
991 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
992 then
993 Error_Msg_N ("cannot return local access to subprogram", N);
994 end if;
996 -- The expression cannot be of a formal incomplete type
998 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
999 and then Is_Generic_Type (Etype (Expr))
1000 then
1001 Error_Msg_N
1002 ("cannot return expression of a formal incomplete type", N);
1003 end if;
1005 -- If the result type is class-wide, then check that the return
1006 -- expression's type is not declared at a deeper level than the
1007 -- function (RM05-6.5(5.6/2)).
1009 if Ada_Version >= Ada_2005
1010 and then Is_Class_Wide_Type (R_Type)
1011 then
1012 if Type_Access_Level (Etype (Expr)) >
1013 Subprogram_Access_Level (Scope_Id)
1014 then
1015 Error_Msg_N
1016 ("level of return expression type is deeper than "
1017 & "class-wide function!", Expr);
1018 end if;
1019 end if;
1021 -- Check incorrect use of dynamically tagged expression
1023 if Is_Tagged_Type (R_Type) then
1024 Check_Dynamically_Tagged_Expression
1025 (Expr => Expr,
1026 Typ => R_Type,
1027 Related_Nod => N);
1028 end if;
1030 -- ??? A real run-time accessibility check is needed in cases
1031 -- involving dereferences of access parameters. For now we just
1032 -- check the static cases.
1034 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1035 and then Is_Limited_View (Etype (Scope_Id))
1036 and then Object_Access_Level (Expr) >
1037 Subprogram_Access_Level (Scope_Id)
1038 then
1039 -- Suppress the message in a generic, where the rewriting
1040 -- is irrelevant.
1042 if Inside_A_Generic then
1043 null;
1045 else
1046 Rewrite (N,
1047 Make_Raise_Program_Error (Loc,
1048 Reason => PE_Accessibility_Check_Failed));
1049 Analyze (N);
1051 Error_Msg_Warn := SPARK_Mode /= On;
1052 Error_Msg_N ("cannot return a local value by reference<<", N);
1053 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1054 end if;
1055 end if;
1057 if Known_Null (Expr)
1058 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1059 and then Null_Exclusion_Present (Parent (Scope_Id))
1060 then
1061 Apply_Compile_Time_Constraint_Error
1062 (N => Expr,
1063 Msg => "(Ada 2005) null not allowed for "
1064 & "null-excluding return??",
1065 Reason => CE_Null_Not_Allowed);
1066 end if;
1067 end if;
1068 end Analyze_Function_Return;
1070 -------------------------------------
1071 -- Analyze_Generic_Subprogram_Body --
1072 -------------------------------------
1074 procedure Analyze_Generic_Subprogram_Body
1075 (N : Node_Id;
1076 Gen_Id : Entity_Id)
1078 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1079 Kind : constant Entity_Kind := Ekind (Gen_Id);
1080 Body_Id : Entity_Id;
1081 New_N : Node_Id;
1082 Spec : Node_Id;
1084 begin
1085 -- Copy body and disable expansion while analyzing the generic For a
1086 -- stub, do not copy the stub (which would load the proper body), this
1087 -- will be done when the proper body is analyzed.
1089 if Nkind (N) /= N_Subprogram_Body_Stub then
1090 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1091 Rewrite (N, New_N);
1092 Start_Generic;
1093 end if;
1095 Spec := Specification (N);
1097 -- Within the body of the generic, the subprogram is callable, and
1098 -- behaves like the corresponding non-generic unit.
1100 Body_Id := Defining_Entity (Spec);
1102 if Kind = E_Generic_Procedure
1103 and then Nkind (Spec) /= N_Procedure_Specification
1104 then
1105 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1106 return;
1108 elsif Kind = E_Generic_Function
1109 and then Nkind (Spec) /= N_Function_Specification
1110 then
1111 Error_Msg_N ("invalid body for generic function ", Body_Id);
1112 return;
1113 end if;
1115 Set_Corresponding_Body (Gen_Decl, Body_Id);
1117 if Has_Completion (Gen_Id)
1118 and then Nkind (Parent (N)) /= N_Subunit
1119 then
1120 Error_Msg_N ("duplicate generic body", N);
1121 return;
1122 else
1123 Set_Has_Completion (Gen_Id);
1124 end if;
1126 if Nkind (N) = N_Subprogram_Body_Stub then
1127 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1128 else
1129 Set_Corresponding_Spec (N, Gen_Id);
1130 end if;
1132 if Nkind (Parent (N)) = N_Compilation_Unit then
1133 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1134 end if;
1136 -- Make generic parameters immediately visible in the body. They are
1137 -- needed to process the formals declarations. Then make the formals
1138 -- visible in a separate step.
1140 Push_Scope (Gen_Id);
1142 declare
1143 E : Entity_Id;
1144 First_Ent : Entity_Id;
1146 begin
1147 First_Ent := First_Entity (Gen_Id);
1149 E := First_Ent;
1150 while Present (E) and then not Is_Formal (E) loop
1151 Install_Entity (E);
1152 Next_Entity (E);
1153 end loop;
1155 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1157 -- Now generic formals are visible, and the specification can be
1158 -- analyzed, for subsequent conformance check.
1160 Body_Id := Analyze_Subprogram_Specification (Spec);
1162 -- Make formal parameters visible
1164 if Present (E) then
1166 -- E is the first formal parameter, we loop through the formals
1167 -- installing them so that they will be visible.
1169 Set_First_Entity (Gen_Id, E);
1170 while Present (E) loop
1171 Install_Entity (E);
1172 Next_Formal (E);
1173 end loop;
1174 end if;
1176 -- Visible generic entity is callable within its own body
1178 Set_Ekind (Gen_Id, Ekind (Body_Id));
1179 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
1180 Set_Ekind (Body_Id, E_Subprogram_Body);
1181 Set_Convention (Body_Id, Convention (Gen_Id));
1182 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1183 Set_Scope (Body_Id, Scope (Gen_Id));
1184 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1186 if Nkind (N) = N_Subprogram_Body_Stub then
1188 -- No body to analyze, so restore state of generic unit
1190 Set_Ekind (Gen_Id, Kind);
1191 Set_Ekind (Body_Id, Kind);
1193 if Present (First_Ent) then
1194 Set_First_Entity (Gen_Id, First_Ent);
1195 end if;
1197 End_Scope;
1198 return;
1199 end if;
1201 -- If this is a compilation unit, it must be made visible explicitly,
1202 -- because the compilation of the declaration, unlike other library
1203 -- unit declarations, does not. If it is not a unit, the following
1204 -- is redundant but harmless.
1206 Set_Is_Immediately_Visible (Gen_Id);
1207 Reference_Body_Formals (Gen_Id, Body_Id);
1209 if Is_Child_Unit (Gen_Id) then
1210 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1211 end if;
1213 Set_Actual_Subtypes (N, Current_Scope);
1215 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
1216 -- invariants and predicates associated with the body and its spec.
1217 -- Note that this is not pure expansion as Expand_Subprogram_Contract
1218 -- prepares the contract assertions for generic subprograms or for
1219 -- ASIS. Do not generate contract checks in SPARK mode.
1221 if not GNATprove_Mode then
1222 Expand_Subprogram_Contract (N, Gen_Id, Body_Id);
1223 end if;
1225 -- If the generic unit carries pre- or post-conditions, copy them
1226 -- to the original generic tree, so that they are properly added
1227 -- to any instantiation.
1229 declare
1230 Orig : constant Node_Id := Original_Node (N);
1231 Cond : Node_Id;
1233 begin
1234 Cond := First (Declarations (N));
1235 while Present (Cond) loop
1236 if Nkind (Cond) = N_Pragma
1237 and then Pragma_Name (Cond) = Name_Check
1238 then
1239 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1241 elsif Nkind (Cond) = N_Pragma
1242 and then Pragma_Name (Cond) = Name_Postcondition
1243 then
1244 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
1245 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1246 else
1247 exit;
1248 end if;
1250 Next (Cond);
1251 end loop;
1252 end;
1254 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1255 Set_SPARK_Pragma_Inherited (Body_Id, True);
1257 Analyze_Declarations (Declarations (N));
1258 Check_Completion;
1259 Analyze (Handled_Statement_Sequence (N));
1261 Save_Global_References (Original_Node (N));
1263 -- Prior to exiting the scope, include generic formals again (if any
1264 -- are present) in the set of local entities.
1266 if Present (First_Ent) then
1267 Set_First_Entity (Gen_Id, First_Ent);
1268 end if;
1270 Check_References (Gen_Id);
1271 end;
1273 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1274 End_Scope;
1275 Check_Subprogram_Order (N);
1277 -- Outside of its body, unit is generic again
1279 Set_Ekind (Gen_Id, Kind);
1280 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1282 if Style_Check then
1283 Style.Check_Identifier (Body_Id, Gen_Id);
1284 end if;
1286 End_Generic;
1287 end Analyze_Generic_Subprogram_Body;
1289 ----------------------------
1290 -- Analyze_Null_Procedure --
1291 ----------------------------
1293 procedure Analyze_Null_Procedure
1294 (N : Node_Id;
1295 Is_Completion : out Boolean)
1297 Loc : constant Source_Ptr := Sloc (N);
1298 Spec : constant Node_Id := Specification (N);
1299 Designator : Entity_Id;
1300 Form : Node_Id;
1301 Null_Body : Node_Id := Empty;
1302 Prev : Entity_Id;
1304 begin
1305 -- Capture the profile of the null procedure before analysis, for
1306 -- expansion at the freeze point and at each point of call. The body is
1307 -- used if the procedure has preconditions, or if it is a completion. In
1308 -- the first case the body is analyzed at the freeze point, in the other
1309 -- it replaces the null procedure declaration.
1311 Null_Body :=
1312 Make_Subprogram_Body (Loc,
1313 Specification => New_Copy_Tree (Spec),
1314 Declarations => New_List,
1315 Handled_Statement_Sequence =>
1316 Make_Handled_Sequence_Of_Statements (Loc,
1317 Statements => New_List (Make_Null_Statement (Loc))));
1319 -- Create new entities for body and formals
1321 Set_Defining_Unit_Name (Specification (Null_Body),
1322 Make_Defining_Identifier
1323 (Sloc (Defining_Entity (N)),
1324 Chars (Defining_Entity (N))));
1326 Form := First (Parameter_Specifications (Specification (Null_Body)));
1327 while Present (Form) loop
1328 Set_Defining_Identifier (Form,
1329 Make_Defining_Identifier
1330 (Sloc (Defining_Identifier (Form)),
1331 Chars (Defining_Identifier (Form))));
1332 Next (Form);
1333 end loop;
1335 -- Determine whether the null procedure may be a completion of a generic
1336 -- suprogram, in which case we use the new null body as the completion
1337 -- and set minimal semantic information on the original declaration,
1338 -- which is rewritten as a null statement.
1340 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1342 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1343 Insert_Before (N, Null_Body);
1344 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1345 Set_Contract (Defining_Entity (N), Make_Contract (Loc));
1347 Rewrite (N, Make_Null_Statement (Loc));
1348 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1349 Is_Completion := True;
1350 return;
1352 else
1353 -- Resolve the types of the formals now, because the freeze point
1354 -- may appear in a different context, e.g. an instantiation.
1356 Form := First (Parameter_Specifications (Specification (Null_Body)));
1357 while Present (Form) loop
1358 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1359 Find_Type (Parameter_Type (Form));
1361 elsif
1362 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1363 then
1364 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1366 else
1367 -- The case of a null procedure with a formal that is an
1368 -- access_to_subprogram type, and that is used as an actual
1369 -- in an instantiation is left to the enthusiastic reader.
1371 null;
1372 end if;
1374 Next (Form);
1375 end loop;
1376 end if;
1378 -- If there are previous overloadable entities with the same name,
1379 -- check whether any of them is completed by the null procedure.
1381 if Present (Prev) and then Is_Overloadable (Prev) then
1382 Designator := Analyze_Subprogram_Specification (Spec);
1383 Prev := Find_Corresponding_Spec (N);
1384 end if;
1386 if No (Prev) or else not Comes_From_Source (Prev) then
1387 Designator := Analyze_Subprogram_Specification (Spec);
1388 Set_Has_Completion (Designator);
1390 -- Signal to caller that this is a procedure declaration
1392 Is_Completion := False;
1394 -- Null procedures are always inlined, but generic formal subprograms
1395 -- which appear as such in the internal instance of formal packages,
1396 -- need no completion and are not marked Inline.
1398 if Expander_Active
1399 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1400 then
1401 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1402 Set_Body_To_Inline (N, Null_Body);
1403 Set_Is_Inlined (Designator);
1404 end if;
1406 else
1407 -- The null procedure is a completion. We unconditionally rewrite
1408 -- this as a null body (even if expansion is not active), because
1409 -- there are various error checks that are applied on this body
1410 -- when it is analyzed (e.g. correct aspect placement).
1412 Is_Completion := True;
1413 Rewrite (N, Null_Body);
1414 Analyze (N);
1415 end if;
1416 end Analyze_Null_Procedure;
1418 -----------------------------
1419 -- Analyze_Operator_Symbol --
1420 -----------------------------
1422 -- An operator symbol such as "+" or "and" may appear in context where the
1423 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1424 -- is just a string, as in (conjunction = "or"). In these cases the parser
1425 -- generates this node, and the semantics does the disambiguation. Other
1426 -- such case are actuals in an instantiation, the generic unit in an
1427 -- instantiation, and pragma arguments.
1429 procedure Analyze_Operator_Symbol (N : Node_Id) is
1430 Par : constant Node_Id := Parent (N);
1432 begin
1433 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1434 or else Nkind (Par) = N_Function_Instantiation
1435 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1436 or else (Nkind (Par) = N_Pragma_Argument_Association
1437 and then not Is_Pragma_String_Literal (Par))
1438 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1439 or else (Nkind (Par) = N_Attribute_Reference
1440 and then Attribute_Name (Par) /= Name_Value)
1441 then
1442 Find_Direct_Name (N);
1444 else
1445 Change_Operator_Symbol_To_String_Literal (N);
1446 Analyze (N);
1447 end if;
1448 end Analyze_Operator_Symbol;
1450 -----------------------------------
1451 -- Analyze_Parameter_Association --
1452 -----------------------------------
1454 procedure Analyze_Parameter_Association (N : Node_Id) is
1455 begin
1456 Analyze (Explicit_Actual_Parameter (N));
1457 end Analyze_Parameter_Association;
1459 ----------------------------
1460 -- Analyze_Procedure_Call --
1461 ----------------------------
1463 procedure Analyze_Procedure_Call (N : Node_Id) is
1464 Loc : constant Source_Ptr := Sloc (N);
1465 P : constant Node_Id := Name (N);
1466 Actuals : constant List_Id := Parameter_Associations (N);
1467 Actual : Node_Id;
1468 New_N : Node_Id;
1470 procedure Analyze_Call_And_Resolve;
1471 -- Do Analyze and Resolve calls for procedure call
1472 -- At end, check illegal order dependence.
1474 ------------------------------
1475 -- Analyze_Call_And_Resolve --
1476 ------------------------------
1478 procedure Analyze_Call_And_Resolve is
1479 begin
1480 if Nkind (N) = N_Procedure_Call_Statement then
1481 Analyze_Call (N);
1482 Resolve (N, Standard_Void_Type);
1483 else
1484 Analyze (N);
1485 end if;
1486 end Analyze_Call_And_Resolve;
1488 -- Start of processing for Analyze_Procedure_Call
1490 begin
1491 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1492 -- a procedure call or an entry call. The prefix may denote an access
1493 -- to subprogram type, in which case an implicit dereference applies.
1494 -- If the prefix is an indexed component (without implicit dereference)
1495 -- then the construct denotes a call to a member of an entire family.
1496 -- If the prefix is a simple name, it may still denote a call to a
1497 -- parameterless member of an entry family. Resolution of these various
1498 -- interpretations is delicate.
1500 Analyze (P);
1502 -- If this is a call of the form Obj.Op, the call may have been
1503 -- analyzed and possibly rewritten into a block, in which case
1504 -- we are done.
1506 if Analyzed (N) then
1507 return;
1508 end if;
1510 -- If there is an error analyzing the name (which may have been
1511 -- rewritten if the original call was in prefix notation) then error
1512 -- has been emitted already, mark node and return.
1514 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1515 Set_Etype (N, Any_Type);
1516 return;
1517 end if;
1519 -- Otherwise analyze the parameters
1521 if Present (Actuals) then
1522 Actual := First (Actuals);
1524 while Present (Actual) loop
1525 Analyze (Actual);
1526 Check_Parameterless_Call (Actual);
1527 Next (Actual);
1528 end loop;
1529 end if;
1531 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1533 if Nkind (P) = N_Attribute_Reference
1534 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1535 Name_Elab_Body,
1536 Name_Elab_Subp_Body)
1537 then
1538 if Present (Actuals) then
1539 Error_Msg_N
1540 ("no parameters allowed for this call", First (Actuals));
1541 return;
1542 end if;
1544 Set_Etype (N, Standard_Void_Type);
1545 Set_Analyzed (N);
1547 elsif Is_Entity_Name (P)
1548 and then Is_Record_Type (Etype (Entity (P)))
1549 and then Remote_AST_I_Dereference (P)
1550 then
1551 return;
1553 elsif Is_Entity_Name (P)
1554 and then Ekind (Entity (P)) /= E_Entry_Family
1555 then
1556 if Is_Access_Type (Etype (P))
1557 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1558 and then No (Actuals)
1559 and then Comes_From_Source (N)
1560 then
1561 Error_Msg_N ("missing explicit dereference in call", N);
1562 end if;
1564 Analyze_Call_And_Resolve;
1566 -- If the prefix is the simple name of an entry family, this is
1567 -- a parameterless call from within the task body itself.
1569 elsif Is_Entity_Name (P)
1570 and then Nkind (P) = N_Identifier
1571 and then Ekind (Entity (P)) = E_Entry_Family
1572 and then Present (Actuals)
1573 and then No (Next (First (Actuals)))
1574 then
1575 -- Can be call to parameterless entry family. What appears to be the
1576 -- sole argument is in fact the entry index. Rewrite prefix of node
1577 -- accordingly. Source representation is unchanged by this
1578 -- transformation.
1580 New_N :=
1581 Make_Indexed_Component (Loc,
1582 Prefix =>
1583 Make_Selected_Component (Loc,
1584 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1585 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1586 Expressions => Actuals);
1587 Set_Name (N, New_N);
1588 Set_Etype (New_N, Standard_Void_Type);
1589 Set_Parameter_Associations (N, No_List);
1590 Analyze_Call_And_Resolve;
1592 elsif Nkind (P) = N_Explicit_Dereference then
1593 if Ekind (Etype (P)) = E_Subprogram_Type then
1594 Analyze_Call_And_Resolve;
1595 else
1596 Error_Msg_N ("expect access to procedure in call", P);
1597 end if;
1599 -- The name can be a selected component or an indexed component that
1600 -- yields an access to subprogram. Such a prefix is legal if the call
1601 -- has parameter associations.
1603 elsif Is_Access_Type (Etype (P))
1604 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1605 then
1606 if Present (Actuals) then
1607 Analyze_Call_And_Resolve;
1608 else
1609 Error_Msg_N ("missing explicit dereference in call ", N);
1610 end if;
1612 -- If not an access to subprogram, then the prefix must resolve to the
1613 -- name of an entry, entry family, or protected operation.
1615 -- For the case of a simple entry call, P is a selected component where
1616 -- the prefix is the task and the selector name is the entry. A call to
1617 -- a protected procedure will have the same syntax. If the protected
1618 -- object contains overloaded operations, the entity may appear as a
1619 -- function, the context will select the operation whose type is Void.
1621 elsif Nkind (P) = N_Selected_Component
1622 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1623 E_Procedure,
1624 E_Function)
1625 then
1626 Analyze_Call_And_Resolve;
1628 elsif Nkind (P) = N_Selected_Component
1629 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1630 and then Present (Actuals)
1631 and then No (Next (First (Actuals)))
1632 then
1633 -- Can be call to parameterless entry family. What appears to be the
1634 -- sole argument is in fact the entry index. Rewrite prefix of node
1635 -- accordingly. Source representation is unchanged by this
1636 -- transformation.
1638 New_N :=
1639 Make_Indexed_Component (Loc,
1640 Prefix => New_Copy (P),
1641 Expressions => Actuals);
1642 Set_Name (N, New_N);
1643 Set_Etype (New_N, Standard_Void_Type);
1644 Set_Parameter_Associations (N, No_List);
1645 Analyze_Call_And_Resolve;
1647 -- For the case of a reference to an element of an entry family, P is
1648 -- an indexed component whose prefix is a selected component (task and
1649 -- entry family), and whose index is the entry family index.
1651 elsif Nkind (P) = N_Indexed_Component
1652 and then Nkind (Prefix (P)) = N_Selected_Component
1653 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1654 then
1655 Analyze_Call_And_Resolve;
1657 -- If the prefix is the name of an entry family, it is a call from
1658 -- within the task body itself.
1660 elsif Nkind (P) = N_Indexed_Component
1661 and then Nkind (Prefix (P)) = N_Identifier
1662 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1663 then
1664 New_N :=
1665 Make_Selected_Component (Loc,
1666 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1667 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1668 Rewrite (Prefix (P), New_N);
1669 Analyze (P);
1670 Analyze_Call_And_Resolve;
1672 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1673 -- procedure name, so the construct can only be a qualified expression.
1675 elsif Nkind (P) = N_Qualified_Expression
1676 and then Ada_Version >= Ada_2012
1677 then
1678 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1679 Analyze (N);
1681 -- Anything else is an error
1683 else
1684 Error_Msg_N ("invalid procedure or entry call", N);
1685 end if;
1686 end Analyze_Procedure_Call;
1688 ------------------------------
1689 -- Analyze_Return_Statement --
1690 ------------------------------
1692 procedure Analyze_Return_Statement (N : Node_Id) is
1694 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1695 N_Extended_Return_Statement));
1697 Returns_Object : constant Boolean :=
1698 Nkind (N) = N_Extended_Return_Statement
1699 or else
1700 (Nkind (N) = N_Simple_Return_Statement
1701 and then Present (Expression (N)));
1702 -- True if we're returning something; that is, "return <expression>;"
1703 -- or "return Result : T [:= ...]". False for "return;". Used for error
1704 -- checking: If Returns_Object is True, N should apply to a function
1705 -- body; otherwise N should apply to a procedure body, entry body,
1706 -- accept statement, or extended return statement.
1708 function Find_What_It_Applies_To return Entity_Id;
1709 -- Find the entity representing the innermost enclosing body, accept
1710 -- statement, or extended return statement. If the result is a callable
1711 -- construct or extended return statement, then this will be the value
1712 -- of the Return_Applies_To attribute. Otherwise, the program is
1713 -- illegal. See RM-6.5(4/2).
1715 -----------------------------
1716 -- Find_What_It_Applies_To --
1717 -----------------------------
1719 function Find_What_It_Applies_To return Entity_Id is
1720 Result : Entity_Id := Empty;
1722 begin
1723 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1724 -- and postconditions.
1726 for J in reverse 0 .. Scope_Stack.Last loop
1727 Result := Scope_Stack.Table (J).Entity;
1728 exit when not Ekind_In (Result, E_Block, E_Loop)
1729 and then Chars (Result) /= Name_uPostconditions;
1730 end loop;
1732 pragma Assert (Present (Result));
1733 return Result;
1734 end Find_What_It_Applies_To;
1736 -- Local declarations
1738 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1739 Kind : constant Entity_Kind := Ekind (Scope_Id);
1740 Loc : constant Source_Ptr := Sloc (N);
1741 Stm_Entity : constant Entity_Id :=
1742 New_Internal_Entity
1743 (E_Return_Statement, Current_Scope, Loc, 'R');
1745 -- Start of processing for Analyze_Return_Statement
1747 begin
1748 Set_Return_Statement_Entity (N, Stm_Entity);
1750 Set_Etype (Stm_Entity, Standard_Void_Type);
1751 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1753 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1754 -- (4/2): an inner return statement will apply to this extended return.
1756 if Nkind (N) = N_Extended_Return_Statement then
1757 Push_Scope (Stm_Entity);
1758 end if;
1760 -- Check that pragma No_Return is obeyed. Don't complain about the
1761 -- implicitly-generated return that is placed at the end.
1763 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1764 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1765 end if;
1767 -- Warn on any unassigned OUT parameters if in procedure
1769 if Ekind (Scope_Id) = E_Procedure then
1770 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1771 end if;
1773 -- Check that functions return objects, and other things do not
1775 if Kind = E_Function or else Kind = E_Generic_Function then
1776 if not Returns_Object then
1777 Error_Msg_N ("missing expression in return from function", N);
1778 end if;
1780 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1781 if Returns_Object then
1782 Error_Msg_N ("procedure cannot return value (use function)", N);
1783 end if;
1785 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1786 if Returns_Object then
1787 if Is_Protected_Type (Scope (Scope_Id)) then
1788 Error_Msg_N ("entry body cannot return value", N);
1789 else
1790 Error_Msg_N ("accept statement cannot return value", N);
1791 end if;
1792 end if;
1794 elsif Kind = E_Return_Statement then
1796 -- We are nested within another return statement, which must be an
1797 -- extended_return_statement.
1799 if Returns_Object then
1800 if Nkind (N) = N_Extended_Return_Statement then
1801 Error_Msg_N
1802 ("extended return statement cannot be nested (use `RETURN;`)",
1805 -- Case of a simple return statement with a value inside extended
1806 -- return statement.
1808 else
1809 Error_Msg_N
1810 ("return nested in extended return statement cannot return "
1811 & "value (use `RETURN;`)", N);
1812 end if;
1813 end if;
1815 else
1816 Error_Msg_N ("illegal context for return statement", N);
1817 end if;
1819 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1820 Analyze_Function_Return (N);
1822 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1823 Set_Return_Present (Scope_Id);
1824 end if;
1826 if Nkind (N) = N_Extended_Return_Statement then
1827 End_Scope;
1828 end if;
1830 Kill_Current_Values (Last_Assignment_Only => True);
1831 Check_Unreachable_Code (N);
1833 Analyze_Dimension (N);
1834 end Analyze_Return_Statement;
1836 -------------------------------------
1837 -- Analyze_Simple_Return_Statement --
1838 -------------------------------------
1840 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1841 begin
1842 if Present (Expression (N)) then
1843 Mark_Coextensions (N, Expression (N));
1844 end if;
1846 Analyze_Return_Statement (N);
1847 end Analyze_Simple_Return_Statement;
1849 -------------------------
1850 -- Analyze_Return_Type --
1851 -------------------------
1853 procedure Analyze_Return_Type (N : Node_Id) is
1854 Designator : constant Entity_Id := Defining_Entity (N);
1855 Typ : Entity_Id := Empty;
1857 begin
1858 -- Normal case where result definition does not indicate an error
1860 if Result_Definition (N) /= Error then
1861 if Nkind (Result_Definition (N)) = N_Access_Definition then
1862 Check_SPARK_05_Restriction
1863 ("access result is not allowed", Result_Definition (N));
1865 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1867 declare
1868 AD : constant Node_Id :=
1869 Access_To_Subprogram_Definition (Result_Definition (N));
1870 begin
1871 if Present (AD) and then Protected_Present (AD) then
1872 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1873 else
1874 Typ := Access_Definition (N, Result_Definition (N));
1875 end if;
1876 end;
1878 Set_Parent (Typ, Result_Definition (N));
1879 Set_Is_Local_Anonymous_Access (Typ);
1880 Set_Etype (Designator, Typ);
1882 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1884 Null_Exclusion_Static_Checks (N);
1886 -- Subtype_Mark case
1888 else
1889 Find_Type (Result_Definition (N));
1890 Typ := Entity (Result_Definition (N));
1891 Set_Etype (Designator, Typ);
1893 -- Unconstrained array as result is not allowed in SPARK
1895 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1896 Check_SPARK_05_Restriction
1897 ("returning an unconstrained array is not allowed",
1898 Result_Definition (N));
1899 end if;
1901 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1903 Null_Exclusion_Static_Checks (N);
1905 -- If a null exclusion is imposed on the result type, then create
1906 -- a null-excluding itype (an access subtype) and use it as the
1907 -- function's Etype. Note that the null exclusion checks are done
1908 -- right before this, because they don't get applied to types that
1909 -- do not come from source.
1911 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
1912 Set_Etype (Designator,
1913 Create_Null_Excluding_Itype
1914 (T => Typ,
1915 Related_Nod => N,
1916 Scope_Id => Scope (Current_Scope)));
1918 -- The new subtype must be elaborated before use because
1919 -- it is visible outside of the function. However its base
1920 -- type may not be frozen yet, so the reference that will
1921 -- force elaboration must be attached to the freezing of
1922 -- the base type.
1924 -- If the return specification appears on a proper body,
1925 -- the subtype will have been created already on the spec.
1927 if Is_Frozen (Typ) then
1928 if Nkind (Parent (N)) = N_Subprogram_Body
1929 and then Nkind (Parent (Parent (N))) = N_Subunit
1930 then
1931 null;
1932 else
1933 Build_Itype_Reference (Etype (Designator), Parent (N));
1934 end if;
1936 else
1937 Ensure_Freeze_Node (Typ);
1939 declare
1940 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1941 begin
1942 Set_Itype (IR, Etype (Designator));
1943 Append_Freeze_Actions (Typ, New_List (IR));
1944 end;
1945 end if;
1947 else
1948 Set_Etype (Designator, Typ);
1949 end if;
1951 if Ekind (Typ) = E_Incomplete_Type
1952 and then Is_Value_Type (Typ)
1953 then
1954 null;
1956 elsif Ekind (Typ) = E_Incomplete_Type
1957 or else (Is_Class_Wide_Type (Typ)
1958 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1959 then
1960 -- AI05-0151: Tagged incomplete types are allowed in all formal
1961 -- parts. Untagged incomplete types are not allowed in bodies.
1962 -- As a consequence, limited views cannot appear in a basic
1963 -- declaration that is itself within a body, because there is
1964 -- no point at which the non-limited view will become visible.
1966 if Ada_Version >= Ada_2012 then
1967 if From_Limited_With (Typ) and then In_Package_Body then
1968 Error_Msg_NE
1969 ("invalid use of incomplete type&",
1970 Result_Definition (N), Typ);
1972 -- The return type of a subprogram body cannot be of a
1973 -- formal incomplete type.
1975 elsif Is_Generic_Type (Typ)
1976 and then Nkind (Parent (N)) = N_Subprogram_Body
1977 then
1978 Error_Msg_N
1979 ("return type cannot be a formal incomplete type",
1980 Result_Definition (N));
1982 elsif Is_Class_Wide_Type (Typ)
1983 and then Is_Generic_Type (Root_Type (Typ))
1984 and then Nkind (Parent (N)) = N_Subprogram_Body
1985 then
1986 Error_Msg_N
1987 ("return type cannot be a formal incomplete type",
1988 Result_Definition (N));
1990 elsif Is_Tagged_Type (Typ) then
1991 null;
1993 elsif Nkind (Parent (N)) = N_Subprogram_Body
1994 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
1995 N_Entry_Body)
1996 then
1997 Error_Msg_NE
1998 ("invalid use of untagged incomplete type&",
1999 Designator, Typ);
2000 end if;
2002 -- The type must be completed in the current package. This
2003 -- is checked at the end of the package declaration when
2004 -- Taft-amendment types are identified. If the return type
2005 -- is class-wide, there is no required check, the type can
2006 -- be a bona fide TAT.
2008 if Ekind (Scope (Current_Scope)) = E_Package
2009 and then In_Private_Part (Scope (Current_Scope))
2010 and then not Is_Class_Wide_Type (Typ)
2011 then
2012 Append_Elmt (Designator, Private_Dependents (Typ));
2013 end if;
2015 else
2016 Error_Msg_NE
2017 ("invalid use of incomplete type&", Designator, Typ);
2018 end if;
2019 end if;
2020 end if;
2022 -- Case where result definition does indicate an error
2024 else
2025 Set_Etype (Designator, Any_Type);
2026 end if;
2027 end Analyze_Return_Type;
2029 -----------------------------
2030 -- Analyze_Subprogram_Body --
2031 -----------------------------
2033 procedure Analyze_Subprogram_Body (N : Node_Id) is
2034 Loc : constant Source_Ptr := Sloc (N);
2035 Body_Spec : constant Node_Id := Specification (N);
2036 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2038 begin
2039 if Debug_Flag_C then
2040 Write_Str ("==> subprogram body ");
2041 Write_Name (Chars (Body_Id));
2042 Write_Str (" from ");
2043 Write_Location (Loc);
2044 Write_Eol;
2045 Indent;
2046 end if;
2048 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2050 -- The real work is split out into the helper, so it can do "return;"
2051 -- without skipping the debug output:
2053 Analyze_Subprogram_Body_Helper (N);
2055 if Debug_Flag_C then
2056 Outdent;
2057 Write_Str ("<== subprogram body ");
2058 Write_Name (Chars (Body_Id));
2059 Write_Str (" from ");
2060 Write_Location (Loc);
2061 Write_Eol;
2062 end if;
2063 end Analyze_Subprogram_Body;
2065 --------------------------------------
2066 -- Analyze_Subprogram_Body_Contract --
2067 --------------------------------------
2069 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
2070 Body_Decl : constant Node_Id := Parent (Parent (Body_Id));
2071 Mode : SPARK_Mode_Type;
2072 Prag : Node_Id;
2073 Ref_Depends : Node_Id := Empty;
2074 Ref_Global : Node_Id := Empty;
2075 Spec_Id : Entity_Id;
2077 begin
2078 -- Due to the timing of contract analysis, delayed pragmas may be
2079 -- subject to the wrong SPARK_Mode, usually that of the enclosing
2080 -- context. To remedy this, restore the original SPARK_Mode of the
2081 -- related subprogram body.
2083 Save_SPARK_Mode_And_Set (Body_Id, Mode);
2085 -- When a subprogram body declaration is illegal, its defining entity is
2086 -- left unanalyzed. There is nothing left to do in this case because the
2087 -- body lacks a contract, or even a proper Ekind.
2089 if Ekind (Body_Id) = E_Void then
2090 return;
2091 end if;
2093 if Nkind (Body_Decl) = N_Subprogram_Body_Stub then
2094 Spec_Id := Corresponding_Spec_Of_Stub (Body_Decl);
2095 else
2096 Spec_Id := Corresponding_Spec (Body_Decl);
2097 end if;
2099 -- Locate and store pragmas Refined_Depends and Refined_Global since
2100 -- their order of analysis matters.
2102 Prag := Classifications (Contract (Body_Id));
2103 while Present (Prag) loop
2104 if Pragma_Name (Prag) = Name_Refined_Depends then
2105 Ref_Depends := Prag;
2106 elsif Pragma_Name (Prag) = Name_Refined_Global then
2107 Ref_Global := Prag;
2108 end if;
2110 Prag := Next_Pragma (Prag);
2111 end loop;
2113 -- Analyze Refined_Global first as Refined_Depends may mention items
2114 -- classified in the global refinement.
2116 if Present (Ref_Global) then
2117 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
2119 -- When the corresponding Global aspect/pragma references a state with
2120 -- visible refinement, the body requires Refined_Global. Refinement is
2121 -- not required when SPARK checks are suppressed.
2123 elsif Present (Spec_Id) then
2124 Prag := Get_Pragma (Spec_Id, Pragma_Global);
2126 if SPARK_Mode /= Off
2127 and then Present (Prag)
2128 and then Contains_Refined_State (Prag)
2129 then
2130 Error_Msg_NE
2131 ("body of subprogram& requires global refinement",
2132 Body_Decl, Spec_Id);
2133 end if;
2134 end if;
2136 -- Refined_Depends must be analyzed after Refined_Global in order to see
2137 -- the modes of all global refinements.
2139 if Present (Ref_Depends) then
2140 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
2142 -- When the corresponding Depends aspect/pragma references a state with
2143 -- visible refinement, the body requires Refined_Depends. Refinement is
2144 -- not required when SPARK checks are suppressed.
2146 elsif Present (Spec_Id) then
2147 Prag := Get_Pragma (Spec_Id, Pragma_Depends);
2149 if SPARK_Mode /= Off
2150 and then Present (Prag)
2151 and then Contains_Refined_State (Prag)
2152 then
2153 Error_Msg_NE
2154 ("body of subprogram& requires dependance refinement",
2155 Body_Decl, Spec_Id);
2156 end if;
2157 end if;
2159 -- Restore the SPARK_Mode of the enclosing context after all delayed
2160 -- pragmas have been analyzed.
2162 Restore_SPARK_Mode (Mode);
2163 end Analyze_Subprogram_Body_Contract;
2165 ------------------------------------
2166 -- Analyze_Subprogram_Body_Helper --
2167 ------------------------------------
2169 -- This procedure is called for regular subprogram bodies, generic bodies,
2170 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2171 -- specification matters, and is used to create a proper declaration for
2172 -- the subprogram, or to perform conformance checks.
2174 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2175 Loc : constant Source_Ptr := Sloc (N);
2176 Body_Spec : constant Node_Id := Specification (N);
2177 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2178 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2179 Conformant : Boolean;
2180 HSS : Node_Id;
2181 Prot_Typ : Entity_Id := Empty;
2182 Spec_Id : Entity_Id;
2183 Spec_Decl : Node_Id := Empty;
2185 Last_Real_Spec_Entity : Entity_Id := Empty;
2186 -- When we analyze a separate spec, the entity chain ends up containing
2187 -- the formals, as well as any itypes generated during analysis of the
2188 -- default expressions for parameters, or the arguments of associated
2189 -- precondition/postcondition pragmas (which are analyzed in the context
2190 -- of the spec since they have visibility on formals).
2192 -- These entities belong with the spec and not the body. However we do
2193 -- the analysis of the body in the context of the spec (again to obtain
2194 -- visibility to the formals), and all the entities generated during
2195 -- this analysis end up also chained to the entity chain of the spec.
2196 -- But they really belong to the body, and there is circuitry to move
2197 -- them from the spec to the body.
2199 -- However, when we do this move, we don't want to move the real spec
2200 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2201 -- variable points to the last real spec entity, so we only move those
2202 -- chained beyond that point. It is initialized to Empty to deal with
2203 -- the case where there is no separate spec.
2205 procedure Analyze_Aspects_On_Body_Or_Stub;
2206 -- Analyze the aspect specifications of a subprogram body [stub]. It is
2207 -- assumed that N has aspects.
2209 function Body_Has_Contract return Boolean;
2210 -- Check whether unanalyzed body has an aspect or pragma that may
2211 -- generate a SPARK contract.
2213 procedure Check_Anonymous_Return;
2214 -- Ada 2005: if a function returns an access type that denotes a task,
2215 -- or a type that contains tasks, we must create a master entity for
2216 -- the anonymous type, which typically will be used in an allocator
2217 -- in the body of the function.
2219 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2220 -- Look ahead to recognize a pragma that may appear after the body.
2221 -- If there is a previous spec, check that it appears in the same
2222 -- declarative part. If the pragma is Inline_Always, perform inlining
2223 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2224 -- If the body acts as a spec, and inlining is required, we create a
2225 -- subprogram declaration for it, in order to attach the body to inline.
2226 -- If pragma does not appear after the body, check whether there is
2227 -- an inline pragma before any local declarations.
2229 procedure Check_Missing_Return;
2230 -- Checks for a function with a no return statements, and also performs
2231 -- the warning checks implemented by Check_Returns. In formal mode, also
2232 -- verify that a function ends with a RETURN and that a procedure does
2233 -- not contain any RETURN.
2235 function Disambiguate_Spec return Entity_Id;
2236 -- When a primitive is declared between the private view and the full
2237 -- view of a concurrent type which implements an interface, a special
2238 -- mechanism is used to find the corresponding spec of the primitive
2239 -- body.
2241 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2242 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2243 -- incomplete types coming from a limited context and swap their limited
2244 -- views with the non-limited ones.
2246 function Is_Private_Concurrent_Primitive
2247 (Subp_Id : Entity_Id) return Boolean;
2248 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2249 -- type that implements an interface and has a private view.
2251 procedure Set_Trivial_Subprogram (N : Node_Id);
2252 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2253 -- subprogram whose body is being analyzed. N is the statement node
2254 -- causing the flag to be set, if the following statement is a return
2255 -- of an entity, we mark the entity as set in source to suppress any
2256 -- warning on the stylized use of function stubs with a dummy return.
2258 procedure Verify_Overriding_Indicator;
2259 -- If there was a previous spec, the entity has been entered in the
2260 -- current scope previously. If the body itself carries an overriding
2261 -- indicator, check that it is consistent with the known status of the
2262 -- entity.
2264 -------------------------------------
2265 -- Analyze_Aspects_On_Body_Or_Stub --
2266 -------------------------------------
2268 procedure Analyze_Aspects_On_Body_Or_Stub is
2269 procedure Diagnose_Misplaced_Aspects;
2270 -- Subprogram body [stub] N has aspects, but they are not properly
2271 -- placed. Provide precise diagnostics depending on the aspects
2272 -- involved.
2274 --------------------------------
2275 -- Diagnose_Misplaced_Aspects --
2276 --------------------------------
2278 procedure Diagnose_Misplaced_Aspects is
2279 Asp : Node_Id;
2280 Asp_Nam : Name_Id;
2281 Asp_Id : Aspect_Id;
2282 -- The current aspect along with its name and id
2284 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id);
2285 -- Emit an error message concerning SPARK aspect Asp. Ref_Nam is
2286 -- the name of the refined version of the aspect.
2288 ------------------------
2289 -- SPARK_Aspect_Error --
2290 ------------------------
2292 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id) is
2293 begin
2294 -- The corresponding spec already contains the aspect in
2295 -- question and the one appearing on the body must be the
2296 -- refined form:
2298 -- procedure P with Global ...;
2299 -- procedure P with Global ... is ... end P;
2300 -- ^
2301 -- Refined_Global
2303 if Has_Aspect (Spec_Id, Asp_Id) then
2304 Error_Msg_Name_1 := Asp_Nam;
2306 -- Subunits cannot carry aspects that apply to a subprogram
2307 -- declaration.
2309 if Nkind (Parent (N)) = N_Subunit then
2310 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
2312 else
2313 Error_Msg_Name_2 := Ref_Nam;
2314 Error_Msg_N ("aspect % should be %", Asp);
2315 end if;
2317 -- Otherwise the aspect must appear in the spec, not in the
2318 -- body:
2320 -- procedure P;
2321 -- procedure P with Global ... is ... end P;
2323 else
2324 Error_Msg_N
2325 ("aspect specification must appear in subprogram "
2326 & "declaration", Asp);
2327 end if;
2328 end SPARK_Aspect_Error;
2330 -- Start of processing for Diagnose_Misplaced_Aspects
2332 begin
2333 -- Iterate over the aspect specifications and emit specific errors
2334 -- where applicable.
2336 Asp := First (Aspect_Specifications (N));
2337 while Present (Asp) loop
2338 Asp_Nam := Chars (Identifier (Asp));
2339 Asp_Id := Get_Aspect_Id (Asp_Nam);
2341 -- Do not emit errors on aspects that can appear on a
2342 -- subprogram body. This scenario occurs when the aspect
2343 -- specification list contains both misplaced and properly
2344 -- placed aspects.
2346 if Aspect_On_Body_Or_Stub_OK (Asp_Id) then
2347 null;
2349 -- Special diagnostics for SPARK aspects
2351 elsif Asp_Nam = Name_Depends then
2352 SPARK_Aspect_Error (Name_Refined_Depends);
2354 elsif Asp_Nam = Name_Global then
2355 SPARK_Aspect_Error (Name_Refined_Global);
2357 elsif Asp_Nam = Name_Post then
2358 SPARK_Aspect_Error (Name_Refined_Post);
2360 else
2361 Error_Msg_N
2362 ("aspect specification must appear in subprogram "
2363 & "declaration", Asp);
2364 end if;
2366 Next (Asp);
2367 end loop;
2368 end Diagnose_Misplaced_Aspects;
2370 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
2372 begin
2373 -- Language-defined aspects cannot be associated with a subprogram
2374 -- body [stub] if the subprogram has a spec. Certain implementation
2375 -- defined aspects are allowed to break this rule (for list, see
2376 -- table Aspect_On_Body_Or_Stub_OK).
2378 if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then
2379 Diagnose_Misplaced_Aspects;
2380 else
2381 Analyze_Aspect_Specifications (N, Body_Id);
2382 end if;
2383 end Analyze_Aspects_On_Body_Or_Stub;
2385 -----------------------
2386 -- Body_Has_Contract --
2387 -----------------------
2389 function Body_Has_Contract return Boolean is
2390 Decls : constant List_Id := Declarations (N);
2391 A_Spec : Node_Id;
2392 A : Aspect_Id;
2393 Decl : Node_Id;
2394 P_Id : Pragma_Id;
2396 begin
2397 -- Check for unanalyzed aspects in the body that will
2398 -- generate a contract.
2400 if Present (Aspect_Specifications (N)) then
2401 A_Spec := First (Aspect_Specifications (N));
2402 while Present (A_Spec) loop
2403 A := Get_Aspect_Id (Chars (Identifier (A_Spec)));
2405 if A = Aspect_Contract_Cases or else
2406 A = Aspect_Depends or else
2407 A = Aspect_Global or else
2408 A = Aspect_Pre or else
2409 A = Aspect_Precondition or else
2410 A = Aspect_Post or else
2411 A = Aspect_Postcondition
2412 then
2413 return True;
2414 end if;
2416 Next (A_Spec);
2417 end loop;
2418 end if;
2420 -- Check for pragmas that may generate a contract
2422 if Present (Decls) then
2423 Decl := First (Decls);
2424 while Present (Decl) loop
2425 if Nkind (Decl) = N_Pragma then
2426 P_Id := Get_Pragma_Id (Pragma_Name (Decl));
2428 if P_Id = Pragma_Contract_Cases or else
2429 P_Id = Pragma_Depends or else
2430 P_Id = Pragma_Global or else
2431 P_Id = Pragma_Pre or else
2432 P_Id = Pragma_Precondition or else
2433 P_Id = Pragma_Post or else
2434 P_Id = Pragma_Postcondition
2435 then
2436 return True;
2437 end if;
2438 end if;
2440 Next (Decl);
2441 end loop;
2442 end if;
2444 return False;
2445 end Body_Has_Contract;
2447 ----------------------------
2448 -- Check_Anonymous_Return --
2449 ----------------------------
2451 procedure Check_Anonymous_Return is
2452 Decl : Node_Id;
2453 Par : Node_Id;
2454 Scop : Entity_Id;
2456 begin
2457 if Present (Spec_Id) then
2458 Scop := Spec_Id;
2459 else
2460 Scop := Body_Id;
2461 end if;
2463 if Ekind (Scop) = E_Function
2464 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2465 and then not Is_Thunk (Scop)
2466 and then (Has_Task (Designated_Type (Etype (Scop)))
2467 or else
2468 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2469 and then
2470 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2471 and then Expander_Active
2473 -- Avoid cases with no tasking support
2475 and then RTE_Available (RE_Current_Master)
2476 and then not Restriction_Active (No_Task_Hierarchy)
2477 then
2478 Decl :=
2479 Make_Object_Declaration (Loc,
2480 Defining_Identifier =>
2481 Make_Defining_Identifier (Loc, Name_uMaster),
2482 Constant_Present => True,
2483 Object_Definition =>
2484 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2485 Expression =>
2486 Make_Explicit_Dereference (Loc,
2487 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2489 if Present (Declarations (N)) then
2490 Prepend (Decl, Declarations (N));
2491 else
2492 Set_Declarations (N, New_List (Decl));
2493 end if;
2495 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2496 Set_Has_Master_Entity (Scop);
2498 -- Now mark the containing scope as a task master
2500 Par := N;
2501 while Nkind (Par) /= N_Compilation_Unit loop
2502 Par := Parent (Par);
2503 pragma Assert (Present (Par));
2505 -- If we fall off the top, we are at the outer level, and
2506 -- the environment task is our effective master, so nothing
2507 -- to mark.
2509 if Nkind_In
2510 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2511 then
2512 Set_Is_Task_Master (Par, True);
2513 exit;
2514 end if;
2515 end loop;
2516 end if;
2517 end Check_Anonymous_Return;
2519 -------------------------
2520 -- Check_Inline_Pragma --
2521 -------------------------
2523 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2524 Prag : Node_Id;
2525 Plist : List_Id;
2527 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2528 -- True when N is a pragma Inline or Inline_Always that applies
2529 -- to this subprogram.
2531 -----------------------
2532 -- Is_Inline_Pragma --
2533 -----------------------
2535 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2536 begin
2537 return
2538 Nkind (N) = N_Pragma
2539 and then
2540 (Pragma_Name (N) = Name_Inline_Always
2541 or else
2542 (Front_End_Inlining
2543 and then Pragma_Name (N) = Name_Inline))
2544 and then
2545 Chars
2546 (Expression (First (Pragma_Argument_Associations (N)))) =
2547 Chars (Body_Id);
2548 end Is_Inline_Pragma;
2550 -- Start of processing for Check_Inline_Pragma
2552 begin
2553 if not Expander_Active then
2554 return;
2555 end if;
2557 if Is_List_Member (N)
2558 and then Present (Next (N))
2559 and then Is_Inline_Pragma (Next (N))
2560 then
2561 Prag := Next (N);
2563 elsif Nkind (N) /= N_Subprogram_Body_Stub
2564 and then Present (Declarations (N))
2565 and then Is_Inline_Pragma (First (Declarations (N)))
2566 then
2567 Prag := First (Declarations (N));
2569 else
2570 Prag := Empty;
2571 end if;
2573 if Present (Prag) then
2574 if Present (Spec_Id) then
2575 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2576 Analyze (Prag);
2577 end if;
2579 else
2580 -- Create a subprogram declaration, to make treatment uniform
2582 declare
2583 Subp : constant Entity_Id :=
2584 Make_Defining_Identifier (Loc, Chars (Body_Id));
2585 Decl : constant Node_Id :=
2586 Make_Subprogram_Declaration (Loc,
2587 Specification =>
2588 New_Copy_Tree (Specification (N)));
2590 begin
2591 Set_Defining_Unit_Name (Specification (Decl), Subp);
2593 if Present (First_Formal (Body_Id)) then
2594 Plist := Copy_Parameter_List (Body_Id);
2595 Set_Parameter_Specifications
2596 (Specification (Decl), Plist);
2597 end if;
2599 Insert_Before (N, Decl);
2600 Analyze (Decl);
2601 Analyze (Prag);
2602 Set_Has_Pragma_Inline (Subp);
2604 if Pragma_Name (Prag) = Name_Inline_Always then
2605 Set_Is_Inlined (Subp);
2606 Set_Has_Pragma_Inline_Always (Subp);
2607 end if;
2609 -- Prior to copying the subprogram body to create a template
2610 -- for it for subsequent inlining, remove the pragma from
2611 -- the current body so that the copy that will produce the
2612 -- new body will start from a completely unanalyzed tree.
2614 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2615 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2616 end if;
2618 Spec := Subp;
2619 end;
2620 end if;
2621 end if;
2622 end Check_Inline_Pragma;
2624 --------------------------
2625 -- Check_Missing_Return --
2626 --------------------------
2628 procedure Check_Missing_Return is
2629 Id : Entity_Id;
2630 Missing_Ret : Boolean;
2632 begin
2633 if Nkind (Body_Spec) = N_Function_Specification then
2634 if Present (Spec_Id) then
2635 Id := Spec_Id;
2636 else
2637 Id := Body_Id;
2638 end if;
2640 if Return_Present (Id) then
2641 Check_Returns (HSS, 'F', Missing_Ret);
2643 if Missing_Ret then
2644 Set_Has_Missing_Return (Id);
2645 end if;
2647 elsif Is_Generic_Subprogram (Id)
2648 or else not Is_Machine_Code_Subprogram (Id)
2649 then
2650 Error_Msg_N ("missing RETURN statement in function body", N);
2651 end if;
2653 -- If procedure with No_Return, check returns
2655 elsif Nkind (Body_Spec) = N_Procedure_Specification
2656 and then Present (Spec_Id)
2657 and then No_Return (Spec_Id)
2658 then
2659 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2660 end if;
2662 -- Special checks in SPARK mode
2664 if Nkind (Body_Spec) = N_Function_Specification then
2666 -- In SPARK mode, last statement of a function should be a return
2668 declare
2669 Stat : constant Node_Id := Last_Source_Statement (HSS);
2670 begin
2671 if Present (Stat)
2672 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2673 N_Extended_Return_Statement)
2674 then
2675 Check_SPARK_05_Restriction
2676 ("last statement in function should be RETURN", Stat);
2677 end if;
2678 end;
2680 -- In SPARK mode, verify that a procedure has no return
2682 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2683 if Present (Spec_Id) then
2684 Id := Spec_Id;
2685 else
2686 Id := Body_Id;
2687 end if;
2689 -- Would be nice to point to return statement here, can we
2690 -- borrow the Check_Returns procedure here ???
2692 if Return_Present (Id) then
2693 Check_SPARK_05_Restriction
2694 ("procedure should not have RETURN", N);
2695 end if;
2696 end if;
2697 end Check_Missing_Return;
2699 -----------------------
2700 -- Disambiguate_Spec --
2701 -----------------------
2703 function Disambiguate_Spec return Entity_Id is
2704 Priv_Spec : Entity_Id;
2705 Spec_N : Entity_Id;
2707 procedure Replace_Types (To_Corresponding : Boolean);
2708 -- Depending on the flag, replace the type of formal parameters of
2709 -- Body_Id if it is a concurrent type implementing interfaces with
2710 -- the corresponding record type or the other way around.
2712 procedure Replace_Types (To_Corresponding : Boolean) is
2713 Formal : Entity_Id;
2714 Formal_Typ : Entity_Id;
2716 begin
2717 Formal := First_Formal (Body_Id);
2718 while Present (Formal) loop
2719 Formal_Typ := Etype (Formal);
2721 if Is_Class_Wide_Type (Formal_Typ) then
2722 Formal_Typ := Root_Type (Formal_Typ);
2723 end if;
2725 -- From concurrent type to corresponding record
2727 if To_Corresponding then
2728 if Is_Concurrent_Type (Formal_Typ)
2729 and then Present (Corresponding_Record_Type (Formal_Typ))
2730 and then Present (Interfaces (
2731 Corresponding_Record_Type (Formal_Typ)))
2732 then
2733 Set_Etype (Formal,
2734 Corresponding_Record_Type (Formal_Typ));
2735 end if;
2737 -- From corresponding record to concurrent type
2739 else
2740 if Is_Concurrent_Record_Type (Formal_Typ)
2741 and then Present (Interfaces (Formal_Typ))
2742 then
2743 Set_Etype (Formal,
2744 Corresponding_Concurrent_Type (Formal_Typ));
2745 end if;
2746 end if;
2748 Next_Formal (Formal);
2749 end loop;
2750 end Replace_Types;
2752 -- Start of processing for Disambiguate_Spec
2754 begin
2755 -- Try to retrieve the specification of the body as is. All error
2756 -- messages are suppressed because the body may not have a spec in
2757 -- its current state.
2759 Spec_N := Find_Corresponding_Spec (N, False);
2761 -- It is possible that this is the body of a primitive declared
2762 -- between a private and a full view of a concurrent type. The
2763 -- controlling parameter of the spec carries the concurrent type,
2764 -- not the corresponding record type as transformed by Analyze_
2765 -- Subprogram_Specification. In such cases, we undo the change
2766 -- made by the analysis of the specification and try to find the
2767 -- spec again.
2769 -- Note that wrappers already have their corresponding specs and
2770 -- bodies set during their creation, so if the candidate spec is
2771 -- a wrapper, then we definitely need to swap all types to their
2772 -- original concurrent status.
2774 if No (Spec_N)
2775 or else Is_Primitive_Wrapper (Spec_N)
2776 then
2777 -- Restore all references of corresponding record types to the
2778 -- original concurrent types.
2780 Replace_Types (To_Corresponding => False);
2781 Priv_Spec := Find_Corresponding_Spec (N, False);
2783 -- The current body truly belongs to a primitive declared between
2784 -- a private and a full view. We leave the modified body as is,
2785 -- and return the true spec.
2787 if Present (Priv_Spec)
2788 and then Is_Private_Primitive (Priv_Spec)
2789 then
2790 return Priv_Spec;
2791 end if;
2793 -- In case that this is some sort of error, restore the original
2794 -- state of the body.
2796 Replace_Types (To_Corresponding => True);
2797 end if;
2799 return Spec_N;
2800 end Disambiguate_Spec;
2802 ----------------------------
2803 -- Exchange_Limited_Views --
2804 ----------------------------
2806 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2807 procedure Detect_And_Exchange (Id : Entity_Id);
2808 -- Determine whether Id's type denotes an incomplete type associated
2809 -- with a limited with clause and exchange the limited view with the
2810 -- non-limited one.
2812 -------------------------
2813 -- Detect_And_Exchange --
2814 -------------------------
2816 procedure Detect_And_Exchange (Id : Entity_Id) is
2817 Typ : constant Entity_Id := Etype (Id);
2819 begin
2820 if Ekind (Typ) = E_Incomplete_Type
2821 and then From_Limited_With (Typ)
2822 and then Present (Non_Limited_View (Typ))
2823 then
2824 Set_Etype (Id, Non_Limited_View (Typ));
2825 end if;
2826 end Detect_And_Exchange;
2828 -- Local variables
2830 Formal : Entity_Id;
2832 -- Start of processing for Exchange_Limited_Views
2834 begin
2835 if No (Subp_Id) then
2836 return;
2838 -- Do not process subprogram bodies as they already use the non-
2839 -- limited view of types.
2841 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2842 return;
2843 end if;
2845 -- Examine all formals and swap views when applicable
2847 Formal := First_Formal (Subp_Id);
2848 while Present (Formal) loop
2849 Detect_And_Exchange (Formal);
2851 Next_Formal (Formal);
2852 end loop;
2854 -- Process the return type of a function
2856 if Ekind (Subp_Id) = E_Function then
2857 Detect_And_Exchange (Subp_Id);
2858 end if;
2859 end Exchange_Limited_Views;
2861 -------------------------------------
2862 -- Is_Private_Concurrent_Primitive --
2863 -------------------------------------
2865 function Is_Private_Concurrent_Primitive
2866 (Subp_Id : Entity_Id) return Boolean
2868 Formal_Typ : Entity_Id;
2870 begin
2871 if Present (First_Formal (Subp_Id)) then
2872 Formal_Typ := Etype (First_Formal (Subp_Id));
2874 if Is_Concurrent_Record_Type (Formal_Typ) then
2875 if Is_Class_Wide_Type (Formal_Typ) then
2876 Formal_Typ := Root_Type (Formal_Typ);
2877 end if;
2879 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2880 end if;
2882 -- The type of the first formal is a concurrent tagged type with
2883 -- a private view.
2885 return
2886 Is_Concurrent_Type (Formal_Typ)
2887 and then Is_Tagged_Type (Formal_Typ)
2888 and then Has_Private_Declaration (Formal_Typ);
2889 end if;
2891 return False;
2892 end Is_Private_Concurrent_Primitive;
2894 ----------------------------
2895 -- Set_Trivial_Subprogram --
2896 ----------------------------
2898 procedure Set_Trivial_Subprogram (N : Node_Id) is
2899 Nxt : constant Node_Id := Next (N);
2901 begin
2902 Set_Is_Trivial_Subprogram (Body_Id);
2904 if Present (Spec_Id) then
2905 Set_Is_Trivial_Subprogram (Spec_Id);
2906 end if;
2908 if Present (Nxt)
2909 and then Nkind (Nxt) = N_Simple_Return_Statement
2910 and then No (Next (Nxt))
2911 and then Present (Expression (Nxt))
2912 and then Is_Entity_Name (Expression (Nxt))
2913 then
2914 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2915 end if;
2916 end Set_Trivial_Subprogram;
2918 ---------------------------------
2919 -- Verify_Overriding_Indicator --
2920 ---------------------------------
2922 procedure Verify_Overriding_Indicator is
2923 begin
2924 if Must_Override (Body_Spec) then
2925 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2926 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2927 then
2928 null;
2930 elsif not Present (Overridden_Operation (Spec_Id)) then
2931 Error_Msg_NE
2932 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2934 -- Overriding indicators aren't allowed for protected subprogram
2935 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2936 -- this to a warning if -gnatd.E is enabled.
2938 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2939 Error_Msg_Warn := Error_To_Warning;
2940 Error_Msg_N
2941 ("<<overriding indicator not allowed for protected "
2942 & "subprogram body", Body_Spec);
2943 end if;
2945 elsif Must_Not_Override (Body_Spec) then
2946 if Present (Overridden_Operation (Spec_Id)) then
2947 Error_Msg_NE
2948 ("subprogram& overrides inherited operation",
2949 Body_Spec, Spec_Id);
2951 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2952 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2953 then
2954 Error_Msg_NE
2955 ("subprogram& overrides predefined operator ",
2956 Body_Spec, Spec_Id);
2958 -- Overriding indicators aren't allowed for protected subprogram
2959 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2960 -- this to a warning if -gnatd.E is enabled.
2962 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2963 Error_Msg_Warn := Error_To_Warning;
2965 Error_Msg_N
2966 ("<<overriding indicator not allowed "
2967 & "for protected subprogram body", Body_Spec);
2969 -- If this is not a primitive operation, then the overriding
2970 -- indicator is altogether illegal.
2972 elsif not Is_Primitive (Spec_Id) then
2973 Error_Msg_N
2974 ("overriding indicator only allowed "
2975 & "if subprogram is primitive", Body_Spec);
2976 end if;
2978 -- If checking the style rule and the operation overrides, then
2979 -- issue a warning about a missing overriding_indicator. Protected
2980 -- subprogram bodies are excluded from this style checking, since
2981 -- they aren't primitives (even though their declarations can
2982 -- override) and aren't allowed to have an overriding_indicator.
2984 elsif Style_Check
2985 and then Present (Overridden_Operation (Spec_Id))
2986 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2987 then
2988 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2989 Style.Missing_Overriding (N, Body_Id);
2991 elsif Style_Check
2992 and then Can_Override_Operator (Spec_Id)
2993 and then not Is_Predefined_File_Name
2994 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2995 then
2996 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2997 Style.Missing_Overriding (N, Body_Id);
2998 end if;
2999 end Verify_Overriding_Indicator;
3001 -- Start of processing for Analyze_Subprogram_Body_Helper
3003 begin
3004 -- Generic subprograms are handled separately. They always have a
3005 -- generic specification. Determine whether current scope has a
3006 -- previous declaration.
3008 -- If the subprogram body is defined within an instance of the same
3009 -- name, the instance appears as a package renaming, and will be hidden
3010 -- within the subprogram.
3012 if Present (Prev_Id)
3013 and then not Is_Overloadable (Prev_Id)
3014 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3015 or else Comes_From_Source (Prev_Id))
3016 then
3017 if Is_Generic_Subprogram (Prev_Id) then
3018 Spec_Id := Prev_Id;
3019 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3020 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3022 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3024 if Nkind (N) = N_Subprogram_Body then
3025 HSS := Handled_Statement_Sequence (N);
3026 Check_Missing_Return;
3027 end if;
3029 return;
3031 else
3032 -- Previous entity conflicts with subprogram name. Attempting to
3033 -- enter name will post error.
3035 Enter_Name (Body_Id);
3036 return;
3037 end if;
3039 -- Non-generic case, find the subprogram declaration, if one was seen,
3040 -- or enter new overloaded entity in the current scope. If the
3041 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3042 -- part of the context of one of its subunits. No need to redo the
3043 -- analysis.
3045 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3046 return;
3048 else
3049 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3051 if Nkind (N) = N_Subprogram_Body_Stub
3052 or else No (Corresponding_Spec (N))
3053 then
3054 if Is_Private_Concurrent_Primitive (Body_Id) then
3055 Spec_Id := Disambiguate_Spec;
3056 else
3057 Spec_Id := Find_Corresponding_Spec (N);
3059 -- In GNATprove mode, if the body has no previous spec, create
3060 -- one so that the inlining machinery can operate properly.
3061 -- Transfer aspects, if any, to the new spec, so that they
3062 -- are legal and can be processed ahead of the body.
3063 -- We make two copies of the given spec, one for the new
3064 -- declaration, and one for the body.
3066 if No (Spec_Id)
3067 and then GNATprove_Mode
3069 -- Inlining does not apply during pre-analysis of code
3071 and then Full_Analysis
3073 -- Inlining only applies to full bodies, not stubs
3075 and then Nkind (N) /= N_Subprogram_Body_Stub
3077 -- Inlining only applies to bodies in the source code, not to
3078 -- those generated by the compiler. In particular, expression
3079 -- functions, whose body is generated by the compiler, are
3080 -- treated specially by GNATprove.
3082 and then Comes_From_Source (Body_Id)
3084 -- This cannot be done for a compilation unit, which is not
3085 -- in a context where we can insert a new spec.
3087 and then Is_List_Member (N)
3089 -- Inlining only applies to subprograms without contracts,
3090 -- as a contract is a sign that GNATprove should perform a
3091 -- modular analysis of the subprogram instead of a contextual
3092 -- analysis at each call site. The same test is performed in
3093 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3094 -- here in another form (because the contract has not
3095 -- been attached to the body) to avoid frontend errors in
3096 -- case pragmas are used instead of aspects, because the
3097 -- corresponding pragmas in the body would not be transferred
3098 -- to the spec, leading to legality errors.
3100 and then not Body_Has_Contract
3101 then
3102 declare
3103 Body_Spec : constant Node_Id :=
3104 Copy_Separate_Tree (Specification (N));
3105 New_Decl : constant Node_Id :=
3106 Make_Subprogram_Declaration (Loc,
3107 Copy_Separate_Tree (Specification (N)));
3109 SPARK_Mode_Aspect : Node_Id;
3110 Aspects : List_Id;
3111 Prag, Aspect : Node_Id;
3113 begin
3114 Insert_Before (N, New_Decl);
3115 Move_Aspects (From => N, To => New_Decl);
3117 -- Mark the newly moved aspects as not analyzed, so that
3118 -- their effect on New_Decl is properly analyzed.
3120 Aspect := First (Aspect_Specifications (New_Decl));
3121 while Present (Aspect) loop
3122 Set_Analyzed (Aspect, False);
3123 Next (Aspect);
3124 end loop;
3126 Analyze (New_Decl);
3128 -- The analysis of the generated subprogram declaration
3129 -- may have introduced pragmas that need to be analyzed.
3131 Prag := Next (New_Decl);
3132 while Prag /= N loop
3133 Analyze (Prag);
3134 Next (Prag);
3135 end loop;
3137 Spec_Id := Defining_Entity (New_Decl);
3139 -- As Body_Id originally comes from source, mark the new
3140 -- Spec_Id as such, which is required so that calls to
3141 -- this subprogram are registered in the local effects
3142 -- stored in ALI files for GNATprove.
3144 Set_Comes_From_Source (Spec_Id, True);
3146 -- If aspect SPARK_Mode was specified on the body, it
3147 -- needs to be repeated on the generated decl and the
3148 -- body. Since the original aspect was moved to the
3149 -- generated decl, copy it for the body.
3151 if Has_Aspect (Spec_Id, Aspect_SPARK_Mode) then
3152 SPARK_Mode_Aspect :=
3153 New_Copy (Find_Aspect (Spec_Id, Aspect_SPARK_Mode));
3154 Set_Analyzed (SPARK_Mode_Aspect, False);
3155 Aspects := New_List (SPARK_Mode_Aspect);
3156 Set_Aspect_Specifications (N, Aspects);
3157 end if;
3159 Set_Specification (N, Body_Spec);
3160 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3161 Set_Corresponding_Spec (N, Spec_Id);
3162 end;
3163 end if;
3164 end if;
3166 -- If this is a duplicate body, no point in analyzing it
3168 if Error_Posted (N) then
3169 return;
3170 end if;
3172 -- A subprogram body should cause freezing of its own declaration,
3173 -- but if there was no previous explicit declaration, then the
3174 -- subprogram will get frozen too late (there may be code within
3175 -- the body that depends on the subprogram having been frozen,
3176 -- such as uses of extra formals), so we force it to be frozen
3177 -- here. Same holds if the body and spec are compilation units.
3178 -- Finally, if the return type is an anonymous access to protected
3179 -- subprogram, it must be frozen before the body because its
3180 -- expansion has generated an equivalent type that is used when
3181 -- elaborating the body.
3183 -- An exception in the case of Ada 2012, AI05-177: The bodies
3184 -- created for expression functions do not freeze.
3186 if No (Spec_Id)
3187 and then Nkind (Original_Node (N)) /= N_Expression_Function
3188 then
3189 Freeze_Before (N, Body_Id);
3191 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3192 Freeze_Before (N, Spec_Id);
3194 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3195 Freeze_Before (N, Etype (Body_Id));
3196 end if;
3198 else
3199 Spec_Id := Corresponding_Spec (N);
3200 end if;
3201 end if;
3203 -- Previously we scanned the body to look for nested subprograms, and
3204 -- rejected an inline directive if nested subprograms were present,
3205 -- because the back-end would generate conflicting symbols for the
3206 -- nested bodies. This is now unnecessary.
3208 -- Look ahead to recognize a pragma Inline that appears after the body
3210 Check_Inline_Pragma (Spec_Id);
3212 -- Deal with special case of a fully private operation in the body of
3213 -- the protected type. We must create a declaration for the subprogram,
3214 -- in order to attach the protected subprogram that will be used in
3215 -- internal calls. We exclude compiler generated bodies from the
3216 -- expander since the issue does not arise for those cases.
3218 if No (Spec_Id)
3219 and then Comes_From_Source (N)
3220 and then Is_Protected_Type (Current_Scope)
3221 then
3222 Spec_Id := Build_Private_Protected_Declaration (N);
3223 end if;
3225 -- If a separate spec is present, then deal with freezing issues
3227 if Present (Spec_Id) then
3228 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3229 Verify_Overriding_Indicator;
3231 -- In general, the spec will be frozen when we start analyzing the
3232 -- body. However, for internally generated operations, such as
3233 -- wrapper functions for inherited operations with controlling
3234 -- results, the spec may not have been frozen by the time we expand
3235 -- the freeze actions that include the bodies. In particular, extra
3236 -- formals for accessibility or for return-in-place may need to be
3237 -- generated. Freeze nodes, if any, are inserted before the current
3238 -- body. These freeze actions are also needed in ASIS mode to enable
3239 -- the proper back-annotations.
3241 if not Is_Frozen (Spec_Id)
3242 and then (Expander_Active or ASIS_Mode)
3243 then
3244 -- Force the generation of its freezing node to ensure proper
3245 -- management of access types in the backend.
3247 -- This is definitely needed for some cases, but it is not clear
3248 -- why, to be investigated further???
3250 Set_Has_Delayed_Freeze (Spec_Id);
3251 Freeze_Before (N, Spec_Id);
3252 end if;
3253 end if;
3255 -- Mark presence of postcondition procedure in current scope and mark
3256 -- the procedure itself as needing debug info. The latter is important
3257 -- when analyzing decision coverage (for example, for MC/DC coverage).
3259 if Chars (Body_Id) = Name_uPostconditions then
3260 Set_Has_Postconditions (Current_Scope);
3261 Set_Debug_Info_Needed (Body_Id);
3262 end if;
3264 -- Place subprogram on scope stack, and make formals visible. If there
3265 -- is a spec, the visible entity remains that of the spec.
3267 if Present (Spec_Id) then
3268 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3270 if Is_Child_Unit (Spec_Id) then
3271 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3272 end if;
3274 if Style_Check then
3275 Style.Check_Identifier (Body_Id, Spec_Id);
3276 end if;
3278 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3279 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3281 if Is_Abstract_Subprogram (Spec_Id) then
3282 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3283 return;
3285 else
3286 Set_Convention (Body_Id, Convention (Spec_Id));
3287 Set_Has_Completion (Spec_Id);
3289 if Is_Protected_Type (Scope (Spec_Id)) then
3290 Prot_Typ := Scope (Spec_Id);
3291 end if;
3293 -- If this is a body generated for a renaming, do not check for
3294 -- full conformance. The check is redundant, because the spec of
3295 -- the body is a copy of the spec in the renaming declaration,
3296 -- and the test can lead to spurious errors on nested defaults.
3298 if Present (Spec_Decl)
3299 and then not Comes_From_Source (N)
3300 and then
3301 (Nkind (Original_Node (Spec_Decl)) =
3302 N_Subprogram_Renaming_Declaration
3303 or else (Present (Corresponding_Body (Spec_Decl))
3304 and then
3305 Nkind (Unit_Declaration_Node
3306 (Corresponding_Body (Spec_Decl))) =
3307 N_Subprogram_Renaming_Declaration))
3308 then
3309 Conformant := True;
3311 -- Conversely, the spec may have been generated for specless body
3312 -- with an inline pragma.
3314 elsif Comes_From_Source (N)
3315 and then not Comes_From_Source (Spec_Id)
3316 and then Has_Pragma_Inline (Spec_Id)
3317 then
3318 Conformant := True;
3320 else
3321 Check_Conformance
3322 (Body_Id, Spec_Id,
3323 Fully_Conformant, True, Conformant, Body_Id);
3324 end if;
3326 -- If the body is not fully conformant, we have to decide if we
3327 -- should analyze it or not. If it has a really messed up profile
3328 -- then we probably should not analyze it, since we will get too
3329 -- many bogus messages.
3331 -- Our decision is to go ahead in the non-fully conformant case
3332 -- only if it is at least mode conformant with the spec. Note
3333 -- that the call to Check_Fully_Conformant has issued the proper
3334 -- error messages to complain about the lack of conformance.
3336 if not Conformant
3337 and then not Mode_Conformant (Body_Id, Spec_Id)
3338 then
3339 return;
3340 end if;
3341 end if;
3343 if Spec_Id /= Body_Id then
3344 Reference_Body_Formals (Spec_Id, Body_Id);
3345 end if;
3347 Set_Ekind (Body_Id, E_Subprogram_Body);
3349 if Nkind (N) = N_Subprogram_Body_Stub then
3350 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3352 -- Regular body
3354 else
3355 Set_Corresponding_Spec (N, Spec_Id);
3357 -- Ada 2005 (AI-345): If the operation is a primitive operation
3358 -- of a concurrent type, the type of the first parameter has been
3359 -- replaced with the corresponding record, which is the proper
3360 -- run-time structure to use. However, within the body there may
3361 -- be uses of the formals that depend on primitive operations
3362 -- of the type (in particular calls in prefixed form) for which
3363 -- we need the original concurrent type. The operation may have
3364 -- several controlling formals, so the replacement must be done
3365 -- for all of them.
3367 if Comes_From_Source (Spec_Id)
3368 and then Present (First_Entity (Spec_Id))
3369 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3370 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3371 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3372 and then Present (Corresponding_Concurrent_Type
3373 (Etype (First_Entity (Spec_Id))))
3374 then
3375 declare
3376 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3377 Form : Entity_Id;
3379 begin
3380 Form := First_Formal (Spec_Id);
3381 while Present (Form) loop
3382 if Etype (Form) = Typ then
3383 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3384 end if;
3386 Next_Formal (Form);
3387 end loop;
3388 end;
3389 end if;
3391 -- Make the formals visible, and place subprogram on scope stack.
3392 -- This is also the point at which we set Last_Real_Spec_Entity
3393 -- to mark the entities which will not be moved to the body.
3395 Install_Formals (Spec_Id);
3396 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3398 -- Within an instance, add local renaming declarations so that
3399 -- gdb can retrieve the values of actuals more easily. This is
3400 -- only relevant if generating code (and indeed we definitely
3401 -- do not want these definitions -gnatc mode, because that would
3402 -- confuse ASIS).
3404 if Is_Generic_Instance (Spec_Id)
3405 and then Is_Wrapper_Package (Current_Scope)
3406 and then Expander_Active
3407 then
3408 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3409 end if;
3411 Push_Scope (Spec_Id);
3413 -- Make sure that the subprogram is immediately visible. For
3414 -- child units that have no separate spec this is indispensable.
3415 -- Otherwise it is safe albeit redundant.
3417 Set_Is_Immediately_Visible (Spec_Id);
3418 end if;
3420 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3421 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3422 Set_Scope (Body_Id, Scope (Spec_Id));
3423 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3425 -- Case of subprogram body with no previous spec
3427 else
3428 -- Check for style warning required
3430 if Style_Check
3432 -- Only apply check for source level subprograms for which checks
3433 -- have not been suppressed.
3435 and then Comes_From_Source (Body_Id)
3436 and then not Suppress_Style_Checks (Body_Id)
3438 -- No warnings within an instance
3440 and then not In_Instance
3442 -- No warnings for expression functions
3444 and then Nkind (Original_Node (N)) /= N_Expression_Function
3445 then
3446 Style.Body_With_No_Spec (N);
3447 end if;
3449 New_Overloaded_Entity (Body_Id);
3451 if Nkind (N) /= N_Subprogram_Body_Stub then
3452 Set_Acts_As_Spec (N);
3453 Generate_Definition (Body_Id);
3454 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3455 Generate_Reference
3456 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3457 Install_Formals (Body_Id);
3459 Push_Scope (Body_Id);
3460 end if;
3462 -- For stubs and bodies with no previous spec, generate references to
3463 -- formals.
3465 Generate_Reference_To_Formals (Body_Id);
3466 end if;
3468 -- Set SPARK_Mode from context
3470 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3471 Set_SPARK_Pragma_Inherited (Body_Id, True);
3473 -- If the return type is an anonymous access type whose designated type
3474 -- is the limited view of a class-wide type and the non-limited view is
3475 -- available, update the return type accordingly.
3477 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3478 declare
3479 Etyp : Entity_Id;
3480 Rtyp : Entity_Id;
3482 begin
3483 Rtyp := Etype (Current_Scope);
3485 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3486 Etyp := Directly_Designated_Type (Rtyp);
3488 if Is_Class_Wide_Type (Etyp)
3489 and then From_Limited_With (Etyp)
3490 then
3491 Set_Directly_Designated_Type
3492 (Etype (Current_Scope), Available_View (Etyp));
3493 end if;
3494 end if;
3495 end;
3496 end if;
3498 -- If this is the proper body of a stub, we must verify that the stub
3499 -- conforms to the body, and to the previous spec if one was present.
3500 -- We know already that the body conforms to that spec. This test is
3501 -- only required for subprograms that come from source.
3503 if Nkind (Parent (N)) = N_Subunit
3504 and then Comes_From_Source (N)
3505 and then not Error_Posted (Body_Id)
3506 and then Nkind (Corresponding_Stub (Parent (N))) =
3507 N_Subprogram_Body_Stub
3508 then
3509 declare
3510 Old_Id : constant Entity_Id :=
3511 Defining_Entity
3512 (Specification (Corresponding_Stub (Parent (N))));
3514 Conformant : Boolean := False;
3516 begin
3517 if No (Spec_Id) then
3518 Check_Fully_Conformant (Body_Id, Old_Id);
3520 else
3521 Check_Conformance
3522 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3524 if not Conformant then
3526 -- The stub was taken to be a new declaration. Indicate that
3527 -- it lacks a body.
3529 Set_Has_Completion (Old_Id, False);
3530 end if;
3531 end if;
3532 end;
3533 end if;
3535 Set_Has_Completion (Body_Id);
3536 Check_Eliminated (Body_Id);
3538 if Nkind (N) = N_Subprogram_Body_Stub then
3540 -- Analyze any aspect specifications that appear on the subprogram
3541 -- body stub.
3543 if Has_Aspects (N) then
3544 Analyze_Aspects_On_Body_Or_Stub;
3545 end if;
3547 -- Stop the analysis now as the stub cannot be inlined, plus it does
3548 -- not have declarative or statement lists.
3550 return;
3551 end if;
3553 -- Handle frontend inlining
3555 -- Note: Normally we don't do any inlining if expansion is off, since
3556 -- we won't generate code in any case. An exception arises in GNATprove
3557 -- mode where we want to expand some calls in place, even with expansion
3558 -- disabled, since the inlining eases formal verification.
3560 if not GNATprove_Mode
3561 and then Expander_Active
3562 and then Serious_Errors_Detected = 0
3563 and then Present (Spec_Id)
3564 and then Has_Pragma_Inline (Spec_Id)
3565 then
3566 -- Legacy implementation (relying on frontend inlining)
3568 if not Back_End_Inlining then
3569 if Has_Pragma_Inline_Always (Spec_Id)
3570 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining)
3571 then
3572 Build_Body_To_Inline (N, Spec_Id);
3573 end if;
3575 -- New implementation (relying on backend inlining). Enabled by
3576 -- debug flag gnatd.z for testing
3578 else
3579 if Has_Pragma_Inline_Always (Spec_Id)
3580 or else Optimization_Level > 0
3581 then
3582 -- Handle function returning an unconstrained type
3584 if Comes_From_Source (Body_Id)
3585 and then Ekind (Spec_Id) = E_Function
3586 and then Returns_Unconstrained_Type (Spec_Id)
3587 then
3588 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
3590 else
3591 declare
3592 Subp_Body : constant Node_Id :=
3593 Unit_Declaration_Node (Body_Id);
3594 Subp_Decl : constant List_Id := Declarations (Subp_Body);
3596 begin
3597 -- Do not pass inlining to the backend if the subprogram
3598 -- has declarations or statements which cannot be inlined
3599 -- by the backend. This check is done here to emit an
3600 -- error instead of the generic warning message reported
3601 -- by the GCC backend (ie. "function might not be
3602 -- inlinable").
3604 if Present (Subp_Decl)
3605 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
3606 then
3607 null;
3609 elsif Has_Excluded_Statement
3610 (Spec_Id,
3611 Statements
3612 (Handled_Statement_Sequence (Subp_Body)))
3613 then
3614 null;
3616 -- If the backend inlining is available then at this
3617 -- stage we only have to mark the subprogram as inlined.
3618 -- The expander will take care of registering it in the
3619 -- table of subprograms inlined by the backend a part of
3620 -- processing calls to it (cf. Expand_Call)
3622 else
3623 Set_Is_Inlined (Spec_Id);
3624 end if;
3625 end;
3626 end if;
3627 end if;
3628 end if;
3630 -- In GNATprove mode, inline only when there is a separate subprogram
3631 -- declaration for now, as inlining of subprogram bodies acting as
3632 -- declarations, or subprogram stubs, are not supported by frontend
3633 -- inlining. This inlining should occur after analysis of the body, so
3634 -- that it is known whether the value of SPARK_Mode applicable to the
3635 -- body, which can be defined by a pragma inside the body.
3637 elsif GNATprove_Mode
3638 and then Full_Analysis
3639 and then not Inside_A_Generic
3640 and then Present (Spec_Id)
3641 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3642 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
3643 and then not Body_Has_Contract
3644 then
3645 Build_Body_To_Inline (N, Spec_Id);
3646 end if;
3648 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3649 -- of the specification we have to install the private withed units.
3650 -- This holds for child units as well.
3652 if Is_Compilation_Unit (Body_Id)
3653 or else Nkind (Parent (N)) = N_Compilation_Unit
3654 then
3655 Install_Private_With_Clauses (Body_Id);
3656 end if;
3658 Check_Anonymous_Return;
3660 -- Set the Protected_Formal field of each extra formal of the protected
3661 -- subprogram to reference the corresponding extra formal of the
3662 -- subprogram that implements it. For regular formals this occurs when
3663 -- the protected subprogram's declaration is expanded, but the extra
3664 -- formals don't get created until the subprogram is frozen. We need to
3665 -- do this before analyzing the protected subprogram's body so that any
3666 -- references to the original subprogram's extra formals will be changed
3667 -- refer to the implementing subprogram's formals (see Expand_Formal).
3669 if Present (Spec_Id)
3670 and then Is_Protected_Type (Scope (Spec_Id))
3671 and then Present (Protected_Body_Subprogram (Spec_Id))
3672 then
3673 declare
3674 Impl_Subp : constant Entity_Id :=
3675 Protected_Body_Subprogram (Spec_Id);
3676 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3677 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3678 begin
3679 while Present (Prot_Ext_Formal) loop
3680 pragma Assert (Present (Impl_Ext_Formal));
3681 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3682 Next_Formal_With_Extras (Prot_Ext_Formal);
3683 Next_Formal_With_Extras (Impl_Ext_Formal);
3684 end loop;
3685 end;
3686 end if;
3688 -- Now we can go on to analyze the body
3690 HSS := Handled_Statement_Sequence (N);
3691 Set_Actual_Subtypes (N, Current_Scope);
3693 -- Add a declaration for the Protection object, renaming declarations
3694 -- for discriminals and privals and finally a declaration for the entry
3695 -- family index (if applicable). This form of early expansion is done
3696 -- when the Expander is active because Install_Private_Data_Declarations
3697 -- references entities which were created during regular expansion. The
3698 -- subprogram entity must come from source, and not be an internally
3699 -- generated subprogram.
3701 if Expander_Active
3702 and then Present (Prot_Typ)
3703 and then Present (Spec_Id)
3704 and then Comes_From_Source (Spec_Id)
3705 and then not Is_Eliminated (Spec_Id)
3706 then
3707 Install_Private_Data_Declarations
3708 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3709 end if;
3711 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3712 -- may now appear in parameter and result profiles. Since the analysis
3713 -- of a subprogram body may use the parameter and result profile of the
3714 -- spec, swap any limited views with their non-limited counterpart.
3716 if Ada_Version >= Ada_2012 then
3717 Exchange_Limited_Views (Spec_Id);
3718 end if;
3720 -- Analyze any aspect specifications that appear on the subprogram body
3722 if Has_Aspects (N) then
3723 Analyze_Aspects_On_Body_Or_Stub;
3724 end if;
3726 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
3727 -- invariants and predicates associated with the body and its spec.
3728 -- Note that this is not pure expansion as Expand_Subprogram_Contract
3729 -- prepares the contract assertions for generic subprograms or for ASIS.
3730 -- Do not generate contract checks in SPARK mode.
3732 if not GNATprove_Mode then
3733 Expand_Subprogram_Contract (N, Spec_Id, Body_Id);
3734 end if;
3736 -- Analyze the declarations (this call will analyze the precondition
3737 -- Check pragmas we prepended to the list, as well as the declaration
3738 -- of the _Postconditions procedure).
3740 Analyze_Declarations (Declarations (N));
3742 -- Verify that the SPARK_Mode of the body agrees with that of its spec
3744 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
3745 if Present (SPARK_Pragma (Spec_Id)) then
3746 if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off
3747 and then
3748 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
3749 then
3750 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3751 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3752 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3753 Error_Msg_NE
3754 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
3755 end if;
3757 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3758 null;
3760 else
3761 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3762 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
3763 Error_Msg_Sloc := Sloc (Spec_Id);
3764 Error_Msg_NE
3765 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
3766 end if;
3767 end if;
3769 -- If SPARK_Mode for body is not On, disable frontend inlining for this
3770 -- subprogram in GNATprove mode, as its body should not be analyzed.
3772 if SPARK_Mode /= On
3773 and then GNATprove_Mode
3774 and then Present (Spec_Id)
3775 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3776 then
3777 Set_Body_To_Inline (Parent (Parent (Spec_Id)), Empty);
3778 Set_Is_Inlined_Always (Spec_Id, False);
3779 end if;
3781 -- Check completion, and analyze the statements
3783 Check_Completion;
3784 Inspect_Deferred_Constant_Completion (Declarations (N));
3785 Analyze (HSS);
3787 -- Deal with end of scope processing for the body
3789 Process_End_Label (HSS, 't', Current_Scope);
3790 End_Scope;
3791 Check_Subprogram_Order (N);
3792 Set_Analyzed (Body_Id);
3794 -- If we have a separate spec, then the analysis of the declarations
3795 -- caused the entities in the body to be chained to the spec id, but
3796 -- we want them chained to the body id. Only the formal parameters
3797 -- end up chained to the spec id in this case.
3799 if Present (Spec_Id) then
3801 -- We must conform to the categorization of our spec
3803 Validate_Categorization_Dependency (N, Spec_Id);
3805 -- And if this is a child unit, the parent units must conform
3807 if Is_Child_Unit (Spec_Id) then
3808 Validate_Categorization_Dependency
3809 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3810 end if;
3812 -- Here is where we move entities from the spec to the body
3814 -- Case where there are entities that stay with the spec
3816 if Present (Last_Real_Spec_Entity) then
3818 -- No body entities (happens when the only real spec entities come
3819 -- from precondition and postcondition pragmas).
3821 if No (Last_Entity (Body_Id)) then
3822 Set_First_Entity
3823 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3825 -- Body entities present (formals), so chain stuff past them
3827 else
3828 Set_Next_Entity
3829 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3830 end if;
3832 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3833 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3834 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3836 -- Case where there are no spec entities, in this case there can be
3837 -- no body entities either, so just move everything.
3839 else
3840 pragma Assert (No (Last_Entity (Body_Id)));
3841 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3842 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3843 Set_First_Entity (Spec_Id, Empty);
3844 Set_Last_Entity (Spec_Id, Empty);
3845 end if;
3846 end if;
3848 Check_Missing_Return;
3850 -- Now we are going to check for variables that are never modified in
3851 -- the body of the procedure. But first we deal with a special case
3852 -- where we want to modify this check. If the body of the subprogram
3853 -- starts with a raise statement or its equivalent, or if the body
3854 -- consists entirely of a null statement, then it is pretty obvious that
3855 -- it is OK to not reference the parameters. For example, this might be
3856 -- the following common idiom for a stubbed function: statement of the
3857 -- procedure raises an exception. In particular this deals with the
3858 -- common idiom of a stubbed function, which appears something like:
3860 -- function F (A : Integer) return Some_Type;
3861 -- X : Some_Type;
3862 -- begin
3863 -- raise Program_Error;
3864 -- return X;
3865 -- end F;
3867 -- Here the purpose of X is simply to satisfy the annoying requirement
3868 -- in Ada that there be at least one return, and we certainly do not
3869 -- want to go posting warnings on X that it is not initialized. On
3870 -- the other hand, if X is entirely unreferenced that should still
3871 -- get a warning.
3873 -- What we do is to detect these cases, and if we find them, flag the
3874 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3875 -- suppress unwanted warnings. For the case of the function stub above
3876 -- we have a special test to set X as apparently assigned to suppress
3877 -- the warning.
3879 declare
3880 Stm : Node_Id;
3882 begin
3883 -- Skip initial labels (for one thing this occurs when we are in
3884 -- front end ZCX mode, but in any case it is irrelevant), and also
3885 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3887 Stm := First (Statements (HSS));
3888 while Nkind (Stm) = N_Label
3889 or else Nkind (Stm) in N_Push_xxx_Label
3890 loop
3891 Next (Stm);
3892 end loop;
3894 -- Do the test on the original statement before expansion
3896 declare
3897 Ostm : constant Node_Id := Original_Node (Stm);
3899 begin
3900 -- If explicit raise statement, turn on flag
3902 if Nkind (Ostm) = N_Raise_Statement then
3903 Set_Trivial_Subprogram (Stm);
3905 -- If null statement, and no following statements, turn on flag
3907 elsif Nkind (Stm) = N_Null_Statement
3908 and then Comes_From_Source (Stm)
3909 and then No (Next (Stm))
3910 then
3911 Set_Trivial_Subprogram (Stm);
3913 -- Check for explicit call cases which likely raise an exception
3915 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3916 if Is_Entity_Name (Name (Ostm)) then
3917 declare
3918 Ent : constant Entity_Id := Entity (Name (Ostm));
3920 begin
3921 -- If the procedure is marked No_Return, then likely it
3922 -- raises an exception, but in any case it is not coming
3923 -- back here, so turn on the flag.
3925 if Present (Ent)
3926 and then Ekind (Ent) = E_Procedure
3927 and then No_Return (Ent)
3928 then
3929 Set_Trivial_Subprogram (Stm);
3930 end if;
3931 end;
3932 end if;
3933 end if;
3934 end;
3935 end;
3937 -- Check for variables that are never modified
3939 declare
3940 E1, E2 : Entity_Id;
3942 begin
3943 -- If there is a separate spec, then transfer Never_Set_In_Source
3944 -- flags from out parameters to the corresponding entities in the
3945 -- body. The reason we do that is we want to post error flags on
3946 -- the body entities, not the spec entities.
3948 if Present (Spec_Id) then
3949 E1 := First_Entity (Spec_Id);
3950 while Present (E1) loop
3951 if Ekind (E1) = E_Out_Parameter then
3952 E2 := First_Entity (Body_Id);
3953 while Present (E2) loop
3954 exit when Chars (E1) = Chars (E2);
3955 Next_Entity (E2);
3956 end loop;
3958 if Present (E2) then
3959 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
3960 end if;
3961 end if;
3963 Next_Entity (E1);
3964 end loop;
3965 end if;
3967 -- Check references in body
3969 Check_References (Body_Id);
3970 end;
3971 end Analyze_Subprogram_Body_Helper;
3973 ---------------------------------
3974 -- Analyze_Subprogram_Contract --
3975 ---------------------------------
3977 procedure Analyze_Subprogram_Contract (Subp : Entity_Id) is
3978 Items : constant Node_Id := Contract (Subp);
3979 Case_Prag : Node_Id := Empty;
3980 Depends : Node_Id := Empty;
3981 Global : Node_Id := Empty;
3982 Mode : SPARK_Mode_Type;
3983 Nam : Name_Id;
3984 Post_Prag : Node_Id := Empty;
3985 Prag : Node_Id;
3986 Seen_In_Case : Boolean := False;
3987 Seen_In_Post : Boolean := False;
3989 begin
3990 -- Due to the timing of contract analysis, delayed pragmas may be
3991 -- subject to the wrong SPARK_Mode, usually that of the enclosing
3992 -- context. To remedy this, restore the original SPARK_Mode of the
3993 -- related subprogram body.
3995 Save_SPARK_Mode_And_Set (Subp, Mode);
3997 if Present (Items) then
3999 -- Analyze pre- and postconditions
4001 Prag := Pre_Post_Conditions (Items);
4002 while Present (Prag) loop
4003 Analyze_Pre_Post_Condition_In_Decl_Part (Prag, Subp);
4005 -- Verify whether a postcondition mentions attribute 'Result and
4006 -- its expression introduces a post-state.
4008 if Warn_On_Suspicious_Contract
4009 and then Pragma_Name (Prag) = Name_Postcondition
4010 then
4011 Post_Prag := Prag;
4012 Check_Result_And_Post_State (Prag, Seen_In_Post);
4013 end if;
4015 Prag := Next_Pragma (Prag);
4016 end loop;
4018 -- Analyze contract-cases and test-cases
4020 Prag := Contract_Test_Cases (Items);
4021 while Present (Prag) loop
4022 Nam := Pragma_Name (Prag);
4024 if Nam = Name_Contract_Cases then
4025 Analyze_Contract_Cases_In_Decl_Part (Prag);
4027 -- Verify whether contract-cases mention attribute 'Result and
4028 -- its expression introduces a post-state. Perform the check
4029 -- only when the pragma is legal.
4031 if Warn_On_Suspicious_Contract
4032 and then not Error_Posted (Prag)
4033 then
4034 Case_Prag := Prag;
4035 Check_Result_And_Post_State (Prag, Seen_In_Case);
4036 end if;
4038 else
4039 pragma Assert (Nam = Name_Test_Case);
4040 Analyze_Test_Case_In_Decl_Part (Prag, Subp);
4041 end if;
4043 Prag := Next_Pragma (Prag);
4044 end loop;
4046 -- Analyze classification pragmas
4048 Prag := Classifications (Items);
4049 while Present (Prag) loop
4050 Nam := Pragma_Name (Prag);
4052 if Nam = Name_Depends then
4053 Depends := Prag;
4054 else pragma Assert (Nam = Name_Global);
4055 Global := Prag;
4056 end if;
4058 Prag := Next_Pragma (Prag);
4059 end loop;
4061 -- Analyze Global first as Depends may mention items classified in
4062 -- the global categorization.
4064 if Present (Global) then
4065 Analyze_Global_In_Decl_Part (Global);
4066 end if;
4068 -- Depends must be analyzed after Global in order to see the modes of
4069 -- all global items.
4071 if Present (Depends) then
4072 Analyze_Depends_In_Decl_Part (Depends);
4073 end if;
4074 end if;
4076 -- Emit an error when neither the postconditions nor the contract-cases
4077 -- mention attribute 'Result in the context of a function.
4079 if Warn_On_Suspicious_Contract
4080 and then Ekind_In (Subp, E_Function, E_Generic_Function)
4081 then
4082 if Present (Case_Prag)
4083 and then not Seen_In_Case
4084 and then Present (Post_Prag)
4085 and then not Seen_In_Post
4086 then
4087 Error_Msg_N
4088 ("neither function postcondition nor contract cases mention "
4089 & "result?T?", Post_Prag);
4091 elsif Present (Case_Prag) and then not Seen_In_Case then
4092 Error_Msg_N
4093 ("contract cases do not mention result?T?", Case_Prag);
4095 -- OK if we have at least one IN OUT parameter
4097 elsif Present (Post_Prag) and then not Seen_In_Post then
4098 declare
4099 F : Entity_Id;
4100 begin
4101 F := First_Formal (Subp);
4102 while Present (F) loop
4103 if Ekind (F) = E_In_Out_Parameter then
4104 return;
4105 else
4106 Next_Formal (F);
4107 end if;
4108 end loop;
4109 end;
4111 -- If no in-out parameters and no mention of Result, the contract
4112 -- is certainly suspicious.
4114 Error_Msg_N
4115 ("function postcondition does not mention result?T?", Post_Prag);
4116 end if;
4117 end if;
4119 -- Restore the SPARK_Mode of the enclosing context after all delayed
4120 -- pragmas have been analyzed.
4122 Restore_SPARK_Mode (Mode);
4123 end Analyze_Subprogram_Contract;
4125 ------------------------------------
4126 -- Analyze_Subprogram_Declaration --
4127 ------------------------------------
4129 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4130 Scop : constant Entity_Id := Current_Scope;
4131 Designator : Entity_Id;
4133 Is_Completion : Boolean;
4134 -- Indicates whether a null procedure declaration is a completion
4136 begin
4137 -- Null procedures are not allowed in SPARK
4139 if Nkind (Specification (N)) = N_Procedure_Specification
4140 and then Null_Present (Specification (N))
4141 then
4142 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4144 if Is_Protected_Type (Current_Scope) then
4145 Error_Msg_N ("protected operation cannot be a null procedure", N);
4146 end if;
4148 Analyze_Null_Procedure (N, Is_Completion);
4150 if Is_Completion then
4152 -- The null procedure acts as a body, nothing further is needed.
4154 return;
4155 end if;
4156 end if;
4158 Designator := Analyze_Subprogram_Specification (Specification (N));
4160 -- A reference may already have been generated for the unit name, in
4161 -- which case the following call is redundant. However it is needed for
4162 -- declarations that are the rewriting of an expression function.
4164 Generate_Definition (Designator);
4166 -- Set SPARK mode from current context (may be overwritten later with
4167 -- explicit pragma).
4169 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4170 Set_SPARK_Pragma_Inherited (Designator, True);
4172 if Debug_Flag_C then
4173 Write_Str ("==> subprogram spec ");
4174 Write_Name (Chars (Designator));
4175 Write_Str (" from ");
4176 Write_Location (Sloc (N));
4177 Write_Eol;
4178 Indent;
4179 end if;
4181 Validate_RCI_Subprogram_Declaration (N);
4182 New_Overloaded_Entity (Designator);
4183 Check_Delayed_Subprogram (Designator);
4185 -- If the type of the first formal of the current subprogram is a non-
4186 -- generic tagged private type, mark the subprogram as being a private
4187 -- primitive. Ditto if this is a function with controlling result, and
4188 -- the return type is currently private. In both cases, the type of the
4189 -- controlling argument or result must be in the current scope for the
4190 -- operation to be primitive.
4192 if Has_Controlling_Result (Designator)
4193 and then Is_Private_Type (Etype (Designator))
4194 and then Scope (Etype (Designator)) = Current_Scope
4195 and then not Is_Generic_Actual_Type (Etype (Designator))
4196 then
4197 Set_Is_Private_Primitive (Designator);
4199 elsif Present (First_Formal (Designator)) then
4200 declare
4201 Formal_Typ : constant Entity_Id :=
4202 Etype (First_Formal (Designator));
4203 begin
4204 Set_Is_Private_Primitive (Designator,
4205 Is_Tagged_Type (Formal_Typ)
4206 and then Scope (Formal_Typ) = Current_Scope
4207 and then Is_Private_Type (Formal_Typ)
4208 and then not Is_Generic_Actual_Type (Formal_Typ));
4209 end;
4210 end if;
4212 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4213 -- or null.
4215 if Ada_Version >= Ada_2005
4216 and then Comes_From_Source (N)
4217 and then Is_Dispatching_Operation (Designator)
4218 then
4219 declare
4220 E : Entity_Id;
4221 Etyp : Entity_Id;
4223 begin
4224 if Has_Controlling_Result (Designator) then
4225 Etyp := Etype (Designator);
4227 else
4228 E := First_Entity (Designator);
4229 while Present (E)
4230 and then Is_Formal (E)
4231 and then not Is_Controlling_Formal (E)
4232 loop
4233 Next_Entity (E);
4234 end loop;
4236 Etyp := Etype (E);
4237 end if;
4239 if Is_Access_Type (Etyp) then
4240 Etyp := Directly_Designated_Type (Etyp);
4241 end if;
4243 if Is_Interface (Etyp)
4244 and then not Is_Abstract_Subprogram (Designator)
4245 and then not (Ekind (Designator) = E_Procedure
4246 and then Null_Present (Specification (N)))
4247 then
4248 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4250 -- Specialize error message based on procedures vs. functions,
4251 -- since functions can't be null subprograms.
4253 if Ekind (Designator) = E_Procedure then
4254 Error_Msg_N
4255 ("interface procedure % must be abstract or null", N);
4256 else
4257 Error_Msg_N
4258 ("interface function % must be abstract", N);
4259 end if;
4260 end if;
4261 end;
4262 end if;
4264 -- What is the following code for, it used to be
4266 -- ??? Set_Suppress_Elaboration_Checks
4267 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4269 -- The following seems equivalent, but a bit dubious
4271 if Elaboration_Checks_Suppressed (Designator) then
4272 Set_Kill_Elaboration_Checks (Designator);
4273 end if;
4275 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4276 Set_Categorization_From_Scope (Designator, Scop);
4278 else
4279 -- For a compilation unit, check for library-unit pragmas
4281 Push_Scope (Designator);
4282 Set_Categorization_From_Pragmas (N);
4283 Validate_Categorization_Dependency (N, Designator);
4284 Pop_Scope;
4285 end if;
4287 -- For a compilation unit, set body required. This flag will only be
4288 -- reset if a valid Import or Interface pragma is processed later on.
4290 if Nkind (Parent (N)) = N_Compilation_Unit then
4291 Set_Body_Required (Parent (N), True);
4293 if Ada_Version >= Ada_2005
4294 and then Nkind (Specification (N)) = N_Procedure_Specification
4295 and then Null_Present (Specification (N))
4296 then
4297 Error_Msg_N
4298 ("null procedure cannot be declared at library level", N);
4299 end if;
4300 end if;
4302 Generate_Reference_To_Formals (Designator);
4303 Check_Eliminated (Designator);
4305 if Debug_Flag_C then
4306 Outdent;
4307 Write_Str ("<== subprogram spec ");
4308 Write_Name (Chars (Designator));
4309 Write_Str (" from ");
4310 Write_Location (Sloc (N));
4311 Write_Eol;
4312 end if;
4314 if Is_Protected_Type (Current_Scope) then
4316 -- Indicate that this is a protected operation, because it may be
4317 -- used in subsequent declarations within the protected type.
4319 Set_Convention (Designator, Convention_Protected);
4320 end if;
4322 List_Inherited_Pre_Post_Aspects (Designator);
4324 if Has_Aspects (N) then
4325 Analyze_Aspect_Specifications (N, Designator);
4326 end if;
4327 end Analyze_Subprogram_Declaration;
4329 --------------------------------------
4330 -- Analyze_Subprogram_Specification --
4331 --------------------------------------
4333 -- Reminder: N here really is a subprogram specification (not a subprogram
4334 -- declaration). This procedure is called to analyze the specification in
4335 -- both subprogram bodies and subprogram declarations (specs).
4337 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4338 Designator : constant Entity_Id := Defining_Entity (N);
4339 Formals : constant List_Id := Parameter_Specifications (N);
4341 -- Start of processing for Analyze_Subprogram_Specification
4343 begin
4344 -- User-defined operator is not allowed in SPARK, except as a renaming
4346 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4347 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4348 then
4349 Check_SPARK_05_Restriction
4350 ("user-defined operator is not allowed", N);
4351 end if;
4353 -- Proceed with analysis. Do not emit a cross-reference entry if the
4354 -- specification comes from an expression function, because it may be
4355 -- the completion of a previous declaration. It is is not, the cross-
4356 -- reference entry will be emitted for the new subprogram declaration.
4358 if Nkind (Parent (N)) /= N_Expression_Function then
4359 Generate_Definition (Designator);
4360 end if;
4362 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
4364 if Nkind (N) = N_Function_Specification then
4365 Set_Ekind (Designator, E_Function);
4366 Set_Mechanism (Designator, Default_Mechanism);
4367 else
4368 Set_Ekind (Designator, E_Procedure);
4369 Set_Etype (Designator, Standard_Void_Type);
4370 end if;
4372 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4373 -- those subprograms which could be inlined in GNATprove mode (because
4374 -- Body_To_Inline is non-Empty) but cannot be inlined.
4376 if GNATprove_Mode then
4377 Set_Is_Inlined_Always (Designator);
4378 end if;
4380 -- Introduce new scope for analysis of the formals and the return type
4382 Set_Scope (Designator, Current_Scope);
4384 if Present (Formals) then
4385 Push_Scope (Designator);
4386 Process_Formals (Formals, N);
4388 -- Check dimensions in N for formals with default expression
4390 Analyze_Dimension_Formals (N, Formals);
4392 -- Ada 2005 (AI-345): If this is an overriding operation of an
4393 -- inherited interface operation, and the controlling type is
4394 -- a synchronized type, replace the type with its corresponding
4395 -- record, to match the proper signature of an overriding operation.
4396 -- Same processing for an access parameter whose designated type is
4397 -- derived from a synchronized interface.
4399 if Ada_Version >= Ada_2005 then
4400 declare
4401 Formal : Entity_Id;
4402 Formal_Typ : Entity_Id;
4403 Rec_Typ : Entity_Id;
4404 Desig_Typ : Entity_Id;
4406 begin
4407 Formal := First_Formal (Designator);
4408 while Present (Formal) loop
4409 Formal_Typ := Etype (Formal);
4411 if Is_Concurrent_Type (Formal_Typ)
4412 and then Present (Corresponding_Record_Type (Formal_Typ))
4413 then
4414 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4416 if Present (Interfaces (Rec_Typ)) then
4417 Set_Etype (Formal, Rec_Typ);
4418 end if;
4420 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4421 Desig_Typ := Designated_Type (Formal_Typ);
4423 if Is_Concurrent_Type (Desig_Typ)
4424 and then Present (Corresponding_Record_Type (Desig_Typ))
4425 then
4426 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4428 if Present (Interfaces (Rec_Typ)) then
4429 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4430 end if;
4431 end if;
4432 end if;
4434 Next_Formal (Formal);
4435 end loop;
4436 end;
4437 end if;
4439 End_Scope;
4441 -- The subprogram scope is pushed and popped around the processing of
4442 -- the return type for consistency with call above to Process_Formals
4443 -- (which itself can call Analyze_Return_Type), and to ensure that any
4444 -- itype created for the return type will be associated with the proper
4445 -- scope.
4447 elsif Nkind (N) = N_Function_Specification then
4448 Push_Scope (Designator);
4449 Analyze_Return_Type (N);
4450 End_Scope;
4451 end if;
4453 -- Function case
4455 if Nkind (N) = N_Function_Specification then
4457 -- Deal with operator symbol case
4459 if Nkind (Designator) = N_Defining_Operator_Symbol then
4460 Valid_Operator_Definition (Designator);
4461 end if;
4463 May_Need_Actuals (Designator);
4465 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4466 -- the subprogram is abstract also. This does not apply to renaming
4467 -- declarations, where abstractness is inherited, and to subprogram
4468 -- bodies generated for stream operations, which become renamings as
4469 -- bodies.
4471 -- In case of primitives associated with abstract interface types
4472 -- the check is applied later (see Analyze_Subprogram_Declaration).
4474 if not Nkind_In (Original_Node (Parent (N)),
4475 N_Subprogram_Renaming_Declaration,
4476 N_Abstract_Subprogram_Declaration,
4477 N_Formal_Abstract_Subprogram_Declaration)
4478 then
4479 if Is_Abstract_Type (Etype (Designator))
4480 and then not Is_Interface (Etype (Designator))
4481 then
4482 Error_Msg_N
4483 ("function that returns abstract type must be abstract", N);
4485 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4486 -- access result whose designated type is abstract.
4488 elsif Nkind (Result_Definition (N)) = N_Access_Definition
4489 and then
4490 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4491 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4492 and then Ada_Version >= Ada_2012
4493 then
4494 Error_Msg_N ("function whose access result designates "
4495 & "abstract type must be abstract", N);
4496 end if;
4497 end if;
4498 end if;
4500 return Designator;
4501 end Analyze_Subprogram_Specification;
4503 -----------------------
4504 -- Check_Conformance --
4505 -----------------------
4507 procedure Check_Conformance
4508 (New_Id : Entity_Id;
4509 Old_Id : Entity_Id;
4510 Ctype : Conformance_Type;
4511 Errmsg : Boolean;
4512 Conforms : out Boolean;
4513 Err_Loc : Node_Id := Empty;
4514 Get_Inst : Boolean := False;
4515 Skip_Controlling_Formals : Boolean := False)
4517 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
4518 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4519 -- If Errmsg is True, then processing continues to post an error message
4520 -- for conformance error on given node. Two messages are output. The
4521 -- first message points to the previous declaration with a general "no
4522 -- conformance" message. The second is the detailed reason, supplied as
4523 -- Msg. The parameter N provide information for a possible & insertion
4524 -- in the message, and also provides the location for posting the
4525 -- message in the absence of a specified Err_Loc location.
4527 -----------------------
4528 -- Conformance_Error --
4529 -----------------------
4531 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4532 Enode : Node_Id;
4534 begin
4535 Conforms := False;
4537 if Errmsg then
4538 if No (Err_Loc) then
4539 Enode := N;
4540 else
4541 Enode := Err_Loc;
4542 end if;
4544 Error_Msg_Sloc := Sloc (Old_Id);
4546 case Ctype is
4547 when Type_Conformant =>
4548 Error_Msg_N -- CODEFIX
4549 ("not type conformant with declaration#!", Enode);
4551 when Mode_Conformant =>
4552 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4553 Error_Msg_N
4554 ("not mode conformant with operation inherited#!",
4555 Enode);
4556 else
4557 Error_Msg_N
4558 ("not mode conformant with declaration#!", Enode);
4559 end if;
4561 when Subtype_Conformant =>
4562 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4563 Error_Msg_N
4564 ("not subtype conformant with operation inherited#!",
4565 Enode);
4566 else
4567 Error_Msg_N
4568 ("not subtype conformant with declaration#!", Enode);
4569 end if;
4571 when Fully_Conformant =>
4572 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4573 Error_Msg_N -- CODEFIX
4574 ("not fully conformant with operation inherited#!",
4575 Enode);
4576 else
4577 Error_Msg_N -- CODEFIX
4578 ("not fully conformant with declaration#!", Enode);
4579 end if;
4580 end case;
4582 Error_Msg_NE (Msg, Enode, N);
4583 end if;
4584 end Conformance_Error;
4586 -- Local Variables
4588 Old_Type : constant Entity_Id := Etype (Old_Id);
4589 New_Type : constant Entity_Id := Etype (New_Id);
4590 Old_Formal : Entity_Id;
4591 New_Formal : Entity_Id;
4592 Access_Types_Match : Boolean;
4593 Old_Formal_Base : Entity_Id;
4594 New_Formal_Base : Entity_Id;
4596 -- Start of processing for Check_Conformance
4598 begin
4599 Conforms := True;
4601 -- We need a special case for operators, since they don't appear
4602 -- explicitly.
4604 if Ctype = Type_Conformant then
4605 if Ekind (New_Id) = E_Operator
4606 and then Operator_Matches_Spec (New_Id, Old_Id)
4607 then
4608 return;
4609 end if;
4610 end if;
4612 -- If both are functions/operators, check return types conform
4614 if Old_Type /= Standard_Void_Type
4615 and then New_Type /= Standard_Void_Type
4616 then
4618 -- If we are checking interface conformance we omit controlling
4619 -- arguments and result, because we are only checking the conformance
4620 -- of the remaining parameters.
4622 if Has_Controlling_Result (Old_Id)
4623 and then Has_Controlling_Result (New_Id)
4624 and then Skip_Controlling_Formals
4625 then
4626 null;
4628 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
4629 if Ctype >= Subtype_Conformant
4630 and then not Predicates_Match (Old_Type, New_Type)
4631 then
4632 Conformance_Error
4633 ("\predicate of return type does not match!", New_Id);
4634 else
4635 Conformance_Error
4636 ("\return type does not match!", New_Id);
4637 end if;
4639 return;
4640 end if;
4642 -- Ada 2005 (AI-231): In case of anonymous access types check the
4643 -- null-exclusion and access-to-constant attributes match.
4645 if Ada_Version >= Ada_2005
4646 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
4647 and then
4648 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
4649 or else Is_Access_Constant (Etype (Old_Type)) /=
4650 Is_Access_Constant (Etype (New_Type)))
4651 then
4652 Conformance_Error ("\return type does not match!", New_Id);
4653 return;
4654 end if;
4656 -- If either is a function/operator and the other isn't, error
4658 elsif Old_Type /= Standard_Void_Type
4659 or else New_Type /= Standard_Void_Type
4660 then
4661 Conformance_Error ("\functions can only match functions!", New_Id);
4662 return;
4663 end if;
4665 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
4666 -- If this is a renaming as body, refine error message to indicate that
4667 -- the conflict is with the original declaration. If the entity is not
4668 -- frozen, the conventions don't have to match, the one of the renamed
4669 -- entity is inherited.
4671 if Ctype >= Subtype_Conformant then
4672 if Convention (Old_Id) /= Convention (New_Id) then
4673 if not Is_Frozen (New_Id) then
4674 null;
4676 elsif Present (Err_Loc)
4677 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
4678 and then Present (Corresponding_Spec (Err_Loc))
4679 then
4680 Error_Msg_Name_1 := Chars (New_Id);
4681 Error_Msg_Name_2 :=
4682 Name_Ada + Convention_Id'Pos (Convention (New_Id));
4683 Conformance_Error ("\prior declaration for% has convention %!");
4685 else
4686 Conformance_Error ("\calling conventions do not match!");
4687 end if;
4689 return;
4691 elsif Is_Formal_Subprogram (Old_Id)
4692 or else Is_Formal_Subprogram (New_Id)
4693 then
4694 Conformance_Error ("\formal subprograms not allowed!");
4695 return;
4696 end if;
4697 end if;
4699 -- Deal with parameters
4701 -- Note: we use the entity information, rather than going directly
4702 -- to the specification in the tree. This is not only simpler, but
4703 -- absolutely necessary for some cases of conformance tests between
4704 -- operators, where the declaration tree simply does not exist.
4706 Old_Formal := First_Formal (Old_Id);
4707 New_Formal := First_Formal (New_Id);
4708 while Present (Old_Formal) and then Present (New_Formal) loop
4709 if Is_Controlling_Formal (Old_Formal)
4710 and then Is_Controlling_Formal (New_Formal)
4711 and then Skip_Controlling_Formals
4712 then
4713 -- The controlling formals will have different types when
4714 -- comparing an interface operation with its match, but both
4715 -- or neither must be access parameters.
4717 if Is_Access_Type (Etype (Old_Formal))
4719 Is_Access_Type (Etype (New_Formal))
4720 then
4721 goto Skip_Controlling_Formal;
4722 else
4723 Conformance_Error
4724 ("\access parameter does not match!", New_Formal);
4725 end if;
4726 end if;
4728 -- Ada 2012: Mode conformance also requires that formal parameters
4729 -- be both aliased, or neither.
4731 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
4732 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
4733 Conformance_Error
4734 ("\aliased parameter mismatch!", New_Formal);
4735 end if;
4736 end if;
4738 if Ctype = Fully_Conformant then
4740 -- Names must match. Error message is more accurate if we do
4741 -- this before checking that the types of the formals match.
4743 if Chars (Old_Formal) /= Chars (New_Formal) then
4744 Conformance_Error ("\name& does not match!", New_Formal);
4746 -- Set error posted flag on new formal as well to stop
4747 -- junk cascaded messages in some cases.
4749 Set_Error_Posted (New_Formal);
4750 return;
4751 end if;
4753 -- Null exclusion must match
4755 if Null_Exclusion_Present (Parent (Old_Formal))
4757 Null_Exclusion_Present (Parent (New_Formal))
4758 then
4759 -- Only give error if both come from source. This should be
4760 -- investigated some time, since it should not be needed ???
4762 if Comes_From_Source (Old_Formal)
4763 and then
4764 Comes_From_Source (New_Formal)
4765 then
4766 Conformance_Error
4767 ("\null exclusion for& does not match", New_Formal);
4769 -- Mark error posted on the new formal to avoid duplicated
4770 -- complaint about types not matching.
4772 Set_Error_Posted (New_Formal);
4773 end if;
4774 end if;
4775 end if;
4777 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
4778 -- case occurs whenever a subprogram is being renamed and one of its
4779 -- parameters imposes a null exclusion. For example:
4781 -- type T is null record;
4782 -- type Acc_T is access T;
4783 -- subtype Acc_T_Sub is Acc_T;
4785 -- procedure P (Obj : not null Acc_T_Sub); -- itype
4786 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
4787 -- renames P;
4789 Old_Formal_Base := Etype (Old_Formal);
4790 New_Formal_Base := Etype (New_Formal);
4792 if Get_Inst then
4793 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
4794 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
4795 end if;
4797 Access_Types_Match := Ada_Version >= Ada_2005
4799 -- Ensure that this rule is only applied when New_Id is a
4800 -- renaming of Old_Id.
4802 and then Nkind (Parent (Parent (New_Id))) =
4803 N_Subprogram_Renaming_Declaration
4804 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
4805 and then Present (Entity (Name (Parent (Parent (New_Id)))))
4806 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
4808 -- Now handle the allowed access-type case
4810 and then Is_Access_Type (Old_Formal_Base)
4811 and then Is_Access_Type (New_Formal_Base)
4813 -- The type kinds must match. The only exception occurs with
4814 -- multiple generics of the form:
4816 -- generic generic
4817 -- type F is private; type A is private;
4818 -- type F_Ptr is access F; type A_Ptr is access A;
4819 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
4820 -- package F_Pack is ... package A_Pack is
4821 -- package F_Inst is
4822 -- new F_Pack (A, A_Ptr, A_P);
4824 -- When checking for conformance between the parameters of A_P
4825 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
4826 -- because the compiler has transformed A_Ptr into a subtype of
4827 -- F_Ptr. We catch this case in the code below.
4829 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
4830 or else
4831 (Is_Generic_Type (Old_Formal_Base)
4832 and then Is_Generic_Type (New_Formal_Base)
4833 and then Is_Internal (New_Formal_Base)
4834 and then Etype (Etype (New_Formal_Base)) =
4835 Old_Formal_Base))
4836 and then Directly_Designated_Type (Old_Formal_Base) =
4837 Directly_Designated_Type (New_Formal_Base)
4838 and then ((Is_Itype (Old_Formal_Base)
4839 and then Can_Never_Be_Null (Old_Formal_Base))
4840 or else
4841 (Is_Itype (New_Formal_Base)
4842 and then Can_Never_Be_Null (New_Formal_Base)));
4844 -- Types must always match. In the visible part of an instance,
4845 -- usual overloading rules for dispatching operations apply, and
4846 -- we check base types (not the actual subtypes).
4848 if In_Instance_Visible_Part
4849 and then Is_Dispatching_Operation (New_Id)
4850 then
4851 if not Conforming_Types
4852 (T1 => Base_Type (Etype (Old_Formal)),
4853 T2 => Base_Type (Etype (New_Formal)),
4854 Ctype => Ctype,
4855 Get_Inst => Get_Inst)
4856 and then not Access_Types_Match
4857 then
4858 Conformance_Error ("\type of & does not match!", New_Formal);
4859 return;
4860 end if;
4862 elsif not Conforming_Types
4863 (T1 => Old_Formal_Base,
4864 T2 => New_Formal_Base,
4865 Ctype => Ctype,
4866 Get_Inst => Get_Inst)
4867 and then not Access_Types_Match
4868 then
4869 -- Don't give error message if old type is Any_Type. This test
4870 -- avoids some cascaded errors, e.g. in case of a bad spec.
4872 if Errmsg and then Old_Formal_Base = Any_Type then
4873 Conforms := False;
4874 else
4875 if Ctype >= Subtype_Conformant
4876 and then
4877 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
4878 then
4879 Conformance_Error
4880 ("\predicate of & does not match!", New_Formal);
4881 else
4882 Conformance_Error
4883 ("\type of & does not match!", New_Formal);
4884 end if;
4885 end if;
4887 return;
4888 end if;
4890 -- For mode conformance, mode must match
4892 if Ctype >= Mode_Conformant then
4893 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
4894 if not Ekind_In (New_Id, E_Function, E_Procedure)
4895 or else not Is_Primitive_Wrapper (New_Id)
4896 then
4897 Conformance_Error ("\mode of & does not match!", New_Formal);
4899 else
4900 declare
4901 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
4902 begin
4903 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
4904 then
4905 Error_Msg_PT (T, New_Id);
4906 else
4907 Conformance_Error
4908 ("\mode of & does not match!", New_Formal);
4909 end if;
4910 end;
4911 end if;
4913 return;
4915 -- Part of mode conformance for access types is having the same
4916 -- constant modifier.
4918 elsif Access_Types_Match
4919 and then Is_Access_Constant (Old_Formal_Base) /=
4920 Is_Access_Constant (New_Formal_Base)
4921 then
4922 Conformance_Error
4923 ("\constant modifier does not match!", New_Formal);
4924 return;
4925 end if;
4926 end if;
4928 if Ctype >= Subtype_Conformant then
4930 -- Ada 2005 (AI-231): In case of anonymous access types check
4931 -- the null-exclusion and access-to-constant attributes must
4932 -- match. For null exclusion, we test the types rather than the
4933 -- formals themselves, since the attribute is only set reliably
4934 -- on the formals in the Ada 95 case, and we exclude the case
4935 -- where Old_Formal is marked as controlling, to avoid errors
4936 -- when matching completing bodies with dispatching declarations
4937 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
4939 if Ada_Version >= Ada_2005
4940 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
4941 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
4942 and then
4943 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
4944 Can_Never_Be_Null (Etype (New_Formal))
4945 and then
4946 not Is_Controlling_Formal (Old_Formal))
4947 or else
4948 Is_Access_Constant (Etype (Old_Formal)) /=
4949 Is_Access_Constant (Etype (New_Formal)))
4951 -- Do not complain if error already posted on New_Formal. This
4952 -- avoids some redundant error messages.
4954 and then not Error_Posted (New_Formal)
4955 then
4956 -- It is allowed to omit the null-exclusion in case of stream
4957 -- attribute subprograms. We recognize stream subprograms
4958 -- through their TSS-generated suffix.
4960 declare
4961 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
4963 begin
4964 if TSS_Name /= TSS_Stream_Read
4965 and then TSS_Name /= TSS_Stream_Write
4966 and then TSS_Name /= TSS_Stream_Input
4967 and then TSS_Name /= TSS_Stream_Output
4968 then
4969 -- Here we have a definite conformance error. It is worth
4970 -- special casing the error message for the case of a
4971 -- controlling formal (which excludes null).
4973 if Is_Controlling_Formal (New_Formal) then
4974 Error_Msg_Node_2 := Scope (New_Formal);
4975 Conformance_Error
4976 ("\controlling formal & of & excludes null, "
4977 & "declaration must exclude null as well",
4978 New_Formal);
4980 -- Normal case (couldn't we give more detail here???)
4982 else
4983 Conformance_Error
4984 ("\type of & does not match!", New_Formal);
4985 end if;
4987 return;
4988 end if;
4989 end;
4990 end if;
4991 end if;
4993 -- Full conformance checks
4995 if Ctype = Fully_Conformant then
4997 -- We have checked already that names match
4999 if Parameter_Mode (Old_Formal) = E_In_Parameter then
5001 -- Check default expressions for in parameters
5003 declare
5004 NewD : constant Boolean :=
5005 Present (Default_Value (New_Formal));
5006 OldD : constant Boolean :=
5007 Present (Default_Value (Old_Formal));
5008 begin
5009 if NewD or OldD then
5011 -- The old default value has been analyzed because the
5012 -- current full declaration will have frozen everything
5013 -- before. The new default value has not been analyzed,
5014 -- so analyze it now before we check for conformance.
5016 if NewD then
5017 Push_Scope (New_Id);
5018 Preanalyze_Spec_Expression
5019 (Default_Value (New_Formal), Etype (New_Formal));
5020 End_Scope;
5021 end if;
5023 if not (NewD and OldD)
5024 or else not Fully_Conformant_Expressions
5025 (Default_Value (Old_Formal),
5026 Default_Value (New_Formal))
5027 then
5028 Conformance_Error
5029 ("\default expression for & does not match!",
5030 New_Formal);
5031 return;
5032 end if;
5033 end if;
5034 end;
5035 end if;
5036 end if;
5038 -- A couple of special checks for Ada 83 mode. These checks are
5039 -- skipped if either entity is an operator in package Standard,
5040 -- or if either old or new instance is not from the source program.
5042 if Ada_Version = Ada_83
5043 and then Sloc (Old_Id) > Standard_Location
5044 and then Sloc (New_Id) > Standard_Location
5045 and then Comes_From_Source (Old_Id)
5046 and then Comes_From_Source (New_Id)
5047 then
5048 declare
5049 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5050 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5052 begin
5053 -- Explicit IN must be present or absent in both cases. This
5054 -- test is required only in the full conformance case.
5056 if In_Present (Old_Param) /= In_Present (New_Param)
5057 and then Ctype = Fully_Conformant
5058 then
5059 Conformance_Error
5060 ("\(Ada 83) IN must appear in both declarations",
5061 New_Formal);
5062 return;
5063 end if;
5065 -- Grouping (use of comma in param lists) must be the same
5066 -- This is where we catch a misconformance like:
5068 -- A, B : Integer
5069 -- A : Integer; B : Integer
5071 -- which are represented identically in the tree except
5072 -- for the setting of the flags More_Ids and Prev_Ids.
5074 if More_Ids (Old_Param) /= More_Ids (New_Param)
5075 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5076 then
5077 Conformance_Error
5078 ("\grouping of & does not match!", New_Formal);
5079 return;
5080 end if;
5081 end;
5082 end if;
5084 -- This label is required when skipping controlling formals
5086 <<Skip_Controlling_Formal>>
5088 Next_Formal (Old_Formal);
5089 Next_Formal (New_Formal);
5090 end loop;
5092 if Present (Old_Formal) then
5093 Conformance_Error ("\too few parameters!");
5094 return;
5096 elsif Present (New_Formal) then
5097 Conformance_Error ("\too many parameters!", New_Formal);
5098 return;
5099 end if;
5100 end Check_Conformance;
5102 -----------------------
5103 -- Check_Conventions --
5104 -----------------------
5106 procedure Check_Conventions (Typ : Entity_Id) is
5107 Ifaces_List : Elist_Id;
5109 procedure Check_Convention (Op : Entity_Id);
5110 -- Verify that the convention of inherited dispatching operation Op is
5111 -- consistent among all subprograms it overrides. In order to minimize
5112 -- the search, Search_From is utilized to designate a specific point in
5113 -- the list rather than iterating over the whole list once more.
5115 ----------------------
5116 -- Check_Convention --
5117 ----------------------
5119 procedure Check_Convention (Op : Entity_Id) is
5120 function Convention_Of (Id : Entity_Id) return Convention_Id;
5121 -- Given an entity, return its convention. The function treats Ghost
5122 -- as convention Ada because the two have the same dynamic semantics.
5124 -------------------
5125 -- Convention_Of --
5126 -------------------
5128 function Convention_Of (Id : Entity_Id) return Convention_Id is
5129 Conv : constant Convention_Id := Convention (Id);
5130 begin
5131 if Conv = Convention_Ghost then
5132 return Convention_Ada;
5133 else
5134 return Conv;
5135 end if;
5136 end Convention_Of;
5138 -- Local variables
5140 Op_Conv : constant Convention_Id := Convention_Of (Op);
5141 Iface_Conv : Convention_Id;
5142 Iface_Elmt : Elmt_Id;
5143 Iface_Prim_Elmt : Elmt_Id;
5144 Iface_Prim : Entity_Id;
5146 -- Start of processing for Check_Convention
5148 begin
5149 Iface_Elmt := First_Elmt (Ifaces_List);
5150 while Present (Iface_Elmt) loop
5151 Iface_Prim_Elmt :=
5152 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5153 while Present (Iface_Prim_Elmt) loop
5154 Iface_Prim := Node (Iface_Prim_Elmt);
5155 Iface_Conv := Convention_Of (Iface_Prim);
5157 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5158 and then Iface_Conv /= Op_Conv
5159 then
5160 Error_Msg_N
5161 ("inconsistent conventions in primitive operations", Typ);
5163 Error_Msg_Name_1 := Chars (Op);
5164 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5165 Error_Msg_Sloc := Sloc (Op);
5167 if Comes_From_Source (Op) or else No (Alias (Op)) then
5168 if not Present (Overridden_Operation (Op)) then
5169 Error_Msg_N ("\\primitive % defined #", Typ);
5170 else
5171 Error_Msg_N
5172 ("\\overriding operation % with "
5173 & "convention % defined #", Typ);
5174 end if;
5176 else pragma Assert (Present (Alias (Op)));
5177 Error_Msg_Sloc := Sloc (Alias (Op));
5178 Error_Msg_N ("\\inherited operation % with "
5179 & "convention % defined #", Typ);
5180 end if;
5182 Error_Msg_Name_1 := Chars (Op);
5183 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5184 Error_Msg_Sloc := Sloc (Iface_Prim);
5185 Error_Msg_N ("\\overridden operation % with "
5186 & "convention % defined #", Typ);
5188 -- Avoid cascading errors
5190 return;
5191 end if;
5193 Next_Elmt (Iface_Prim_Elmt);
5194 end loop;
5196 Next_Elmt (Iface_Elmt);
5197 end loop;
5198 end Check_Convention;
5200 -- Local variables
5202 Prim_Op : Entity_Id;
5203 Prim_Op_Elmt : Elmt_Id;
5205 -- Start of processing for Check_Conventions
5207 begin
5208 if not Has_Interfaces (Typ) then
5209 return;
5210 end if;
5212 Collect_Interfaces (Typ, Ifaces_List);
5214 -- The algorithm checks every overriding dispatching operation against
5215 -- all the corresponding overridden dispatching operations, detecting
5216 -- differences in conventions.
5218 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5219 while Present (Prim_Op_Elmt) loop
5220 Prim_Op := Node (Prim_Op_Elmt);
5222 -- A small optimization: skip the predefined dispatching operations
5223 -- since they always have the same convention.
5225 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5226 Check_Convention (Prim_Op);
5227 end if;
5229 Next_Elmt (Prim_Op_Elmt);
5230 end loop;
5231 end Check_Conventions;
5233 ------------------------------
5234 -- Check_Delayed_Subprogram --
5235 ------------------------------
5237 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5238 F : Entity_Id;
5240 procedure Possible_Freeze (T : Entity_Id);
5241 -- T is the type of either a formal parameter or of the return type.
5242 -- If T is not yet frozen and needs a delayed freeze, then the
5243 -- subprogram itself must be delayed. If T is the limited view of an
5244 -- incomplete type the subprogram must be frozen as well, because
5245 -- T may depend on local types that have not been frozen yet.
5247 ---------------------
5248 -- Possible_Freeze --
5249 ---------------------
5251 procedure Possible_Freeze (T : Entity_Id) is
5252 begin
5253 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5254 Set_Has_Delayed_Freeze (Designator);
5256 elsif Is_Access_Type (T)
5257 and then Has_Delayed_Freeze (Designated_Type (T))
5258 and then not Is_Frozen (Designated_Type (T))
5259 then
5260 Set_Has_Delayed_Freeze (Designator);
5262 elsif Ekind (T) = E_Incomplete_Type
5263 and then From_Limited_With (T)
5264 then
5265 Set_Has_Delayed_Freeze (Designator);
5267 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
5268 -- of a subprogram or entry declaration.
5270 elsif Ekind (T) = E_Incomplete_Type
5271 and then Ada_Version >= Ada_2012
5272 then
5273 Set_Has_Delayed_Freeze (Designator);
5274 end if;
5276 end Possible_Freeze;
5278 -- Start of processing for Check_Delayed_Subprogram
5280 begin
5281 -- All subprograms, including abstract subprograms, may need a freeze
5282 -- node if some formal type or the return type needs one.
5284 Possible_Freeze (Etype (Designator));
5285 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5287 -- Need delayed freeze if any of the formal types themselves need
5288 -- a delayed freeze and are not yet frozen.
5290 F := First_Formal (Designator);
5291 while Present (F) loop
5292 Possible_Freeze (Etype (F));
5293 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5294 Next_Formal (F);
5295 end loop;
5297 -- Mark functions that return by reference. Note that it cannot be
5298 -- done for delayed_freeze subprograms because the underlying
5299 -- returned type may not be known yet (for private types)
5301 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5302 declare
5303 Typ : constant Entity_Id := Etype (Designator);
5304 Utyp : constant Entity_Id := Underlying_Type (Typ);
5305 begin
5306 if Is_Limited_View (Typ) then
5307 Set_Returns_By_Ref (Designator);
5308 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5309 Set_Returns_By_Ref (Designator);
5310 end if;
5311 end;
5312 end if;
5313 end Check_Delayed_Subprogram;
5315 ------------------------------------
5316 -- Check_Discriminant_Conformance --
5317 ------------------------------------
5319 procedure Check_Discriminant_Conformance
5320 (N : Node_Id;
5321 Prev : Entity_Id;
5322 Prev_Loc : Node_Id)
5324 Old_Discr : Entity_Id := First_Discriminant (Prev);
5325 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5326 New_Discr_Id : Entity_Id;
5327 New_Discr_Type : Entity_Id;
5329 procedure Conformance_Error (Msg : String; N : Node_Id);
5330 -- Post error message for conformance error on given node. Two messages
5331 -- are output. The first points to the previous declaration with a
5332 -- general "no conformance" message. The second is the detailed reason,
5333 -- supplied as Msg. The parameter N provide information for a possible
5334 -- & insertion in the message.
5336 -----------------------
5337 -- Conformance_Error --
5338 -----------------------
5340 procedure Conformance_Error (Msg : String; N : Node_Id) is
5341 begin
5342 Error_Msg_Sloc := Sloc (Prev_Loc);
5343 Error_Msg_N -- CODEFIX
5344 ("not fully conformant with declaration#!", N);
5345 Error_Msg_NE (Msg, N, N);
5346 end Conformance_Error;
5348 -- Start of processing for Check_Discriminant_Conformance
5350 begin
5351 while Present (Old_Discr) and then Present (New_Discr) loop
5352 New_Discr_Id := Defining_Identifier (New_Discr);
5354 -- The subtype mark of the discriminant on the full type has not
5355 -- been analyzed so we do it here. For an access discriminant a new
5356 -- type is created.
5358 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5359 New_Discr_Type :=
5360 Access_Definition (N, Discriminant_Type (New_Discr));
5362 else
5363 Analyze (Discriminant_Type (New_Discr));
5364 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
5366 -- Ada 2005: if the discriminant definition carries a null
5367 -- exclusion, create an itype to check properly for consistency
5368 -- with partial declaration.
5370 if Is_Access_Type (New_Discr_Type)
5371 and then Null_Exclusion_Present (New_Discr)
5372 then
5373 New_Discr_Type :=
5374 Create_Null_Excluding_Itype
5375 (T => New_Discr_Type,
5376 Related_Nod => New_Discr,
5377 Scope_Id => Current_Scope);
5378 end if;
5379 end if;
5381 if not Conforming_Types
5382 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5383 then
5384 Conformance_Error ("type of & does not match!", New_Discr_Id);
5385 return;
5386 else
5387 -- Treat the new discriminant as an occurrence of the old one,
5388 -- for navigation purposes, and fill in some semantic
5389 -- information, for completeness.
5391 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5392 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5393 Set_Scope (New_Discr_Id, Scope (Old_Discr));
5394 end if;
5396 -- Names must match
5398 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5399 Conformance_Error ("name & does not match!", New_Discr_Id);
5400 return;
5401 end if;
5403 -- Default expressions must match
5405 declare
5406 NewD : constant Boolean :=
5407 Present (Expression (New_Discr));
5408 OldD : constant Boolean :=
5409 Present (Expression (Parent (Old_Discr)));
5411 begin
5412 if NewD or OldD then
5414 -- The old default value has been analyzed and expanded,
5415 -- because the current full declaration will have frozen
5416 -- everything before. The new default values have not been
5417 -- expanded, so expand now to check conformance.
5419 if NewD then
5420 Preanalyze_Spec_Expression
5421 (Expression (New_Discr), New_Discr_Type);
5422 end if;
5424 if not (NewD and OldD)
5425 or else not Fully_Conformant_Expressions
5426 (Expression (Parent (Old_Discr)),
5427 Expression (New_Discr))
5429 then
5430 Conformance_Error
5431 ("default expression for & does not match!",
5432 New_Discr_Id);
5433 return;
5434 end if;
5435 end if;
5436 end;
5438 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5440 if Ada_Version = Ada_83 then
5441 declare
5442 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5444 begin
5445 -- Grouping (use of comma in param lists) must be the same
5446 -- This is where we catch a misconformance like:
5448 -- A, B : Integer
5449 -- A : Integer; B : Integer
5451 -- which are represented identically in the tree except
5452 -- for the setting of the flags More_Ids and Prev_Ids.
5454 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
5455 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
5456 then
5457 Conformance_Error
5458 ("grouping of & does not match!", New_Discr_Id);
5459 return;
5460 end if;
5461 end;
5462 end if;
5464 Next_Discriminant (Old_Discr);
5465 Next (New_Discr);
5466 end loop;
5468 if Present (Old_Discr) then
5469 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
5470 return;
5472 elsif Present (New_Discr) then
5473 Conformance_Error
5474 ("too many discriminants!", Defining_Identifier (New_Discr));
5475 return;
5476 end if;
5477 end Check_Discriminant_Conformance;
5479 ----------------------------
5480 -- Check_Fully_Conformant --
5481 ----------------------------
5483 procedure Check_Fully_Conformant
5484 (New_Id : Entity_Id;
5485 Old_Id : Entity_Id;
5486 Err_Loc : Node_Id := Empty)
5488 Result : Boolean;
5489 pragma Warnings (Off, Result);
5490 begin
5491 Check_Conformance
5492 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
5493 end Check_Fully_Conformant;
5495 ---------------------------
5496 -- Check_Mode_Conformant --
5497 ---------------------------
5499 procedure Check_Mode_Conformant
5500 (New_Id : Entity_Id;
5501 Old_Id : Entity_Id;
5502 Err_Loc : Node_Id := Empty;
5503 Get_Inst : Boolean := False)
5505 Result : Boolean;
5506 pragma Warnings (Off, Result);
5507 begin
5508 Check_Conformance
5509 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
5510 end Check_Mode_Conformant;
5512 --------------------------------
5513 -- Check_Overriding_Indicator --
5514 --------------------------------
5516 procedure Check_Overriding_Indicator
5517 (Subp : Entity_Id;
5518 Overridden_Subp : Entity_Id;
5519 Is_Primitive : Boolean)
5521 Decl : Node_Id;
5522 Spec : Node_Id;
5524 begin
5525 -- No overriding indicator for literals
5527 if Ekind (Subp) = E_Enumeration_Literal then
5528 return;
5530 elsif Ekind (Subp) = E_Entry then
5531 Decl := Parent (Subp);
5533 -- No point in analyzing a malformed operator
5535 elsif Nkind (Subp) = N_Defining_Operator_Symbol
5536 and then Error_Posted (Subp)
5537 then
5538 return;
5540 else
5541 Decl := Unit_Declaration_Node (Subp);
5542 end if;
5544 if Nkind_In (Decl, N_Subprogram_Body,
5545 N_Subprogram_Body_Stub,
5546 N_Subprogram_Declaration,
5547 N_Abstract_Subprogram_Declaration,
5548 N_Subprogram_Renaming_Declaration)
5549 then
5550 Spec := Specification (Decl);
5552 elsif Nkind (Decl) = N_Entry_Declaration then
5553 Spec := Decl;
5555 else
5556 return;
5557 end if;
5559 -- The overriding operation is type conformant with the overridden one,
5560 -- but the names of the formals are not required to match. If the names
5561 -- appear permuted in the overriding operation, this is a possible
5562 -- source of confusion that is worth diagnosing. Controlling formals
5563 -- often carry names that reflect the type, and it is not worthwhile
5564 -- requiring that their names match.
5566 if Present (Overridden_Subp)
5567 and then Nkind (Subp) /= N_Defining_Operator_Symbol
5568 then
5569 declare
5570 Form1 : Entity_Id;
5571 Form2 : Entity_Id;
5573 begin
5574 Form1 := First_Formal (Subp);
5575 Form2 := First_Formal (Overridden_Subp);
5577 -- If the overriding operation is a synchronized operation, skip
5578 -- the first parameter of the overridden operation, which is
5579 -- implicit in the new one. If the operation is declared in the
5580 -- body it is not primitive and all formals must match.
5582 if Is_Concurrent_Type (Scope (Subp))
5583 and then Is_Tagged_Type (Scope (Subp))
5584 and then not Has_Completion (Scope (Subp))
5585 then
5586 Form2 := Next_Formal (Form2);
5587 end if;
5589 if Present (Form1) then
5590 Form1 := Next_Formal (Form1);
5591 Form2 := Next_Formal (Form2);
5592 end if;
5594 while Present (Form1) loop
5595 if not Is_Controlling_Formal (Form1)
5596 and then Present (Next_Formal (Form2))
5597 and then Chars (Form1) = Chars (Next_Formal (Form2))
5598 then
5599 Error_Msg_Node_2 := Alias (Overridden_Subp);
5600 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
5601 Error_Msg_NE
5602 ("& does not match corresponding formal of&#",
5603 Form1, Form1);
5604 exit;
5605 end if;
5607 Next_Formal (Form1);
5608 Next_Formal (Form2);
5609 end loop;
5610 end;
5611 end if;
5613 -- If there is an overridden subprogram, then check that there is no
5614 -- "not overriding" indicator, and mark the subprogram as overriding.
5615 -- This is not done if the overridden subprogram is marked as hidden,
5616 -- which can occur for the case of inherited controlled operations
5617 -- (see Derive_Subprogram), unless the inherited subprogram's parent
5618 -- subprogram is not itself hidden. (Note: This condition could probably
5619 -- be simplified, leaving out the testing for the specific controlled
5620 -- cases, but it seems safer and clearer this way, and echoes similar
5621 -- special-case tests of this kind in other places.)
5623 if Present (Overridden_Subp)
5624 and then (not Is_Hidden (Overridden_Subp)
5625 or else
5626 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
5627 Name_Adjust,
5628 Name_Finalize)
5629 and then Present (Alias (Overridden_Subp))
5630 and then not Is_Hidden (Alias (Overridden_Subp))))
5631 then
5632 if Must_Not_Override (Spec) then
5633 Error_Msg_Sloc := Sloc (Overridden_Subp);
5635 if Ekind (Subp) = E_Entry then
5636 Error_Msg_NE
5637 ("entry & overrides inherited operation #", Spec, Subp);
5638 else
5639 Error_Msg_NE
5640 ("subprogram & overrides inherited operation #", Spec, Subp);
5641 end if;
5643 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
5644 -- as an extension of Root_Controlled, and thus has a useless Adjust
5645 -- operation. This operation should not be inherited by other limited
5646 -- controlled types. An explicit Adjust for them is not overriding.
5648 elsif Must_Override (Spec)
5649 and then Chars (Overridden_Subp) = Name_Adjust
5650 and then Is_Limited_Type (Etype (First_Formal (Subp)))
5651 and then Present (Alias (Overridden_Subp))
5652 and then
5653 Is_Predefined_File_Name
5654 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
5655 then
5656 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5658 elsif Is_Subprogram (Subp) then
5659 if Is_Init_Proc (Subp) then
5660 null;
5662 elsif No (Overridden_Operation (Subp)) then
5664 -- For entities generated by Derive_Subprograms the overridden
5665 -- operation is the inherited primitive (which is available
5666 -- through the attribute alias)
5668 if (Is_Dispatching_Operation (Subp)
5669 or else Is_Dispatching_Operation (Overridden_Subp))
5670 and then not Comes_From_Source (Overridden_Subp)
5671 and then Find_Dispatching_Type (Overridden_Subp) =
5672 Find_Dispatching_Type (Subp)
5673 and then Present (Alias (Overridden_Subp))
5674 and then Comes_From_Source (Alias (Overridden_Subp))
5675 then
5676 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
5678 else
5679 Set_Overridden_Operation (Subp, Overridden_Subp);
5680 end if;
5681 end if;
5682 end if;
5684 -- If primitive flag is set or this is a protected operation, then
5685 -- the operation is overriding at the point of its declaration, so
5686 -- warn if necessary. Otherwise it may have been declared before the
5687 -- operation it overrides and no check is required.
5689 if Style_Check
5690 and then not Must_Override (Spec)
5691 and then (Is_Primitive
5692 or else Ekind (Scope (Subp)) = E_Protected_Type)
5693 then
5694 Style.Missing_Overriding (Decl, Subp);
5695 end if;
5697 -- If Subp is an operator, it may override a predefined operation, if
5698 -- it is defined in the same scope as the type to which it applies.
5699 -- In that case Overridden_Subp is empty because of our implicit
5700 -- representation for predefined operators. We have to check whether the
5701 -- signature of Subp matches that of a predefined operator. Note that
5702 -- first argument provides the name of the operator, and the second
5703 -- argument the signature that may match that of a standard operation.
5704 -- If the indicator is overriding, then the operator must match a
5705 -- predefined signature, because we know already that there is no
5706 -- explicit overridden operation.
5708 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
5709 if Must_Not_Override (Spec) then
5711 -- If this is not a primitive or a protected subprogram, then
5712 -- "not overriding" is illegal.
5714 if not Is_Primitive
5715 and then Ekind (Scope (Subp)) /= E_Protected_Type
5716 then
5717 Error_Msg_N ("overriding indicator only allowed "
5718 & "if subprogram is primitive", Subp);
5720 elsif Can_Override_Operator (Subp) then
5721 Error_Msg_NE
5722 ("subprogram& overrides predefined operator ", Spec, Subp);
5723 end if;
5725 elsif Must_Override (Spec) then
5726 if No (Overridden_Operation (Subp))
5727 and then not Can_Override_Operator (Subp)
5728 then
5729 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5730 end if;
5732 elsif not Error_Posted (Subp)
5733 and then Style_Check
5734 and then Can_Override_Operator (Subp)
5735 and then
5736 not Is_Predefined_File_Name
5737 (Unit_File_Name (Get_Source_Unit (Subp)))
5738 then
5739 -- If style checks are enabled, indicate that the indicator is
5740 -- missing. However, at the point of declaration, the type of
5741 -- which this is a primitive operation may be private, in which
5742 -- case the indicator would be premature.
5744 if Has_Private_Declaration (Etype (Subp))
5745 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
5746 then
5747 null;
5748 else
5749 Style.Missing_Overriding (Decl, Subp);
5750 end if;
5751 end if;
5753 elsif Must_Override (Spec) then
5754 if Ekind (Subp) = E_Entry then
5755 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5756 else
5757 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5758 end if;
5760 -- If the operation is marked "not overriding" and it's not primitive
5761 -- then an error is issued, unless this is an operation of a task or
5762 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
5763 -- has been specified have already been checked above.
5765 elsif Must_Not_Override (Spec)
5766 and then not Is_Primitive
5767 and then Ekind (Subp) /= E_Entry
5768 and then Ekind (Scope (Subp)) /= E_Protected_Type
5769 then
5770 Error_Msg_N
5771 ("overriding indicator only allowed if subprogram is primitive",
5772 Subp);
5773 return;
5774 end if;
5775 end Check_Overriding_Indicator;
5777 -------------------
5778 -- Check_Returns --
5779 -------------------
5781 -- Note: this procedure needs to know far too much about how the expander
5782 -- messes with exceptions. The use of the flag Exception_Junk and the
5783 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
5784 -- works, but is not very clean. It would be better if the expansion
5785 -- routines would leave Original_Node working nicely, and we could use
5786 -- Original_Node here to ignore all the peculiar expander messing ???
5788 procedure Check_Returns
5789 (HSS : Node_Id;
5790 Mode : Character;
5791 Err : out Boolean;
5792 Proc : Entity_Id := Empty)
5794 Handler : Node_Id;
5796 procedure Check_Statement_Sequence (L : List_Id);
5797 -- Internal recursive procedure to check a list of statements for proper
5798 -- termination by a return statement (or a transfer of control or a
5799 -- compound statement that is itself internally properly terminated).
5801 ------------------------------
5802 -- Check_Statement_Sequence --
5803 ------------------------------
5805 procedure Check_Statement_Sequence (L : List_Id) is
5806 Last_Stm : Node_Id;
5807 Stm : Node_Id;
5808 Kind : Node_Kind;
5810 function Assert_False return Boolean;
5811 -- Returns True if Last_Stm is a pragma Assert (False) that has been
5812 -- rewritten as a null statement when assertions are off. The assert
5813 -- is not active, but it is still enough to kill the warning.
5815 ------------------
5816 -- Assert_False --
5817 ------------------
5819 function Assert_False return Boolean is
5820 Orig : constant Node_Id := Original_Node (Last_Stm);
5822 begin
5823 if Nkind (Orig) = N_Pragma
5824 and then Pragma_Name (Orig) = Name_Assert
5825 and then not Error_Posted (Orig)
5826 then
5827 declare
5828 Arg : constant Node_Id :=
5829 First (Pragma_Argument_Associations (Orig));
5830 Exp : constant Node_Id := Expression (Arg);
5831 begin
5832 return Nkind (Exp) = N_Identifier
5833 and then Chars (Exp) = Name_False;
5834 end;
5836 else
5837 return False;
5838 end if;
5839 end Assert_False;
5841 -- Local variables
5843 Raise_Exception_Call : Boolean;
5844 -- Set True if statement sequence terminated by Raise_Exception call
5845 -- or a Reraise_Occurrence call.
5847 -- Start of processing for Check_Statement_Sequence
5849 begin
5850 Raise_Exception_Call := False;
5852 -- Get last real statement
5854 Last_Stm := Last (L);
5856 -- Deal with digging out exception handler statement sequences that
5857 -- have been transformed by the local raise to goto optimization.
5858 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
5859 -- optimization has occurred, we are looking at something like:
5861 -- begin
5862 -- original stmts in block
5864 -- exception \
5865 -- when excep1 => |
5866 -- goto L1; | omitted if No_Exception_Propagation
5867 -- when excep2 => |
5868 -- goto L2; /
5869 -- end;
5871 -- goto L3; -- skip handler when exception not raised
5873 -- <<L1>> -- target label for local exception
5874 -- begin
5875 -- estmts1
5876 -- end;
5878 -- goto L3;
5880 -- <<L2>>
5881 -- begin
5882 -- estmts2
5883 -- end;
5885 -- <<L3>>
5887 -- and what we have to do is to dig out the estmts1 and estmts2
5888 -- sequences (which were the original sequences of statements in
5889 -- the exception handlers) and check them.
5891 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
5892 Stm := Last_Stm;
5893 loop
5894 Prev (Stm);
5895 exit when No (Stm);
5896 exit when Nkind (Stm) /= N_Block_Statement;
5897 exit when not Exception_Junk (Stm);
5898 Prev (Stm);
5899 exit when No (Stm);
5900 exit when Nkind (Stm) /= N_Label;
5901 exit when not Exception_Junk (Stm);
5902 Check_Statement_Sequence
5903 (Statements (Handled_Statement_Sequence (Next (Stm))));
5905 Prev (Stm);
5906 Last_Stm := Stm;
5907 exit when No (Stm);
5908 exit when Nkind (Stm) /= N_Goto_Statement;
5909 exit when not Exception_Junk (Stm);
5910 end loop;
5911 end if;
5913 -- Don't count pragmas
5915 while Nkind (Last_Stm) = N_Pragma
5917 -- Don't count call to SS_Release (can happen after Raise_Exception)
5919 or else
5920 (Nkind (Last_Stm) = N_Procedure_Call_Statement
5921 and then
5922 Nkind (Name (Last_Stm)) = N_Identifier
5923 and then
5924 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
5926 -- Don't count exception junk
5928 or else
5929 (Nkind_In (Last_Stm, N_Goto_Statement,
5930 N_Label,
5931 N_Object_Declaration)
5932 and then Exception_Junk (Last_Stm))
5933 or else Nkind (Last_Stm) in N_Push_xxx_Label
5934 or else Nkind (Last_Stm) in N_Pop_xxx_Label
5936 -- Inserted code, such as finalization calls, is irrelevant: we only
5937 -- need to check original source.
5939 or else Is_Rewrite_Insertion (Last_Stm)
5940 loop
5941 Prev (Last_Stm);
5942 end loop;
5944 -- Here we have the "real" last statement
5946 Kind := Nkind (Last_Stm);
5948 -- Transfer of control, OK. Note that in the No_Return procedure
5949 -- case, we already diagnosed any explicit return statements, so
5950 -- we can treat them as OK in this context.
5952 if Is_Transfer (Last_Stm) then
5953 return;
5955 -- Check cases of explicit non-indirect procedure calls
5957 elsif Kind = N_Procedure_Call_Statement
5958 and then Is_Entity_Name (Name (Last_Stm))
5959 then
5960 -- Check call to Raise_Exception procedure which is treated
5961 -- specially, as is a call to Reraise_Occurrence.
5963 -- We suppress the warning in these cases since it is likely that
5964 -- the programmer really does not expect to deal with the case
5965 -- of Null_Occurrence, and thus would find a warning about a
5966 -- missing return curious, and raising Program_Error does not
5967 -- seem such a bad behavior if this does occur.
5969 -- Note that in the Ada 2005 case for Raise_Exception, the actual
5970 -- behavior will be to raise Constraint_Error (see AI-329).
5972 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
5973 or else
5974 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
5975 then
5976 Raise_Exception_Call := True;
5978 -- For Raise_Exception call, test first argument, if it is
5979 -- an attribute reference for a 'Identity call, then we know
5980 -- that the call cannot possibly return.
5982 declare
5983 Arg : constant Node_Id :=
5984 Original_Node (First_Actual (Last_Stm));
5985 begin
5986 if Nkind (Arg) = N_Attribute_Reference
5987 and then Attribute_Name (Arg) = Name_Identity
5988 then
5989 return;
5990 end if;
5991 end;
5992 end if;
5994 -- If statement, need to look inside if there is an else and check
5995 -- each constituent statement sequence for proper termination.
5997 elsif Kind = N_If_Statement
5998 and then Present (Else_Statements (Last_Stm))
5999 then
6000 Check_Statement_Sequence (Then_Statements (Last_Stm));
6001 Check_Statement_Sequence (Else_Statements (Last_Stm));
6003 if Present (Elsif_Parts (Last_Stm)) then
6004 declare
6005 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6007 begin
6008 while Present (Elsif_Part) loop
6009 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6010 Next (Elsif_Part);
6011 end loop;
6012 end;
6013 end if;
6015 return;
6017 -- Case statement, check each case for proper termination
6019 elsif Kind = N_Case_Statement then
6020 declare
6021 Case_Alt : Node_Id;
6022 begin
6023 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6024 while Present (Case_Alt) loop
6025 Check_Statement_Sequence (Statements (Case_Alt));
6026 Next_Non_Pragma (Case_Alt);
6027 end loop;
6028 end;
6030 return;
6032 -- Block statement, check its handled sequence of statements
6034 elsif Kind = N_Block_Statement then
6035 declare
6036 Err1 : Boolean;
6038 begin
6039 Check_Returns
6040 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6042 if Err1 then
6043 Err := True;
6044 end if;
6046 return;
6047 end;
6049 -- Loop statement. If there is an iteration scheme, we can definitely
6050 -- fall out of the loop. Similarly if there is an exit statement, we
6051 -- can fall out. In either case we need a following return.
6053 elsif Kind = N_Loop_Statement then
6054 if Present (Iteration_Scheme (Last_Stm))
6055 or else Has_Exit (Entity (Identifier (Last_Stm)))
6056 then
6057 null;
6059 -- A loop with no exit statement or iteration scheme is either
6060 -- an infinite loop, or it has some other exit (raise/return).
6061 -- In either case, no warning is required.
6063 else
6064 return;
6065 end if;
6067 -- Timed entry call, check entry call and delay alternatives
6069 -- Note: in expanded code, the timed entry call has been converted
6070 -- to a set of expanded statements on which the check will work
6071 -- correctly in any case.
6073 elsif Kind = N_Timed_Entry_Call then
6074 declare
6075 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6076 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6078 begin
6079 -- If statement sequence of entry call alternative is missing,
6080 -- then we can definitely fall through, and we post the error
6081 -- message on the entry call alternative itself.
6083 if No (Statements (ECA)) then
6084 Last_Stm := ECA;
6086 -- If statement sequence of delay alternative is missing, then
6087 -- we can definitely fall through, and we post the error
6088 -- message on the delay alternative itself.
6090 -- Note: if both ECA and DCA are missing the return, then we
6091 -- post only one message, should be enough to fix the bugs.
6092 -- If not we will get a message next time on the DCA when the
6093 -- ECA is fixed.
6095 elsif No (Statements (DCA)) then
6096 Last_Stm := DCA;
6098 -- Else check both statement sequences
6100 else
6101 Check_Statement_Sequence (Statements (ECA));
6102 Check_Statement_Sequence (Statements (DCA));
6103 return;
6104 end if;
6105 end;
6107 -- Conditional entry call, check entry call and else part
6109 -- Note: in expanded code, the conditional entry call has been
6110 -- converted to a set of expanded statements on which the check
6111 -- will work correctly in any case.
6113 elsif Kind = N_Conditional_Entry_Call then
6114 declare
6115 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6117 begin
6118 -- If statement sequence of entry call alternative is missing,
6119 -- then we can definitely fall through, and we post the error
6120 -- message on the entry call alternative itself.
6122 if No (Statements (ECA)) then
6123 Last_Stm := ECA;
6125 -- Else check statement sequence and else part
6127 else
6128 Check_Statement_Sequence (Statements (ECA));
6129 Check_Statement_Sequence (Else_Statements (Last_Stm));
6130 return;
6131 end if;
6132 end;
6133 end if;
6135 -- If we fall through, issue appropriate message
6137 if Mode = 'F' then
6139 -- Kill warning if last statement is a raise exception call,
6140 -- or a pragma Assert (False). Note that with assertions enabled,
6141 -- such a pragma has been converted into a raise exception call
6142 -- already, so the Assert_False is for the assertions off case.
6144 if not Raise_Exception_Call and then not Assert_False then
6146 -- In GNATprove mode, it is an error to have a missing return
6148 Error_Msg_Warn := SPARK_Mode /= On;
6150 -- Issue error message or warning
6152 Error_Msg_N
6153 ("RETURN statement missing following this statement<<!",
6154 Last_Stm);
6155 Error_Msg_N
6156 ("\Program_Error ]<<!", Last_Stm);
6157 end if;
6159 -- Note: we set Err even though we have not issued a warning
6160 -- because we still have a case of a missing return. This is
6161 -- an extremely marginal case, probably will never be noticed
6162 -- but we might as well get it right.
6164 Err := True;
6166 -- Otherwise we have the case of a procedure marked No_Return
6168 else
6169 if not Raise_Exception_Call then
6170 if GNATprove_Mode then
6171 Error_Msg_N
6172 ("implied return after this statement "
6173 & "would have raised Program_Error", Last_Stm);
6174 else
6175 Error_Msg_N
6176 ("implied return after this statement "
6177 & "will raise Program_Error??", Last_Stm);
6178 end if;
6180 Error_Msg_Warn := SPARK_Mode /= On;
6181 Error_Msg_NE
6182 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6183 end if;
6185 declare
6186 RE : constant Node_Id :=
6187 Make_Raise_Program_Error (Sloc (Last_Stm),
6188 Reason => PE_Implicit_Return);
6189 begin
6190 Insert_After (Last_Stm, RE);
6191 Analyze (RE);
6192 end;
6193 end if;
6194 end Check_Statement_Sequence;
6196 -- Start of processing for Check_Returns
6198 begin
6199 Err := False;
6200 Check_Statement_Sequence (Statements (HSS));
6202 if Present (Exception_Handlers (HSS)) then
6203 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6204 while Present (Handler) loop
6205 Check_Statement_Sequence (Statements (Handler));
6206 Next_Non_Pragma (Handler);
6207 end loop;
6208 end if;
6209 end Check_Returns;
6211 ----------------------------
6212 -- Check_Subprogram_Order --
6213 ----------------------------
6215 procedure Check_Subprogram_Order (N : Node_Id) is
6217 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6218 -- This is used to check if S1 > S2 in the sense required by this test,
6219 -- for example nameab < namec, but name2 < name10.
6221 -----------------------------
6222 -- Subprogram_Name_Greater --
6223 -----------------------------
6225 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6226 L1, L2 : Positive;
6227 N1, N2 : Natural;
6229 begin
6230 -- Deal with special case where names are identical except for a
6231 -- numerical suffix. These are handled specially, taking the numeric
6232 -- ordering from the suffix into account.
6234 L1 := S1'Last;
6235 while S1 (L1) in '0' .. '9' loop
6236 L1 := L1 - 1;
6237 end loop;
6239 L2 := S2'Last;
6240 while S2 (L2) in '0' .. '9' loop
6241 L2 := L2 - 1;
6242 end loop;
6244 -- If non-numeric parts non-equal, do straight compare
6246 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6247 return S1 > S2;
6249 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6250 -- that a missing suffix is treated as numeric zero in this test.
6252 else
6253 N1 := 0;
6254 while L1 < S1'Last loop
6255 L1 := L1 + 1;
6256 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6257 end loop;
6259 N2 := 0;
6260 while L2 < S2'Last loop
6261 L2 := L2 + 1;
6262 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6263 end loop;
6265 return N1 > N2;
6266 end if;
6267 end Subprogram_Name_Greater;
6269 -- Start of processing for Check_Subprogram_Order
6271 begin
6272 -- Check body in alpha order if this is option
6274 if Style_Check
6275 and then Style_Check_Order_Subprograms
6276 and then Nkind (N) = N_Subprogram_Body
6277 and then Comes_From_Source (N)
6278 and then In_Extended_Main_Source_Unit (N)
6279 then
6280 declare
6281 LSN : String_Ptr
6282 renames Scope_Stack.Table
6283 (Scope_Stack.Last).Last_Subprogram_Name;
6285 Body_Id : constant Entity_Id :=
6286 Defining_Entity (Specification (N));
6288 begin
6289 Get_Decoded_Name_String (Chars (Body_Id));
6291 if LSN /= null then
6292 if Subprogram_Name_Greater
6293 (LSN.all, Name_Buffer (1 .. Name_Len))
6294 then
6295 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6296 end if;
6298 Free (LSN);
6299 end if;
6301 LSN := new String'(Name_Buffer (1 .. Name_Len));
6302 end;
6303 end if;
6304 end Check_Subprogram_Order;
6306 ------------------------------
6307 -- Check_Subtype_Conformant --
6308 ------------------------------
6310 procedure Check_Subtype_Conformant
6311 (New_Id : Entity_Id;
6312 Old_Id : Entity_Id;
6313 Err_Loc : Node_Id := Empty;
6314 Skip_Controlling_Formals : Boolean := False;
6315 Get_Inst : Boolean := False)
6317 Result : Boolean;
6318 pragma Warnings (Off, Result);
6319 begin
6320 Check_Conformance
6321 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
6322 Skip_Controlling_Formals => Skip_Controlling_Formals,
6323 Get_Inst => Get_Inst);
6324 end Check_Subtype_Conformant;
6326 ---------------------------
6327 -- Check_Type_Conformant --
6328 ---------------------------
6330 procedure Check_Type_Conformant
6331 (New_Id : Entity_Id;
6332 Old_Id : Entity_Id;
6333 Err_Loc : Node_Id := Empty)
6335 Result : Boolean;
6336 pragma Warnings (Off, Result);
6337 begin
6338 Check_Conformance
6339 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
6340 end Check_Type_Conformant;
6342 ---------------------------
6343 -- Can_Override_Operator --
6344 ---------------------------
6346 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
6347 Typ : Entity_Id;
6349 begin
6350 if Nkind (Subp) /= N_Defining_Operator_Symbol then
6351 return False;
6353 else
6354 Typ := Base_Type (Etype (First_Formal (Subp)));
6356 -- Check explicitly that the operation is a primitive of the type
6358 return Operator_Matches_Spec (Subp, Subp)
6359 and then not Is_Generic_Type (Typ)
6360 and then Scope (Subp) = Scope (Typ)
6361 and then not Is_Class_Wide_Type (Typ);
6362 end if;
6363 end Can_Override_Operator;
6365 ----------------------
6366 -- Conforming_Types --
6367 ----------------------
6369 function Conforming_Types
6370 (T1 : Entity_Id;
6371 T2 : Entity_Id;
6372 Ctype : Conformance_Type;
6373 Get_Inst : Boolean := False) return Boolean
6375 Type_1 : Entity_Id := T1;
6376 Type_2 : Entity_Id := T2;
6377 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
6379 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
6380 -- If neither T1 nor T2 are generic actual types, or if they are in
6381 -- different scopes (e.g. parent and child instances), then verify that
6382 -- the base types are equal. Otherwise T1 and T2 must be on the same
6383 -- subtype chain. The whole purpose of this procedure is to prevent
6384 -- spurious ambiguities in an instantiation that may arise if two
6385 -- distinct generic types are instantiated with the same actual.
6387 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
6388 -- An access parameter can designate an incomplete type. If the
6389 -- incomplete type is the limited view of a type from a limited_
6390 -- with_clause, check whether the non-limited view is available. If
6391 -- it is a (non-limited) incomplete type, get the full view.
6393 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
6394 -- Returns True if and only if either T1 denotes a limited view of T2
6395 -- or T2 denotes a limited view of T1. This can arise when the limited
6396 -- with view of a type is used in a subprogram declaration and the
6397 -- subprogram body is in the scope of a regular with clause for the
6398 -- same unit. In such a case, the two type entities can be considered
6399 -- identical for purposes of conformance checking.
6401 ----------------------
6402 -- Base_Types_Match --
6403 ----------------------
6405 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
6406 BT1 : constant Entity_Id := Base_Type (T1);
6407 BT2 : constant Entity_Id := Base_Type (T2);
6409 begin
6410 if T1 = T2 then
6411 return True;
6413 elsif BT1 = BT2 then
6415 -- The following is too permissive. A more precise test should
6416 -- check that the generic actual is an ancestor subtype of the
6417 -- other ???.
6419 -- See code in Find_Corresponding_Spec that applies an additional
6420 -- filter to handle accidental amiguities in instances.
6422 return not Is_Generic_Actual_Type (T1)
6423 or else not Is_Generic_Actual_Type (T2)
6424 or else Scope (T1) /= Scope (T2);
6426 -- If T2 is a generic actual type it is declared as the subtype of
6427 -- the actual. If that actual is itself a subtype we need to use its
6428 -- own base type to check for compatibility.
6430 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
6431 return True;
6433 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
6434 return True;
6436 else
6437 return False;
6438 end if;
6439 end Base_Types_Match;
6441 --------------------------
6442 -- Find_Designated_Type --
6443 --------------------------
6445 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
6446 Desig : Entity_Id;
6448 begin
6449 Desig := Directly_Designated_Type (T);
6451 if Ekind (Desig) = E_Incomplete_Type then
6453 -- If regular incomplete type, get full view if available
6455 if Present (Full_View (Desig)) then
6456 Desig := Full_View (Desig);
6458 -- If limited view of a type, get non-limited view if available,
6459 -- and check again for a regular incomplete type.
6461 elsif Present (Non_Limited_View (Desig)) then
6462 Desig := Get_Full_View (Non_Limited_View (Desig));
6463 end if;
6464 end if;
6466 return Desig;
6467 end Find_Designated_Type;
6469 -------------------------------
6470 -- Matches_Limited_With_View --
6471 -------------------------------
6473 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
6474 begin
6475 -- In some cases a type imported through a limited_with clause, and
6476 -- its nonlimited view are both visible, for example in an anonymous
6477 -- access-to-class-wide type in a formal. Both entities designate the
6478 -- same type.
6480 if From_Limited_With (T1) and then T2 = Available_View (T1) then
6481 return True;
6483 elsif From_Limited_With (T2) and then T1 = Available_View (T2) then
6484 return True;
6486 elsif From_Limited_With (T1)
6487 and then From_Limited_With (T2)
6488 and then Available_View (T1) = Available_View (T2)
6489 then
6490 return True;
6492 else
6493 return False;
6494 end if;
6495 end Matches_Limited_With_View;
6497 -- Start of processing for Conforming_Types
6499 begin
6500 -- The context is an instance association for a formal access-to-
6501 -- subprogram type; the formal parameter types require mapping because
6502 -- they may denote other formal parameters of the generic unit.
6504 if Get_Inst then
6505 Type_1 := Get_Instance_Of (T1);
6506 Type_2 := Get_Instance_Of (T2);
6507 end if;
6509 -- If one of the types is a view of the other introduced by a limited
6510 -- with clause, treat these as conforming for all purposes.
6512 if Matches_Limited_With_View (T1, T2) then
6513 return True;
6515 elsif Base_Types_Match (Type_1, Type_2) then
6516 return Ctype <= Mode_Conformant
6517 or else Subtypes_Statically_Match (Type_1, Type_2);
6519 elsif Is_Incomplete_Or_Private_Type (Type_1)
6520 and then Present (Full_View (Type_1))
6521 and then Base_Types_Match (Full_View (Type_1), Type_2)
6522 then
6523 return Ctype <= Mode_Conformant
6524 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
6526 elsif Ekind (Type_2) = E_Incomplete_Type
6527 and then Present (Full_View (Type_2))
6528 and then Base_Types_Match (Type_1, Full_View (Type_2))
6529 then
6530 return Ctype <= Mode_Conformant
6531 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6533 elsif Is_Private_Type (Type_2)
6534 and then In_Instance
6535 and then Present (Full_View (Type_2))
6536 and then Base_Types_Match (Type_1, Full_View (Type_2))
6537 then
6538 return Ctype <= Mode_Conformant
6539 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6540 end if;
6542 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
6543 -- treated recursively because they carry a signature. As far as
6544 -- conformance is concerned, convention plays no role, and either
6545 -- or both could be access to protected subprograms.
6547 Are_Anonymous_Access_To_Subprogram_Types :=
6548 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
6549 E_Anonymous_Access_Protected_Subprogram_Type)
6550 and then
6551 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
6552 E_Anonymous_Access_Protected_Subprogram_Type);
6554 -- Test anonymous access type case. For this case, static subtype
6555 -- matching is required for mode conformance (RM 6.3.1(15)). We check
6556 -- the base types because we may have built internal subtype entities
6557 -- to handle null-excluding types (see Process_Formals).
6559 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
6560 and then
6561 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
6563 -- Ada 2005 (AI-254)
6565 or else Are_Anonymous_Access_To_Subprogram_Types
6566 then
6567 declare
6568 Desig_1 : Entity_Id;
6569 Desig_2 : Entity_Id;
6571 begin
6572 -- In Ada 2005, access constant indicators must match for
6573 -- subtype conformance.
6575 if Ada_Version >= Ada_2005
6576 and then Ctype >= Subtype_Conformant
6577 and then
6578 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
6579 then
6580 return False;
6581 end if;
6583 Desig_1 := Find_Designated_Type (Type_1);
6584 Desig_2 := Find_Designated_Type (Type_2);
6586 -- If the context is an instance association for a formal
6587 -- access-to-subprogram type; formal access parameter designated
6588 -- types require mapping because they may denote other formal
6589 -- parameters of the generic unit.
6591 if Get_Inst then
6592 Desig_1 := Get_Instance_Of (Desig_1);
6593 Desig_2 := Get_Instance_Of (Desig_2);
6594 end if;
6596 -- It is possible for a Class_Wide_Type to be introduced for an
6597 -- incomplete type, in which case there is a separate class_ wide
6598 -- type for the full view. The types conform if their Etypes
6599 -- conform, i.e. one may be the full view of the other. This can
6600 -- only happen in the context of an access parameter, other uses
6601 -- of an incomplete Class_Wide_Type are illegal.
6603 if Is_Class_Wide_Type (Desig_1)
6604 and then
6605 Is_Class_Wide_Type (Desig_2)
6606 then
6607 return
6608 Conforming_Types
6609 (Etype (Base_Type (Desig_1)),
6610 Etype (Base_Type (Desig_2)), Ctype);
6612 elsif Are_Anonymous_Access_To_Subprogram_Types then
6613 if Ada_Version < Ada_2005 then
6614 return Ctype = Type_Conformant
6615 or else
6616 Subtypes_Statically_Match (Desig_1, Desig_2);
6618 -- We must check the conformance of the signatures themselves
6620 else
6621 declare
6622 Conformant : Boolean;
6623 begin
6624 Check_Conformance
6625 (Desig_1, Desig_2, Ctype, False, Conformant);
6626 return Conformant;
6627 end;
6628 end if;
6630 else
6631 return Base_Type (Desig_1) = Base_Type (Desig_2)
6632 and then (Ctype = Type_Conformant
6633 or else
6634 Subtypes_Statically_Match (Desig_1, Desig_2));
6635 end if;
6636 end;
6638 -- Otherwise definitely no match
6640 else
6641 if ((Ekind (Type_1) = E_Anonymous_Access_Type
6642 and then Is_Access_Type (Type_2))
6643 or else (Ekind (Type_2) = E_Anonymous_Access_Type
6644 and then Is_Access_Type (Type_1)))
6645 and then
6646 Conforming_Types
6647 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
6648 then
6649 May_Hide_Profile := True;
6650 end if;
6652 return False;
6653 end if;
6654 end Conforming_Types;
6656 --------------------------
6657 -- Create_Extra_Formals --
6658 --------------------------
6660 procedure Create_Extra_Formals (E : Entity_Id) is
6661 Formal : Entity_Id;
6662 First_Extra : Entity_Id := Empty;
6663 Last_Extra : Entity_Id;
6664 Formal_Type : Entity_Id;
6665 P_Formal : Entity_Id := Empty;
6667 function Add_Extra_Formal
6668 (Assoc_Entity : Entity_Id;
6669 Typ : Entity_Id;
6670 Scope : Entity_Id;
6671 Suffix : String) return Entity_Id;
6672 -- Add an extra formal to the current list of formals and extra formals.
6673 -- The extra formal is added to the end of the list of extra formals,
6674 -- and also returned as the result. These formals are always of mode IN.
6675 -- The new formal has the type Typ, is declared in Scope, and its name
6676 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
6677 -- The following suffixes are currently used. They should not be changed
6678 -- without coordinating with CodePeer, which makes use of these to
6679 -- provide better messages.
6681 -- O denotes the Constrained bit.
6682 -- L denotes the accessibility level.
6683 -- BIP_xxx denotes an extra formal for a build-in-place function. See
6684 -- the full list in exp_ch6.BIP_Formal_Kind.
6686 ----------------------
6687 -- Add_Extra_Formal --
6688 ----------------------
6690 function Add_Extra_Formal
6691 (Assoc_Entity : Entity_Id;
6692 Typ : Entity_Id;
6693 Scope : Entity_Id;
6694 Suffix : String) return Entity_Id
6696 EF : constant Entity_Id :=
6697 Make_Defining_Identifier (Sloc (Assoc_Entity),
6698 Chars => New_External_Name (Chars (Assoc_Entity),
6699 Suffix => Suffix));
6701 begin
6702 -- A little optimization. Never generate an extra formal for the
6703 -- _init operand of an initialization procedure, since it could
6704 -- never be used.
6706 if Chars (Formal) = Name_uInit then
6707 return Empty;
6708 end if;
6710 Set_Ekind (EF, E_In_Parameter);
6711 Set_Actual_Subtype (EF, Typ);
6712 Set_Etype (EF, Typ);
6713 Set_Scope (EF, Scope);
6714 Set_Mechanism (EF, Default_Mechanism);
6715 Set_Formal_Validity (EF);
6717 if No (First_Extra) then
6718 First_Extra := EF;
6719 Set_Extra_Formals (Scope, First_Extra);
6720 end if;
6722 if Present (Last_Extra) then
6723 Set_Extra_Formal (Last_Extra, EF);
6724 end if;
6726 Last_Extra := EF;
6728 return EF;
6729 end Add_Extra_Formal;
6731 -- Start of processing for Create_Extra_Formals
6733 begin
6734 -- We never generate extra formals if expansion is not active because we
6735 -- don't need them unless we are generating code.
6737 if not Expander_Active then
6738 return;
6739 end if;
6741 -- No need to generate extra formals in interface thunks whose target
6742 -- primitive has no extra formals.
6744 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
6745 return;
6746 end if;
6748 -- If this is a derived subprogram then the subtypes of the parent
6749 -- subprogram's formal parameters will be used to determine the need
6750 -- for extra formals.
6752 if Is_Overloadable (E) and then Present (Alias (E)) then
6753 P_Formal := First_Formal (Alias (E));
6754 end if;
6756 Last_Extra := Empty;
6757 Formal := First_Formal (E);
6758 while Present (Formal) loop
6759 Last_Extra := Formal;
6760 Next_Formal (Formal);
6761 end loop;
6763 -- If Extra_formals were already created, don't do it again. This
6764 -- situation may arise for subprogram types created as part of
6765 -- dispatching calls (see Expand_Dispatching_Call)
6767 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
6768 return;
6769 end if;
6771 -- If the subprogram is a predefined dispatching subprogram then don't
6772 -- generate any extra constrained or accessibility level formals. In
6773 -- general we suppress these for internal subprograms (by not calling
6774 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
6775 -- generated stream attributes do get passed through because extra
6776 -- build-in-place formals are needed in some cases (limited 'Input).
6778 if Is_Predefined_Internal_Operation (E) then
6779 goto Test_For_Func_Result_Extras;
6780 end if;
6782 Formal := First_Formal (E);
6783 while Present (Formal) loop
6785 -- Create extra formal for supporting the attribute 'Constrained.
6786 -- The case of a private type view without discriminants also
6787 -- requires the extra formal if the underlying type has defaulted
6788 -- discriminants.
6790 if Ekind (Formal) /= E_In_Parameter then
6791 if Present (P_Formal) then
6792 Formal_Type := Etype (P_Formal);
6793 else
6794 Formal_Type := Etype (Formal);
6795 end if;
6797 -- Do not produce extra formals for Unchecked_Union parameters.
6798 -- Jump directly to the end of the loop.
6800 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
6801 goto Skip_Extra_Formal_Generation;
6802 end if;
6804 if not Has_Discriminants (Formal_Type)
6805 and then Ekind (Formal_Type) in Private_Kind
6806 and then Present (Underlying_Type (Formal_Type))
6807 then
6808 Formal_Type := Underlying_Type (Formal_Type);
6809 end if;
6811 -- Suppress the extra formal if formal's subtype is constrained or
6812 -- indefinite, or we're compiling for Ada 2012 and the underlying
6813 -- type is tagged and limited. In Ada 2012, a limited tagged type
6814 -- can have defaulted discriminants, but 'Constrained is required
6815 -- to return True, so the formal is never needed (see AI05-0214).
6816 -- Note that this ensures consistency of calling sequences for
6817 -- dispatching operations when some types in a class have defaults
6818 -- on discriminants and others do not (and requiring the extra
6819 -- formal would introduce distributed overhead).
6821 -- If the type does not have a completion yet, treat as prior to
6822 -- Ada 2012 for consistency.
6824 if Has_Discriminants (Formal_Type)
6825 and then not Is_Constrained (Formal_Type)
6826 and then not Is_Indefinite_Subtype (Formal_Type)
6827 and then (Ada_Version < Ada_2012
6828 or else No (Underlying_Type (Formal_Type))
6829 or else not
6830 (Is_Limited_Type (Formal_Type)
6831 and then
6832 (Is_Tagged_Type
6833 (Underlying_Type (Formal_Type)))))
6834 then
6835 Set_Extra_Constrained
6836 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
6837 end if;
6838 end if;
6840 -- Create extra formal for supporting accessibility checking. This
6841 -- is done for both anonymous access formals and formals of named
6842 -- access types that are marked as controlling formals. The latter
6843 -- case can occur when Expand_Dispatching_Call creates a subprogram
6844 -- type and substitutes the types of access-to-class-wide actuals
6845 -- for the anonymous access-to-specific-type of controlling formals.
6846 -- Base_Type is applied because in cases where there is a null
6847 -- exclusion the formal may have an access subtype.
6849 -- This is suppressed if we specifically suppress accessibility
6850 -- checks at the package level for either the subprogram, or the
6851 -- package in which it resides. However, we do not suppress it
6852 -- simply if the scope has accessibility checks suppressed, since
6853 -- this could cause trouble when clients are compiled with a
6854 -- different suppression setting. The explicit checks at the
6855 -- package level are safe from this point of view.
6857 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
6858 or else (Is_Controlling_Formal (Formal)
6859 and then Is_Access_Type (Base_Type (Etype (Formal)))))
6860 and then not
6861 (Explicit_Suppress (E, Accessibility_Check)
6862 or else
6863 Explicit_Suppress (Scope (E), Accessibility_Check))
6864 and then
6865 (No (P_Formal)
6866 or else Present (Extra_Accessibility (P_Formal)))
6867 then
6868 Set_Extra_Accessibility
6869 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
6870 end if;
6872 -- This label is required when skipping extra formal generation for
6873 -- Unchecked_Union parameters.
6875 <<Skip_Extra_Formal_Generation>>
6877 if Present (P_Formal) then
6878 Next_Formal (P_Formal);
6879 end if;
6881 Next_Formal (Formal);
6882 end loop;
6884 <<Test_For_Func_Result_Extras>>
6886 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
6887 -- function call is ... determined by the point of call ...".
6889 if Needs_Result_Accessibility_Level (E) then
6890 Set_Extra_Accessibility_Of_Result
6891 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
6892 end if;
6894 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
6895 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
6897 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
6898 declare
6899 Result_Subt : constant Entity_Id := Etype (E);
6900 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
6901 Formal_Typ : Entity_Id;
6903 Discard : Entity_Id;
6904 pragma Warnings (Off, Discard);
6906 begin
6907 -- In the case of functions with unconstrained result subtypes,
6908 -- add a 4-state formal indicating whether the return object is
6909 -- allocated by the caller (1), or should be allocated by the
6910 -- callee on the secondary stack (2), in the global heap (3), or
6911 -- in a user-defined storage pool (4). For the moment we just use
6912 -- Natural for the type of this formal. Note that this formal
6913 -- isn't usually needed in the case where the result subtype is
6914 -- constrained, but it is needed when the function has a tagged
6915 -- result, because generally such functions can be called in a
6916 -- dispatching context and such calls must be handled like calls
6917 -- to a class-wide function.
6919 if Needs_BIP_Alloc_Form (E) then
6920 Discard :=
6921 Add_Extra_Formal
6922 (E, Standard_Natural,
6923 E, BIP_Formal_Suffix (BIP_Alloc_Form));
6925 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
6926 -- use a user-defined pool. This formal is not added on
6927 -- .NET/JVM/ZFP as those targets do not support pools.
6929 if VM_Target = No_VM
6930 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
6931 then
6932 Discard :=
6933 Add_Extra_Formal
6934 (E, RTE (RE_Root_Storage_Pool_Ptr),
6935 E, BIP_Formal_Suffix (BIP_Storage_Pool));
6936 end if;
6937 end if;
6939 -- In the case of functions whose result type needs finalization,
6940 -- add an extra formal which represents the finalization master.
6942 if Needs_BIP_Finalization_Master (E) then
6943 Discard :=
6944 Add_Extra_Formal
6945 (E, RTE (RE_Finalization_Master_Ptr),
6946 E, BIP_Formal_Suffix (BIP_Finalization_Master));
6947 end if;
6949 -- When the result type contains tasks, add two extra formals: the
6950 -- master of the tasks to be created, and the caller's activation
6951 -- chain.
6953 if Has_Task (Full_Subt) then
6954 Discard :=
6955 Add_Extra_Formal
6956 (E, RTE (RE_Master_Id),
6957 E, BIP_Formal_Suffix (BIP_Task_Master));
6958 Discard :=
6959 Add_Extra_Formal
6960 (E, RTE (RE_Activation_Chain_Access),
6961 E, BIP_Formal_Suffix (BIP_Activation_Chain));
6962 end if;
6964 -- All build-in-place functions get an extra formal that will be
6965 -- passed the address of the return object within the caller.
6967 Formal_Typ :=
6968 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
6970 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
6971 Set_Etype (Formal_Typ, Formal_Typ);
6972 Set_Depends_On_Private
6973 (Formal_Typ, Has_Private_Component (Formal_Typ));
6974 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
6975 Set_Is_Access_Constant (Formal_Typ, False);
6977 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
6978 -- the designated type comes from the limited view (for back-end
6979 -- purposes).
6981 Set_From_Limited_With
6982 (Formal_Typ, From_Limited_With (Result_Subt));
6984 Layout_Type (Formal_Typ);
6986 Discard :=
6987 Add_Extra_Formal
6988 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
6989 end;
6990 end if;
6991 end Create_Extra_Formals;
6993 -----------------------------
6994 -- Enter_Overloaded_Entity --
6995 -----------------------------
6997 procedure Enter_Overloaded_Entity (S : Entity_Id) is
6998 E : Entity_Id := Current_Entity_In_Scope (S);
6999 C_E : Entity_Id := Current_Entity (S);
7001 begin
7002 if Present (E) then
7003 Set_Has_Homonym (E);
7004 Set_Has_Homonym (S);
7005 end if;
7007 Set_Is_Immediately_Visible (S);
7008 Set_Scope (S, Current_Scope);
7010 -- Chain new entity if front of homonym in current scope, so that
7011 -- homonyms are contiguous.
7013 if Present (E) and then E /= C_E then
7014 while Homonym (C_E) /= E loop
7015 C_E := Homonym (C_E);
7016 end loop;
7018 Set_Homonym (C_E, S);
7020 else
7021 E := C_E;
7022 Set_Current_Entity (S);
7023 end if;
7025 Set_Homonym (S, E);
7027 if Is_Inherited_Operation (S) then
7028 Append_Inherited_Subprogram (S);
7029 else
7030 Append_Entity (S, Current_Scope);
7031 end if;
7033 Set_Public_Status (S);
7035 if Debug_Flag_E then
7036 Write_Str ("New overloaded entity chain: ");
7037 Write_Name (Chars (S));
7039 E := S;
7040 while Present (E) loop
7041 Write_Str (" "); Write_Int (Int (E));
7042 E := Homonym (E);
7043 end loop;
7045 Write_Eol;
7046 end if;
7048 -- Generate warning for hiding
7050 if Warn_On_Hiding
7051 and then Comes_From_Source (S)
7052 and then In_Extended_Main_Source_Unit (S)
7053 then
7054 E := S;
7055 loop
7056 E := Homonym (E);
7057 exit when No (E);
7059 -- Warn unless genuine overloading. Do not emit warning on
7060 -- hiding predefined operators in Standard (these are either an
7061 -- (artifact of our implicit declarations, or simple noise) but
7062 -- keep warning on a operator defined on a local subtype, because
7063 -- of the real danger that different operators may be applied in
7064 -- various parts of the program.
7066 -- Note that if E and S have the same scope, there is never any
7067 -- hiding. Either the two conflict, and the program is illegal,
7068 -- or S is overriding an implicit inherited subprogram.
7070 if Scope (E) /= Scope (S)
7071 and then (not Is_Overloadable (E)
7072 or else Subtype_Conformant (E, S))
7073 and then (Is_Immediately_Visible (E)
7074 or else
7075 Is_Potentially_Use_Visible (S))
7076 then
7077 if Scope (E) /= Standard_Standard then
7078 Error_Msg_Sloc := Sloc (E);
7079 Error_Msg_N ("declaration of & hides one #?h?", S);
7081 elsif Nkind (S) = N_Defining_Operator_Symbol
7082 and then
7083 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
7084 then
7085 Error_Msg_N
7086 ("declaration of & hides predefined operator?h?", S);
7087 end if;
7088 end if;
7089 end loop;
7090 end if;
7091 end Enter_Overloaded_Entity;
7093 -----------------------------
7094 -- Check_Untagged_Equality --
7095 -----------------------------
7097 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
7098 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
7099 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
7100 Obj_Decl : Node_Id;
7102 begin
7103 -- This check applies only if we have a subprogram declaration with an
7104 -- untagged record type.
7106 if Nkind (Decl) /= N_Subprogram_Declaration
7107 or else not Is_Record_Type (Typ)
7108 or else Is_Tagged_Type (Typ)
7109 then
7110 return;
7111 end if;
7113 -- In Ada 2012 case, we will output errors or warnings depending on
7114 -- the setting of debug flag -gnatd.E.
7116 if Ada_Version >= Ada_2012 then
7117 Error_Msg_Warn := Debug_Flag_Dot_EE;
7119 -- In earlier versions of Ada, nothing to do unless we are warning on
7120 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
7122 else
7123 if not Warn_On_Ada_2012_Compatibility then
7124 return;
7125 end if;
7126 end if;
7128 -- Cases where the type has already been frozen
7130 if Is_Frozen (Typ) then
7132 -- If the type is not declared in a package, or if we are in the body
7133 -- of the package or in some other scope, the new operation is not
7134 -- primitive, and therefore legal, though suspicious. Should we
7135 -- generate a warning in this case ???
7137 if Ekind (Scope (Typ)) /= E_Package
7138 or else Scope (Typ) /= Current_Scope
7139 then
7140 return;
7142 -- If the type is a generic actual (sub)type, the operation is not
7143 -- primitive either because the base type is declared elsewhere.
7145 elsif Is_Generic_Actual_Type (Typ) then
7146 return;
7148 -- Here we have a definite error of declaration after freezing
7150 else
7151 if Ada_Version >= Ada_2012 then
7152 Error_Msg_NE
7153 ("equality operator must be declared before type & is "
7154 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
7156 -- In Ada 2012 mode with error turned to warning, output one
7157 -- more warning to warn that the equality operation may not
7158 -- compose. This is the consequence of ignoring the error.
7160 if Error_Msg_Warn then
7161 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
7162 end if;
7164 else
7165 Error_Msg_NE
7166 ("equality operator must be declared before type& is "
7167 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
7168 end if;
7170 -- If we are in the package body, we could just move the
7171 -- declaration to the package spec, so add a message saying that.
7173 if In_Package_Body (Scope (Typ)) then
7174 if Ada_Version >= Ada_2012 then
7175 Error_Msg_N
7176 ("\move declaration to package spec<<", Eq_Op);
7177 else
7178 Error_Msg_N
7179 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
7180 end if;
7182 -- Otherwise try to find the freezing point
7184 else
7185 Obj_Decl := Next (Parent (Typ));
7186 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
7187 if Nkind (Obj_Decl) = N_Object_Declaration
7188 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
7189 then
7190 -- Freezing point, output warnings
7192 if Ada_Version >= Ada_2012 then
7193 Error_Msg_NE
7194 ("type& is frozen by declaration??", Obj_Decl, Typ);
7195 Error_Msg_N
7196 ("\an equality operator cannot be declared after "
7197 & "this point??",
7198 Obj_Decl);
7199 else
7200 Error_Msg_NE
7201 ("type& is frozen by declaration (Ada 2012)?y?",
7202 Obj_Decl, Typ);
7203 Error_Msg_N
7204 ("\an equality operator cannot be declared after "
7205 & "this point (Ada 2012)?y?",
7206 Obj_Decl);
7207 end if;
7209 exit;
7210 end if;
7212 Next (Obj_Decl);
7213 end loop;
7214 end if;
7215 end if;
7217 -- Here if type is not frozen yet. It is illegal to have a primitive
7218 -- equality declared in the private part if the type is visible.
7220 elsif not In_Same_List (Parent (Typ), Decl)
7221 and then not Is_Limited_Type (Typ)
7222 then
7223 -- Shouldn't we give an RM reference here???
7225 if Ada_Version >= Ada_2012 then
7226 Error_Msg_N
7227 ("equality operator appears too late<<", Eq_Op);
7228 else
7229 Error_Msg_N
7230 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
7231 end if;
7233 -- No error detected
7235 else
7236 return;
7237 end if;
7238 end Check_Untagged_Equality;
7240 -----------------------------
7241 -- Find_Corresponding_Spec --
7242 -----------------------------
7244 function Find_Corresponding_Spec
7245 (N : Node_Id;
7246 Post_Error : Boolean := True) return Entity_Id
7248 Spec : constant Node_Id := Specification (N);
7249 Designator : constant Entity_Id := Defining_Entity (Spec);
7251 E : Entity_Id;
7253 function Different_Generic_Profile (E : Entity_Id) return Boolean;
7254 -- Even if fully conformant, a body may depend on a generic actual when
7255 -- the spec does not, or vice versa, in which case they were distinct
7256 -- entities in the generic.
7258 -------------------------------
7259 -- Different_Generic_Profile --
7260 -------------------------------
7262 function Different_Generic_Profile (E : Entity_Id) return Boolean is
7263 F1, F2 : Entity_Id;
7265 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
7266 -- Check that the types of corresponding formals have the same
7267 -- generic actual if any. We have to account for subtypes of a
7268 -- generic formal, declared between a spec and a body, which may
7269 -- appear distinct in an instance but matched in the generic, and
7270 -- the subtype may be used either in the spec or the body of the
7271 -- subprogram being checked.
7273 -------------------------
7274 -- Same_Generic_Actual --
7275 -------------------------
7277 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
7279 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
7280 -- Predicate to check whether S1 is a subtype of S2 in the source
7281 -- of the instance.
7283 -------------------------
7284 -- Is_Declared_Subtype --
7285 -------------------------
7287 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
7288 begin
7289 return Comes_From_Source (Parent (S1))
7290 and then Nkind (Parent (S1)) = N_Subtype_Declaration
7291 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
7292 and then Entity (Subtype_Indication (Parent (S1))) = S2;
7293 end Is_Declared_Subtype;
7295 -- Start of processing for Same_Generic_Actual
7297 begin
7298 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
7299 or else Is_Declared_Subtype (T1, T2)
7300 or else Is_Declared_Subtype (T2, T1);
7301 end Same_Generic_Actual;
7303 -- Start of processing for Different_Generic_Profile
7305 begin
7306 if not In_Instance then
7307 return False;
7309 elsif Ekind (E) = E_Function
7310 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
7311 then
7312 return True;
7313 end if;
7315 F1 := First_Formal (Designator);
7316 F2 := First_Formal (E);
7317 while Present (F1) loop
7318 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
7319 return True;
7320 end if;
7322 Next_Formal (F1);
7323 Next_Formal (F2);
7324 end loop;
7326 return False;
7327 end Different_Generic_Profile;
7329 -- Start of processing for Find_Corresponding_Spec
7331 begin
7332 E := Current_Entity (Designator);
7333 while Present (E) loop
7335 -- We are looking for a matching spec. It must have the same scope,
7336 -- and the same name, and either be type conformant, or be the case
7337 -- of a library procedure spec and its body (which belong to one
7338 -- another regardless of whether they are type conformant or not).
7340 if Scope (E) = Current_Scope then
7341 if Current_Scope = Standard_Standard
7342 or else (Ekind (E) = Ekind (Designator)
7343 and then Type_Conformant (E, Designator))
7344 then
7345 -- Within an instantiation, we know that spec and body are
7346 -- subtype conformant, because they were subtype conformant in
7347 -- the generic. We choose the subtype-conformant entity here as
7348 -- well, to resolve spurious ambiguities in the instance that
7349 -- were not present in the generic (i.e. when two different
7350 -- types are given the same actual). If we are looking for a
7351 -- spec to match a body, full conformance is expected.
7353 if In_Instance then
7354 Set_Convention (Designator, Convention (E));
7356 -- Skip past subprogram bodies and subprogram renamings that
7357 -- may appear to have a matching spec, but that aren't fully
7358 -- conformant with it. That can occur in cases where an
7359 -- actual type causes unrelated homographs in the instance.
7361 if Nkind_In (N, N_Subprogram_Body,
7362 N_Subprogram_Renaming_Declaration)
7363 and then Present (Homonym (E))
7364 and then not Fully_Conformant (Designator, E)
7365 then
7366 goto Next_Entity;
7368 elsif not Subtype_Conformant (Designator, E) then
7369 goto Next_Entity;
7371 elsif Different_Generic_Profile (E) then
7372 goto Next_Entity;
7373 end if;
7374 end if;
7376 -- Ada 2012 (AI05-0165): For internally generated bodies of
7377 -- null procedures locate the internally generated spec. We
7378 -- enforce mode conformance since a tagged type may inherit
7379 -- from interfaces several null primitives which differ only
7380 -- in the mode of the formals.
7382 if not (Comes_From_Source (E))
7383 and then Is_Null_Procedure (E)
7384 and then not Mode_Conformant (Designator, E)
7385 then
7386 null;
7388 -- For null procedures coming from source that are completions,
7389 -- analysis of the generated body will establish the link.
7391 elsif Comes_From_Source (E)
7392 and then Nkind (Spec) = N_Procedure_Specification
7393 and then Null_Present (Spec)
7394 then
7395 return E;
7397 elsif not Has_Completion (E) then
7398 if Nkind (N) /= N_Subprogram_Body_Stub then
7399 Set_Corresponding_Spec (N, E);
7400 end if;
7402 Set_Has_Completion (E);
7403 return E;
7405 elsif Nkind (Parent (N)) = N_Subunit then
7407 -- If this is the proper body of a subunit, the completion
7408 -- flag is set when analyzing the stub.
7410 return E;
7412 -- If E is an internal function with a controlling result that
7413 -- was created for an operation inherited by a null extension,
7414 -- it may be overridden by a body without a previous spec (one
7415 -- more reason why these should be shunned). In that case we
7416 -- remove the generated body if present, because the current
7417 -- one is the explicit overriding.
7419 elsif Ekind (E) = E_Function
7420 and then Ada_Version >= Ada_2005
7421 and then not Comes_From_Source (E)
7422 and then Has_Controlling_Result (E)
7423 and then Is_Null_Extension (Etype (E))
7424 and then Comes_From_Source (Spec)
7425 then
7426 Set_Has_Completion (E, False);
7428 if Expander_Active
7429 and then Nkind (Parent (E)) = N_Function_Specification
7430 then
7431 Remove
7432 (Unit_Declaration_Node
7433 (Corresponding_Body (Unit_Declaration_Node (E))));
7435 return E;
7437 -- If expansion is disabled, or if the wrapper function has
7438 -- not been generated yet, this a late body overriding an
7439 -- inherited operation, or it is an overriding by some other
7440 -- declaration before the controlling result is frozen. In
7441 -- either case this is a declaration of a new entity.
7443 else
7444 return Empty;
7445 end if;
7447 -- If the body already exists, then this is an error unless
7448 -- the previous declaration is the implicit declaration of a
7449 -- derived subprogram. It is also legal for an instance to
7450 -- contain type conformant overloadable declarations (but the
7451 -- generic declaration may not), per 8.3(26/2).
7453 elsif No (Alias (E))
7454 and then not Is_Intrinsic_Subprogram (E)
7455 and then not In_Instance
7456 and then Post_Error
7457 then
7458 Error_Msg_Sloc := Sloc (E);
7460 if Is_Imported (E) then
7461 Error_Msg_NE
7462 ("body not allowed for imported subprogram & declared#",
7463 N, E);
7464 else
7465 Error_Msg_NE ("duplicate body for & declared#", N, E);
7466 end if;
7467 end if;
7469 -- Child units cannot be overloaded, so a conformance mismatch
7470 -- between body and a previous spec is an error.
7472 elsif Is_Child_Unit (E)
7473 and then
7474 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
7475 and then
7476 Nkind (Parent (Unit_Declaration_Node (Designator))) =
7477 N_Compilation_Unit
7478 and then Post_Error
7479 then
7480 Error_Msg_N
7481 ("body of child unit does not match previous declaration", N);
7482 end if;
7483 end if;
7485 <<Next_Entity>>
7486 E := Homonym (E);
7487 end loop;
7489 -- On exit, we know that no previous declaration of subprogram exists
7491 return Empty;
7492 end Find_Corresponding_Spec;
7494 ----------------------
7495 -- Fully_Conformant --
7496 ----------------------
7498 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7499 Result : Boolean;
7500 begin
7501 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
7502 return Result;
7503 end Fully_Conformant;
7505 ----------------------------------
7506 -- Fully_Conformant_Expressions --
7507 ----------------------------------
7509 function Fully_Conformant_Expressions
7510 (Given_E1 : Node_Id;
7511 Given_E2 : Node_Id) return Boolean
7513 E1 : constant Node_Id := Original_Node (Given_E1);
7514 E2 : constant Node_Id := Original_Node (Given_E2);
7515 -- We always test conformance on original nodes, since it is possible
7516 -- for analysis and/or expansion to make things look as though they
7517 -- conform when they do not, e.g. by converting 1+2 into 3.
7519 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
7520 renames Fully_Conformant_Expressions;
7522 function FCL (L1, L2 : List_Id) return Boolean;
7523 -- Compare elements of two lists for conformance. Elements have to be
7524 -- conformant, and actuals inserted as default parameters do not match
7525 -- explicit actuals with the same value.
7527 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
7528 -- Compare an operator node with a function call
7530 ---------
7531 -- FCL --
7532 ---------
7534 function FCL (L1, L2 : List_Id) return Boolean is
7535 N1, N2 : Node_Id;
7537 begin
7538 if L1 = No_List then
7539 N1 := Empty;
7540 else
7541 N1 := First (L1);
7542 end if;
7544 if L2 = No_List then
7545 N2 := Empty;
7546 else
7547 N2 := First (L2);
7548 end if;
7550 -- Compare two lists, skipping rewrite insertions (we want to compare
7551 -- the original trees, not the expanded versions).
7553 loop
7554 if Is_Rewrite_Insertion (N1) then
7555 Next (N1);
7556 elsif Is_Rewrite_Insertion (N2) then
7557 Next (N2);
7558 elsif No (N1) then
7559 return No (N2);
7560 elsif No (N2) then
7561 return False;
7562 elsif not FCE (N1, N2) then
7563 return False;
7564 else
7565 Next (N1);
7566 Next (N2);
7567 end if;
7568 end loop;
7569 end FCL;
7571 ---------
7572 -- FCO --
7573 ---------
7575 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
7576 Actuals : constant List_Id := Parameter_Associations (Call_Node);
7577 Act : Node_Id;
7579 begin
7580 if No (Actuals)
7581 or else Entity (Op_Node) /= Entity (Name (Call_Node))
7582 then
7583 return False;
7585 else
7586 Act := First (Actuals);
7588 if Nkind (Op_Node) in N_Binary_Op then
7589 if not FCE (Left_Opnd (Op_Node), Act) then
7590 return False;
7591 end if;
7593 Next (Act);
7594 end if;
7596 return Present (Act)
7597 and then FCE (Right_Opnd (Op_Node), Act)
7598 and then No (Next (Act));
7599 end if;
7600 end FCO;
7602 -- Start of processing for Fully_Conformant_Expressions
7604 begin
7605 -- Non-conformant if paren count does not match. Note: if some idiot
7606 -- complains that we don't do this right for more than 3 levels of
7607 -- parentheses, they will be treated with the respect they deserve.
7609 if Paren_Count (E1) /= Paren_Count (E2) then
7610 return False;
7612 -- If same entities are referenced, then they are conformant even if
7613 -- they have different forms (RM 8.3.1(19-20)).
7615 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
7616 if Present (Entity (E1)) then
7617 return Entity (E1) = Entity (E2)
7618 or else (Chars (Entity (E1)) = Chars (Entity (E2))
7619 and then Ekind (Entity (E1)) = E_Discriminant
7620 and then Ekind (Entity (E2)) = E_In_Parameter);
7622 elsif Nkind (E1) = N_Expanded_Name
7623 and then Nkind (E2) = N_Expanded_Name
7624 and then Nkind (Selector_Name (E1)) = N_Character_Literal
7625 and then Nkind (Selector_Name (E2)) = N_Character_Literal
7626 then
7627 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
7629 else
7630 -- Identifiers in component associations don't always have
7631 -- entities, but their names must conform.
7633 return Nkind (E1) = N_Identifier
7634 and then Nkind (E2) = N_Identifier
7635 and then Chars (E1) = Chars (E2);
7636 end if;
7638 elsif Nkind (E1) = N_Character_Literal
7639 and then Nkind (E2) = N_Expanded_Name
7640 then
7641 return Nkind (Selector_Name (E2)) = N_Character_Literal
7642 and then Chars (E1) = Chars (Selector_Name (E2));
7644 elsif Nkind (E2) = N_Character_Literal
7645 and then Nkind (E1) = N_Expanded_Name
7646 then
7647 return Nkind (Selector_Name (E1)) = N_Character_Literal
7648 and then Chars (E2) = Chars (Selector_Name (E1));
7650 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
7651 return FCO (E1, E2);
7653 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
7654 return FCO (E2, E1);
7656 -- Otherwise we must have the same syntactic entity
7658 elsif Nkind (E1) /= Nkind (E2) then
7659 return False;
7661 -- At this point, we specialize by node type
7663 else
7664 case Nkind (E1) is
7666 when N_Aggregate =>
7667 return
7668 FCL (Expressions (E1), Expressions (E2))
7669 and then
7670 FCL (Component_Associations (E1),
7671 Component_Associations (E2));
7673 when N_Allocator =>
7674 if Nkind (Expression (E1)) = N_Qualified_Expression
7675 or else
7676 Nkind (Expression (E2)) = N_Qualified_Expression
7677 then
7678 return FCE (Expression (E1), Expression (E2));
7680 -- Check that the subtype marks and any constraints
7681 -- are conformant
7683 else
7684 declare
7685 Indic1 : constant Node_Id := Expression (E1);
7686 Indic2 : constant Node_Id := Expression (E2);
7687 Elt1 : Node_Id;
7688 Elt2 : Node_Id;
7690 begin
7691 if Nkind (Indic1) /= N_Subtype_Indication then
7692 return
7693 Nkind (Indic2) /= N_Subtype_Indication
7694 and then Entity (Indic1) = Entity (Indic2);
7696 elsif Nkind (Indic2) /= N_Subtype_Indication then
7697 return
7698 Nkind (Indic1) /= N_Subtype_Indication
7699 and then Entity (Indic1) = Entity (Indic2);
7701 else
7702 if Entity (Subtype_Mark (Indic1)) /=
7703 Entity (Subtype_Mark (Indic2))
7704 then
7705 return False;
7706 end if;
7708 Elt1 := First (Constraints (Constraint (Indic1)));
7709 Elt2 := First (Constraints (Constraint (Indic2)));
7710 while Present (Elt1) and then Present (Elt2) loop
7711 if not FCE (Elt1, Elt2) then
7712 return False;
7713 end if;
7715 Next (Elt1);
7716 Next (Elt2);
7717 end loop;
7719 return True;
7720 end if;
7721 end;
7722 end if;
7724 when N_Attribute_Reference =>
7725 return
7726 Attribute_Name (E1) = Attribute_Name (E2)
7727 and then FCL (Expressions (E1), Expressions (E2));
7729 when N_Binary_Op =>
7730 return
7731 Entity (E1) = Entity (E2)
7732 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
7733 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
7735 when N_Short_Circuit | N_Membership_Test =>
7736 return
7737 FCE (Left_Opnd (E1), Left_Opnd (E2))
7738 and then
7739 FCE (Right_Opnd (E1), Right_Opnd (E2));
7741 when N_Case_Expression =>
7742 declare
7743 Alt1 : Node_Id;
7744 Alt2 : Node_Id;
7746 begin
7747 if not FCE (Expression (E1), Expression (E2)) then
7748 return False;
7750 else
7751 Alt1 := First (Alternatives (E1));
7752 Alt2 := First (Alternatives (E2));
7753 loop
7754 if Present (Alt1) /= Present (Alt2) then
7755 return False;
7756 elsif No (Alt1) then
7757 return True;
7758 end if;
7760 if not FCE (Expression (Alt1), Expression (Alt2))
7761 or else not FCL (Discrete_Choices (Alt1),
7762 Discrete_Choices (Alt2))
7763 then
7764 return False;
7765 end if;
7767 Next (Alt1);
7768 Next (Alt2);
7769 end loop;
7770 end if;
7771 end;
7773 when N_Character_Literal =>
7774 return
7775 Char_Literal_Value (E1) = Char_Literal_Value (E2);
7777 when N_Component_Association =>
7778 return
7779 FCL (Choices (E1), Choices (E2))
7780 and then
7781 FCE (Expression (E1), Expression (E2));
7783 when N_Explicit_Dereference =>
7784 return
7785 FCE (Prefix (E1), Prefix (E2));
7787 when N_Extension_Aggregate =>
7788 return
7789 FCL (Expressions (E1), Expressions (E2))
7790 and then Null_Record_Present (E1) =
7791 Null_Record_Present (E2)
7792 and then FCL (Component_Associations (E1),
7793 Component_Associations (E2));
7795 when N_Function_Call =>
7796 return
7797 FCE (Name (E1), Name (E2))
7798 and then
7799 FCL (Parameter_Associations (E1),
7800 Parameter_Associations (E2));
7802 when N_If_Expression =>
7803 return
7804 FCL (Expressions (E1), Expressions (E2));
7806 when N_Indexed_Component =>
7807 return
7808 FCE (Prefix (E1), Prefix (E2))
7809 and then
7810 FCL (Expressions (E1), Expressions (E2));
7812 when N_Integer_Literal =>
7813 return (Intval (E1) = Intval (E2));
7815 when N_Null =>
7816 return True;
7818 when N_Operator_Symbol =>
7819 return
7820 Chars (E1) = Chars (E2);
7822 when N_Others_Choice =>
7823 return True;
7825 when N_Parameter_Association =>
7826 return
7827 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
7828 and then FCE (Explicit_Actual_Parameter (E1),
7829 Explicit_Actual_Parameter (E2));
7831 when N_Qualified_Expression =>
7832 return
7833 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7834 and then
7835 FCE (Expression (E1), Expression (E2));
7837 when N_Quantified_Expression =>
7838 if not FCE (Condition (E1), Condition (E2)) then
7839 return False;
7840 end if;
7842 if Present (Loop_Parameter_Specification (E1))
7843 and then Present (Loop_Parameter_Specification (E2))
7844 then
7845 declare
7846 L1 : constant Node_Id :=
7847 Loop_Parameter_Specification (E1);
7848 L2 : constant Node_Id :=
7849 Loop_Parameter_Specification (E2);
7851 begin
7852 return
7853 Reverse_Present (L1) = Reverse_Present (L2)
7854 and then
7855 FCE (Defining_Identifier (L1),
7856 Defining_Identifier (L2))
7857 and then
7858 FCE (Discrete_Subtype_Definition (L1),
7859 Discrete_Subtype_Definition (L2));
7860 end;
7862 elsif Present (Iterator_Specification (E1))
7863 and then Present (Iterator_Specification (E2))
7864 then
7865 declare
7866 I1 : constant Node_Id := Iterator_Specification (E1);
7867 I2 : constant Node_Id := Iterator_Specification (E2);
7869 begin
7870 return
7871 FCE (Defining_Identifier (I1),
7872 Defining_Identifier (I2))
7873 and then
7874 Of_Present (I1) = Of_Present (I2)
7875 and then
7876 Reverse_Present (I1) = Reverse_Present (I2)
7877 and then FCE (Name (I1), Name (I2))
7878 and then FCE (Subtype_Indication (I1),
7879 Subtype_Indication (I2));
7880 end;
7882 -- The quantified expressions used different specifications to
7883 -- walk their respective ranges.
7885 else
7886 return False;
7887 end if;
7889 when N_Range =>
7890 return
7891 FCE (Low_Bound (E1), Low_Bound (E2))
7892 and then
7893 FCE (High_Bound (E1), High_Bound (E2));
7895 when N_Real_Literal =>
7896 return (Realval (E1) = Realval (E2));
7898 when N_Selected_Component =>
7899 return
7900 FCE (Prefix (E1), Prefix (E2))
7901 and then
7902 FCE (Selector_Name (E1), Selector_Name (E2));
7904 when N_Slice =>
7905 return
7906 FCE (Prefix (E1), Prefix (E2))
7907 and then
7908 FCE (Discrete_Range (E1), Discrete_Range (E2));
7910 when N_String_Literal =>
7911 declare
7912 S1 : constant String_Id := Strval (E1);
7913 S2 : constant String_Id := Strval (E2);
7914 L1 : constant Nat := String_Length (S1);
7915 L2 : constant Nat := String_Length (S2);
7917 begin
7918 if L1 /= L2 then
7919 return False;
7921 else
7922 for J in 1 .. L1 loop
7923 if Get_String_Char (S1, J) /=
7924 Get_String_Char (S2, J)
7925 then
7926 return False;
7927 end if;
7928 end loop;
7930 return True;
7931 end if;
7932 end;
7934 when N_Type_Conversion =>
7935 return
7936 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7937 and then
7938 FCE (Expression (E1), Expression (E2));
7940 when N_Unary_Op =>
7941 return
7942 Entity (E1) = Entity (E2)
7943 and then
7944 FCE (Right_Opnd (E1), Right_Opnd (E2));
7946 when N_Unchecked_Type_Conversion =>
7947 return
7948 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7949 and then
7950 FCE (Expression (E1), Expression (E2));
7952 -- All other node types cannot appear in this context. Strictly
7953 -- we should raise a fatal internal error. Instead we just ignore
7954 -- the nodes. This means that if anyone makes a mistake in the
7955 -- expander and mucks an expression tree irretrievably, the result
7956 -- will be a failure to detect a (probably very obscure) case
7957 -- of non-conformance, which is better than bombing on some
7958 -- case where two expressions do in fact conform.
7960 when others =>
7961 return True;
7963 end case;
7964 end if;
7965 end Fully_Conformant_Expressions;
7967 ----------------------------------------
7968 -- Fully_Conformant_Discrete_Subtypes --
7969 ----------------------------------------
7971 function Fully_Conformant_Discrete_Subtypes
7972 (Given_S1 : Node_Id;
7973 Given_S2 : Node_Id) return Boolean
7975 S1 : constant Node_Id := Original_Node (Given_S1);
7976 S2 : constant Node_Id := Original_Node (Given_S2);
7978 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
7979 -- Special-case for a bound given by a discriminant, which in the body
7980 -- is replaced with the discriminal of the enclosing type.
7982 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
7983 -- Check both bounds
7985 -----------------------
7986 -- Conforming_Bounds --
7987 -----------------------
7989 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
7990 begin
7991 if Is_Entity_Name (B1)
7992 and then Is_Entity_Name (B2)
7993 and then Ekind (Entity (B1)) = E_Discriminant
7994 then
7995 return Chars (B1) = Chars (B2);
7997 else
7998 return Fully_Conformant_Expressions (B1, B2);
7999 end if;
8000 end Conforming_Bounds;
8002 -----------------------
8003 -- Conforming_Ranges --
8004 -----------------------
8006 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
8007 begin
8008 return
8009 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
8010 and then
8011 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
8012 end Conforming_Ranges;
8014 -- Start of processing for Fully_Conformant_Discrete_Subtypes
8016 begin
8017 if Nkind (S1) /= Nkind (S2) then
8018 return False;
8020 elsif Is_Entity_Name (S1) then
8021 return Entity (S1) = Entity (S2);
8023 elsif Nkind (S1) = N_Range then
8024 return Conforming_Ranges (S1, S2);
8026 elsif Nkind (S1) = N_Subtype_Indication then
8027 return
8028 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
8029 and then
8030 Conforming_Ranges
8031 (Range_Expression (Constraint (S1)),
8032 Range_Expression (Constraint (S2)));
8033 else
8034 return True;
8035 end if;
8036 end Fully_Conformant_Discrete_Subtypes;
8038 --------------------
8039 -- Install_Entity --
8040 --------------------
8042 procedure Install_Entity (E : Entity_Id) is
8043 Prev : constant Entity_Id := Current_Entity (E);
8044 begin
8045 Set_Is_Immediately_Visible (E);
8046 Set_Current_Entity (E);
8047 Set_Homonym (E, Prev);
8048 end Install_Entity;
8050 ---------------------
8051 -- Install_Formals --
8052 ---------------------
8054 procedure Install_Formals (Id : Entity_Id) is
8055 F : Entity_Id;
8056 begin
8057 F := First_Formal (Id);
8058 while Present (F) loop
8059 Install_Entity (F);
8060 Next_Formal (F);
8061 end loop;
8062 end Install_Formals;
8064 -----------------------------
8065 -- Is_Interface_Conformant --
8066 -----------------------------
8068 function Is_Interface_Conformant
8069 (Tagged_Type : Entity_Id;
8070 Iface_Prim : Entity_Id;
8071 Prim : Entity_Id) return Boolean
8073 -- The operation may in fact be an inherited (implicit) operation
8074 -- rather than the original interface primitive, so retrieve the
8075 -- ultimate ancestor.
8077 Iface : constant Entity_Id :=
8078 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
8079 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
8081 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
8082 -- Return the controlling formal of Prim
8084 ------------------------
8085 -- Controlling_Formal --
8086 ------------------------
8088 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
8089 E : Entity_Id;
8091 begin
8092 E := First_Entity (Prim);
8093 while Present (E) loop
8094 if Is_Formal (E) and then Is_Controlling_Formal (E) then
8095 return E;
8096 end if;
8098 Next_Entity (E);
8099 end loop;
8101 return Empty;
8102 end Controlling_Formal;
8104 -- Local variables
8106 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
8107 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
8109 -- Start of processing for Is_Interface_Conformant
8111 begin
8112 pragma Assert (Is_Subprogram (Iface_Prim)
8113 and then Is_Subprogram (Prim)
8114 and then Is_Dispatching_Operation (Iface_Prim)
8115 and then Is_Dispatching_Operation (Prim));
8117 pragma Assert (Is_Interface (Iface)
8118 or else (Present (Alias (Iface_Prim))
8119 and then
8120 Is_Interface
8121 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
8123 if Prim = Iface_Prim
8124 or else not Is_Subprogram (Prim)
8125 or else Ekind (Prim) /= Ekind (Iface_Prim)
8126 or else not Is_Dispatching_Operation (Prim)
8127 or else Scope (Prim) /= Scope (Tagged_Type)
8128 or else No (Typ)
8129 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
8130 or else not Primitive_Names_Match (Iface_Prim, Prim)
8131 then
8132 return False;
8134 -- The mode of the controlling formals must match
8136 elsif Present (Iface_Ctrl_F)
8137 and then Present (Prim_Ctrl_F)
8138 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
8139 then
8140 return False;
8142 -- Case of a procedure, or a function whose result type matches the
8143 -- result type of the interface primitive, or a function that has no
8144 -- controlling result (I or access I).
8146 elsif Ekind (Iface_Prim) = E_Procedure
8147 or else Etype (Prim) = Etype (Iface_Prim)
8148 or else not Has_Controlling_Result (Prim)
8149 then
8150 return Type_Conformant
8151 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
8153 -- Case of a function returning an interface, or an access to one. Check
8154 -- that the return types correspond.
8156 elsif Implements_Interface (Typ, Iface) then
8157 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
8159 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
8160 then
8161 return False;
8162 else
8163 return
8164 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
8165 Skip_Controlling_Formals => True);
8166 end if;
8168 else
8169 return False;
8170 end if;
8171 end Is_Interface_Conformant;
8173 ---------------------------------
8174 -- Is_Non_Overriding_Operation --
8175 ---------------------------------
8177 function Is_Non_Overriding_Operation
8178 (Prev_E : Entity_Id;
8179 New_E : Entity_Id) return Boolean
8181 Formal : Entity_Id;
8182 F_Typ : Entity_Id;
8183 G_Typ : Entity_Id := Empty;
8185 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
8186 -- If F_Type is a derived type associated with a generic actual subtype,
8187 -- then return its Generic_Parent_Type attribute, else return Empty.
8189 function Types_Correspond
8190 (P_Type : Entity_Id;
8191 N_Type : Entity_Id) return Boolean;
8192 -- Returns true if and only if the types (or designated types in the
8193 -- case of anonymous access types) are the same or N_Type is derived
8194 -- directly or indirectly from P_Type.
8196 -----------------------------
8197 -- Get_Generic_Parent_Type --
8198 -----------------------------
8200 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
8201 G_Typ : Entity_Id;
8202 Defn : Node_Id;
8203 Indic : Node_Id;
8205 begin
8206 if Is_Derived_Type (F_Typ)
8207 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
8208 then
8209 -- The tree must be traversed to determine the parent subtype in
8210 -- the generic unit, which unfortunately isn't always available
8211 -- via semantic attributes. ??? (Note: The use of Original_Node
8212 -- is needed for cases where a full derived type has been
8213 -- rewritten.)
8215 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
8216 if Nkind (Defn) = N_Derived_Type_Definition then
8217 Indic := Subtype_Indication (Defn);
8219 if Nkind (Indic) = N_Subtype_Indication then
8220 G_Typ := Entity (Subtype_Mark (Indic));
8221 else
8222 G_Typ := Entity (Indic);
8223 end if;
8225 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
8226 and then Present (Generic_Parent_Type (Parent (G_Typ)))
8227 then
8228 return Generic_Parent_Type (Parent (G_Typ));
8229 end if;
8230 end if;
8231 end if;
8233 return Empty;
8234 end Get_Generic_Parent_Type;
8236 ----------------------
8237 -- Types_Correspond --
8238 ----------------------
8240 function Types_Correspond
8241 (P_Type : Entity_Id;
8242 N_Type : Entity_Id) return Boolean
8244 Prev_Type : Entity_Id := Base_Type (P_Type);
8245 New_Type : Entity_Id := Base_Type (N_Type);
8247 begin
8248 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
8249 Prev_Type := Designated_Type (Prev_Type);
8250 end if;
8252 if Ekind (New_Type) = E_Anonymous_Access_Type then
8253 New_Type := Designated_Type (New_Type);
8254 end if;
8256 if Prev_Type = New_Type then
8257 return True;
8259 elsif not Is_Class_Wide_Type (New_Type) then
8260 while Etype (New_Type) /= New_Type loop
8261 New_Type := Etype (New_Type);
8262 if New_Type = Prev_Type then
8263 return True;
8264 end if;
8265 end loop;
8266 end if;
8267 return False;
8268 end Types_Correspond;
8270 -- Start of processing for Is_Non_Overriding_Operation
8272 begin
8273 -- In the case where both operations are implicit derived subprograms
8274 -- then neither overrides the other. This can only occur in certain
8275 -- obscure cases (e.g., derivation from homographs created in a generic
8276 -- instantiation).
8278 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
8279 return True;
8281 elsif Ekind (Current_Scope) = E_Package
8282 and then Is_Generic_Instance (Current_Scope)
8283 and then In_Private_Part (Current_Scope)
8284 and then Comes_From_Source (New_E)
8285 then
8286 -- We examine the formals and result type of the inherited operation,
8287 -- to determine whether their type is derived from (the instance of)
8288 -- a generic type. The first such formal or result type is the one
8289 -- tested.
8291 Formal := First_Formal (Prev_E);
8292 while Present (Formal) loop
8293 F_Typ := Base_Type (Etype (Formal));
8295 if Ekind (F_Typ) = E_Anonymous_Access_Type then
8296 F_Typ := Designated_Type (F_Typ);
8297 end if;
8299 G_Typ := Get_Generic_Parent_Type (F_Typ);
8300 exit when Present (G_Typ);
8302 Next_Formal (Formal);
8303 end loop;
8305 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
8306 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
8307 end if;
8309 if No (G_Typ) then
8310 return False;
8311 end if;
8313 -- If the generic type is a private type, then the original operation
8314 -- was not overriding in the generic, because there was no primitive
8315 -- operation to override.
8317 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
8318 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
8319 N_Formal_Private_Type_Definition
8320 then
8321 return True;
8323 -- The generic parent type is the ancestor of a formal derived
8324 -- type declaration. We need to check whether it has a primitive
8325 -- operation that should be overridden by New_E in the generic.
8327 else
8328 declare
8329 P_Formal : Entity_Id;
8330 N_Formal : Entity_Id;
8331 P_Typ : Entity_Id;
8332 N_Typ : Entity_Id;
8333 P_Prim : Entity_Id;
8334 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
8336 begin
8337 while Present (Prim_Elt) loop
8338 P_Prim := Node (Prim_Elt);
8340 if Chars (P_Prim) = Chars (New_E)
8341 and then Ekind (P_Prim) = Ekind (New_E)
8342 then
8343 P_Formal := First_Formal (P_Prim);
8344 N_Formal := First_Formal (New_E);
8345 while Present (P_Formal) and then Present (N_Formal) loop
8346 P_Typ := Etype (P_Formal);
8347 N_Typ := Etype (N_Formal);
8349 if not Types_Correspond (P_Typ, N_Typ) then
8350 exit;
8351 end if;
8353 Next_Entity (P_Formal);
8354 Next_Entity (N_Formal);
8355 end loop;
8357 -- Found a matching primitive operation belonging to the
8358 -- formal ancestor type, so the new subprogram is
8359 -- overriding.
8361 if No (P_Formal)
8362 and then No (N_Formal)
8363 and then (Ekind (New_E) /= E_Function
8364 or else
8365 Types_Correspond
8366 (Etype (P_Prim), Etype (New_E)))
8367 then
8368 return False;
8369 end if;
8370 end if;
8372 Next_Elmt (Prim_Elt);
8373 end loop;
8375 -- If no match found, then the new subprogram does not override
8376 -- in the generic (nor in the instance).
8378 -- If the type in question is not abstract, and the subprogram
8379 -- is, this will be an error if the new operation is in the
8380 -- private part of the instance. Emit a warning now, which will
8381 -- make the subsequent error message easier to understand.
8383 if not Is_Abstract_Type (F_Typ)
8384 and then Is_Abstract_Subprogram (Prev_E)
8385 and then In_Private_Part (Current_Scope)
8386 then
8387 Error_Msg_Node_2 := F_Typ;
8388 Error_Msg_NE
8389 ("private operation& in generic unit does not override "
8390 & "any primitive operation of& (RM 12.3 (18))??",
8391 New_E, New_E);
8392 end if;
8394 return True;
8395 end;
8396 end if;
8397 else
8398 return False;
8399 end if;
8400 end Is_Non_Overriding_Operation;
8402 -------------------------------------
8403 -- List_Inherited_Pre_Post_Aspects --
8404 -------------------------------------
8406 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
8407 begin
8408 if Opt.List_Inherited_Aspects
8409 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
8410 then
8411 declare
8412 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
8413 P : Node_Id;
8415 begin
8416 for J in Inherited'Range loop
8417 P := Pre_Post_Conditions (Contract (Inherited (J)));
8418 while Present (P) loop
8419 Error_Msg_Sloc := Sloc (P);
8421 if Class_Present (P) and then not Split_PPC (P) then
8422 if Pragma_Name (P) = Name_Precondition then
8423 Error_Msg_N ("info: & inherits `Pre''Class` aspect "
8424 & "from #?L?", E);
8425 else
8426 Error_Msg_N ("info: & inherits `Post''Class` aspect "
8427 & "from #?L?", E);
8428 end if;
8429 end if;
8431 P := Next_Pragma (P);
8432 end loop;
8433 end loop;
8434 end;
8435 end if;
8436 end List_Inherited_Pre_Post_Aspects;
8438 ------------------------------
8439 -- Make_Inequality_Operator --
8440 ------------------------------
8442 -- S is the defining identifier of an equality operator. We build a
8443 -- subprogram declaration with the right signature. This operation is
8444 -- intrinsic, because it is always expanded as the negation of the
8445 -- call to the equality function.
8447 procedure Make_Inequality_Operator (S : Entity_Id) is
8448 Loc : constant Source_Ptr := Sloc (S);
8449 Decl : Node_Id;
8450 Formals : List_Id;
8451 Op_Name : Entity_Id;
8453 FF : constant Entity_Id := First_Formal (S);
8454 NF : constant Entity_Id := Next_Formal (FF);
8456 begin
8457 -- Check that equality was properly defined, ignore call if not
8459 if No (NF) then
8460 return;
8461 end if;
8463 declare
8464 A : constant Entity_Id :=
8465 Make_Defining_Identifier (Sloc (FF),
8466 Chars => Chars (FF));
8468 B : constant Entity_Id :=
8469 Make_Defining_Identifier (Sloc (NF),
8470 Chars => Chars (NF));
8472 begin
8473 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
8475 Formals := New_List (
8476 Make_Parameter_Specification (Loc,
8477 Defining_Identifier => A,
8478 Parameter_Type =>
8479 New_Occurrence_Of (Etype (First_Formal (S)),
8480 Sloc (Etype (First_Formal (S))))),
8482 Make_Parameter_Specification (Loc,
8483 Defining_Identifier => B,
8484 Parameter_Type =>
8485 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
8486 Sloc (Etype (Next_Formal (First_Formal (S)))))));
8488 Decl :=
8489 Make_Subprogram_Declaration (Loc,
8490 Specification =>
8491 Make_Function_Specification (Loc,
8492 Defining_Unit_Name => Op_Name,
8493 Parameter_Specifications => Formals,
8494 Result_Definition =>
8495 New_Occurrence_Of (Standard_Boolean, Loc)));
8497 -- Insert inequality right after equality if it is explicit or after
8498 -- the derived type when implicit. These entities are created only
8499 -- for visibility purposes, and eventually replaced in the course
8500 -- of expansion, so they do not need to be attached to the tree and
8501 -- seen by the back-end. Keeping them internal also avoids spurious
8502 -- freezing problems. The declaration is inserted in the tree for
8503 -- analysis, and removed afterwards. If the equality operator comes
8504 -- from an explicit declaration, attach the inequality immediately
8505 -- after. Else the equality is inherited from a derived type
8506 -- declaration, so insert inequality after that declaration.
8508 if No (Alias (S)) then
8509 Insert_After (Unit_Declaration_Node (S), Decl);
8510 elsif Is_List_Member (Parent (S)) then
8511 Insert_After (Parent (S), Decl);
8512 else
8513 Insert_After (Parent (Etype (First_Formal (S))), Decl);
8514 end if;
8516 Mark_Rewrite_Insertion (Decl);
8517 Set_Is_Intrinsic_Subprogram (Op_Name);
8518 Analyze (Decl);
8519 Remove (Decl);
8520 Set_Has_Completion (Op_Name);
8521 Set_Corresponding_Equality (Op_Name, S);
8522 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
8523 end;
8524 end Make_Inequality_Operator;
8526 ----------------------
8527 -- May_Need_Actuals --
8528 ----------------------
8530 procedure May_Need_Actuals (Fun : Entity_Id) is
8531 F : Entity_Id;
8532 B : Boolean;
8534 begin
8535 F := First_Formal (Fun);
8536 B := True;
8537 while Present (F) loop
8538 if No (Default_Value (F)) then
8539 B := False;
8540 exit;
8541 end if;
8543 Next_Formal (F);
8544 end loop;
8546 Set_Needs_No_Actuals (Fun, B);
8547 end May_Need_Actuals;
8549 ---------------------
8550 -- Mode_Conformant --
8551 ---------------------
8553 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8554 Result : Boolean;
8555 begin
8556 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
8557 return Result;
8558 end Mode_Conformant;
8560 ---------------------------
8561 -- New_Overloaded_Entity --
8562 ---------------------------
8564 procedure New_Overloaded_Entity
8565 (S : Entity_Id;
8566 Derived_Type : Entity_Id := Empty)
8568 Overridden_Subp : Entity_Id := Empty;
8569 -- Set if the current scope has an operation that is type-conformant
8570 -- with S, and becomes hidden by S.
8572 Is_Primitive_Subp : Boolean;
8573 -- Set to True if the new subprogram is primitive
8575 E : Entity_Id;
8576 -- Entity that S overrides
8578 Prev_Vis : Entity_Id := Empty;
8579 -- Predecessor of E in Homonym chain
8581 procedure Check_For_Primitive_Subprogram
8582 (Is_Primitive : out Boolean;
8583 Is_Overriding : Boolean := False);
8584 -- If the subprogram being analyzed is a primitive operation of the type
8585 -- of a formal or result, set the Has_Primitive_Operations flag on the
8586 -- type, and set Is_Primitive to True (otherwise set to False). Set the
8587 -- corresponding flag on the entity itself for later use.
8589 procedure Check_Synchronized_Overriding
8590 (Def_Id : Entity_Id;
8591 Overridden_Subp : out Entity_Id);
8592 -- First determine if Def_Id is an entry or a subprogram either defined
8593 -- in the scope of a task or protected type, or is a primitive of such
8594 -- a type. Check whether Def_Id overrides a subprogram of an interface
8595 -- implemented by the synchronized type, return the overridden entity
8596 -- or Empty.
8598 function Is_Private_Declaration (E : Entity_Id) return Boolean;
8599 -- Check that E is declared in the private part of the current package,
8600 -- or in the package body, where it may hide a previous declaration.
8601 -- We can't use In_Private_Part by itself because this flag is also
8602 -- set when freezing entities, so we must examine the place of the
8603 -- declaration in the tree, and recognize wrapper packages as well.
8605 function Is_Overriding_Alias
8606 (Old_E : Entity_Id;
8607 New_E : Entity_Id) return Boolean;
8608 -- Check whether new subprogram and old subprogram are both inherited
8609 -- from subprograms that have distinct dispatch table entries. This can
8610 -- occur with derivations from instances with accidental homonyms. The
8611 -- function is conservative given that the converse is only true within
8612 -- instances that contain accidental overloadings.
8614 ------------------------------------
8615 -- Check_For_Primitive_Subprogram --
8616 ------------------------------------
8618 procedure Check_For_Primitive_Subprogram
8619 (Is_Primitive : out Boolean;
8620 Is_Overriding : Boolean := False)
8622 Formal : Entity_Id;
8623 F_Typ : Entity_Id;
8624 B_Typ : Entity_Id;
8626 function Visible_Part_Type (T : Entity_Id) return Boolean;
8627 -- Returns true if T is declared in the visible part of the current
8628 -- package scope; otherwise returns false. Assumes that T is declared
8629 -- in a package.
8631 procedure Check_Private_Overriding (T : Entity_Id);
8632 -- Checks that if a primitive abstract subprogram of a visible
8633 -- abstract type is declared in a private part, then it must override
8634 -- an abstract subprogram declared in the visible part. Also checks
8635 -- that if a primitive function with a controlling result is declared
8636 -- in a private part, then it must override a function declared in
8637 -- the visible part.
8639 ------------------------------
8640 -- Check_Private_Overriding --
8641 ------------------------------
8643 procedure Check_Private_Overriding (T : Entity_Id) is
8644 begin
8645 if Is_Package_Or_Generic_Package (Current_Scope)
8646 and then In_Private_Part (Current_Scope)
8647 and then Visible_Part_Type (T)
8648 and then not In_Instance
8649 then
8650 if Is_Abstract_Type (T)
8651 and then Is_Abstract_Subprogram (S)
8652 and then (not Is_Overriding
8653 or else not Is_Abstract_Subprogram (E))
8654 then
8655 Error_Msg_N ("abstract subprograms must be visible "
8656 & "(RM 3.9.3(10))!", S);
8658 elsif Ekind (S) = E_Function and then not Is_Overriding then
8659 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
8660 Error_Msg_N ("private function with tagged result must"
8661 & " override visible-part function", S);
8662 Error_Msg_N ("\move subprogram to the visible part"
8663 & " (RM 3.9.3(10))", S);
8665 -- AI05-0073: extend this test to the case of a function
8666 -- with a controlling access result.
8668 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
8669 and then Is_Tagged_Type (Designated_Type (Etype (S)))
8670 and then
8671 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
8672 and then Ada_Version >= Ada_2012
8673 then
8674 Error_Msg_N
8675 ("private function with controlling access result "
8676 & "must override visible-part function", S);
8677 Error_Msg_N
8678 ("\move subprogram to the visible part"
8679 & " (RM 3.9.3(10))", S);
8680 end if;
8681 end if;
8682 end if;
8683 end Check_Private_Overriding;
8685 -----------------------
8686 -- Visible_Part_Type --
8687 -----------------------
8689 function Visible_Part_Type (T : Entity_Id) return Boolean is
8690 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
8691 N : Node_Id;
8693 begin
8694 -- If the entity is a private type, then it must be declared in a
8695 -- visible part.
8697 if Ekind (T) in Private_Kind then
8698 return True;
8699 end if;
8701 -- Otherwise, we traverse the visible part looking for its
8702 -- corresponding declaration. We cannot use the declaration
8703 -- node directly because in the private part the entity of a
8704 -- private type is the one in the full view, which does not
8705 -- indicate that it is the completion of something visible.
8707 N := First (Visible_Declarations (Specification (P)));
8708 while Present (N) loop
8709 if Nkind (N) = N_Full_Type_Declaration
8710 and then Present (Defining_Identifier (N))
8711 and then T = Defining_Identifier (N)
8712 then
8713 return True;
8715 elsif Nkind_In (N, N_Private_Type_Declaration,
8716 N_Private_Extension_Declaration)
8717 and then Present (Defining_Identifier (N))
8718 and then T = Full_View (Defining_Identifier (N))
8719 then
8720 return True;
8721 end if;
8723 Next (N);
8724 end loop;
8726 return False;
8727 end Visible_Part_Type;
8729 -- Start of processing for Check_For_Primitive_Subprogram
8731 begin
8732 Is_Primitive := False;
8734 if not Comes_From_Source (S) then
8735 null;
8737 -- If subprogram is at library level, it is not primitive operation
8739 elsif Current_Scope = Standard_Standard then
8740 null;
8742 elsif (Is_Package_Or_Generic_Package (Current_Scope)
8743 and then not In_Package_Body (Current_Scope))
8744 or else Is_Overriding
8745 then
8746 -- For function, check return type
8748 if Ekind (S) = E_Function then
8749 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
8750 F_Typ := Designated_Type (Etype (S));
8751 else
8752 F_Typ := Etype (S);
8753 end if;
8755 B_Typ := Base_Type (F_Typ);
8757 if Scope (B_Typ) = Current_Scope
8758 and then not Is_Class_Wide_Type (B_Typ)
8759 and then not Is_Generic_Type (B_Typ)
8760 then
8761 Is_Primitive := True;
8762 Set_Has_Primitive_Operations (B_Typ);
8763 Set_Is_Primitive (S);
8764 Check_Private_Overriding (B_Typ);
8765 end if;
8766 end if;
8768 -- For all subprograms, check formals
8770 Formal := First_Formal (S);
8771 while Present (Formal) loop
8772 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
8773 F_Typ := Designated_Type (Etype (Formal));
8774 else
8775 F_Typ := Etype (Formal);
8776 end if;
8778 B_Typ := Base_Type (F_Typ);
8780 if Ekind (B_Typ) = E_Access_Subtype then
8781 B_Typ := Base_Type (B_Typ);
8782 end if;
8784 if Scope (B_Typ) = Current_Scope
8785 and then not Is_Class_Wide_Type (B_Typ)
8786 and then not Is_Generic_Type (B_Typ)
8787 then
8788 Is_Primitive := True;
8789 Set_Is_Primitive (S);
8790 Set_Has_Primitive_Operations (B_Typ);
8791 Check_Private_Overriding (B_Typ);
8792 end if;
8794 Next_Formal (Formal);
8795 end loop;
8797 -- Special case: An equality function can be redefined for a type
8798 -- occurring in a declarative part, and won't otherwise be treated as
8799 -- a primitive because it doesn't occur in a package spec and doesn't
8800 -- override an inherited subprogram. It's important that we mark it
8801 -- primitive so it can be returned by Collect_Primitive_Operations
8802 -- and be used in composing the equality operation of later types
8803 -- that have a component of the type.
8805 elsif Chars (S) = Name_Op_Eq
8806 and then Etype (S) = Standard_Boolean
8807 then
8808 B_Typ := Base_Type (Etype (First_Formal (S)));
8810 if Scope (B_Typ) = Current_Scope
8811 and then
8812 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
8813 and then not Is_Limited_Type (B_Typ)
8814 then
8815 Is_Primitive := True;
8816 Set_Is_Primitive (S);
8817 Set_Has_Primitive_Operations (B_Typ);
8818 Check_Private_Overriding (B_Typ);
8819 end if;
8820 end if;
8821 end Check_For_Primitive_Subprogram;
8823 -----------------------------------
8824 -- Check_Synchronized_Overriding --
8825 -----------------------------------
8827 procedure Check_Synchronized_Overriding
8828 (Def_Id : Entity_Id;
8829 Overridden_Subp : out Entity_Id)
8831 Ifaces_List : Elist_Id;
8832 In_Scope : Boolean;
8833 Typ : Entity_Id;
8835 function Matches_Prefixed_View_Profile
8836 (Prim_Params : List_Id;
8837 Iface_Params : List_Id) return Boolean;
8838 -- Determine whether a subprogram's parameter profile Prim_Params
8839 -- matches that of a potentially overridden interface subprogram
8840 -- Iface_Params. Also determine if the type of first parameter of
8841 -- Iface_Params is an implemented interface.
8843 -----------------------------------
8844 -- Matches_Prefixed_View_Profile --
8845 -----------------------------------
8847 function Matches_Prefixed_View_Profile
8848 (Prim_Params : List_Id;
8849 Iface_Params : List_Id) return Boolean
8851 Iface_Id : Entity_Id;
8852 Iface_Param : Node_Id;
8853 Iface_Typ : Entity_Id;
8854 Prim_Id : Entity_Id;
8855 Prim_Param : Node_Id;
8856 Prim_Typ : Entity_Id;
8858 function Is_Implemented
8859 (Ifaces_List : Elist_Id;
8860 Iface : Entity_Id) return Boolean;
8861 -- Determine if Iface is implemented by the current task or
8862 -- protected type.
8864 --------------------
8865 -- Is_Implemented --
8866 --------------------
8868 function Is_Implemented
8869 (Ifaces_List : Elist_Id;
8870 Iface : Entity_Id) return Boolean
8872 Iface_Elmt : Elmt_Id;
8874 begin
8875 Iface_Elmt := First_Elmt (Ifaces_List);
8876 while Present (Iface_Elmt) loop
8877 if Node (Iface_Elmt) = Iface then
8878 return True;
8879 end if;
8881 Next_Elmt (Iface_Elmt);
8882 end loop;
8884 return False;
8885 end Is_Implemented;
8887 -- Start of processing for Matches_Prefixed_View_Profile
8889 begin
8890 Iface_Param := First (Iface_Params);
8891 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
8893 if Is_Access_Type (Iface_Typ) then
8894 Iface_Typ := Designated_Type (Iface_Typ);
8895 end if;
8897 Prim_Param := First (Prim_Params);
8899 -- The first parameter of the potentially overridden subprogram
8900 -- must be an interface implemented by Prim.
8902 if not Is_Interface (Iface_Typ)
8903 or else not Is_Implemented (Ifaces_List, Iface_Typ)
8904 then
8905 return False;
8906 end if;
8908 -- The checks on the object parameters are done, move onto the
8909 -- rest of the parameters.
8911 if not In_Scope then
8912 Prim_Param := Next (Prim_Param);
8913 end if;
8915 Iface_Param := Next (Iface_Param);
8916 while Present (Iface_Param) and then Present (Prim_Param) loop
8917 Iface_Id := Defining_Identifier (Iface_Param);
8918 Iface_Typ := Find_Parameter_Type (Iface_Param);
8920 Prim_Id := Defining_Identifier (Prim_Param);
8921 Prim_Typ := Find_Parameter_Type (Prim_Param);
8923 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
8924 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
8925 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
8926 then
8927 Iface_Typ := Designated_Type (Iface_Typ);
8928 Prim_Typ := Designated_Type (Prim_Typ);
8929 end if;
8931 -- Case of multiple interface types inside a parameter profile
8933 -- (Obj_Param : in out Iface; ...; Param : Iface)
8935 -- If the interface type is implemented, then the matching type
8936 -- in the primitive should be the implementing record type.
8938 if Ekind (Iface_Typ) = E_Record_Type
8939 and then Is_Interface (Iface_Typ)
8940 and then Is_Implemented (Ifaces_List, Iface_Typ)
8941 then
8942 if Prim_Typ /= Typ then
8943 return False;
8944 end if;
8946 -- The two parameters must be both mode and subtype conformant
8948 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
8949 or else not
8950 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
8951 then
8952 return False;
8953 end if;
8955 Next (Iface_Param);
8956 Next (Prim_Param);
8957 end loop;
8959 -- One of the two lists contains more parameters than the other
8961 if Present (Iface_Param) or else Present (Prim_Param) then
8962 return False;
8963 end if;
8965 return True;
8966 end Matches_Prefixed_View_Profile;
8968 -- Start of processing for Check_Synchronized_Overriding
8970 begin
8971 Overridden_Subp := Empty;
8973 -- Def_Id must be an entry or a subprogram. We should skip predefined
8974 -- primitives internally generated by the frontend; however at this
8975 -- stage predefined primitives are still not fully decorated. As a
8976 -- minor optimization we skip here internally generated subprograms.
8978 if (Ekind (Def_Id) /= E_Entry
8979 and then Ekind (Def_Id) /= E_Function
8980 and then Ekind (Def_Id) /= E_Procedure)
8981 or else not Comes_From_Source (Def_Id)
8982 then
8983 return;
8984 end if;
8986 -- Search for the concurrent declaration since it contains the list
8987 -- of all implemented interfaces. In this case, the subprogram is
8988 -- declared within the scope of a protected or a task type.
8990 if Present (Scope (Def_Id))
8991 and then Is_Concurrent_Type (Scope (Def_Id))
8992 and then not Is_Generic_Actual_Type (Scope (Def_Id))
8993 then
8994 Typ := Scope (Def_Id);
8995 In_Scope := True;
8997 -- The enclosing scope is not a synchronized type and the subprogram
8998 -- has no formals.
9000 elsif No (First_Formal (Def_Id)) then
9001 return;
9003 -- The subprogram has formals and hence it may be a primitive of a
9004 -- concurrent type.
9006 else
9007 Typ := Etype (First_Formal (Def_Id));
9009 if Is_Access_Type (Typ) then
9010 Typ := Directly_Designated_Type (Typ);
9011 end if;
9013 if Is_Concurrent_Type (Typ)
9014 and then not Is_Generic_Actual_Type (Typ)
9015 then
9016 In_Scope := False;
9018 -- This case occurs when the concurrent type is declared within
9019 -- a generic unit. As a result the corresponding record has been
9020 -- built and used as the type of the first formal, we just have
9021 -- to retrieve the corresponding concurrent type.
9023 elsif Is_Concurrent_Record_Type (Typ)
9024 and then not Is_Class_Wide_Type (Typ)
9025 and then Present (Corresponding_Concurrent_Type (Typ))
9026 then
9027 Typ := Corresponding_Concurrent_Type (Typ);
9028 In_Scope := False;
9030 else
9031 return;
9032 end if;
9033 end if;
9035 -- There is no overriding to check if is an inherited operation in a
9036 -- type derivation on for a generic actual.
9038 Collect_Interfaces (Typ, Ifaces_List);
9040 if Is_Empty_Elmt_List (Ifaces_List) then
9041 return;
9042 end if;
9044 -- Determine whether entry or subprogram Def_Id overrides a primitive
9045 -- operation that belongs to one of the interfaces in Ifaces_List.
9047 declare
9048 Candidate : Entity_Id := Empty;
9049 Hom : Entity_Id := Empty;
9050 Iface_Typ : Entity_Id;
9051 Subp : Entity_Id := Empty;
9053 begin
9054 -- Traverse the homonym chain, looking for a potentially
9055 -- overridden subprogram that belongs to an implemented
9056 -- interface.
9058 Hom := Current_Entity_In_Scope (Def_Id);
9059 while Present (Hom) loop
9060 Subp := Hom;
9062 if Subp = Def_Id
9063 or else not Is_Overloadable (Subp)
9064 or else not Is_Primitive (Subp)
9065 or else not Is_Dispatching_Operation (Subp)
9066 or else not Present (Find_Dispatching_Type (Subp))
9067 or else not Is_Interface (Find_Dispatching_Type (Subp))
9068 then
9069 null;
9071 -- Entries and procedures can override abstract or null
9072 -- interface procedures.
9074 elsif (Ekind (Def_Id) = E_Procedure
9075 or else Ekind (Def_Id) = E_Entry)
9076 and then Ekind (Subp) = E_Procedure
9077 and then Matches_Prefixed_View_Profile
9078 (Parameter_Specifications (Parent (Def_Id)),
9079 Parameter_Specifications (Parent (Subp)))
9080 then
9081 Candidate := Subp;
9083 -- For an overridden subprogram Subp, check whether the mode
9084 -- of its first parameter is correct depending on the kind
9085 -- of synchronized type.
9087 declare
9088 Formal : constant Node_Id := First_Formal (Candidate);
9090 begin
9091 -- In order for an entry or a protected procedure to
9092 -- override, the first parameter of the overridden
9093 -- routine must be of mode "out", "in out" or
9094 -- access-to-variable.
9096 if Ekind_In (Candidate, E_Entry, E_Procedure)
9097 and then Is_Protected_Type (Typ)
9098 and then Ekind (Formal) /= E_In_Out_Parameter
9099 and then Ekind (Formal) /= E_Out_Parameter
9100 and then Nkind (Parameter_Type (Parent (Formal))) /=
9101 N_Access_Definition
9102 then
9103 null;
9105 -- All other cases are OK since a task entry or routine
9106 -- does not have a restriction on the mode of the first
9107 -- parameter of the overridden interface routine.
9109 else
9110 Overridden_Subp := Candidate;
9111 return;
9112 end if;
9113 end;
9115 -- Functions can override abstract interface functions
9117 elsif Ekind (Def_Id) = E_Function
9118 and then Ekind (Subp) = E_Function
9119 and then Matches_Prefixed_View_Profile
9120 (Parameter_Specifications (Parent (Def_Id)),
9121 Parameter_Specifications (Parent (Subp)))
9122 and then Etype (Result_Definition (Parent (Def_Id))) =
9123 Etype (Result_Definition (Parent (Subp)))
9124 then
9125 Overridden_Subp := Subp;
9126 return;
9127 end if;
9129 Hom := Homonym (Hom);
9130 end loop;
9132 -- After examining all candidates for overriding, we are left with
9133 -- the best match which is a mode incompatible interface routine.
9134 -- Do not emit an error if the Expander is active since this error
9135 -- will be detected later on after all concurrent types are
9136 -- expanded and all wrappers are built. This check is meant for
9137 -- spec-only compilations.
9139 if Present (Candidate) and then not Expander_Active then
9140 Iface_Typ :=
9141 Find_Parameter_Type (Parent (First_Formal (Candidate)));
9143 -- Def_Id is primitive of a protected type, declared inside the
9144 -- type, and the candidate is primitive of a limited or
9145 -- synchronized interface.
9147 if In_Scope
9148 and then Is_Protected_Type (Typ)
9149 and then
9150 (Is_Limited_Interface (Iface_Typ)
9151 or else Is_Protected_Interface (Iface_Typ)
9152 or else Is_Synchronized_Interface (Iface_Typ)
9153 or else Is_Task_Interface (Iface_Typ))
9154 then
9155 Error_Msg_PT (Parent (Typ), Candidate);
9156 end if;
9157 end if;
9159 Overridden_Subp := Candidate;
9160 return;
9161 end;
9162 end Check_Synchronized_Overriding;
9164 ----------------------------
9165 -- Is_Private_Declaration --
9166 ----------------------------
9168 function Is_Private_Declaration (E : Entity_Id) return Boolean is
9169 Priv_Decls : List_Id;
9170 Decl : constant Node_Id := Unit_Declaration_Node (E);
9172 begin
9173 if Is_Package_Or_Generic_Package (Current_Scope)
9174 and then In_Private_Part (Current_Scope)
9175 then
9176 Priv_Decls :=
9177 Private_Declarations (Package_Specification (Current_Scope));
9179 return In_Package_Body (Current_Scope)
9180 or else
9181 (Is_List_Member (Decl)
9182 and then List_Containing (Decl) = Priv_Decls)
9183 or else (Nkind (Parent (Decl)) = N_Package_Specification
9184 and then not
9185 Is_Compilation_Unit
9186 (Defining_Entity (Parent (Decl)))
9187 and then List_Containing (Parent (Parent (Decl))) =
9188 Priv_Decls);
9189 else
9190 return False;
9191 end if;
9192 end Is_Private_Declaration;
9194 --------------------------
9195 -- Is_Overriding_Alias --
9196 --------------------------
9198 function Is_Overriding_Alias
9199 (Old_E : Entity_Id;
9200 New_E : Entity_Id) return Boolean
9202 AO : constant Entity_Id := Alias (Old_E);
9203 AN : constant Entity_Id := Alias (New_E);
9204 begin
9205 return Scope (AO) /= Scope (AN)
9206 or else No (DTC_Entity (AO))
9207 or else No (DTC_Entity (AN))
9208 or else DT_Position (AO) = DT_Position (AN);
9209 end Is_Overriding_Alias;
9211 -- Start of processing for New_Overloaded_Entity
9213 begin
9214 -- We need to look for an entity that S may override. This must be a
9215 -- homonym in the current scope, so we look for the first homonym of
9216 -- S in the current scope as the starting point for the search.
9218 E := Current_Entity_In_Scope (S);
9220 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
9221 -- They are directly added to the list of primitive operations of
9222 -- Derived_Type, unless this is a rederivation in the private part
9223 -- of an operation that was already derived in the visible part of
9224 -- the current package.
9226 if Ada_Version >= Ada_2005
9227 and then Present (Derived_Type)
9228 and then Present (Alias (S))
9229 and then Is_Dispatching_Operation (Alias (S))
9230 and then Present (Find_Dispatching_Type (Alias (S)))
9231 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
9232 then
9233 -- For private types, when the full-view is processed we propagate to
9234 -- the full view the non-overridden entities whose attribute "alias"
9235 -- references an interface primitive. These entities were added by
9236 -- Derive_Subprograms to ensure that interface primitives are
9237 -- covered.
9239 -- Inside_Freeze_Actions is non zero when S corresponds with an
9240 -- internal entity that links an interface primitive with its
9241 -- covering primitive through attribute Interface_Alias (see
9242 -- Add_Internal_Interface_Entities).
9244 if Inside_Freezing_Actions = 0
9245 and then Is_Package_Or_Generic_Package (Current_Scope)
9246 and then In_Private_Part (Current_Scope)
9247 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
9248 and then Nkind (Parent (S)) = N_Full_Type_Declaration
9249 and then Full_View (Defining_Identifier (Parent (E)))
9250 = Defining_Identifier (Parent (S))
9251 and then Alias (E) = Alias (S)
9252 then
9253 Check_Operation_From_Private_View (S, E);
9254 Set_Is_Dispatching_Operation (S);
9256 -- Common case
9258 else
9259 Enter_Overloaded_Entity (S);
9260 Check_Dispatching_Operation (S, Empty);
9261 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9262 end if;
9264 return;
9265 end if;
9267 -- If there is no homonym then this is definitely not overriding
9269 if No (E) then
9270 Enter_Overloaded_Entity (S);
9271 Check_Dispatching_Operation (S, Empty);
9272 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9274 -- If subprogram has an explicit declaration, check whether it has an
9275 -- overriding indicator.
9277 if Comes_From_Source (S) then
9278 Check_Synchronized_Overriding (S, Overridden_Subp);
9280 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
9281 -- it may have overridden some hidden inherited primitive. Update
9282 -- Overridden_Subp to avoid spurious errors when checking the
9283 -- overriding indicator.
9285 if Ada_Version >= Ada_2012
9286 and then No (Overridden_Subp)
9287 and then Is_Dispatching_Operation (S)
9288 and then Present (Overridden_Operation (S))
9289 then
9290 Overridden_Subp := Overridden_Operation (S);
9291 end if;
9293 Check_Overriding_Indicator
9294 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9295 end if;
9297 -- If there is a homonym that is not overloadable, then we have an
9298 -- error, except for the special cases checked explicitly below.
9300 elsif not Is_Overloadable (E) then
9302 -- Check for spurious conflict produced by a subprogram that has the
9303 -- same name as that of the enclosing generic package. The conflict
9304 -- occurs within an instance, between the subprogram and the renaming
9305 -- declaration for the package. After the subprogram, the package
9306 -- renaming declaration becomes hidden.
9308 if Ekind (E) = E_Package
9309 and then Present (Renamed_Object (E))
9310 and then Renamed_Object (E) = Current_Scope
9311 and then Nkind (Parent (Renamed_Object (E))) =
9312 N_Package_Specification
9313 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
9314 then
9315 Set_Is_Hidden (E);
9316 Set_Is_Immediately_Visible (E, False);
9317 Enter_Overloaded_Entity (S);
9318 Set_Homonym (S, Homonym (E));
9319 Check_Dispatching_Operation (S, Empty);
9320 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
9322 -- If the subprogram is implicit it is hidden by the previous
9323 -- declaration. However if it is dispatching, it must appear in the
9324 -- dispatch table anyway, because it can be dispatched to even if it
9325 -- cannot be called directly.
9327 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
9328 Set_Scope (S, Current_Scope);
9330 if Is_Dispatching_Operation (Alias (S)) then
9331 Check_Dispatching_Operation (S, Empty);
9332 end if;
9334 return;
9336 else
9337 Error_Msg_Sloc := Sloc (E);
9339 -- Generate message, with useful additional warning if in generic
9341 if Is_Generic_Unit (E) then
9342 Error_Msg_N ("previous generic unit cannot be overloaded", S);
9343 Error_Msg_N ("\& conflicts with declaration#", S);
9344 else
9345 Error_Msg_N ("& conflicts with declaration#", S);
9346 end if;
9348 return;
9349 end if;
9351 -- E exists and is overloadable
9353 else
9354 Check_Synchronized_Overriding (S, Overridden_Subp);
9356 -- Loop through E and its homonyms to determine if any of them is
9357 -- the candidate for overriding by S.
9359 while Present (E) loop
9361 -- Definitely not interesting if not in the current scope
9363 if Scope (E) /= Current_Scope then
9364 null;
9366 -- A function can overload the name of an abstract state. The
9367 -- state can be viewed as a function with a profile that cannot
9368 -- be matched by anything.
9370 elsif Ekind (S) = E_Function
9371 and then Ekind (E) = E_Abstract_State
9372 then
9373 Enter_Overloaded_Entity (S);
9374 return;
9376 -- Ada 2012 (AI05-0165): For internally generated bodies of null
9377 -- procedures locate the internally generated spec. We enforce
9378 -- mode conformance since a tagged type may inherit from
9379 -- interfaces several null primitives which differ only in
9380 -- the mode of the formals.
9382 elsif not Comes_From_Source (S)
9383 and then Is_Null_Procedure (S)
9384 and then not Mode_Conformant (E, S)
9385 then
9386 null;
9388 -- Check if we have type conformance
9390 elsif Type_Conformant (E, S) then
9392 -- If the old and new entities have the same profile and one
9393 -- is not the body of the other, then this is an error, unless
9394 -- one of them is implicitly declared.
9396 -- There are some cases when both can be implicit, for example
9397 -- when both a literal and a function that overrides it are
9398 -- inherited in a derivation, or when an inherited operation
9399 -- of a tagged full type overrides the inherited operation of
9400 -- a private extension. Ada 83 had a special rule for the
9401 -- literal case. In Ada 95, the later implicit operation hides
9402 -- the former, and the literal is always the former. In the
9403 -- odd case where both are derived operations declared at the
9404 -- same point, both operations should be declared, and in that
9405 -- case we bypass the following test and proceed to the next
9406 -- part. This can only occur for certain obscure cases in
9407 -- instances, when an operation on a type derived from a formal
9408 -- private type does not override a homograph inherited from
9409 -- the actual. In subsequent derivations of such a type, the
9410 -- DT positions of these operations remain distinct, if they
9411 -- have been set.
9413 if Present (Alias (S))
9414 and then (No (Alias (E))
9415 or else Comes_From_Source (E)
9416 or else Is_Abstract_Subprogram (S)
9417 or else
9418 (Is_Dispatching_Operation (E)
9419 and then Is_Overriding_Alias (E, S)))
9420 and then Ekind (E) /= E_Enumeration_Literal
9421 then
9422 -- When an derived operation is overloaded it may be due to
9423 -- the fact that the full view of a private extension
9424 -- re-inherits. It has to be dealt with.
9426 if Is_Package_Or_Generic_Package (Current_Scope)
9427 and then In_Private_Part (Current_Scope)
9428 then
9429 Check_Operation_From_Private_View (S, E);
9430 end if;
9432 -- In any case the implicit operation remains hidden by the
9433 -- existing declaration, which is overriding. Indicate that
9434 -- E overrides the operation from which S is inherited.
9436 if Present (Alias (S)) then
9437 Set_Overridden_Operation (E, Alias (S));
9438 else
9439 Set_Overridden_Operation (E, S);
9440 end if;
9442 if Comes_From_Source (E) then
9443 Check_Overriding_Indicator (E, S, Is_Primitive => False);
9444 end if;
9446 return;
9448 -- Within an instance, the renaming declarations for actual
9449 -- subprograms may become ambiguous, but they do not hide each
9450 -- other.
9452 elsif Ekind (E) /= E_Entry
9453 and then not Comes_From_Source (E)
9454 and then not Is_Generic_Instance (E)
9455 and then (Present (Alias (E))
9456 or else Is_Intrinsic_Subprogram (E))
9457 and then (not In_Instance
9458 or else No (Parent (E))
9459 or else Nkind (Unit_Declaration_Node (E)) /=
9460 N_Subprogram_Renaming_Declaration)
9461 then
9462 -- A subprogram child unit is not allowed to override an
9463 -- inherited subprogram (10.1.1(20)).
9465 if Is_Child_Unit (S) then
9466 Error_Msg_N
9467 ("child unit overrides inherited subprogram in parent",
9469 return;
9470 end if;
9472 if Is_Non_Overriding_Operation (E, S) then
9473 Enter_Overloaded_Entity (S);
9475 if No (Derived_Type)
9476 or else Is_Tagged_Type (Derived_Type)
9477 then
9478 Check_Dispatching_Operation (S, Empty);
9479 end if;
9481 return;
9482 end if;
9484 -- E is a derived operation or an internal operator which
9485 -- is being overridden. Remove E from further visibility.
9486 -- Furthermore, if E is a dispatching operation, it must be
9487 -- replaced in the list of primitive operations of its type
9488 -- (see Override_Dispatching_Operation).
9490 Overridden_Subp := E;
9492 declare
9493 Prev : Entity_Id;
9495 begin
9496 Prev := First_Entity (Current_Scope);
9497 while Present (Prev) and then Next_Entity (Prev) /= E loop
9498 Next_Entity (Prev);
9499 end loop;
9501 -- It is possible for E to be in the current scope and
9502 -- yet not in the entity chain. This can only occur in a
9503 -- generic context where E is an implicit concatenation
9504 -- in the formal part, because in a generic body the
9505 -- entity chain starts with the formals.
9507 pragma Assert
9508 (Present (Prev) or else Chars (E) = Name_Op_Concat);
9510 -- E must be removed both from the entity_list of the
9511 -- current scope, and from the visibility chain
9513 if Debug_Flag_E then
9514 Write_Str ("Override implicit operation ");
9515 Write_Int (Int (E));
9516 Write_Eol;
9517 end if;
9519 -- If E is a predefined concatenation, it stands for four
9520 -- different operations. As a result, a single explicit
9521 -- declaration does not hide it. In a possible ambiguous
9522 -- situation, Disambiguate chooses the user-defined op,
9523 -- so it is correct to retain the previous internal one.
9525 if Chars (E) /= Name_Op_Concat
9526 or else Ekind (E) /= E_Operator
9527 then
9528 -- For nondispatching derived operations that are
9529 -- overridden by a subprogram declared in the private
9530 -- part of a package, we retain the derived subprogram
9531 -- but mark it as not immediately visible. If the
9532 -- derived operation was declared in the visible part
9533 -- then this ensures that it will still be visible
9534 -- outside the package with the proper signature
9535 -- (calls from outside must also be directed to this
9536 -- version rather than the overriding one, unlike the
9537 -- dispatching case). Calls from inside the package
9538 -- will still resolve to the overriding subprogram
9539 -- since the derived one is marked as not visible
9540 -- within the package.
9542 -- If the private operation is dispatching, we achieve
9543 -- the overriding by keeping the implicit operation
9544 -- but setting its alias to be the overriding one. In
9545 -- this fashion the proper body is executed in all
9546 -- cases, but the original signature is used outside
9547 -- of the package.
9549 -- If the overriding is not in the private part, we
9550 -- remove the implicit operation altogether.
9552 if Is_Private_Declaration (S) then
9553 if not Is_Dispatching_Operation (E) then
9554 Set_Is_Immediately_Visible (E, False);
9555 else
9556 -- Work done in Override_Dispatching_Operation,
9557 -- so nothing else needs to be done here.
9559 null;
9560 end if;
9562 else
9563 -- Find predecessor of E in Homonym chain
9565 if E = Current_Entity (E) then
9566 Prev_Vis := Empty;
9567 else
9568 Prev_Vis := Current_Entity (E);
9569 while Homonym (Prev_Vis) /= E loop
9570 Prev_Vis := Homonym (Prev_Vis);
9571 end loop;
9572 end if;
9574 if Prev_Vis /= Empty then
9576 -- Skip E in the visibility chain
9578 Set_Homonym (Prev_Vis, Homonym (E));
9580 else
9581 Set_Name_Entity_Id (Chars (E), Homonym (E));
9582 end if;
9584 Set_Next_Entity (Prev, Next_Entity (E));
9586 if No (Next_Entity (Prev)) then
9587 Set_Last_Entity (Current_Scope, Prev);
9588 end if;
9589 end if;
9590 end if;
9592 Enter_Overloaded_Entity (S);
9594 -- For entities generated by Derive_Subprograms the
9595 -- overridden operation is the inherited primitive
9596 -- (which is available through the attribute alias).
9598 if not (Comes_From_Source (E))
9599 and then Is_Dispatching_Operation (E)
9600 and then Find_Dispatching_Type (E) =
9601 Find_Dispatching_Type (S)
9602 and then Present (Alias (E))
9603 and then Comes_From_Source (Alias (E))
9604 then
9605 Set_Overridden_Operation (S, Alias (E));
9607 -- Normal case of setting entity as overridden
9609 -- Note: Static_Initialization and Overridden_Operation
9610 -- attributes use the same field in subprogram entities.
9611 -- Static_Initialization is only defined for internal
9612 -- initialization procedures, where Overridden_Operation
9613 -- is irrelevant. Therefore the setting of this attribute
9614 -- must check whether the target is an init_proc.
9616 elsif not Is_Init_Proc (S) then
9617 Set_Overridden_Operation (S, E);
9618 end if;
9620 Check_Overriding_Indicator (S, E, Is_Primitive => True);
9622 -- If S is a user-defined subprogram or a null procedure
9623 -- expanded to override an inherited null procedure, or a
9624 -- predefined dispatching primitive then indicate that E
9625 -- overrides the operation from which S is inherited.
9627 if Comes_From_Source (S)
9628 or else
9629 (Present (Parent (S))
9630 and then
9631 Nkind (Parent (S)) = N_Procedure_Specification
9632 and then
9633 Null_Present (Parent (S)))
9634 or else
9635 (Present (Alias (E))
9636 and then
9637 Is_Predefined_Dispatching_Operation (Alias (E)))
9638 then
9639 if Present (Alias (E)) then
9640 Set_Overridden_Operation (S, Alias (E));
9641 end if;
9642 end if;
9644 if Is_Dispatching_Operation (E) then
9646 -- An overriding dispatching subprogram inherits the
9647 -- convention of the overridden subprogram (AI-117).
9649 Set_Convention (S, Convention (E));
9650 Check_Dispatching_Operation (S, E);
9652 else
9653 Check_Dispatching_Operation (S, Empty);
9654 end if;
9656 Check_For_Primitive_Subprogram
9657 (Is_Primitive_Subp, Is_Overriding => True);
9658 goto Check_Inequality;
9659 end;
9661 -- Apparent redeclarations in instances can occur when two
9662 -- formal types get the same actual type. The subprograms in
9663 -- in the instance are legal, even if not callable from the
9664 -- outside. Calls from within are disambiguated elsewhere.
9665 -- For dispatching operations in the visible part, the usual
9666 -- rules apply, and operations with the same profile are not
9667 -- legal (B830001).
9669 elsif (In_Instance_Visible_Part
9670 and then not Is_Dispatching_Operation (E))
9671 or else In_Instance_Not_Visible
9672 then
9673 null;
9675 -- Here we have a real error (identical profile)
9677 else
9678 Error_Msg_Sloc := Sloc (E);
9680 -- Avoid cascaded errors if the entity appears in
9681 -- subsequent calls.
9683 Set_Scope (S, Current_Scope);
9685 -- Generate error, with extra useful warning for the case
9686 -- of a generic instance with no completion.
9688 if Is_Generic_Instance (S)
9689 and then not Has_Completion (E)
9690 then
9691 Error_Msg_N
9692 ("instantiation cannot provide body for&", S);
9693 Error_Msg_N ("\& conflicts with declaration#", S);
9694 else
9695 Error_Msg_N ("& conflicts with declaration#", S);
9696 end if;
9698 return;
9699 end if;
9701 else
9702 -- If one subprogram has an access parameter and the other
9703 -- a parameter of an access type, calls to either might be
9704 -- ambiguous. Verify that parameters match except for the
9705 -- access parameter.
9707 if May_Hide_Profile then
9708 declare
9709 F1 : Entity_Id;
9710 F2 : Entity_Id;
9712 begin
9713 F1 := First_Formal (S);
9714 F2 := First_Formal (E);
9715 while Present (F1) and then Present (F2) loop
9716 if Is_Access_Type (Etype (F1)) then
9717 if not Is_Access_Type (Etype (F2))
9718 or else not Conforming_Types
9719 (Designated_Type (Etype (F1)),
9720 Designated_Type (Etype (F2)),
9721 Type_Conformant)
9722 then
9723 May_Hide_Profile := False;
9724 end if;
9726 elsif
9727 not Conforming_Types
9728 (Etype (F1), Etype (F2), Type_Conformant)
9729 then
9730 May_Hide_Profile := False;
9731 end if;
9733 Next_Formal (F1);
9734 Next_Formal (F2);
9735 end loop;
9737 if May_Hide_Profile
9738 and then No (F1)
9739 and then No (F2)
9740 then
9741 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
9742 end if;
9743 end;
9744 end if;
9745 end if;
9747 E := Homonym (E);
9748 end loop;
9750 -- On exit, we know that S is a new entity
9752 Enter_Overloaded_Entity (S);
9753 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9754 Check_Overriding_Indicator
9755 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9757 -- Overloading is not allowed in SPARK, except for operators
9759 if Nkind (S) /= N_Defining_Operator_Symbol then
9760 Error_Msg_Sloc := Sloc (Homonym (S));
9761 Check_SPARK_05_Restriction
9762 ("overloading not allowed with entity#", S);
9763 end if;
9765 -- If S is a derived operation for an untagged type then by
9766 -- definition it's not a dispatching operation (even if the parent
9767 -- operation was dispatching), so Check_Dispatching_Operation is not
9768 -- called in that case.
9770 if No (Derived_Type)
9771 or else Is_Tagged_Type (Derived_Type)
9772 then
9773 Check_Dispatching_Operation (S, Empty);
9774 end if;
9775 end if;
9777 -- If this is a user-defined equality operator that is not a derived
9778 -- subprogram, create the corresponding inequality. If the operation is
9779 -- dispatching, the expansion is done elsewhere, and we do not create
9780 -- an explicit inequality operation.
9782 <<Check_Inequality>>
9783 if Chars (S) = Name_Op_Eq
9784 and then Etype (S) = Standard_Boolean
9785 and then Present (Parent (S))
9786 and then not Is_Dispatching_Operation (S)
9787 then
9788 Make_Inequality_Operator (S);
9789 Check_Untagged_Equality (S);
9790 end if;
9791 end New_Overloaded_Entity;
9793 ---------------------
9794 -- Process_Formals --
9795 ---------------------
9797 procedure Process_Formals
9798 (T : List_Id;
9799 Related_Nod : Node_Id)
9801 Param_Spec : Node_Id;
9802 Formal : Entity_Id;
9803 Formal_Type : Entity_Id;
9804 Default : Node_Id;
9805 Ptype : Entity_Id;
9807 Num_Out_Params : Nat := 0;
9808 First_Out_Param : Entity_Id := Empty;
9809 -- Used for setting Is_Only_Out_Parameter
9811 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
9812 -- Determine whether an access type designates a type coming from a
9813 -- limited view.
9815 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
9816 -- Check whether the default has a class-wide type. After analysis the
9817 -- default has the type of the formal, so we must also check explicitly
9818 -- for an access attribute.
9820 ----------------------------------
9821 -- Designates_From_Limited_With --
9822 ----------------------------------
9824 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
9825 Desig : Entity_Id := Typ;
9827 begin
9828 if Is_Access_Type (Desig) then
9829 Desig := Directly_Designated_Type (Desig);
9830 end if;
9832 if Is_Class_Wide_Type (Desig) then
9833 Desig := Root_Type (Desig);
9834 end if;
9836 return
9837 Ekind (Desig) = E_Incomplete_Type
9838 and then From_Limited_With (Desig);
9839 end Designates_From_Limited_With;
9841 ---------------------------
9842 -- Is_Class_Wide_Default --
9843 ---------------------------
9845 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
9846 begin
9847 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
9848 or else (Nkind (D) = N_Attribute_Reference
9849 and then Attribute_Name (D) = Name_Access
9850 and then Is_Class_Wide_Type (Etype (Prefix (D))));
9851 end Is_Class_Wide_Default;
9853 -- Start of processing for Process_Formals
9855 begin
9856 -- In order to prevent premature use of the formals in the same formal
9857 -- part, the Ekind is left undefined until all default expressions are
9858 -- analyzed. The Ekind is established in a separate loop at the end.
9860 Param_Spec := First (T);
9861 while Present (Param_Spec) loop
9862 Formal := Defining_Identifier (Param_Spec);
9863 Set_Never_Set_In_Source (Formal, True);
9864 Enter_Name (Formal);
9866 -- Case of ordinary parameters
9868 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
9869 Find_Type (Parameter_Type (Param_Spec));
9870 Ptype := Parameter_Type (Param_Spec);
9872 if Ptype = Error then
9873 goto Continue;
9874 end if;
9876 Formal_Type := Entity (Ptype);
9878 if Is_Incomplete_Type (Formal_Type)
9879 or else
9880 (Is_Class_Wide_Type (Formal_Type)
9881 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
9882 then
9883 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
9884 -- primitive operations, as long as their completion is
9885 -- in the same declarative part. If in the private part
9886 -- this means that the type cannot be a Taft-amendment type.
9887 -- Check is done on package exit. For access to subprograms,
9888 -- the use is legal for Taft-amendment types.
9890 -- Ada 2012: tagged incomplete types are allowed as generic
9891 -- formal types. They do not introduce dependencies and the
9892 -- corresponding generic subprogram does not have a delayed
9893 -- freeze, because it does not need a freeze node. However,
9894 -- it is still the case that untagged incomplete types cannot
9895 -- be Taft-amendment types and must be completed in private
9896 -- part, so the subprogram must appear in the list of private
9897 -- dependents of the type.
9899 if Is_Tagged_Type (Formal_Type)
9900 or else (Ada_Version >= Ada_2012
9901 and then not From_Limited_With (Formal_Type)
9902 and then not Is_Generic_Type (Formal_Type))
9903 then
9904 if Ekind (Scope (Current_Scope)) = E_Package
9905 and then not Is_Generic_Type (Formal_Type)
9906 and then not Is_Class_Wide_Type (Formal_Type)
9907 then
9908 if not Nkind_In
9909 (Parent (T), N_Access_Function_Definition,
9910 N_Access_Procedure_Definition)
9911 then
9912 Append_Elmt
9913 (Current_Scope,
9914 To => Private_Dependents (Base_Type (Formal_Type)));
9916 -- Freezing is delayed to ensure that Register_Prim
9917 -- will get called for this operation, which is needed
9918 -- in cases where static dispatch tables aren't built.
9919 -- (Note that the same is done for controlling access
9920 -- parameter cases in function Access_Definition.)
9922 Set_Has_Delayed_Freeze (Current_Scope);
9923 end if;
9924 end if;
9926 -- Special handling of Value_Type for CIL case
9928 elsif Is_Value_Type (Formal_Type) then
9929 null;
9931 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
9932 N_Access_Procedure_Definition)
9933 then
9934 -- AI05-0151: Tagged incomplete types are allowed in all
9935 -- formal parts. Untagged incomplete types are not allowed
9936 -- in bodies. Limited views of either kind are not allowed
9937 -- if there is no place at which the non-limited view can
9938 -- become available.
9940 -- Incomplete formal untagged types are not allowed in
9941 -- subprogram bodies (but are legal in their declarations).
9943 if Is_Generic_Type (Formal_Type)
9944 and then not Is_Tagged_Type (Formal_Type)
9945 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
9946 then
9947 Error_Msg_N
9948 ("invalid use of formal incomplete type", Param_Spec);
9950 elsif Ada_Version >= Ada_2012 then
9951 if Is_Tagged_Type (Formal_Type)
9952 and then (not From_Limited_With (Formal_Type)
9953 or else not In_Package_Body)
9954 then
9955 null;
9957 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
9958 N_Accept_Alternative,
9959 N_Entry_Body,
9960 N_Subprogram_Body)
9961 then
9962 Error_Msg_NE
9963 ("invalid use of untagged incomplete type&",
9964 Ptype, Formal_Type);
9965 end if;
9967 else
9968 Error_Msg_NE
9969 ("invalid use of incomplete type&",
9970 Param_Spec, Formal_Type);
9972 -- Further checks on the legality of incomplete types
9973 -- in formal parts are delayed until the freeze point
9974 -- of the enclosing subprogram or access to subprogram.
9975 end if;
9976 end if;
9978 elsif Ekind (Formal_Type) = E_Void then
9979 Error_Msg_NE
9980 ("premature use of&",
9981 Parameter_Type (Param_Spec), Formal_Type);
9982 end if;
9984 -- Ada 2012 (AI-142): Handle aliased parameters
9986 if Ada_Version >= Ada_2012
9987 and then Aliased_Present (Param_Spec)
9988 then
9989 Set_Is_Aliased (Formal);
9990 end if;
9992 -- Ada 2005 (AI-231): Create and decorate an internal subtype
9993 -- declaration corresponding to the null-excluding type of the
9994 -- formal in the enclosing scope. Finally, replace the parameter
9995 -- type of the formal with the internal subtype.
9997 if Ada_Version >= Ada_2005
9998 and then Null_Exclusion_Present (Param_Spec)
9999 then
10000 if not Is_Access_Type (Formal_Type) then
10001 Error_Msg_N
10002 ("`NOT NULL` allowed only for an access type", Param_Spec);
10004 else
10005 if Can_Never_Be_Null (Formal_Type)
10006 and then Comes_From_Source (Related_Nod)
10007 then
10008 Error_Msg_NE
10009 ("`NOT NULL` not allowed (& already excludes null)",
10010 Param_Spec, Formal_Type);
10011 end if;
10013 Formal_Type :=
10014 Create_Null_Excluding_Itype
10015 (T => Formal_Type,
10016 Related_Nod => Related_Nod,
10017 Scope_Id => Scope (Current_Scope));
10019 -- If the designated type of the itype is an itype that is
10020 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10021 -- on the access subtype, to prevent order-of-elaboration
10022 -- issues in the backend.
10024 -- Example:
10025 -- type T is access procedure;
10026 -- procedure Op (O : not null T);
10028 if Is_Itype (Directly_Designated_Type (Formal_Type))
10029 and then
10030 not Is_Frozen (Directly_Designated_Type (Formal_Type))
10031 then
10032 Set_Has_Delayed_Freeze (Formal_Type);
10033 end if;
10034 end if;
10035 end if;
10037 -- An access formal type
10039 else
10040 Formal_Type :=
10041 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
10043 -- No need to continue if we already notified errors
10045 if not Present (Formal_Type) then
10046 return;
10047 end if;
10049 -- Ada 2005 (AI-254)
10051 declare
10052 AD : constant Node_Id :=
10053 Access_To_Subprogram_Definition
10054 (Parameter_Type (Param_Spec));
10055 begin
10056 if Present (AD) and then Protected_Present (AD) then
10057 Formal_Type :=
10058 Replace_Anonymous_Access_To_Protected_Subprogram
10059 (Param_Spec);
10060 end if;
10061 end;
10062 end if;
10064 Set_Etype (Formal, Formal_Type);
10066 -- Deal with default expression if present
10068 Default := Expression (Param_Spec);
10070 if Present (Default) then
10071 Check_SPARK_05_Restriction
10072 ("default expression is not allowed", Default);
10074 if Out_Present (Param_Spec) then
10075 Error_Msg_N
10076 ("default initialization only allowed for IN parameters",
10077 Param_Spec);
10078 end if;
10080 -- Do the special preanalysis of the expression (see section on
10081 -- "Handling of Default Expressions" in the spec of package Sem).
10083 Preanalyze_Spec_Expression (Default, Formal_Type);
10085 -- An access to constant cannot be the default for
10086 -- an access parameter that is an access to variable.
10088 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10089 and then not Is_Access_Constant (Formal_Type)
10090 and then Is_Access_Type (Etype (Default))
10091 and then Is_Access_Constant (Etype (Default))
10092 then
10093 Error_Msg_N
10094 ("formal that is access to variable cannot be initialized "
10095 & "with an access-to-constant expression", Default);
10096 end if;
10098 -- Check that the designated type of an access parameter's default
10099 -- is not a class-wide type unless the parameter's designated type
10100 -- is also class-wide.
10102 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10103 and then not Designates_From_Limited_With (Formal_Type)
10104 and then Is_Class_Wide_Default (Default)
10105 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
10106 then
10107 Error_Msg_N
10108 ("access to class-wide expression not allowed here", Default);
10109 end if;
10111 -- Check incorrect use of dynamically tagged expressions
10113 if Is_Tagged_Type (Formal_Type) then
10114 Check_Dynamically_Tagged_Expression
10115 (Expr => Default,
10116 Typ => Formal_Type,
10117 Related_Nod => Default);
10118 end if;
10119 end if;
10121 -- Ada 2005 (AI-231): Static checks
10123 if Ada_Version >= Ada_2005
10124 and then Is_Access_Type (Etype (Formal))
10125 and then Can_Never_Be_Null (Etype (Formal))
10126 then
10127 Null_Exclusion_Static_Checks (Param_Spec);
10128 end if;
10130 -- The following checks are relevant when SPARK_Mode is on as these
10131 -- are not standard Ada legality rules.
10133 if SPARK_Mode = On then
10134 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
10136 -- A function cannot have a parameter of mode IN OUT or OUT
10137 -- (SPARK RM 6.1).
10139 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
10140 Error_Msg_N
10141 ("function cannot have parameter of mode `OUT` or "
10142 & "`IN OUT`", Formal);
10144 -- A function cannot have an effectively volatile formal
10145 -- parameter (SPARK RM 7.1.3(10)).
10147 elsif Is_Effectively_Volatile (Formal) then
10148 Error_Msg_N
10149 ("function cannot have a volatile formal parameter",
10150 Formal);
10151 end if;
10153 -- A procedure cannot have an effectively volatile formal
10154 -- parameter of mode IN because it behaves as a constant
10155 -- (SPARK RM 7.1.3(6)).
10157 elsif Ekind (Scope (Formal)) = E_Procedure
10158 and then Ekind (Formal) = E_In_Parameter
10159 and then Is_Effectively_Volatile (Formal)
10160 then
10161 Error_Msg_N
10162 ("formal parameter of mode `IN` cannot be volatile", Formal);
10163 end if;
10164 end if;
10166 <<Continue>>
10167 Next (Param_Spec);
10168 end loop;
10170 -- If this is the formal part of a function specification, analyze the
10171 -- subtype mark in the context where the formals are visible but not
10172 -- yet usable, and may hide outer homographs.
10174 if Nkind (Related_Nod) = N_Function_Specification then
10175 Analyze_Return_Type (Related_Nod);
10176 end if;
10178 -- Now set the kind (mode) of each formal
10180 Param_Spec := First (T);
10181 while Present (Param_Spec) loop
10182 Formal := Defining_Identifier (Param_Spec);
10183 Set_Formal_Mode (Formal);
10185 if Ekind (Formal) = E_In_Parameter then
10186 Set_Default_Value (Formal, Expression (Param_Spec));
10188 if Present (Expression (Param_Spec)) then
10189 Default := Expression (Param_Spec);
10191 if Is_Scalar_Type (Etype (Default)) then
10192 if Nkind (Parameter_Type (Param_Spec)) /=
10193 N_Access_Definition
10194 then
10195 Formal_Type := Entity (Parameter_Type (Param_Spec));
10196 else
10197 Formal_Type :=
10198 Access_Definition
10199 (Related_Nod, Parameter_Type (Param_Spec));
10200 end if;
10202 Apply_Scalar_Range_Check (Default, Formal_Type);
10203 end if;
10204 end if;
10206 elsif Ekind (Formal) = E_Out_Parameter then
10207 Num_Out_Params := Num_Out_Params + 1;
10209 if Num_Out_Params = 1 then
10210 First_Out_Param := Formal;
10211 end if;
10213 elsif Ekind (Formal) = E_In_Out_Parameter then
10214 Num_Out_Params := Num_Out_Params + 1;
10215 end if;
10217 -- Skip remaining processing if formal type was in error
10219 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
10220 goto Next_Parameter;
10221 end if;
10223 -- Force call by reference if aliased
10225 if Is_Aliased (Formal) then
10226 Set_Mechanism (Formal, By_Reference);
10228 -- Warn if user asked this to be passed by copy
10230 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10231 Error_Msg_N
10232 ("cannot pass aliased parameter & by copy??", Formal);
10233 end if;
10235 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
10237 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10238 Set_Mechanism (Formal, By_Copy);
10240 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
10241 Set_Mechanism (Formal, By_Reference);
10242 end if;
10244 <<Next_Parameter>>
10245 Next (Param_Spec);
10246 end loop;
10248 if Present (First_Out_Param) and then Num_Out_Params = 1 then
10249 Set_Is_Only_Out_Parameter (First_Out_Param);
10250 end if;
10251 end Process_Formals;
10253 ----------------------------
10254 -- Reference_Body_Formals --
10255 ----------------------------
10257 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
10258 Fs : Entity_Id;
10259 Fb : Entity_Id;
10261 begin
10262 if Error_Posted (Spec) then
10263 return;
10264 end if;
10266 -- Iterate over both lists. They may be of different lengths if the two
10267 -- specs are not conformant.
10269 Fs := First_Formal (Spec);
10270 Fb := First_Formal (Bod);
10271 while Present (Fs) and then Present (Fb) loop
10272 Generate_Reference (Fs, Fb, 'b');
10274 if Style_Check then
10275 Style.Check_Identifier (Fb, Fs);
10276 end if;
10278 Set_Spec_Entity (Fb, Fs);
10279 Set_Referenced (Fs, False);
10280 Next_Formal (Fs);
10281 Next_Formal (Fb);
10282 end loop;
10283 end Reference_Body_Formals;
10285 -------------------------
10286 -- Set_Actual_Subtypes --
10287 -------------------------
10289 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
10290 Decl : Node_Id;
10291 Formal : Entity_Id;
10292 T : Entity_Id;
10293 First_Stmt : Node_Id := Empty;
10294 AS_Needed : Boolean;
10296 begin
10297 -- If this is an empty initialization procedure, no need to create
10298 -- actual subtypes (small optimization).
10300 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
10301 return;
10302 end if;
10304 Formal := First_Formal (Subp);
10305 while Present (Formal) loop
10306 T := Etype (Formal);
10308 -- We never need an actual subtype for a constrained formal
10310 if Is_Constrained (T) then
10311 AS_Needed := False;
10313 -- If we have unknown discriminants, then we do not need an actual
10314 -- subtype, or more accurately we cannot figure it out. Note that
10315 -- all class-wide types have unknown discriminants.
10317 elsif Has_Unknown_Discriminants (T) then
10318 AS_Needed := False;
10320 -- At this stage we have an unconstrained type that may need an
10321 -- actual subtype. For sure the actual subtype is needed if we have
10322 -- an unconstrained array type.
10324 elsif Is_Array_Type (T) then
10325 AS_Needed := True;
10327 -- The only other case needing an actual subtype is an unconstrained
10328 -- record type which is an IN parameter (we cannot generate actual
10329 -- subtypes for the OUT or IN OUT case, since an assignment can
10330 -- change the discriminant values. However we exclude the case of
10331 -- initialization procedures, since discriminants are handled very
10332 -- specially in this context, see the section entitled "Handling of
10333 -- Discriminants" in Einfo.
10335 -- We also exclude the case of Discrim_SO_Functions (functions used
10336 -- in front end layout mode for size/offset values), since in such
10337 -- functions only discriminants are referenced, and not only are such
10338 -- subtypes not needed, but they cannot always be generated, because
10339 -- of order of elaboration issues.
10341 elsif Is_Record_Type (T)
10342 and then Ekind (Formal) = E_In_Parameter
10343 and then Chars (Formal) /= Name_uInit
10344 and then not Is_Unchecked_Union (T)
10345 and then not Is_Discrim_SO_Function (Subp)
10346 then
10347 AS_Needed := True;
10349 -- All other cases do not need an actual subtype
10351 else
10352 AS_Needed := False;
10353 end if;
10355 -- Generate actual subtypes for unconstrained arrays and
10356 -- unconstrained discriminated records.
10358 if AS_Needed then
10359 if Nkind (N) = N_Accept_Statement then
10361 -- If expansion is active, the formal is replaced by a local
10362 -- variable that renames the corresponding entry of the
10363 -- parameter block, and it is this local variable that may
10364 -- require an actual subtype.
10366 if Expander_Active then
10367 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
10368 else
10369 Decl := Build_Actual_Subtype (T, Formal);
10370 end if;
10372 if Present (Handled_Statement_Sequence (N)) then
10373 First_Stmt :=
10374 First (Statements (Handled_Statement_Sequence (N)));
10375 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
10376 Mark_Rewrite_Insertion (Decl);
10377 else
10378 -- If the accept statement has no body, there will be no
10379 -- reference to the actuals, so no need to compute actual
10380 -- subtypes.
10382 return;
10383 end if;
10385 else
10386 Decl := Build_Actual_Subtype (T, Formal);
10387 Prepend (Decl, Declarations (N));
10388 Mark_Rewrite_Insertion (Decl);
10389 end if;
10391 -- The declaration uses the bounds of an existing object, and
10392 -- therefore needs no constraint checks.
10394 Analyze (Decl, Suppress => All_Checks);
10396 -- We need to freeze manually the generated type when it is
10397 -- inserted anywhere else than in a declarative part.
10399 if Present (First_Stmt) then
10400 Insert_List_Before_And_Analyze (First_Stmt,
10401 Freeze_Entity (Defining_Identifier (Decl), N));
10403 -- Ditto if the type has a dynamic predicate, because the
10404 -- generated function will mention the actual subtype.
10406 elsif Has_Dynamic_Predicate_Aspect (T) then
10407 Insert_List_Before_And_Analyze (Decl,
10408 Freeze_Entity (Defining_Identifier (Decl), N));
10409 end if;
10411 if Nkind (N) = N_Accept_Statement
10412 and then Expander_Active
10413 then
10414 Set_Actual_Subtype (Renamed_Object (Formal),
10415 Defining_Identifier (Decl));
10416 else
10417 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
10418 end if;
10419 end if;
10421 Next_Formal (Formal);
10422 end loop;
10423 end Set_Actual_Subtypes;
10425 ---------------------
10426 -- Set_Formal_Mode --
10427 ---------------------
10429 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
10430 Spec : constant Node_Id := Parent (Formal_Id);
10432 begin
10433 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
10434 -- since we ensure that corresponding actuals are always valid at the
10435 -- point of the call.
10437 if Out_Present (Spec) then
10438 if Ekind_In (Scope (Formal_Id), E_Function, E_Generic_Function) then
10440 -- [IN] OUT parameters allowed for functions in Ada 2012
10442 if Ada_Version >= Ada_2012 then
10444 -- Even in Ada 2012 operators can only have IN parameters
10446 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
10447 Error_Msg_N ("operators can only have IN parameters", Spec);
10448 end if;
10450 if In_Present (Spec) then
10451 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10452 else
10453 Set_Ekind (Formal_Id, E_Out_Parameter);
10454 end if;
10456 Set_Has_Out_Or_In_Out_Parameter (Scope (Formal_Id), True);
10458 -- But not in earlier versions of Ada
10460 else
10461 Error_Msg_N ("functions can only have IN parameters", Spec);
10462 Set_Ekind (Formal_Id, E_In_Parameter);
10463 end if;
10465 elsif In_Present (Spec) then
10466 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10468 else
10469 Set_Ekind (Formal_Id, E_Out_Parameter);
10470 Set_Never_Set_In_Source (Formal_Id, True);
10471 Set_Is_True_Constant (Formal_Id, False);
10472 Set_Current_Value (Formal_Id, Empty);
10473 end if;
10475 else
10476 Set_Ekind (Formal_Id, E_In_Parameter);
10477 end if;
10479 -- Set Is_Known_Non_Null for access parameters since the language
10480 -- guarantees that access parameters are always non-null. We also set
10481 -- Can_Never_Be_Null, since there is no way to change the value.
10483 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
10485 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
10486 -- null; In Ada 2005, only if then null_exclusion is explicit.
10488 if Ada_Version < Ada_2005
10489 or else Can_Never_Be_Null (Etype (Formal_Id))
10490 then
10491 Set_Is_Known_Non_Null (Formal_Id);
10492 Set_Can_Never_Be_Null (Formal_Id);
10493 end if;
10495 -- Ada 2005 (AI-231): Null-exclusion access subtype
10497 elsif Is_Access_Type (Etype (Formal_Id))
10498 and then Can_Never_Be_Null (Etype (Formal_Id))
10499 then
10500 Set_Is_Known_Non_Null (Formal_Id);
10502 -- We can also set Can_Never_Be_Null (thus preventing some junk
10503 -- access checks) for the case of an IN parameter, which cannot
10504 -- be changed, or for an IN OUT parameter, which can be changed but
10505 -- not to a null value. But for an OUT parameter, the initial value
10506 -- passed in can be null, so we can't set this flag in that case.
10508 if Ekind (Formal_Id) /= E_Out_Parameter then
10509 Set_Can_Never_Be_Null (Formal_Id);
10510 end if;
10511 end if;
10513 Set_Mechanism (Formal_Id, Default_Mechanism);
10514 Set_Formal_Validity (Formal_Id);
10515 end Set_Formal_Mode;
10517 -------------------------
10518 -- Set_Formal_Validity --
10519 -------------------------
10521 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
10522 begin
10523 -- If no validity checking, then we cannot assume anything about the
10524 -- validity of parameters, since we do not know there is any checking
10525 -- of the validity on the call side.
10527 if not Validity_Checks_On then
10528 return;
10530 -- If validity checking for parameters is enabled, this means we are
10531 -- not supposed to make any assumptions about argument values.
10533 elsif Validity_Check_Parameters then
10534 return;
10536 -- If we are checking in parameters, we will assume that the caller is
10537 -- also checking parameters, so we can assume the parameter is valid.
10539 elsif Ekind (Formal_Id) = E_In_Parameter
10540 and then Validity_Check_In_Params
10541 then
10542 Set_Is_Known_Valid (Formal_Id, True);
10544 -- Similar treatment for IN OUT parameters
10546 elsif Ekind (Formal_Id) = E_In_Out_Parameter
10547 and then Validity_Check_In_Out_Params
10548 then
10549 Set_Is_Known_Valid (Formal_Id, True);
10550 end if;
10551 end Set_Formal_Validity;
10553 ------------------------
10554 -- Subtype_Conformant --
10555 ------------------------
10557 function Subtype_Conformant
10558 (New_Id : Entity_Id;
10559 Old_Id : Entity_Id;
10560 Skip_Controlling_Formals : Boolean := False) return Boolean
10562 Result : Boolean;
10563 begin
10564 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
10565 Skip_Controlling_Formals => Skip_Controlling_Formals);
10566 return Result;
10567 end Subtype_Conformant;
10569 ---------------------
10570 -- Type_Conformant --
10571 ---------------------
10573 function Type_Conformant
10574 (New_Id : Entity_Id;
10575 Old_Id : Entity_Id;
10576 Skip_Controlling_Formals : Boolean := False) return Boolean
10578 Result : Boolean;
10579 begin
10580 May_Hide_Profile := False;
10581 Check_Conformance
10582 (New_Id, Old_Id, Type_Conformant, False, Result,
10583 Skip_Controlling_Formals => Skip_Controlling_Formals);
10584 return Result;
10585 end Type_Conformant;
10587 -------------------------------
10588 -- Valid_Operator_Definition --
10589 -------------------------------
10591 procedure Valid_Operator_Definition (Designator : Entity_Id) is
10592 N : Integer := 0;
10593 F : Entity_Id;
10594 Id : constant Name_Id := Chars (Designator);
10595 N_OK : Boolean;
10597 begin
10598 F := First_Formal (Designator);
10599 while Present (F) loop
10600 N := N + 1;
10602 if Present (Default_Value (F)) then
10603 Error_Msg_N
10604 ("default values not allowed for operator parameters",
10605 Parent (F));
10607 -- For function instantiations that are operators, we must check
10608 -- separately that the corresponding generic only has in-parameters.
10609 -- For subprogram declarations this is done in Set_Formal_Mode. Such
10610 -- an error could not arise in earlier versions of the language.
10612 elsif Ekind (F) /= E_In_Parameter then
10613 Error_Msg_N ("operators can only have IN parameters", F);
10614 end if;
10616 Next_Formal (F);
10617 end loop;
10619 -- Verify that user-defined operators have proper number of arguments
10620 -- First case of operators which can only be unary
10622 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
10623 N_OK := (N = 1);
10625 -- Case of operators which can be unary or binary
10627 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
10628 N_OK := (N in 1 .. 2);
10630 -- All other operators can only be binary
10632 else
10633 N_OK := (N = 2);
10634 end if;
10636 if not N_OK then
10637 Error_Msg_N
10638 ("incorrect number of arguments for operator", Designator);
10639 end if;
10641 if Id = Name_Op_Ne
10642 and then Base_Type (Etype (Designator)) = Standard_Boolean
10643 and then not Is_Intrinsic_Subprogram (Designator)
10644 then
10645 Error_Msg_N
10646 ("explicit definition of inequality not allowed", Designator);
10647 end if;
10648 end Valid_Operator_Definition;
10650 end Sem_Ch6;