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