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