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