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