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