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