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