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