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