[multiple changes]
[gcc.git] / gcc / ada / sem_ch6.adb
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 ------------------------------------------------------------------------------
25
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 Itypes; use Itypes;
44 with Lib.Xref; use Lib.Xref;
45 with Layout; use Layout;
46 with Namet; use Namet;
47 with Lib; use Lib;
48 with Nlists; use Nlists;
49 with Nmake; use Nmake;
50 with Opt; use Opt;
51 with Output; use Output;
52 with Restrict; use Restrict;
53 with Rident; use Rident;
54 with Rtsfind; use Rtsfind;
55 with Sem; use Sem;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch3; use Sem_Ch3;
59 with Sem_Ch4; use Sem_Ch4;
60 with Sem_Ch5; use Sem_Ch5;
61 with Sem_Ch8; use Sem_Ch8;
62 with Sem_Ch10; use Sem_Ch10;
63 with Sem_Ch12; use Sem_Ch12;
64 with Sem_Ch13; use Sem_Ch13;
65 with Sem_Dim; use Sem_Dim;
66 with Sem_Disp; use Sem_Disp;
67 with Sem_Dist; use Sem_Dist;
68 with Sem_Elim; use Sem_Elim;
69 with Sem_Eval; use Sem_Eval;
70 with Sem_Mech; use Sem_Mech;
71 with Sem_Prag; use Sem_Prag;
72 with Sem_Res; use Sem_Res;
73 with Sem_Util; use Sem_Util;
74 with Sem_Type; use Sem_Type;
75 with Sem_Warn; use Sem_Warn;
76 with Sinput; use Sinput;
77 with Stand; use Stand;
78 with Sinfo; use Sinfo;
79 with Sinfo.CN; use Sinfo.CN;
80 with Snames; use Snames;
81 with Stringt; use Stringt;
82 with Style;
83 with Stylesw; use Stylesw;
84 with Targparm; use Targparm;
85 with Tbuild; use Tbuild;
86 with Uintp; use Uintp;
87 with Urealp; use Urealp;
88 with Validsw; use Validsw;
89
90 package body Sem_Ch6 is
91
92 May_Hide_Profile : Boolean := False;
93 -- This flag is used to indicate that two formals in two subprograms being
94 -- checked for conformance differ only in that one is an access parameter
95 -- while the other is of a general access type with the same designated
96 -- type. In this case, if the rest of the signatures match, a call to
97 -- either subprogram may be ambiguous, which is worth a warning. The flag
98 -- is set in Compatible_Types, and the warning emitted in
99 -- New_Overloaded_Entity.
100
101 -----------------------
102 -- Local Subprograms --
103 -----------------------
104
105 procedure Analyze_Null_Procedure
106 (N : Node_Id;
107 Is_Completion : out Boolean);
108 -- A null procedure can be a declaration or (Ada 2012) a completion.
109
110 procedure Analyze_Return_Statement (N : Node_Id);
111 -- Common processing for simple and extended return statements
112
113 procedure Analyze_Function_Return (N : Node_Id);
114 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
115 -- applies to a [generic] function.
116
117 procedure Analyze_Return_Type (N : Node_Id);
118 -- Subsidiary to Process_Formals: analyze subtype mark in function
119 -- specification in a context where the formals are visible and hide
120 -- outer homographs.
121
122 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
123 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
124 -- that we can use RETURN but not skip the debug output at the end.
125
126 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
127 -- Analyze a generic subprogram body. N is the body to be analyzed, and
128 -- Gen_Id is the defining entity Id for the corresponding spec.
129
130 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
131 -- If a subprogram has pragma Inline and inlining is active, use generic
132 -- machinery to build an unexpanded body for the subprogram. This body is
133 -- subsequently used for inline expansions at call sites. If subprogram can
134 -- be inlined (depending on size and nature of local declarations) this
135 -- function returns true. Otherwise subprogram body is treated normally.
136 -- If proper warnings are enabled and the subprogram contains a construct
137 -- that cannot be inlined, the offending construct is flagged accordingly.
138
139 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
140 -- Returns true if Subp can override a predefined operator.
141
142 procedure Check_And_Build_Body_To_Inline
143 (N : Node_Id;
144 Spec_Id : Entity_Id;
145 Body_Id : Entity_Id);
146 -- Spec_Id and Body_Id are the entities of the specification and body of
147 -- the subprogram body N. If N can be inlined by the frontend (supported
148 -- cases documented in Check_Body_To_Inline) then build the body-to-inline
149 -- associated with N and attach it to the declaration node of Spec_Id.
150
151 procedure Check_Conformance
152 (New_Id : Entity_Id;
153 Old_Id : Entity_Id;
154 Ctype : Conformance_Type;
155 Errmsg : Boolean;
156 Conforms : out Boolean;
157 Err_Loc : Node_Id := Empty;
158 Get_Inst : Boolean := False;
159 Skip_Controlling_Formals : Boolean := False);
160 -- Given two entities, this procedure checks that the profiles associated
161 -- with these entities meet the conformance criterion given by the third
162 -- parameter. If they conform, Conforms is set True and control returns
163 -- to the caller. If they do not conform, Conforms is set to False, and
164 -- in addition, if Errmsg is True on the call, proper messages are output
165 -- to complain about the conformance failure. If Err_Loc is non_Empty
166 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
167 -- error messages are placed on the appropriate part of the construct
168 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
169 -- against a formal access-to-subprogram type so Get_Instance_Of must
170 -- be called.
171
172 procedure Check_Subprogram_Order (N : Node_Id);
173 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
174 -- the alpha ordering rule for N if this ordering requirement applicable.
175
176 procedure Check_Returns
177 (HSS : Node_Id;
178 Mode : Character;
179 Err : out Boolean;
180 Proc : Entity_Id := Empty);
181 -- Called to check for missing return statements in a function body, or for
182 -- returns present in a procedure body which has No_Return set. HSS is the
183 -- handled statement sequence for the subprogram body. This procedure
184 -- checks all flow paths to make sure they either have return (Mode = 'F',
185 -- used for functions) or do not have a return (Mode = 'P', used for
186 -- No_Return procedures). The flag Err is set if there are any control
187 -- paths not explicitly terminated by a return in the function case, and is
188 -- True otherwise. Proc is the entity for the procedure case and is used
189 -- in posting the warning message.
190
191 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
192 -- In Ada 2012, a primitive equality operator on an untagged record type
193 -- must appear before the type is frozen, and have the same visibility as
194 -- that of the type. This procedure checks that this rule is met, and
195 -- otherwise emits an error on the subprogram declaration and a warning
196 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
197 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
198 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
199 -- is set, otherwise the call has no effect.
200
201 procedure Enter_Overloaded_Entity (S : Entity_Id);
202 -- This procedure makes S, a new overloaded entity, into the first visible
203 -- entity with that name.
204
205 function Is_Non_Overriding_Operation
206 (Prev_E : Entity_Id;
207 New_E : Entity_Id) return Boolean;
208 -- Enforce the rule given in 12.3(18): a private operation in an instance
209 -- overrides an inherited operation only if the corresponding operation
210 -- was overriding in the generic. This needs to be checked for primitive
211 -- operations of types derived (in the generic unit) from formal private
212 -- or formal derived types.
213
214 procedure Make_Inequality_Operator (S : Entity_Id);
215 -- Create the declaration for an inequality operator that is implicitly
216 -- created by a user-defined equality operator that yields a boolean.
217
218 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
219 -- Formal_Id is an formal parameter entity. This procedure deals with
220 -- setting the proper validity status for this entity, which depends on
221 -- the kind of parameter and the validity checking mode.
222
223 ---------------------------------------------
224 -- Analyze_Abstract_Subprogram_Declaration --
225 ---------------------------------------------
226
227 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
228 Designator : constant Entity_Id :=
229 Analyze_Subprogram_Specification (Specification (N));
230 Scop : constant Entity_Id := Current_Scope;
231
232 begin
233 Check_SPARK_Restriction ("abstract subprogram is not allowed", N);
234
235 Generate_Definition (Designator);
236 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
237 Set_Is_Abstract_Subprogram (Designator);
238 New_Overloaded_Entity (Designator);
239 Check_Delayed_Subprogram (Designator);
240
241 Set_Categorization_From_Scope (Designator, Scop);
242
243 if Ekind (Scope (Designator)) = E_Protected_Type then
244 Error_Msg_N
245 ("abstract subprogram not allowed in protected type", N);
246
247 -- Issue a warning if the abstract subprogram is neither a dispatching
248 -- operation nor an operation that overrides an inherited subprogram or
249 -- predefined operator, since this most likely indicates a mistake.
250
251 elsif Warn_On_Redundant_Constructs
252 and then not Is_Dispatching_Operation (Designator)
253 and then not Present (Overridden_Operation (Designator))
254 and then (not Is_Operator_Symbol_Name (Chars (Designator))
255 or else Scop /= Scope (Etype (First_Formal (Designator))))
256 then
257 Error_Msg_N
258 ("abstract subprogram is not dispatching or overriding?r?", N);
259 end if;
260
261 Generate_Reference_To_Formals (Designator);
262 Check_Eliminated (Designator);
263
264 if Has_Aspects (N) then
265 Analyze_Aspect_Specifications (N, Designator);
266 end if;
267 end Analyze_Abstract_Subprogram_Declaration;
268
269 ---------------------------------
270 -- Analyze_Expression_Function --
271 ---------------------------------
272
273 procedure Analyze_Expression_Function (N : Node_Id) is
274 Loc : constant Source_Ptr := Sloc (N);
275 LocX : constant Source_Ptr := Sloc (Expression (N));
276 Expr : constant Node_Id := Expression (N);
277 Spec : constant Node_Id := Specification (N);
278
279 Def_Id : Entity_Id;
280
281 Prev : Entity_Id;
282 -- If the expression is a completion, Prev is the entity whose
283 -- declaration is completed. Def_Id is needed to analyze the spec.
284
285 New_Body : Node_Id;
286 New_Decl : Node_Id;
287 New_Spec : Node_Id;
288 Ret : Node_Id;
289
290 begin
291 -- This is one of the occasions on which we transform the tree during
292 -- semantic analysis. If this is a completion, transform the expression
293 -- function into an equivalent subprogram body, and analyze it.
294
295 -- Expression functions are inlined unconditionally. The back-end will
296 -- determine whether this is possible.
297
298 Inline_Processing_Required := True;
299
300 -- Create a specification for the generated body. Types and defauts in
301 -- the profile are copies of the spec, but new entities must be created
302 -- for the unit name and the formals.
303
304 New_Spec := New_Copy_Tree (Spec);
305 Set_Defining_Unit_Name (New_Spec,
306 Make_Defining_Identifier (Sloc (Defining_Unit_Name (Spec)),
307 Chars (Defining_Unit_Name (Spec))));
308
309 if Present (Parameter_Specifications (New_Spec)) then
310 declare
311 Formal_Spec : Node_Id;
312 Def : Entity_Id;
313
314 begin
315 Formal_Spec := First (Parameter_Specifications (New_Spec));
316
317 -- Create a new formal parameter at the same source position
318
319 while Present (Formal_Spec) loop
320 Def := Defining_Identifier (Formal_Spec);
321 Set_Defining_Identifier (Formal_Spec,
322 Make_Defining_Identifier (Sloc (Def),
323 Chars => Chars (Def)));
324 Next (Formal_Spec);
325 end loop;
326 end;
327 end if;
328
329 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
330
331 -- If there are previous overloadable entities with the same name,
332 -- check whether any of them is completed by the expression function.
333
334 if Present (Prev) and then Is_Overloadable (Prev) then
335 Def_Id := Analyze_Subprogram_Specification (Spec);
336 Prev := Find_Corresponding_Spec (N);
337 end if;
338
339 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
340
341 New_Body :=
342 Make_Subprogram_Body (Loc,
343 Specification => New_Spec,
344 Declarations => Empty_List,
345 Handled_Statement_Sequence =>
346 Make_Handled_Sequence_Of_Statements (LocX,
347 Statements => New_List (Ret)));
348
349 -- If the expression completes a generic subprogram, we must create a
350 -- separate node for the body, because at instantiation the original
351 -- node of the generic copy must be a generic subprogram body, and
352 -- cannot be a expression function. Otherwise we just rewrite the
353 -- expression with the non-generic body.
354
355 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
356 Insert_After (N, New_Body);
357
358 -- Propagate any aspects or pragmas that apply to the expression
359 -- function to the proper body when the expression function acts
360 -- as a completion.
361
362 if Has_Aspects (N) then
363 Move_Aspects (N, To => New_Body);
364 end if;
365
366 Relocate_Pragmas_To_Body (New_Body);
367
368 Rewrite (N, Make_Null_Statement (Loc));
369 Set_Has_Completion (Prev, False);
370 Analyze (N);
371 Analyze (New_Body);
372 Set_Is_Inlined (Prev);
373
374 elsif Present (Prev) and then Comes_From_Source (Prev) then
375 Set_Has_Completion (Prev, False);
376
377 -- An expression function that is a completion freezes the
378 -- expression. This means freezing the return type, and if it is
379 -- an access type, freezing its designated type as well.
380
381 -- Note that we cannot defer this freezing to the analysis of the
382 -- expression itself, because a freeze node might appear in a nested
383 -- scope, leading to an elaboration order issue in gigi.
384
385 Freeze_Before (N, Etype (Prev));
386
387 if Is_Access_Type (Etype (Prev)) then
388 Freeze_Before (N, Designated_Type (Etype (Prev)));
389 end if;
390
391 -- For navigation purposes, indicate that the function is a body
392
393 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
394 Rewrite (N, New_Body);
395
396 -- Correct the parent pointer of the aspect specification list to
397 -- reference the rewritten node.
398
399 if Has_Aspects (N) then
400 Set_Parent (Aspect_Specifications (N), N);
401 end if;
402
403 -- Propagate any pragmas that apply to the expression function to the
404 -- proper body when the expression function acts as a completion.
405 -- Aspects are automatically transfered because of node rewriting.
406
407 Relocate_Pragmas_To_Body (N);
408 Analyze (N);
409
410 -- Prev is the previous entity with the same name, but it is can
411 -- be an unrelated spec that is not completed by the expression
412 -- function. In that case the relevant entity is the one in the body.
413 -- Not clear that the backend can inline it in this case ???
414
415 if Has_Completion (Prev) then
416 Set_Is_Inlined (Prev);
417
418 -- The formals of the expression function are body formals,
419 -- and do not appear in the ali file, which will only contain
420 -- references to the formals of the original subprogram spec.
421
422 declare
423 F1 : Entity_Id;
424 F2 : Entity_Id;
425
426 begin
427 F1 := First_Formal (Def_Id);
428 F2 := First_Formal (Prev);
429
430 while Present (F1) loop
431 Set_Spec_Entity (F1, F2);
432 Next_Formal (F1);
433 Next_Formal (F2);
434 end loop;
435 end;
436
437 else
438 Set_Is_Inlined (Defining_Entity (New_Body));
439 end if;
440
441 -- If this is not a completion, create both a declaration and a body, so
442 -- that the expression can be inlined whenever possible.
443
444 else
445 -- An expression function that is not a completion is not a
446 -- subprogram declaration, and thus cannot appear in a protected
447 -- definition.
448
449 if Nkind (Parent (N)) = N_Protected_Definition then
450 Error_Msg_N
451 ("an expression function is not a legal protected operation", N);
452 end if;
453
454 New_Decl :=
455 Make_Subprogram_Declaration (Loc, Specification => Spec);
456
457 Rewrite (N, New_Decl);
458
459 -- Correct the parent pointer of the aspect specification list to
460 -- reference the rewritten node.
461
462 if Has_Aspects (N) then
463 Set_Parent (Aspect_Specifications (N), N);
464 end if;
465
466 Analyze (N);
467 Set_Is_Inlined (Defining_Entity (New_Decl));
468
469 -- To prevent premature freeze action, insert the new body at the end
470 -- of the current declarations, or at the end of the package spec.
471 -- However, resolve usage names now, to prevent spurious visibility
472 -- on later entities. Note that the function can now be called in
473 -- the current declarative part, which will appear to be prior to
474 -- the presence of the body in the code. There are nevertheless no
475 -- order of elaboration issues because all name resolution has taken
476 -- place at the point of declaration.
477
478 declare
479 Decls : List_Id := List_Containing (N);
480 Par : constant Node_Id := Parent (Decls);
481 Id : constant Entity_Id := Defining_Entity (New_Decl);
482
483 begin
484 if Nkind (Par) = N_Package_Specification
485 and then Decls = Visible_Declarations (Par)
486 and then Present (Private_Declarations (Par))
487 and then not Is_Empty_List (Private_Declarations (Par))
488 then
489 Decls := Private_Declarations (Par);
490 end if;
491
492 Insert_After (Last (Decls), New_Body);
493 Push_Scope (Id);
494 Install_Formals (Id);
495
496 -- Preanalyze the expression for name capture, except in an
497 -- instance, where this has been done during generic analysis,
498 -- and will be redone when analyzing the body.
499
500 declare
501 Expr : constant Node_Id := Expression (Ret);
502
503 begin
504 Set_Parent (Expr, Ret);
505
506 if not In_Instance then
507 Preanalyze_Spec_Expression (Expr, Etype (Id));
508 end if;
509 end;
510
511 End_Scope;
512 end;
513 end if;
514
515 -- If the return expression is a static constant, we suppress warning
516 -- messages on unused formals, which in most cases will be noise.
517
518 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
519 Is_OK_Static_Expression (Expr));
520 end Analyze_Expression_Function;
521
522 ----------------------------------------
523 -- Analyze_Extended_Return_Statement --
524 ----------------------------------------
525
526 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
527 begin
528 Analyze_Return_Statement (N);
529 end Analyze_Extended_Return_Statement;
530
531 ----------------------------
532 -- Analyze_Function_Call --
533 ----------------------------
534
535 procedure Analyze_Function_Call (N : Node_Id) is
536 Actuals : constant List_Id := Parameter_Associations (N);
537 Func_Nam : constant Node_Id := Name (N);
538 Actual : Node_Id;
539
540 begin
541 Analyze (Func_Nam);
542
543 -- A call of the form A.B (X) may be an Ada 2005 call, which is
544 -- rewritten as B (A, X). If the rewriting is successful, the call
545 -- has been analyzed and we just return.
546
547 if Nkind (Func_Nam) = N_Selected_Component
548 and then Name (N) /= Func_Nam
549 and then Is_Rewrite_Substitution (N)
550 and then Present (Etype (N))
551 then
552 return;
553 end if;
554
555 -- If error analyzing name, then set Any_Type as result type and return
556
557 if Etype (Func_Nam) = Any_Type then
558 Set_Etype (N, Any_Type);
559 return;
560 end if;
561
562 -- Otherwise analyze the parameters
563
564 if Present (Actuals) then
565 Actual := First (Actuals);
566 while Present (Actual) loop
567 Analyze (Actual);
568 Check_Parameterless_Call (Actual);
569 Next (Actual);
570 end loop;
571 end if;
572
573 Analyze_Call (N);
574 end Analyze_Function_Call;
575
576 -----------------------------
577 -- Analyze_Function_Return --
578 -----------------------------
579
580 procedure Analyze_Function_Return (N : Node_Id) is
581 Loc : constant Source_Ptr := Sloc (N);
582 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
583 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
584
585 R_Type : constant Entity_Id := Etype (Scope_Id);
586 -- Function result subtype
587
588 procedure Check_Limited_Return (Expr : Node_Id);
589 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
590 -- limited types. Used only for simple return statements.
591 -- Expr is the expression returned.
592
593 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
594 -- Check that the return_subtype_indication properly matches the result
595 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
596
597 --------------------------
598 -- Check_Limited_Return --
599 --------------------------
600
601 procedure Check_Limited_Return (Expr : Node_Id) is
602 begin
603 -- Ada 2005 (AI-318-02): Return-by-reference types have been
604 -- removed and replaced by anonymous access results. This is an
605 -- incompatibility with Ada 95. Not clear whether this should be
606 -- enforced yet or perhaps controllable with special switch. ???
607
608 -- A limited interface that is not immutably limited is OK.
609
610 if Is_Limited_Interface (R_Type)
611 and then
612 not (Is_Task_Interface (R_Type)
613 or else Is_Protected_Interface (R_Type)
614 or else Is_Synchronized_Interface (R_Type))
615 then
616 null;
617
618 elsif Is_Limited_Type (R_Type)
619 and then not Is_Interface (R_Type)
620 and then Comes_From_Source (N)
621 and then not In_Instance_Body
622 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
623 then
624 -- Error in Ada 2005
625
626 if Ada_Version >= Ada_2005
627 and then not Debug_Flag_Dot_L
628 and then not GNAT_Mode
629 then
630 Error_Msg_N
631 ("(Ada 2005) cannot copy object of a limited type " &
632 "(RM-2005 6.5(5.5/2))", Expr);
633
634 if Is_Limited_View (R_Type) then
635 Error_Msg_N
636 ("\return by reference not permitted in Ada 2005", Expr);
637 end if;
638
639 -- Warn in Ada 95 mode, to give folks a heads up about this
640 -- incompatibility.
641
642 -- In GNAT mode, this is just a warning, to allow it to be
643 -- evilly turned off. Otherwise it is a real error.
644
645 -- In a generic context, simplify the warning because it makes
646 -- no sense to discuss pass-by-reference or copy.
647
648 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
649 if Inside_A_Generic then
650 Error_Msg_N
651 ("return of limited object not permitted in Ada 2005 "
652 & "(RM-2005 6.5(5.5/2))?y?", Expr);
653
654 elsif Is_Limited_View (R_Type) then
655 Error_Msg_N
656 ("return by reference not permitted in Ada 2005 "
657 & "(RM-2005 6.5(5.5/2))?y?", Expr);
658 else
659 Error_Msg_N
660 ("cannot copy object of a limited type in Ada 2005 "
661 & "(RM-2005 6.5(5.5/2))?y?", Expr);
662 end if;
663
664 -- Ada 95 mode, compatibility warnings disabled
665
666 else
667 return; -- skip continuation messages below
668 end if;
669
670 if not Inside_A_Generic then
671 Error_Msg_N
672 ("\consider switching to return of access type", Expr);
673 Explain_Limited_Type (R_Type, Expr);
674 end if;
675 end if;
676 end Check_Limited_Return;
677
678 -------------------------------------
679 -- Check_Return_Subtype_Indication --
680 -------------------------------------
681
682 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
683 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
684
685 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
686 -- Subtype given in the extended return statement (must match R_Type)
687
688 Subtype_Ind : constant Node_Id :=
689 Object_Definition (Original_Node (Obj_Decl));
690
691 R_Type_Is_Anon_Access : constant Boolean :=
692 Ekind_In (R_Type,
693 E_Anonymous_Access_Subprogram_Type,
694 E_Anonymous_Access_Protected_Subprogram_Type,
695 E_Anonymous_Access_Type);
696 -- True if return type of the function is an anonymous access type
697 -- Can't we make Is_Anonymous_Access_Type in einfo ???
698
699 R_Stm_Type_Is_Anon_Access : constant Boolean :=
700 Ekind_In (R_Stm_Type,
701 E_Anonymous_Access_Subprogram_Type,
702 E_Anonymous_Access_Protected_Subprogram_Type,
703 E_Anonymous_Access_Type);
704 -- True if type of the return object is an anonymous access type
705
706 procedure Error_No_Match (N : Node_Id);
707 -- Output error messages for case where types do not statically
708 -- match. N is the location for the messages.
709
710 --------------------
711 -- Error_No_Match --
712 --------------------
713
714 procedure Error_No_Match (N : Node_Id) is
715 begin
716 Error_Msg_N
717 ("subtype must statically match function result subtype", N);
718
719 if not Predicates_Match (R_Stm_Type, R_Type) then
720 Error_Msg_Node_2 := R_Type;
721 Error_Msg_NE
722 ("\predicate of & does not match predicate of &",
723 N, R_Stm_Type);
724 end if;
725 end Error_No_Match;
726
727 -- Start of processing for Check_Return_Subtype_Indication
728
729 begin
730 -- First, avoid cascaded errors
731
732 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
733 return;
734 end if;
735
736 -- "return access T" case; check that the return statement also has
737 -- "access T", and that the subtypes statically match:
738 -- if this is an access to subprogram the signatures must match.
739
740 if R_Type_Is_Anon_Access then
741 if R_Stm_Type_Is_Anon_Access then
742 if
743 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
744 then
745 if Base_Type (Designated_Type (R_Stm_Type)) /=
746 Base_Type (Designated_Type (R_Type))
747 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
748 then
749 Error_No_Match (Subtype_Mark (Subtype_Ind));
750 end if;
751
752 else
753 -- For two anonymous access to subprogram types, the
754 -- types themselves must be type conformant.
755
756 if not Conforming_Types
757 (R_Stm_Type, R_Type, Fully_Conformant)
758 then
759 Error_No_Match (Subtype_Ind);
760 end if;
761 end if;
762
763 else
764 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
765 end if;
766
767 -- If the return object is of an anonymous access type, then report
768 -- an error if the function's result type is not also anonymous.
769
770 elsif R_Stm_Type_Is_Anon_Access
771 and then not R_Type_Is_Anon_Access
772 then
773 Error_Msg_N ("anonymous access not allowed for function with " &
774 "named access result", Subtype_Ind);
775
776 -- Subtype indication case: check that the return object's type is
777 -- covered by the result type, and that the subtypes statically match
778 -- when the result subtype is constrained. Also handle record types
779 -- with unknown discriminants for which we have built the underlying
780 -- record view. Coverage is needed to allow specific-type return
781 -- objects when the result type is class-wide (see AI05-32).
782
783 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
784 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
785 and then
786 Covers
787 (Base_Type (R_Type),
788 Underlying_Record_View (Base_Type (R_Stm_Type))))
789 then
790 -- A null exclusion may be present on the return type, on the
791 -- function specification, on the object declaration or on the
792 -- subtype itself.
793
794 if Is_Access_Type (R_Type)
795 and then
796 (Can_Never_Be_Null (R_Type)
797 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
798 Can_Never_Be_Null (R_Stm_Type)
799 then
800 Error_No_Match (Subtype_Ind);
801 end if;
802
803 -- AI05-103: for elementary types, subtypes must statically match
804
805 if Is_Constrained (R_Type)
806 or else Is_Access_Type (R_Type)
807 then
808 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
809 Error_No_Match (Subtype_Ind);
810 end if;
811 end if;
812
813 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
814 and then Is_Null_Extension (Base_Type (R_Type))
815 then
816 null;
817
818 else
819 Error_Msg_N
820 ("wrong type for return_subtype_indication", Subtype_Ind);
821 end if;
822 end Check_Return_Subtype_Indication;
823
824 ---------------------
825 -- Local Variables --
826 ---------------------
827
828 Expr : Node_Id;
829
830 -- Start of processing for Analyze_Function_Return
831
832 begin
833 Set_Return_Present (Scope_Id);
834
835 if Nkind (N) = N_Simple_Return_Statement then
836 Expr := Expression (N);
837
838 -- Guard against a malformed expression. The parser may have tried to
839 -- recover but the node is not analyzable.
840
841 if Nkind (Expr) = N_Error then
842 Set_Etype (Expr, Any_Type);
843 Expander_Mode_Save_And_Set (False);
844 return;
845
846 else
847 -- The resolution of a controlled [extension] aggregate associated
848 -- with a return statement creates a temporary which needs to be
849 -- finalized on function exit. Wrap the return statement inside a
850 -- block so that the finalization machinery can detect this case.
851 -- This early expansion is done only when the return statement is
852 -- not part of a handled sequence of statements.
853
854 if Nkind_In (Expr, N_Aggregate,
855 N_Extension_Aggregate)
856 and then Needs_Finalization (R_Type)
857 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
858 then
859 Rewrite (N,
860 Make_Block_Statement (Loc,
861 Handled_Statement_Sequence =>
862 Make_Handled_Sequence_Of_Statements (Loc,
863 Statements => New_List (Relocate_Node (N)))));
864
865 Analyze (N);
866 return;
867 end if;
868
869 Analyze_And_Resolve (Expr, R_Type);
870 Check_Limited_Return (Expr);
871 end if;
872
873 -- RETURN only allowed in SPARK as the last statement in function
874
875 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
876 and then
877 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
878 or else Present (Next (N)))
879 then
880 Check_SPARK_Restriction
881 ("RETURN should be the last statement in function", N);
882 end if;
883
884 else
885 Check_SPARK_Restriction ("extended RETURN is not allowed", N);
886
887 -- Analyze parts specific to extended_return_statement:
888
889 declare
890 Obj_Decl : constant Node_Id :=
891 Last (Return_Object_Declarations (N));
892 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
893 HSS : constant Node_Id := Handled_Statement_Sequence (N);
894
895 begin
896 Expr := Expression (Obj_Decl);
897
898 -- Note: The check for OK_For_Limited_Init will happen in
899 -- Analyze_Object_Declaration; we treat it as a normal
900 -- object declaration.
901
902 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
903 Analyze (Obj_Decl);
904
905 Check_Return_Subtype_Indication (Obj_Decl);
906
907 if Present (HSS) then
908 Analyze (HSS);
909
910 if Present (Exception_Handlers (HSS)) then
911
912 -- ???Has_Nested_Block_With_Handler needs to be set.
913 -- Probably by creating an actual N_Block_Statement.
914 -- Probably in Expand.
915
916 null;
917 end if;
918 end if;
919
920 -- Mark the return object as referenced, since the return is an
921 -- implicit reference of the object.
922
923 Set_Referenced (Defining_Identifier (Obj_Decl));
924
925 Check_References (Stm_Entity);
926
927 -- Check RM 6.5 (5.9/3)
928
929 if Has_Aliased then
930 if Ada_Version < Ada_2012 then
931
932 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
933 -- Can it really happen (extended return???)
934
935 Error_Msg_N
936 ("aliased only allowed for limited return objects "
937 & "in Ada 2012??", N);
938
939 elsif not Is_Limited_View (R_Type) then
940 Error_Msg_N ("aliased only allowed for limited"
941 & " return objects", N);
942 end if;
943 end if;
944 end;
945 end if;
946
947 -- Case of Expr present
948
949 if Present (Expr)
950
951 -- Defend against previous errors
952
953 and then Nkind (Expr) /= N_Empty
954 and then Present (Etype (Expr))
955 then
956 -- Apply constraint check. Note that this is done before the implicit
957 -- conversion of the expression done for anonymous access types to
958 -- ensure correct generation of the null-excluding check associated
959 -- with null-excluding expressions found in return statements.
960
961 Apply_Constraint_Check (Expr, R_Type);
962
963 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
964 -- type, apply an implicit conversion of the expression to that type
965 -- to force appropriate static and run-time accessibility checks.
966
967 if Ada_Version >= Ada_2005
968 and then Ekind (R_Type) = E_Anonymous_Access_Type
969 then
970 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
971 Analyze_And_Resolve (Expr, R_Type);
972
973 -- If this is a local anonymous access to subprogram, the
974 -- accessibility check can be applied statically. The return is
975 -- illegal if the access type of the return expression is declared
976 -- inside of the subprogram (except if it is the subtype indication
977 -- of an extended return statement).
978
979 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
980 if not Comes_From_Source (Current_Scope)
981 or else Ekind (Current_Scope) = E_Return_Statement
982 then
983 null;
984
985 elsif
986 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
987 then
988 Error_Msg_N ("cannot return local access to subprogram", N);
989 end if;
990 end if;
991
992 -- If the result type is class-wide, then check that the return
993 -- expression's type is not declared at a deeper level than the
994 -- function (RM05-6.5(5.6/2)).
995
996 if Ada_Version >= Ada_2005
997 and then Is_Class_Wide_Type (R_Type)
998 then
999 if Type_Access_Level (Etype (Expr)) >
1000 Subprogram_Access_Level (Scope_Id)
1001 then
1002 Error_Msg_N
1003 ("level of return expression type is deeper than " &
1004 "class-wide function!", Expr);
1005 end if;
1006 end if;
1007
1008 -- Check incorrect use of dynamically tagged expression
1009
1010 if Is_Tagged_Type (R_Type) then
1011 Check_Dynamically_Tagged_Expression
1012 (Expr => Expr,
1013 Typ => R_Type,
1014 Related_Nod => N);
1015 end if;
1016
1017 -- ??? A real run-time accessibility check is needed in cases
1018 -- involving dereferences of access parameters. For now we just
1019 -- check the static cases.
1020
1021 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1022 and then Is_Limited_View (Etype (Scope_Id))
1023 and then Object_Access_Level (Expr) >
1024 Subprogram_Access_Level (Scope_Id)
1025 then
1026 -- Suppress the message in a generic, where the rewriting
1027 -- is irrelevant.
1028
1029 if Inside_A_Generic then
1030 null;
1031
1032 else
1033 Rewrite (N,
1034 Make_Raise_Program_Error (Loc,
1035 Reason => PE_Accessibility_Check_Failed));
1036 Analyze (N);
1037
1038 Error_Msg_Warn := SPARK_Mode /= On;
1039 Error_Msg_N ("cannot return a local value by reference<<", N);
1040 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1041 end if;
1042 end if;
1043
1044 if Known_Null (Expr)
1045 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1046 and then Null_Exclusion_Present (Parent (Scope_Id))
1047 then
1048 Apply_Compile_Time_Constraint_Error
1049 (N => Expr,
1050 Msg => "(Ada 2005) null not allowed for "
1051 & "null-excluding return??",
1052 Reason => CE_Null_Not_Allowed);
1053 end if;
1054 end if;
1055 end Analyze_Function_Return;
1056
1057 -------------------------------------
1058 -- Analyze_Generic_Subprogram_Body --
1059 -------------------------------------
1060
1061 procedure Analyze_Generic_Subprogram_Body
1062 (N : Node_Id;
1063 Gen_Id : Entity_Id)
1064 is
1065 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1066 Kind : constant Entity_Kind := Ekind (Gen_Id);
1067 Body_Id : Entity_Id;
1068 New_N : Node_Id;
1069 Spec : Node_Id;
1070
1071 begin
1072 -- Copy body and disable expansion while analyzing the generic For a
1073 -- stub, do not copy the stub (which would load the proper body), this
1074 -- will be done when the proper body is analyzed.
1075
1076 if Nkind (N) /= N_Subprogram_Body_Stub then
1077 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1078 Rewrite (N, New_N);
1079 Start_Generic;
1080 end if;
1081
1082 Spec := Specification (N);
1083
1084 -- Within the body of the generic, the subprogram is callable, and
1085 -- behaves like the corresponding non-generic unit.
1086
1087 Body_Id := Defining_Entity (Spec);
1088
1089 if Kind = E_Generic_Procedure
1090 and then Nkind (Spec) /= N_Procedure_Specification
1091 then
1092 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1093 return;
1094
1095 elsif Kind = E_Generic_Function
1096 and then Nkind (Spec) /= N_Function_Specification
1097 then
1098 Error_Msg_N ("invalid body for generic function ", Body_Id);
1099 return;
1100 end if;
1101
1102 Set_Corresponding_Body (Gen_Decl, Body_Id);
1103
1104 if Has_Completion (Gen_Id)
1105 and then Nkind (Parent (N)) /= N_Subunit
1106 then
1107 Error_Msg_N ("duplicate generic body", N);
1108 return;
1109 else
1110 Set_Has_Completion (Gen_Id);
1111 end if;
1112
1113 if Nkind (N) = N_Subprogram_Body_Stub then
1114 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1115 else
1116 Set_Corresponding_Spec (N, Gen_Id);
1117 end if;
1118
1119 if Nkind (Parent (N)) = N_Compilation_Unit then
1120 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1121 end if;
1122
1123 -- Make generic parameters immediately visible in the body. They are
1124 -- needed to process the formals declarations. Then make the formals
1125 -- visible in a separate step.
1126
1127 Push_Scope (Gen_Id);
1128
1129 declare
1130 E : Entity_Id;
1131 First_Ent : Entity_Id;
1132
1133 begin
1134 First_Ent := First_Entity (Gen_Id);
1135
1136 E := First_Ent;
1137 while Present (E) and then not Is_Formal (E) loop
1138 Install_Entity (E);
1139 Next_Entity (E);
1140 end loop;
1141
1142 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1143
1144 -- Now generic formals are visible, and the specification can be
1145 -- analyzed, for subsequent conformance check.
1146
1147 Body_Id := Analyze_Subprogram_Specification (Spec);
1148
1149 -- Make formal parameters visible
1150
1151 if Present (E) then
1152
1153 -- E is the first formal parameter, we loop through the formals
1154 -- installing them so that they will be visible.
1155
1156 Set_First_Entity (Gen_Id, E);
1157 while Present (E) loop
1158 Install_Entity (E);
1159 Next_Formal (E);
1160 end loop;
1161 end if;
1162
1163 -- Visible generic entity is callable within its own body
1164
1165 Set_Ekind (Gen_Id, Ekind (Body_Id));
1166 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
1167 Set_Ekind (Body_Id, E_Subprogram_Body);
1168 Set_Convention (Body_Id, Convention (Gen_Id));
1169 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1170 Set_Scope (Body_Id, Scope (Gen_Id));
1171 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1172
1173 if Nkind (N) = N_Subprogram_Body_Stub then
1174
1175 -- No body to analyze, so restore state of generic unit
1176
1177 Set_Ekind (Gen_Id, Kind);
1178 Set_Ekind (Body_Id, Kind);
1179
1180 if Present (First_Ent) then
1181 Set_First_Entity (Gen_Id, First_Ent);
1182 end if;
1183
1184 End_Scope;
1185 return;
1186 end if;
1187
1188 -- If this is a compilation unit, it must be made visible explicitly,
1189 -- because the compilation of the declaration, unlike other library
1190 -- unit declarations, does not. If it is not a unit, the following
1191 -- is redundant but harmless.
1192
1193 Set_Is_Immediately_Visible (Gen_Id);
1194 Reference_Body_Formals (Gen_Id, Body_Id);
1195
1196 if Is_Child_Unit (Gen_Id) then
1197 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1198 end if;
1199
1200 Set_Actual_Subtypes (N, Current_Scope);
1201
1202 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
1203 -- invariants and predicates associated with the body and its spec.
1204 -- Note that this is not pure expansion as Expand_Subprogram_Contract
1205 -- prepares the contract assertions for generic subprograms or for
1206 -- ASIS. Do not generate contract checks in SPARK mode.
1207
1208 if not GNATprove_Mode then
1209 Expand_Subprogram_Contract (N, Gen_Id, Body_Id);
1210 end if;
1211
1212 -- If the generic unit carries pre- or post-conditions, copy them
1213 -- to the original generic tree, so that they are properly added
1214 -- to any instantiation.
1215
1216 declare
1217 Orig : constant Node_Id := Original_Node (N);
1218 Cond : Node_Id;
1219
1220 begin
1221 Cond := First (Declarations (N));
1222 while Present (Cond) loop
1223 if Nkind (Cond) = N_Pragma
1224 and then Pragma_Name (Cond) = Name_Check
1225 then
1226 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1227
1228 elsif Nkind (Cond) = N_Pragma
1229 and then Pragma_Name (Cond) = Name_Postcondition
1230 then
1231 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
1232 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1233 else
1234 exit;
1235 end if;
1236
1237 Next (Cond);
1238 end loop;
1239 end;
1240
1241 Check_SPARK_Mode_In_Generic (N);
1242
1243 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1244 Set_SPARK_Pragma_Inherited (Body_Id, True);
1245
1246 Analyze_Declarations (Declarations (N));
1247 Check_Completion;
1248 Analyze (Handled_Statement_Sequence (N));
1249
1250 Save_Global_References (Original_Node (N));
1251
1252 -- Prior to exiting the scope, include generic formals again (if any
1253 -- are present) in the set of local entities.
1254
1255 if Present (First_Ent) then
1256 Set_First_Entity (Gen_Id, First_Ent);
1257 end if;
1258
1259 Check_References (Gen_Id);
1260 end;
1261
1262 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1263 End_Scope;
1264 Check_Subprogram_Order (N);
1265
1266 -- Outside of its body, unit is generic again
1267
1268 Set_Ekind (Gen_Id, Kind);
1269 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1270
1271 if Style_Check then
1272 Style.Check_Identifier (Body_Id, Gen_Id);
1273 end if;
1274
1275 End_Generic;
1276 end Analyze_Generic_Subprogram_Body;
1277
1278 ----------------------------
1279 -- Analyze_Null_Procedure --
1280 ----------------------------
1281
1282 procedure Analyze_Null_Procedure
1283 (N : Node_Id;
1284 Is_Completion : out Boolean)
1285 is
1286 Loc : constant Source_Ptr := Sloc (N);
1287 Spec : constant Node_Id := Specification (N);
1288 Designator : Entity_Id;
1289 Form : Node_Id;
1290 Null_Body : Node_Id := Empty;
1291 Prev : Entity_Id;
1292
1293 begin
1294 -- Capture the profile of the null procedure before analysis, for
1295 -- expansion at the freeze point and at each point of call. The body is
1296 -- used if the procedure has preconditions, or if it is a completion. In
1297 -- the first case the body is analyzed at the freeze point, in the other
1298 -- it replaces the null procedure declaration.
1299
1300 Null_Body :=
1301 Make_Subprogram_Body (Loc,
1302 Specification => New_Copy_Tree (Spec),
1303 Declarations => New_List,
1304 Handled_Statement_Sequence =>
1305 Make_Handled_Sequence_Of_Statements (Loc,
1306 Statements => New_List (Make_Null_Statement (Loc))));
1307
1308 -- Create new entities for body and formals
1309
1310 Set_Defining_Unit_Name (Specification (Null_Body),
1311 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
1312
1313 Form := First (Parameter_Specifications (Specification (Null_Body)));
1314 while Present (Form) loop
1315 Set_Defining_Identifier (Form,
1316 Make_Defining_Identifier (Loc, Chars (Defining_Identifier (Form))));
1317 Next (Form);
1318 end loop;
1319
1320 -- Determine whether the null procedure may be a completion of a generic
1321 -- suprogram, in which case we use the new null body as the completion
1322 -- and set minimal semantic information on the original declaration,
1323 -- which is rewritten as a null statement.
1324
1325 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1326
1327 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1328 Insert_Before (N, Null_Body);
1329 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1330 Set_Contract (Defining_Entity (N), Make_Contract (Loc));
1331
1332 Rewrite (N, Make_Null_Statement (Loc));
1333 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1334 Is_Completion := True;
1335 return;
1336
1337 else
1338 -- Resolve the types of the formals now, because the freeze point
1339 -- may appear in a different context, e.g. an instantiation.
1340
1341 Form := First (Parameter_Specifications (Specification (Null_Body)));
1342 while Present (Form) loop
1343 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1344 Find_Type (Parameter_Type (Form));
1345
1346 elsif
1347 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1348 then
1349 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1350
1351 else
1352 -- The case of a null procedure with a formal that is an
1353 -- access_to_subprogram type, and that is used as an actual
1354 -- in an instantiation is left to the enthusiastic reader.
1355
1356 null;
1357 end if;
1358
1359 Next (Form);
1360 end loop;
1361 end if;
1362
1363 -- If there are previous overloadable entities with the same name,
1364 -- check whether any of them is completed by the null procedure.
1365
1366 if Present (Prev) and then Is_Overloadable (Prev) then
1367 Designator := Analyze_Subprogram_Specification (Spec);
1368 Prev := Find_Corresponding_Spec (N);
1369 end if;
1370
1371 if No (Prev) or else not Comes_From_Source (Prev) then
1372 Designator := Analyze_Subprogram_Specification (Spec);
1373 Set_Has_Completion (Designator);
1374
1375 -- Signal to caller that this is a procedure declaration
1376
1377 Is_Completion := False;
1378
1379 -- Null procedures are always inlined, but generic formal subprograms
1380 -- which appear as such in the internal instance of formal packages,
1381 -- need no completion and are not marked Inline.
1382
1383 if Expander_Active
1384 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1385 then
1386 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1387 Set_Body_To_Inline (N, Null_Body);
1388 Set_Is_Inlined (Designator);
1389 end if;
1390
1391 else
1392 -- The null procedure is a completion
1393
1394 Is_Completion := True;
1395
1396 if Expander_Active then
1397 Rewrite (N, Null_Body);
1398 Analyze (N);
1399
1400 else
1401 Designator := Analyze_Subprogram_Specification (Spec);
1402 Set_Has_Completion (Designator);
1403 Set_Has_Completion (Prev);
1404 end if;
1405 end if;
1406 end Analyze_Null_Procedure;
1407
1408 -----------------------------
1409 -- Analyze_Operator_Symbol --
1410 -----------------------------
1411
1412 -- An operator symbol such as "+" or "and" may appear in context where the
1413 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1414 -- is just a string, as in (conjunction = "or"). In these cases the parser
1415 -- generates this node, and the semantics does the disambiguation. Other
1416 -- such case are actuals in an instantiation, the generic unit in an
1417 -- instantiation, and pragma arguments.
1418
1419 procedure Analyze_Operator_Symbol (N : Node_Id) is
1420 Par : constant Node_Id := Parent (N);
1421
1422 begin
1423 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1424 or else Nkind (Par) = N_Function_Instantiation
1425 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1426 or else (Nkind (Par) = N_Pragma_Argument_Association
1427 and then not Is_Pragma_String_Literal (Par))
1428 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1429 or else (Nkind (Par) = N_Attribute_Reference
1430 and then Attribute_Name (Par) /= Name_Value)
1431 then
1432 Find_Direct_Name (N);
1433
1434 else
1435 Change_Operator_Symbol_To_String_Literal (N);
1436 Analyze (N);
1437 end if;
1438 end Analyze_Operator_Symbol;
1439
1440 -----------------------------------
1441 -- Analyze_Parameter_Association --
1442 -----------------------------------
1443
1444 procedure Analyze_Parameter_Association (N : Node_Id) is
1445 begin
1446 Analyze (Explicit_Actual_Parameter (N));
1447 end Analyze_Parameter_Association;
1448
1449 ----------------------------
1450 -- Analyze_Procedure_Call --
1451 ----------------------------
1452
1453 procedure Analyze_Procedure_Call (N : Node_Id) is
1454 Loc : constant Source_Ptr := Sloc (N);
1455 P : constant Node_Id := Name (N);
1456 Actuals : constant List_Id := Parameter_Associations (N);
1457 Actual : Node_Id;
1458 New_N : Node_Id;
1459
1460 procedure Analyze_Call_And_Resolve;
1461 -- Do Analyze and Resolve calls for procedure call
1462 -- At end, check illegal order dependence.
1463
1464 ------------------------------
1465 -- Analyze_Call_And_Resolve --
1466 ------------------------------
1467
1468 procedure Analyze_Call_And_Resolve is
1469 begin
1470 if Nkind (N) = N_Procedure_Call_Statement then
1471 Analyze_Call (N);
1472 Resolve (N, Standard_Void_Type);
1473 else
1474 Analyze (N);
1475 end if;
1476 end Analyze_Call_And_Resolve;
1477
1478 -- Start of processing for Analyze_Procedure_Call
1479
1480 begin
1481 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1482 -- a procedure call or an entry call. The prefix may denote an access
1483 -- to subprogram type, in which case an implicit dereference applies.
1484 -- If the prefix is an indexed component (without implicit dereference)
1485 -- then the construct denotes a call to a member of an entire family.
1486 -- If the prefix is a simple name, it may still denote a call to a
1487 -- parameterless member of an entry family. Resolution of these various
1488 -- interpretations is delicate.
1489
1490 Analyze (P);
1491
1492 -- If this is a call of the form Obj.Op, the call may have been
1493 -- analyzed and possibly rewritten into a block, in which case
1494 -- we are done.
1495
1496 if Analyzed (N) then
1497 return;
1498 end if;
1499
1500 -- If there is an error analyzing the name (which may have been
1501 -- rewritten if the original call was in prefix notation) then error
1502 -- has been emitted already, mark node and return.
1503
1504 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1505 Set_Etype (N, Any_Type);
1506 return;
1507 end if;
1508
1509 -- Otherwise analyze the parameters
1510
1511 if Present (Actuals) then
1512 Actual := First (Actuals);
1513
1514 while Present (Actual) loop
1515 Analyze (Actual);
1516 Check_Parameterless_Call (Actual);
1517 Next (Actual);
1518 end loop;
1519 end if;
1520
1521 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1522
1523 if Nkind (P) = N_Attribute_Reference
1524 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1525 Name_Elab_Body,
1526 Name_Elab_Subp_Body)
1527 then
1528 if Present (Actuals) then
1529 Error_Msg_N
1530 ("no parameters allowed for this call", First (Actuals));
1531 return;
1532 end if;
1533
1534 Set_Etype (N, Standard_Void_Type);
1535 Set_Analyzed (N);
1536
1537 elsif Is_Entity_Name (P)
1538 and then Is_Record_Type (Etype (Entity (P)))
1539 and then Remote_AST_I_Dereference (P)
1540 then
1541 return;
1542
1543 elsif Is_Entity_Name (P)
1544 and then Ekind (Entity (P)) /= E_Entry_Family
1545 then
1546 if Is_Access_Type (Etype (P))
1547 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1548 and then No (Actuals)
1549 and then Comes_From_Source (N)
1550 then
1551 Error_Msg_N ("missing explicit dereference in call", N);
1552 end if;
1553
1554 Analyze_Call_And_Resolve;
1555
1556 -- If the prefix is the simple name of an entry family, this is
1557 -- a parameterless call from within the task body itself.
1558
1559 elsif Is_Entity_Name (P)
1560 and then Nkind (P) = N_Identifier
1561 and then Ekind (Entity (P)) = E_Entry_Family
1562 and then Present (Actuals)
1563 and then No (Next (First (Actuals)))
1564 then
1565 -- Can be call to parameterless entry family. What appears to be the
1566 -- sole argument is in fact the entry index. Rewrite prefix of node
1567 -- accordingly. Source representation is unchanged by this
1568 -- transformation.
1569
1570 New_N :=
1571 Make_Indexed_Component (Loc,
1572 Prefix =>
1573 Make_Selected_Component (Loc,
1574 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1575 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1576 Expressions => Actuals);
1577 Set_Name (N, New_N);
1578 Set_Etype (New_N, Standard_Void_Type);
1579 Set_Parameter_Associations (N, No_List);
1580 Analyze_Call_And_Resolve;
1581
1582 elsif Nkind (P) = N_Explicit_Dereference then
1583 if Ekind (Etype (P)) = E_Subprogram_Type then
1584 Analyze_Call_And_Resolve;
1585 else
1586 Error_Msg_N ("expect access to procedure in call", P);
1587 end if;
1588
1589 -- The name can be a selected component or an indexed component that
1590 -- yields an access to subprogram. Such a prefix is legal if the call
1591 -- has parameter associations.
1592
1593 elsif Is_Access_Type (Etype (P))
1594 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1595 then
1596 if Present (Actuals) then
1597 Analyze_Call_And_Resolve;
1598 else
1599 Error_Msg_N ("missing explicit dereference in call ", N);
1600 end if;
1601
1602 -- If not an access to subprogram, then the prefix must resolve to the
1603 -- name of an entry, entry family, or protected operation.
1604
1605 -- For the case of a simple entry call, P is a selected component where
1606 -- the prefix is the task and the selector name is the entry. A call to
1607 -- a protected procedure will have the same syntax. If the protected
1608 -- object contains overloaded operations, the entity may appear as a
1609 -- function, the context will select the operation whose type is Void.
1610
1611 elsif Nkind (P) = N_Selected_Component
1612 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1613 E_Procedure,
1614 E_Function)
1615 then
1616 Analyze_Call_And_Resolve;
1617
1618 elsif Nkind (P) = N_Selected_Component
1619 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1620 and then Present (Actuals)
1621 and then No (Next (First (Actuals)))
1622 then
1623 -- Can be call to parameterless entry family. What appears to be the
1624 -- sole argument is in fact the entry index. Rewrite prefix of node
1625 -- accordingly. Source representation is unchanged by this
1626 -- transformation.
1627
1628 New_N :=
1629 Make_Indexed_Component (Loc,
1630 Prefix => New_Copy (P),
1631 Expressions => Actuals);
1632 Set_Name (N, New_N);
1633 Set_Etype (New_N, Standard_Void_Type);
1634 Set_Parameter_Associations (N, No_List);
1635 Analyze_Call_And_Resolve;
1636
1637 -- For the case of a reference to an element of an entry family, P is
1638 -- an indexed component whose prefix is a selected component (task and
1639 -- entry family), and whose index is the entry family index.
1640
1641 elsif Nkind (P) = N_Indexed_Component
1642 and then Nkind (Prefix (P)) = N_Selected_Component
1643 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1644 then
1645 Analyze_Call_And_Resolve;
1646
1647 -- If the prefix is the name of an entry family, it is a call from
1648 -- within the task body itself.
1649
1650 elsif Nkind (P) = N_Indexed_Component
1651 and then Nkind (Prefix (P)) = N_Identifier
1652 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1653 then
1654 New_N :=
1655 Make_Selected_Component (Loc,
1656 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1657 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1658 Rewrite (Prefix (P), New_N);
1659 Analyze (P);
1660 Analyze_Call_And_Resolve;
1661
1662 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1663 -- procedure name, so the construct can only be a qualified expression.
1664
1665 elsif Nkind (P) = N_Qualified_Expression
1666 and then Ada_Version >= Ada_2012
1667 then
1668 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1669 Analyze (N);
1670
1671 -- Anything else is an error
1672
1673 else
1674 Error_Msg_N ("invalid procedure or entry call", N);
1675 end if;
1676 end Analyze_Procedure_Call;
1677
1678 ------------------------------
1679 -- Analyze_Return_Statement --
1680 ------------------------------
1681
1682 procedure Analyze_Return_Statement (N : Node_Id) is
1683
1684 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1685 N_Extended_Return_Statement));
1686
1687 Returns_Object : constant Boolean :=
1688 Nkind (N) = N_Extended_Return_Statement
1689 or else
1690 (Nkind (N) = N_Simple_Return_Statement
1691 and then Present (Expression (N)));
1692 -- True if we're returning something; that is, "return <expression>;"
1693 -- or "return Result : T [:= ...]". False for "return;". Used for error
1694 -- checking: If Returns_Object is True, N should apply to a function
1695 -- body; otherwise N should apply to a procedure body, entry body,
1696 -- accept statement, or extended return statement.
1697
1698 function Find_What_It_Applies_To return Entity_Id;
1699 -- Find the entity representing the innermost enclosing body, accept
1700 -- statement, or extended return statement. If the result is a callable
1701 -- construct or extended return statement, then this will be the value
1702 -- of the Return_Applies_To attribute. Otherwise, the program is
1703 -- illegal. See RM-6.5(4/2).
1704
1705 -----------------------------
1706 -- Find_What_It_Applies_To --
1707 -----------------------------
1708
1709 function Find_What_It_Applies_To return Entity_Id is
1710 Result : Entity_Id := Empty;
1711
1712 begin
1713 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1714 -- and postconditions.
1715
1716 for J in reverse 0 .. Scope_Stack.Last loop
1717 Result := Scope_Stack.Table (J).Entity;
1718 exit when not Ekind_In (Result, E_Block, E_Loop)
1719 and then Chars (Result) /= Name_uPostconditions;
1720 end loop;
1721
1722 pragma Assert (Present (Result));
1723 return Result;
1724 end Find_What_It_Applies_To;
1725
1726 -- Local declarations
1727
1728 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1729 Kind : constant Entity_Kind := Ekind (Scope_Id);
1730 Loc : constant Source_Ptr := Sloc (N);
1731 Stm_Entity : constant Entity_Id :=
1732 New_Internal_Entity
1733 (E_Return_Statement, Current_Scope, Loc, 'R');
1734
1735 -- Start of processing for Analyze_Return_Statement
1736
1737 begin
1738 Set_Return_Statement_Entity (N, Stm_Entity);
1739
1740 Set_Etype (Stm_Entity, Standard_Void_Type);
1741 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1742
1743 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1744 -- (4/2): an inner return statement will apply to this extended return.
1745
1746 if Nkind (N) = N_Extended_Return_Statement then
1747 Push_Scope (Stm_Entity);
1748 end if;
1749
1750 -- Check that pragma No_Return is obeyed. Don't complain about the
1751 -- implicitly-generated return that is placed at the end.
1752
1753 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1754 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1755 end if;
1756
1757 -- Warn on any unassigned OUT parameters if in procedure
1758
1759 if Ekind (Scope_Id) = E_Procedure then
1760 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1761 end if;
1762
1763 -- Check that functions return objects, and other things do not
1764
1765 if Kind = E_Function or else Kind = E_Generic_Function then
1766 if not Returns_Object then
1767 Error_Msg_N ("missing expression in return from function", N);
1768 end if;
1769
1770 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1771 if Returns_Object then
1772 Error_Msg_N ("procedure cannot return value (use function)", N);
1773 end if;
1774
1775 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1776 if Returns_Object then
1777 if Is_Protected_Type (Scope (Scope_Id)) then
1778 Error_Msg_N ("entry body cannot return value", N);
1779 else
1780 Error_Msg_N ("accept statement cannot return value", N);
1781 end if;
1782 end if;
1783
1784 elsif Kind = E_Return_Statement then
1785
1786 -- We are nested within another return statement, which must be an
1787 -- extended_return_statement.
1788
1789 if Returns_Object then
1790 if Nkind (N) = N_Extended_Return_Statement then
1791 Error_Msg_N
1792 ("extended return statement cannot be nested (use `RETURN;`)",
1793 N);
1794
1795 -- Case of a simple return statement with a value inside extended
1796 -- return statement.
1797
1798 else
1799 Error_Msg_N
1800 ("return nested in extended return statement cannot return " &
1801 "value (use `RETURN;`)", N);
1802 end if;
1803 end if;
1804
1805 else
1806 Error_Msg_N ("illegal context for return statement", N);
1807 end if;
1808
1809 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1810 Analyze_Function_Return (N);
1811
1812 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1813 Set_Return_Present (Scope_Id);
1814 end if;
1815
1816 if Nkind (N) = N_Extended_Return_Statement then
1817 End_Scope;
1818 end if;
1819
1820 Kill_Current_Values (Last_Assignment_Only => True);
1821 Check_Unreachable_Code (N);
1822
1823 Analyze_Dimension (N);
1824 end Analyze_Return_Statement;
1825
1826 -------------------------------------
1827 -- Analyze_Simple_Return_Statement --
1828 -------------------------------------
1829
1830 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1831 begin
1832 if Present (Expression (N)) then
1833 Mark_Coextensions (N, Expression (N));
1834 end if;
1835
1836 Analyze_Return_Statement (N);
1837 end Analyze_Simple_Return_Statement;
1838
1839 -------------------------
1840 -- Analyze_Return_Type --
1841 -------------------------
1842
1843 procedure Analyze_Return_Type (N : Node_Id) is
1844 Designator : constant Entity_Id := Defining_Entity (N);
1845 Typ : Entity_Id := Empty;
1846
1847 begin
1848 -- Normal case where result definition does not indicate an error
1849
1850 if Result_Definition (N) /= Error then
1851 if Nkind (Result_Definition (N)) = N_Access_Definition then
1852 Check_SPARK_Restriction
1853 ("access result is not allowed", Result_Definition (N));
1854
1855 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1856
1857 declare
1858 AD : constant Node_Id :=
1859 Access_To_Subprogram_Definition (Result_Definition (N));
1860 begin
1861 if Present (AD) and then Protected_Present (AD) then
1862 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1863 else
1864 Typ := Access_Definition (N, Result_Definition (N));
1865 end if;
1866 end;
1867
1868 Set_Parent (Typ, Result_Definition (N));
1869 Set_Is_Local_Anonymous_Access (Typ);
1870 Set_Etype (Designator, Typ);
1871
1872 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1873
1874 Null_Exclusion_Static_Checks (N);
1875
1876 -- Subtype_Mark case
1877
1878 else
1879 Find_Type (Result_Definition (N));
1880 Typ := Entity (Result_Definition (N));
1881 Set_Etype (Designator, Typ);
1882
1883 -- Unconstrained array as result is not allowed in SPARK
1884
1885 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1886 Check_SPARK_Restriction
1887 ("returning an unconstrained array is not allowed",
1888 Result_Definition (N));
1889 end if;
1890
1891 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1892
1893 Null_Exclusion_Static_Checks (N);
1894
1895 -- If a null exclusion is imposed on the result type, then create
1896 -- a null-excluding itype (an access subtype) and use it as the
1897 -- function's Etype. Note that the null exclusion checks are done
1898 -- right before this, because they don't get applied to types that
1899 -- do not come from source.
1900
1901 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
1902 Set_Etype (Designator,
1903 Create_Null_Excluding_Itype
1904 (T => Typ,
1905 Related_Nod => N,
1906 Scope_Id => Scope (Current_Scope)));
1907
1908 -- The new subtype must be elaborated before use because
1909 -- it is visible outside of the function. However its base
1910 -- type may not be frozen yet, so the reference that will
1911 -- force elaboration must be attached to the freezing of
1912 -- the base type.
1913
1914 -- If the return specification appears on a proper body,
1915 -- the subtype will have been created already on the spec.
1916
1917 if Is_Frozen (Typ) then
1918 if Nkind (Parent (N)) = N_Subprogram_Body
1919 and then Nkind (Parent (Parent (N))) = N_Subunit
1920 then
1921 null;
1922 else
1923 Build_Itype_Reference (Etype (Designator), Parent (N));
1924 end if;
1925
1926 else
1927 Ensure_Freeze_Node (Typ);
1928
1929 declare
1930 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1931 begin
1932 Set_Itype (IR, Etype (Designator));
1933 Append_Freeze_Actions (Typ, New_List (IR));
1934 end;
1935 end if;
1936
1937 else
1938 Set_Etype (Designator, Typ);
1939 end if;
1940
1941 if Ekind (Typ) = E_Incomplete_Type
1942 and then Is_Value_Type (Typ)
1943 then
1944 null;
1945
1946 elsif Ekind (Typ) = E_Incomplete_Type
1947 or else (Is_Class_Wide_Type (Typ)
1948 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1949 then
1950 -- AI05-0151: Tagged incomplete types are allowed in all formal
1951 -- parts. Untagged incomplete types are not allowed in bodies.
1952
1953 if Ada_Version >= Ada_2012 then
1954 if Is_Tagged_Type (Typ) then
1955 null;
1956
1957 elsif Nkind (Parent (N)) = N_Subprogram_Body
1958 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
1959 N_Entry_Body)
1960 then
1961 Error_Msg_NE
1962 ("invalid use of untagged incomplete type&",
1963 Designator, Typ);
1964 end if;
1965
1966 -- The type must be completed in the current package. This
1967 -- is checked at the end of the package declaration when
1968 -- Taft-amendment types are identified. If the return type
1969 -- is class-wide, there is no required check, the type can
1970 -- be a bona fide TAT.
1971
1972 if Ekind (Scope (Current_Scope)) = E_Package
1973 and then In_Private_Part (Scope (Current_Scope))
1974 and then not Is_Class_Wide_Type (Typ)
1975 then
1976 Append_Elmt (Designator, Private_Dependents (Typ));
1977 end if;
1978
1979 else
1980 Error_Msg_NE
1981 ("invalid use of incomplete type&", Designator, Typ);
1982 end if;
1983 end if;
1984 end if;
1985
1986 -- Case where result definition does indicate an error
1987
1988 else
1989 Set_Etype (Designator, Any_Type);
1990 end if;
1991 end Analyze_Return_Type;
1992
1993 -----------------------------
1994 -- Analyze_Subprogram_Body --
1995 -----------------------------
1996
1997 procedure Analyze_Subprogram_Body (N : Node_Id) is
1998 Loc : constant Source_Ptr := Sloc (N);
1999 Body_Spec : constant Node_Id := Specification (N);
2000 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2001
2002 begin
2003 if Debug_Flag_C then
2004 Write_Str ("==> subprogram body ");
2005 Write_Name (Chars (Body_Id));
2006 Write_Str (" from ");
2007 Write_Location (Loc);
2008 Write_Eol;
2009 Indent;
2010 end if;
2011
2012 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2013
2014 -- The real work is split out into the helper, so it can do "return;"
2015 -- without skipping the debug output:
2016
2017 Analyze_Subprogram_Body_Helper (N);
2018
2019 if Debug_Flag_C then
2020 Outdent;
2021 Write_Str ("<== subprogram body ");
2022 Write_Name (Chars (Body_Id));
2023 Write_Str (" from ");
2024 Write_Location (Loc);
2025 Write_Eol;
2026 end if;
2027 end Analyze_Subprogram_Body;
2028
2029 --------------------------------------
2030 -- Analyze_Subprogram_Body_Contract --
2031 --------------------------------------
2032
2033 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
2034 Body_Decl : constant Node_Id := Parent (Parent (Body_Id));
2035 Prag : Node_Id;
2036 Ref_Depends : Node_Id := Empty;
2037 Ref_Global : Node_Id := Empty;
2038 Spec_Id : Entity_Id;
2039
2040 begin
2041 -- When a subprogram body declaration is illegal, its defining entity is
2042 -- left unanalyzed. There is nothing left to do in this case because the
2043 -- body lacks a contract, or even a proper Ekind.
2044
2045 if Ekind (Body_Id) = E_Void then
2046 return;
2047 end if;
2048
2049 if Nkind (Body_Decl) = N_Subprogram_Body_Stub then
2050 Spec_Id := Corresponding_Spec_Of_Stub (Body_Decl);
2051 else
2052 Spec_Id := Corresponding_Spec (Body_Decl);
2053 end if;
2054
2055 -- Locate and store pragmas Refined_Depends and Refined_Global since
2056 -- their order of analysis matters.
2057
2058 Prag := Classifications (Contract (Body_Id));
2059 while Present (Prag) loop
2060 if Pragma_Name (Prag) = Name_Refined_Depends then
2061 Ref_Depends := Prag;
2062 elsif Pragma_Name (Prag) = Name_Refined_Global then
2063 Ref_Global := Prag;
2064 end if;
2065
2066 Prag := Next_Pragma (Prag);
2067 end loop;
2068
2069 -- Analyze Refined_Global first as Refined_Depends may mention items
2070 -- classified in the global refinement.
2071
2072 if Present (Ref_Global) then
2073 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
2074
2075 -- When the corresponding Global aspect/pragma references a state with
2076 -- visible refinement, the body requires Refined_Global. Refinement is
2077 -- not required when SPARK checks are suppressed.
2078
2079 elsif Present (Spec_Id) then
2080 Prag := Get_Pragma (Spec_Id, Pragma_Global);
2081
2082 if SPARK_Mode /= Off
2083 and then Present (Prag)
2084 and then Contains_Refined_State (Prag)
2085 then
2086 Error_Msg_NE
2087 ("body of subprogram & requires global refinement",
2088 Body_Decl, Spec_Id);
2089 end if;
2090 end if;
2091
2092 -- Refined_Depends must be analyzed after Refined_Global in order to see
2093 -- the modes of all global refinements.
2094
2095 if Present (Ref_Depends) then
2096 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
2097
2098 -- When the corresponding Depends aspect/pragma references a state with
2099 -- visible refinement, the body requires Refined_Depends. Refinement is
2100 -- not required when SPARK checks are suppressed.
2101
2102 elsif Present (Spec_Id) then
2103 Prag := Get_Pragma (Spec_Id, Pragma_Depends);
2104
2105 if SPARK_Mode /= Off
2106 and then Present (Prag)
2107 and then Contains_Refined_State (Prag)
2108 then
2109 Error_Msg_NE
2110 ("body of subprogram & requires dependance refinement",
2111 Body_Decl, Spec_Id);
2112 end if;
2113 end if;
2114 end Analyze_Subprogram_Body_Contract;
2115
2116 ------------------------------------
2117 -- Analyze_Subprogram_Body_Helper --
2118 ------------------------------------
2119
2120 -- This procedure is called for regular subprogram bodies, generic bodies,
2121 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2122 -- specification matters, and is used to create a proper declaration for
2123 -- the subprogram, or to perform conformance checks.
2124
2125 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2126 Loc : constant Source_Ptr := Sloc (N);
2127 Body_Spec : constant Node_Id := Specification (N);
2128 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2129 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2130 Conformant : Boolean;
2131 HSS : Node_Id;
2132 Prot_Typ : Entity_Id := Empty;
2133 Spec_Id : Entity_Id;
2134 Spec_Decl : Node_Id := Empty;
2135
2136 Last_Real_Spec_Entity : Entity_Id := Empty;
2137 -- When we analyze a separate spec, the entity chain ends up containing
2138 -- the formals, as well as any itypes generated during analysis of the
2139 -- default expressions for parameters, or the arguments of associated
2140 -- precondition/postcondition pragmas (which are analyzed in the context
2141 -- of the spec since they have visibility on formals).
2142 --
2143 -- These entities belong with the spec and not the body. However we do
2144 -- the analysis of the body in the context of the spec (again to obtain
2145 -- visibility to the formals), and all the entities generated during
2146 -- this analysis end up also chained to the entity chain of the spec.
2147 -- But they really belong to the body, and there is circuitry to move
2148 -- them from the spec to the body.
2149 --
2150 -- However, when we do this move, we don't want to move the real spec
2151 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2152 -- variable points to the last real spec entity, so we only move those
2153 -- chained beyond that point. It is initialized to Empty to deal with
2154 -- the case where there is no separate spec.
2155
2156 procedure Analyze_Aspects_On_Body_Or_Stub;
2157 -- Analyze the aspect specifications of a subprogram body [stub]. It is
2158 -- assumed that N has aspects.
2159
2160 procedure Check_Anonymous_Return;
2161 -- Ada 2005: if a function returns an access type that denotes a task,
2162 -- or a type that contains tasks, we must create a master entity for
2163 -- the anonymous type, which typically will be used in an allocator
2164 -- in the body of the function.
2165
2166 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2167 -- Look ahead to recognize a pragma that may appear after the body.
2168 -- If there is a previous spec, check that it appears in the same
2169 -- declarative part. If the pragma is Inline_Always, perform inlining
2170 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2171 -- If the body acts as a spec, and inlining is required, we create a
2172 -- subprogram declaration for it, in order to attach the body to inline.
2173 -- If pragma does not appear after the body, check whether there is
2174 -- an inline pragma before any local declarations.
2175
2176 procedure Check_Missing_Return;
2177 -- Checks for a function with a no return statements, and also performs
2178 -- the warning checks implemented by Check_Returns. In formal mode, also
2179 -- verify that a function ends with a RETURN and that a procedure does
2180 -- not contain any RETURN.
2181
2182 function Disambiguate_Spec return Entity_Id;
2183 -- When a primitive is declared between the private view and the full
2184 -- view of a concurrent type which implements an interface, a special
2185 -- mechanism is used to find the corresponding spec of the primitive
2186 -- body.
2187
2188 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2189 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2190 -- incomplete types coming from a limited context and swap their limited
2191 -- views with the non-limited ones.
2192
2193 function Is_Private_Concurrent_Primitive
2194 (Subp_Id : Entity_Id) return Boolean;
2195 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2196 -- type that implements an interface and has a private view.
2197
2198 procedure Set_Trivial_Subprogram (N : Node_Id);
2199 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2200 -- subprogram whose body is being analyzed. N is the statement node
2201 -- causing the flag to be set, if the following statement is a return
2202 -- of an entity, we mark the entity as set in source to suppress any
2203 -- warning on the stylized use of function stubs with a dummy return.
2204
2205 procedure Verify_Overriding_Indicator;
2206 -- If there was a previous spec, the entity has been entered in the
2207 -- current scope previously. If the body itself carries an overriding
2208 -- indicator, check that it is consistent with the known status of the
2209 -- entity.
2210
2211 -------------------------------------
2212 -- Analyze_Aspects_On_Body_Or_Stub --
2213 -------------------------------------
2214
2215 procedure Analyze_Aspects_On_Body_Or_Stub is
2216 procedure Diagnose_Misplaced_Aspects;
2217 -- Subprogram body [stub] N has aspects, but they are not properly
2218 -- placed. Provide precise diagnostics depending on the aspects
2219 -- involved.
2220
2221 --------------------------------
2222 -- Diagnose_Misplaced_Aspects --
2223 --------------------------------
2224
2225 procedure Diagnose_Misplaced_Aspects is
2226 Asp : Node_Id;
2227 Asp_Nam : Name_Id;
2228 Asp_Id : Aspect_Id;
2229 -- The current aspect along with its name and id
2230
2231 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id);
2232 -- Emit an error message concerning SPARK aspect Asp. Ref_Nam is
2233 -- the name of the refined version of the aspect.
2234
2235 ------------------------
2236 -- SPARK_Aspect_Error --
2237 ------------------------
2238
2239 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id) is
2240 begin
2241 -- The corresponding spec already contains the aspect in
2242 -- question and the one appearing on the body must be the
2243 -- refined form:
2244
2245 -- procedure P with Global ...;
2246 -- procedure P with Global ... is ... end P;
2247 -- ^
2248 -- Refined_Global
2249
2250 if Has_Aspect (Spec_Id, Asp_Id) then
2251 Error_Msg_Name_1 := Asp_Nam;
2252
2253 -- Subunits cannot carry aspects that apply to a subprogram
2254 -- declaration.
2255
2256 if Nkind (Parent (N)) = N_Subunit then
2257 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
2258
2259 else
2260 Error_Msg_Name_2 := Ref_Nam;
2261 Error_Msg_N ("aspect % should be %", Asp);
2262 end if;
2263
2264 -- Otherwise the aspect must appear in the spec, not in the
2265 -- body:
2266
2267 -- procedure P;
2268 -- procedure P with Global ... is ... end P;
2269
2270 else
2271 Error_Msg_N
2272 ("aspect specification must appear in subprogram "
2273 & "declaration", Asp);
2274 end if;
2275 end SPARK_Aspect_Error;
2276
2277 -- Start of processing for Diagnose_Misplaced_Aspects
2278
2279 begin
2280 -- Iterate over the aspect specifications and emit specific errors
2281 -- where applicable.
2282
2283 Asp := First (Aspect_Specifications (N));
2284 while Present (Asp) loop
2285 Asp_Nam := Chars (Identifier (Asp));
2286 Asp_Id := Get_Aspect_Id (Asp_Nam);
2287
2288 -- Do not emit errors on aspects that can appear on a
2289 -- subprogram body. This scenario occurs when the aspect
2290 -- specification list contains both misplaced and properly
2291 -- placed aspects.
2292
2293 if Aspect_On_Body_Or_Stub_OK (Asp_Id) then
2294 null;
2295
2296 -- Special diagnostics for SPARK aspects
2297
2298 elsif Asp_Nam = Name_Depends then
2299 SPARK_Aspect_Error (Name_Refined_Depends);
2300
2301 elsif Asp_Nam = Name_Global then
2302 SPARK_Aspect_Error (Name_Refined_Global);
2303
2304 elsif Asp_Nam = Name_Post then
2305 SPARK_Aspect_Error (Name_Refined_Post);
2306
2307 else
2308 Error_Msg_N
2309 ("aspect specification must appear in subprogram "
2310 & "declaration", Asp);
2311 end if;
2312
2313 Next (Asp);
2314 end loop;
2315 end Diagnose_Misplaced_Aspects;
2316
2317 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
2318
2319 begin
2320 -- Language-defined aspects cannot be associated with a subprogram
2321 -- body [stub] if the subprogram has a spec. Certain implementation
2322 -- defined aspects are allowed to break this rule (for list, see
2323 -- table Aspect_On_Body_Or_Stub_OK).
2324
2325 if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then
2326 Diagnose_Misplaced_Aspects;
2327 else
2328 Analyze_Aspect_Specifications (N, Body_Id);
2329 end if;
2330 end Analyze_Aspects_On_Body_Or_Stub;
2331
2332 ----------------------------
2333 -- Check_Anonymous_Return --
2334 ----------------------------
2335
2336 procedure Check_Anonymous_Return is
2337 Decl : Node_Id;
2338 Par : Node_Id;
2339 Scop : Entity_Id;
2340
2341 begin
2342 if Present (Spec_Id) then
2343 Scop := Spec_Id;
2344 else
2345 Scop := Body_Id;
2346 end if;
2347
2348 if Ekind (Scop) = E_Function
2349 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2350 and then not Is_Thunk (Scop)
2351 and then (Has_Task (Designated_Type (Etype (Scop)))
2352 or else
2353 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2354 and then
2355 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2356 and then Expander_Active
2357
2358 -- Avoid cases with no tasking support
2359
2360 and then RTE_Available (RE_Current_Master)
2361 and then not Restriction_Active (No_Task_Hierarchy)
2362 then
2363 Decl :=
2364 Make_Object_Declaration (Loc,
2365 Defining_Identifier =>
2366 Make_Defining_Identifier (Loc, Name_uMaster),
2367 Constant_Present => True,
2368 Object_Definition =>
2369 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2370 Expression =>
2371 Make_Explicit_Dereference (Loc,
2372 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2373
2374 if Present (Declarations (N)) then
2375 Prepend (Decl, Declarations (N));
2376 else
2377 Set_Declarations (N, New_List (Decl));
2378 end if;
2379
2380 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2381 Set_Has_Master_Entity (Scop);
2382
2383 -- Now mark the containing scope as a task master
2384
2385 Par := N;
2386 while Nkind (Par) /= N_Compilation_Unit loop
2387 Par := Parent (Par);
2388 pragma Assert (Present (Par));
2389
2390 -- If we fall off the top, we are at the outer level, and
2391 -- the environment task is our effective master, so nothing
2392 -- to mark.
2393
2394 if Nkind_In
2395 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2396 then
2397 Set_Is_Task_Master (Par, True);
2398 exit;
2399 end if;
2400 end loop;
2401 end if;
2402 end Check_Anonymous_Return;
2403
2404 -------------------------
2405 -- Check_Inline_Pragma --
2406 -------------------------
2407
2408 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2409 Prag : Node_Id;
2410 Plist : List_Id;
2411
2412 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2413 -- True when N is a pragma Inline or Inline_Always that applies
2414 -- to this subprogram.
2415
2416 -----------------------
2417 -- Is_Inline_Pragma --
2418 -----------------------
2419
2420 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2421 begin
2422 return
2423 Nkind (N) = N_Pragma
2424 and then
2425 (Pragma_Name (N) = Name_Inline_Always
2426 or else
2427 (Front_End_Inlining
2428 and then Pragma_Name (N) = Name_Inline))
2429 and then
2430 Chars
2431 (Expression (First (Pragma_Argument_Associations (N)))) =
2432 Chars (Body_Id);
2433 end Is_Inline_Pragma;
2434
2435 -- Start of processing for Check_Inline_Pragma
2436
2437 begin
2438 if not Expander_Active then
2439 return;
2440 end if;
2441
2442 if Is_List_Member (N)
2443 and then Present (Next (N))
2444 and then Is_Inline_Pragma (Next (N))
2445 then
2446 Prag := Next (N);
2447
2448 elsif Nkind (N) /= N_Subprogram_Body_Stub
2449 and then Present (Declarations (N))
2450 and then Is_Inline_Pragma (First (Declarations (N)))
2451 then
2452 Prag := First (Declarations (N));
2453
2454 else
2455 Prag := Empty;
2456 end if;
2457
2458 if Present (Prag) then
2459 if Present (Spec_Id) then
2460 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2461 Analyze (Prag);
2462 end if;
2463
2464 else
2465 -- Create a subprogram declaration, to make treatment uniform
2466
2467 declare
2468 Subp : constant Entity_Id :=
2469 Make_Defining_Identifier (Loc, Chars (Body_Id));
2470 Decl : constant Node_Id :=
2471 Make_Subprogram_Declaration (Loc,
2472 Specification =>
2473 New_Copy_Tree (Specification (N)));
2474
2475 begin
2476 Set_Defining_Unit_Name (Specification (Decl), Subp);
2477
2478 if Present (First_Formal (Body_Id)) then
2479 Plist := Copy_Parameter_List (Body_Id);
2480 Set_Parameter_Specifications
2481 (Specification (Decl), Plist);
2482 end if;
2483
2484 Insert_Before (N, Decl);
2485 Analyze (Decl);
2486 Analyze (Prag);
2487 Set_Has_Pragma_Inline (Subp);
2488
2489 if Pragma_Name (Prag) = Name_Inline_Always then
2490 Set_Is_Inlined (Subp);
2491 Set_Has_Pragma_Inline_Always (Subp);
2492 end if;
2493
2494 -- Prior to copying the subprogram body to create a template
2495 -- for it for subsequent inlining, remove the pragma from
2496 -- the current body so that the copy that will produce the
2497 -- new body will start from a completely unanalyzed tree.
2498
2499 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2500 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2501 end if;
2502
2503 Spec := Subp;
2504 end;
2505 end if;
2506 end if;
2507 end Check_Inline_Pragma;
2508
2509 --------------------------
2510 -- Check_Missing_Return --
2511 --------------------------
2512
2513 procedure Check_Missing_Return is
2514 Id : Entity_Id;
2515 Missing_Ret : Boolean;
2516
2517 begin
2518 if Nkind (Body_Spec) = N_Function_Specification then
2519 if Present (Spec_Id) then
2520 Id := Spec_Id;
2521 else
2522 Id := Body_Id;
2523 end if;
2524
2525 if Return_Present (Id) then
2526 Check_Returns (HSS, 'F', Missing_Ret);
2527
2528 if Missing_Ret then
2529 Set_Has_Missing_Return (Id);
2530 end if;
2531
2532 elsif Is_Generic_Subprogram (Id)
2533 or else not Is_Machine_Code_Subprogram (Id)
2534 then
2535 Error_Msg_N ("missing RETURN statement in function body", N);
2536 end if;
2537
2538 -- If procedure with No_Return, check returns
2539
2540 elsif Nkind (Body_Spec) = N_Procedure_Specification
2541 and then Present (Spec_Id)
2542 and then No_Return (Spec_Id)
2543 then
2544 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2545 end if;
2546
2547 -- Special checks in SPARK mode
2548
2549 if Nkind (Body_Spec) = N_Function_Specification then
2550
2551 -- In SPARK mode, last statement of a function should be a return
2552
2553 declare
2554 Stat : constant Node_Id := Last_Source_Statement (HSS);
2555 begin
2556 if Present (Stat)
2557 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2558 N_Extended_Return_Statement)
2559 then
2560 Check_SPARK_Restriction
2561 ("last statement in function should be RETURN", Stat);
2562 end if;
2563 end;
2564
2565 -- In SPARK mode, verify that a procedure has no return
2566
2567 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2568 if Present (Spec_Id) then
2569 Id := Spec_Id;
2570 else
2571 Id := Body_Id;
2572 end if;
2573
2574 -- Would be nice to point to return statement here, can we
2575 -- borrow the Check_Returns procedure here ???
2576
2577 if Return_Present (Id) then
2578 Check_SPARK_Restriction
2579 ("procedure should not have RETURN", N);
2580 end if;
2581 end if;
2582 end Check_Missing_Return;
2583
2584 -----------------------
2585 -- Disambiguate_Spec --
2586 -----------------------
2587
2588 function Disambiguate_Spec return Entity_Id is
2589 Priv_Spec : Entity_Id;
2590 Spec_N : Entity_Id;
2591
2592 procedure Replace_Types (To_Corresponding : Boolean);
2593 -- Depending on the flag, replace the type of formal parameters of
2594 -- Body_Id if it is a concurrent type implementing interfaces with
2595 -- the corresponding record type or the other way around.
2596
2597 procedure Replace_Types (To_Corresponding : Boolean) is
2598 Formal : Entity_Id;
2599 Formal_Typ : Entity_Id;
2600
2601 begin
2602 Formal := First_Formal (Body_Id);
2603 while Present (Formal) loop
2604 Formal_Typ := Etype (Formal);
2605
2606 if Is_Class_Wide_Type (Formal_Typ) then
2607 Formal_Typ := Root_Type (Formal_Typ);
2608 end if;
2609
2610 -- From concurrent type to corresponding record
2611
2612 if To_Corresponding then
2613 if Is_Concurrent_Type (Formal_Typ)
2614 and then Present (Corresponding_Record_Type (Formal_Typ))
2615 and then Present (Interfaces (
2616 Corresponding_Record_Type (Formal_Typ)))
2617 then
2618 Set_Etype (Formal,
2619 Corresponding_Record_Type (Formal_Typ));
2620 end if;
2621
2622 -- From corresponding record to concurrent type
2623
2624 else
2625 if Is_Concurrent_Record_Type (Formal_Typ)
2626 and then Present (Interfaces (Formal_Typ))
2627 then
2628 Set_Etype (Formal,
2629 Corresponding_Concurrent_Type (Formal_Typ));
2630 end if;
2631 end if;
2632
2633 Next_Formal (Formal);
2634 end loop;
2635 end Replace_Types;
2636
2637 -- Start of processing for Disambiguate_Spec
2638
2639 begin
2640 -- Try to retrieve the specification of the body as is. All error
2641 -- messages are suppressed because the body may not have a spec in
2642 -- its current state.
2643
2644 Spec_N := Find_Corresponding_Spec (N, False);
2645
2646 -- It is possible that this is the body of a primitive declared
2647 -- between a private and a full view of a concurrent type. The
2648 -- controlling parameter of the spec carries the concurrent type,
2649 -- not the corresponding record type as transformed by Analyze_
2650 -- Subprogram_Specification. In such cases, we undo the change
2651 -- made by the analysis of the specification and try to find the
2652 -- spec again.
2653
2654 -- Note that wrappers already have their corresponding specs and
2655 -- bodies set during their creation, so if the candidate spec is
2656 -- a wrapper, then we definitely need to swap all types to their
2657 -- original concurrent status.
2658
2659 if No (Spec_N)
2660 or else Is_Primitive_Wrapper (Spec_N)
2661 then
2662 -- Restore all references of corresponding record types to the
2663 -- original concurrent types.
2664
2665 Replace_Types (To_Corresponding => False);
2666 Priv_Spec := Find_Corresponding_Spec (N, False);
2667
2668 -- The current body truly belongs to a primitive declared between
2669 -- a private and a full view. We leave the modified body as is,
2670 -- and return the true spec.
2671
2672 if Present (Priv_Spec)
2673 and then Is_Private_Primitive (Priv_Spec)
2674 then
2675 return Priv_Spec;
2676 end if;
2677
2678 -- In case that this is some sort of error, restore the original
2679 -- state of the body.
2680
2681 Replace_Types (To_Corresponding => True);
2682 end if;
2683
2684 return Spec_N;
2685 end Disambiguate_Spec;
2686
2687 ----------------------------
2688 -- Exchange_Limited_Views --
2689 ----------------------------
2690
2691 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2692 procedure Detect_And_Exchange (Id : Entity_Id);
2693 -- Determine whether Id's type denotes an incomplete type associated
2694 -- with a limited with clause and exchange the limited view with the
2695 -- non-limited one.
2696
2697 -------------------------
2698 -- Detect_And_Exchange --
2699 -------------------------
2700
2701 procedure Detect_And_Exchange (Id : Entity_Id) is
2702 Typ : constant Entity_Id := Etype (Id);
2703
2704 begin
2705 if Ekind (Typ) = E_Incomplete_Type
2706 and then From_Limited_With (Typ)
2707 and then Present (Non_Limited_View (Typ))
2708 then
2709 Set_Etype (Id, Non_Limited_View (Typ));
2710 end if;
2711 end Detect_And_Exchange;
2712
2713 -- Local variables
2714
2715 Formal : Entity_Id;
2716
2717 -- Start of processing for Exchange_Limited_Views
2718
2719 begin
2720 if No (Subp_Id) then
2721 return;
2722
2723 -- Do not process subprogram bodies as they already use the non-
2724 -- limited view of types.
2725
2726 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2727 return;
2728 end if;
2729
2730 -- Examine all formals and swap views when applicable
2731
2732 Formal := First_Formal (Subp_Id);
2733 while Present (Formal) loop
2734 Detect_And_Exchange (Formal);
2735
2736 Next_Formal (Formal);
2737 end loop;
2738
2739 -- Process the return type of a function
2740
2741 if Ekind (Subp_Id) = E_Function then
2742 Detect_And_Exchange (Subp_Id);
2743 end if;
2744 end Exchange_Limited_Views;
2745
2746 -------------------------------------
2747 -- Is_Private_Concurrent_Primitive --
2748 -------------------------------------
2749
2750 function Is_Private_Concurrent_Primitive
2751 (Subp_Id : Entity_Id) return Boolean
2752 is
2753 Formal_Typ : Entity_Id;
2754
2755 begin
2756 if Present (First_Formal (Subp_Id)) then
2757 Formal_Typ := Etype (First_Formal (Subp_Id));
2758
2759 if Is_Concurrent_Record_Type (Formal_Typ) then
2760 if Is_Class_Wide_Type (Formal_Typ) then
2761 Formal_Typ := Root_Type (Formal_Typ);
2762 end if;
2763
2764 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2765 end if;
2766
2767 -- The type of the first formal is a concurrent tagged type with
2768 -- a private view.
2769
2770 return
2771 Is_Concurrent_Type (Formal_Typ)
2772 and then Is_Tagged_Type (Formal_Typ)
2773 and then Has_Private_Declaration (Formal_Typ);
2774 end if;
2775
2776 return False;
2777 end Is_Private_Concurrent_Primitive;
2778
2779 ----------------------------
2780 -- Set_Trivial_Subprogram --
2781 ----------------------------
2782
2783 procedure Set_Trivial_Subprogram (N : Node_Id) is
2784 Nxt : constant Node_Id := Next (N);
2785
2786 begin
2787 Set_Is_Trivial_Subprogram (Body_Id);
2788
2789 if Present (Spec_Id) then
2790 Set_Is_Trivial_Subprogram (Spec_Id);
2791 end if;
2792
2793 if Present (Nxt)
2794 and then Nkind (Nxt) = N_Simple_Return_Statement
2795 and then No (Next (Nxt))
2796 and then Present (Expression (Nxt))
2797 and then Is_Entity_Name (Expression (Nxt))
2798 then
2799 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2800 end if;
2801 end Set_Trivial_Subprogram;
2802
2803 ---------------------------------
2804 -- Verify_Overriding_Indicator --
2805 ---------------------------------
2806
2807 procedure Verify_Overriding_Indicator is
2808 begin
2809 if Must_Override (Body_Spec) then
2810 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2811 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2812 then
2813 null;
2814
2815 elsif not Present (Overridden_Operation (Spec_Id)) then
2816 Error_Msg_NE
2817 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2818
2819 -- Overriding indicators aren't allowed for protected subprogram
2820 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2821 -- this to a warning if -gnatd.E is enabled.
2822
2823 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2824 Error_Msg_Warn := Error_To_Warning;
2825 Error_Msg_N
2826 ("<<overriding indicator not allowed for protected "
2827 & "subprogram body", Body_Spec);
2828 end if;
2829
2830 elsif Must_Not_Override (Body_Spec) then
2831 if Present (Overridden_Operation (Spec_Id)) then
2832 Error_Msg_NE
2833 ("subprogram& overrides inherited operation",
2834 Body_Spec, Spec_Id);
2835
2836 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2837 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2838 then
2839 Error_Msg_NE
2840 ("subprogram & overrides predefined operator ",
2841 Body_Spec, Spec_Id);
2842
2843 -- Overriding indicators aren't allowed for protected subprogram
2844 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2845 -- this to a warning if -gnatd.E is enabled.
2846
2847 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2848 Error_Msg_Warn := Error_To_Warning;
2849
2850 Error_Msg_N
2851 ("<<overriding indicator not allowed " &
2852 "for protected subprogram body",
2853 Body_Spec);
2854
2855 -- If this is not a primitive operation, then the overriding
2856 -- indicator is altogether illegal.
2857
2858 elsif not Is_Primitive (Spec_Id) then
2859 Error_Msg_N
2860 ("overriding indicator only allowed " &
2861 "if subprogram is primitive",
2862 Body_Spec);
2863 end if;
2864
2865 -- If checking the style rule and the operation overrides, then
2866 -- issue a warning about a missing overriding_indicator. Protected
2867 -- subprogram bodies are excluded from this style checking, since
2868 -- they aren't primitives (even though their declarations can
2869 -- override) and aren't allowed to have an overriding_indicator.
2870
2871 elsif Style_Check
2872 and then Present (Overridden_Operation (Spec_Id))
2873 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2874 then
2875 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2876 Style.Missing_Overriding (N, Body_Id);
2877
2878 elsif Style_Check
2879 and then Can_Override_Operator (Spec_Id)
2880 and then not Is_Predefined_File_Name
2881 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2882 then
2883 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2884 Style.Missing_Overriding (N, Body_Id);
2885 end if;
2886 end Verify_Overriding_Indicator;
2887
2888 -- Start of processing for Analyze_Subprogram_Body_Helper
2889
2890 begin
2891 -- Generic subprograms are handled separately. They always have a
2892 -- generic specification. Determine whether current scope has a
2893 -- previous declaration.
2894
2895 -- If the subprogram body is defined within an instance of the same
2896 -- name, the instance appears as a package renaming, and will be hidden
2897 -- within the subprogram.
2898
2899 if Present (Prev_Id)
2900 and then not Is_Overloadable (Prev_Id)
2901 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2902 or else Comes_From_Source (Prev_Id))
2903 then
2904 if Is_Generic_Subprogram (Prev_Id) then
2905 Spec_Id := Prev_Id;
2906 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2907 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2908
2909 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2910
2911 if Nkind (N) = N_Subprogram_Body then
2912 HSS := Handled_Statement_Sequence (N);
2913 Check_Missing_Return;
2914 end if;
2915
2916 return;
2917
2918 else
2919 -- Previous entity conflicts with subprogram name. Attempting to
2920 -- enter name will post error.
2921
2922 Enter_Name (Body_Id);
2923 return;
2924 end if;
2925
2926 -- Non-generic case, find the subprogram declaration, if one was seen,
2927 -- or enter new overloaded entity in the current scope. If the
2928 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2929 -- part of the context of one of its subunits. No need to redo the
2930 -- analysis.
2931
2932 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
2933 return;
2934
2935 else
2936 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2937
2938 if Nkind (N) = N_Subprogram_Body_Stub
2939 or else No (Corresponding_Spec (N))
2940 then
2941 if Is_Private_Concurrent_Primitive (Body_Id) then
2942 Spec_Id := Disambiguate_Spec;
2943 else
2944 Spec_Id := Find_Corresponding_Spec (N);
2945 end if;
2946
2947 -- If this is a duplicate body, no point in analyzing it
2948
2949 if Error_Posted (N) then
2950 return;
2951 end if;
2952
2953 -- A subprogram body should cause freezing of its own declaration,
2954 -- but if there was no previous explicit declaration, then the
2955 -- subprogram will get frozen too late (there may be code within
2956 -- the body that depends on the subprogram having been frozen,
2957 -- such as uses of extra formals), so we force it to be frozen
2958 -- here. Same holds if the body and spec are compilation units.
2959 -- Finally, if the return type is an anonymous access to protected
2960 -- subprogram, it must be frozen before the body because its
2961 -- expansion has generated an equivalent type that is used when
2962 -- elaborating the body.
2963
2964 -- An exception in the case of Ada 2012, AI05-177: The bodies
2965 -- created for expression functions do not freeze.
2966
2967 if No (Spec_Id)
2968 and then Nkind (Original_Node (N)) /= N_Expression_Function
2969 then
2970 Freeze_Before (N, Body_Id);
2971
2972 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2973 Freeze_Before (N, Spec_Id);
2974
2975 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2976 Freeze_Before (N, Etype (Body_Id));
2977 end if;
2978
2979 else
2980 Spec_Id := Corresponding_Spec (N);
2981 end if;
2982 end if;
2983
2984 -- Previously we scanned the body to look for nested subprograms, and
2985 -- rejected an inline directive if nested subprograms were present,
2986 -- because the back-end would generate conflicting symbols for the
2987 -- nested bodies. This is now unnecessary.
2988
2989 -- Look ahead to recognize a pragma Inline that appears after the body
2990
2991 Check_Inline_Pragma (Spec_Id);
2992
2993 -- Deal with special case of a fully private operation in the body of
2994 -- the protected type. We must create a declaration for the subprogram,
2995 -- in order to attach the protected subprogram that will be used in
2996 -- internal calls. We exclude compiler generated bodies from the
2997 -- expander since the issue does not arise for those cases.
2998
2999 if No (Spec_Id)
3000 and then Comes_From_Source (N)
3001 and then Is_Protected_Type (Current_Scope)
3002 then
3003 Spec_Id := Build_Private_Protected_Declaration (N);
3004 end if;
3005
3006 -- If a separate spec is present, then deal with freezing issues
3007
3008 if Present (Spec_Id) then
3009 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3010 Verify_Overriding_Indicator;
3011
3012 -- In general, the spec will be frozen when we start analyzing the
3013 -- body. However, for internally generated operations, such as
3014 -- wrapper functions for inherited operations with controlling
3015 -- results, the spec may not have been frozen by the time we expand
3016 -- the freeze actions that include the bodies. In particular, extra
3017 -- formals for accessibility or for return-in-place may need to be
3018 -- generated. Freeze nodes, if any, are inserted before the current
3019 -- body. These freeze actions are also needed in ASIS mode to enable
3020 -- the proper back-annotations.
3021
3022 if not Is_Frozen (Spec_Id)
3023 and then (Expander_Active or ASIS_Mode)
3024 then
3025 -- Force the generation of its freezing node to ensure proper
3026 -- management of access types in the backend.
3027
3028 -- This is definitely needed for some cases, but it is not clear
3029 -- why, to be investigated further???
3030
3031 Set_Has_Delayed_Freeze (Spec_Id);
3032 Freeze_Before (N, Spec_Id);
3033 end if;
3034 end if;
3035
3036 -- Mark presence of postcondition procedure in current scope and mark
3037 -- the procedure itself as needing debug info. The latter is important
3038 -- when analyzing decision coverage (for example, for MC/DC coverage).
3039
3040 if Chars (Body_Id) = Name_uPostconditions then
3041 Set_Has_Postconditions (Current_Scope);
3042 Set_Debug_Info_Needed (Body_Id);
3043 end if;
3044
3045 -- Place subprogram on scope stack, and make formals visible. If there
3046 -- is a spec, the visible entity remains that of the spec.
3047
3048 if Present (Spec_Id) then
3049 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3050
3051 if Is_Child_Unit (Spec_Id) then
3052 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3053 end if;
3054
3055 if Style_Check then
3056 Style.Check_Identifier (Body_Id, Spec_Id);
3057 end if;
3058
3059 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3060 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3061
3062 if Is_Abstract_Subprogram (Spec_Id) then
3063 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3064 return;
3065
3066 else
3067 Set_Convention (Body_Id, Convention (Spec_Id));
3068 Set_Has_Completion (Spec_Id);
3069
3070 if Is_Protected_Type (Scope (Spec_Id)) then
3071 Prot_Typ := Scope (Spec_Id);
3072 end if;
3073
3074 -- If this is a body generated for a renaming, do not check for
3075 -- full conformance. The check is redundant, because the spec of
3076 -- the body is a copy of the spec in the renaming declaration,
3077 -- and the test can lead to spurious errors on nested defaults.
3078
3079 if Present (Spec_Decl)
3080 and then not Comes_From_Source (N)
3081 and then
3082 (Nkind (Original_Node (Spec_Decl)) =
3083 N_Subprogram_Renaming_Declaration
3084 or else (Present (Corresponding_Body (Spec_Decl))
3085 and then
3086 Nkind (Unit_Declaration_Node
3087 (Corresponding_Body (Spec_Decl))) =
3088 N_Subprogram_Renaming_Declaration))
3089 then
3090 Conformant := True;
3091
3092 -- Conversely, the spec may have been generated for specless body
3093 -- with an inline pragma.
3094
3095 elsif Comes_From_Source (N)
3096 and then not Comes_From_Source (Spec_Id)
3097 and then Has_Pragma_Inline (Spec_Id)
3098 then
3099 Conformant := True;
3100
3101 else
3102 Check_Conformance
3103 (Body_Id, Spec_Id,
3104 Fully_Conformant, True, Conformant, Body_Id);
3105 end if;
3106
3107 -- If the body is not fully conformant, we have to decide if we
3108 -- should analyze it or not. If it has a really messed up profile
3109 -- then we probably should not analyze it, since we will get too
3110 -- many bogus messages.
3111
3112 -- Our decision is to go ahead in the non-fully conformant case
3113 -- only if it is at least mode conformant with the spec. Note
3114 -- that the call to Check_Fully_Conformant has issued the proper
3115 -- error messages to complain about the lack of conformance.
3116
3117 if not Conformant
3118 and then not Mode_Conformant (Body_Id, Spec_Id)
3119 then
3120 return;
3121 end if;
3122 end if;
3123
3124 if Spec_Id /= Body_Id then
3125 Reference_Body_Formals (Spec_Id, Body_Id);
3126 end if;
3127
3128 Set_Ekind (Body_Id, E_Subprogram_Body);
3129
3130 if Nkind (N) = N_Subprogram_Body_Stub then
3131 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3132
3133 -- Regular body
3134
3135 else
3136 Set_Corresponding_Spec (N, Spec_Id);
3137
3138 -- Ada 2005 (AI-345): If the operation is a primitive operation
3139 -- of a concurrent type, the type of the first parameter has been
3140 -- replaced with the corresponding record, which is the proper
3141 -- run-time structure to use. However, within the body there may
3142 -- be uses of the formals that depend on primitive operations
3143 -- of the type (in particular calls in prefixed form) for which
3144 -- we need the original concurrent type. The operation may have
3145 -- several controlling formals, so the replacement must be done
3146 -- for all of them.
3147
3148 if Comes_From_Source (Spec_Id)
3149 and then Present (First_Entity (Spec_Id))
3150 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3151 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3152 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3153 and then Present (Corresponding_Concurrent_Type
3154 (Etype (First_Entity (Spec_Id))))
3155 then
3156 declare
3157 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3158 Form : Entity_Id;
3159
3160 begin
3161 Form := First_Formal (Spec_Id);
3162 while Present (Form) loop
3163 if Etype (Form) = Typ then
3164 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3165 end if;
3166
3167 Next_Formal (Form);
3168 end loop;
3169 end;
3170 end if;
3171
3172 -- Make the formals visible, and place subprogram on scope stack.
3173 -- This is also the point at which we set Last_Real_Spec_Entity
3174 -- to mark the entities which will not be moved to the body.
3175
3176 Install_Formals (Spec_Id);
3177 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3178
3179 -- Within an instance, add local renaming declarations so that
3180 -- gdb can retrieve the values of actuals more easily. This is
3181 -- only relevant if generating code (and indeed we definitely
3182 -- do not want these definitions -gnatc mode, because that would
3183 -- confuse ASIS).
3184
3185 if Is_Generic_Instance (Spec_Id)
3186 and then Is_Wrapper_Package (Current_Scope)
3187 and then Expander_Active
3188 then
3189 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3190 end if;
3191
3192 Push_Scope (Spec_Id);
3193
3194 -- Make sure that the subprogram is immediately visible. For
3195 -- child units that have no separate spec this is indispensable.
3196 -- Otherwise it is safe albeit redundant.
3197
3198 Set_Is_Immediately_Visible (Spec_Id);
3199 end if;
3200
3201 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3202 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3203 Set_Scope (Body_Id, Scope (Spec_Id));
3204 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3205
3206 -- Case of subprogram body with no previous spec
3207
3208 else
3209 -- Check for style warning required
3210
3211 if Style_Check
3212
3213 -- Only apply check for source level subprograms for which checks
3214 -- have not been suppressed.
3215
3216 and then Comes_From_Source (Body_Id)
3217 and then not Suppress_Style_Checks (Body_Id)
3218
3219 -- No warnings within an instance
3220
3221 and then not In_Instance
3222
3223 -- No warnings for expression functions
3224
3225 and then Nkind (Original_Node (N)) /= N_Expression_Function
3226 then
3227 Style.Body_With_No_Spec (N);
3228 end if;
3229
3230 New_Overloaded_Entity (Body_Id);
3231
3232 if Nkind (N) /= N_Subprogram_Body_Stub then
3233 Set_Acts_As_Spec (N);
3234 Generate_Definition (Body_Id);
3235 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3236 Generate_Reference
3237 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3238 Install_Formals (Body_Id);
3239
3240 Push_Scope (Body_Id);
3241 end if;
3242
3243 -- For stubs and bodies with no previous spec, generate references to
3244 -- formals.
3245
3246 Generate_Reference_To_Formals (Body_Id);
3247 end if;
3248
3249 -- Set SPARK_Mode from context
3250
3251 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3252 Set_SPARK_Pragma_Inherited (Body_Id, True);
3253
3254 -- If the return type is an anonymous access type whose designated type
3255 -- is the limited view of a class-wide type and the non-limited view is
3256 -- available, update the return type accordingly.
3257
3258 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3259 declare
3260 Etyp : Entity_Id;
3261 Rtyp : Entity_Id;
3262
3263 begin
3264 Rtyp := Etype (Current_Scope);
3265
3266 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3267 Etyp := Directly_Designated_Type (Rtyp);
3268
3269 if Is_Class_Wide_Type (Etyp)
3270 and then From_Limited_With (Etyp)
3271 then
3272 Set_Directly_Designated_Type
3273 (Etype (Current_Scope), Available_View (Etyp));
3274 end if;
3275 end if;
3276 end;
3277 end if;
3278
3279 -- If this is the proper body of a stub, we must verify that the stub
3280 -- conforms to the body, and to the previous spec if one was present.
3281 -- We know already that the body conforms to that spec. This test is
3282 -- only required for subprograms that come from source.
3283
3284 if Nkind (Parent (N)) = N_Subunit
3285 and then Comes_From_Source (N)
3286 and then not Error_Posted (Body_Id)
3287 and then Nkind (Corresponding_Stub (Parent (N))) =
3288 N_Subprogram_Body_Stub
3289 then
3290 declare
3291 Old_Id : constant Entity_Id :=
3292 Defining_Entity
3293 (Specification (Corresponding_Stub (Parent (N))));
3294
3295 Conformant : Boolean := False;
3296
3297 begin
3298 if No (Spec_Id) then
3299 Check_Fully_Conformant (Body_Id, Old_Id);
3300
3301 else
3302 Check_Conformance
3303 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3304
3305 if not Conformant then
3306
3307 -- The stub was taken to be a new declaration. Indicate that
3308 -- it lacks a body.
3309
3310 Set_Has_Completion (Old_Id, False);
3311 end if;
3312 end if;
3313 end;
3314 end if;
3315
3316 Set_Has_Completion (Body_Id);
3317 Check_Eliminated (Body_Id);
3318
3319 if Nkind (N) = N_Subprogram_Body_Stub then
3320
3321 -- Analyze any aspect specifications that appear on the subprogram
3322 -- body stub.
3323
3324 if Has_Aspects (N) then
3325 Analyze_Aspects_On_Body_Or_Stub;
3326 end if;
3327
3328 -- Stop the analysis now as the stub cannot be inlined, plus it does
3329 -- not have declarative or statement lists.
3330
3331 return;
3332 end if;
3333
3334 -- Handle frontend inlining. There is no need to prepare us for inlining
3335 -- if we will not generate the code.
3336
3337 -- Old semantics
3338
3339 if not Debug_Flag_Dot_K then
3340 if Present (Spec_Id)
3341 and then Expander_Active
3342 and then
3343 (Has_Pragma_Inline_Always (Spec_Id)
3344 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
3345 then
3346 Build_Body_To_Inline (N, Spec_Id);
3347 end if;
3348
3349 -- New semantics
3350
3351 elsif Expander_Active
3352 and then Serious_Errors_Detected = 0
3353 and then Present (Spec_Id)
3354 and then Has_Pragma_Inline (Spec_Id)
3355 then
3356 Check_And_Build_Body_To_Inline (N, Spec_Id, Body_Id);
3357 end if;
3358
3359 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3360 -- of the specification we have to install the private withed units.
3361 -- This holds for child units as well.
3362
3363 if Is_Compilation_Unit (Body_Id)
3364 or else Nkind (Parent (N)) = N_Compilation_Unit
3365 then
3366 Install_Private_With_Clauses (Body_Id);
3367 end if;
3368
3369 Check_Anonymous_Return;
3370
3371 -- Set the Protected_Formal field of each extra formal of the protected
3372 -- subprogram to reference the corresponding extra formal of the
3373 -- subprogram that implements it. For regular formals this occurs when
3374 -- the protected subprogram's declaration is expanded, but the extra
3375 -- formals don't get created until the subprogram is frozen. We need to
3376 -- do this before analyzing the protected subprogram's body so that any
3377 -- references to the original subprogram's extra formals will be changed
3378 -- refer to the implementing subprogram's formals (see Expand_Formal).
3379
3380 if Present (Spec_Id)
3381 and then Is_Protected_Type (Scope (Spec_Id))
3382 and then Present (Protected_Body_Subprogram (Spec_Id))
3383 then
3384 declare
3385 Impl_Subp : constant Entity_Id :=
3386 Protected_Body_Subprogram (Spec_Id);
3387 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3388 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3389 begin
3390 while Present (Prot_Ext_Formal) loop
3391 pragma Assert (Present (Impl_Ext_Formal));
3392 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3393 Next_Formal_With_Extras (Prot_Ext_Formal);
3394 Next_Formal_With_Extras (Impl_Ext_Formal);
3395 end loop;
3396 end;
3397 end if;
3398
3399 -- Now we can go on to analyze the body
3400
3401 HSS := Handled_Statement_Sequence (N);
3402 Set_Actual_Subtypes (N, Current_Scope);
3403
3404 -- Add a declaration for the Protection object, renaming declarations
3405 -- for discriminals and privals and finally a declaration for the entry
3406 -- family index (if applicable). This form of early expansion is done
3407 -- when the Expander is active because Install_Private_Data_Declarations
3408 -- references entities which were created during regular expansion. The
3409 -- subprogram entity must come from source, and not be an internally
3410 -- generated subprogram.
3411
3412 if Expander_Active
3413 and then Present (Prot_Typ)
3414 and then Present (Spec_Id)
3415 and then Comes_From_Source (Spec_Id)
3416 and then not Is_Eliminated (Spec_Id)
3417 then
3418 Install_Private_Data_Declarations
3419 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3420 end if;
3421
3422 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3423 -- may now appear in parameter and result profiles. Since the analysis
3424 -- of a subprogram body may use the parameter and result profile of the
3425 -- spec, swap any limited views with their non-limited counterpart.
3426
3427 if Ada_Version >= Ada_2012 then
3428 Exchange_Limited_Views (Spec_Id);
3429 end if;
3430
3431 -- Analyze any aspect specifications that appear on the subprogram body
3432
3433 if Has_Aspects (N) then
3434 Analyze_Aspects_On_Body_Or_Stub;
3435 end if;
3436
3437 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
3438 -- invariants and predicates associated with the body and its spec.
3439 -- Note that this is not pure expansion as Expand_Subprogram_Contract
3440 -- prepares the contract assertions for generic subprograms or for ASIS.
3441 -- Do not generate contract checks in SPARK mode.
3442
3443 if not GNATprove_Mode then
3444 Expand_Subprogram_Contract (N, Spec_Id, Body_Id);
3445 end if;
3446
3447 -- Analyze the declarations (this call will analyze the precondition
3448 -- Check pragmas we prepended to the list, as well as the declaration
3449 -- of the _Postconditions procedure).
3450
3451 Analyze_Declarations (Declarations (N));
3452
3453 -- After declarations have been analyzed, the body has been set
3454 -- its final value of SPARK_Mode. Check that SPARK_Mode for body
3455 -- is consistent with SPARK_Mode for spec.
3456
3457 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
3458 if Present (SPARK_Pragma (Spec_Id)) then
3459 if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off
3460 and then
3461 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
3462 then
3463 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3464 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3465 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3466 Error_Msg_NE
3467 ("\value Off was set for SPARK_Mode on&#", N, Spec_Id);
3468 end if;
3469
3470 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3471 null;
3472
3473 else
3474 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3475 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3476 Error_Msg_Sloc := Sloc (Spec_Id);
3477 Error_Msg_NE ("\no value was set for SPARK_Mode on&#", N, Spec_Id);
3478 end if;
3479 end if;
3480
3481 -- Check completion, and analyze the statements
3482
3483 Check_Completion;
3484 Inspect_Deferred_Constant_Completion (Declarations (N));
3485 Analyze (HSS);
3486
3487 -- Deal with end of scope processing for the body
3488
3489 Process_End_Label (HSS, 't', Current_Scope);
3490 End_Scope;
3491 Check_Subprogram_Order (N);
3492 Set_Analyzed (Body_Id);
3493
3494 -- If we have a separate spec, then the analysis of the declarations
3495 -- caused the entities in the body to be chained to the spec id, but
3496 -- we want them chained to the body id. Only the formal parameters
3497 -- end up chained to the spec id in this case.
3498
3499 if Present (Spec_Id) then
3500
3501 -- We must conform to the categorization of our spec
3502
3503 Validate_Categorization_Dependency (N, Spec_Id);
3504
3505 -- And if this is a child unit, the parent units must conform
3506
3507 if Is_Child_Unit (Spec_Id) then
3508 Validate_Categorization_Dependency
3509 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3510 end if;
3511
3512 -- Here is where we move entities from the spec to the body
3513
3514 -- Case where there are entities that stay with the spec
3515
3516 if Present (Last_Real_Spec_Entity) then
3517
3518 -- No body entities (happens when the only real spec entities come
3519 -- from precondition and postcondition pragmas).
3520
3521 if No (Last_Entity (Body_Id)) then
3522 Set_First_Entity
3523 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3524
3525 -- Body entities present (formals), so chain stuff past them
3526
3527 else
3528 Set_Next_Entity
3529 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3530 end if;
3531
3532 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3533 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3534 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3535
3536 -- Case where there are no spec entities, in this case there can be
3537 -- no body entities either, so just move everything.
3538
3539 else
3540 pragma Assert (No (Last_Entity (Body_Id)));
3541 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3542 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3543 Set_First_Entity (Spec_Id, Empty);
3544 Set_Last_Entity (Spec_Id, Empty);
3545 end if;
3546 end if;
3547
3548 Check_Missing_Return;
3549
3550 -- Now we are going to check for variables that are never modified in
3551 -- the body of the procedure. But first we deal with a special case
3552 -- where we want to modify this check. If the body of the subprogram
3553 -- starts with a raise statement or its equivalent, or if the body
3554 -- consists entirely of a null statement, then it is pretty obvious that
3555 -- it is OK to not reference the parameters. For example, this might be
3556 -- the following common idiom for a stubbed function: statement of the
3557 -- procedure raises an exception. In particular this deals with the
3558 -- common idiom of a stubbed function, which appears something like:
3559
3560 -- function F (A : Integer) return Some_Type;
3561 -- X : Some_Type;
3562 -- begin
3563 -- raise Program_Error;
3564 -- return X;
3565 -- end F;
3566
3567 -- Here the purpose of X is simply to satisfy the annoying requirement
3568 -- in Ada that there be at least one return, and we certainly do not
3569 -- want to go posting warnings on X that it is not initialized. On
3570 -- the other hand, if X is entirely unreferenced that should still
3571 -- get a warning.
3572
3573 -- What we do is to detect these cases, and if we find them, flag the
3574 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3575 -- suppress unwanted warnings. For the case of the function stub above
3576 -- we have a special test to set X as apparently assigned to suppress
3577 -- the warning.
3578
3579 declare
3580 Stm : Node_Id;
3581
3582 begin
3583 -- Skip initial labels (for one thing this occurs when we are in
3584 -- front end ZCX mode, but in any case it is irrelevant), and also
3585 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3586
3587 Stm := First (Statements (HSS));
3588 while Nkind (Stm) = N_Label
3589 or else Nkind (Stm) in N_Push_xxx_Label
3590 loop
3591 Next (Stm);
3592 end loop;
3593
3594 -- Do the test on the original statement before expansion
3595
3596 declare
3597 Ostm : constant Node_Id := Original_Node (Stm);
3598
3599 begin
3600 -- If explicit raise statement, turn on flag
3601
3602 if Nkind (Ostm) = N_Raise_Statement then
3603 Set_Trivial_Subprogram (Stm);
3604
3605 -- If null statement, and no following statements, turn on flag
3606
3607 elsif Nkind (Stm) = N_Null_Statement
3608 and then Comes_From_Source (Stm)
3609 and then No (Next (Stm))
3610 then
3611 Set_Trivial_Subprogram (Stm);
3612
3613 -- Check for explicit call cases which likely raise an exception
3614
3615 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3616 if Is_Entity_Name (Name (Ostm)) then
3617 declare
3618 Ent : constant Entity_Id := Entity (Name (Ostm));
3619
3620 begin
3621 -- If the procedure is marked No_Return, then likely it
3622 -- raises an exception, but in any case it is not coming
3623 -- back here, so turn on the flag.
3624
3625 if Present (Ent)
3626 and then Ekind (Ent) = E_Procedure
3627 and then No_Return (Ent)
3628 then
3629 Set_Trivial_Subprogram (Stm);
3630 end if;
3631 end;
3632 end if;
3633 end if;
3634 end;
3635 end;
3636
3637 -- Check for variables that are never modified
3638
3639 declare
3640 E1, E2 : Entity_Id;
3641
3642 begin
3643 -- If there is a separate spec, then transfer Never_Set_In_Source
3644 -- flags from out parameters to the corresponding entities in the
3645 -- body. The reason we do that is we want to post error flags on
3646 -- the body entities, not the spec entities.
3647
3648 if Present (Spec_Id) then
3649 E1 := First_Entity (Spec_Id);
3650 while Present (E1) loop
3651 if Ekind (E1) = E_Out_Parameter then
3652 E2 := First_Entity (Body_Id);
3653 while Present (E2) loop
3654 exit when Chars (E1) = Chars (E2);
3655 Next_Entity (E2);
3656 end loop;
3657
3658 if Present (E2) then
3659 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
3660 end if;
3661 end if;
3662
3663 Next_Entity (E1);
3664 end loop;
3665 end if;
3666
3667 -- Check references in body
3668
3669 Check_References (Body_Id);
3670 end;
3671 end Analyze_Subprogram_Body_Helper;
3672
3673 ---------------------------------
3674 -- Analyze_Subprogram_Contract --
3675 ---------------------------------
3676
3677 procedure Analyze_Subprogram_Contract (Subp : Entity_Id) is
3678 Items : constant Node_Id := Contract (Subp);
3679 Case_Prag : Node_Id := Empty;
3680 Depends : Node_Id := Empty;
3681 Global : Node_Id := Empty;
3682 Nam : Name_Id;
3683 Post_Prag : Node_Id := Empty;
3684 Prag : Node_Id;
3685 Seen_In_Case : Boolean := False;
3686 Seen_In_Post : Boolean := False;
3687
3688 begin
3689 if Present (Items) then
3690
3691 -- Analyze pre- and postconditions
3692
3693 Prag := Pre_Post_Conditions (Items);
3694 while Present (Prag) loop
3695 Analyze_Pre_Post_Condition_In_Decl_Part (Prag, Subp);
3696
3697 -- Verify whether a postcondition mentions attribute 'Result and
3698 -- its expression introduces a post-state.
3699
3700 if Warn_On_Suspicious_Contract
3701 and then Pragma_Name (Prag) = Name_Postcondition
3702 then
3703 Post_Prag := Prag;
3704 Check_Result_And_Post_State (Prag, Seen_In_Post);
3705 end if;
3706
3707 Prag := Next_Pragma (Prag);
3708 end loop;
3709
3710 -- Analyze contract-cases and test-cases
3711
3712 Prag := Contract_Test_Cases (Items);
3713 while Present (Prag) loop
3714 Nam := Pragma_Name (Prag);
3715
3716 if Nam = Name_Contract_Cases then
3717 Analyze_Contract_Cases_In_Decl_Part (Prag);
3718
3719 -- Verify whether contract-cases mention attribute 'Result and
3720 -- its expression introduces a post-state. Perform the check
3721 -- only when the pragma is legal.
3722
3723 if Warn_On_Suspicious_Contract
3724 and then not Error_Posted (Prag)
3725 then
3726 Case_Prag := Prag;
3727 Check_Result_And_Post_State (Prag, Seen_In_Case);
3728 end if;
3729
3730 else
3731 pragma Assert (Nam = Name_Test_Case);
3732 Analyze_Test_Case_In_Decl_Part (Prag, Subp);
3733 end if;
3734
3735 Prag := Next_Pragma (Prag);
3736 end loop;
3737
3738 -- Analyze classification pragmas
3739
3740 Prag := Classifications (Items);
3741 while Present (Prag) loop
3742 Nam := Pragma_Name (Prag);
3743
3744 if Nam = Name_Depends then
3745 Depends := Prag;
3746 else pragma Assert (Nam = Name_Global);
3747 Global := Prag;
3748 end if;
3749
3750 Prag := Next_Pragma (Prag);
3751 end loop;
3752
3753 -- Analyze Global first as Depends may mention items classified in
3754 -- the global categorization.
3755
3756 if Present (Global) then
3757 Analyze_Global_In_Decl_Part (Global);
3758 end if;
3759
3760 -- Depends must be analyzed after Global in order to see the modes of
3761 -- all global items.
3762
3763 if Present (Depends) then
3764 Analyze_Depends_In_Decl_Part (Depends);
3765 end if;
3766 end if;
3767
3768 -- Emit an error when neither the postconditions nor the contract-cases
3769 -- mention attribute 'Result in the context of a function.
3770
3771 if Warn_On_Suspicious_Contract
3772 and then Ekind_In (Subp, E_Function, E_Generic_Function)
3773 then
3774 if Present (Case_Prag)
3775 and then not Seen_In_Case
3776 and then Present (Post_Prag)
3777 and then not Seen_In_Post
3778 then
3779 Error_Msg_N
3780 ("neither function postcondition nor contract cases mention "
3781 & "result?T?", Post_Prag);
3782
3783 elsif Present (Case_Prag) and then not Seen_In_Case then
3784 Error_Msg_N
3785 ("contract cases do not mention result?T?", Case_Prag);
3786
3787 -- OK if we have at least one IN OUT parameter
3788
3789 elsif Present (Post_Prag) and then not Seen_In_Post then
3790 declare
3791 F : Entity_Id;
3792 begin
3793 F := First_Formal (Subp);
3794 while Present (F) loop
3795 if Ekind (F) = E_In_Out_Parameter then
3796 return;
3797 else
3798 Next_Formal (F);
3799 end if;
3800 end loop;
3801 end;
3802
3803 -- If no in-out parameters and no mention of Result, the contract
3804 -- is certainly suspicious.
3805
3806 Error_Msg_N
3807 ("function postcondition does not mention result?T?", Post_Prag);
3808 end if;
3809 end if;
3810 end Analyze_Subprogram_Contract;
3811
3812 ------------------------------------
3813 -- Analyze_Subprogram_Declaration --
3814 ------------------------------------
3815
3816 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
3817 Scop : constant Entity_Id := Current_Scope;
3818 Designator : Entity_Id;
3819
3820 Is_Completion : Boolean;
3821 -- Indicates whether a null procedure declaration is a completion
3822
3823 begin
3824 -- Null procedures are not allowed in SPARK
3825
3826 if Nkind (Specification (N)) = N_Procedure_Specification
3827 and then Null_Present (Specification (N))
3828 then
3829 Check_SPARK_Restriction ("null procedure is not allowed", N);
3830
3831 if Is_Protected_Type (Current_Scope) then
3832 Error_Msg_N ("protected operation cannot be a null procedure", N);
3833 end if;
3834
3835 Analyze_Null_Procedure (N, Is_Completion);
3836
3837 if Is_Completion then
3838
3839 -- The null procedure acts as a body, nothing further is needed.
3840
3841 return;
3842 end if;
3843 end if;
3844
3845 Designator := Analyze_Subprogram_Specification (Specification (N));
3846
3847 -- A reference may already have been generated for the unit name, in
3848 -- which case the following call is redundant. However it is needed for
3849 -- declarations that are the rewriting of an expression function.
3850
3851 Generate_Definition (Designator);
3852
3853 -- Set SPARK mode from current context (may be overwritten later with
3854 -- explicit pragma).
3855
3856 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
3857 Set_SPARK_Pragma_Inherited (Designator, True);
3858
3859 if Debug_Flag_C then
3860 Write_Str ("==> subprogram spec ");
3861 Write_Name (Chars (Designator));
3862 Write_Str (" from ");
3863 Write_Location (Sloc (N));
3864 Write_Eol;
3865 Indent;
3866 end if;
3867
3868 Validate_RCI_Subprogram_Declaration (N);
3869 New_Overloaded_Entity (Designator);
3870 Check_Delayed_Subprogram (Designator);
3871
3872 -- If the type of the first formal of the current subprogram is a non-
3873 -- generic tagged private type, mark the subprogram as being a private
3874 -- primitive. Ditto if this is a function with controlling result, and
3875 -- the return type is currently private. In both cases, the type of the
3876 -- controlling argument or result must be in the current scope for the
3877 -- operation to be primitive.
3878
3879 if Has_Controlling_Result (Designator)
3880 and then Is_Private_Type (Etype (Designator))
3881 and then Scope (Etype (Designator)) = Current_Scope
3882 and then not Is_Generic_Actual_Type (Etype (Designator))
3883 then
3884 Set_Is_Private_Primitive (Designator);
3885
3886 elsif Present (First_Formal (Designator)) then
3887 declare
3888 Formal_Typ : constant Entity_Id :=
3889 Etype (First_Formal (Designator));
3890 begin
3891 Set_Is_Private_Primitive (Designator,
3892 Is_Tagged_Type (Formal_Typ)
3893 and then Scope (Formal_Typ) = Current_Scope
3894 and then Is_Private_Type (Formal_Typ)
3895 and then not Is_Generic_Actual_Type (Formal_Typ));
3896 end;
3897 end if;
3898
3899 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
3900 -- or null.
3901
3902 if Ada_Version >= Ada_2005
3903 and then Comes_From_Source (N)
3904 and then Is_Dispatching_Operation (Designator)
3905 then
3906 declare
3907 E : Entity_Id;
3908 Etyp : Entity_Id;
3909
3910 begin
3911 if Has_Controlling_Result (Designator) then
3912 Etyp := Etype (Designator);
3913
3914 else
3915 E := First_Entity (Designator);
3916 while Present (E)
3917 and then Is_Formal (E)
3918 and then not Is_Controlling_Formal (E)
3919 loop
3920 Next_Entity (E);
3921 end loop;
3922
3923 Etyp := Etype (E);
3924 end if;
3925
3926 if Is_Access_Type (Etyp) then
3927 Etyp := Directly_Designated_Type (Etyp);
3928 end if;
3929
3930 if Is_Interface (Etyp)
3931 and then not Is_Abstract_Subprogram (Designator)
3932 and then not (Ekind (Designator) = E_Procedure
3933 and then Null_Present (Specification (N)))
3934 then
3935 Error_Msg_Name_1 := Chars (Defining_Entity (N));
3936
3937 -- Specialize error message based on procedures vs. functions,
3938 -- since functions can't be null subprograms.
3939
3940 if Ekind (Designator) = E_Procedure then
3941 Error_Msg_N
3942 ("interface procedure % must be abstract or null", N);
3943 else
3944 Error_Msg_N ("interface function % must be abstract", N);
3945 end if;
3946 end if;
3947 end;
3948 end if;
3949
3950 -- What is the following code for, it used to be
3951
3952 -- ??? Set_Suppress_Elaboration_Checks
3953 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3954
3955 -- The following seems equivalent, but a bit dubious
3956
3957 if Elaboration_Checks_Suppressed (Designator) then
3958 Set_Kill_Elaboration_Checks (Designator);
3959 end if;
3960
3961 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
3962 Set_Categorization_From_Scope (Designator, Scop);
3963
3964 else
3965 -- For a compilation unit, check for library-unit pragmas
3966
3967 Push_Scope (Designator);
3968 Set_Categorization_From_Pragmas (N);
3969 Validate_Categorization_Dependency (N, Designator);
3970 Pop_Scope;
3971 end if;
3972
3973 -- For a compilation unit, set body required. This flag will only be
3974 -- reset if a valid Import or Interface pragma is processed later on.
3975
3976 if Nkind (Parent (N)) = N_Compilation_Unit then
3977 Set_Body_Required (Parent (N), True);
3978
3979 if Ada_Version >= Ada_2005
3980 and then Nkind (Specification (N)) = N_Procedure_Specification
3981 and then Null_Present (Specification (N))
3982 then
3983 Error_Msg_N
3984 ("null procedure cannot be declared at library level", N);
3985 end if;
3986 end if;
3987
3988 Generate_Reference_To_Formals (Designator);
3989 Check_Eliminated (Designator);
3990
3991 if Debug_Flag_C then
3992 Outdent;
3993 Write_Str ("<== subprogram spec ");
3994 Write_Name (Chars (Designator));
3995 Write_Str (" from ");
3996 Write_Location (Sloc (N));
3997 Write_Eol;
3998 end if;
3999
4000 if Is_Protected_Type (Current_Scope) then
4001
4002 -- Indicate that this is a protected operation, because it may be
4003 -- used in subsequent declarations within the protected type.
4004
4005 Set_Convention (Designator, Convention_Protected);
4006 end if;
4007
4008 List_Inherited_Pre_Post_Aspects (Designator);
4009
4010 if Has_Aspects (N) then
4011 Analyze_Aspect_Specifications (N, Designator);
4012 end if;
4013 end Analyze_Subprogram_Declaration;
4014
4015 --------------------------------------
4016 -- Analyze_Subprogram_Specification --
4017 --------------------------------------
4018
4019 -- Reminder: N here really is a subprogram specification (not a subprogram
4020 -- declaration). This procedure is called to analyze the specification in
4021 -- both subprogram bodies and subprogram declarations (specs).
4022
4023 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4024 Designator : constant Entity_Id := Defining_Entity (N);
4025 Formals : constant List_Id := Parameter_Specifications (N);
4026
4027 -- Start of processing for Analyze_Subprogram_Specification
4028
4029 begin
4030 -- User-defined operator is not allowed in SPARK, except as a renaming
4031
4032 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4033 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4034 then
4035 Check_SPARK_Restriction ("user-defined operator is not allowed", N);
4036 end if;
4037
4038 -- Proceed with analysis. Do not emit a cross-reference entry if the
4039 -- specification comes from an expression function, because it may be
4040 -- the completion of a previous declaration. It is is not, the cross-
4041 -- reference entry will be emitted for the new subprogram declaration.
4042
4043 if Nkind (Parent (N)) /= N_Expression_Function then
4044 Generate_Definition (Designator);
4045 end if;
4046
4047 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
4048
4049 if Nkind (N) = N_Function_Specification then
4050 Set_Ekind (Designator, E_Function);
4051 Set_Mechanism (Designator, Default_Mechanism);
4052 else
4053 Set_Ekind (Designator, E_Procedure);
4054 Set_Etype (Designator, Standard_Void_Type);
4055 end if;
4056
4057 -- Introduce new scope for analysis of the formals and the return type
4058
4059 Set_Scope (Designator, Current_Scope);
4060
4061 if Present (Formals) then
4062 Push_Scope (Designator);
4063 Process_Formals (Formals, N);
4064
4065 -- Check dimensions in N for formals with default expression
4066
4067 Analyze_Dimension_Formals (N, Formals);
4068
4069 -- Ada 2005 (AI-345): If this is an overriding operation of an
4070 -- inherited interface operation, and the controlling type is
4071 -- a synchronized type, replace the type with its corresponding
4072 -- record, to match the proper signature of an overriding operation.
4073 -- Same processing for an access parameter whose designated type is
4074 -- derived from a synchronized interface.
4075
4076 if Ada_Version >= Ada_2005 then
4077 declare
4078 Formal : Entity_Id;
4079 Formal_Typ : Entity_Id;
4080 Rec_Typ : Entity_Id;
4081 Desig_Typ : Entity_Id;
4082
4083 begin
4084 Formal := First_Formal (Designator);
4085 while Present (Formal) loop
4086 Formal_Typ := Etype (Formal);
4087
4088 if Is_Concurrent_Type (Formal_Typ)
4089 and then Present (Corresponding_Record_Type (Formal_Typ))
4090 then
4091 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4092
4093 if Present (Interfaces (Rec_Typ)) then
4094 Set_Etype (Formal, Rec_Typ);
4095 end if;
4096
4097 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4098 Desig_Typ := Designated_Type (Formal_Typ);
4099
4100 if Is_Concurrent_Type (Desig_Typ)
4101 and then Present (Corresponding_Record_Type (Desig_Typ))
4102 then
4103 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4104
4105 if Present (Interfaces (Rec_Typ)) then
4106 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4107 end if;
4108 end if;
4109 end if;
4110
4111 Next_Formal (Formal);
4112 end loop;
4113 end;
4114 end if;
4115
4116 End_Scope;
4117
4118 -- The subprogram scope is pushed and popped around the processing of
4119 -- the return type for consistency with call above to Process_Formals
4120 -- (which itself can call Analyze_Return_Type), and to ensure that any
4121 -- itype created for the return type will be associated with the proper
4122 -- scope.
4123
4124 elsif Nkind (N) = N_Function_Specification then
4125 Push_Scope (Designator);
4126 Analyze_Return_Type (N);
4127 End_Scope;
4128 end if;
4129
4130 -- Function case
4131
4132 if Nkind (N) = N_Function_Specification then
4133
4134 -- Deal with operator symbol case
4135
4136 if Nkind (Designator) = N_Defining_Operator_Symbol then
4137 Valid_Operator_Definition (Designator);
4138 end if;
4139
4140 May_Need_Actuals (Designator);
4141
4142 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4143 -- the subprogram is abstract also. This does not apply to renaming
4144 -- declarations, where abstractness is inherited, and to subprogram
4145 -- bodies generated for stream operations, which become renamings as
4146 -- bodies.
4147
4148 -- In case of primitives associated with abstract interface types
4149 -- the check is applied later (see Analyze_Subprogram_Declaration).
4150
4151 if not Nkind_In (Original_Node (Parent (N)),
4152 N_Subprogram_Renaming_Declaration,
4153 N_Abstract_Subprogram_Declaration,
4154 N_Formal_Abstract_Subprogram_Declaration)
4155 then
4156 if Is_Abstract_Type (Etype (Designator))
4157 and then not Is_Interface (Etype (Designator))
4158 then
4159 Error_Msg_N
4160 ("function that returns abstract type must be abstract", N);
4161
4162 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4163 -- access result whose designated type is abstract.
4164
4165 elsif Nkind (Result_Definition (N)) = N_Access_Definition
4166 and then
4167 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4168 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4169 and then Ada_Version >= Ada_2012
4170 then
4171 Error_Msg_N ("function whose access result designates "
4172 & "abstract type must be abstract", N);
4173 end if;
4174 end if;
4175 end if;
4176
4177 return Designator;
4178 end Analyze_Subprogram_Specification;
4179
4180 --------------------------
4181 -- Build_Body_To_Inline --
4182 --------------------------
4183
4184 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
4185 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
4186 Original_Body : Node_Id;
4187 Body_To_Analyze : Node_Id;
4188 Max_Size : constant := 10;
4189 Stat_Count : Integer := 0;
4190
4191 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
4192 -- Check for declarations that make inlining not worthwhile
4193
4194 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
4195 -- Check for statements that make inlining not worthwhile: any tasking
4196 -- statement, nested at any level. Keep track of total number of
4197 -- elementary statements, as a measure of acceptable size.
4198
4199 function Has_Pending_Instantiation return Boolean;
4200 -- If some enclosing body contains instantiations that appear before the
4201 -- corresponding generic body, the enclosing body has a freeze node so
4202 -- that it can be elaborated after the generic itself. This might
4203 -- conflict with subsequent inlinings, so that it is unsafe to try to
4204 -- inline in such a case.
4205
4206 function Has_Single_Return return Boolean;
4207 -- In general we cannot inline functions that return unconstrained type.
4208 -- However, we can handle such functions if all return statements return
4209 -- a local variable that is the only declaration in the body of the
4210 -- function. In that case the call can be replaced by that local
4211 -- variable as is done for other inlined calls.
4212
4213 procedure Remove_Pragmas;
4214 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
4215 -- parameter has no meaning when the body is inlined and the formals
4216 -- are rewritten. Remove it from body to inline. The analysis of the
4217 -- non-inlined body will handle the pragma properly.
4218
4219 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
4220 -- If the body of the subprogram includes a call that returns an
4221 -- unconstrained type, the secondary stack is involved, and it
4222 -- is not worth inlining.
4223
4224 ------------------------------
4225 -- Has_Excluded_Declaration --
4226 ------------------------------
4227
4228 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
4229 D : Node_Id;
4230
4231 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4232 -- Nested subprograms make a given body ineligible for inlining, but
4233 -- we make an exception for instantiations of unchecked conversion.
4234 -- The body has not been analyzed yet, so check the name, and verify
4235 -- that the visible entity with that name is the predefined unit.
4236
4237 -----------------------------
4238 -- Is_Unchecked_Conversion --
4239 -----------------------------
4240
4241 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4242 Id : constant Node_Id := Name (D);
4243 Conv : Entity_Id;
4244
4245 begin
4246 if Nkind (Id) = N_Identifier
4247 and then Chars (Id) = Name_Unchecked_Conversion
4248 then
4249 Conv := Current_Entity (Id);
4250
4251 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
4252 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4253 then
4254 Conv := Current_Entity (Selector_Name (Id));
4255 else
4256 return False;
4257 end if;
4258
4259 return Present (Conv)
4260 and then Is_Predefined_File_Name
4261 (Unit_File_Name (Get_Source_Unit (Conv)))
4262 and then Is_Intrinsic_Subprogram (Conv);
4263 end Is_Unchecked_Conversion;
4264
4265 -- Start of processing for Has_Excluded_Declaration
4266
4267 begin
4268 D := First (Decls);
4269 while Present (D) loop
4270 if (Nkind (D) = N_Function_Instantiation
4271 and then not Is_Unchecked_Conversion (D))
4272 or else Nkind_In (D, N_Protected_Type_Declaration,
4273 N_Package_Declaration,
4274 N_Package_Instantiation,
4275 N_Subprogram_Body,
4276 N_Procedure_Instantiation,
4277 N_Task_Type_Declaration)
4278 then
4279 Cannot_Inline
4280 ("cannot inline & (non-allowed declaration)?", D, Subp);
4281 return True;
4282 end if;
4283
4284 Next (D);
4285 end loop;
4286
4287 return False;
4288 end Has_Excluded_Declaration;
4289
4290 ----------------------------
4291 -- Has_Excluded_Statement --
4292 ----------------------------
4293
4294 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4295 S : Node_Id;
4296 E : Node_Id;
4297
4298 begin
4299 S := First (Stats);
4300 while Present (S) loop
4301 Stat_Count := Stat_Count + 1;
4302
4303 if Nkind_In (S, N_Abort_Statement,
4304 N_Asynchronous_Select,
4305 N_Conditional_Entry_Call,
4306 N_Delay_Relative_Statement,
4307 N_Delay_Until_Statement,
4308 N_Selective_Accept,
4309 N_Timed_Entry_Call)
4310 then
4311 Cannot_Inline
4312 ("cannot inline & (non-allowed statement)?", S, Subp);
4313 return True;
4314
4315 elsif Nkind (S) = N_Block_Statement then
4316 if Present (Declarations (S))
4317 and then Has_Excluded_Declaration (Declarations (S))
4318 then
4319 return True;
4320
4321 elsif Present (Handled_Statement_Sequence (S))
4322 and then
4323 (Present
4324 (Exception_Handlers (Handled_Statement_Sequence (S)))
4325 or else
4326 Has_Excluded_Statement
4327 (Statements (Handled_Statement_Sequence (S))))
4328 then
4329 return True;
4330 end if;
4331
4332 elsif Nkind (S) = N_Case_Statement then
4333 E := First (Alternatives (S));
4334 while Present (E) loop
4335 if Has_Excluded_Statement (Statements (E)) then
4336 return True;
4337 end if;
4338
4339 Next (E);
4340 end loop;
4341
4342 elsif Nkind (S) = N_If_Statement then
4343 if Has_Excluded_Statement (Then_Statements (S)) then
4344 return True;
4345 end if;
4346
4347 if Present (Elsif_Parts (S)) then
4348 E := First (Elsif_Parts (S));
4349 while Present (E) loop
4350 if Has_Excluded_Statement (Then_Statements (E)) then
4351 return True;
4352 end if;
4353
4354 Next (E);
4355 end loop;
4356 end if;
4357
4358 if Present (Else_Statements (S))
4359 and then Has_Excluded_Statement (Else_Statements (S))
4360 then
4361 return True;
4362 end if;
4363
4364 elsif Nkind (S) = N_Loop_Statement
4365 and then Has_Excluded_Statement (Statements (S))
4366 then
4367 return True;
4368
4369 elsif Nkind (S) = N_Extended_Return_Statement then
4370 if Has_Excluded_Statement
4371 (Statements (Handled_Statement_Sequence (S)))
4372 or else Present
4373 (Exception_Handlers (Handled_Statement_Sequence (S)))
4374 then
4375 return True;
4376 end if;
4377 end if;
4378
4379 Next (S);
4380 end loop;
4381
4382 return False;
4383 end Has_Excluded_Statement;
4384
4385 -------------------------------
4386 -- Has_Pending_Instantiation --
4387 -------------------------------
4388
4389 function Has_Pending_Instantiation return Boolean is
4390 S : Entity_Id;
4391
4392 begin
4393 S := Current_Scope;
4394 while Present (S) loop
4395 if Is_Compilation_Unit (S)
4396 or else Is_Child_Unit (S)
4397 then
4398 return False;
4399
4400 elsif Ekind (S) = E_Package
4401 and then Has_Forward_Instantiation (S)
4402 then
4403 return True;
4404 end if;
4405
4406 S := Scope (S);
4407 end loop;
4408
4409 return False;
4410 end Has_Pending_Instantiation;
4411
4412 ------------------------
4413 -- Has_Single_Return --
4414 ------------------------
4415
4416 function Has_Single_Return return Boolean is
4417 Return_Statement : Node_Id := Empty;
4418
4419 function Check_Return (N : Node_Id) return Traverse_Result;
4420
4421 ------------------
4422 -- Check_Return --
4423 ------------------
4424
4425 function Check_Return (N : Node_Id) return Traverse_Result is
4426 begin
4427 if Nkind (N) = N_Simple_Return_Statement then
4428 if Present (Expression (N))
4429 and then Is_Entity_Name (Expression (N))
4430 then
4431 if No (Return_Statement) then
4432 Return_Statement := N;
4433 return OK;
4434
4435 elsif Chars (Expression (N)) =
4436 Chars (Expression (Return_Statement))
4437 then
4438 return OK;
4439
4440 else
4441 return Abandon;
4442 end if;
4443
4444 -- A return statement within an extended return is a noop
4445 -- after inlining.
4446
4447 elsif No (Expression (N))
4448 and then Nkind (Parent (Parent (N))) =
4449 N_Extended_Return_Statement
4450 then
4451 return OK;
4452
4453 else
4454 -- Expression has wrong form
4455
4456 return Abandon;
4457 end if;
4458
4459 -- We can only inline a build-in-place function if
4460 -- it has a single extended return.
4461
4462 elsif Nkind (N) = N_Extended_Return_Statement then
4463 if No (Return_Statement) then
4464 Return_Statement := N;
4465 return OK;
4466
4467 else
4468 return Abandon;
4469 end if;
4470
4471 else
4472 return OK;
4473 end if;
4474 end Check_Return;
4475
4476 function Check_All_Returns is new Traverse_Func (Check_Return);
4477
4478 -- Start of processing for Has_Single_Return
4479
4480 begin
4481 if Check_All_Returns (N) /= OK then
4482 return False;
4483
4484 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
4485 return True;
4486
4487 else
4488 return Present (Declarations (N))
4489 and then Present (First (Declarations (N)))
4490 and then Chars (Expression (Return_Statement)) =
4491 Chars (Defining_Identifier (First (Declarations (N))));
4492 end if;
4493 end Has_Single_Return;
4494
4495 --------------------
4496 -- Remove_Pragmas --
4497 --------------------
4498
4499 procedure Remove_Pragmas is
4500 Decl : Node_Id;
4501 Nxt : Node_Id;
4502
4503 begin
4504 Decl := First (Declarations (Body_To_Analyze));
4505 while Present (Decl) loop
4506 Nxt := Next (Decl);
4507
4508 if Nkind (Decl) = N_Pragma
4509 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
4510 Name_Unmodified)
4511 then
4512 Remove (Decl);
4513 end if;
4514
4515 Decl := Nxt;
4516 end loop;
4517 end Remove_Pragmas;
4518
4519 --------------------------
4520 -- Uses_Secondary_Stack --
4521 --------------------------
4522
4523 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
4524 function Check_Call (N : Node_Id) return Traverse_Result;
4525 -- Look for function calls that return an unconstrained type
4526
4527 ----------------
4528 -- Check_Call --
4529 ----------------
4530
4531 function Check_Call (N : Node_Id) return Traverse_Result is
4532 begin
4533 if Nkind (N) = N_Function_Call
4534 and then Is_Entity_Name (Name (N))
4535 and then Is_Composite_Type (Etype (Entity (Name (N))))
4536 and then not Is_Constrained (Etype (Entity (Name (N))))
4537 then
4538 Cannot_Inline
4539 ("cannot inline & (call returns unconstrained type)?",
4540 N, Subp);
4541 return Abandon;
4542 else
4543 return OK;
4544 end if;
4545 end Check_Call;
4546
4547 function Check_Calls is new Traverse_Func (Check_Call);
4548
4549 begin
4550 return Check_Calls (Bod) = Abandon;
4551 end Uses_Secondary_Stack;
4552
4553 -- Start of processing for Build_Body_To_Inline
4554
4555 begin
4556 -- Return immediately if done already
4557
4558 if Nkind (Decl) = N_Subprogram_Declaration
4559 and then Present (Body_To_Inline (Decl))
4560 then
4561 return;
4562
4563 -- Functions that return unconstrained composite types require
4564 -- secondary stack handling, and cannot currently be inlined, unless
4565 -- all return statements return a local variable that is the first
4566 -- local declaration in the body.
4567
4568 elsif Ekind (Subp) = E_Function
4569 and then not Is_Scalar_Type (Etype (Subp))
4570 and then not Is_Access_Type (Etype (Subp))
4571 and then not Is_Constrained (Etype (Subp))
4572 then
4573 if not Has_Single_Return then
4574 Cannot_Inline
4575 ("cannot inline & (unconstrained return type)?", N, Subp);
4576 return;
4577 end if;
4578
4579 -- Ditto for functions that return controlled types, where controlled
4580 -- actions interfere in complex ways with inlining.
4581
4582 elsif Ekind (Subp) = E_Function
4583 and then Needs_Finalization (Etype (Subp))
4584 then
4585 Cannot_Inline
4586 ("cannot inline & (controlled return type)?", N, Subp);
4587 return;
4588 end if;
4589
4590 if Present (Declarations (N))
4591 and then Has_Excluded_Declaration (Declarations (N))
4592 then
4593 return;
4594 end if;
4595
4596 if Present (Handled_Statement_Sequence (N)) then
4597 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
4598 Cannot_Inline
4599 ("cannot inline& (exception handler)?",
4600 First (Exception_Handlers (Handled_Statement_Sequence (N))),
4601 Subp);
4602 return;
4603 elsif
4604 Has_Excluded_Statement
4605 (Statements (Handled_Statement_Sequence (N)))
4606 then
4607 return;
4608 end if;
4609 end if;
4610
4611 -- We do not inline a subprogram that is too large, unless it is
4612 -- marked Inline_Always. This pragma does not suppress the other
4613 -- checks on inlining (forbidden declarations, handlers, etc).
4614
4615 if Stat_Count > Max_Size
4616 and then not Has_Pragma_Inline_Always (Subp)
4617 then
4618 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
4619 return;
4620 end if;
4621
4622 if Has_Pending_Instantiation then
4623 Cannot_Inline
4624 ("cannot inline& (forward instance within enclosing body)?",
4625 N, Subp);
4626 return;
4627 end if;
4628
4629 -- Within an instance, the body to inline must be treated as a nested
4630 -- generic, so that the proper global references are preserved.
4631
4632 -- Note that we do not do this at the library level, because it is not
4633 -- needed, and furthermore this causes trouble if front end inlining
4634 -- is activated (-gnatN).
4635
4636 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4637 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4638 Original_Body := Copy_Generic_Node (N, Empty, True);
4639 else
4640 Original_Body := Copy_Separate_Tree (N);
4641 end if;
4642
4643 -- We need to capture references to the formals in order to substitute
4644 -- the actuals at the point of inlining, i.e. instantiation. To treat
4645 -- the formals as globals to the body to inline, we nest it within
4646 -- a dummy parameterless subprogram, declared within the real one.
4647 -- To avoid generating an internal name (which is never public, and
4648 -- which affects serial numbers of other generated names), we use
4649 -- an internal symbol that cannot conflict with user declarations.
4650
4651 Set_Parameter_Specifications (Specification (Original_Body), No_List);
4652 Set_Defining_Unit_Name
4653 (Specification (Original_Body),
4654 Make_Defining_Identifier (Sloc (N), Name_uParent));
4655 Set_Corresponding_Spec (Original_Body, Empty);
4656
4657 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4658
4659 -- Set return type of function, which is also global and does not need
4660 -- to be resolved.
4661
4662 if Ekind (Subp) = E_Function then
4663 Set_Result_Definition (Specification (Body_To_Analyze),
4664 New_Occurrence_Of (Etype (Subp), Sloc (N)));
4665 end if;
4666
4667 if No (Declarations (N)) then
4668 Set_Declarations (N, New_List (Body_To_Analyze));
4669 else
4670 Append (Body_To_Analyze, Declarations (N));
4671 end if;
4672
4673 Expander_Mode_Save_And_Set (False);
4674 Remove_Pragmas;
4675
4676 Analyze (Body_To_Analyze);
4677 Push_Scope (Defining_Entity (Body_To_Analyze));
4678 Save_Global_References (Original_Body);
4679 End_Scope;
4680 Remove (Body_To_Analyze);
4681
4682 Expander_Mode_Restore;
4683
4684 -- Restore environment if previously saved
4685
4686 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4687 Restore_Env;
4688 end if;
4689
4690 -- If secondary stk used there is no point in inlining. We have
4691 -- already issued the warning in this case, so nothing to do.
4692
4693 if Uses_Secondary_Stack (Body_To_Analyze) then
4694 return;
4695 end if;
4696
4697 Set_Body_To_Inline (Decl, Original_Body);
4698 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
4699 Set_Is_Inlined (Subp);
4700 end Build_Body_To_Inline;
4701
4702 -------------------
4703 -- Cannot_Inline --
4704 -------------------
4705
4706 procedure Cannot_Inline
4707 (Msg : String;
4708 N : Node_Id;
4709 Subp : Entity_Id;
4710 Is_Serious : Boolean := False)
4711 is
4712 begin
4713 pragma Assert (Msg (Msg'Last) = '?');
4714
4715 -- Old semantics
4716
4717 if not Debug_Flag_Dot_K then
4718
4719 -- Do not emit warning if this is a predefined unit which is not
4720 -- the main unit. With validity checks enabled, some predefined
4721 -- subprograms may contain nested subprograms and become ineligible
4722 -- for inlining.
4723
4724 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4725 and then not In_Extended_Main_Source_Unit (Subp)
4726 then
4727 null;
4728
4729 elsif Has_Pragma_Inline_Always (Subp) then
4730
4731 -- Remove last character (question mark) to make this into an
4732 -- error, because the Inline_Always pragma cannot be obeyed.
4733
4734 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4735
4736 elsif Ineffective_Inline_Warnings then
4737 Error_Msg_NE (Msg & "p?", N, Subp);
4738 end if;
4739
4740 return;
4741
4742 -- New semantics
4743
4744 elsif Is_Serious then
4745
4746 -- Remove last character (question mark) to make this into an error.
4747
4748 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4749
4750 elsif Optimization_Level = 0 then
4751
4752 -- Do not emit warning if this is a predefined unit which is not
4753 -- the main unit. This behavior is currently provided for backward
4754 -- compatibility but it will be removed when we enforce the
4755 -- strictness of the new rules.
4756
4757 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4758 and then not In_Extended_Main_Source_Unit (Subp)
4759 then
4760 null;
4761
4762 elsif Has_Pragma_Inline_Always (Subp) then
4763
4764 -- Emit a warning if this is a call to a runtime subprogram
4765 -- which is located inside a generic. Previously this call
4766 -- was silently skipped.
4767
4768 if Is_Generic_Instance (Subp) then
4769 declare
4770 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
4771 begin
4772 if Is_Predefined_File_Name
4773 (Unit_File_Name (Get_Source_Unit (Gen_P)))
4774 then
4775 Set_Is_Inlined (Subp, False);
4776 Error_Msg_NE (Msg & "p?", N, Subp);
4777 return;
4778 end if;
4779 end;
4780 end if;
4781
4782 -- Remove last character (question mark) to make this into an
4783 -- error, because the Inline_Always pragma cannot be obeyed.
4784
4785 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4786
4787 else pragma Assert (Front_End_Inlining);
4788 Set_Is_Inlined (Subp, False);
4789
4790 -- When inlining cannot take place we must issue an error.
4791 -- For backward compatibility we still report a warning.
4792
4793 if Ineffective_Inline_Warnings then
4794 Error_Msg_NE (Msg & "p?", N, Subp);
4795 end if;
4796 end if;
4797
4798 -- Compiling with optimizations enabled it is too early to report
4799 -- problems since the backend may still perform inlining. In order
4800 -- to report unhandled inlinings the program must be compiled with
4801 -- -Winline and the error is reported by the backend.
4802
4803 else
4804 null;
4805 end if;
4806 end Cannot_Inline;
4807
4808 ------------------------------------
4809 -- Check_And_Build_Body_To_Inline --
4810 ------------------------------------
4811
4812 procedure Check_And_Build_Body_To_Inline
4813 (N : Node_Id;
4814 Spec_Id : Entity_Id;
4815 Body_Id : Entity_Id)
4816 is
4817 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
4818 -- Use generic machinery to build an unexpanded body for the subprogram.
4819 -- This body is subsequently used for inline expansions at call sites.
4820
4821 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
4822 -- Return true if we generate code for the function body N, the function
4823 -- body N has no local declarations and its unique statement is a single
4824 -- extended return statement with a handled statements sequence.
4825
4826 function Check_Body_To_Inline
4827 (N : Node_Id;
4828 Subp : Entity_Id) return Boolean;
4829 -- N is the N_Subprogram_Body of Subp. Return true if Subp can be
4830 -- inlined by the frontend. These are the rules:
4831 -- * At -O0 use fe inlining when inline_always is specified except if
4832 -- the function returns a controlled type.
4833 -- * At other optimization levels use the fe inlining for both inline
4834 -- and inline_always in the following cases:
4835 -- - function returning a known at compile time constant
4836 -- - function returning a call to an intrinsic function
4837 -- - function returning an unconstrained type (see Can_Split
4838 -- Unconstrained_Function).
4839 -- - function returning a call to a frontend-inlined function
4840 -- Use the back-end mechanism otherwise
4841 --
4842 -- In addition, in the following cases the function cannot be inlined by
4843 -- the frontend:
4844 -- - functions that uses the secondary stack
4845 -- - functions that have declarations of:
4846 -- - Concurrent types
4847 -- - Packages
4848 -- - Instantiations
4849 -- - Subprograms
4850 -- - functions that have some of the following statements:
4851 -- - abort
4852 -- - asynchronous-select
4853 -- - conditional-entry-call
4854 -- - delay-relative
4855 -- - delay-until
4856 -- - selective-accept
4857 -- - timed-entry-call
4858 -- - functions that have exception handlers
4859 -- - functions that have some enclosing body containing instantiations
4860 -- that appear before the corresponding generic body.
4861
4862 procedure Generate_Body_To_Inline
4863 (N : Node_Id;
4864 Body_To_Inline : out Node_Id);
4865 -- Generate a parameterless duplicate of subprogram body N. Occurrences
4866 -- of pragmas referencing the formals are removed since they have no
4867 -- meaning when the body is inlined and the formals are rewritten (the
4868 -- analysis of the non-inlined body will handle these pragmas properly).
4869 -- A new internal name is associated with Body_To_Inline.
4870
4871 procedure Split_Unconstrained_Function
4872 (N : Node_Id;
4873 Spec_Id : Entity_Id);
4874 -- N is an inlined function body that returns an unconstrained type and
4875 -- has a single extended return statement. Split N in two subprograms:
4876 -- a procedure P' and a function F'. The formals of P' duplicate the
4877 -- formals of N plus an extra formal which is used return a value;
4878 -- its body is composed by the declarations and list of statements
4879 -- of the extended return statement of N.
4880
4881 --------------------------
4882 -- Build_Body_To_Inline --
4883 --------------------------
4884
4885 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
4886 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
4887 Original_Body : Node_Id;
4888 Body_To_Analyze : Node_Id;
4889
4890 begin
4891 pragma Assert (Current_Scope = Spec_Id);
4892
4893 -- Within an instance, the body to inline must be treated as a nested
4894 -- generic, so that the proper global references are preserved. We
4895 -- do not do this at the library level, because it is not needed, and
4896 -- furthermore this causes trouble if front end inlining is activated
4897 -- (-gnatN).
4898
4899 if In_Instance
4900 and then Scope (Current_Scope) /= Standard_Standard
4901 then
4902 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4903 end if;
4904
4905 -- We need to capture references to the formals in order
4906 -- to substitute the actuals at the point of inlining, i.e.
4907 -- instantiation. To treat the formals as globals to the body to
4908 -- inline, we nest it within a dummy parameterless subprogram,
4909 -- declared within the real one.
4910
4911 Generate_Body_To_Inline (N, Original_Body);
4912 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4913
4914 -- Set return type of function, which is also global and does not
4915 -- need to be resolved.
4916
4917 if Ekind (Spec_Id) = E_Function then
4918 Set_Result_Definition (Specification (Body_To_Analyze),
4919 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
4920 end if;
4921
4922 if No (Declarations (N)) then
4923 Set_Declarations (N, New_List (Body_To_Analyze));
4924 else
4925 Append_To (Declarations (N), Body_To_Analyze);
4926 end if;
4927
4928 Preanalyze (Body_To_Analyze);
4929
4930 Push_Scope (Defining_Entity (Body_To_Analyze));
4931 Save_Global_References (Original_Body);
4932 End_Scope;
4933 Remove (Body_To_Analyze);
4934
4935 -- Restore environment if previously saved
4936
4937 if In_Instance
4938 and then Scope (Current_Scope) /= Standard_Standard
4939 then
4940 Restore_Env;
4941 end if;
4942
4943 pragma Assert (No (Body_To_Inline (Decl)));
4944 Set_Body_To_Inline (Decl, Original_Body);
4945 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
4946 end Build_Body_To_Inline;
4947
4948 --------------------------
4949 -- Check_Body_To_Inline --
4950 --------------------------
4951
4952 function Check_Body_To_Inline
4953 (N : Node_Id;
4954 Subp : Entity_Id) return Boolean
4955 is
4956 Max_Size : constant := 10;
4957 Stat_Count : Integer := 0;
4958
4959 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
4960 -- Check for declarations that make inlining not worthwhile
4961
4962 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
4963 -- Check for statements that make inlining not worthwhile: any
4964 -- tasking statement, nested at any level. Keep track of total
4965 -- number of elementary statements, as a measure of acceptable size.
4966
4967 function Has_Pending_Instantiation return Boolean;
4968 -- Return True if some enclosing body contains instantiations that
4969 -- appear before the corresponding generic body.
4970
4971 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean;
4972 -- Return True if all the return statements of the function body N
4973 -- are simple return statements and return a compile time constant
4974
4975 function Returns_Intrinsic_Function_Call (N : Node_Id) return Boolean;
4976 -- Return True if all the return statements of the function body N
4977 -- are simple return statements and return an intrinsic function call
4978
4979 function Uses_Secondary_Stack (N : Node_Id) return Boolean;
4980 -- If the body of the subprogram includes a call that returns an
4981 -- unconstrained type, the secondary stack is involved, and it
4982 -- is not worth inlining.
4983
4984 ------------------------------
4985 -- Has_Excluded_Declaration --
4986 ------------------------------
4987
4988 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
4989 D : Node_Id;
4990
4991 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4992 -- Nested subprograms make a given body ineligible for inlining,
4993 -- but we make an exception for instantiations of unchecked
4994 -- conversion. The body has not been analyzed yet, so check the
4995 -- name, and verify that the visible entity with that name is the
4996 -- predefined unit.
4997
4998 -----------------------------
4999 -- Is_Unchecked_Conversion --
5000 -----------------------------
5001
5002 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
5003 Id : constant Node_Id := Name (D);
5004 Conv : Entity_Id;
5005
5006 begin
5007 if Nkind (Id) = N_Identifier
5008 and then Chars (Id) = Name_Unchecked_Conversion
5009 then
5010 Conv := Current_Entity (Id);
5011
5012 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
5013 and then
5014 Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
5015 then
5016 Conv := Current_Entity (Selector_Name (Id));
5017 else
5018 return False;
5019 end if;
5020
5021 return Present (Conv)
5022 and then Is_Predefined_File_Name
5023 (Unit_File_Name (Get_Source_Unit (Conv)))
5024 and then Is_Intrinsic_Subprogram (Conv);
5025 end Is_Unchecked_Conversion;
5026
5027 -- Start of processing for Has_Excluded_Declaration
5028
5029 begin
5030 D := First (Decls);
5031 while Present (D) loop
5032 if (Nkind (D) = N_Function_Instantiation
5033 and then not Is_Unchecked_Conversion (D))
5034 or else Nkind_In (D, N_Protected_Type_Declaration,
5035 N_Package_Declaration,
5036 N_Package_Instantiation,
5037 N_Subprogram_Body,
5038 N_Procedure_Instantiation,
5039 N_Task_Type_Declaration)
5040 then
5041 Cannot_Inline
5042 ("cannot inline & (non-allowed declaration)?", D, Subp);
5043
5044 return True;
5045 end if;
5046
5047 Next (D);
5048 end loop;
5049
5050 return False;
5051 end Has_Excluded_Declaration;
5052
5053 ----------------------------
5054 -- Has_Excluded_Statement --
5055 ----------------------------
5056
5057 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
5058 S : Node_Id;
5059 E : Node_Id;
5060
5061 begin
5062 S := First (Stats);
5063 while Present (S) loop
5064 Stat_Count := Stat_Count + 1;
5065
5066 if Nkind_In (S, N_Abort_Statement,
5067 N_Asynchronous_Select,
5068 N_Conditional_Entry_Call,
5069 N_Delay_Relative_Statement,
5070 N_Delay_Until_Statement,
5071 N_Selective_Accept,
5072 N_Timed_Entry_Call)
5073 then
5074 Cannot_Inline
5075 ("cannot inline & (non-allowed statement)?", S, Subp);
5076 return True;
5077
5078 elsif Nkind (S) = N_Block_Statement then
5079 if Present (Declarations (S))
5080 and then Has_Excluded_Declaration (Declarations (S))
5081 then
5082 return True;
5083
5084 elsif Present (Handled_Statement_Sequence (S)) then
5085 if Present
5086 (Exception_Handlers (Handled_Statement_Sequence (S)))
5087 then
5088 Cannot_Inline
5089 ("cannot inline& (exception handler)?",
5090 First (Exception_Handlers
5091 (Handled_Statement_Sequence (S))),
5092 Subp);
5093 return True;
5094
5095 elsif Has_Excluded_Statement
5096 (Statements (Handled_Statement_Sequence (S)))
5097 then
5098 return True;
5099 end if;
5100 end if;
5101
5102 elsif Nkind (S) = N_Case_Statement then
5103 E := First (Alternatives (S));
5104 while Present (E) loop
5105 if Has_Excluded_Statement (Statements (E)) then
5106 return True;
5107 end if;
5108
5109 Next (E);
5110 end loop;
5111
5112 elsif Nkind (S) = N_If_Statement then
5113 if Has_Excluded_Statement (Then_Statements (S)) then
5114 return True;
5115 end if;
5116
5117 if Present (Elsif_Parts (S)) then
5118 E := First (Elsif_Parts (S));
5119 while Present (E) loop
5120 if Has_Excluded_Statement (Then_Statements (E)) then
5121 return True;
5122 end if;
5123 Next (E);
5124 end loop;
5125 end if;
5126
5127 if Present (Else_Statements (S))
5128 and then Has_Excluded_Statement (Else_Statements (S))
5129 then
5130 return True;
5131 end if;
5132
5133 elsif Nkind (S) = N_Loop_Statement
5134 and then Has_Excluded_Statement (Statements (S))
5135 then
5136 return True;
5137
5138 elsif Nkind (S) = N_Extended_Return_Statement then
5139 if Present (Handled_Statement_Sequence (S))
5140 and then
5141 Has_Excluded_Statement
5142 (Statements (Handled_Statement_Sequence (S)))
5143 then
5144 return True;
5145
5146 elsif Present (Handled_Statement_Sequence (S))
5147 and then
5148 Present (Exception_Handlers
5149 (Handled_Statement_Sequence (S)))
5150 then
5151 Cannot_Inline
5152 ("cannot inline& (exception handler)?",
5153 First (Exception_Handlers
5154 (Handled_Statement_Sequence (S))),
5155 Subp);
5156 return True;
5157 end if;
5158 end if;
5159
5160 Next (S);
5161 end loop;
5162
5163 return False;
5164 end Has_Excluded_Statement;
5165
5166 -------------------------------
5167 -- Has_Pending_Instantiation --
5168 -------------------------------
5169
5170 function Has_Pending_Instantiation return Boolean is
5171 S : Entity_Id;
5172
5173 begin
5174 S := Current_Scope;
5175 while Present (S) loop
5176 if Is_Compilation_Unit (S)
5177 or else Is_Child_Unit (S)
5178 then
5179 return False;
5180
5181 elsif Ekind (S) = E_Package
5182 and then Has_Forward_Instantiation (S)
5183 then
5184 return True;
5185 end if;
5186
5187 S := Scope (S);
5188 end loop;
5189
5190 return False;
5191 end Has_Pending_Instantiation;
5192
5193 ------------------------------------
5194 -- Returns_Compile_Time_Constant --
5195 ------------------------------------
5196
5197 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean is
5198
5199 function Check_Return (N : Node_Id) return Traverse_Result;
5200
5201 ------------------
5202 -- Check_Return --
5203 ------------------
5204
5205 function Check_Return (N : Node_Id) return Traverse_Result is
5206 begin
5207 if Nkind (N) = N_Extended_Return_Statement then
5208 return Abandon;
5209
5210 elsif Nkind (N) = N_Simple_Return_Statement then
5211 if Present (Expression (N)) then
5212 declare
5213 Orig_Expr : constant Node_Id :=
5214 Original_Node (Expression (N));
5215
5216 begin
5217 if Nkind_In (Orig_Expr, N_Integer_Literal,
5218 N_Real_Literal,
5219 N_Character_Literal)
5220 then
5221 return OK;
5222
5223 elsif Is_Entity_Name (Orig_Expr)
5224 and then Ekind (Entity (Orig_Expr)) = E_Constant
5225 and then Is_Static_Expression (Orig_Expr)
5226 then
5227 return OK;
5228 else
5229 return Abandon;
5230 end if;
5231 end;
5232
5233 -- Expression has wrong form
5234
5235 else
5236 return Abandon;
5237 end if;
5238
5239 -- Continue analyzing statements
5240
5241 else
5242 return OK;
5243 end if;
5244 end Check_Return;
5245
5246 function Check_All_Returns is new Traverse_Func (Check_Return);
5247
5248 -- Start of processing for Returns_Compile_Time_Constant
5249
5250 begin
5251 return Check_All_Returns (N) = OK;
5252 end Returns_Compile_Time_Constant;
5253
5254 --------------------------------------
5255 -- Returns_Intrinsic_Function_Call --
5256 --------------------------------------
5257
5258 function Returns_Intrinsic_Function_Call
5259 (N : Node_Id) return Boolean
5260 is
5261 function Check_Return (N : Node_Id) return Traverse_Result;
5262
5263 ------------------
5264 -- Check_Return --
5265 ------------------
5266
5267 function Check_Return (N : Node_Id) return Traverse_Result is
5268 begin
5269 if Nkind (N) = N_Extended_Return_Statement then
5270 return Abandon;
5271
5272 elsif Nkind (N) = N_Simple_Return_Statement then
5273 if Present (Expression (N)) then
5274 declare
5275 Orig_Expr : constant Node_Id :=
5276 Original_Node (Expression (N));
5277
5278 begin
5279 if Nkind (Orig_Expr) in N_Op
5280 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5281 then
5282 return OK;
5283
5284 elsif Nkind (Orig_Expr) in N_Has_Entity
5285 and then Present (Entity (Orig_Expr))
5286 and then Ekind (Entity (Orig_Expr)) = E_Function
5287 and then Is_Inlined (Entity (Orig_Expr))
5288 then
5289 return OK;
5290
5291 elsif Nkind (Orig_Expr) in N_Has_Entity
5292 and then Present (Entity (Orig_Expr))
5293 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5294 then
5295 return OK;
5296
5297 else
5298 return Abandon;
5299 end if;
5300 end;
5301
5302 -- Expression has wrong form
5303
5304 else
5305 return Abandon;
5306 end if;
5307
5308 -- Continue analyzing statements
5309
5310 else
5311 return OK;
5312 end if;
5313 end Check_Return;
5314
5315 function Check_All_Returns is new Traverse_Func (Check_Return);
5316
5317 -- Start of processing for Returns_Intrinsic_Function_Call
5318
5319 begin
5320 return Check_All_Returns (N) = OK;
5321 end Returns_Intrinsic_Function_Call;
5322
5323 --------------------------
5324 -- Uses_Secondary_Stack --
5325 --------------------------
5326
5327 function Uses_Secondary_Stack (N : Node_Id) return Boolean is
5328
5329 function Check_Call (N : Node_Id) return Traverse_Result;
5330 -- Look for function calls that return an unconstrained type
5331
5332 ----------------
5333 -- Check_Call --
5334 ----------------
5335
5336 function Check_Call (N : Node_Id) return Traverse_Result is
5337 begin
5338 if Nkind (N) = N_Function_Call
5339 and then Is_Entity_Name (Name (N))
5340 and then Is_Composite_Type (Etype (Entity (Name (N))))
5341 and then not Is_Constrained (Etype (Entity (Name (N))))
5342 then
5343 Cannot_Inline
5344 ("cannot inline & (call returns unconstrained type)?",
5345 N, Subp);
5346
5347 return Abandon;
5348 else
5349 return OK;
5350 end if;
5351 end Check_Call;
5352
5353 function Check_Calls is new Traverse_Func (Check_Call);
5354
5355 -- Start of processing for Uses_Secondary_Stack
5356
5357 begin
5358 return Check_Calls (N) = Abandon;
5359 end Uses_Secondary_Stack;
5360
5361 -- Local variables
5362
5363 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
5364 May_Inline : constant Boolean :=
5365 Has_Pragma_Inline_Always (Spec_Id)
5366 or else (Has_Pragma_Inline (Spec_Id)
5367 and then ((Optimization_Level > 0
5368 and then Ekind (Spec_Id)
5369 = E_Function)
5370 or else Front_End_Inlining));
5371 Body_To_Analyze : Node_Id;
5372
5373 -- Start of processing for Check_Body_To_Inline
5374
5375 begin
5376 -- No action needed in stubs since the attribute Body_To_Inline
5377 -- is not available
5378
5379 if Nkind (Decl) = N_Subprogram_Body_Stub then
5380 return False;
5381
5382 -- Cannot build the body to inline if the attribute is already set.
5383 -- This attribute may have been set if this is a subprogram renaming
5384 -- declarations (see Freeze.Build_Renamed_Body).
5385
5386 elsif Present (Body_To_Inline (Decl)) then
5387 return False;
5388
5389 -- No action needed if the subprogram does not fulfill the minimum
5390 -- conditions to be inlined by the frontend
5391
5392 elsif not May_Inline then
5393 return False;
5394 end if;
5395
5396 -- Check excluded declarations
5397
5398 if Present (Declarations (N))
5399 and then Has_Excluded_Declaration (Declarations (N))
5400 then
5401 return False;
5402 end if;
5403
5404 -- Check excluded statements
5405
5406 if Present (Handled_Statement_Sequence (N)) then
5407 if Present
5408 (Exception_Handlers (Handled_Statement_Sequence (N)))
5409 then
5410 Cannot_Inline
5411 ("cannot inline& (exception handler)?",
5412 First
5413 (Exception_Handlers (Handled_Statement_Sequence (N))),
5414 Subp);
5415
5416 return False;
5417
5418 elsif Has_Excluded_Statement
5419 (Statements (Handled_Statement_Sequence (N)))
5420 then
5421 return False;
5422 end if;
5423 end if;
5424
5425 -- For backward compatibility, compiling under -gnatN we do not
5426 -- inline a subprogram that is too large, unless it is marked
5427 -- Inline_Always. This pragma does not suppress the other checks
5428 -- on inlining (forbidden declarations, handlers, etc).
5429
5430 if Front_End_Inlining
5431 and then not Has_Pragma_Inline_Always (Subp)
5432 and then Stat_Count > Max_Size
5433 then
5434 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
5435 return False;
5436 end if;
5437
5438 -- If some enclosing body contains instantiations that appear before
5439 -- the corresponding generic body, the enclosing body has a freeze
5440 -- node so that it can be elaborated after the generic itself. This
5441 -- might conflict with subsequent inlinings, so that it is unsafe to
5442 -- try to inline in such a case.
5443
5444 if Has_Pending_Instantiation then
5445 Cannot_Inline
5446 ("cannot inline& (forward instance within enclosing body)?",
5447 N, Subp);
5448
5449 return False;
5450 end if;
5451
5452 -- Generate and preanalyze the body to inline (needed to perform
5453 -- the rest of the checks)
5454
5455 Generate_Body_To_Inline (N, Body_To_Analyze);
5456
5457 if Ekind (Subp) = E_Function then
5458 Set_Result_Definition (Specification (Body_To_Analyze),
5459 New_Occurrence_Of (Etype (Subp), Sloc (N)));
5460 end if;
5461
5462 -- Nest the body to analyze within the real one
5463
5464 if No (Declarations (N)) then
5465 Set_Declarations (N, New_List (Body_To_Analyze));
5466 else
5467 Append_To (Declarations (N), Body_To_Analyze);
5468 end if;
5469
5470 Preanalyze (Body_To_Analyze);
5471 Remove (Body_To_Analyze);
5472
5473 -- Keep separate checks needed when compiling without optimizations
5474
5475 if Optimization_Level = 0
5476
5477 -- AAMP and VM targets have no support for inlining in the backend
5478 -- and hence we use frontend inlining at all optimization levels.
5479
5480 or else AAMP_On_Target
5481 or else VM_Target /= No_VM
5482 then
5483 -- Cannot inline functions whose body has a call that returns an
5484 -- unconstrained type since the secondary stack is involved, and
5485 -- it is not worth inlining.
5486
5487 if Uses_Secondary_Stack (Body_To_Analyze) then
5488 return False;
5489
5490 -- Cannot inline functions that return controlled types since
5491 -- controlled actions interfere in complex ways with inlining.
5492
5493 elsif Ekind (Subp) = E_Function
5494 and then Needs_Finalization (Etype (Subp))
5495 then
5496 Cannot_Inline
5497 ("cannot inline & (controlled return type)?", N, Subp);
5498 return False;
5499
5500 elsif Returns_Unconstrained_Type (Subp) then
5501 Cannot_Inline
5502 ("cannot inline & (unconstrained return type)?", N, Subp);
5503 return False;
5504 end if;
5505
5506 -- Compiling with optimizations enabled
5507
5508 else
5509 -- Procedures are never frontend inlined in this case
5510
5511 if Ekind (Subp) /= E_Function then
5512 return False;
5513
5514 -- Functions returning unconstrained types are tested
5515 -- separately (see Can_Split_Unconstrained_Function).
5516
5517 elsif Returns_Unconstrained_Type (Subp) then
5518 null;
5519
5520 -- Check supported cases
5521
5522 elsif not Returns_Compile_Time_Constant (Body_To_Analyze)
5523 and then Convention (Subp) /= Convention_Intrinsic
5524 and then not Returns_Intrinsic_Function_Call (Body_To_Analyze)
5525 then
5526 return False;
5527 end if;
5528 end if;
5529
5530 return True;
5531 end Check_Body_To_Inline;
5532
5533 --------------------------------------
5534 -- Can_Split_Unconstrained_Function --
5535 --------------------------------------
5536
5537 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean
5538 is
5539 Ret_Node : constant Node_Id :=
5540 First (Statements (Handled_Statement_Sequence (N)));
5541 D : Node_Id;
5542
5543 begin
5544 -- No user defined declarations allowed in the function except inside
5545 -- the unique return statement; implicit labels are the only allowed
5546 -- declarations.
5547
5548 if not Is_Empty_List (Declarations (N)) then
5549 D := First (Declarations (N));
5550 while Present (D) loop
5551 if Nkind (D) /= N_Implicit_Label_Declaration then
5552 return False;
5553 end if;
5554
5555 Next (D);
5556 end loop;
5557 end if;
5558
5559 -- We only split the inlined function when we are generating the code
5560 -- of its body; otherwise we leave duplicated split subprograms in
5561 -- the tree which (if referenced) generate wrong references at link
5562 -- time.
5563
5564 return In_Extended_Main_Code_Unit (N)
5565 and then Present (Ret_Node)
5566 and then Nkind (Ret_Node) = N_Extended_Return_Statement
5567 and then No (Next (Ret_Node))
5568 and then Present (Handled_Statement_Sequence (Ret_Node));
5569 end Can_Split_Unconstrained_Function;
5570
5571 -----------------------------
5572 -- Generate_Body_To_Inline --
5573 -----------------------------
5574
5575 procedure Generate_Body_To_Inline
5576 (N : Node_Id;
5577 Body_To_Inline : out Node_Id)
5578 is
5579 procedure Remove_Pragmas (N : Node_Id);
5580 -- Remove occurrences of pragmas that may reference the formals of
5581 -- N. The analysis of the non-inlined body will handle these pragmas
5582 -- properly.
5583
5584 --------------------
5585 -- Remove_Pragmas --
5586 --------------------
5587
5588 procedure Remove_Pragmas (N : Node_Id) is
5589 Decl : Node_Id;
5590 Nxt : Node_Id;
5591
5592 begin
5593 Decl := First (Declarations (N));
5594 while Present (Decl) loop
5595 Nxt := Next (Decl);
5596
5597 if Nkind (Decl) = N_Pragma
5598 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
5599 Name_Unmodified)
5600 then
5601 Remove (Decl);
5602 end if;
5603
5604 Decl := Nxt;
5605 end loop;
5606 end Remove_Pragmas;
5607
5608 -- Start of processing for Generate_Body_To_Inline
5609
5610 begin
5611 -- Within an instance, the body to inline must be treated as a nested
5612 -- generic, so that the proper global references are preserved.
5613
5614 -- Note that we do not do this at the library level, because it
5615 -- is not needed, and furthermore this causes trouble if front
5616 -- end inlining is activated (-gnatN).
5617
5618 if In_Instance
5619 and then Scope (Current_Scope) /= Standard_Standard
5620 then
5621 Body_To_Inline := Copy_Generic_Node (N, Empty, True);
5622 else
5623 Body_To_Inline := Copy_Separate_Tree (N);
5624 end if;
5625
5626 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
5627 -- parameter has no meaning when the body is inlined and the formals
5628 -- are rewritten. Remove it from body to inline. The analysis of the
5629 -- non-inlined body will handle the pragma properly.
5630
5631 Remove_Pragmas (Body_To_Inline);
5632
5633 -- We need to capture references to the formals in order
5634 -- to substitute the actuals at the point of inlining, i.e.
5635 -- instantiation. To treat the formals as globals to the body to
5636 -- inline, we nest it within a dummy parameterless subprogram,
5637 -- declared within the real one.
5638
5639 Set_Parameter_Specifications
5640 (Specification (Body_To_Inline), No_List);
5641
5642 -- A new internal name is associated with Body_To_Inline to avoid
5643 -- conflicts when the non-inlined body N is analyzed.
5644
5645 Set_Defining_Unit_Name (Specification (Body_To_Inline),
5646 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
5647 Set_Corresponding_Spec (Body_To_Inline, Empty);
5648 end Generate_Body_To_Inline;
5649
5650 ----------------------------------
5651 -- Split_Unconstrained_Function --
5652 ----------------------------------
5653
5654 procedure Split_Unconstrained_Function
5655 (N : Node_Id;
5656 Spec_Id : Entity_Id)
5657 is
5658 Loc : constant Source_Ptr := Sloc (N);
5659 Ret_Node : constant Node_Id :=
5660 First (Statements (Handled_Statement_Sequence (N)));
5661 Ret_Obj : constant Node_Id :=
5662 First (Return_Object_Declarations (Ret_Node));
5663
5664 procedure Build_Procedure
5665 (Proc_Id : out Entity_Id;
5666 Decl_List : out List_Id);
5667 -- Build a procedure containing the statements found in the extended
5668 -- return statement of the unconstrained function body N.
5669
5670 procedure Build_Procedure
5671 (Proc_Id : out Entity_Id;
5672 Decl_List : out List_Id)
5673 is
5674 Formal : Entity_Id;
5675 Formal_List : constant List_Id := New_List;
5676 Proc_Spec : Node_Id;
5677 Proc_Body : Node_Id;
5678 Subp_Name : constant Name_Id := New_Internal_Name ('F');
5679 Body_Decl_List : List_Id := No_List;
5680 Param_Type : Node_Id;
5681
5682 begin
5683 if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then
5684 Param_Type := New_Copy (Object_Definition (Ret_Obj));
5685 else
5686 Param_Type :=
5687 New_Copy (Subtype_Mark (Object_Definition (Ret_Obj)));
5688 end if;
5689
5690 Append_To (Formal_List,
5691 Make_Parameter_Specification (Loc,
5692 Defining_Identifier =>
5693 Make_Defining_Identifier (Loc,
5694 Chars => Chars (Defining_Identifier (Ret_Obj))),
5695 In_Present => False,
5696 Out_Present => True,
5697 Null_Exclusion_Present => False,
5698 Parameter_Type => Param_Type));
5699
5700 Formal := First_Formal (Spec_Id);
5701 while Present (Formal) loop
5702 Append_To (Formal_List,
5703 Make_Parameter_Specification (Loc,
5704 Defining_Identifier =>
5705 Make_Defining_Identifier (Sloc (Formal),
5706 Chars => Chars (Formal)),
5707 In_Present => In_Present (Parent (Formal)),
5708 Out_Present => Out_Present (Parent (Formal)),
5709 Null_Exclusion_Present =>
5710 Null_Exclusion_Present (Parent (Formal)),
5711 Parameter_Type =>
5712 New_Occurrence_Of (Etype (Formal), Loc),
5713 Expression =>
5714 Copy_Separate_Tree (Expression (Parent (Formal)))));
5715
5716 Next_Formal (Formal);
5717 end loop;
5718
5719 Proc_Id :=
5720 Make_Defining_Identifier (Loc, Chars => Subp_Name);
5721
5722 Proc_Spec :=
5723 Make_Procedure_Specification (Loc,
5724 Defining_Unit_Name => Proc_Id,
5725 Parameter_Specifications => Formal_List);
5726
5727 Decl_List := New_List;
5728
5729 Append_To (Decl_List,
5730 Make_Subprogram_Declaration (Loc, Proc_Spec));
5731
5732 -- Can_Convert_Unconstrained_Function checked that the function
5733 -- has no local declarations except implicit label declarations.
5734 -- Copy these declarations to the built procedure.
5735
5736 if Present (Declarations (N)) then
5737 Body_Decl_List := New_List;
5738
5739 declare
5740 D : Node_Id;
5741 New_D : Node_Id;
5742
5743 begin
5744 D := First (Declarations (N));
5745 while Present (D) loop
5746 pragma Assert (Nkind (D) = N_Implicit_Label_Declaration);
5747
5748 New_D :=
5749 Make_Implicit_Label_Declaration (Loc,
5750 Make_Defining_Identifier (Loc,
5751 Chars => Chars (Defining_Identifier (D))),
5752 Label_Construct => Empty);
5753 Append_To (Body_Decl_List, New_D);
5754
5755 Next (D);
5756 end loop;
5757 end;
5758 end if;
5759
5760 pragma Assert (Present (Handled_Statement_Sequence (Ret_Node)));
5761
5762 Proc_Body :=
5763 Make_Subprogram_Body (Loc,
5764 Specification => Copy_Separate_Tree (Proc_Spec),
5765 Declarations => Body_Decl_List,
5766 Handled_Statement_Sequence =>
5767 Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node)));
5768
5769 Set_Defining_Unit_Name (Specification (Proc_Body),
5770 Make_Defining_Identifier (Loc, Subp_Name));
5771
5772 Append_To (Decl_List, Proc_Body);
5773 end Build_Procedure;
5774
5775 -- Local variables
5776
5777 New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj);
5778 Blk_Stmt : Node_Id;
5779 Proc_Id : Entity_Id;
5780 Proc_Call : Node_Id;
5781
5782 -- Start of processing for Split_Unconstrained_Function
5783
5784 begin
5785 -- Build the associated procedure, analyze it and insert it before
5786 -- the function body N
5787
5788 declare
5789 Scope : constant Entity_Id := Current_Scope;
5790 Decl_List : List_Id;
5791 begin
5792 Pop_Scope;
5793 Build_Procedure (Proc_Id, Decl_List);
5794 Insert_Actions (N, Decl_List);
5795 Push_Scope (Scope);
5796 end;
5797
5798 -- Build the call to the generated procedure
5799
5800 declare
5801 Actual_List : constant List_Id := New_List;
5802 Formal : Entity_Id;
5803
5804 begin
5805 Append_To (Actual_List,
5806 New_Occurrence_Of (Defining_Identifier (New_Obj), Loc));
5807
5808 Formal := First_Formal (Spec_Id);
5809 while Present (Formal) loop
5810 Append_To (Actual_List, New_Occurrence_Of (Formal, Loc));
5811
5812 -- Avoid spurious warning on unreferenced formals
5813
5814 Set_Referenced (Formal);
5815 Next_Formal (Formal);
5816 end loop;
5817
5818 Proc_Call :=
5819 Make_Procedure_Call_Statement (Loc,
5820 Name => New_Occurrence_Of (Proc_Id, Loc),
5821 Parameter_Associations => Actual_List);
5822 end;
5823
5824 -- Generate
5825
5826 -- declare
5827 -- New_Obj : ...
5828 -- begin
5829 -- main_1__F1b (New_Obj, ...);
5830 -- return Obj;
5831 -- end B10b;
5832
5833 Blk_Stmt :=
5834 Make_Block_Statement (Loc,
5835 Declarations => New_List (New_Obj),
5836 Handled_Statement_Sequence =>
5837 Make_Handled_Sequence_Of_Statements (Loc,
5838 Statements => New_List (
5839
5840 Proc_Call,
5841
5842 Make_Simple_Return_Statement (Loc,
5843 Expression =>
5844 New_Occurrence_Of
5845 (Defining_Identifier (New_Obj), Loc)))));
5846
5847 Rewrite (Ret_Node, Blk_Stmt);
5848 end Split_Unconstrained_Function;
5849
5850 -- Start of processing for Check_And_Build_Body_To_Inline
5851
5852 begin
5853 -- Do not inline any subprogram that contains nested subprograms, since
5854 -- the backend inlining circuit seems to generate uninitialized
5855 -- references in this case. We know this happens in the case of front
5856 -- end ZCX support, but it also appears it can happen in other cases as
5857 -- well. The backend often rejects attempts to inline in the case of
5858 -- nested procedures anyway, so little if anything is lost by this.
5859 -- Note that this is test is for the benefit of the back-end. There is
5860 -- a separate test for front-end inlining that also rejects nested
5861 -- subprograms.
5862
5863 -- Do not do this test if errors have been detected, because in some
5864 -- error cases, this code blows up, and we don't need it anyway if
5865 -- there have been errors, since we won't get to the linker anyway.
5866
5867 if Comes_From_Source (Body_Id)
5868 and then (Has_Pragma_Inline_Always (Spec_Id)
5869 or else Optimization_Level > 0)
5870 and then Serious_Errors_Detected = 0
5871 then
5872 declare
5873 P_Ent : Node_Id;
5874
5875 begin
5876 P_Ent := Body_Id;
5877 loop
5878 P_Ent := Scope (P_Ent);
5879 exit when No (P_Ent) or else P_Ent = Standard_Standard;
5880
5881 if Is_Subprogram (P_Ent) then
5882 Set_Is_Inlined (P_Ent, False);
5883
5884 if Comes_From_Source (P_Ent)
5885 and then Has_Pragma_Inline (P_Ent)
5886 then
5887 Cannot_Inline
5888 ("cannot inline& (nested subprogram)?", N, P_Ent,
5889 Is_Serious => True);
5890 end if;
5891 end if;
5892 end loop;
5893 end;
5894 end if;
5895
5896 -- Build the body to inline only if really needed
5897
5898 if Check_Body_To_Inline (N, Spec_Id)
5899 and then Serious_Errors_Detected = 0
5900 then
5901 if Returns_Unconstrained_Type (Spec_Id) then
5902 if Can_Split_Unconstrained_Function (N) then
5903 Split_Unconstrained_Function (N, Spec_Id);
5904 Build_Body_To_Inline (N, Spec_Id);
5905 Set_Is_Inlined (Spec_Id);
5906 end if;
5907 else
5908 Build_Body_To_Inline (N, Spec_Id);
5909 Set_Is_Inlined (Spec_Id);
5910 end if;
5911 end if;
5912 end Check_And_Build_Body_To_Inline;
5913
5914 -----------------------
5915 -- Check_Conformance --
5916 -----------------------
5917
5918 procedure Check_Conformance
5919 (New_Id : Entity_Id;
5920 Old_Id : Entity_Id;
5921 Ctype : Conformance_Type;
5922 Errmsg : Boolean;
5923 Conforms : out Boolean;
5924 Err_Loc : Node_Id := Empty;
5925 Get_Inst : Boolean := False;
5926 Skip_Controlling_Formals : Boolean := False)
5927 is
5928 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5929 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5930 -- If Errmsg is True, then processing continues to post an error message
5931 -- for conformance error on given node. Two messages are output. The
5932 -- first message points to the previous declaration with a general "no
5933 -- conformance" message. The second is the detailed reason, supplied as
5934 -- Msg. The parameter N provide information for a possible & insertion
5935 -- in the message, and also provides the location for posting the
5936 -- message in the absence of a specified Err_Loc location.
5937
5938 -----------------------
5939 -- Conformance_Error --
5940 -----------------------
5941
5942 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5943 Enode : Node_Id;
5944
5945 begin
5946 Conforms := False;
5947
5948 if Errmsg then
5949 if No (Err_Loc) then
5950 Enode := N;
5951 else
5952 Enode := Err_Loc;
5953 end if;
5954
5955 Error_Msg_Sloc := Sloc (Old_Id);
5956
5957 case Ctype is
5958 when Type_Conformant =>
5959 Error_Msg_N -- CODEFIX
5960 ("not type conformant with declaration#!", Enode);
5961
5962 when Mode_Conformant =>
5963 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5964 Error_Msg_N
5965 ("not mode conformant with operation inherited#!",
5966 Enode);
5967 else
5968 Error_Msg_N
5969 ("not mode conformant with declaration#!", Enode);
5970 end if;
5971
5972 when Subtype_Conformant =>
5973 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5974 Error_Msg_N
5975 ("not subtype conformant with operation inherited#!",
5976 Enode);
5977 else
5978 Error_Msg_N
5979 ("not subtype conformant with declaration#!", Enode);
5980 end if;
5981
5982 when Fully_Conformant =>
5983 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5984 Error_Msg_N -- CODEFIX
5985 ("not fully conformant with operation inherited#!",
5986 Enode);
5987 else
5988 Error_Msg_N -- CODEFIX
5989 ("not fully conformant with declaration#!", Enode);
5990 end if;
5991 end case;
5992
5993 Error_Msg_NE (Msg, Enode, N);
5994 end if;
5995 end Conformance_Error;
5996
5997 -- Local Variables
5998
5999 Old_Type : constant Entity_Id := Etype (Old_Id);
6000 New_Type : constant Entity_Id := Etype (New_Id);
6001 Old_Formal : Entity_Id;
6002 New_Formal : Entity_Id;
6003 Access_Types_Match : Boolean;
6004 Old_Formal_Base : Entity_Id;
6005 New_Formal_Base : Entity_Id;
6006
6007 -- Start of processing for Check_Conformance
6008
6009 begin
6010 Conforms := True;
6011
6012 -- We need a special case for operators, since they don't appear
6013 -- explicitly.
6014
6015 if Ctype = Type_Conformant then
6016 if Ekind (New_Id) = E_Operator
6017 and then Operator_Matches_Spec (New_Id, Old_Id)
6018 then
6019 return;
6020 end if;
6021 end if;
6022
6023 -- If both are functions/operators, check return types conform
6024
6025 if Old_Type /= Standard_Void_Type
6026 and then New_Type /= Standard_Void_Type
6027 then
6028
6029 -- If we are checking interface conformance we omit controlling
6030 -- arguments and result, because we are only checking the conformance
6031 -- of the remaining parameters.
6032
6033 if Has_Controlling_Result (Old_Id)
6034 and then Has_Controlling_Result (New_Id)
6035 and then Skip_Controlling_Formals
6036 then
6037 null;
6038
6039 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
6040 if Ctype >= Subtype_Conformant
6041 and then not Predicates_Match (Old_Type, New_Type)
6042 then
6043 Conformance_Error
6044 ("\predicate of return type does not match!", New_Id);
6045 else
6046 Conformance_Error
6047 ("\return type does not match!", New_Id);
6048 end if;
6049
6050 return;
6051 end if;
6052
6053 -- Ada 2005 (AI-231): In case of anonymous access types check the
6054 -- null-exclusion and access-to-constant attributes match.
6055
6056 if Ada_Version >= Ada_2005
6057 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
6058 and then
6059 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
6060 or else Is_Access_Constant (Etype (Old_Type)) /=
6061 Is_Access_Constant (Etype (New_Type)))
6062 then
6063 Conformance_Error ("\return type does not match!", New_Id);
6064 return;
6065 end if;
6066
6067 -- If either is a function/operator and the other isn't, error
6068
6069 elsif Old_Type /= Standard_Void_Type
6070 or else New_Type /= Standard_Void_Type
6071 then
6072 Conformance_Error ("\functions can only match functions!", New_Id);
6073 return;
6074 end if;
6075
6076 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
6077 -- If this is a renaming as body, refine error message to indicate that
6078 -- the conflict is with the original declaration. If the entity is not
6079 -- frozen, the conventions don't have to match, the one of the renamed
6080 -- entity is inherited.
6081
6082 if Ctype >= Subtype_Conformant then
6083 if Convention (Old_Id) /= Convention (New_Id) then
6084 if not Is_Frozen (New_Id) then
6085 null;
6086
6087 elsif Present (Err_Loc)
6088 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
6089 and then Present (Corresponding_Spec (Err_Loc))
6090 then
6091 Error_Msg_Name_1 := Chars (New_Id);
6092 Error_Msg_Name_2 :=
6093 Name_Ada + Convention_Id'Pos (Convention (New_Id));
6094 Conformance_Error ("\prior declaration for% has convention %!");
6095
6096 else
6097 Conformance_Error ("\calling conventions do not match!");
6098 end if;
6099
6100 return;
6101
6102 elsif Is_Formal_Subprogram (Old_Id)
6103 or else Is_Formal_Subprogram (New_Id)
6104 then
6105 Conformance_Error ("\formal subprograms not allowed!");
6106 return;
6107 end if;
6108 end if;
6109
6110 -- Deal with parameters
6111
6112 -- Note: we use the entity information, rather than going directly
6113 -- to the specification in the tree. This is not only simpler, but
6114 -- absolutely necessary for some cases of conformance tests between
6115 -- operators, where the declaration tree simply does not exist.
6116
6117 Old_Formal := First_Formal (Old_Id);
6118 New_Formal := First_Formal (New_Id);
6119 while Present (Old_Formal) and then Present (New_Formal) loop
6120 if Is_Controlling_Formal (Old_Formal)
6121 and then Is_Controlling_Formal (New_Formal)
6122 and then Skip_Controlling_Formals
6123 then
6124 -- The controlling formals will have different types when
6125 -- comparing an interface operation with its match, but both
6126 -- or neither must be access parameters.
6127
6128 if Is_Access_Type (Etype (Old_Formal))
6129 =
6130 Is_Access_Type (Etype (New_Formal))
6131 then
6132 goto Skip_Controlling_Formal;
6133 else
6134 Conformance_Error
6135 ("\access parameter does not match!", New_Formal);
6136 end if;
6137 end if;
6138
6139 -- Ada 2012: Mode conformance also requires that formal parameters
6140 -- be both aliased, or neither.
6141
6142 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
6143 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
6144 Conformance_Error
6145 ("\aliased parameter mismatch!", New_Formal);
6146 end if;
6147 end if;
6148
6149 if Ctype = Fully_Conformant then
6150
6151 -- Names must match. Error message is more accurate if we do
6152 -- this before checking that the types of the formals match.
6153
6154 if Chars (Old_Formal) /= Chars (New_Formal) then
6155 Conformance_Error ("\name & does not match!", New_Formal);
6156
6157 -- Set error posted flag on new formal as well to stop
6158 -- junk cascaded messages in some cases.
6159
6160 Set_Error_Posted (New_Formal);
6161 return;
6162 end if;
6163
6164 -- Null exclusion must match
6165
6166 if Null_Exclusion_Present (Parent (Old_Formal))
6167 /=
6168 Null_Exclusion_Present (Parent (New_Formal))
6169 then
6170 -- Only give error if both come from source. This should be
6171 -- investigated some time, since it should not be needed ???
6172
6173 if Comes_From_Source (Old_Formal)
6174 and then
6175 Comes_From_Source (New_Formal)
6176 then
6177 Conformance_Error
6178 ("\null exclusion for & does not match", New_Formal);
6179
6180 -- Mark error posted on the new formal to avoid duplicated
6181 -- complaint about types not matching.
6182
6183 Set_Error_Posted (New_Formal);
6184 end if;
6185 end if;
6186 end if;
6187
6188 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
6189 -- case occurs whenever a subprogram is being renamed and one of its
6190 -- parameters imposes a null exclusion. For example:
6191
6192 -- type T is null record;
6193 -- type Acc_T is access T;
6194 -- subtype Acc_T_Sub is Acc_T;
6195
6196 -- procedure P (Obj : not null Acc_T_Sub); -- itype
6197 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
6198 -- renames P;
6199
6200 Old_Formal_Base := Etype (Old_Formal);
6201 New_Formal_Base := Etype (New_Formal);
6202
6203 if Get_Inst then
6204 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
6205 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
6206 end if;
6207
6208 Access_Types_Match := Ada_Version >= Ada_2005
6209
6210 -- Ensure that this rule is only applied when New_Id is a
6211 -- renaming of Old_Id.
6212
6213 and then Nkind (Parent (Parent (New_Id))) =
6214 N_Subprogram_Renaming_Declaration
6215 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
6216 and then Present (Entity (Name (Parent (Parent (New_Id)))))
6217 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
6218
6219 -- Now handle the allowed access-type case
6220
6221 and then Is_Access_Type (Old_Formal_Base)
6222 and then Is_Access_Type (New_Formal_Base)
6223
6224 -- The type kinds must match. The only exception occurs with
6225 -- multiple generics of the form:
6226
6227 -- generic generic
6228 -- type F is private; type A is private;
6229 -- type F_Ptr is access F; type A_Ptr is access A;
6230 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
6231 -- package F_Pack is ... package A_Pack is
6232 -- package F_Inst is
6233 -- new F_Pack (A, A_Ptr, A_P);
6234
6235 -- When checking for conformance between the parameters of A_P
6236 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
6237 -- because the compiler has transformed A_Ptr into a subtype of
6238 -- F_Ptr. We catch this case in the code below.
6239
6240 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
6241 or else
6242 (Is_Generic_Type (Old_Formal_Base)
6243 and then Is_Generic_Type (New_Formal_Base)
6244 and then Is_Internal (New_Formal_Base)
6245 and then Etype (Etype (New_Formal_Base)) =
6246 Old_Formal_Base))
6247 and then Directly_Designated_Type (Old_Formal_Base) =
6248 Directly_Designated_Type (New_Formal_Base)
6249 and then ((Is_Itype (Old_Formal_Base)
6250 and then Can_Never_Be_Null (Old_Formal_Base))
6251 or else
6252 (Is_Itype (New_Formal_Base)
6253 and then Can_Never_Be_Null (New_Formal_Base)));
6254
6255 -- Types must always match. In the visible part of an instance,
6256 -- usual overloading rules for dispatching operations apply, and
6257 -- we check base types (not the actual subtypes).
6258
6259 if In_Instance_Visible_Part
6260 and then Is_Dispatching_Operation (New_Id)
6261 then
6262 if not Conforming_Types
6263 (T1 => Base_Type (Etype (Old_Formal)),
6264 T2 => Base_Type (Etype (New_Formal)),
6265 Ctype => Ctype,
6266 Get_Inst => Get_Inst)
6267 and then not Access_Types_Match
6268 then
6269 Conformance_Error ("\type of & does not match!", New_Formal);
6270 return;
6271 end if;
6272
6273 elsif not Conforming_Types
6274 (T1 => Old_Formal_Base,
6275 T2 => New_Formal_Base,
6276 Ctype => Ctype,
6277 Get_Inst => Get_Inst)
6278 and then not Access_Types_Match
6279 then
6280 -- Don't give error message if old type is Any_Type. This test
6281 -- avoids some cascaded errors, e.g. in case of a bad spec.
6282
6283 if Errmsg and then Old_Formal_Base = Any_Type then
6284 Conforms := False;
6285 else
6286 if Ctype >= Subtype_Conformant
6287 and then
6288 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
6289 then
6290 Conformance_Error
6291 ("\predicate of & does not match!", New_Formal);
6292 else
6293 Conformance_Error
6294 ("\type of & does not match!", New_Formal);
6295 end if;
6296 end if;
6297
6298 return;
6299 end if;
6300
6301 -- For mode conformance, mode must match
6302
6303 if Ctype >= Mode_Conformant then
6304 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
6305 if not Ekind_In (New_Id, E_Function, E_Procedure)
6306 or else not Is_Primitive_Wrapper (New_Id)
6307 then
6308 Conformance_Error ("\mode of & does not match!", New_Formal);
6309
6310 else
6311 declare
6312 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
6313 begin
6314 if Is_Protected_Type
6315 (Corresponding_Concurrent_Type (T))
6316 then
6317 Error_Msg_PT (T, New_Id);
6318 else
6319 Conformance_Error
6320 ("\mode of & does not match!", New_Formal);
6321 end if;
6322 end;
6323 end if;
6324
6325 return;
6326
6327 -- Part of mode conformance for access types is having the same
6328 -- constant modifier.
6329
6330 elsif Access_Types_Match
6331 and then Is_Access_Constant (Old_Formal_Base) /=
6332 Is_Access_Constant (New_Formal_Base)
6333 then
6334 Conformance_Error
6335 ("\constant modifier does not match!", New_Formal);
6336 return;
6337 end if;
6338 end if;
6339
6340 if Ctype >= Subtype_Conformant then
6341
6342 -- Ada 2005 (AI-231): In case of anonymous access types check
6343 -- the null-exclusion and access-to-constant attributes must
6344 -- match. For null exclusion, we test the types rather than the
6345 -- formals themselves, since the attribute is only set reliably
6346 -- on the formals in the Ada 95 case, and we exclude the case
6347 -- where Old_Formal is marked as controlling, to avoid errors
6348 -- when matching completing bodies with dispatching declarations
6349 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6350
6351 if Ada_Version >= Ada_2005
6352 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
6353 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
6354 and then
6355 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
6356 Can_Never_Be_Null (Etype (New_Formal))
6357 and then
6358 not Is_Controlling_Formal (Old_Formal))
6359 or else
6360 Is_Access_Constant (Etype (Old_Formal)) /=
6361 Is_Access_Constant (Etype (New_Formal)))
6362
6363 -- Do not complain if error already posted on New_Formal. This
6364 -- avoids some redundant error messages.
6365
6366 and then not Error_Posted (New_Formal)
6367 then
6368 -- It is allowed to omit the null-exclusion in case of stream
6369 -- attribute subprograms. We recognize stream subprograms
6370 -- through their TSS-generated suffix.
6371
6372 declare
6373 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6374
6375 begin
6376 if TSS_Name /= TSS_Stream_Read
6377 and then TSS_Name /= TSS_Stream_Write
6378 and then TSS_Name /= TSS_Stream_Input
6379 and then TSS_Name /= TSS_Stream_Output
6380 then
6381 -- Here we have a definite conformance error. It is worth
6382 -- special casing the error message for the case of a
6383 -- controlling formal (which excludes null).
6384
6385 if Is_Controlling_Formal (New_Formal) then
6386 Error_Msg_Node_2 := Scope (New_Formal);
6387 Conformance_Error
6388 ("\controlling formal& of& excludes null, "
6389 & "declaration must exclude null as well",
6390 New_Formal);
6391
6392 -- Normal case (couldn't we give more detail here???)
6393
6394 else
6395 Conformance_Error
6396 ("\type of & does not match!", New_Formal);
6397 end if;
6398
6399 return;
6400 end if;
6401 end;
6402 end if;
6403 end if;
6404
6405 -- Full conformance checks
6406
6407 if Ctype = Fully_Conformant then
6408
6409 -- We have checked already that names match
6410
6411 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6412
6413 -- Check default expressions for in parameters
6414
6415 declare
6416 NewD : constant Boolean :=
6417 Present (Default_Value (New_Formal));
6418 OldD : constant Boolean :=
6419 Present (Default_Value (Old_Formal));
6420 begin
6421 if NewD or OldD then
6422
6423 -- The old default value has been analyzed because the
6424 -- current full declaration will have frozen everything
6425 -- before. The new default value has not been analyzed,
6426 -- so analyze it now before we check for conformance.
6427
6428 if NewD then
6429 Push_Scope (New_Id);
6430 Preanalyze_Spec_Expression
6431 (Default_Value (New_Formal), Etype (New_Formal));
6432 End_Scope;
6433 end if;
6434
6435 if not (NewD and OldD)
6436 or else not Fully_Conformant_Expressions
6437 (Default_Value (Old_Formal),
6438 Default_Value (New_Formal))
6439 then
6440 Conformance_Error
6441 ("\default expression for & does not match!",
6442 New_Formal);
6443 return;
6444 end if;
6445 end if;
6446 end;
6447 end if;
6448 end if;
6449
6450 -- A couple of special checks for Ada 83 mode. These checks are
6451 -- skipped if either entity is an operator in package Standard,
6452 -- or if either old or new instance is not from the source program.
6453
6454 if Ada_Version = Ada_83
6455 and then Sloc (Old_Id) > Standard_Location
6456 and then Sloc (New_Id) > Standard_Location
6457 and then Comes_From_Source (Old_Id)
6458 and then Comes_From_Source (New_Id)
6459 then
6460 declare
6461 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6462 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6463
6464 begin
6465 -- Explicit IN must be present or absent in both cases. This
6466 -- test is required only in the full conformance case.
6467
6468 if In_Present (Old_Param) /= In_Present (New_Param)
6469 and then Ctype = Fully_Conformant
6470 then
6471 Conformance_Error
6472 ("\(Ada 83) IN must appear in both declarations",
6473 New_Formal);
6474 return;
6475 end if;
6476
6477 -- Grouping (use of comma in param lists) must be the same
6478 -- This is where we catch a misconformance like:
6479
6480 -- A, B : Integer
6481 -- A : Integer; B : Integer
6482
6483 -- which are represented identically in the tree except
6484 -- for the setting of the flags More_Ids and Prev_Ids.
6485
6486 if More_Ids (Old_Param) /= More_Ids (New_Param)
6487 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6488 then
6489 Conformance_Error
6490 ("\grouping of & does not match!", New_Formal);
6491 return;
6492 end if;
6493 end;
6494 end if;
6495
6496 -- This label is required when skipping controlling formals
6497
6498 <<Skip_Controlling_Formal>>
6499
6500 Next_Formal (Old_Formal);
6501 Next_Formal (New_Formal);
6502 end loop;
6503
6504 if Present (Old_Formal) then
6505 Conformance_Error ("\too few parameters!");
6506 return;
6507
6508 elsif Present (New_Formal) then
6509 Conformance_Error ("\too many parameters!", New_Formal);
6510 return;
6511 end if;
6512 end Check_Conformance;
6513
6514 -----------------------
6515 -- Check_Conventions --
6516 -----------------------
6517
6518 procedure Check_Conventions (Typ : Entity_Id) is
6519 Ifaces_List : Elist_Id;
6520
6521 procedure Check_Convention (Op : Entity_Id);
6522 -- Verify that the convention of inherited dispatching operation Op is
6523 -- consistent among all subprograms it overrides. In order to minimize
6524 -- the search, Search_From is utilized to designate a specific point in
6525 -- the list rather than iterating over the whole list once more.
6526
6527 ----------------------
6528 -- Check_Convention --
6529 ----------------------
6530
6531 procedure Check_Convention (Op : Entity_Id) is
6532 function Convention_Of (Id : Entity_Id) return Convention_Id;
6533 -- Given an entity, return its convention. The function treats Ghost
6534 -- as convention Ada because the two have the same dynamic semantics.
6535
6536 -------------------
6537 -- Convention_Of --
6538 -------------------
6539
6540 function Convention_Of (Id : Entity_Id) return Convention_Id is
6541 Conv : constant Convention_Id := Convention (Id);
6542 begin
6543 if Conv = Convention_Ghost then
6544 return Convention_Ada;
6545 else
6546 return Conv;
6547 end if;
6548 end Convention_Of;
6549
6550 -- Local variables
6551
6552 Op_Conv : constant Convention_Id := Convention_Of (Op);
6553 Iface_Conv : Convention_Id;
6554 Iface_Elmt : Elmt_Id;
6555 Iface_Prim_Elmt : Elmt_Id;
6556 Iface_Prim : Entity_Id;
6557
6558 -- Start of processing for Check_Convention
6559
6560 begin
6561 Iface_Elmt := First_Elmt (Ifaces_List);
6562 while Present (Iface_Elmt) loop
6563 Iface_Prim_Elmt :=
6564 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6565 while Present (Iface_Prim_Elmt) loop
6566 Iface_Prim := Node (Iface_Prim_Elmt);
6567 Iface_Conv := Convention_Of (Iface_Prim);
6568
6569 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6570 and then Iface_Conv /= Op_Conv
6571 then
6572 Error_Msg_N
6573 ("inconsistent conventions in primitive operations", Typ);
6574
6575 Error_Msg_Name_1 := Chars (Op);
6576 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6577 Error_Msg_Sloc := Sloc (Op);
6578
6579 if Comes_From_Source (Op) or else No (Alias (Op)) then
6580 if not Present (Overridden_Operation (Op)) then
6581 Error_Msg_N ("\\primitive % defined #", Typ);
6582 else
6583 Error_Msg_N
6584 ("\\overriding operation % with " &
6585 "convention % defined #", Typ);
6586 end if;
6587
6588 else pragma Assert (Present (Alias (Op)));
6589 Error_Msg_Sloc := Sloc (Alias (Op));
6590 Error_Msg_N
6591 ("\\inherited operation % with " &
6592 "convention % defined #", Typ);
6593 end if;
6594
6595 Error_Msg_Name_1 := Chars (Op);
6596 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6597 Error_Msg_Sloc := Sloc (Iface_Prim);
6598 Error_Msg_N
6599 ("\\overridden operation % with " &
6600 "convention % defined #", Typ);
6601
6602 -- Avoid cascading errors
6603
6604 return;
6605 end if;
6606
6607 Next_Elmt (Iface_Prim_Elmt);
6608 end loop;
6609
6610 Next_Elmt (Iface_Elmt);
6611 end loop;
6612 end Check_Convention;
6613
6614 -- Local variables
6615
6616 Prim_Op : Entity_Id;
6617 Prim_Op_Elmt : Elmt_Id;
6618
6619 -- Start of processing for Check_Conventions
6620
6621 begin
6622 if not Has_Interfaces (Typ) then
6623 return;
6624 end if;
6625
6626 Collect_Interfaces (Typ, Ifaces_List);
6627
6628 -- The algorithm checks every overriding dispatching operation against
6629 -- all the corresponding overridden dispatching operations, detecting
6630 -- differences in conventions.
6631
6632 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6633 while Present (Prim_Op_Elmt) loop
6634 Prim_Op := Node (Prim_Op_Elmt);
6635
6636 -- A small optimization: skip the predefined dispatching operations
6637 -- since they always have the same convention.
6638
6639 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6640 Check_Convention (Prim_Op);
6641 end if;
6642
6643 Next_Elmt (Prim_Op_Elmt);
6644 end loop;
6645 end Check_Conventions;
6646
6647 ------------------------------
6648 -- Check_Delayed_Subprogram --
6649 ------------------------------
6650
6651 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6652 F : Entity_Id;
6653
6654 procedure Possible_Freeze (T : Entity_Id);
6655 -- T is the type of either a formal parameter or of the return type.
6656 -- If T is not yet frozen and needs a delayed freeze, then the
6657 -- subprogram itself must be delayed. If T is the limited view of an
6658 -- incomplete type the subprogram must be frozen as well, because
6659 -- T may depend on local types that have not been frozen yet.
6660
6661 ---------------------
6662 -- Possible_Freeze --
6663 ---------------------
6664
6665 procedure Possible_Freeze (T : Entity_Id) is
6666 begin
6667 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6668 Set_Has_Delayed_Freeze (Designator);
6669
6670 elsif Is_Access_Type (T)
6671 and then Has_Delayed_Freeze (Designated_Type (T))
6672 and then not Is_Frozen (Designated_Type (T))
6673 then
6674 Set_Has_Delayed_Freeze (Designator);
6675
6676 elsif Ekind (T) = E_Incomplete_Type
6677 and then From_Limited_With (T)
6678 then
6679 Set_Has_Delayed_Freeze (Designator);
6680
6681 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
6682 -- of a subprogram or entry declaration.
6683
6684 elsif Ekind (T) = E_Incomplete_Type
6685 and then Ada_Version >= Ada_2012
6686 then
6687 Set_Has_Delayed_Freeze (Designator);
6688 end if;
6689
6690 end Possible_Freeze;
6691
6692 -- Start of processing for Check_Delayed_Subprogram
6693
6694 begin
6695 -- All subprograms, including abstract subprograms, may need a freeze
6696 -- node if some formal type or the return type needs one.
6697
6698 Possible_Freeze (Etype (Designator));
6699 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6700
6701 -- Need delayed freeze if any of the formal types themselves need
6702 -- a delayed freeze and are not yet frozen.
6703
6704 F := First_Formal (Designator);
6705 while Present (F) loop
6706 Possible_Freeze (Etype (F));
6707 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6708 Next_Formal (F);
6709 end loop;
6710
6711 -- Mark functions that return by reference. Note that it cannot be
6712 -- done for delayed_freeze subprograms because the underlying
6713 -- returned type may not be known yet (for private types)
6714
6715 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6716 declare
6717 Typ : constant Entity_Id := Etype (Designator);
6718 Utyp : constant Entity_Id := Underlying_Type (Typ);
6719 begin
6720 if Is_Limited_View (Typ) then
6721 Set_Returns_By_Ref (Designator);
6722 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6723 Set_Returns_By_Ref (Designator);
6724 end if;
6725 end;
6726 end if;
6727 end Check_Delayed_Subprogram;
6728
6729 ------------------------------------
6730 -- Check_Discriminant_Conformance --
6731 ------------------------------------
6732
6733 procedure Check_Discriminant_Conformance
6734 (N : Node_Id;
6735 Prev : Entity_Id;
6736 Prev_Loc : Node_Id)
6737 is
6738 Old_Discr : Entity_Id := First_Discriminant (Prev);
6739 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6740 New_Discr_Id : Entity_Id;
6741 New_Discr_Type : Entity_Id;
6742
6743 procedure Conformance_Error (Msg : String; N : Node_Id);
6744 -- Post error message for conformance error on given node. Two messages
6745 -- are output. The first points to the previous declaration with a
6746 -- general "no conformance" message. The second is the detailed reason,
6747 -- supplied as Msg. The parameter N provide information for a possible
6748 -- & insertion in the message.
6749
6750 -----------------------
6751 -- Conformance_Error --
6752 -----------------------
6753
6754 procedure Conformance_Error (Msg : String; N : Node_Id) is
6755 begin
6756 Error_Msg_Sloc := Sloc (Prev_Loc);
6757 Error_Msg_N -- CODEFIX
6758 ("not fully conformant with declaration#!", N);
6759 Error_Msg_NE (Msg, N, N);
6760 end Conformance_Error;
6761
6762 -- Start of processing for Check_Discriminant_Conformance
6763
6764 begin
6765 while Present (Old_Discr) and then Present (New_Discr) loop
6766 New_Discr_Id := Defining_Identifier (New_Discr);
6767
6768 -- The subtype mark of the discriminant on the full type has not
6769 -- been analyzed so we do it here. For an access discriminant a new
6770 -- type is created.
6771
6772 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6773 New_Discr_Type :=
6774 Access_Definition (N, Discriminant_Type (New_Discr));
6775
6776 else
6777 Analyze (Discriminant_Type (New_Discr));
6778 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6779
6780 -- Ada 2005: if the discriminant definition carries a null
6781 -- exclusion, create an itype to check properly for consistency
6782 -- with partial declaration.
6783
6784 if Is_Access_Type (New_Discr_Type)
6785 and then Null_Exclusion_Present (New_Discr)
6786 then
6787 New_Discr_Type :=
6788 Create_Null_Excluding_Itype
6789 (T => New_Discr_Type,
6790 Related_Nod => New_Discr,
6791 Scope_Id => Current_Scope);
6792 end if;
6793 end if;
6794
6795 if not Conforming_Types
6796 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6797 then
6798 Conformance_Error ("type of & does not match!", New_Discr_Id);
6799 return;
6800 else
6801 -- Treat the new discriminant as an occurrence of the old one,
6802 -- for navigation purposes, and fill in some semantic
6803 -- information, for completeness.
6804
6805 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6806 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6807 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6808 end if;
6809
6810 -- Names must match
6811
6812 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6813 Conformance_Error ("name & does not match!", New_Discr_Id);
6814 return;
6815 end if;
6816
6817 -- Default expressions must match
6818
6819 declare
6820 NewD : constant Boolean :=
6821 Present (Expression (New_Discr));
6822 OldD : constant Boolean :=
6823 Present (Expression (Parent (Old_Discr)));
6824
6825 begin
6826 if NewD or OldD then
6827
6828 -- The old default value has been analyzed and expanded,
6829 -- because the current full declaration will have frozen
6830 -- everything before. The new default values have not been
6831 -- expanded, so expand now to check conformance.
6832
6833 if NewD then
6834 Preanalyze_Spec_Expression
6835 (Expression (New_Discr), New_Discr_Type);
6836 end if;
6837
6838 if not (NewD and OldD)
6839 or else not Fully_Conformant_Expressions
6840 (Expression (Parent (Old_Discr)),
6841 Expression (New_Discr))
6842
6843 then
6844 Conformance_Error
6845 ("default expression for & does not match!",
6846 New_Discr_Id);
6847 return;
6848 end if;
6849 end if;
6850 end;
6851
6852 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6853
6854 if Ada_Version = Ada_83 then
6855 declare
6856 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6857
6858 begin
6859 -- Grouping (use of comma in param lists) must be the same
6860 -- This is where we catch a misconformance like:
6861
6862 -- A, B : Integer
6863 -- A : Integer; B : Integer
6864
6865 -- which are represented identically in the tree except
6866 -- for the setting of the flags More_Ids and Prev_Ids.
6867
6868 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6869 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6870 then
6871 Conformance_Error
6872 ("grouping of & does not match!", New_Discr_Id);
6873 return;
6874 end if;
6875 end;
6876 end if;
6877
6878 Next_Discriminant (Old_Discr);
6879 Next (New_Discr);
6880 end loop;
6881
6882 if Present (Old_Discr) then
6883 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6884 return;
6885
6886 elsif Present (New_Discr) then
6887 Conformance_Error
6888 ("too many discriminants!", Defining_Identifier (New_Discr));
6889 return;
6890 end if;
6891 end Check_Discriminant_Conformance;
6892
6893 ----------------------------
6894 -- Check_Fully_Conformant --
6895 ----------------------------
6896
6897 procedure Check_Fully_Conformant
6898 (New_Id : Entity_Id;
6899 Old_Id : Entity_Id;
6900 Err_Loc : Node_Id := Empty)
6901 is
6902 Result : Boolean;
6903 pragma Warnings (Off, Result);
6904 begin
6905 Check_Conformance
6906 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6907 end Check_Fully_Conformant;
6908
6909 ---------------------------
6910 -- Check_Mode_Conformant --
6911 ---------------------------
6912
6913 procedure Check_Mode_Conformant
6914 (New_Id : Entity_Id;
6915 Old_Id : Entity_Id;
6916 Err_Loc : Node_Id := Empty;
6917 Get_Inst : Boolean := False)
6918 is
6919 Result : Boolean;
6920 pragma Warnings (Off, Result);
6921 begin
6922 Check_Conformance
6923 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6924 end Check_Mode_Conformant;
6925
6926 --------------------------------
6927 -- Check_Overriding_Indicator --
6928 --------------------------------
6929
6930 procedure Check_Overriding_Indicator
6931 (Subp : Entity_Id;
6932 Overridden_Subp : Entity_Id;
6933 Is_Primitive : Boolean)
6934 is
6935 Decl : Node_Id;
6936 Spec : Node_Id;
6937
6938 begin
6939 -- No overriding indicator for literals
6940
6941 if Ekind (Subp) = E_Enumeration_Literal then
6942 return;
6943
6944 elsif Ekind (Subp) = E_Entry then
6945 Decl := Parent (Subp);
6946
6947 -- No point in analyzing a malformed operator
6948
6949 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6950 and then Error_Posted (Subp)
6951 then
6952 return;
6953
6954 else
6955 Decl := Unit_Declaration_Node (Subp);
6956 end if;
6957
6958 if Nkind_In (Decl, N_Subprogram_Body,
6959 N_Subprogram_Body_Stub,
6960 N_Subprogram_Declaration,
6961 N_Abstract_Subprogram_Declaration,
6962 N_Subprogram_Renaming_Declaration)
6963 then
6964 Spec := Specification (Decl);
6965
6966 elsif Nkind (Decl) = N_Entry_Declaration then
6967 Spec := Decl;
6968
6969 else
6970 return;
6971 end if;
6972
6973 -- The overriding operation is type conformant with the overridden one,
6974 -- but the names of the formals are not required to match. If the names
6975 -- appear permuted in the overriding operation, this is a possible
6976 -- source of confusion that is worth diagnosing. Controlling formals
6977 -- often carry names that reflect the type, and it is not worthwhile
6978 -- requiring that their names match.
6979
6980 if Present (Overridden_Subp)
6981 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6982 then
6983 declare
6984 Form1 : Entity_Id;
6985 Form2 : Entity_Id;
6986
6987 begin
6988 Form1 := First_Formal (Subp);
6989 Form2 := First_Formal (Overridden_Subp);
6990
6991 -- If the overriding operation is a synchronized operation, skip
6992 -- the first parameter of the overridden operation, which is
6993 -- implicit in the new one. If the operation is declared in the
6994 -- body it is not primitive and all formals must match.
6995
6996 if Is_Concurrent_Type (Scope (Subp))
6997 and then Is_Tagged_Type (Scope (Subp))
6998 and then not Has_Completion (Scope (Subp))
6999 then
7000 Form2 := Next_Formal (Form2);
7001 end if;
7002
7003 if Present (Form1) then
7004 Form1 := Next_Formal (Form1);
7005 Form2 := Next_Formal (Form2);
7006 end if;
7007
7008 while Present (Form1) loop
7009 if not Is_Controlling_Formal (Form1)
7010 and then Present (Next_Formal (Form2))
7011 and then Chars (Form1) = Chars (Next_Formal (Form2))
7012 then
7013 Error_Msg_Node_2 := Alias (Overridden_Subp);
7014 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
7015 Error_Msg_NE
7016 ("& does not match corresponding formal of&#",
7017 Form1, Form1);
7018 exit;
7019 end if;
7020
7021 Next_Formal (Form1);
7022 Next_Formal (Form2);
7023 end loop;
7024 end;
7025 end if;
7026
7027 -- If there is an overridden subprogram, then check that there is no
7028 -- "not overriding" indicator, and mark the subprogram as overriding.
7029 -- This is not done if the overridden subprogram is marked as hidden,
7030 -- which can occur for the case of inherited controlled operations
7031 -- (see Derive_Subprogram), unless the inherited subprogram's parent
7032 -- subprogram is not itself hidden. (Note: This condition could probably
7033 -- be simplified, leaving out the testing for the specific controlled
7034 -- cases, but it seems safer and clearer this way, and echoes similar
7035 -- special-case tests of this kind in other places.)
7036
7037 if Present (Overridden_Subp)
7038 and then (not Is_Hidden (Overridden_Subp)
7039 or else
7040 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
7041 Name_Adjust,
7042 Name_Finalize)
7043 and then Present (Alias (Overridden_Subp))
7044 and then not Is_Hidden (Alias (Overridden_Subp))))
7045 then
7046 if Must_Not_Override (Spec) then
7047 Error_Msg_Sloc := Sloc (Overridden_Subp);
7048
7049 if Ekind (Subp) = E_Entry then
7050 Error_Msg_NE
7051 ("entry & overrides inherited operation #", Spec, Subp);
7052 else
7053 Error_Msg_NE
7054 ("subprogram & overrides inherited operation #", Spec, Subp);
7055 end if;
7056
7057 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
7058 -- as an extension of Root_Controlled, and thus has a useless Adjust
7059 -- operation. This operation should not be inherited by other limited
7060 -- controlled types. An explicit Adjust for them is not overriding.
7061
7062 elsif Must_Override (Spec)
7063 and then Chars (Overridden_Subp) = Name_Adjust
7064 and then Is_Limited_Type (Etype (First_Formal (Subp)))
7065 and then Present (Alias (Overridden_Subp))
7066 and then
7067 Is_Predefined_File_Name
7068 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
7069 then
7070 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7071
7072 elsif Is_Subprogram (Subp) then
7073 if Is_Init_Proc (Subp) then
7074 null;
7075
7076 elsif No (Overridden_Operation (Subp)) then
7077
7078 -- For entities generated by Derive_Subprograms the overridden
7079 -- operation is the inherited primitive (which is available
7080 -- through the attribute alias)
7081
7082 if (Is_Dispatching_Operation (Subp)
7083 or else Is_Dispatching_Operation (Overridden_Subp))
7084 and then not Comes_From_Source (Overridden_Subp)
7085 and then Find_Dispatching_Type (Overridden_Subp) =
7086 Find_Dispatching_Type (Subp)
7087 and then Present (Alias (Overridden_Subp))
7088 and then Comes_From_Source (Alias (Overridden_Subp))
7089 then
7090 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
7091
7092 else
7093 Set_Overridden_Operation (Subp, Overridden_Subp);
7094 end if;
7095 end if;
7096 end if;
7097
7098 -- If primitive flag is set or this is a protected operation, then
7099 -- the operation is overriding at the point of its declaration, so
7100 -- warn if necessary. Otherwise it may have been declared before the
7101 -- operation it overrides and no check is required.
7102
7103 if Style_Check
7104 and then not Must_Override (Spec)
7105 and then (Is_Primitive
7106 or else Ekind (Scope (Subp)) = E_Protected_Type)
7107 then
7108 Style.Missing_Overriding (Decl, Subp);
7109 end if;
7110
7111 -- If Subp is an operator, it may override a predefined operation, if
7112 -- it is defined in the same scope as the type to which it applies.
7113 -- In that case Overridden_Subp is empty because of our implicit
7114 -- representation for predefined operators. We have to check whether the
7115 -- signature of Subp matches that of a predefined operator. Note that
7116 -- first argument provides the name of the operator, and the second
7117 -- argument the signature that may match that of a standard operation.
7118 -- If the indicator is overriding, then the operator must match a
7119 -- predefined signature, because we know already that there is no
7120 -- explicit overridden operation.
7121
7122 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
7123 if Must_Not_Override (Spec) then
7124
7125 -- If this is not a primitive or a protected subprogram, then
7126 -- "not overriding" is illegal.
7127
7128 if not Is_Primitive
7129 and then Ekind (Scope (Subp)) /= E_Protected_Type
7130 then
7131 Error_Msg_N
7132 ("overriding indicator only allowed "
7133 & "if subprogram is primitive", Subp);
7134
7135 elsif Can_Override_Operator (Subp) then
7136 Error_Msg_NE
7137 ("subprogram& overrides predefined operator ", Spec, Subp);
7138 end if;
7139
7140 elsif Must_Override (Spec) then
7141 if No (Overridden_Operation (Subp))
7142 and then not Can_Override_Operator (Subp)
7143 then
7144 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7145 end if;
7146
7147 elsif not Error_Posted (Subp)
7148 and then Style_Check
7149 and then Can_Override_Operator (Subp)
7150 and then
7151 not Is_Predefined_File_Name
7152 (Unit_File_Name (Get_Source_Unit (Subp)))
7153 then
7154 -- If style checks are enabled, indicate that the indicator is
7155 -- missing. However, at the point of declaration, the type of
7156 -- which this is a primitive operation may be private, in which
7157 -- case the indicator would be premature.
7158
7159 if Has_Private_Declaration (Etype (Subp))
7160 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
7161 then
7162 null;
7163 else
7164 Style.Missing_Overriding (Decl, Subp);
7165 end if;
7166 end if;
7167
7168 elsif Must_Override (Spec) then
7169 if Ekind (Subp) = E_Entry then
7170 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
7171 else
7172 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7173 end if;
7174
7175 -- If the operation is marked "not overriding" and it's not primitive
7176 -- then an error is issued, unless this is an operation of a task or
7177 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
7178 -- has been specified have already been checked above.
7179
7180 elsif Must_Not_Override (Spec)
7181 and then not Is_Primitive
7182 and then Ekind (Subp) /= E_Entry
7183 and then Ekind (Scope (Subp)) /= E_Protected_Type
7184 then
7185 Error_Msg_N
7186 ("overriding indicator only allowed if subprogram is primitive",
7187 Subp);
7188 return;
7189 end if;
7190 end Check_Overriding_Indicator;
7191
7192 -------------------
7193 -- Check_Returns --
7194 -------------------
7195
7196 -- Note: this procedure needs to know far too much about how the expander
7197 -- messes with exceptions. The use of the flag Exception_Junk and the
7198 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
7199 -- works, but is not very clean. It would be better if the expansion
7200 -- routines would leave Original_Node working nicely, and we could use
7201 -- Original_Node here to ignore all the peculiar expander messing ???
7202
7203 procedure Check_Returns
7204 (HSS : Node_Id;
7205 Mode : Character;
7206 Err : out Boolean;
7207 Proc : Entity_Id := Empty)
7208 is
7209 Handler : Node_Id;
7210
7211 procedure Check_Statement_Sequence (L : List_Id);
7212 -- Internal recursive procedure to check a list of statements for proper
7213 -- termination by a return statement (or a transfer of control or a
7214 -- compound statement that is itself internally properly terminated).
7215
7216 ------------------------------
7217 -- Check_Statement_Sequence --
7218 ------------------------------
7219
7220 procedure Check_Statement_Sequence (L : List_Id) is
7221 Last_Stm : Node_Id;
7222 Stm : Node_Id;
7223 Kind : Node_Kind;
7224
7225 function Assert_False return Boolean;
7226 -- Returns True if Last_Stm is a pragma Assert (False) that has been
7227 -- rewritten as a null statement when assertions are off. The assert
7228 -- is not active, but it is still enough to kill the warning.
7229
7230 ------------------
7231 -- Assert_False --
7232 ------------------
7233
7234 function Assert_False return Boolean is
7235 Orig : constant Node_Id := Original_Node (Last_Stm);
7236
7237 begin
7238 if Nkind (Orig) = N_Pragma
7239 and then Pragma_Name (Orig) = Name_Assert
7240 and then not Error_Posted (Orig)
7241 then
7242 declare
7243 Arg : constant Node_Id :=
7244 First (Pragma_Argument_Associations (Orig));
7245 Exp : constant Node_Id := Expression (Arg);
7246 begin
7247 return Nkind (Exp) = N_Identifier
7248 and then Chars (Exp) = Name_False;
7249 end;
7250
7251 else
7252 return False;
7253 end if;
7254 end Assert_False;
7255
7256 -- Local variables
7257
7258 Raise_Exception_Call : Boolean;
7259 -- Set True if statement sequence terminated by Raise_Exception call
7260 -- or a Reraise_Occurrence call.
7261
7262 -- Start of processing for Check_Statement_Sequence
7263
7264 begin
7265 Raise_Exception_Call := False;
7266
7267 -- Get last real statement
7268
7269 Last_Stm := Last (L);
7270
7271 -- Deal with digging out exception handler statement sequences that
7272 -- have been transformed by the local raise to goto optimization.
7273 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7274 -- optimization has occurred, we are looking at something like:
7275
7276 -- begin
7277 -- original stmts in block
7278
7279 -- exception \
7280 -- when excep1 => |
7281 -- goto L1; | omitted if No_Exception_Propagation
7282 -- when excep2 => |
7283 -- goto L2; /
7284 -- end;
7285
7286 -- goto L3; -- skip handler when exception not raised
7287
7288 -- <<L1>> -- target label for local exception
7289 -- begin
7290 -- estmts1
7291 -- end;
7292
7293 -- goto L3;
7294
7295 -- <<L2>>
7296 -- begin
7297 -- estmts2
7298 -- end;
7299
7300 -- <<L3>>
7301
7302 -- and what we have to do is to dig out the estmts1 and estmts2
7303 -- sequences (which were the original sequences of statements in
7304 -- the exception handlers) and check them.
7305
7306 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7307 Stm := Last_Stm;
7308 loop
7309 Prev (Stm);
7310 exit when No (Stm);
7311 exit when Nkind (Stm) /= N_Block_Statement;
7312 exit when not Exception_Junk (Stm);
7313 Prev (Stm);
7314 exit when No (Stm);
7315 exit when Nkind (Stm) /= N_Label;
7316 exit when not Exception_Junk (Stm);
7317 Check_Statement_Sequence
7318 (Statements (Handled_Statement_Sequence (Next (Stm))));
7319
7320 Prev (Stm);
7321 Last_Stm := Stm;
7322 exit when No (Stm);
7323 exit when Nkind (Stm) /= N_Goto_Statement;
7324 exit when not Exception_Junk (Stm);
7325 end loop;
7326 end if;
7327
7328 -- Don't count pragmas
7329
7330 while Nkind (Last_Stm) = N_Pragma
7331
7332 -- Don't count call to SS_Release (can happen after Raise_Exception)
7333
7334 or else
7335 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7336 and then
7337 Nkind (Name (Last_Stm)) = N_Identifier
7338 and then
7339 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7340
7341 -- Don't count exception junk
7342
7343 or else
7344 (Nkind_In (Last_Stm, N_Goto_Statement,
7345 N_Label,
7346 N_Object_Declaration)
7347 and then Exception_Junk (Last_Stm))
7348 or else Nkind (Last_Stm) in N_Push_xxx_Label
7349 or else Nkind (Last_Stm) in N_Pop_xxx_Label
7350
7351 -- Inserted code, such as finalization calls, is irrelevant: we only
7352 -- need to check original source.
7353
7354 or else Is_Rewrite_Insertion (Last_Stm)
7355 loop
7356 Prev (Last_Stm);
7357 end loop;
7358
7359 -- Here we have the "real" last statement
7360
7361 Kind := Nkind (Last_Stm);
7362
7363 -- Transfer of control, OK. Note that in the No_Return procedure
7364 -- case, we already diagnosed any explicit return statements, so
7365 -- we can treat them as OK in this context.
7366
7367 if Is_Transfer (Last_Stm) then
7368 return;
7369
7370 -- Check cases of explicit non-indirect procedure calls
7371
7372 elsif Kind = N_Procedure_Call_Statement
7373 and then Is_Entity_Name (Name (Last_Stm))
7374 then
7375 -- Check call to Raise_Exception procedure which is treated
7376 -- specially, as is a call to Reraise_Occurrence.
7377
7378 -- We suppress the warning in these cases since it is likely that
7379 -- the programmer really does not expect to deal with the case
7380 -- of Null_Occurrence, and thus would find a warning about a
7381 -- missing return curious, and raising Program_Error does not
7382 -- seem such a bad behavior if this does occur.
7383
7384 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7385 -- behavior will be to raise Constraint_Error (see AI-329).
7386
7387 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7388 or else
7389 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7390 then
7391 Raise_Exception_Call := True;
7392
7393 -- For Raise_Exception call, test first argument, if it is
7394 -- an attribute reference for a 'Identity call, then we know
7395 -- that the call cannot possibly return.
7396
7397 declare
7398 Arg : constant Node_Id :=
7399 Original_Node (First_Actual (Last_Stm));
7400 begin
7401 if Nkind (Arg) = N_Attribute_Reference
7402 and then Attribute_Name (Arg) = Name_Identity
7403 then
7404 return;
7405 end if;
7406 end;
7407 end if;
7408
7409 -- If statement, need to look inside if there is an else and check
7410 -- each constituent statement sequence for proper termination.
7411
7412 elsif Kind = N_If_Statement
7413 and then Present (Else_Statements (Last_Stm))
7414 then
7415 Check_Statement_Sequence (Then_Statements (Last_Stm));
7416 Check_Statement_Sequence (Else_Statements (Last_Stm));
7417
7418 if Present (Elsif_Parts (Last_Stm)) then
7419 declare
7420 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7421
7422 begin
7423 while Present (Elsif_Part) loop
7424 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7425 Next (Elsif_Part);
7426 end loop;
7427 end;
7428 end if;
7429
7430 return;
7431
7432 -- Case statement, check each case for proper termination
7433
7434 elsif Kind = N_Case_Statement then
7435 declare
7436 Case_Alt : Node_Id;
7437 begin
7438 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7439 while Present (Case_Alt) loop
7440 Check_Statement_Sequence (Statements (Case_Alt));
7441 Next_Non_Pragma (Case_Alt);
7442 end loop;
7443 end;
7444
7445 return;
7446
7447 -- Block statement, check its handled sequence of statements
7448
7449 elsif Kind = N_Block_Statement then
7450 declare
7451 Err1 : Boolean;
7452
7453 begin
7454 Check_Returns
7455 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7456
7457 if Err1 then
7458 Err := True;
7459 end if;
7460
7461 return;
7462 end;
7463
7464 -- Loop statement. If there is an iteration scheme, we can definitely
7465 -- fall out of the loop. Similarly if there is an exit statement, we
7466 -- can fall out. In either case we need a following return.
7467
7468 elsif Kind = N_Loop_Statement then
7469 if Present (Iteration_Scheme (Last_Stm))
7470 or else Has_Exit (Entity (Identifier (Last_Stm)))
7471 then
7472 null;
7473
7474 -- A loop with no exit statement or iteration scheme is either
7475 -- an infinite loop, or it has some other exit (raise/return).
7476 -- In either case, no warning is required.
7477
7478 else
7479 return;
7480 end if;
7481
7482 -- Timed entry call, check entry call and delay alternatives
7483
7484 -- Note: in expanded code, the timed entry call has been converted
7485 -- to a set of expanded statements on which the check will work
7486 -- correctly in any case.
7487
7488 elsif Kind = N_Timed_Entry_Call then
7489 declare
7490 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7491 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7492
7493 begin
7494 -- If statement sequence of entry call alternative is missing,
7495 -- then we can definitely fall through, and we post the error
7496 -- message on the entry call alternative itself.
7497
7498 if No (Statements (ECA)) then
7499 Last_Stm := ECA;
7500
7501 -- If statement sequence of delay alternative is missing, then
7502 -- we can definitely fall through, and we post the error
7503 -- message on the delay alternative itself.
7504
7505 -- Note: if both ECA and DCA are missing the return, then we
7506 -- post only one message, should be enough to fix the bugs.
7507 -- If not we will get a message next time on the DCA when the
7508 -- ECA is fixed.
7509
7510 elsif No (Statements (DCA)) then
7511 Last_Stm := DCA;
7512
7513 -- Else check both statement sequences
7514
7515 else
7516 Check_Statement_Sequence (Statements (ECA));
7517 Check_Statement_Sequence (Statements (DCA));
7518 return;
7519 end if;
7520 end;
7521
7522 -- Conditional entry call, check entry call and else part
7523
7524 -- Note: in expanded code, the conditional entry call has been
7525 -- converted to a set of expanded statements on which the check
7526 -- will work correctly in any case.
7527
7528 elsif Kind = N_Conditional_Entry_Call then
7529 declare
7530 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7531
7532 begin
7533 -- If statement sequence of entry call alternative is missing,
7534 -- then we can definitely fall through, and we post the error
7535 -- message on the entry call alternative itself.
7536
7537 if No (Statements (ECA)) then
7538 Last_Stm := ECA;
7539
7540 -- Else check statement sequence and else part
7541
7542 else
7543 Check_Statement_Sequence (Statements (ECA));
7544 Check_Statement_Sequence (Else_Statements (Last_Stm));
7545 return;
7546 end if;
7547 end;
7548 end if;
7549
7550 -- If we fall through, issue appropriate message
7551
7552 if Mode = 'F' then
7553
7554 -- Kill warning if last statement is a raise exception call,
7555 -- or a pragma Assert (False). Note that with assertions enabled,
7556 -- such a pragma has been converted into a raise exception call
7557 -- already, so the Assert_False is for the assertions off case.
7558
7559 if not Raise_Exception_Call and then not Assert_False then
7560
7561 -- In GNATprove mode, it is an error to have a missing return
7562
7563 Error_Msg_Warn := SPARK_Mode /= On;
7564
7565 -- Issue error message or warning
7566
7567 Error_Msg_N
7568 ("RETURN statement missing following this statement<<!",
7569 Last_Stm);
7570 Error_Msg_N
7571 ("\Program_Error ]<<!", Last_Stm);
7572 end if;
7573
7574 -- Note: we set Err even though we have not issued a warning
7575 -- because we still have a case of a missing return. This is
7576 -- an extremely marginal case, probably will never be noticed
7577 -- but we might as well get it right.
7578
7579 Err := True;
7580
7581 -- Otherwise we have the case of a procedure marked No_Return
7582
7583 else
7584 if not Raise_Exception_Call then
7585 if GNATprove_Mode then
7586 Error_Msg_N
7587 ("implied return after this statement "
7588 & "would have raised Program_Error", Last_Stm);
7589 else
7590 Error_Msg_N
7591 ("implied return after this statement "
7592 & "will raise Program_Error??", Last_Stm);
7593 end if;
7594
7595 Error_Msg_Warn := SPARK_Mode /= On;
7596 Error_Msg_NE
7597 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7598 end if;
7599
7600 declare
7601 RE : constant Node_Id :=
7602 Make_Raise_Program_Error (Sloc (Last_Stm),
7603 Reason => PE_Implicit_Return);
7604 begin
7605 Insert_After (Last_Stm, RE);
7606 Analyze (RE);
7607 end;
7608 end if;
7609 end Check_Statement_Sequence;
7610
7611 -- Start of processing for Check_Returns
7612
7613 begin
7614 Err := False;
7615 Check_Statement_Sequence (Statements (HSS));
7616
7617 if Present (Exception_Handlers (HSS)) then
7618 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7619 while Present (Handler) loop
7620 Check_Statement_Sequence (Statements (Handler));
7621 Next_Non_Pragma (Handler);
7622 end loop;
7623 end if;
7624 end Check_Returns;
7625
7626 ----------------------------
7627 -- Check_Subprogram_Order --
7628 ----------------------------
7629
7630 procedure Check_Subprogram_Order (N : Node_Id) is
7631
7632 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7633 -- This is used to check if S1 > S2 in the sense required by this test,
7634 -- for example nameab < namec, but name2 < name10.
7635
7636 -----------------------------
7637 -- Subprogram_Name_Greater --
7638 -----------------------------
7639
7640 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7641 L1, L2 : Positive;
7642 N1, N2 : Natural;
7643
7644 begin
7645 -- Deal with special case where names are identical except for a
7646 -- numerical suffix. These are handled specially, taking the numeric
7647 -- ordering from the suffix into account.
7648
7649 L1 := S1'Last;
7650 while S1 (L1) in '0' .. '9' loop
7651 L1 := L1 - 1;
7652 end loop;
7653
7654 L2 := S2'Last;
7655 while S2 (L2) in '0' .. '9' loop
7656 L2 := L2 - 1;
7657 end loop;
7658
7659 -- If non-numeric parts non-equal, do straight compare
7660
7661 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7662 return S1 > S2;
7663
7664 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7665 -- that a missing suffix is treated as numeric zero in this test.
7666
7667 else
7668 N1 := 0;
7669 while L1 < S1'Last loop
7670 L1 := L1 + 1;
7671 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7672 end loop;
7673
7674 N2 := 0;
7675 while L2 < S2'Last loop
7676 L2 := L2 + 1;
7677 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7678 end loop;
7679
7680 return N1 > N2;
7681 end if;
7682 end Subprogram_Name_Greater;
7683
7684 -- Start of processing for Check_Subprogram_Order
7685
7686 begin
7687 -- Check body in alpha order if this is option
7688
7689 if Style_Check
7690 and then Style_Check_Order_Subprograms
7691 and then Nkind (N) = N_Subprogram_Body
7692 and then Comes_From_Source (N)
7693 and then In_Extended_Main_Source_Unit (N)
7694 then
7695 declare
7696 LSN : String_Ptr
7697 renames Scope_Stack.Table
7698 (Scope_Stack.Last).Last_Subprogram_Name;
7699
7700 Body_Id : constant Entity_Id :=
7701 Defining_Entity (Specification (N));
7702
7703 begin
7704 Get_Decoded_Name_String (Chars (Body_Id));
7705
7706 if LSN /= null then
7707 if Subprogram_Name_Greater
7708 (LSN.all, Name_Buffer (1 .. Name_Len))
7709 then
7710 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7711 end if;
7712
7713 Free (LSN);
7714 end if;
7715
7716 LSN := new String'(Name_Buffer (1 .. Name_Len));
7717 end;
7718 end if;
7719 end Check_Subprogram_Order;
7720
7721 ------------------------------
7722 -- Check_Subtype_Conformant --
7723 ------------------------------
7724
7725 procedure Check_Subtype_Conformant
7726 (New_Id : Entity_Id;
7727 Old_Id : Entity_Id;
7728 Err_Loc : Node_Id := Empty;
7729 Skip_Controlling_Formals : Boolean := False;
7730 Get_Inst : Boolean := False)
7731 is
7732 Result : Boolean;
7733 pragma Warnings (Off, Result);
7734 begin
7735 Check_Conformance
7736 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7737 Skip_Controlling_Formals => Skip_Controlling_Formals,
7738 Get_Inst => Get_Inst);
7739 end Check_Subtype_Conformant;
7740
7741 ---------------------------
7742 -- Check_Type_Conformant --
7743 ---------------------------
7744
7745 procedure Check_Type_Conformant
7746 (New_Id : Entity_Id;
7747 Old_Id : Entity_Id;
7748 Err_Loc : Node_Id := Empty)
7749 is
7750 Result : Boolean;
7751 pragma Warnings (Off, Result);
7752 begin
7753 Check_Conformance
7754 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7755 end Check_Type_Conformant;
7756
7757 ---------------------------
7758 -- Can_Override_Operator --
7759 ---------------------------
7760
7761 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7762 Typ : Entity_Id;
7763
7764 begin
7765 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7766 return False;
7767
7768 else
7769 Typ := Base_Type (Etype (First_Formal (Subp)));
7770
7771 -- Check explicitly that the operation is a primitive of the type
7772
7773 return Operator_Matches_Spec (Subp, Subp)
7774 and then not Is_Generic_Type (Typ)
7775 and then Scope (Subp) = Scope (Typ)
7776 and then not Is_Class_Wide_Type (Typ);
7777 end if;
7778 end Can_Override_Operator;
7779
7780 ----------------------
7781 -- Conforming_Types --
7782 ----------------------
7783
7784 function Conforming_Types
7785 (T1 : Entity_Id;
7786 T2 : Entity_Id;
7787 Ctype : Conformance_Type;
7788 Get_Inst : Boolean := False) return Boolean
7789 is
7790 Type_1 : Entity_Id := T1;
7791 Type_2 : Entity_Id := T2;
7792 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7793
7794 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
7795 -- If neither T1 nor T2 are generic actual types, or if they are in
7796 -- different scopes (e.g. parent and child instances), then verify that
7797 -- the base types are equal. Otherwise T1 and T2 must be on the same
7798 -- subtype chain. The whole purpose of this procedure is to prevent
7799 -- spurious ambiguities in an instantiation that may arise if two
7800 -- distinct generic types are instantiated with the same actual.
7801
7802 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7803 -- An access parameter can designate an incomplete type. If the
7804 -- incomplete type is the limited view of a type from a limited_
7805 -- with_clause, check whether the non-limited view is available. If
7806 -- it is a (non-limited) incomplete type, get the full view.
7807
7808 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7809 -- Returns True if and only if either T1 denotes a limited view of T2
7810 -- or T2 denotes a limited view of T1. This can arise when the limited
7811 -- with view of a type is used in a subprogram declaration and the
7812 -- subprogram body is in the scope of a regular with clause for the
7813 -- same unit. In such a case, the two type entities can be considered
7814 -- identical for purposes of conformance checking.
7815
7816 ----------------------
7817 -- Base_Types_Match --
7818 ----------------------
7819
7820 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7821 BT1 : constant Entity_Id := Base_Type (T1);
7822 BT2 : constant Entity_Id := Base_Type (T2);
7823
7824 begin
7825 if T1 = T2 then
7826 return True;
7827
7828 elsif BT1 = BT2 then
7829
7830 -- The following is too permissive. A more precise test should
7831 -- check that the generic actual is an ancestor subtype of the
7832 -- other ???.
7833
7834 -- See code in Find_Corresponding_Spec that applies an additional
7835 -- filter to handle accidental amiguities in instances.
7836
7837 return not Is_Generic_Actual_Type (T1)
7838 or else not Is_Generic_Actual_Type (T2)
7839 or else Scope (T1) /= Scope (T2);
7840
7841 -- If T2 is a generic actual type it is declared as the subtype of
7842 -- the actual. If that actual is itself a subtype we need to use its
7843 -- own base type to check for compatibility.
7844
7845 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
7846 return True;
7847
7848 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
7849 return True;
7850
7851 else
7852 return False;
7853 end if;
7854 end Base_Types_Match;
7855
7856 --------------------------
7857 -- Find_Designated_Type --
7858 --------------------------
7859
7860 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7861 Desig : Entity_Id;
7862
7863 begin
7864 Desig := Directly_Designated_Type (T);
7865
7866 if Ekind (Desig) = E_Incomplete_Type then
7867
7868 -- If regular incomplete type, get full view if available
7869
7870 if Present (Full_View (Desig)) then
7871 Desig := Full_View (Desig);
7872
7873 -- If limited view of a type, get non-limited view if available,
7874 -- and check again for a regular incomplete type.
7875
7876 elsif Present (Non_Limited_View (Desig)) then
7877 Desig := Get_Full_View (Non_Limited_View (Desig));
7878 end if;
7879 end if;
7880
7881 return Desig;
7882 end Find_Designated_Type;
7883
7884 -------------------------------
7885 -- Matches_Limited_With_View --
7886 -------------------------------
7887
7888 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7889 begin
7890 -- In some cases a type imported through a limited_with clause, and
7891 -- its nonlimited view are both visible, for example in an anonymous
7892 -- access-to-class-wide type in a formal. Both entities designate the
7893 -- same type.
7894
7895 if From_Limited_With (T1) and then T2 = Available_View (T1) then
7896 return True;
7897
7898 elsif From_Limited_With (T2) and then T1 = Available_View (T2) then
7899 return True;
7900
7901 elsif From_Limited_With (T1)
7902 and then From_Limited_With (T2)
7903 and then Available_View (T1) = Available_View (T2)
7904 then
7905 return True;
7906
7907 else
7908 return False;
7909 end if;
7910 end Matches_Limited_With_View;
7911
7912 -- Start of processing for Conforming_Types
7913
7914 begin
7915 -- The context is an instance association for a formal access-to-
7916 -- subprogram type; the formal parameter types require mapping because
7917 -- they may denote other formal parameters of the generic unit.
7918
7919 if Get_Inst then
7920 Type_1 := Get_Instance_Of (T1);
7921 Type_2 := Get_Instance_Of (T2);
7922 end if;
7923
7924 -- If one of the types is a view of the other introduced by a limited
7925 -- with clause, treat these as conforming for all purposes.
7926
7927 if Matches_Limited_With_View (T1, T2) then
7928 return True;
7929
7930 elsif Base_Types_Match (Type_1, Type_2) then
7931 return Ctype <= Mode_Conformant
7932 or else Subtypes_Statically_Match (Type_1, Type_2);
7933
7934 elsif Is_Incomplete_Or_Private_Type (Type_1)
7935 and then Present (Full_View (Type_1))
7936 and then Base_Types_Match (Full_View (Type_1), Type_2)
7937 then
7938 return Ctype <= Mode_Conformant
7939 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7940
7941 elsif Ekind (Type_2) = E_Incomplete_Type
7942 and then Present (Full_View (Type_2))
7943 and then Base_Types_Match (Type_1, Full_View (Type_2))
7944 then
7945 return Ctype <= Mode_Conformant
7946 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7947
7948 elsif Is_Private_Type (Type_2)
7949 and then In_Instance
7950 and then Present (Full_View (Type_2))
7951 and then Base_Types_Match (Type_1, Full_View (Type_2))
7952 then
7953 return Ctype <= Mode_Conformant
7954 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7955 end if;
7956
7957 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7958 -- treated recursively because they carry a signature. As far as
7959 -- conformance is concerned, convention plays no role, and either
7960 -- or both could be access to protected subprograms.
7961
7962 Are_Anonymous_Access_To_Subprogram_Types :=
7963 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7964 E_Anonymous_Access_Protected_Subprogram_Type)
7965 and then
7966 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7967 E_Anonymous_Access_Protected_Subprogram_Type);
7968
7969 -- Test anonymous access type case. For this case, static subtype
7970 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7971 -- the base types because we may have built internal subtype entities
7972 -- to handle null-excluding types (see Process_Formals).
7973
7974 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7975 and then
7976 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7977
7978 -- Ada 2005 (AI-254)
7979
7980 or else Are_Anonymous_Access_To_Subprogram_Types
7981 then
7982 declare
7983 Desig_1 : Entity_Id;
7984 Desig_2 : Entity_Id;
7985
7986 begin
7987 -- In Ada 2005, access constant indicators must match for
7988 -- subtype conformance.
7989
7990 if Ada_Version >= Ada_2005
7991 and then Ctype >= Subtype_Conformant
7992 and then
7993 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7994 then
7995 return False;
7996 end if;
7997
7998 Desig_1 := Find_Designated_Type (Type_1);
7999 Desig_2 := Find_Designated_Type (Type_2);
8000
8001 -- If the context is an instance association for a formal
8002 -- access-to-subprogram type; formal access parameter designated
8003 -- types require mapping because they may denote other formal
8004 -- parameters of the generic unit.
8005
8006 if Get_Inst then
8007 Desig_1 := Get_Instance_Of (Desig_1);
8008 Desig_2 := Get_Instance_Of (Desig_2);
8009 end if;
8010
8011 -- It is possible for a Class_Wide_Type to be introduced for an
8012 -- incomplete type, in which case there is a separate class_ wide
8013 -- type for the full view. The types conform if their Etypes
8014 -- conform, i.e. one may be the full view of the other. This can
8015 -- only happen in the context of an access parameter, other uses
8016 -- of an incomplete Class_Wide_Type are illegal.
8017
8018 if Is_Class_Wide_Type (Desig_1)
8019 and then
8020 Is_Class_Wide_Type (Desig_2)
8021 then
8022 return
8023 Conforming_Types
8024 (Etype (Base_Type (Desig_1)),
8025 Etype (Base_Type (Desig_2)), Ctype);
8026
8027 elsif Are_Anonymous_Access_To_Subprogram_Types then
8028 if Ada_Version < Ada_2005 then
8029 return Ctype = Type_Conformant
8030 or else
8031 Subtypes_Statically_Match (Desig_1, Desig_2);
8032
8033 -- We must check the conformance of the signatures themselves
8034
8035 else
8036 declare
8037 Conformant : Boolean;
8038 begin
8039 Check_Conformance
8040 (Desig_1, Desig_2, Ctype, False, Conformant);
8041 return Conformant;
8042 end;
8043 end if;
8044
8045 else
8046 return Base_Type (Desig_1) = Base_Type (Desig_2)
8047 and then (Ctype = Type_Conformant
8048 or else
8049 Subtypes_Statically_Match (Desig_1, Desig_2));
8050 end if;
8051 end;
8052
8053 -- Otherwise definitely no match
8054
8055 else
8056 if ((Ekind (Type_1) = E_Anonymous_Access_Type
8057 and then Is_Access_Type (Type_2))
8058 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8059 and then Is_Access_Type (Type_1)))
8060 and then
8061 Conforming_Types
8062 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
8063 then
8064 May_Hide_Profile := True;
8065 end if;
8066
8067 return False;
8068 end if;
8069 end Conforming_Types;
8070
8071 --------------------------
8072 -- Create_Extra_Formals --
8073 --------------------------
8074
8075 procedure Create_Extra_Formals (E : Entity_Id) is
8076 Formal : Entity_Id;
8077 First_Extra : Entity_Id := Empty;
8078 Last_Extra : Entity_Id;
8079 Formal_Type : Entity_Id;
8080 P_Formal : Entity_Id := Empty;
8081
8082 function Add_Extra_Formal
8083 (Assoc_Entity : Entity_Id;
8084 Typ : Entity_Id;
8085 Scope : Entity_Id;
8086 Suffix : String) return Entity_Id;
8087 -- Add an extra formal to the current list of formals and extra formals.
8088 -- The extra formal is added to the end of the list of extra formals,
8089 -- and also returned as the result. These formals are always of mode IN.
8090 -- The new formal has the type Typ, is declared in Scope, and its name
8091 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
8092 -- The following suffixes are currently used. They should not be changed
8093 -- without coordinating with CodePeer, which makes use of these to
8094 -- provide better messages.
8095
8096 -- O denotes the Constrained bit.
8097 -- L denotes the accessibility level.
8098 -- BIP_xxx denotes an extra formal for a build-in-place function. See
8099 -- the full list in exp_ch6.BIP_Formal_Kind.
8100
8101 ----------------------
8102 -- Add_Extra_Formal --
8103 ----------------------
8104
8105 function Add_Extra_Formal
8106 (Assoc_Entity : Entity_Id;
8107 Typ : Entity_Id;
8108 Scope : Entity_Id;
8109 Suffix : String) return Entity_Id
8110 is
8111 EF : constant Entity_Id :=
8112 Make_Defining_Identifier (Sloc (Assoc_Entity),
8113 Chars => New_External_Name (Chars (Assoc_Entity),
8114 Suffix => Suffix));
8115
8116 begin
8117 -- A little optimization. Never generate an extra formal for the
8118 -- _init operand of an initialization procedure, since it could
8119 -- never be used.
8120
8121 if Chars (Formal) = Name_uInit then
8122 return Empty;
8123 end if;
8124
8125 Set_Ekind (EF, E_In_Parameter);
8126 Set_Actual_Subtype (EF, Typ);
8127 Set_Etype (EF, Typ);
8128 Set_Scope (EF, Scope);
8129 Set_Mechanism (EF, Default_Mechanism);
8130 Set_Formal_Validity (EF);
8131
8132 if No (First_Extra) then
8133 First_Extra := EF;
8134 Set_Extra_Formals (Scope, First_Extra);
8135 end if;
8136
8137 if Present (Last_Extra) then
8138 Set_Extra_Formal (Last_Extra, EF);
8139 end if;
8140
8141 Last_Extra := EF;
8142
8143 return EF;
8144 end Add_Extra_Formal;
8145
8146 -- Start of processing for Create_Extra_Formals
8147
8148 begin
8149 -- We never generate extra formals if expansion is not active because we
8150 -- don't need them unless we are generating code.
8151
8152 if not Expander_Active then
8153 return;
8154 end if;
8155
8156 -- No need to generate extra formals in interface thunks whose target
8157 -- primitive has no extra formals.
8158
8159 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
8160 return;
8161 end if;
8162
8163 -- If this is a derived subprogram then the subtypes of the parent
8164 -- subprogram's formal parameters will be used to determine the need
8165 -- for extra formals.
8166
8167 if Is_Overloadable (E) and then Present (Alias (E)) then
8168 P_Formal := First_Formal (Alias (E));
8169 end if;
8170
8171 Last_Extra := Empty;
8172 Formal := First_Formal (E);
8173 while Present (Formal) loop
8174 Last_Extra := Formal;
8175 Next_Formal (Formal);
8176 end loop;
8177
8178 -- If Extra_formals were already created, don't do it again. This
8179 -- situation may arise for subprogram types created as part of
8180 -- dispatching calls (see Expand_Dispatching_Call)
8181
8182 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
8183 return;
8184 end if;
8185
8186 -- If the subprogram is a predefined dispatching subprogram then don't
8187 -- generate any extra constrained or accessibility level formals. In
8188 -- general we suppress these for internal subprograms (by not calling
8189 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
8190 -- generated stream attributes do get passed through because extra
8191 -- build-in-place formals are needed in some cases (limited 'Input).
8192
8193 if Is_Predefined_Internal_Operation (E) then
8194 goto Test_For_Func_Result_Extras;
8195 end if;
8196
8197 Formal := First_Formal (E);
8198 while Present (Formal) loop
8199
8200 -- Create extra formal for supporting the attribute 'Constrained.
8201 -- The case of a private type view without discriminants also
8202 -- requires the extra formal if the underlying type has defaulted
8203 -- discriminants.
8204
8205 if Ekind (Formal) /= E_In_Parameter then
8206 if Present (P_Formal) then
8207 Formal_Type := Etype (P_Formal);
8208 else
8209 Formal_Type := Etype (Formal);
8210 end if;
8211
8212 -- Do not produce extra formals for Unchecked_Union parameters.
8213 -- Jump directly to the end of the loop.
8214
8215 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
8216 goto Skip_Extra_Formal_Generation;
8217 end if;
8218
8219 if not Has_Discriminants (Formal_Type)
8220 and then Ekind (Formal_Type) in Private_Kind
8221 and then Present (Underlying_Type (Formal_Type))
8222 then
8223 Formal_Type := Underlying_Type (Formal_Type);
8224 end if;
8225
8226 -- Suppress the extra formal if formal's subtype is constrained or
8227 -- indefinite, or we're compiling for Ada 2012 and the underlying
8228 -- type is tagged and limited. In Ada 2012, a limited tagged type
8229 -- can have defaulted discriminants, but 'Constrained is required
8230 -- to return True, so the formal is never needed (see AI05-0214).
8231 -- Note that this ensures consistency of calling sequences for
8232 -- dispatching operations when some types in a class have defaults
8233 -- on discriminants and others do not (and requiring the extra
8234 -- formal would introduce distributed overhead).
8235
8236 -- If the type does not have a completion yet, treat as prior to
8237 -- Ada 2012 for consistency.
8238
8239 if Has_Discriminants (Formal_Type)
8240 and then not Is_Constrained (Formal_Type)
8241 and then not Is_Indefinite_Subtype (Formal_Type)
8242 and then (Ada_Version < Ada_2012
8243 or else No (Underlying_Type (Formal_Type))
8244 or else not
8245 (Is_Limited_Type (Formal_Type)
8246 and then
8247 (Is_Tagged_Type
8248 (Underlying_Type (Formal_Type)))))
8249 then
8250 Set_Extra_Constrained
8251 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
8252 end if;
8253 end if;
8254
8255 -- Create extra formal for supporting accessibility checking. This
8256 -- is done for both anonymous access formals and formals of named
8257 -- access types that are marked as controlling formals. The latter
8258 -- case can occur when Expand_Dispatching_Call creates a subprogram
8259 -- type and substitutes the types of access-to-class-wide actuals
8260 -- for the anonymous access-to-specific-type of controlling formals.
8261 -- Base_Type is applied because in cases where there is a null
8262 -- exclusion the formal may have an access subtype.
8263
8264 -- This is suppressed if we specifically suppress accessibility
8265 -- checks at the package level for either the subprogram, or the
8266 -- package in which it resides. However, we do not suppress it
8267 -- simply if the scope has accessibility checks suppressed, since
8268 -- this could cause trouble when clients are compiled with a
8269 -- different suppression setting. The explicit checks at the
8270 -- package level are safe from this point of view.
8271
8272 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8273 or else (Is_Controlling_Formal (Formal)
8274 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8275 and then not
8276 (Explicit_Suppress (E, Accessibility_Check)
8277 or else
8278 Explicit_Suppress (Scope (E), Accessibility_Check))
8279 and then
8280 (No (P_Formal)
8281 or else Present (Extra_Accessibility (P_Formal)))
8282 then
8283 Set_Extra_Accessibility
8284 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8285 end if;
8286
8287 -- This label is required when skipping extra formal generation for
8288 -- Unchecked_Union parameters.
8289
8290 <<Skip_Extra_Formal_Generation>>
8291
8292 if Present (P_Formal) then
8293 Next_Formal (P_Formal);
8294 end if;
8295
8296 Next_Formal (Formal);
8297 end loop;
8298
8299 <<Test_For_Func_Result_Extras>>
8300
8301 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8302 -- function call is ... determined by the point of call ...".
8303
8304 if Needs_Result_Accessibility_Level (E) then
8305 Set_Extra_Accessibility_Of_Result
8306 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8307 end if;
8308
8309 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8310 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8311
8312 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
8313 declare
8314 Result_Subt : constant Entity_Id := Etype (E);
8315 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
8316 Formal_Typ : Entity_Id;
8317
8318 Discard : Entity_Id;
8319 pragma Warnings (Off, Discard);
8320
8321 begin
8322 -- In the case of functions with unconstrained result subtypes,
8323 -- add a 4-state formal indicating whether the return object is
8324 -- allocated by the caller (1), or should be allocated by the
8325 -- callee on the secondary stack (2), in the global heap (3), or
8326 -- in a user-defined storage pool (4). For the moment we just use
8327 -- Natural for the type of this formal. Note that this formal
8328 -- isn't usually needed in the case where the result subtype is
8329 -- constrained, but it is needed when the function has a tagged
8330 -- result, because generally such functions can be called in a
8331 -- dispatching context and such calls must be handled like calls
8332 -- to a class-wide function.
8333
8334 if Needs_BIP_Alloc_Form (E) then
8335 Discard :=
8336 Add_Extra_Formal
8337 (E, Standard_Natural,
8338 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8339
8340 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8341 -- use a user-defined pool. This formal is not added on
8342 -- .NET/JVM/ZFP as those targets do not support pools.
8343
8344 if VM_Target = No_VM
8345 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
8346 then
8347 Discard :=
8348 Add_Extra_Formal
8349 (E, RTE (RE_Root_Storage_Pool_Ptr),
8350 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8351 end if;
8352 end if;
8353
8354 -- In the case of functions whose result type needs finalization,
8355 -- add an extra formal which represents the finalization master.
8356
8357 if Needs_BIP_Finalization_Master (E) then
8358 Discard :=
8359 Add_Extra_Formal
8360 (E, RTE (RE_Finalization_Master_Ptr),
8361 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8362 end if;
8363
8364 -- When the result type contains tasks, add two extra formals: the
8365 -- master of the tasks to be created, and the caller's activation
8366 -- chain.
8367
8368 if Has_Task (Full_Subt) then
8369 Discard :=
8370 Add_Extra_Formal
8371 (E, RTE (RE_Master_Id),
8372 E, BIP_Formal_Suffix (BIP_Task_Master));
8373 Discard :=
8374 Add_Extra_Formal
8375 (E, RTE (RE_Activation_Chain_Access),
8376 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8377 end if;
8378
8379 -- All build-in-place functions get an extra formal that will be
8380 -- passed the address of the return object within the caller.
8381
8382 Formal_Typ :=
8383 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8384
8385 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
8386 Set_Etype (Formal_Typ, Formal_Typ);
8387 Set_Depends_On_Private
8388 (Formal_Typ, Has_Private_Component (Formal_Typ));
8389 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8390 Set_Is_Access_Constant (Formal_Typ, False);
8391
8392 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8393 -- the designated type comes from the limited view (for back-end
8394 -- purposes).
8395
8396 Set_From_Limited_With
8397 (Formal_Typ, From_Limited_With (Result_Subt));
8398
8399 Layout_Type (Formal_Typ);
8400
8401 Discard :=
8402 Add_Extra_Formal
8403 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8404 end;
8405 end if;
8406 end Create_Extra_Formals;
8407
8408 -----------------------------
8409 -- Enter_Overloaded_Entity --
8410 -----------------------------
8411
8412 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8413 E : Entity_Id := Current_Entity_In_Scope (S);
8414 C_E : Entity_Id := Current_Entity (S);
8415
8416 begin
8417 if Present (E) then
8418 Set_Has_Homonym (E);
8419 Set_Has_Homonym (S);
8420 end if;
8421
8422 Set_Is_Immediately_Visible (S);
8423 Set_Scope (S, Current_Scope);
8424
8425 -- Chain new entity if front of homonym in current scope, so that
8426 -- homonyms are contiguous.
8427
8428 if Present (E) and then E /= C_E then
8429 while Homonym (C_E) /= E loop
8430 C_E := Homonym (C_E);
8431 end loop;
8432
8433 Set_Homonym (C_E, S);
8434
8435 else
8436 E := C_E;
8437 Set_Current_Entity (S);
8438 end if;
8439
8440 Set_Homonym (S, E);
8441
8442 if Is_Inherited_Operation (S) then
8443 Append_Inherited_Subprogram (S);
8444 else
8445 Append_Entity (S, Current_Scope);
8446 end if;
8447
8448 Set_Public_Status (S);
8449
8450 if Debug_Flag_E then
8451 Write_Str ("New overloaded entity chain: ");
8452 Write_Name (Chars (S));
8453
8454 E := S;
8455 while Present (E) loop
8456 Write_Str (" "); Write_Int (Int (E));
8457 E := Homonym (E);
8458 end loop;
8459
8460 Write_Eol;
8461 end if;
8462
8463 -- Generate warning for hiding
8464
8465 if Warn_On_Hiding
8466 and then Comes_From_Source (S)
8467 and then In_Extended_Main_Source_Unit (S)
8468 then
8469 E := S;
8470 loop
8471 E := Homonym (E);
8472 exit when No (E);
8473
8474 -- Warn unless genuine overloading. Do not emit warning on
8475 -- hiding predefined operators in Standard (these are either an
8476 -- (artifact of our implicit declarations, or simple noise) but
8477 -- keep warning on a operator defined on a local subtype, because
8478 -- of the real danger that different operators may be applied in
8479 -- various parts of the program.
8480
8481 -- Note that if E and S have the same scope, there is never any
8482 -- hiding. Either the two conflict, and the program is illegal,
8483 -- or S is overriding an implicit inherited subprogram.
8484
8485 if Scope (E) /= Scope (S)
8486 and then (not Is_Overloadable (E)
8487 or else Subtype_Conformant (E, S))
8488 and then (Is_Immediately_Visible (E)
8489 or else
8490 Is_Potentially_Use_Visible (S))
8491 then
8492 if Scope (E) /= Standard_Standard then
8493 Error_Msg_Sloc := Sloc (E);
8494 Error_Msg_N ("declaration of & hides one#?h?", S);
8495
8496 elsif Nkind (S) = N_Defining_Operator_Symbol
8497 and then
8498 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
8499 then
8500 Error_Msg_N
8501 ("declaration of & hides predefined operator?h?", S);
8502 end if;
8503 end if;
8504 end loop;
8505 end if;
8506 end Enter_Overloaded_Entity;
8507
8508 -----------------------------
8509 -- Check_Untagged_Equality --
8510 -----------------------------
8511
8512 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8513 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8514 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8515 Obj_Decl : Node_Id;
8516
8517 begin
8518 -- This check applies only if we have a subprogram declaration with a
8519 -- non-tagged record type.
8520
8521 if Nkind (Decl) /= N_Subprogram_Declaration
8522 or else not Is_Record_Type (Typ)
8523 or else Is_Tagged_Type (Typ)
8524 then
8525 return;
8526 end if;
8527
8528 -- In Ada 2012 case, we will output errors or warnings depending on
8529 -- the setting of debug flag -gnatd.E.
8530
8531 if Ada_Version >= Ada_2012 then
8532 Error_Msg_Warn := Debug_Flag_Dot_EE;
8533
8534 -- In earlier versions of Ada, nothing to do unless we are warning on
8535 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
8536
8537 else
8538 if not Warn_On_Ada_2012_Compatibility then
8539 return;
8540 end if;
8541 end if;
8542
8543 -- Cases where the type has already been frozen
8544
8545 if Is_Frozen (Typ) then
8546
8547 -- If the type is not declared in a package, or if we are in the body
8548 -- of the package or in some other scope, the new operation is not
8549 -- primitive, and therefore legal, though suspicious. Should we
8550 -- generate a warning in this case ???
8551
8552 if Ekind (Scope (Typ)) /= E_Package
8553 or else Scope (Typ) /= Current_Scope
8554 then
8555 return;
8556
8557 -- If the type is a generic actual (sub)type, the operation is not
8558 -- primitive either because the base type is declared elsewhere.
8559
8560 elsif Is_Generic_Actual_Type (Typ) then
8561 return;
8562
8563 -- Here we have a definite error of declaration after freezing
8564
8565 else
8566 if Ada_Version >= Ada_2012 then
8567 Error_Msg_NE
8568 ("equality operator must be declared before type& is "
8569 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
8570
8571 -- In Ada 2012 mode with error turned to warning, output one
8572 -- more warning to warn that the equality operation may not
8573 -- compose. This is the consequence of ignoring the error.
8574
8575 if Error_Msg_Warn then
8576 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
8577 end if;
8578
8579 else
8580 Error_Msg_NE
8581 ("equality operator must be declared before type& is "
8582 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
8583 end if;
8584
8585 -- If we are in the package body, we could just move the
8586 -- declaration to the package spec, so add a message saying that.
8587
8588 if In_Package_Body (Scope (Typ)) then
8589 if Ada_Version >= Ada_2012 then
8590 Error_Msg_N
8591 ("\move declaration to package spec<<", Eq_Op);
8592 else
8593 Error_Msg_N
8594 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
8595 end if;
8596
8597 -- Otherwise try to find the freezing point
8598
8599 else
8600 Obj_Decl := Next (Parent (Typ));
8601 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8602 if Nkind (Obj_Decl) = N_Object_Declaration
8603 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8604 then
8605 -- Freezing point, output warnings
8606
8607 if Ada_Version >= Ada_2012 then
8608 Error_Msg_NE
8609 ("type& is frozen by declaration??", Obj_Decl, Typ);
8610 Error_Msg_N
8611 ("\an equality operator cannot be declared after "
8612 & "this point??",
8613 Obj_Decl);
8614 else
8615 Error_Msg_NE
8616 ("type& is frozen by declaration (Ada 2012)?y?",
8617 Obj_Decl, Typ);
8618 Error_Msg_N
8619 ("\an equality operator cannot be declared after "
8620 & "this point (Ada 2012)?y?",
8621 Obj_Decl);
8622 end if;
8623
8624 exit;
8625 end if;
8626
8627 Next (Obj_Decl);
8628 end loop;
8629 end if;
8630 end if;
8631
8632 -- Here if type is not frozen yet. It is illegal to have a primitive
8633 -- equality declared in the private part if the type is visible.
8634
8635 elsif not In_Same_List (Parent (Typ), Decl)
8636 and then not Is_Limited_Type (Typ)
8637 then
8638 -- Shouldn't we give an RM reference here???
8639
8640 if Ada_Version >= Ada_2012 then
8641 Error_Msg_N
8642 ("equality operator appears too late<<", Eq_Op);
8643 else
8644 Error_Msg_N
8645 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
8646 end if;
8647
8648 -- No error detected
8649
8650 else
8651 return;
8652 end if;
8653 end Check_Untagged_Equality;
8654
8655 -----------------------------
8656 -- Find_Corresponding_Spec --
8657 -----------------------------
8658
8659 function Find_Corresponding_Spec
8660 (N : Node_Id;
8661 Post_Error : Boolean := True) return Entity_Id
8662 is
8663 Spec : constant Node_Id := Specification (N);
8664 Designator : constant Entity_Id := Defining_Entity (Spec);
8665
8666 E : Entity_Id;
8667
8668 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8669 -- Even if fully conformant, a body may depend on a generic actual when
8670 -- the spec does not, or vice versa, in which case they were distinct
8671 -- entities in the generic.
8672
8673 -------------------------------
8674 -- Different_Generic_Profile --
8675 -------------------------------
8676
8677 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8678 F1, F2 : Entity_Id;
8679
8680 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
8681 -- Check that the types of corresponding formals have the same
8682 -- generic actual if any. We have to account for subtypes of a
8683 -- generic formal, declared between a spec and a body, which may
8684 -- appear distinct in an instance but matched in the generic.
8685
8686 -------------------------
8687 -- Same_Generic_Actual --
8688 -------------------------
8689
8690 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
8691 begin
8692 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
8693 or else
8694 (Present (Parent (T1))
8695 and then Comes_From_Source (Parent (T1))
8696 and then Nkind (Parent (T1)) = N_Subtype_Declaration
8697 and then Is_Entity_Name (Subtype_Indication (Parent (T1)))
8698 and then Entity (Subtype_Indication (Parent (T1))) = T2);
8699 end Same_Generic_Actual;
8700
8701 -- Start of processing for Different_Generic_Profile
8702
8703 begin
8704 if not In_Instance then
8705 return False;
8706
8707 elsif Ekind (E) = E_Function
8708 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
8709 then
8710 return True;
8711 end if;
8712
8713 F1 := First_Formal (Designator);
8714 F2 := First_Formal (E);
8715 while Present (F1) loop
8716 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
8717 return True;
8718 end if;
8719
8720 Next_Formal (F1);
8721 Next_Formal (F2);
8722 end loop;
8723
8724 return False;
8725 end Different_Generic_Profile;
8726
8727 -- Start of processing for Find_Corresponding_Spec
8728
8729 begin
8730 E := Current_Entity (Designator);
8731 while Present (E) loop
8732
8733 -- We are looking for a matching spec. It must have the same scope,
8734 -- and the same name, and either be type conformant, or be the case
8735 -- of a library procedure spec and its body (which belong to one
8736 -- another regardless of whether they are type conformant or not).
8737
8738 if Scope (E) = Current_Scope then
8739 if Current_Scope = Standard_Standard
8740 or else (Ekind (E) = Ekind (Designator)
8741 and then Type_Conformant (E, Designator))
8742 then
8743 -- Within an instantiation, we know that spec and body are
8744 -- subtype conformant, because they were subtype conformant in
8745 -- the generic. We choose the subtype-conformant entity here as
8746 -- well, to resolve spurious ambiguities in the instance that
8747 -- were not present in the generic (i.e. when two different
8748 -- types are given the same actual). If we are looking for a
8749 -- spec to match a body, full conformance is expected.
8750
8751 if In_Instance then
8752 Set_Convention (Designator, Convention (E));
8753
8754 -- Skip past subprogram bodies and subprogram renamings that
8755 -- may appear to have a matching spec, but that aren't fully
8756 -- conformant with it. That can occur in cases where an
8757 -- actual type causes unrelated homographs in the instance.
8758
8759 if Nkind_In (N, N_Subprogram_Body,
8760 N_Subprogram_Renaming_Declaration)
8761 and then Present (Homonym (E))
8762 and then not Fully_Conformant (Designator, E)
8763 then
8764 goto Next_Entity;
8765
8766 elsif not Subtype_Conformant (Designator, E) then
8767 goto Next_Entity;
8768
8769 elsif Different_Generic_Profile (E) then
8770 goto Next_Entity;
8771 end if;
8772 end if;
8773
8774 -- Ada 2012 (AI05-0165): For internally generated bodies of
8775 -- null procedures locate the internally generated spec. We
8776 -- enforce mode conformance since a tagged type may inherit
8777 -- from interfaces several null primitives which differ only
8778 -- in the mode of the formals.
8779
8780 if not (Comes_From_Source (E))
8781 and then Is_Null_Procedure (E)
8782 and then not Mode_Conformant (Designator, E)
8783 then
8784 null;
8785
8786 -- For null procedures coming from source that are completions,
8787 -- analysis of the generated body will establish the link.
8788
8789 elsif Comes_From_Source (E)
8790 and then Nkind (Spec) = N_Procedure_Specification
8791 and then Null_Present (Spec)
8792 then
8793 return E;
8794
8795 elsif not Has_Completion (E) then
8796 if Nkind (N) /= N_Subprogram_Body_Stub then
8797 Set_Corresponding_Spec (N, E);
8798 end if;
8799
8800 Set_Has_Completion (E);
8801 return E;
8802
8803 elsif Nkind (Parent (N)) = N_Subunit then
8804
8805 -- If this is the proper body of a subunit, the completion
8806 -- flag is set when analyzing the stub.
8807
8808 return E;
8809
8810 -- If E is an internal function with a controlling result that
8811 -- was created for an operation inherited by a null extension,
8812 -- it may be overridden by a body without a previous spec (one
8813 -- more reason why these should be shunned). In that case we
8814 -- remove the generated body if present, because the current
8815 -- one is the explicit overriding.
8816
8817 elsif Ekind (E) = E_Function
8818 and then Ada_Version >= Ada_2005
8819 and then not Comes_From_Source (E)
8820 and then Has_Controlling_Result (E)
8821 and then Is_Null_Extension (Etype (E))
8822 and then Comes_From_Source (Spec)
8823 then
8824 Set_Has_Completion (E, False);
8825
8826 if Expander_Active
8827 and then Nkind (Parent (E)) = N_Function_Specification
8828 then
8829 Remove
8830 (Unit_Declaration_Node
8831 (Corresponding_Body (Unit_Declaration_Node (E))));
8832
8833 return E;
8834
8835 -- If expansion is disabled, or if the wrapper function has
8836 -- not been generated yet, this a late body overriding an
8837 -- inherited operation, or it is an overriding by some other
8838 -- declaration before the controlling result is frozen. In
8839 -- either case this is a declaration of a new entity.
8840
8841 else
8842 return Empty;
8843 end if;
8844
8845 -- If the body already exists, then this is an error unless
8846 -- the previous declaration is the implicit declaration of a
8847 -- derived subprogram. It is also legal for an instance to
8848 -- contain type conformant overloadable declarations (but the
8849 -- generic declaration may not), per 8.3(26/2).
8850
8851 elsif No (Alias (E))
8852 and then not Is_Intrinsic_Subprogram (E)
8853 and then not In_Instance
8854 and then Post_Error
8855 then
8856 Error_Msg_Sloc := Sloc (E);
8857
8858 if Is_Imported (E) then
8859 Error_Msg_NE
8860 ("body not allowed for imported subprogram & declared#",
8861 N, E);
8862 else
8863 Error_Msg_NE ("duplicate body for & declared#", N, E);
8864 end if;
8865 end if;
8866
8867 -- Child units cannot be overloaded, so a conformance mismatch
8868 -- between body and a previous spec is an error.
8869
8870 elsif Is_Child_Unit (E)
8871 and then
8872 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8873 and then
8874 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8875 N_Compilation_Unit
8876 and then Post_Error
8877 then
8878 Error_Msg_N
8879 ("body of child unit does not match previous declaration", N);
8880 end if;
8881 end if;
8882
8883 <<Next_Entity>>
8884 E := Homonym (E);
8885 end loop;
8886
8887 -- On exit, we know that no previous declaration of subprogram exists
8888
8889 return Empty;
8890 end Find_Corresponding_Spec;
8891
8892 ----------------------
8893 -- Fully_Conformant --
8894 ----------------------
8895
8896 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8897 Result : Boolean;
8898 begin
8899 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8900 return Result;
8901 end Fully_Conformant;
8902
8903 ----------------------------------
8904 -- Fully_Conformant_Expressions --
8905 ----------------------------------
8906
8907 function Fully_Conformant_Expressions
8908 (Given_E1 : Node_Id;
8909 Given_E2 : Node_Id) return Boolean
8910 is
8911 E1 : constant Node_Id := Original_Node (Given_E1);
8912 E2 : constant Node_Id := Original_Node (Given_E2);
8913 -- We always test conformance on original nodes, since it is possible
8914 -- for analysis and/or expansion to make things look as though they
8915 -- conform when they do not, e.g. by converting 1+2 into 3.
8916
8917 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8918 renames Fully_Conformant_Expressions;
8919
8920 function FCL (L1, L2 : List_Id) return Boolean;
8921 -- Compare elements of two lists for conformance. Elements have to be
8922 -- conformant, and actuals inserted as default parameters do not match
8923 -- explicit actuals with the same value.
8924
8925 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8926 -- Compare an operator node with a function call
8927
8928 ---------
8929 -- FCL --
8930 ---------
8931
8932 function FCL (L1, L2 : List_Id) return Boolean is
8933 N1, N2 : Node_Id;
8934
8935 begin
8936 if L1 = No_List then
8937 N1 := Empty;
8938 else
8939 N1 := First (L1);
8940 end if;
8941
8942 if L2 = No_List then
8943 N2 := Empty;
8944 else
8945 N2 := First (L2);
8946 end if;
8947
8948 -- Compare two lists, skipping rewrite insertions (we want to compare
8949 -- the original trees, not the expanded versions).
8950
8951 loop
8952 if Is_Rewrite_Insertion (N1) then
8953 Next (N1);
8954 elsif Is_Rewrite_Insertion (N2) then
8955 Next (N2);
8956 elsif No (N1) then
8957 return No (N2);
8958 elsif No (N2) then
8959 return False;
8960 elsif not FCE (N1, N2) then
8961 return False;
8962 else
8963 Next (N1);
8964 Next (N2);
8965 end if;
8966 end loop;
8967 end FCL;
8968
8969 ---------
8970 -- FCO --
8971 ---------
8972
8973 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8974 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8975 Act : Node_Id;
8976
8977 begin
8978 if No (Actuals)
8979 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8980 then
8981 return False;
8982
8983 else
8984 Act := First (Actuals);
8985
8986 if Nkind (Op_Node) in N_Binary_Op then
8987 if not FCE (Left_Opnd (Op_Node), Act) then
8988 return False;
8989 end if;
8990
8991 Next (Act);
8992 end if;
8993
8994 return Present (Act)
8995 and then FCE (Right_Opnd (Op_Node), Act)
8996 and then No (Next (Act));
8997 end if;
8998 end FCO;
8999
9000 -- Start of processing for Fully_Conformant_Expressions
9001
9002 begin
9003 -- Non-conformant if paren count does not match. Note: if some idiot
9004 -- complains that we don't do this right for more than 3 levels of
9005 -- parentheses, they will be treated with the respect they deserve.
9006
9007 if Paren_Count (E1) /= Paren_Count (E2) then
9008 return False;
9009
9010 -- If same entities are referenced, then they are conformant even if
9011 -- they have different forms (RM 8.3.1(19-20)).
9012
9013 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
9014 if Present (Entity (E1)) then
9015 return Entity (E1) = Entity (E2)
9016 or else (Chars (Entity (E1)) = Chars (Entity (E2))
9017 and then Ekind (Entity (E1)) = E_Discriminant
9018 and then Ekind (Entity (E2)) = E_In_Parameter);
9019
9020 elsif Nkind (E1) = N_Expanded_Name
9021 and then Nkind (E2) = N_Expanded_Name
9022 and then Nkind (Selector_Name (E1)) = N_Character_Literal
9023 and then Nkind (Selector_Name (E2)) = N_Character_Literal
9024 then
9025 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
9026
9027 else
9028 -- Identifiers in component associations don't always have
9029 -- entities, but their names must conform.
9030
9031 return Nkind (E1) = N_Identifier
9032 and then Nkind (E2) = N_Identifier
9033 and then Chars (E1) = Chars (E2);
9034 end if;
9035
9036 elsif Nkind (E1) = N_Character_Literal
9037 and then Nkind (E2) = N_Expanded_Name
9038 then
9039 return Nkind (Selector_Name (E2)) = N_Character_Literal
9040 and then Chars (E1) = Chars (Selector_Name (E2));
9041
9042 elsif Nkind (E2) = N_Character_Literal
9043 and then Nkind (E1) = N_Expanded_Name
9044 then
9045 return Nkind (Selector_Name (E1)) = N_Character_Literal
9046 and then Chars (E2) = Chars (Selector_Name (E1));
9047
9048 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
9049 return FCO (E1, E2);
9050
9051 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
9052 return FCO (E2, E1);
9053
9054 -- Otherwise we must have the same syntactic entity
9055
9056 elsif Nkind (E1) /= Nkind (E2) then
9057 return False;
9058
9059 -- At this point, we specialize by node type
9060
9061 else
9062 case Nkind (E1) is
9063
9064 when N_Aggregate =>
9065 return
9066 FCL (Expressions (E1), Expressions (E2))
9067 and then
9068 FCL (Component_Associations (E1),
9069 Component_Associations (E2));
9070
9071 when N_Allocator =>
9072 if Nkind (Expression (E1)) = N_Qualified_Expression
9073 or else
9074 Nkind (Expression (E2)) = N_Qualified_Expression
9075 then
9076 return FCE (Expression (E1), Expression (E2));
9077
9078 -- Check that the subtype marks and any constraints
9079 -- are conformant
9080
9081 else
9082 declare
9083 Indic1 : constant Node_Id := Expression (E1);
9084 Indic2 : constant Node_Id := Expression (E2);
9085 Elt1 : Node_Id;
9086 Elt2 : Node_Id;
9087
9088 begin
9089 if Nkind (Indic1) /= N_Subtype_Indication then
9090 return
9091 Nkind (Indic2) /= N_Subtype_Indication
9092 and then Entity (Indic1) = Entity (Indic2);
9093
9094 elsif Nkind (Indic2) /= N_Subtype_Indication then
9095 return
9096 Nkind (Indic1) /= N_Subtype_Indication
9097 and then Entity (Indic1) = Entity (Indic2);
9098
9099 else
9100 if Entity (Subtype_Mark (Indic1)) /=
9101 Entity (Subtype_Mark (Indic2))
9102 then
9103 return False;
9104 end if;
9105
9106 Elt1 := First (Constraints (Constraint (Indic1)));
9107 Elt2 := First (Constraints (Constraint (Indic2)));
9108 while Present (Elt1) and then Present (Elt2) loop
9109 if not FCE (Elt1, Elt2) then
9110 return False;
9111 end if;
9112
9113 Next (Elt1);
9114 Next (Elt2);
9115 end loop;
9116
9117 return True;
9118 end if;
9119 end;
9120 end if;
9121
9122 when N_Attribute_Reference =>
9123 return
9124 Attribute_Name (E1) = Attribute_Name (E2)
9125 and then FCL (Expressions (E1), Expressions (E2));
9126
9127 when N_Binary_Op =>
9128 return
9129 Entity (E1) = Entity (E2)
9130 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
9131 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
9132
9133 when N_Short_Circuit | N_Membership_Test =>
9134 return
9135 FCE (Left_Opnd (E1), Left_Opnd (E2))
9136 and then
9137 FCE (Right_Opnd (E1), Right_Opnd (E2));
9138
9139 when N_Case_Expression =>
9140 declare
9141 Alt1 : Node_Id;
9142 Alt2 : Node_Id;
9143
9144 begin
9145 if not FCE (Expression (E1), Expression (E2)) then
9146 return False;
9147
9148 else
9149 Alt1 := First (Alternatives (E1));
9150 Alt2 := First (Alternatives (E2));
9151 loop
9152 if Present (Alt1) /= Present (Alt2) then
9153 return False;
9154 elsif No (Alt1) then
9155 return True;
9156 end if;
9157
9158 if not FCE (Expression (Alt1), Expression (Alt2))
9159 or else not FCL (Discrete_Choices (Alt1),
9160 Discrete_Choices (Alt2))
9161 then
9162 return False;
9163 end if;
9164
9165 Next (Alt1);
9166 Next (Alt2);
9167 end loop;
9168 end if;
9169 end;
9170
9171 when N_Character_Literal =>
9172 return
9173 Char_Literal_Value (E1) = Char_Literal_Value (E2);
9174
9175 when N_Component_Association =>
9176 return
9177 FCL (Choices (E1), Choices (E2))
9178 and then
9179 FCE (Expression (E1), Expression (E2));
9180
9181 when N_Explicit_Dereference =>
9182 return
9183 FCE (Prefix (E1), Prefix (E2));
9184
9185 when N_Extension_Aggregate =>
9186 return
9187 FCL (Expressions (E1), Expressions (E2))
9188 and then Null_Record_Present (E1) =
9189 Null_Record_Present (E2)
9190 and then FCL (Component_Associations (E1),
9191 Component_Associations (E2));
9192
9193 when N_Function_Call =>
9194 return
9195 FCE (Name (E1), Name (E2))
9196 and then
9197 FCL (Parameter_Associations (E1),
9198 Parameter_Associations (E2));
9199
9200 when N_If_Expression =>
9201 return
9202 FCL (Expressions (E1), Expressions (E2));
9203
9204 when N_Indexed_Component =>
9205 return
9206 FCE (Prefix (E1), Prefix (E2))
9207 and then
9208 FCL (Expressions (E1), Expressions (E2));
9209
9210 when N_Integer_Literal =>
9211 return (Intval (E1) = Intval (E2));
9212
9213 when N_Null =>
9214 return True;
9215
9216 when N_Operator_Symbol =>
9217 return
9218 Chars (E1) = Chars (E2);
9219
9220 when N_Others_Choice =>
9221 return True;
9222
9223 when N_Parameter_Association =>
9224 return
9225 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
9226 and then FCE (Explicit_Actual_Parameter (E1),
9227 Explicit_Actual_Parameter (E2));
9228
9229 when N_Qualified_Expression =>
9230 return
9231 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9232 and then
9233 FCE (Expression (E1), Expression (E2));
9234
9235 when N_Quantified_Expression =>
9236 if not FCE (Condition (E1), Condition (E2)) then
9237 return False;
9238 end if;
9239
9240 if Present (Loop_Parameter_Specification (E1))
9241 and then Present (Loop_Parameter_Specification (E2))
9242 then
9243 declare
9244 L1 : constant Node_Id :=
9245 Loop_Parameter_Specification (E1);
9246 L2 : constant Node_Id :=
9247 Loop_Parameter_Specification (E2);
9248
9249 begin
9250 return
9251 Reverse_Present (L1) = Reverse_Present (L2)
9252 and then
9253 FCE (Defining_Identifier (L1),
9254 Defining_Identifier (L2))
9255 and then
9256 FCE (Discrete_Subtype_Definition (L1),
9257 Discrete_Subtype_Definition (L2));
9258 end;
9259
9260 elsif Present (Iterator_Specification (E1))
9261 and then Present (Iterator_Specification (E2))
9262 then
9263 declare
9264 I1 : constant Node_Id := Iterator_Specification (E1);
9265 I2 : constant Node_Id := Iterator_Specification (E2);
9266
9267 begin
9268 return
9269 FCE (Defining_Identifier (I1),
9270 Defining_Identifier (I2))
9271 and then
9272 Of_Present (I1) = Of_Present (I2)
9273 and then
9274 Reverse_Present (I1) = Reverse_Present (I2)
9275 and then FCE (Name (I1), Name (I2))
9276 and then FCE (Subtype_Indication (I1),
9277 Subtype_Indication (I2));
9278 end;
9279
9280 -- The quantified expressions used different specifications to
9281 -- walk their respective ranges.
9282
9283 else
9284 return False;
9285 end if;
9286
9287 when N_Range =>
9288 return
9289 FCE (Low_Bound (E1), Low_Bound (E2))
9290 and then
9291 FCE (High_Bound (E1), High_Bound (E2));
9292
9293 when N_Real_Literal =>
9294 return (Realval (E1) = Realval (E2));
9295
9296 when N_Selected_Component =>
9297 return
9298 FCE (Prefix (E1), Prefix (E2))
9299 and then
9300 FCE (Selector_Name (E1), Selector_Name (E2));
9301
9302 when N_Slice =>
9303 return
9304 FCE (Prefix (E1), Prefix (E2))
9305 and then
9306 FCE (Discrete_Range (E1), Discrete_Range (E2));
9307
9308 when N_String_Literal =>
9309 declare
9310 S1 : constant String_Id := Strval (E1);
9311 S2 : constant String_Id := Strval (E2);
9312 L1 : constant Nat := String_Length (S1);
9313 L2 : constant Nat := String_Length (S2);
9314
9315 begin
9316 if L1 /= L2 then
9317 return False;
9318
9319 else
9320 for J in 1 .. L1 loop
9321 if Get_String_Char (S1, J) /=
9322 Get_String_Char (S2, J)
9323 then
9324 return False;
9325 end if;
9326 end loop;
9327
9328 return True;
9329 end if;
9330 end;
9331
9332 when N_Type_Conversion =>
9333 return
9334 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9335 and then
9336 FCE (Expression (E1), Expression (E2));
9337
9338 when N_Unary_Op =>
9339 return
9340 Entity (E1) = Entity (E2)
9341 and then
9342 FCE (Right_Opnd (E1), Right_Opnd (E2));
9343
9344 when N_Unchecked_Type_Conversion =>
9345 return
9346 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9347 and then
9348 FCE (Expression (E1), Expression (E2));
9349
9350 -- All other node types cannot appear in this context. Strictly
9351 -- we should raise a fatal internal error. Instead we just ignore
9352 -- the nodes. This means that if anyone makes a mistake in the
9353 -- expander and mucks an expression tree irretrievably, the result
9354 -- will be a failure to detect a (probably very obscure) case
9355 -- of non-conformance, which is better than bombing on some
9356 -- case where two expressions do in fact conform.
9357
9358 when others =>
9359 return True;
9360
9361 end case;
9362 end if;
9363 end Fully_Conformant_Expressions;
9364
9365 ----------------------------------------
9366 -- Fully_Conformant_Discrete_Subtypes --
9367 ----------------------------------------
9368
9369 function Fully_Conformant_Discrete_Subtypes
9370 (Given_S1 : Node_Id;
9371 Given_S2 : Node_Id) return Boolean
9372 is
9373 S1 : constant Node_Id := Original_Node (Given_S1);
9374 S2 : constant Node_Id := Original_Node (Given_S2);
9375
9376 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9377 -- Special-case for a bound given by a discriminant, which in the body
9378 -- is replaced with the discriminal of the enclosing type.
9379
9380 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9381 -- Check both bounds
9382
9383 -----------------------
9384 -- Conforming_Bounds --
9385 -----------------------
9386
9387 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9388 begin
9389 if Is_Entity_Name (B1)
9390 and then Is_Entity_Name (B2)
9391 and then Ekind (Entity (B1)) = E_Discriminant
9392 then
9393 return Chars (B1) = Chars (B2);
9394
9395 else
9396 return Fully_Conformant_Expressions (B1, B2);
9397 end if;
9398 end Conforming_Bounds;
9399
9400 -----------------------
9401 -- Conforming_Ranges --
9402 -----------------------
9403
9404 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9405 begin
9406 return
9407 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9408 and then
9409 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9410 end Conforming_Ranges;
9411
9412 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9413
9414 begin
9415 if Nkind (S1) /= Nkind (S2) then
9416 return False;
9417
9418 elsif Is_Entity_Name (S1) then
9419 return Entity (S1) = Entity (S2);
9420
9421 elsif Nkind (S1) = N_Range then
9422 return Conforming_Ranges (S1, S2);
9423
9424 elsif Nkind (S1) = N_Subtype_Indication then
9425 return
9426 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9427 and then
9428 Conforming_Ranges
9429 (Range_Expression (Constraint (S1)),
9430 Range_Expression (Constraint (S2)));
9431 else
9432 return True;
9433 end if;
9434 end Fully_Conformant_Discrete_Subtypes;
9435
9436 --------------------
9437 -- Install_Entity --
9438 --------------------
9439
9440 procedure Install_Entity (E : Entity_Id) is
9441 Prev : constant Entity_Id := Current_Entity (E);
9442 begin
9443 Set_Is_Immediately_Visible (E);
9444 Set_Current_Entity (E);
9445 Set_Homonym (E, Prev);
9446 end Install_Entity;
9447
9448 ---------------------
9449 -- Install_Formals --
9450 ---------------------
9451
9452 procedure Install_Formals (Id : Entity_Id) is
9453 F : Entity_Id;
9454 begin
9455 F := First_Formal (Id);
9456 while Present (F) loop
9457 Install_Entity (F);
9458 Next_Formal (F);
9459 end loop;
9460 end Install_Formals;
9461
9462 -----------------------------
9463 -- Is_Interface_Conformant --
9464 -----------------------------
9465
9466 function Is_Interface_Conformant
9467 (Tagged_Type : Entity_Id;
9468 Iface_Prim : Entity_Id;
9469 Prim : Entity_Id) return Boolean
9470 is
9471 -- The operation may in fact be an inherited (implicit) operation
9472 -- rather than the original interface primitive, so retrieve the
9473 -- ultimate ancestor.
9474
9475 Iface : constant Entity_Id :=
9476 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9477 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9478
9479 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9480 -- Return the controlling formal of Prim
9481
9482 ------------------------
9483 -- Controlling_Formal --
9484 ------------------------
9485
9486 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9487 E : Entity_Id;
9488
9489 begin
9490 E := First_Entity (Prim);
9491 while Present (E) loop
9492 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9493 return E;
9494 end if;
9495
9496 Next_Entity (E);
9497 end loop;
9498
9499 return Empty;
9500 end Controlling_Formal;
9501
9502 -- Local variables
9503
9504 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9505 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9506
9507 -- Start of processing for Is_Interface_Conformant
9508
9509 begin
9510 pragma Assert (Is_Subprogram (Iface_Prim)
9511 and then Is_Subprogram (Prim)
9512 and then Is_Dispatching_Operation (Iface_Prim)
9513 and then Is_Dispatching_Operation (Prim));
9514
9515 pragma Assert (Is_Interface (Iface)
9516 or else (Present (Alias (Iface_Prim))
9517 and then
9518 Is_Interface
9519 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9520
9521 if Prim = Iface_Prim
9522 or else not Is_Subprogram (Prim)
9523 or else Ekind (Prim) /= Ekind (Iface_Prim)
9524 or else not Is_Dispatching_Operation (Prim)
9525 or else Scope (Prim) /= Scope (Tagged_Type)
9526 or else No (Typ)
9527 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9528 or else not Primitive_Names_Match (Iface_Prim, Prim)
9529 then
9530 return False;
9531
9532 -- The mode of the controlling formals must match
9533
9534 elsif Present (Iface_Ctrl_F)
9535 and then Present (Prim_Ctrl_F)
9536 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9537 then
9538 return False;
9539
9540 -- Case of a procedure, or a function whose result type matches the
9541 -- result type of the interface primitive, or a function that has no
9542 -- controlling result (I or access I).
9543
9544 elsif Ekind (Iface_Prim) = E_Procedure
9545 or else Etype (Prim) = Etype (Iface_Prim)
9546 or else not Has_Controlling_Result (Prim)
9547 then
9548 return Type_Conformant
9549 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9550
9551 -- Case of a function returning an interface, or an access to one. Check
9552 -- that the return types correspond.
9553
9554 elsif Implements_Interface (Typ, Iface) then
9555 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9556 /=
9557 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9558 then
9559 return False;
9560 else
9561 return
9562 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
9563 Skip_Controlling_Formals => True);
9564 end if;
9565
9566 else
9567 return False;
9568 end if;
9569 end Is_Interface_Conformant;
9570
9571 ---------------------------------
9572 -- Is_Non_Overriding_Operation --
9573 ---------------------------------
9574
9575 function Is_Non_Overriding_Operation
9576 (Prev_E : Entity_Id;
9577 New_E : Entity_Id) return Boolean
9578 is
9579 Formal : Entity_Id;
9580 F_Typ : Entity_Id;
9581 G_Typ : Entity_Id := Empty;
9582
9583 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9584 -- If F_Type is a derived type associated with a generic actual subtype,
9585 -- then return its Generic_Parent_Type attribute, else return Empty.
9586
9587 function Types_Correspond
9588 (P_Type : Entity_Id;
9589 N_Type : Entity_Id) return Boolean;
9590 -- Returns true if and only if the types (or designated types in the
9591 -- case of anonymous access types) are the same or N_Type is derived
9592 -- directly or indirectly from P_Type.
9593
9594 -----------------------------
9595 -- Get_Generic_Parent_Type --
9596 -----------------------------
9597
9598 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9599 G_Typ : Entity_Id;
9600 Defn : Node_Id;
9601 Indic : Node_Id;
9602
9603 begin
9604 if Is_Derived_Type (F_Typ)
9605 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9606 then
9607 -- The tree must be traversed to determine the parent subtype in
9608 -- the generic unit, which unfortunately isn't always available
9609 -- via semantic attributes. ??? (Note: The use of Original_Node
9610 -- is needed for cases where a full derived type has been
9611 -- rewritten.)
9612
9613 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9614 if Nkind (Defn) = N_Derived_Type_Definition then
9615 Indic := Subtype_Indication (Defn);
9616
9617 if Nkind (Indic) = N_Subtype_Indication then
9618 G_Typ := Entity (Subtype_Mark (Indic));
9619 else
9620 G_Typ := Entity (Indic);
9621 end if;
9622
9623 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9624 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9625 then
9626 return Generic_Parent_Type (Parent (G_Typ));
9627 end if;
9628 end if;
9629 end if;
9630
9631 return Empty;
9632 end Get_Generic_Parent_Type;
9633
9634 ----------------------
9635 -- Types_Correspond --
9636 ----------------------
9637
9638 function Types_Correspond
9639 (P_Type : Entity_Id;
9640 N_Type : Entity_Id) return Boolean
9641 is
9642 Prev_Type : Entity_Id := Base_Type (P_Type);
9643 New_Type : Entity_Id := Base_Type (N_Type);
9644
9645 begin
9646 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9647 Prev_Type := Designated_Type (Prev_Type);
9648 end if;
9649
9650 if Ekind (New_Type) = E_Anonymous_Access_Type then
9651 New_Type := Designated_Type (New_Type);
9652 end if;
9653
9654 if Prev_Type = New_Type then
9655 return True;
9656
9657 elsif not Is_Class_Wide_Type (New_Type) then
9658 while Etype (New_Type) /= New_Type loop
9659 New_Type := Etype (New_Type);
9660 if New_Type = Prev_Type then
9661 return True;
9662 end if;
9663 end loop;
9664 end if;
9665 return False;
9666 end Types_Correspond;
9667
9668 -- Start of processing for Is_Non_Overriding_Operation
9669
9670 begin
9671 -- In the case where both operations are implicit derived subprograms
9672 -- then neither overrides the other. This can only occur in certain
9673 -- obscure cases (e.g., derivation from homographs created in a generic
9674 -- instantiation).
9675
9676 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9677 return True;
9678
9679 elsif Ekind (Current_Scope) = E_Package
9680 and then Is_Generic_Instance (Current_Scope)
9681 and then In_Private_Part (Current_Scope)
9682 and then Comes_From_Source (New_E)
9683 then
9684 -- We examine the formals and result type of the inherited operation,
9685 -- to determine whether their type is derived from (the instance of)
9686 -- a generic type. The first such formal or result type is the one
9687 -- tested.
9688
9689 Formal := First_Formal (Prev_E);
9690 while Present (Formal) loop
9691 F_Typ := Base_Type (Etype (Formal));
9692
9693 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9694 F_Typ := Designated_Type (F_Typ);
9695 end if;
9696
9697 G_Typ := Get_Generic_Parent_Type (F_Typ);
9698 exit when Present (G_Typ);
9699
9700 Next_Formal (Formal);
9701 end loop;
9702
9703 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9704 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9705 end if;
9706
9707 if No (G_Typ) then
9708 return False;
9709 end if;
9710
9711 -- If the generic type is a private type, then the original operation
9712 -- was not overriding in the generic, because there was no primitive
9713 -- operation to override.
9714
9715 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9716 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9717 N_Formal_Private_Type_Definition
9718 then
9719 return True;
9720
9721 -- The generic parent type is the ancestor of a formal derived
9722 -- type declaration. We need to check whether it has a primitive
9723 -- operation that should be overridden by New_E in the generic.
9724
9725 else
9726 declare
9727 P_Formal : Entity_Id;
9728 N_Formal : Entity_Id;
9729 P_Typ : Entity_Id;
9730 N_Typ : Entity_Id;
9731 P_Prim : Entity_Id;
9732 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9733
9734 begin
9735 while Present (Prim_Elt) loop
9736 P_Prim := Node (Prim_Elt);
9737
9738 if Chars (P_Prim) = Chars (New_E)
9739 and then Ekind (P_Prim) = Ekind (New_E)
9740 then
9741 P_Formal := First_Formal (P_Prim);
9742 N_Formal := First_Formal (New_E);
9743 while Present (P_Formal) and then Present (N_Formal) loop
9744 P_Typ := Etype (P_Formal);
9745 N_Typ := Etype (N_Formal);
9746
9747 if not Types_Correspond (P_Typ, N_Typ) then
9748 exit;
9749 end if;
9750
9751 Next_Entity (P_Formal);
9752 Next_Entity (N_Formal);
9753 end loop;
9754
9755 -- Found a matching primitive operation belonging to the
9756 -- formal ancestor type, so the new subprogram is
9757 -- overriding.
9758
9759 if No (P_Formal)
9760 and then No (N_Formal)
9761 and then (Ekind (New_E) /= E_Function
9762 or else
9763 Types_Correspond
9764 (Etype (P_Prim), Etype (New_E)))
9765 then
9766 return False;
9767 end if;
9768 end if;
9769
9770 Next_Elmt (Prim_Elt);
9771 end loop;
9772
9773 -- If no match found, then the new subprogram does not override
9774 -- in the generic (nor in the instance).
9775
9776 -- If the type in question is not abstract, and the subprogram
9777 -- is, this will be an error if the new operation is in the
9778 -- private part of the instance. Emit a warning now, which will
9779 -- make the subsequent error message easier to understand.
9780
9781 if not Is_Abstract_Type (F_Typ)
9782 and then Is_Abstract_Subprogram (Prev_E)
9783 and then In_Private_Part (Current_Scope)
9784 then
9785 Error_Msg_Node_2 := F_Typ;
9786 Error_Msg_NE
9787 ("private operation& in generic unit does not override " &
9788 "any primitive operation of& (RM 12.3 (18))??",
9789 New_E, New_E);
9790 end if;
9791
9792 return True;
9793 end;
9794 end if;
9795 else
9796 return False;
9797 end if;
9798 end Is_Non_Overriding_Operation;
9799
9800 -------------------------------------
9801 -- List_Inherited_Pre_Post_Aspects --
9802 -------------------------------------
9803
9804 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9805 begin
9806 if Opt.List_Inherited_Aspects
9807 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
9808 then
9809 declare
9810 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
9811 P : Node_Id;
9812
9813 begin
9814 for J in Inherited'Range loop
9815 P := Pre_Post_Conditions (Contract (Inherited (J)));
9816 while Present (P) loop
9817 Error_Msg_Sloc := Sloc (P);
9818
9819 if Class_Present (P) and then not Split_PPC (P) then
9820 if Pragma_Name (P) = Name_Precondition then
9821 Error_Msg_N
9822 ("info: & inherits `Pre''Class` aspect from #?L?",
9823 E);
9824 else
9825 Error_Msg_N
9826 ("info: & inherits `Post''Class` aspect from #?L?",
9827 E);
9828 end if;
9829 end if;
9830
9831 P := Next_Pragma (P);
9832 end loop;
9833 end loop;
9834 end;
9835 end if;
9836 end List_Inherited_Pre_Post_Aspects;
9837
9838 ------------------------------
9839 -- Make_Inequality_Operator --
9840 ------------------------------
9841
9842 -- S is the defining identifier of an equality operator. We build a
9843 -- subprogram declaration with the right signature. This operation is
9844 -- intrinsic, because it is always expanded as the negation of the
9845 -- call to the equality function.
9846
9847 procedure Make_Inequality_Operator (S : Entity_Id) is
9848 Loc : constant Source_Ptr := Sloc (S);
9849 Decl : Node_Id;
9850 Formals : List_Id;
9851 Op_Name : Entity_Id;
9852
9853 FF : constant Entity_Id := First_Formal (S);
9854 NF : constant Entity_Id := Next_Formal (FF);
9855
9856 begin
9857 -- Check that equality was properly defined, ignore call if not
9858
9859 if No (NF) then
9860 return;
9861 end if;
9862
9863 declare
9864 A : constant Entity_Id :=
9865 Make_Defining_Identifier (Sloc (FF),
9866 Chars => Chars (FF));
9867
9868 B : constant Entity_Id :=
9869 Make_Defining_Identifier (Sloc (NF),
9870 Chars => Chars (NF));
9871
9872 begin
9873 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9874
9875 Formals := New_List (
9876 Make_Parameter_Specification (Loc,
9877 Defining_Identifier => A,
9878 Parameter_Type =>
9879 New_Occurrence_Of (Etype (First_Formal (S)),
9880 Sloc (Etype (First_Formal (S))))),
9881
9882 Make_Parameter_Specification (Loc,
9883 Defining_Identifier => B,
9884 Parameter_Type =>
9885 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
9886 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9887
9888 Decl :=
9889 Make_Subprogram_Declaration (Loc,
9890 Specification =>
9891 Make_Function_Specification (Loc,
9892 Defining_Unit_Name => Op_Name,
9893 Parameter_Specifications => Formals,
9894 Result_Definition =>
9895 New_Occurrence_Of (Standard_Boolean, Loc)));
9896
9897 -- Insert inequality right after equality if it is explicit or after
9898 -- the derived type when implicit. These entities are created only
9899 -- for visibility purposes, and eventually replaced in the course
9900 -- of expansion, so they do not need to be attached to the tree and
9901 -- seen by the back-end. Keeping them internal also avoids spurious
9902 -- freezing problems. The declaration is inserted in the tree for
9903 -- analysis, and removed afterwards. If the equality operator comes
9904 -- from an explicit declaration, attach the inequality immediately
9905 -- after. Else the equality is inherited from a derived type
9906 -- declaration, so insert inequality after that declaration.
9907
9908 if No (Alias (S)) then
9909 Insert_After (Unit_Declaration_Node (S), Decl);
9910 elsif Is_List_Member (Parent (S)) then
9911 Insert_After (Parent (S), Decl);
9912 else
9913 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9914 end if;
9915
9916 Mark_Rewrite_Insertion (Decl);
9917 Set_Is_Intrinsic_Subprogram (Op_Name);
9918 Analyze (Decl);
9919 Remove (Decl);
9920 Set_Has_Completion (Op_Name);
9921 Set_Corresponding_Equality (Op_Name, S);
9922 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9923 end;
9924 end Make_Inequality_Operator;
9925
9926 ----------------------
9927 -- May_Need_Actuals --
9928 ----------------------
9929
9930 procedure May_Need_Actuals (Fun : Entity_Id) is
9931 F : Entity_Id;
9932 B : Boolean;
9933
9934 begin
9935 F := First_Formal (Fun);
9936 B := True;
9937 while Present (F) loop
9938 if No (Default_Value (F)) then
9939 B := False;
9940 exit;
9941 end if;
9942
9943 Next_Formal (F);
9944 end loop;
9945
9946 Set_Needs_No_Actuals (Fun, B);
9947 end May_Need_Actuals;
9948
9949 ---------------------
9950 -- Mode_Conformant --
9951 ---------------------
9952
9953 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9954 Result : Boolean;
9955 begin
9956 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9957 return Result;
9958 end Mode_Conformant;
9959
9960 ---------------------------
9961 -- New_Overloaded_Entity --
9962 ---------------------------
9963
9964 procedure New_Overloaded_Entity
9965 (S : Entity_Id;
9966 Derived_Type : Entity_Id := Empty)
9967 is
9968 Overridden_Subp : Entity_Id := Empty;
9969 -- Set if the current scope has an operation that is type-conformant
9970 -- with S, and becomes hidden by S.
9971
9972 Is_Primitive_Subp : Boolean;
9973 -- Set to True if the new subprogram is primitive
9974
9975 E : Entity_Id;
9976 -- Entity that S overrides
9977
9978 Prev_Vis : Entity_Id := Empty;
9979 -- Predecessor of E in Homonym chain
9980
9981 procedure Check_For_Primitive_Subprogram
9982 (Is_Primitive : out Boolean;
9983 Is_Overriding : Boolean := False);
9984 -- If the subprogram being analyzed is a primitive operation of the type
9985 -- of a formal or result, set the Has_Primitive_Operations flag on the
9986 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9987 -- corresponding flag on the entity itself for later use.
9988
9989 procedure Check_Synchronized_Overriding
9990 (Def_Id : Entity_Id;
9991 Overridden_Subp : out Entity_Id);
9992 -- First determine if Def_Id is an entry or a subprogram either defined
9993 -- in the scope of a task or protected type, or is a primitive of such
9994 -- a type. Check whether Def_Id overrides a subprogram of an interface
9995 -- implemented by the synchronized type, return the overridden entity
9996 -- or Empty.
9997
9998 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9999 -- Check that E is declared in the private part of the current package,
10000 -- or in the package body, where it may hide a previous declaration.
10001 -- We can't use In_Private_Part by itself because this flag is also
10002 -- set when freezing entities, so we must examine the place of the
10003 -- declaration in the tree, and recognize wrapper packages as well.
10004
10005 function Is_Overriding_Alias
10006 (Old_E : Entity_Id;
10007 New_E : Entity_Id) return Boolean;
10008 -- Check whether new subprogram and old subprogram are both inherited
10009 -- from subprograms that have distinct dispatch table entries. This can
10010 -- occur with derivations from instances with accidental homonyms. The
10011 -- function is conservative given that the converse is only true within
10012 -- instances that contain accidental overloadings.
10013
10014 ------------------------------------
10015 -- Check_For_Primitive_Subprogram --
10016 ------------------------------------
10017
10018 procedure Check_For_Primitive_Subprogram
10019 (Is_Primitive : out Boolean;
10020 Is_Overriding : Boolean := False)
10021 is
10022 Formal : Entity_Id;
10023 F_Typ : Entity_Id;
10024 B_Typ : Entity_Id;
10025
10026 function Visible_Part_Type (T : Entity_Id) return Boolean;
10027 -- Returns true if T is declared in the visible part of the current
10028 -- package scope; otherwise returns false. Assumes that T is declared
10029 -- in a package.
10030
10031 procedure Check_Private_Overriding (T : Entity_Id);
10032 -- Checks that if a primitive abstract subprogram of a visible
10033 -- abstract type is declared in a private part, then it must override
10034 -- an abstract subprogram declared in the visible part. Also checks
10035 -- that if a primitive function with a controlling result is declared
10036 -- in a private part, then it must override a function declared in
10037 -- the visible part.
10038
10039 ------------------------------
10040 -- Check_Private_Overriding --
10041 ------------------------------
10042
10043 procedure Check_Private_Overriding (T : Entity_Id) is
10044 begin
10045 if Is_Package_Or_Generic_Package (Current_Scope)
10046 and then In_Private_Part (Current_Scope)
10047 and then Visible_Part_Type (T)
10048 and then not In_Instance
10049 then
10050 if Is_Abstract_Type (T)
10051 and then Is_Abstract_Subprogram (S)
10052 and then (not Is_Overriding
10053 or else not Is_Abstract_Subprogram (E))
10054 then
10055 Error_Msg_N
10056 ("abstract subprograms must be visible "
10057 & "(RM 3.9.3(10))!", S);
10058
10059 elsif Ekind (S) = E_Function and then not Is_Overriding then
10060 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
10061 Error_Msg_N
10062 ("private function with tagged result must"
10063 & " override visible-part function", S);
10064 Error_Msg_N
10065 ("\move subprogram to the visible part"
10066 & " (RM 3.9.3(10))", S);
10067
10068 -- AI05-0073: extend this test to the case of a function
10069 -- with a controlling access result.
10070
10071 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
10072 and then Is_Tagged_Type (Designated_Type (Etype (S)))
10073 and then
10074 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
10075 and then Ada_Version >= Ada_2012
10076 then
10077 Error_Msg_N
10078 ("private function with controlling access result "
10079 & "must override visible-part function", S);
10080 Error_Msg_N
10081 ("\move subprogram to the visible part"
10082 & " (RM 3.9.3(10))", S);
10083 end if;
10084 end if;
10085 end if;
10086 end Check_Private_Overriding;
10087
10088 -----------------------
10089 -- Visible_Part_Type --
10090 -----------------------
10091
10092 function Visible_Part_Type (T : Entity_Id) return Boolean is
10093 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
10094 N : Node_Id;
10095
10096 begin
10097 -- If the entity is a private type, then it must be declared in a
10098 -- visible part.
10099
10100 if Ekind (T) in Private_Kind then
10101 return True;
10102 end if;
10103
10104 -- Otherwise, we traverse the visible part looking for its
10105 -- corresponding declaration. We cannot use the declaration
10106 -- node directly because in the private part the entity of a
10107 -- private type is the one in the full view, which does not
10108 -- indicate that it is the completion of something visible.
10109
10110 N := First (Visible_Declarations (Specification (P)));
10111 while Present (N) loop
10112 if Nkind (N) = N_Full_Type_Declaration
10113 and then Present (Defining_Identifier (N))
10114 and then T = Defining_Identifier (N)
10115 then
10116 return True;
10117
10118 elsif Nkind_In (N, N_Private_Type_Declaration,
10119 N_Private_Extension_Declaration)
10120 and then Present (Defining_Identifier (N))
10121 and then T = Full_View (Defining_Identifier (N))
10122 then
10123 return True;
10124 end if;
10125
10126 Next (N);
10127 end loop;
10128
10129 return False;
10130 end Visible_Part_Type;
10131
10132 -- Start of processing for Check_For_Primitive_Subprogram
10133
10134 begin
10135 Is_Primitive := False;
10136
10137 if not Comes_From_Source (S) then
10138 null;
10139
10140 -- If subprogram is at library level, it is not primitive operation
10141
10142 elsif Current_Scope = Standard_Standard then
10143 null;
10144
10145 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10146 and then not In_Package_Body (Current_Scope))
10147 or else Is_Overriding
10148 then
10149 -- For function, check return type
10150
10151 if Ekind (S) = E_Function then
10152 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10153 F_Typ := Designated_Type (Etype (S));
10154 else
10155 F_Typ := Etype (S);
10156 end if;
10157
10158 B_Typ := Base_Type (F_Typ);
10159
10160 if Scope (B_Typ) = Current_Scope
10161 and then not Is_Class_Wide_Type (B_Typ)
10162 and then not Is_Generic_Type (B_Typ)
10163 then
10164 Is_Primitive := True;
10165 Set_Has_Primitive_Operations (B_Typ);
10166 Set_Is_Primitive (S);
10167 Check_Private_Overriding (B_Typ);
10168 end if;
10169 end if;
10170
10171 -- For all subprograms, check formals
10172
10173 Formal := First_Formal (S);
10174 while Present (Formal) loop
10175 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10176 F_Typ := Designated_Type (Etype (Formal));
10177 else
10178 F_Typ := Etype (Formal);
10179 end if;
10180
10181 B_Typ := Base_Type (F_Typ);
10182
10183 if Ekind (B_Typ) = E_Access_Subtype then
10184 B_Typ := Base_Type (B_Typ);
10185 end if;
10186
10187 if Scope (B_Typ) = Current_Scope
10188 and then not Is_Class_Wide_Type (B_Typ)
10189 and then not Is_Generic_Type (B_Typ)
10190 then
10191 Is_Primitive := True;
10192 Set_Is_Primitive (S);
10193 Set_Has_Primitive_Operations (B_Typ);
10194 Check_Private_Overriding (B_Typ);
10195 end if;
10196
10197 Next_Formal (Formal);
10198 end loop;
10199
10200 -- Special case: An equality function can be redefined for a type
10201 -- occurring in a declarative part, and won't otherwise be treated as
10202 -- a primitive because it doesn't occur in a package spec and doesn't
10203 -- override an inherited subprogram. It's important that we mark it
10204 -- primitive so it can be returned by Collect_Primitive_Operations
10205 -- and be used in composing the equality operation of later types
10206 -- that have a component of the type.
10207
10208 elsif Chars (S) = Name_Op_Eq
10209 and then Etype (S) = Standard_Boolean
10210 then
10211 B_Typ := Base_Type (Etype (First_Formal (S)));
10212
10213 if Scope (B_Typ) = Current_Scope
10214 and then
10215 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
10216 and then not Is_Limited_Type (B_Typ)
10217 then
10218 Is_Primitive := True;
10219 Set_Is_Primitive (S);
10220 Set_Has_Primitive_Operations (B_Typ);
10221 Check_Private_Overriding (B_Typ);
10222 end if;
10223 end if;
10224 end Check_For_Primitive_Subprogram;
10225
10226 -----------------------------------
10227 -- Check_Synchronized_Overriding --
10228 -----------------------------------
10229
10230 procedure Check_Synchronized_Overriding
10231 (Def_Id : Entity_Id;
10232 Overridden_Subp : out Entity_Id)
10233 is
10234 Ifaces_List : Elist_Id;
10235 In_Scope : Boolean;
10236 Typ : Entity_Id;
10237
10238 function Matches_Prefixed_View_Profile
10239 (Prim_Params : List_Id;
10240 Iface_Params : List_Id) return Boolean;
10241 -- Determine whether a subprogram's parameter profile Prim_Params
10242 -- matches that of a potentially overridden interface subprogram
10243 -- Iface_Params. Also determine if the type of first parameter of
10244 -- Iface_Params is an implemented interface.
10245
10246 -----------------------------------
10247 -- Matches_Prefixed_View_Profile --
10248 -----------------------------------
10249
10250 function Matches_Prefixed_View_Profile
10251 (Prim_Params : List_Id;
10252 Iface_Params : List_Id) return Boolean
10253 is
10254 Iface_Id : Entity_Id;
10255 Iface_Param : Node_Id;
10256 Iface_Typ : Entity_Id;
10257 Prim_Id : Entity_Id;
10258 Prim_Param : Node_Id;
10259 Prim_Typ : Entity_Id;
10260
10261 function Is_Implemented
10262 (Ifaces_List : Elist_Id;
10263 Iface : Entity_Id) return Boolean;
10264 -- Determine if Iface is implemented by the current task or
10265 -- protected type.
10266
10267 --------------------
10268 -- Is_Implemented --
10269 --------------------
10270
10271 function Is_Implemented
10272 (Ifaces_List : Elist_Id;
10273 Iface : Entity_Id) return Boolean
10274 is
10275 Iface_Elmt : Elmt_Id;
10276
10277 begin
10278 Iface_Elmt := First_Elmt (Ifaces_List);
10279 while Present (Iface_Elmt) loop
10280 if Node (Iface_Elmt) = Iface then
10281 return True;
10282 end if;
10283
10284 Next_Elmt (Iface_Elmt);
10285 end loop;
10286
10287 return False;
10288 end Is_Implemented;
10289
10290 -- Start of processing for Matches_Prefixed_View_Profile
10291
10292 begin
10293 Iface_Param := First (Iface_Params);
10294 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
10295
10296 if Is_Access_Type (Iface_Typ) then
10297 Iface_Typ := Designated_Type (Iface_Typ);
10298 end if;
10299
10300 Prim_Param := First (Prim_Params);
10301
10302 -- The first parameter of the potentially overridden subprogram
10303 -- must be an interface implemented by Prim.
10304
10305 if not Is_Interface (Iface_Typ)
10306 or else not Is_Implemented (Ifaces_List, Iface_Typ)
10307 then
10308 return False;
10309 end if;
10310
10311 -- The checks on the object parameters are done, move onto the
10312 -- rest of the parameters.
10313
10314 if not In_Scope then
10315 Prim_Param := Next (Prim_Param);
10316 end if;
10317
10318 Iface_Param := Next (Iface_Param);
10319 while Present (Iface_Param) and then Present (Prim_Param) loop
10320 Iface_Id := Defining_Identifier (Iface_Param);
10321 Iface_Typ := Find_Parameter_Type (Iface_Param);
10322
10323 Prim_Id := Defining_Identifier (Prim_Param);
10324 Prim_Typ := Find_Parameter_Type (Prim_Param);
10325
10326 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
10327 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
10328 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
10329 then
10330 Iface_Typ := Designated_Type (Iface_Typ);
10331 Prim_Typ := Designated_Type (Prim_Typ);
10332 end if;
10333
10334 -- Case of multiple interface types inside a parameter profile
10335
10336 -- (Obj_Param : in out Iface; ...; Param : Iface)
10337
10338 -- If the interface type is implemented, then the matching type
10339 -- in the primitive should be the implementing record type.
10340
10341 if Ekind (Iface_Typ) = E_Record_Type
10342 and then Is_Interface (Iface_Typ)
10343 and then Is_Implemented (Ifaces_List, Iface_Typ)
10344 then
10345 if Prim_Typ /= Typ then
10346 return False;
10347 end if;
10348
10349 -- The two parameters must be both mode and subtype conformant
10350
10351 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
10352 or else not
10353 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
10354 then
10355 return False;
10356 end if;
10357
10358 Next (Iface_Param);
10359 Next (Prim_Param);
10360 end loop;
10361
10362 -- One of the two lists contains more parameters than the other
10363
10364 if Present (Iface_Param) or else Present (Prim_Param) then
10365 return False;
10366 end if;
10367
10368 return True;
10369 end Matches_Prefixed_View_Profile;
10370
10371 -- Start of processing for Check_Synchronized_Overriding
10372
10373 begin
10374 Overridden_Subp := Empty;
10375
10376 -- Def_Id must be an entry or a subprogram. We should skip predefined
10377 -- primitives internally generated by the frontend; however at this
10378 -- stage predefined primitives are still not fully decorated. As a
10379 -- minor optimization we skip here internally generated subprograms.
10380
10381 if (Ekind (Def_Id) /= E_Entry
10382 and then Ekind (Def_Id) /= E_Function
10383 and then Ekind (Def_Id) /= E_Procedure)
10384 or else not Comes_From_Source (Def_Id)
10385 then
10386 return;
10387 end if;
10388
10389 -- Search for the concurrent declaration since it contains the list
10390 -- of all implemented interfaces. In this case, the subprogram is
10391 -- declared within the scope of a protected or a task type.
10392
10393 if Present (Scope (Def_Id))
10394 and then Is_Concurrent_Type (Scope (Def_Id))
10395 and then not Is_Generic_Actual_Type (Scope (Def_Id))
10396 then
10397 Typ := Scope (Def_Id);
10398 In_Scope := True;
10399
10400 -- The enclosing scope is not a synchronized type and the subprogram
10401 -- has no formals.
10402
10403 elsif No (First_Formal (Def_Id)) then
10404 return;
10405
10406 -- The subprogram has formals and hence it may be a primitive of a
10407 -- concurrent type.
10408
10409 else
10410 Typ := Etype (First_Formal (Def_Id));
10411
10412 if Is_Access_Type (Typ) then
10413 Typ := Directly_Designated_Type (Typ);
10414 end if;
10415
10416 if Is_Concurrent_Type (Typ)
10417 and then not Is_Generic_Actual_Type (Typ)
10418 then
10419 In_Scope := False;
10420
10421 -- This case occurs when the concurrent type is declared within
10422 -- a generic unit. As a result the corresponding record has been
10423 -- built and used as the type of the first formal, we just have
10424 -- to retrieve the corresponding concurrent type.
10425
10426 elsif Is_Concurrent_Record_Type (Typ)
10427 and then not Is_Class_Wide_Type (Typ)
10428 and then Present (Corresponding_Concurrent_Type (Typ))
10429 then
10430 Typ := Corresponding_Concurrent_Type (Typ);
10431 In_Scope := False;
10432
10433 else
10434 return;
10435 end if;
10436 end if;
10437
10438 -- There is no overriding to check if is an inherited operation in a
10439 -- type derivation on for a generic actual.
10440
10441 Collect_Interfaces (Typ, Ifaces_List);
10442
10443 if Is_Empty_Elmt_List (Ifaces_List) then
10444 return;
10445 end if;
10446
10447 -- Determine whether entry or subprogram Def_Id overrides a primitive
10448 -- operation that belongs to one of the interfaces in Ifaces_List.
10449
10450 declare
10451 Candidate : Entity_Id := Empty;
10452 Hom : Entity_Id := Empty;
10453 Iface_Typ : Entity_Id;
10454 Subp : Entity_Id := Empty;
10455
10456 begin
10457 -- Traverse the homonym chain, looking for a potentially
10458 -- overridden subprogram that belongs to an implemented
10459 -- interface.
10460
10461 Hom := Current_Entity_In_Scope (Def_Id);
10462 while Present (Hom) loop
10463 Subp := Hom;
10464
10465 if Subp = Def_Id
10466 or else not Is_Overloadable (Subp)
10467 or else not Is_Primitive (Subp)
10468 or else not Is_Dispatching_Operation (Subp)
10469 or else not Present (Find_Dispatching_Type (Subp))
10470 or else not Is_Interface (Find_Dispatching_Type (Subp))
10471 then
10472 null;
10473
10474 -- Entries and procedures can override abstract or null
10475 -- interface procedures.
10476
10477 elsif (Ekind (Def_Id) = E_Procedure
10478 or else Ekind (Def_Id) = E_Entry)
10479 and then Ekind (Subp) = E_Procedure
10480 and then Matches_Prefixed_View_Profile
10481 (Parameter_Specifications (Parent (Def_Id)),
10482 Parameter_Specifications (Parent (Subp)))
10483 then
10484 Candidate := Subp;
10485
10486 -- For an overridden subprogram Subp, check whether the mode
10487 -- of its first parameter is correct depending on the kind
10488 -- of synchronized type.
10489
10490 declare
10491 Formal : constant Node_Id := First_Formal (Candidate);
10492
10493 begin
10494 -- In order for an entry or a protected procedure to
10495 -- override, the first parameter of the overridden
10496 -- routine must be of mode "out", "in out" or
10497 -- access-to-variable.
10498
10499 if Ekind_In (Candidate, E_Entry, E_Procedure)
10500 and then Is_Protected_Type (Typ)
10501 and then Ekind (Formal) /= E_In_Out_Parameter
10502 and then Ekind (Formal) /= E_Out_Parameter
10503 and then Nkind (Parameter_Type (Parent (Formal))) /=
10504 N_Access_Definition
10505 then
10506 null;
10507
10508 -- All other cases are OK since a task entry or routine
10509 -- does not have a restriction on the mode of the first
10510 -- parameter of the overridden interface routine.
10511
10512 else
10513 Overridden_Subp := Candidate;
10514 return;
10515 end if;
10516 end;
10517
10518 -- Functions can override abstract interface functions
10519
10520 elsif Ekind (Def_Id) = E_Function
10521 and then Ekind (Subp) = E_Function
10522 and then Matches_Prefixed_View_Profile
10523 (Parameter_Specifications (Parent (Def_Id)),
10524 Parameter_Specifications (Parent (Subp)))
10525 and then Etype (Result_Definition (Parent (Def_Id))) =
10526 Etype (Result_Definition (Parent (Subp)))
10527 then
10528 Overridden_Subp := Subp;
10529 return;
10530 end if;
10531
10532 Hom := Homonym (Hom);
10533 end loop;
10534
10535 -- After examining all candidates for overriding, we are left with
10536 -- the best match which is a mode incompatible interface routine.
10537 -- Do not emit an error if the Expander is active since this error
10538 -- will be detected later on after all concurrent types are
10539 -- expanded and all wrappers are built. This check is meant for
10540 -- spec-only compilations.
10541
10542 if Present (Candidate) and then not Expander_Active then
10543 Iface_Typ :=
10544 Find_Parameter_Type (Parent (First_Formal (Candidate)));
10545
10546 -- Def_Id is primitive of a protected type, declared inside the
10547 -- type, and the candidate is primitive of a limited or
10548 -- synchronized interface.
10549
10550 if In_Scope
10551 and then Is_Protected_Type (Typ)
10552 and then
10553 (Is_Limited_Interface (Iface_Typ)
10554 or else Is_Protected_Interface (Iface_Typ)
10555 or else Is_Synchronized_Interface (Iface_Typ)
10556 or else Is_Task_Interface (Iface_Typ))
10557 then
10558 Error_Msg_PT (Parent (Typ), Candidate);
10559 end if;
10560 end if;
10561
10562 Overridden_Subp := Candidate;
10563 return;
10564 end;
10565 end Check_Synchronized_Overriding;
10566
10567 ----------------------------
10568 -- Is_Private_Declaration --
10569 ----------------------------
10570
10571 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10572 Priv_Decls : List_Id;
10573 Decl : constant Node_Id := Unit_Declaration_Node (E);
10574
10575 begin
10576 if Is_Package_Or_Generic_Package (Current_Scope)
10577 and then In_Private_Part (Current_Scope)
10578 then
10579 Priv_Decls :=
10580 Private_Declarations (Package_Specification (Current_Scope));
10581
10582 return In_Package_Body (Current_Scope)
10583 or else
10584 (Is_List_Member (Decl)
10585 and then List_Containing (Decl) = Priv_Decls)
10586 or else (Nkind (Parent (Decl)) = N_Package_Specification
10587 and then not
10588 Is_Compilation_Unit
10589 (Defining_Entity (Parent (Decl)))
10590 and then List_Containing (Parent (Parent (Decl))) =
10591 Priv_Decls);
10592 else
10593 return False;
10594 end if;
10595 end Is_Private_Declaration;
10596
10597 --------------------------
10598 -- Is_Overriding_Alias --
10599 --------------------------
10600
10601 function Is_Overriding_Alias
10602 (Old_E : Entity_Id;
10603 New_E : Entity_Id) return Boolean
10604 is
10605 AO : constant Entity_Id := Alias (Old_E);
10606 AN : constant Entity_Id := Alias (New_E);
10607
10608 begin
10609 return Scope (AO) /= Scope (AN)
10610 or else No (DTC_Entity (AO))
10611 or else No (DTC_Entity (AN))
10612 or else DT_Position (AO) = DT_Position (AN);
10613 end Is_Overriding_Alias;
10614
10615 -- Start of processing for New_Overloaded_Entity
10616
10617 begin
10618 -- We need to look for an entity that S may override. This must be a
10619 -- homonym in the current scope, so we look for the first homonym of
10620 -- S in the current scope as the starting point for the search.
10621
10622 E := Current_Entity_In_Scope (S);
10623
10624 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10625 -- They are directly added to the list of primitive operations of
10626 -- Derived_Type, unless this is a rederivation in the private part
10627 -- of an operation that was already derived in the visible part of
10628 -- the current package.
10629
10630 if Ada_Version >= Ada_2005
10631 and then Present (Derived_Type)
10632 and then Present (Alias (S))
10633 and then Is_Dispatching_Operation (Alias (S))
10634 and then Present (Find_Dispatching_Type (Alias (S)))
10635 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10636 then
10637 -- For private types, when the full-view is processed we propagate to
10638 -- the full view the non-overridden entities whose attribute "alias"
10639 -- references an interface primitive. These entities were added by
10640 -- Derive_Subprograms to ensure that interface primitives are
10641 -- covered.
10642
10643 -- Inside_Freeze_Actions is non zero when S corresponds with an
10644 -- internal entity that links an interface primitive with its
10645 -- covering primitive through attribute Interface_Alias (see
10646 -- Add_Internal_Interface_Entities).
10647
10648 if Inside_Freezing_Actions = 0
10649 and then Is_Package_Or_Generic_Package (Current_Scope)
10650 and then In_Private_Part (Current_Scope)
10651 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10652 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10653 and then Full_View (Defining_Identifier (Parent (E)))
10654 = Defining_Identifier (Parent (S))
10655 and then Alias (E) = Alias (S)
10656 then
10657 Check_Operation_From_Private_View (S, E);
10658 Set_Is_Dispatching_Operation (S);
10659
10660 -- Common case
10661
10662 else
10663 Enter_Overloaded_Entity (S);
10664 Check_Dispatching_Operation (S, Empty);
10665 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10666 end if;
10667
10668 return;
10669 end if;
10670
10671 -- If there is no homonym then this is definitely not overriding
10672
10673 if No (E) then
10674 Enter_Overloaded_Entity (S);
10675 Check_Dispatching_Operation (S, Empty);
10676 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10677
10678 -- If subprogram has an explicit declaration, check whether it has an
10679 -- overriding indicator.
10680
10681 if Comes_From_Source (S) then
10682 Check_Synchronized_Overriding (S, Overridden_Subp);
10683
10684 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10685 -- it may have overridden some hidden inherited primitive. Update
10686 -- Overridden_Subp to avoid spurious errors when checking the
10687 -- overriding indicator.
10688
10689 if Ada_Version >= Ada_2012
10690 and then No (Overridden_Subp)
10691 and then Is_Dispatching_Operation (S)
10692 and then Present (Overridden_Operation (S))
10693 then
10694 Overridden_Subp := Overridden_Operation (S);
10695 end if;
10696
10697 Check_Overriding_Indicator
10698 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10699 end if;
10700
10701 -- If there is a homonym that is not overloadable, then we have an
10702 -- error, except for the special cases checked explicitly below.
10703
10704 elsif not Is_Overloadable (E) then
10705
10706 -- Check for spurious conflict produced by a subprogram that has the
10707 -- same name as that of the enclosing generic package. The conflict
10708 -- occurs within an instance, between the subprogram and the renaming
10709 -- declaration for the package. After the subprogram, the package
10710 -- renaming declaration becomes hidden.
10711
10712 if Ekind (E) = E_Package
10713 and then Present (Renamed_Object (E))
10714 and then Renamed_Object (E) = Current_Scope
10715 and then Nkind (Parent (Renamed_Object (E))) =
10716 N_Package_Specification
10717 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10718 then
10719 Set_Is_Hidden (E);
10720 Set_Is_Immediately_Visible (E, False);
10721 Enter_Overloaded_Entity (S);
10722 Set_Homonym (S, Homonym (E));
10723 Check_Dispatching_Operation (S, Empty);
10724 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10725
10726 -- If the subprogram is implicit it is hidden by the previous
10727 -- declaration. However if it is dispatching, it must appear in the
10728 -- dispatch table anyway, because it can be dispatched to even if it
10729 -- cannot be called directly.
10730
10731 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10732 Set_Scope (S, Current_Scope);
10733
10734 if Is_Dispatching_Operation (Alias (S)) then
10735 Check_Dispatching_Operation (S, Empty);
10736 end if;
10737
10738 return;
10739
10740 else
10741 Error_Msg_Sloc := Sloc (E);
10742
10743 -- Generate message, with useful additional warning if in generic
10744
10745 if Is_Generic_Unit (E) then
10746 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10747 Error_Msg_N ("\& conflicts with declaration#", S);
10748 else
10749 Error_Msg_N ("& conflicts with declaration#", S);
10750 end if;
10751
10752 return;
10753 end if;
10754
10755 -- E exists and is overloadable
10756
10757 else
10758 Check_Synchronized_Overriding (S, Overridden_Subp);
10759
10760 -- Loop through E and its homonyms to determine if any of them is
10761 -- the candidate for overriding by S.
10762
10763 while Present (E) loop
10764
10765 -- Definitely not interesting if not in the current scope
10766
10767 if Scope (E) /= Current_Scope then
10768 null;
10769
10770 -- A function can overload the name of an abstract state. The
10771 -- state can be viewed as a function with a profile that cannot
10772 -- be matched by anything.
10773
10774 elsif Ekind (S) = E_Function
10775 and then Ekind (E) = E_Abstract_State
10776 then
10777 Enter_Overloaded_Entity (S);
10778 return;
10779
10780 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10781 -- procedures locate the internally generated spec. We enforce
10782 -- mode conformance since a tagged type may inherit from
10783 -- interfaces several null primitives which differ only in
10784 -- the mode of the formals.
10785
10786 elsif not Comes_From_Source (S)
10787 and then Is_Null_Procedure (S)
10788 and then not Mode_Conformant (E, S)
10789 then
10790 null;
10791
10792 -- Check if we have type conformance
10793
10794 elsif Type_Conformant (E, S) then
10795
10796 -- If the old and new entities have the same profile and one
10797 -- is not the body of the other, then this is an error, unless
10798 -- one of them is implicitly declared.
10799
10800 -- There are some cases when both can be implicit, for example
10801 -- when both a literal and a function that overrides it are
10802 -- inherited in a derivation, or when an inherited operation
10803 -- of a tagged full type overrides the inherited operation of
10804 -- a private extension. Ada 83 had a special rule for the
10805 -- literal case. In Ada 95, the later implicit operation hides
10806 -- the former, and the literal is always the former. In the
10807 -- odd case where both are derived operations declared at the
10808 -- same point, both operations should be declared, and in that
10809 -- case we bypass the following test and proceed to the next
10810 -- part. This can only occur for certain obscure cases in
10811 -- instances, when an operation on a type derived from a formal
10812 -- private type does not override a homograph inherited from
10813 -- the actual. In subsequent derivations of such a type, the
10814 -- DT positions of these operations remain distinct, if they
10815 -- have been set.
10816
10817 if Present (Alias (S))
10818 and then (No (Alias (E))
10819 or else Comes_From_Source (E)
10820 or else Is_Abstract_Subprogram (S)
10821 or else
10822 (Is_Dispatching_Operation (E)
10823 and then Is_Overriding_Alias (E, S)))
10824 and then Ekind (E) /= E_Enumeration_Literal
10825 then
10826 -- When an derived operation is overloaded it may be due to
10827 -- the fact that the full view of a private extension
10828 -- re-inherits. It has to be dealt with.
10829
10830 if Is_Package_Or_Generic_Package (Current_Scope)
10831 and then In_Private_Part (Current_Scope)
10832 then
10833 Check_Operation_From_Private_View (S, E);
10834 end if;
10835
10836 -- In any case the implicit operation remains hidden by the
10837 -- existing declaration, which is overriding. Indicate that
10838 -- E overrides the operation from which S is inherited.
10839
10840 if Present (Alias (S)) then
10841 Set_Overridden_Operation (E, Alias (S));
10842 else
10843 Set_Overridden_Operation (E, S);
10844 end if;
10845
10846 if Comes_From_Source (E) then
10847 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10848 end if;
10849
10850 return;
10851
10852 -- Within an instance, the renaming declarations for actual
10853 -- subprograms may become ambiguous, but they do not hide each
10854 -- other.
10855
10856 elsif Ekind (E) /= E_Entry
10857 and then not Comes_From_Source (E)
10858 and then not Is_Generic_Instance (E)
10859 and then (Present (Alias (E))
10860 or else Is_Intrinsic_Subprogram (E))
10861 and then (not In_Instance
10862 or else No (Parent (E))
10863 or else Nkind (Unit_Declaration_Node (E)) /=
10864 N_Subprogram_Renaming_Declaration)
10865 then
10866 -- A subprogram child unit is not allowed to override an
10867 -- inherited subprogram (10.1.1(20)).
10868
10869 if Is_Child_Unit (S) then
10870 Error_Msg_N
10871 ("child unit overrides inherited subprogram in parent",
10872 S);
10873 return;
10874 end if;
10875
10876 if Is_Non_Overriding_Operation (E, S) then
10877 Enter_Overloaded_Entity (S);
10878
10879 if No (Derived_Type)
10880 or else Is_Tagged_Type (Derived_Type)
10881 then
10882 Check_Dispatching_Operation (S, Empty);
10883 end if;
10884
10885 return;
10886 end if;
10887
10888 -- E is a derived operation or an internal operator which
10889 -- is being overridden. Remove E from further visibility.
10890 -- Furthermore, if E is a dispatching operation, it must be
10891 -- replaced in the list of primitive operations of its type
10892 -- (see Override_Dispatching_Operation).
10893
10894 Overridden_Subp := E;
10895
10896 declare
10897 Prev : Entity_Id;
10898
10899 begin
10900 Prev := First_Entity (Current_Scope);
10901 while Present (Prev) and then Next_Entity (Prev) /= E loop
10902 Next_Entity (Prev);
10903 end loop;
10904
10905 -- It is possible for E to be in the current scope and
10906 -- yet not in the entity chain. This can only occur in a
10907 -- generic context where E is an implicit concatenation
10908 -- in the formal part, because in a generic body the
10909 -- entity chain starts with the formals.
10910
10911 pragma Assert
10912 (Present (Prev) or else Chars (E) = Name_Op_Concat);
10913
10914 -- E must be removed both from the entity_list of the
10915 -- current scope, and from the visibility chain
10916
10917 if Debug_Flag_E then
10918 Write_Str ("Override implicit operation ");
10919 Write_Int (Int (E));
10920 Write_Eol;
10921 end if;
10922
10923 -- If E is a predefined concatenation, it stands for four
10924 -- different operations. As a result, a single explicit
10925 -- declaration does not hide it. In a possible ambiguous
10926 -- situation, Disambiguate chooses the user-defined op,
10927 -- so it is correct to retain the previous internal one.
10928
10929 if Chars (E) /= Name_Op_Concat
10930 or else Ekind (E) /= E_Operator
10931 then
10932 -- For nondispatching derived operations that are
10933 -- overridden by a subprogram declared in the private
10934 -- part of a package, we retain the derived subprogram
10935 -- but mark it as not immediately visible. If the
10936 -- derived operation was declared in the visible part
10937 -- then this ensures that it will still be visible
10938 -- outside the package with the proper signature
10939 -- (calls from outside must also be directed to this
10940 -- version rather than the overriding one, unlike the
10941 -- dispatching case). Calls from inside the package
10942 -- will still resolve to the overriding subprogram
10943 -- since the derived one is marked as not visible
10944 -- within the package.
10945
10946 -- If the private operation is dispatching, we achieve
10947 -- the overriding by keeping the implicit operation
10948 -- but setting its alias to be the overriding one. In
10949 -- this fashion the proper body is executed in all
10950 -- cases, but the original signature is used outside
10951 -- of the package.
10952
10953 -- If the overriding is not in the private part, we
10954 -- remove the implicit operation altogether.
10955
10956 if Is_Private_Declaration (S) then
10957 if not Is_Dispatching_Operation (E) then
10958 Set_Is_Immediately_Visible (E, False);
10959 else
10960 -- Work done in Override_Dispatching_Operation,
10961 -- so nothing else needs to be done here.
10962
10963 null;
10964 end if;
10965
10966 else
10967 -- Find predecessor of E in Homonym chain
10968
10969 if E = Current_Entity (E) then
10970 Prev_Vis := Empty;
10971 else
10972 Prev_Vis := Current_Entity (E);
10973 while Homonym (Prev_Vis) /= E loop
10974 Prev_Vis := Homonym (Prev_Vis);
10975 end loop;
10976 end if;
10977
10978 if Prev_Vis /= Empty then
10979
10980 -- Skip E in the visibility chain
10981
10982 Set_Homonym (Prev_Vis, Homonym (E));
10983
10984 else
10985 Set_Name_Entity_Id (Chars (E), Homonym (E));
10986 end if;
10987
10988 Set_Next_Entity (Prev, Next_Entity (E));
10989
10990 if No (Next_Entity (Prev)) then
10991 Set_Last_Entity (Current_Scope, Prev);
10992 end if;
10993 end if;
10994 end if;
10995
10996 Enter_Overloaded_Entity (S);
10997
10998 -- For entities generated by Derive_Subprograms the
10999 -- overridden operation is the inherited primitive
11000 -- (which is available through the attribute alias).
11001
11002 if not (Comes_From_Source (E))
11003 and then Is_Dispatching_Operation (E)
11004 and then Find_Dispatching_Type (E) =
11005 Find_Dispatching_Type (S)
11006 and then Present (Alias (E))
11007 and then Comes_From_Source (Alias (E))
11008 then
11009 Set_Overridden_Operation (S, Alias (E));
11010
11011 -- Normal case of setting entity as overridden
11012
11013 -- Note: Static_Initialization and Overridden_Operation
11014 -- attributes use the same field in subprogram entities.
11015 -- Static_Initialization is only defined for internal
11016 -- initialization procedures, where Overridden_Operation
11017 -- is irrelevant. Therefore the setting of this attribute
11018 -- must check whether the target is an init_proc.
11019
11020 elsif not Is_Init_Proc (S) then
11021 Set_Overridden_Operation (S, E);
11022 end if;
11023
11024 Check_Overriding_Indicator (S, E, Is_Primitive => True);
11025
11026 -- If S is a user-defined subprogram or a null procedure
11027 -- expanded to override an inherited null procedure, or a
11028 -- predefined dispatching primitive then indicate that E
11029 -- overrides the operation from which S is inherited.
11030
11031 if Comes_From_Source (S)
11032 or else
11033 (Present (Parent (S))
11034 and then
11035 Nkind (Parent (S)) = N_Procedure_Specification
11036 and then
11037 Null_Present (Parent (S)))
11038 or else
11039 (Present (Alias (E))
11040 and then
11041 Is_Predefined_Dispatching_Operation (Alias (E)))
11042 then
11043 if Present (Alias (E)) then
11044 Set_Overridden_Operation (S, Alias (E));
11045 end if;
11046 end if;
11047
11048 if Is_Dispatching_Operation (E) then
11049
11050 -- An overriding dispatching subprogram inherits the
11051 -- convention of the overridden subprogram (AI-117).
11052
11053 Set_Convention (S, Convention (E));
11054 Check_Dispatching_Operation (S, E);
11055
11056 else
11057 Check_Dispatching_Operation (S, Empty);
11058 end if;
11059
11060 Check_For_Primitive_Subprogram
11061 (Is_Primitive_Subp, Is_Overriding => True);
11062 goto Check_Inequality;
11063 end;
11064
11065 -- Apparent redeclarations in instances can occur when two
11066 -- formal types get the same actual type. The subprograms in
11067 -- in the instance are legal, even if not callable from the
11068 -- outside. Calls from within are disambiguated elsewhere.
11069 -- For dispatching operations in the visible part, the usual
11070 -- rules apply, and operations with the same profile are not
11071 -- legal (B830001).
11072
11073 elsif (In_Instance_Visible_Part
11074 and then not Is_Dispatching_Operation (E))
11075 or else In_Instance_Not_Visible
11076 then
11077 null;
11078
11079 -- Here we have a real error (identical profile)
11080
11081 else
11082 Error_Msg_Sloc := Sloc (E);
11083
11084 -- Avoid cascaded errors if the entity appears in
11085 -- subsequent calls.
11086
11087 Set_Scope (S, Current_Scope);
11088
11089 -- Generate error, with extra useful warning for the case
11090 -- of a generic instance with no completion.
11091
11092 if Is_Generic_Instance (S)
11093 and then not Has_Completion (E)
11094 then
11095 Error_Msg_N
11096 ("instantiation cannot provide body for&", S);
11097 Error_Msg_N ("\& conflicts with declaration#", S);
11098 else
11099 Error_Msg_N ("& conflicts with declaration#", S);
11100 end if;
11101
11102 return;
11103 end if;
11104
11105 else
11106 -- If one subprogram has an access parameter and the other
11107 -- a parameter of an access type, calls to either might be
11108 -- ambiguous. Verify that parameters match except for the
11109 -- access parameter.
11110
11111 if May_Hide_Profile then
11112 declare
11113 F1 : Entity_Id;
11114 F2 : Entity_Id;
11115
11116 begin
11117 F1 := First_Formal (S);
11118 F2 := First_Formal (E);
11119 while Present (F1) and then Present (F2) loop
11120 if Is_Access_Type (Etype (F1)) then
11121 if not Is_Access_Type (Etype (F2))
11122 or else not Conforming_Types
11123 (Designated_Type (Etype (F1)),
11124 Designated_Type (Etype (F2)),
11125 Type_Conformant)
11126 then
11127 May_Hide_Profile := False;
11128 end if;
11129
11130 elsif
11131 not Conforming_Types
11132 (Etype (F1), Etype (F2), Type_Conformant)
11133 then
11134 May_Hide_Profile := False;
11135 end if;
11136
11137 Next_Formal (F1);
11138 Next_Formal (F2);
11139 end loop;
11140
11141 if May_Hide_Profile
11142 and then No (F1)
11143 and then No (F2)
11144 then
11145 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11146 end if;
11147 end;
11148 end if;
11149 end if;
11150
11151 E := Homonym (E);
11152 end loop;
11153
11154 -- On exit, we know that S is a new entity
11155
11156 Enter_Overloaded_Entity (S);
11157 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11158 Check_Overriding_Indicator
11159 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11160
11161 -- Overloading is not allowed in SPARK, except for operators
11162
11163 if Nkind (S) /= N_Defining_Operator_Symbol then
11164 Error_Msg_Sloc := Sloc (Homonym (S));
11165 Check_SPARK_Restriction
11166 ("overloading not allowed with entity#", S);
11167 end if;
11168
11169 -- If S is a derived operation for an untagged type then by
11170 -- definition it's not a dispatching operation (even if the parent
11171 -- operation was dispatching), so Check_Dispatching_Operation is not
11172 -- called in that case.
11173
11174 if No (Derived_Type)
11175 or else Is_Tagged_Type (Derived_Type)
11176 then
11177 Check_Dispatching_Operation (S, Empty);
11178 end if;
11179 end if;
11180
11181 -- If this is a user-defined equality operator that is not a derived
11182 -- subprogram, create the corresponding inequality. If the operation is
11183 -- dispatching, the expansion is done elsewhere, and we do not create
11184 -- an explicit inequality operation.
11185
11186 <<Check_Inequality>>
11187 if Chars (S) = Name_Op_Eq
11188 and then Etype (S) = Standard_Boolean
11189 and then Present (Parent (S))
11190 and then not Is_Dispatching_Operation (S)
11191 then
11192 Make_Inequality_Operator (S);
11193 Check_Untagged_Equality (S);
11194 end if;
11195 end New_Overloaded_Entity;
11196
11197 ---------------------
11198 -- Process_Formals --
11199 ---------------------
11200
11201 procedure Process_Formals
11202 (T : List_Id;
11203 Related_Nod : Node_Id)
11204 is
11205 Param_Spec : Node_Id;
11206 Formal : Entity_Id;
11207 Formal_Type : Entity_Id;
11208 Default : Node_Id;
11209 Ptype : Entity_Id;
11210
11211 Num_Out_Params : Nat := 0;
11212 First_Out_Param : Entity_Id := Empty;
11213 -- Used for setting Is_Only_Out_Parameter
11214
11215 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
11216 -- Determine whether an access type designates a type coming from a
11217 -- limited view.
11218
11219 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
11220 -- Check whether the default has a class-wide type. After analysis the
11221 -- default has the type of the formal, so we must also check explicitly
11222 -- for an access attribute.
11223
11224 ----------------------------------
11225 -- Designates_From_Limited_With --
11226 ----------------------------------
11227
11228 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
11229 Desig : Entity_Id := Typ;
11230
11231 begin
11232 if Is_Access_Type (Desig) then
11233 Desig := Directly_Designated_Type (Desig);
11234 end if;
11235
11236 if Is_Class_Wide_Type (Desig) then
11237 Desig := Root_Type (Desig);
11238 end if;
11239
11240 return
11241 Ekind (Desig) = E_Incomplete_Type
11242 and then From_Limited_With (Desig);
11243 end Designates_From_Limited_With;
11244
11245 ---------------------------
11246 -- Is_Class_Wide_Default --
11247 ---------------------------
11248
11249 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
11250 begin
11251 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
11252 or else (Nkind (D) = N_Attribute_Reference
11253 and then Attribute_Name (D) = Name_Access
11254 and then Is_Class_Wide_Type (Etype (Prefix (D))));
11255 end Is_Class_Wide_Default;
11256
11257 -- Start of processing for Process_Formals
11258
11259 begin
11260 -- In order to prevent premature use of the formals in the same formal
11261 -- part, the Ekind is left undefined until all default expressions are
11262 -- analyzed. The Ekind is established in a separate loop at the end.
11263
11264 Param_Spec := First (T);
11265 while Present (Param_Spec) loop
11266 Formal := Defining_Identifier (Param_Spec);
11267 Set_Never_Set_In_Source (Formal, True);
11268 Enter_Name (Formal);
11269
11270 -- Case of ordinary parameters
11271
11272 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
11273 Find_Type (Parameter_Type (Param_Spec));
11274 Ptype := Parameter_Type (Param_Spec);
11275
11276 if Ptype = Error then
11277 goto Continue;
11278 end if;
11279
11280 Formal_Type := Entity (Ptype);
11281
11282 if Is_Incomplete_Type (Formal_Type)
11283 or else
11284 (Is_Class_Wide_Type (Formal_Type)
11285 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
11286 then
11287 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
11288 -- primitive operations, as long as their completion is
11289 -- in the same declarative part. If in the private part
11290 -- this means that the type cannot be a Taft-amendment type.
11291 -- Check is done on package exit. For access to subprograms,
11292 -- the use is legal for Taft-amendment types.
11293
11294 -- Ada 2012: tagged incomplete types are allowed as generic
11295 -- formal types. They do not introduce dependencies and the
11296 -- corresponding generic subprogram does not have a delayed
11297 -- freeze, because it does not need a freeze node. However,
11298 -- it is still the case that untagged incomplete types cannot
11299 -- be Taft-amendment types and must be completed in private
11300 -- part, so the subprogram must appear in the list of private
11301 -- dependents of the type.
11302
11303 if Is_Tagged_Type (Formal_Type)
11304 or else Ada_Version >= Ada_2012
11305 then
11306 if Ekind (Scope (Current_Scope)) = E_Package
11307 and then not From_Limited_With (Formal_Type)
11308 and then not Is_Generic_Type (Formal_Type)
11309 and then not Is_Class_Wide_Type (Formal_Type)
11310 then
11311 if not Nkind_In
11312 (Parent (T), N_Access_Function_Definition,
11313 N_Access_Procedure_Definition)
11314 then
11315 Append_Elmt
11316 (Current_Scope,
11317 Private_Dependents (Base_Type (Formal_Type)));
11318
11319 -- Freezing is delayed to ensure that Register_Prim
11320 -- will get called for this operation, which is needed
11321 -- in cases where static dispatch tables aren't built.
11322 -- (Note that the same is done for controlling access
11323 -- parameter cases in function Access_Definition.)
11324
11325 Set_Has_Delayed_Freeze (Current_Scope);
11326 end if;
11327 end if;
11328
11329 -- Special handling of Value_Type for CIL case
11330
11331 elsif Is_Value_Type (Formal_Type) then
11332 null;
11333
11334 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
11335 N_Access_Procedure_Definition)
11336 then
11337 -- AI05-0151: Tagged incomplete types are allowed in all
11338 -- formal parts. Untagged incomplete types are not allowed
11339 -- in bodies.
11340
11341 if Ada_Version >= Ada_2012 then
11342 if Is_Tagged_Type (Formal_Type) then
11343 null;
11344
11345 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
11346 N_Entry_Body,
11347 N_Subprogram_Body)
11348 then
11349 Error_Msg_NE
11350 ("invalid use of untagged incomplete type&",
11351 Ptype, Formal_Type);
11352 end if;
11353
11354 else
11355 Error_Msg_NE
11356 ("invalid use of incomplete type&",
11357 Param_Spec, Formal_Type);
11358
11359 -- Further checks on the legality of incomplete types
11360 -- in formal parts are delayed until the freeze point
11361 -- of the enclosing subprogram or access to subprogram.
11362 end if;
11363 end if;
11364
11365 elsif Ekind (Formal_Type) = E_Void then
11366 Error_Msg_NE
11367 ("premature use of&",
11368 Parameter_Type (Param_Spec), Formal_Type);
11369 end if;
11370
11371 -- Ada 2012 (AI-142): Handle aliased parameters
11372
11373 if Ada_Version >= Ada_2012
11374 and then Aliased_Present (Param_Spec)
11375 then
11376 Set_Is_Aliased (Formal);
11377 end if;
11378
11379 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11380 -- declaration corresponding to the null-excluding type of the
11381 -- formal in the enclosing scope. Finally, replace the parameter
11382 -- type of the formal with the internal subtype.
11383
11384 if Ada_Version >= Ada_2005
11385 and then Null_Exclusion_Present (Param_Spec)
11386 then
11387 if not Is_Access_Type (Formal_Type) then
11388 Error_Msg_N
11389 ("`NOT NULL` allowed only for an access type", Param_Spec);
11390
11391 else
11392 if Can_Never_Be_Null (Formal_Type)
11393 and then Comes_From_Source (Related_Nod)
11394 then
11395 Error_Msg_NE
11396 ("`NOT NULL` not allowed (& already excludes null)",
11397 Param_Spec, Formal_Type);
11398 end if;
11399
11400 Formal_Type :=
11401 Create_Null_Excluding_Itype
11402 (T => Formal_Type,
11403 Related_Nod => Related_Nod,
11404 Scope_Id => Scope (Current_Scope));
11405
11406 -- If the designated type of the itype is an itype that is
11407 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11408 -- on the access subtype, to prevent order-of-elaboration
11409 -- issues in the backend.
11410
11411 -- Example:
11412 -- type T is access procedure;
11413 -- procedure Op (O : not null T);
11414
11415 if Is_Itype (Directly_Designated_Type (Formal_Type))
11416 and then
11417 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11418 then
11419 Set_Has_Delayed_Freeze (Formal_Type);
11420 end if;
11421 end if;
11422 end if;
11423
11424 -- An access formal type
11425
11426 else
11427 Formal_Type :=
11428 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11429
11430 -- No need to continue if we already notified errors
11431
11432 if not Present (Formal_Type) then
11433 return;
11434 end if;
11435
11436 -- Ada 2005 (AI-254)
11437
11438 declare
11439 AD : constant Node_Id :=
11440 Access_To_Subprogram_Definition
11441 (Parameter_Type (Param_Spec));
11442 begin
11443 if Present (AD) and then Protected_Present (AD) then
11444 Formal_Type :=
11445 Replace_Anonymous_Access_To_Protected_Subprogram
11446 (Param_Spec);
11447 end if;
11448 end;
11449 end if;
11450
11451 Set_Etype (Formal, Formal_Type);
11452
11453 -- Deal with default expression if present
11454
11455 Default := Expression (Param_Spec);
11456
11457 if Present (Default) then
11458 Check_SPARK_Restriction
11459 ("default expression is not allowed", Default);
11460
11461 if Out_Present (Param_Spec) then
11462 Error_Msg_N
11463 ("default initialization only allowed for IN parameters",
11464 Param_Spec);
11465 end if;
11466
11467 -- Do the special preanalysis of the expression (see section on
11468 -- "Handling of Default Expressions" in the spec of package Sem).
11469
11470 Preanalyze_Spec_Expression (Default, Formal_Type);
11471
11472 -- An access to constant cannot be the default for
11473 -- an access parameter that is an access to variable.
11474
11475 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11476 and then not Is_Access_Constant (Formal_Type)
11477 and then Is_Access_Type (Etype (Default))
11478 and then Is_Access_Constant (Etype (Default))
11479 then
11480 Error_Msg_N
11481 ("formal that is access to variable cannot be initialized " &
11482 "with an access-to-constant expression", Default);
11483 end if;
11484
11485 -- Check that the designated type of an access parameter's default
11486 -- is not a class-wide type unless the parameter's designated type
11487 -- is also class-wide.
11488
11489 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11490 and then not Designates_From_Limited_With (Formal_Type)
11491 and then Is_Class_Wide_Default (Default)
11492 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11493 then
11494 Error_Msg_N
11495 ("access to class-wide expression not allowed here", Default);
11496 end if;
11497
11498 -- Check incorrect use of dynamically tagged expressions
11499
11500 if Is_Tagged_Type (Formal_Type) then
11501 Check_Dynamically_Tagged_Expression
11502 (Expr => Default,
11503 Typ => Formal_Type,
11504 Related_Nod => Default);
11505 end if;
11506 end if;
11507
11508 -- Ada 2005 (AI-231): Static checks
11509
11510 if Ada_Version >= Ada_2005
11511 and then Is_Access_Type (Etype (Formal))
11512 and then Can_Never_Be_Null (Etype (Formal))
11513 then
11514 Null_Exclusion_Static_Checks (Param_Spec);
11515 end if;
11516
11517 -- The following checks are relevant when SPARK_Mode is on as these
11518 -- are not standard Ada legality rules.
11519
11520 if SPARK_Mode = On then
11521 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
11522
11523 -- A function cannot have a parameter of mode IN OUT or OUT
11524 -- (SPARK RM 6.1).
11525
11526 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
11527 Error_Msg_N
11528 ("function cannot have parameter of mode `OUT` or "
11529 & "`IN OUT`", Formal);
11530
11531 -- A function cannot have a volatile formal parameter
11532 -- (SPARK RM 7.1.3(10)).
11533
11534 elsif Is_SPARK_Volatile (Formal) then
11535 Error_Msg_N
11536 ("function cannot have a volatile formal parameter",
11537 Formal);
11538 end if;
11539
11540 -- A procedure cannot have a formal parameter of mode IN because
11541 -- it behaves as a constant (SPARK RM 7.1.3(6)).
11542
11543 elsif Ekind (Scope (Formal)) = E_Procedure
11544 and then Ekind (Formal) = E_In_Parameter
11545 and then Is_SPARK_Volatile (Formal)
11546 then
11547 Error_Msg_N
11548 ("formal parameter of mode `IN` cannot be volatile", Formal);
11549 end if;
11550 end if;
11551
11552 <<Continue>>
11553 Next (Param_Spec);
11554 end loop;
11555
11556 -- If this is the formal part of a function specification, analyze the
11557 -- subtype mark in the context where the formals are visible but not
11558 -- yet usable, and may hide outer homographs.
11559
11560 if Nkind (Related_Nod) = N_Function_Specification then
11561 Analyze_Return_Type (Related_Nod);
11562 end if;
11563
11564 -- Now set the kind (mode) of each formal
11565
11566 Param_Spec := First (T);
11567 while Present (Param_Spec) loop
11568 Formal := Defining_Identifier (Param_Spec);
11569 Set_Formal_Mode (Formal);
11570
11571 if Ekind (Formal) = E_In_Parameter then
11572 Set_Default_Value (Formal, Expression (Param_Spec));
11573
11574 if Present (Expression (Param_Spec)) then
11575 Default := Expression (Param_Spec);
11576
11577 if Is_Scalar_Type (Etype (Default)) then
11578 if Nkind (Parameter_Type (Param_Spec)) /=
11579 N_Access_Definition
11580 then
11581 Formal_Type := Entity (Parameter_Type (Param_Spec));
11582 else
11583 Formal_Type :=
11584 Access_Definition
11585 (Related_Nod, Parameter_Type (Param_Spec));
11586 end if;
11587
11588 Apply_Scalar_Range_Check (Default, Formal_Type);
11589 end if;
11590 end if;
11591
11592 elsif Ekind (Formal) = E_Out_Parameter then
11593 Num_Out_Params := Num_Out_Params + 1;
11594
11595 if Num_Out_Params = 1 then
11596 First_Out_Param := Formal;
11597 end if;
11598
11599 elsif Ekind (Formal) = E_In_Out_Parameter then
11600 Num_Out_Params := Num_Out_Params + 1;
11601 end if;
11602
11603 -- Skip remaining processing if formal type was in error
11604
11605 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11606 goto Next_Parameter;
11607 end if;
11608
11609 -- Force call by reference if aliased
11610
11611 if Is_Aliased (Formal) then
11612 Set_Mechanism (Formal, By_Reference);
11613
11614 -- Warn if user asked this to be passed by copy
11615
11616 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11617 Error_Msg_N
11618 ("cannot pass aliased parameter & by copy??", Formal);
11619 end if;
11620
11621 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11622
11623 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11624 Set_Mechanism (Formal, By_Copy);
11625
11626 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
11627 Set_Mechanism (Formal, By_Reference);
11628 end if;
11629
11630 <<Next_Parameter>>
11631 Next (Param_Spec);
11632 end loop;
11633
11634 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11635 Set_Is_Only_Out_Parameter (First_Out_Param);
11636 end if;
11637 end Process_Formals;
11638
11639 ----------------------------
11640 -- Reference_Body_Formals --
11641 ----------------------------
11642
11643 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
11644 Fs : Entity_Id;
11645 Fb : Entity_Id;
11646
11647 begin
11648 if Error_Posted (Spec) then
11649 return;
11650 end if;
11651
11652 -- Iterate over both lists. They may be of different lengths if the two
11653 -- specs are not conformant.
11654
11655 Fs := First_Formal (Spec);
11656 Fb := First_Formal (Bod);
11657 while Present (Fs) and then Present (Fb) loop
11658 Generate_Reference (Fs, Fb, 'b');
11659
11660 if Style_Check then
11661 Style.Check_Identifier (Fb, Fs);
11662 end if;
11663
11664 Set_Spec_Entity (Fb, Fs);
11665 Set_Referenced (Fs, False);
11666 Next_Formal (Fs);
11667 Next_Formal (Fb);
11668 end loop;
11669 end Reference_Body_Formals;
11670
11671 -------------------------
11672 -- Set_Actual_Subtypes --
11673 -------------------------
11674
11675 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
11676 Decl : Node_Id;
11677 Formal : Entity_Id;
11678 T : Entity_Id;
11679 First_Stmt : Node_Id := Empty;
11680 AS_Needed : Boolean;
11681
11682 begin
11683 -- If this is an empty initialization procedure, no need to create
11684 -- actual subtypes (small optimization).
11685
11686 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
11687 return;
11688 end if;
11689
11690 Formal := First_Formal (Subp);
11691 while Present (Formal) loop
11692 T := Etype (Formal);
11693
11694 -- We never need an actual subtype for a constrained formal
11695
11696 if Is_Constrained (T) then
11697 AS_Needed := False;
11698
11699 -- If we have unknown discriminants, then we do not need an actual
11700 -- subtype, or more accurately we cannot figure it out. Note that
11701 -- all class-wide types have unknown discriminants.
11702
11703 elsif Has_Unknown_Discriminants (T) then
11704 AS_Needed := False;
11705
11706 -- At this stage we have an unconstrained type that may need an
11707 -- actual subtype. For sure the actual subtype is needed if we have
11708 -- an unconstrained array type.
11709
11710 elsif Is_Array_Type (T) then
11711 AS_Needed := True;
11712
11713 -- The only other case needing an actual subtype is an unconstrained
11714 -- record type which is an IN parameter (we cannot generate actual
11715 -- subtypes for the OUT or IN OUT case, since an assignment can
11716 -- change the discriminant values. However we exclude the case of
11717 -- initialization procedures, since discriminants are handled very
11718 -- specially in this context, see the section entitled "Handling of
11719 -- Discriminants" in Einfo.
11720
11721 -- We also exclude the case of Discrim_SO_Functions (functions used
11722 -- in front end layout mode for size/offset values), since in such
11723 -- functions only discriminants are referenced, and not only are such
11724 -- subtypes not needed, but they cannot always be generated, because
11725 -- of order of elaboration issues.
11726
11727 elsif Is_Record_Type (T)
11728 and then Ekind (Formal) = E_In_Parameter
11729 and then Chars (Formal) /= Name_uInit
11730 and then not Is_Unchecked_Union (T)
11731 and then not Is_Discrim_SO_Function (Subp)
11732 then
11733 AS_Needed := True;
11734
11735 -- All other cases do not need an actual subtype
11736
11737 else
11738 AS_Needed := False;
11739 end if;
11740
11741 -- Generate actual subtypes for unconstrained arrays and
11742 -- unconstrained discriminated records.
11743
11744 if AS_Needed then
11745 if Nkind (N) = N_Accept_Statement then
11746
11747 -- If expansion is active, the formal is replaced by a local
11748 -- variable that renames the corresponding entry of the
11749 -- parameter block, and it is this local variable that may
11750 -- require an actual subtype.
11751
11752 if Expander_Active then
11753 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
11754 else
11755 Decl := Build_Actual_Subtype (T, Formal);
11756 end if;
11757
11758 if Present (Handled_Statement_Sequence (N)) then
11759 First_Stmt :=
11760 First (Statements (Handled_Statement_Sequence (N)));
11761 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
11762 Mark_Rewrite_Insertion (Decl);
11763 else
11764 -- If the accept statement has no body, there will be no
11765 -- reference to the actuals, so no need to compute actual
11766 -- subtypes.
11767
11768 return;
11769 end if;
11770
11771 else
11772 Decl := Build_Actual_Subtype (T, Formal);
11773 Prepend (Decl, Declarations (N));
11774 Mark_Rewrite_Insertion (Decl);
11775 end if;
11776
11777 -- The declaration uses the bounds of an existing object, and
11778 -- therefore needs no constraint checks.
11779
11780 Analyze (Decl, Suppress => All_Checks);
11781
11782 -- We need to freeze manually the generated type when it is
11783 -- inserted anywhere else than in a declarative part.
11784
11785 if Present (First_Stmt) then
11786 Insert_List_Before_And_Analyze (First_Stmt,
11787 Freeze_Entity (Defining_Identifier (Decl), N));
11788
11789 -- Ditto if the type has a dynamic predicate, because the
11790 -- generated function will mention the actual subtype.
11791
11792 elsif Has_Dynamic_Predicate_Aspect (T) then
11793 Insert_List_Before_And_Analyze (Decl,
11794 Freeze_Entity (Defining_Identifier (Decl), N));
11795 end if;
11796
11797 if Nkind (N) = N_Accept_Statement
11798 and then Expander_Active
11799 then
11800 Set_Actual_Subtype (Renamed_Object (Formal),
11801 Defining_Identifier (Decl));
11802 else
11803 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
11804 end if;
11805 end if;
11806
11807 Next_Formal (Formal);
11808 end loop;
11809 end Set_Actual_Subtypes;
11810
11811 ---------------------
11812 -- Set_Formal_Mode --
11813 ---------------------
11814
11815 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
11816 Spec : constant Node_Id := Parent (Formal_Id);
11817
11818 begin
11819 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11820 -- since we ensure that corresponding actuals are always valid at the
11821 -- point of the call.
11822
11823 if Out_Present (Spec) then
11824 if Ekind (Scope (Formal_Id)) = E_Function
11825 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
11826 then
11827 -- [IN] OUT parameters allowed for functions in Ada 2012
11828
11829 if Ada_Version >= Ada_2012 then
11830
11831 -- Even in Ada 2012 operators can only have IN parameters
11832
11833 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
11834 Error_Msg_N ("operators can only have IN parameters", Spec);
11835 end if;
11836
11837 if In_Present (Spec) then
11838 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11839 else
11840 Set_Ekind (Formal_Id, E_Out_Parameter);
11841 end if;
11842
11843 -- But not in earlier versions of Ada
11844
11845 else
11846 Error_Msg_N ("functions can only have IN parameters", Spec);
11847 Set_Ekind (Formal_Id, E_In_Parameter);
11848 end if;
11849
11850 elsif In_Present (Spec) then
11851 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11852
11853 else
11854 Set_Ekind (Formal_Id, E_Out_Parameter);
11855 Set_Never_Set_In_Source (Formal_Id, True);
11856 Set_Is_True_Constant (Formal_Id, False);
11857 Set_Current_Value (Formal_Id, Empty);
11858 end if;
11859
11860 else
11861 Set_Ekind (Formal_Id, E_In_Parameter);
11862 end if;
11863
11864 -- Set Is_Known_Non_Null for access parameters since the language
11865 -- guarantees that access parameters are always non-null. We also set
11866 -- Can_Never_Be_Null, since there is no way to change the value.
11867
11868 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
11869
11870 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
11871 -- null; In Ada 2005, only if then null_exclusion is explicit.
11872
11873 if Ada_Version < Ada_2005
11874 or else Can_Never_Be_Null (Etype (Formal_Id))
11875 then
11876 Set_Is_Known_Non_Null (Formal_Id);
11877 Set_Can_Never_Be_Null (Formal_Id);
11878 end if;
11879
11880 -- Ada 2005 (AI-231): Null-exclusion access subtype
11881
11882 elsif Is_Access_Type (Etype (Formal_Id))
11883 and then Can_Never_Be_Null (Etype (Formal_Id))
11884 then
11885 Set_Is_Known_Non_Null (Formal_Id);
11886
11887 -- We can also set Can_Never_Be_Null (thus preventing some junk
11888 -- access checks) for the case of an IN parameter, which cannot
11889 -- be changed, or for an IN OUT parameter, which can be changed but
11890 -- not to a null value. But for an OUT parameter, the initial value
11891 -- passed in can be null, so we can't set this flag in that case.
11892
11893 if Ekind (Formal_Id) /= E_Out_Parameter then
11894 Set_Can_Never_Be_Null (Formal_Id);
11895 end if;
11896 end if;
11897
11898 Set_Mechanism (Formal_Id, Default_Mechanism);
11899 Set_Formal_Validity (Formal_Id);
11900 end Set_Formal_Mode;
11901
11902 -------------------------
11903 -- Set_Formal_Validity --
11904 -------------------------
11905
11906 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
11907 begin
11908 -- If no validity checking, then we cannot assume anything about the
11909 -- validity of parameters, since we do not know there is any checking
11910 -- of the validity on the call side.
11911
11912 if not Validity_Checks_On then
11913 return;
11914
11915 -- If validity checking for parameters is enabled, this means we are
11916 -- not supposed to make any assumptions about argument values.
11917
11918 elsif Validity_Check_Parameters then
11919 return;
11920
11921 -- If we are checking in parameters, we will assume that the caller is
11922 -- also checking parameters, so we can assume the parameter is valid.
11923
11924 elsif Ekind (Formal_Id) = E_In_Parameter
11925 and then Validity_Check_In_Params
11926 then
11927 Set_Is_Known_Valid (Formal_Id, True);
11928
11929 -- Similar treatment for IN OUT parameters
11930
11931 elsif Ekind (Formal_Id) = E_In_Out_Parameter
11932 and then Validity_Check_In_Out_Params
11933 then
11934 Set_Is_Known_Valid (Formal_Id, True);
11935 end if;
11936 end Set_Formal_Validity;
11937
11938 ------------------------
11939 -- Subtype_Conformant --
11940 ------------------------
11941
11942 function Subtype_Conformant
11943 (New_Id : Entity_Id;
11944 Old_Id : Entity_Id;
11945 Skip_Controlling_Formals : Boolean := False) return Boolean
11946 is
11947 Result : Boolean;
11948 begin
11949 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
11950 Skip_Controlling_Formals => Skip_Controlling_Formals);
11951 return Result;
11952 end Subtype_Conformant;
11953
11954 ---------------------
11955 -- Type_Conformant --
11956 ---------------------
11957
11958 function Type_Conformant
11959 (New_Id : Entity_Id;
11960 Old_Id : Entity_Id;
11961 Skip_Controlling_Formals : Boolean := False) return Boolean
11962 is
11963 Result : Boolean;
11964 begin
11965 May_Hide_Profile := False;
11966
11967 Check_Conformance
11968 (New_Id, Old_Id, Type_Conformant, False, Result,
11969 Skip_Controlling_Formals => Skip_Controlling_Formals);
11970 return Result;
11971 end Type_Conformant;
11972
11973 -------------------------------
11974 -- Valid_Operator_Definition --
11975 -------------------------------
11976
11977 procedure Valid_Operator_Definition (Designator : Entity_Id) is
11978 N : Integer := 0;
11979 F : Entity_Id;
11980 Id : constant Name_Id := Chars (Designator);
11981 N_OK : Boolean;
11982
11983 begin
11984 F := First_Formal (Designator);
11985 while Present (F) loop
11986 N := N + 1;
11987
11988 if Present (Default_Value (F)) then
11989 Error_Msg_N
11990 ("default values not allowed for operator parameters",
11991 Parent (F));
11992 end if;
11993
11994 Next_Formal (F);
11995 end loop;
11996
11997 -- Verify that user-defined operators have proper number of arguments
11998 -- First case of operators which can only be unary
11999
12000 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12001 N_OK := (N = 1);
12002
12003 -- Case of operators which can be unary or binary
12004
12005 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12006 N_OK := (N in 1 .. 2);
12007
12008 -- All other operators can only be binary
12009
12010 else
12011 N_OK := (N = 2);
12012 end if;
12013
12014 if not N_OK then
12015 Error_Msg_N
12016 ("incorrect number of arguments for operator", Designator);
12017 end if;
12018
12019 if Id = Name_Op_Ne
12020 and then Base_Type (Etype (Designator)) = Standard_Boolean
12021 and then not Is_Intrinsic_Subprogram (Designator)
12022 then
12023 Error_Msg_N
12024 ("explicit definition of inequality not allowed", Designator);
12025 end if;
12026 end Valid_Operator_Definition;
12027
12028 end Sem_Ch6;