2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / sem_ch6.adb
blob640a20d0a54c66955247fdbe9608b5f872b97524
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-2008, 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 ------------------------------------------------------------------------------
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 Rtsfind; use Rtsfind;
51 with Sem; use Sem;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch4; use Sem_Ch4;
55 with Sem_Ch5; use Sem_Ch5;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch10; use Sem_Ch10;
58 with Sem_Ch12; use Sem_Ch12;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Dist; use Sem_Dist;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Mech; use Sem_Mech;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Res; use Sem_Res;
66 with Sem_Util; use Sem_Util;
67 with Sem_Type; use Sem_Type;
68 with Sem_Warn; use Sem_Warn;
69 with Sinput; use Sinput;
70 with Stand; use Stand;
71 with Sinfo; use Sinfo;
72 with Sinfo.CN; use Sinfo.CN;
73 with Snames; use Snames;
74 with Stringt; use Stringt;
75 with Style;
76 with Stylesw; use Stylesw;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
80 with Validsw; use Validsw;
82 package body Sem_Ch6 is
84 May_Hide_Profile : Boolean := False;
85 -- This flag is used to indicate that two formals in two subprograms being
86 -- checked for conformance differ only in that one is an access parameter
87 -- while the other is of a general access type with the same designated
88 -- type. In this case, if the rest of the signatures match, a call to
89 -- either subprogram may be ambiguous, which is worth a warning. The flag
90 -- is set in Compatible_Types, and the warning emitted in
91 -- New_Overloaded_Entity.
93 -----------------------
94 -- Local Subprograms --
95 -----------------------
97 procedure Analyze_Return_Statement (N : Node_Id);
98 -- Common processing for simple_ and extended_return_statements
100 procedure Analyze_Function_Return (N : Node_Id);
101 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
102 -- applies to a [generic] function.
104 procedure Analyze_Return_Type (N : Node_Id);
105 -- Subsidiary to Process_Formals: analyze subtype mark in function
106 -- specification, in a context where the formals are visible and hide
107 -- outer homographs.
109 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
110 -- Analyze a generic subprogram body. N is the body to be analyzed, and
111 -- Gen_Id is the defining entity Id for the corresponding spec.
113 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
114 -- If a subprogram has pragma Inline and inlining is active, use generic
115 -- machinery to build an unexpanded body for the subprogram. This body is
116 -- subsequently used for inline expansions at call sites. If subprogram can
117 -- be inlined (depending on size and nature of local declarations) this
118 -- function returns true. Otherwise subprogram body is treated normally.
119 -- If proper warnings are enabled and the subprogram contains a construct
120 -- that cannot be inlined, the offending construct is flagged accordingly.
122 procedure Check_Conformance
123 (New_Id : Entity_Id;
124 Old_Id : Entity_Id;
125 Ctype : Conformance_Type;
126 Errmsg : Boolean;
127 Conforms : out Boolean;
128 Err_Loc : Node_Id := Empty;
129 Get_Inst : Boolean := False;
130 Skip_Controlling_Formals : Boolean := False);
131 -- Given two entities, this procedure checks that the profiles associated
132 -- with these entities meet the conformance criterion given by the third
133 -- parameter. If they conform, Conforms is set True and control returns
134 -- to the caller. If they do not conform, Conforms is set to False, and
135 -- in addition, if Errmsg is True on the call, proper messages are output
136 -- to complain about the conformance failure. If Err_Loc is non_Empty
137 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
138 -- error messages are placed on the appropriate part of the construct
139 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
140 -- against a formal access-to-subprogram type so Get_Instance_Of must
141 -- be called.
143 procedure Check_Subprogram_Order (N : Node_Id);
144 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
145 -- the alpha ordering rule for N if this ordering requirement applicable.
147 procedure Check_Returns
148 (HSS : Node_Id;
149 Mode : Character;
150 Err : out Boolean;
151 Proc : Entity_Id := Empty);
152 -- Called to check for missing return statements in a function body, or for
153 -- returns present in a procedure body which has No_Return set. HSS is the
154 -- handled statement sequence for the subprogram body. This procedure
155 -- checks all flow paths to make sure they either have return (Mode = 'F',
156 -- used for functions) or do not have a return (Mode = 'P', used for
157 -- No_Return procedures). The flag Err is set if there are any control
158 -- paths not explicitly terminated by a return in the function case, and is
159 -- True otherwise. Proc is the entity for the procedure case and is used
160 -- in posting the warning message.
162 procedure Enter_Overloaded_Entity (S : Entity_Id);
163 -- This procedure makes S, a new overloaded entity, into the first visible
164 -- entity with that name.
166 procedure Install_Entity (E : Entity_Id);
167 -- Make single entity visible. Used for generic formals as well
169 function Is_Non_Overriding_Operation
170 (Prev_E : Entity_Id;
171 New_E : Entity_Id) return Boolean;
172 -- Enforce the rule given in 12.3(18): a private operation in an instance
173 -- overrides an inherited operation only if the corresponding operation
174 -- was overriding in the generic. This can happen for primitive operations
175 -- of types derived (in the generic unit) from formal private or formal
176 -- derived types.
178 procedure Make_Inequality_Operator (S : Entity_Id);
179 -- Create the declaration for an inequality operator that is implicitly
180 -- created by a user-defined equality operator that yields a boolean.
182 procedure May_Need_Actuals (Fun : Entity_Id);
183 -- Flag functions that can be called without parameters, i.e. those that
184 -- have no parameters, or those for which defaults exist for all parameters
186 procedure Process_PPCs
187 (N : Node_Id;
188 Spec_Id : Entity_Id;
189 Body_Id : Entity_Id);
190 -- Called from Analyze_Body to deal with scanning post conditions for the
191 -- body and assembling and inserting the _postconditions procedure. N is
192 -- the node for the subprogram body and Body_Id/Spec_Id are the entities
193 -- for the body and separate spec (if there is no separate spec, Spec_Id
194 -- is Empty).
196 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
197 -- Formal_Id is an formal parameter entity. This procedure deals with
198 -- setting the proper validity status for this entity, which depends
199 -- on the kind of parameter and the validity checking mode.
201 ------------------------------
202 -- Analyze_Return_Statement --
203 ------------------------------
205 procedure Analyze_Return_Statement (N : Node_Id) is
207 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
208 N_Extended_Return_Statement));
210 Returns_Object : constant Boolean :=
211 Nkind (N) = N_Extended_Return_Statement
212 or else
213 (Nkind (N) = N_Simple_Return_Statement
214 and then Present (Expression (N)));
215 -- True if we're returning something; that is, "return <expression>;"
216 -- or "return Result : T [:= ...]". False for "return;". Used for error
217 -- checking: If Returns_Object is True, N should apply to a function
218 -- body; otherwise N should apply to a procedure body, entry body,
219 -- accept statement, or extended return statement.
221 function Find_What_It_Applies_To return Entity_Id;
222 -- Find the entity representing the innermost enclosing body, accept
223 -- statement, or extended return statement. If the result is a callable
224 -- construct or extended return statement, then this will be the value
225 -- of the Return_Applies_To attribute. Otherwise, the program is
226 -- illegal. See RM-6.5(4/2).
228 -----------------------------
229 -- Find_What_It_Applies_To --
230 -----------------------------
232 function Find_What_It_Applies_To return Entity_Id is
233 Result : Entity_Id := Empty;
235 begin
236 -- Loop outward through the Scope_Stack, skipping blocks and loops
238 for J in reverse 0 .. Scope_Stack.Last loop
239 Result := Scope_Stack.Table (J).Entity;
240 exit when Ekind (Result) /= E_Block and then
241 Ekind (Result) /= E_Loop;
242 end loop;
244 pragma Assert (Present (Result));
245 return Result;
246 end Find_What_It_Applies_To;
248 -- Local declarations
250 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
251 Kind : constant Entity_Kind := Ekind (Scope_Id);
252 Loc : constant Source_Ptr := Sloc (N);
253 Stm_Entity : constant Entity_Id :=
254 New_Internal_Entity
255 (E_Return_Statement, Current_Scope, Loc, 'R');
257 -- Start of processing for Analyze_Return_Statement
259 begin
260 Set_Return_Statement_Entity (N, Stm_Entity);
262 Set_Etype (Stm_Entity, Standard_Void_Type);
263 Set_Return_Applies_To (Stm_Entity, Scope_Id);
265 -- Place Return entity on scope stack, to simplify enforcement of 6.5
266 -- (4/2): an inner return statement will apply to this extended return.
268 if Nkind (N) = N_Extended_Return_Statement then
269 Push_Scope (Stm_Entity);
270 end if;
272 -- Check that pragma No_Return is obeyed
274 if No_Return (Scope_Id) then
275 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
276 end if;
278 -- Warn on any unassigned OUT parameters if in procedure
280 if Ekind (Scope_Id) = E_Procedure then
281 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
282 end if;
284 -- Check that functions return objects, and other things do not
286 if Kind = E_Function or else Kind = E_Generic_Function then
287 if not Returns_Object then
288 Error_Msg_N ("missing expression in return from function", N);
289 end if;
291 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
292 if Returns_Object then
293 Error_Msg_N ("procedure cannot return value (use function)", N);
294 end if;
296 elsif Kind = E_Entry or else Kind = E_Entry_Family then
297 if Returns_Object then
298 if Is_Protected_Type (Scope (Scope_Id)) then
299 Error_Msg_N ("entry body cannot return value", N);
300 else
301 Error_Msg_N ("accept statement cannot return value", N);
302 end if;
303 end if;
305 elsif Kind = E_Return_Statement then
307 -- We are nested within another return statement, which must be an
308 -- extended_return_statement.
310 if Returns_Object then
311 Error_Msg_N
312 ("extended_return_statement cannot return value; " &
313 "use `""RETURN;""`", N);
314 end if;
316 else
317 Error_Msg_N ("illegal context for return statement", N);
318 end if;
320 if Kind = E_Function or else Kind = E_Generic_Function then
321 Analyze_Function_Return (N);
322 end if;
324 if Nkind (N) = N_Extended_Return_Statement then
325 End_Scope;
326 end if;
328 Kill_Current_Values (Last_Assignment_Only => True);
329 Check_Unreachable_Code (N);
330 end Analyze_Return_Statement;
332 ---------------------------------------------
333 -- Analyze_Abstract_Subprogram_Declaration --
334 ---------------------------------------------
336 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
337 Designator : constant Entity_Id :=
338 Analyze_Subprogram_Specification (Specification (N));
339 Scop : constant Entity_Id := Current_Scope;
341 begin
342 Generate_Definition (Designator);
343 Set_Is_Abstract_Subprogram (Designator);
344 New_Overloaded_Entity (Designator);
345 Check_Delayed_Subprogram (Designator);
347 Set_Categorization_From_Scope (Designator, Scop);
349 if Ekind (Scope (Designator)) = E_Protected_Type then
350 Error_Msg_N
351 ("abstract subprogram not allowed in protected type", N);
353 -- Issue a warning if the abstract subprogram is neither a dispatching
354 -- operation nor an operation that overrides an inherited subprogram or
355 -- predefined operator, since this most likely indicates a mistake.
357 elsif Warn_On_Redundant_Constructs
358 and then not Is_Dispatching_Operation (Designator)
359 and then not Is_Overriding_Operation (Designator)
360 and then (not Is_Operator_Symbol_Name (Chars (Designator))
361 or else Scop /= Scope (Etype (First_Formal (Designator))))
362 then
363 Error_Msg_N
364 ("?abstract subprogram is not dispatching or overriding", N);
365 end if;
367 Generate_Reference_To_Formals (Designator);
368 end Analyze_Abstract_Subprogram_Declaration;
370 ----------------------------------------
371 -- Analyze_Extended_Return_Statement --
372 ----------------------------------------
374 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
375 begin
376 Analyze_Return_Statement (N);
377 end Analyze_Extended_Return_Statement;
379 ----------------------------
380 -- Analyze_Function_Call --
381 ----------------------------
383 procedure Analyze_Function_Call (N : Node_Id) is
384 P : constant Node_Id := Name (N);
385 L : constant List_Id := Parameter_Associations (N);
386 Actual : Node_Id;
388 begin
389 Analyze (P);
391 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
392 -- as B (A, X). If the rewriting is successful, the call has been
393 -- analyzed and we just return.
395 if Nkind (P) = N_Selected_Component
396 and then Name (N) /= P
397 and then Is_Rewrite_Substitution (N)
398 and then Present (Etype (N))
399 then
400 return;
401 end if;
403 -- If error analyzing name, then set Any_Type as result type and return
405 if Etype (P) = Any_Type then
406 Set_Etype (N, Any_Type);
407 return;
408 end if;
410 -- Otherwise analyze the parameters
412 if Present (L) then
413 Actual := First (L);
414 while Present (Actual) loop
415 Analyze (Actual);
416 Check_Parameterless_Call (Actual);
417 Next (Actual);
418 end loop;
419 end if;
421 Analyze_Call (N);
422 end Analyze_Function_Call;
424 -----------------------------
425 -- Analyze_Function_Return --
426 -----------------------------
428 procedure Analyze_Function_Return (N : Node_Id) is
429 Loc : constant Source_Ptr := Sloc (N);
430 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
431 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
433 R_Type : constant Entity_Id := Etype (Scope_Id);
434 -- Function result subtype
436 procedure Check_Limited_Return (Expr : Node_Id);
437 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
438 -- limited types. Used only for simple return statements.
439 -- Expr is the expression returned.
441 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
442 -- Check that the return_subtype_indication properly matches the result
443 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
445 --------------------------
446 -- Check_Limited_Return --
447 --------------------------
449 procedure Check_Limited_Return (Expr : Node_Id) is
450 begin
451 -- Ada 2005 (AI-318-02): Return-by-reference types have been
452 -- removed and replaced by anonymous access results. This is an
453 -- incompatibility with Ada 95. Not clear whether this should be
454 -- enforced yet or perhaps controllable with special switch. ???
456 if Is_Limited_Type (R_Type)
457 and then Comes_From_Source (N)
458 and then not In_Instance_Body
459 and then not OK_For_Limited_Init_In_05 (Expr)
460 then
461 -- Error in Ada 2005
463 if Ada_Version >= Ada_05
464 and then not Debug_Flag_Dot_L
465 and then not GNAT_Mode
466 then
467 Error_Msg_N
468 ("(Ada 2005) cannot copy object of a limited type " &
469 "(RM-2005 6.5(5.5/2))", Expr);
470 if Is_Inherently_Limited_Type (R_Type) then
471 Error_Msg_N
472 ("\return by reference not permitted in Ada 2005", Expr);
473 end if;
475 -- Warn in Ada 95 mode, to give folks a heads up about this
476 -- incompatibility.
478 -- In GNAT mode, this is just a warning, to allow it to be
479 -- evilly turned off. Otherwise it is a real error.
481 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
482 if Is_Inherently_Limited_Type (R_Type) then
483 Error_Msg_N
484 ("return by reference not permitted in Ada 2005 " &
485 "(RM-2005 6.5(5.5/2))?", Expr);
486 else
487 Error_Msg_N
488 ("cannot copy object of a limited type in Ada 2005 " &
489 "(RM-2005 6.5(5.5/2))?", Expr);
490 end if;
492 -- Ada 95 mode, compatibility warnings disabled
494 else
495 return; -- skip continuation messages below
496 end if;
498 Error_Msg_N
499 ("\consider switching to return of access type", Expr);
500 Explain_Limited_Type (R_Type, Expr);
501 end if;
502 end Check_Limited_Return;
504 -------------------------------------
505 -- Check_Return_Subtype_Indication --
506 -------------------------------------
508 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
509 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
510 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
511 -- Subtype given in the extended return statement;
512 -- this must match R_Type.
514 Subtype_Ind : constant Node_Id :=
515 Object_Definition (Original_Node (Obj_Decl));
517 R_Type_Is_Anon_Access :
518 constant Boolean :=
519 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
520 or else
521 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
522 or else
523 Ekind (R_Type) = E_Anonymous_Access_Type;
524 -- True if return type of the function is an anonymous access type
525 -- Can't we make Is_Anonymous_Access_Type in einfo ???
527 R_Stm_Type_Is_Anon_Access :
528 constant Boolean :=
529 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
530 or else
531 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
532 or else
533 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
534 -- True if type of the return object is an anonymous access type
536 begin
537 -- First, avoid cascade errors:
539 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
540 return;
541 end if;
543 -- "return access T" case; check that the return statement also has
544 -- "access T", and that the subtypes statically match:
546 if R_Type_Is_Anon_Access then
547 if R_Stm_Type_Is_Anon_Access then
548 if Base_Type (Designated_Type (R_Stm_Type)) /=
549 Base_Type (Designated_Type (R_Type))
550 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
551 then
552 Error_Msg_N
553 ("subtype must statically match function result subtype",
554 Subtype_Mark (Subtype_Ind));
555 end if;
557 else
558 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
559 end if;
561 -- Subtype_indication case; check that the types are the same, and
562 -- statically match if appropriate. A null exclusion may be present
563 -- on the return type, on the function specification, on the object
564 -- declaration or on the subtype itself.
566 elsif Base_Type (R_Stm_Type) = Base_Type (R_Type) then
567 if Is_Access_Type (R_Type)
568 and then
569 (Can_Never_Be_Null (R_Type)
570 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
571 Can_Never_Be_Null (R_Stm_Type)
572 then
573 Error_Msg_N
574 ("subtype must statically match function result subtype",
575 Subtype_Ind);
576 end if;
578 if Is_Constrained (R_Type) then
579 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
580 Error_Msg_N
581 ("subtype must statically match function result subtype",
582 Subtype_Ind);
583 end if;
584 end if;
586 -- If the function's result type doesn't match the return object
587 -- entity's type, then we check for the case where the result type
588 -- is class-wide, and allow the declaration if the type of the object
589 -- definition matches the class-wide type. This prevents rejection
590 -- in the case where the object declaration is initialized by a call
591 -- to a build-in-place function with a specific result type and the
592 -- object entity had its type changed to that specific type. (Note
593 -- that the ARG believes that return objects should be allowed to
594 -- have a type covered by a class-wide result type in any case, so
595 -- once that relaxation is made (see AI05-32), the above check for
596 -- type compatibility should be changed to test Covers rather than
597 -- equality, and then the following special test will no longer be
598 -- needed. ???)
600 elsif Is_Class_Wide_Type (R_Type)
601 and then
602 R_Type = Etype (Object_Definition (Original_Node (Obj_Decl)))
603 then
604 null;
606 else
607 Error_Msg_N
608 ("wrong type for return_subtype_indication", Subtype_Ind);
609 end if;
610 end Check_Return_Subtype_Indication;
612 ---------------------
613 -- Local Variables --
614 ---------------------
616 Expr : Node_Id;
618 -- Start of processing for Analyze_Function_Return
620 begin
621 Set_Return_Present (Scope_Id);
623 if Nkind (N) = N_Simple_Return_Statement then
624 Expr := Expression (N);
625 Analyze_And_Resolve (Expr, R_Type);
626 Check_Limited_Return (Expr);
628 else
629 -- Analyze parts specific to extended_return_statement:
631 declare
632 Obj_Decl : constant Node_Id :=
633 Last (Return_Object_Declarations (N));
635 HSS : constant Node_Id := Handled_Statement_Sequence (N);
637 begin
638 Expr := Expression (Obj_Decl);
640 -- Note: The check for OK_For_Limited_Init will happen in
641 -- Analyze_Object_Declaration; we treat it as a normal
642 -- object declaration.
644 Analyze (Obj_Decl);
646 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
647 Check_Return_Subtype_Indication (Obj_Decl);
649 if Present (HSS) then
650 Analyze (HSS);
652 if Present (Exception_Handlers (HSS)) then
654 -- ???Has_Nested_Block_With_Handler needs to be set.
655 -- Probably by creating an actual N_Block_Statement.
656 -- Probably in Expand.
658 null;
659 end if;
660 end if;
662 Check_References (Stm_Entity);
663 end;
664 end if;
666 -- Case of Expr present
668 if Present (Expr)
670 -- Defend against previous errors
672 and then Nkind (Expr) /= N_Empty
673 and then Present (Etype (Expr))
674 then
675 -- Apply constraint check. Note that this is done before the implicit
676 -- conversion of the expression done for anonymous access types to
677 -- ensure correct generation of the null-excluding check associated
678 -- with null-excluding expressions found in return statements.
680 Apply_Constraint_Check (Expr, R_Type);
682 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
683 -- type, apply an implicit conversion of the expression to that type
684 -- to force appropriate static and run-time accessibility checks.
686 if Ada_Version >= Ada_05
687 and then Ekind (R_Type) = E_Anonymous_Access_Type
688 then
689 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
690 Analyze_And_Resolve (Expr, R_Type);
691 end if;
693 -- If the result type is class-wide, then check that the return
694 -- expression's type is not declared at a deeper level than the
695 -- function (RM05-6.5(5.6/2)).
697 if Ada_Version >= Ada_05
698 and then Is_Class_Wide_Type (R_Type)
699 then
700 if Type_Access_Level (Etype (Expr)) >
701 Subprogram_Access_Level (Scope_Id)
702 then
703 Error_Msg_N
704 ("level of return expression type is deeper than " &
705 "class-wide function!", Expr);
706 end if;
707 end if;
709 if (Is_Class_Wide_Type (Etype (Expr))
710 or else Is_Dynamically_Tagged (Expr))
711 and then not Is_Class_Wide_Type (R_Type)
712 then
713 Error_Msg_N
714 ("dynamically tagged expression not allowed!", Expr);
715 end if;
717 -- ??? A real run-time accessibility check is needed in cases
718 -- involving dereferences of access parameters. For now we just
719 -- check the static cases.
721 if (Ada_Version < Ada_05 or else Debug_Flag_Dot_L)
722 and then Is_Inherently_Limited_Type (Etype (Scope_Id))
723 and then Object_Access_Level (Expr) >
724 Subprogram_Access_Level (Scope_Id)
725 then
726 Rewrite (N,
727 Make_Raise_Program_Error (Loc,
728 Reason => PE_Accessibility_Check_Failed));
729 Analyze (N);
731 Error_Msg_N
732 ("cannot return a local value by reference?", N);
733 Error_Msg_NE
734 ("\& will be raised at run time?",
735 N, Standard_Program_Error);
736 end if;
738 if Known_Null (Expr)
739 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
740 and then Null_Exclusion_Present (Parent (Scope_Id))
741 then
742 Apply_Compile_Time_Constraint_Error
743 (N => Expr,
744 Msg => "(Ada 2005) null not allowed for "
745 & "null-excluding return?",
746 Reason => CE_Null_Not_Allowed);
747 end if;
748 end if;
749 end Analyze_Function_Return;
751 -------------------------------------
752 -- Analyze_Generic_Subprogram_Body --
753 -------------------------------------
755 procedure Analyze_Generic_Subprogram_Body
756 (N : Node_Id;
757 Gen_Id : Entity_Id)
759 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
760 Kind : constant Entity_Kind := Ekind (Gen_Id);
761 Body_Id : Entity_Id;
762 New_N : Node_Id;
763 Spec : Node_Id;
765 begin
766 -- Copy body and disable expansion while analyzing the generic For a
767 -- stub, do not copy the stub (which would load the proper body), this
768 -- will be done when the proper body is analyzed.
770 if Nkind (N) /= N_Subprogram_Body_Stub then
771 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
772 Rewrite (N, New_N);
773 Start_Generic;
774 end if;
776 Spec := Specification (N);
778 -- Within the body of the generic, the subprogram is callable, and
779 -- behaves like the corresponding non-generic unit.
781 Body_Id := Defining_Entity (Spec);
783 if Kind = E_Generic_Procedure
784 and then Nkind (Spec) /= N_Procedure_Specification
785 then
786 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
787 return;
789 elsif Kind = E_Generic_Function
790 and then Nkind (Spec) /= N_Function_Specification
791 then
792 Error_Msg_N ("invalid body for generic function ", Body_Id);
793 return;
794 end if;
796 Set_Corresponding_Body (Gen_Decl, Body_Id);
798 if Has_Completion (Gen_Id)
799 and then Nkind (Parent (N)) /= N_Subunit
800 then
801 Error_Msg_N ("duplicate generic body", N);
802 return;
803 else
804 Set_Has_Completion (Gen_Id);
805 end if;
807 if Nkind (N) = N_Subprogram_Body_Stub then
808 Set_Ekind (Defining_Entity (Specification (N)), Kind);
809 else
810 Set_Corresponding_Spec (N, Gen_Id);
811 end if;
813 if Nkind (Parent (N)) = N_Compilation_Unit then
814 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
815 end if;
817 -- Make generic parameters immediately visible in the body. They are
818 -- needed to process the formals declarations. Then make the formals
819 -- visible in a separate step.
821 Push_Scope (Gen_Id);
823 declare
824 E : Entity_Id;
825 First_Ent : Entity_Id;
827 begin
828 First_Ent := First_Entity (Gen_Id);
830 E := First_Ent;
831 while Present (E) and then not Is_Formal (E) loop
832 Install_Entity (E);
833 Next_Entity (E);
834 end loop;
836 Set_Use (Generic_Formal_Declarations (Gen_Decl));
838 -- Now generic formals are visible, and the specification can be
839 -- analyzed, for subsequent conformance check.
841 Body_Id := Analyze_Subprogram_Specification (Spec);
843 -- Make formal parameters visible
845 if Present (E) then
847 -- E is the first formal parameter, we loop through the formals
848 -- installing them so that they will be visible.
850 Set_First_Entity (Gen_Id, E);
851 while Present (E) loop
852 Install_Entity (E);
853 Next_Formal (E);
854 end loop;
855 end if;
857 -- Visible generic entity is callable within its own body
859 Set_Ekind (Gen_Id, Ekind (Body_Id));
860 Set_Ekind (Body_Id, E_Subprogram_Body);
861 Set_Convention (Body_Id, Convention (Gen_Id));
862 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
863 Set_Scope (Body_Id, Scope (Gen_Id));
864 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
866 if Nkind (N) = N_Subprogram_Body_Stub then
868 -- No body to analyze, so restore state of generic unit
870 Set_Ekind (Gen_Id, Kind);
871 Set_Ekind (Body_Id, Kind);
873 if Present (First_Ent) then
874 Set_First_Entity (Gen_Id, First_Ent);
875 end if;
877 End_Scope;
878 return;
879 end if;
881 -- If this is a compilation unit, it must be made visible explicitly,
882 -- because the compilation of the declaration, unlike other library
883 -- unit declarations, does not. If it is not a unit, the following
884 -- is redundant but harmless.
886 Set_Is_Immediately_Visible (Gen_Id);
887 Reference_Body_Formals (Gen_Id, Body_Id);
889 if Is_Child_Unit (Gen_Id) then
890 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
891 end if;
893 Set_Actual_Subtypes (N, Current_Scope);
894 Analyze_Declarations (Declarations (N));
895 Check_Completion;
896 Analyze (Handled_Statement_Sequence (N));
898 Save_Global_References (Original_Node (N));
900 -- Prior to exiting the scope, include generic formals again (if any
901 -- are present) in the set of local entities.
903 if Present (First_Ent) then
904 Set_First_Entity (Gen_Id, First_Ent);
905 end if;
907 Check_References (Gen_Id);
908 end;
910 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
911 End_Scope;
912 Check_Subprogram_Order (N);
914 -- Outside of its body, unit is generic again
916 Set_Ekind (Gen_Id, Kind);
917 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
919 if Style_Check then
920 Style.Check_Identifier (Body_Id, Gen_Id);
921 end if;
922 End_Generic;
923 end Analyze_Generic_Subprogram_Body;
925 -----------------------------
926 -- Analyze_Operator_Symbol --
927 -----------------------------
929 -- An operator symbol such as "+" or "and" may appear in context where the
930 -- literal denotes an entity name, such as "+"(x, y) or in context when it
931 -- is just a string, as in (conjunction = "or"). In these cases the parser
932 -- generates this node, and the semantics does the disambiguation. Other
933 -- such case are actuals in an instantiation, the generic unit in an
934 -- instantiation, and pragma arguments.
936 procedure Analyze_Operator_Symbol (N : Node_Id) is
937 Par : constant Node_Id := Parent (N);
939 begin
940 if (Nkind (Par) = N_Function_Call
941 and then N = Name (Par))
942 or else Nkind (Par) = N_Function_Instantiation
943 or else (Nkind (Par) = N_Indexed_Component
944 and then N = Prefix (Par))
945 or else (Nkind (Par) = N_Pragma_Argument_Association
946 and then not Is_Pragma_String_Literal (Par))
947 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
948 or else (Nkind (Par) = N_Attribute_Reference
949 and then Attribute_Name (Par) /= Name_Value)
950 then
951 Find_Direct_Name (N);
953 else
954 Change_Operator_Symbol_To_String_Literal (N);
955 Analyze (N);
956 end if;
957 end Analyze_Operator_Symbol;
959 -----------------------------------
960 -- Analyze_Parameter_Association --
961 -----------------------------------
963 procedure Analyze_Parameter_Association (N : Node_Id) is
964 begin
965 Analyze (Explicit_Actual_Parameter (N));
966 end Analyze_Parameter_Association;
968 ----------------------------
969 -- Analyze_Procedure_Call --
970 ----------------------------
972 procedure Analyze_Procedure_Call (N : Node_Id) is
973 Loc : constant Source_Ptr := Sloc (N);
974 P : constant Node_Id := Name (N);
975 Actuals : constant List_Id := Parameter_Associations (N);
976 Actual : Node_Id;
977 New_N : Node_Id;
979 procedure Analyze_Call_And_Resolve;
980 -- Do Analyze and Resolve calls for procedure call
982 ------------------------------
983 -- Analyze_Call_And_Resolve --
984 ------------------------------
986 procedure Analyze_Call_And_Resolve is
987 begin
988 if Nkind (N) = N_Procedure_Call_Statement then
989 Analyze_Call (N);
990 Resolve (N, Standard_Void_Type);
991 else
992 Analyze (N);
993 end if;
994 end Analyze_Call_And_Resolve;
996 -- Start of processing for Analyze_Procedure_Call
998 begin
999 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1000 -- a procedure call or an entry call. The prefix may denote an access
1001 -- to subprogram type, in which case an implicit dereference applies.
1002 -- If the prefix is an indexed component (without implicit dereference)
1003 -- then the construct denotes a call to a member of an entire family.
1004 -- If the prefix is a simple name, it may still denote a call to a
1005 -- parameterless member of an entry family. Resolution of these various
1006 -- interpretations is delicate.
1008 Analyze (P);
1010 -- If this is a call of the form Obj.Op, the call may have been
1011 -- analyzed and possibly rewritten into a block, in which case
1012 -- we are done.
1014 if Analyzed (N) then
1015 return;
1016 end if;
1018 -- If error analyzing prefix, then set Any_Type as result and return
1020 if Etype (P) = Any_Type then
1021 Set_Etype (N, Any_Type);
1022 return;
1023 end if;
1025 -- Otherwise analyze the parameters
1027 if Present (Actuals) then
1028 Actual := First (Actuals);
1030 while Present (Actual) loop
1031 Analyze (Actual);
1032 Check_Parameterless_Call (Actual);
1033 Next (Actual);
1034 end loop;
1035 end if;
1037 -- Special processing for Elab_Spec and Elab_Body calls
1039 if Nkind (P) = N_Attribute_Reference
1040 and then (Attribute_Name (P) = Name_Elab_Spec
1041 or else Attribute_Name (P) = Name_Elab_Body)
1042 then
1043 if Present (Actuals) then
1044 Error_Msg_N
1045 ("no parameters allowed for this call", First (Actuals));
1046 return;
1047 end if;
1049 Set_Etype (N, Standard_Void_Type);
1050 Set_Analyzed (N);
1052 elsif Is_Entity_Name (P)
1053 and then Is_Record_Type (Etype (Entity (P)))
1054 and then Remote_AST_I_Dereference (P)
1055 then
1056 return;
1058 elsif Is_Entity_Name (P)
1059 and then Ekind (Entity (P)) /= E_Entry_Family
1060 then
1061 if Is_Access_Type (Etype (P))
1062 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1063 and then No (Actuals)
1064 and then Comes_From_Source (N)
1065 then
1066 Error_Msg_N ("missing explicit dereference in call", N);
1067 end if;
1069 Analyze_Call_And_Resolve;
1071 -- If the prefix is the simple name of an entry family, this is
1072 -- a parameterless call from within the task body itself.
1074 elsif Is_Entity_Name (P)
1075 and then Nkind (P) = N_Identifier
1076 and then Ekind (Entity (P)) = E_Entry_Family
1077 and then Present (Actuals)
1078 and then No (Next (First (Actuals)))
1079 then
1080 -- Can be call to parameterless entry family. What appears to be the
1081 -- sole argument is in fact the entry index. Rewrite prefix of node
1082 -- accordingly. Source representation is unchanged by this
1083 -- transformation.
1085 New_N :=
1086 Make_Indexed_Component (Loc,
1087 Prefix =>
1088 Make_Selected_Component (Loc,
1089 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1090 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1091 Expressions => Actuals);
1092 Set_Name (N, New_N);
1093 Set_Etype (New_N, Standard_Void_Type);
1094 Set_Parameter_Associations (N, No_List);
1095 Analyze_Call_And_Resolve;
1097 elsif Nkind (P) = N_Explicit_Dereference then
1098 if Ekind (Etype (P)) = E_Subprogram_Type then
1099 Analyze_Call_And_Resolve;
1100 else
1101 Error_Msg_N ("expect access to procedure in call", P);
1102 end if;
1104 -- The name can be a selected component or an indexed component that
1105 -- yields an access to subprogram. Such a prefix is legal if the call
1106 -- has parameter associations.
1108 elsif Is_Access_Type (Etype (P))
1109 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1110 then
1111 if Present (Actuals) then
1112 Analyze_Call_And_Resolve;
1113 else
1114 Error_Msg_N ("missing explicit dereference in call ", N);
1115 end if;
1117 -- If not an access to subprogram, then the prefix must resolve to the
1118 -- name of an entry, entry family, or protected operation.
1120 -- For the case of a simple entry call, P is a selected component where
1121 -- the prefix is the task and the selector name is the entry. A call to
1122 -- a protected procedure will have the same syntax. If the protected
1123 -- object contains overloaded operations, the entity may appear as a
1124 -- function, the context will select the operation whose type is Void.
1126 elsif Nkind (P) = N_Selected_Component
1127 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1128 or else
1129 Ekind (Entity (Selector_Name (P))) = E_Procedure
1130 or else
1131 Ekind (Entity (Selector_Name (P))) = E_Function)
1132 then
1133 Analyze_Call_And_Resolve;
1135 elsif Nkind (P) = N_Selected_Component
1136 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1137 and then Present (Actuals)
1138 and then No (Next (First (Actuals)))
1139 then
1140 -- Can be call to parameterless entry family. What appears to be the
1141 -- sole argument is in fact the entry index. Rewrite prefix of node
1142 -- accordingly. Source representation is unchanged by this
1143 -- transformation.
1145 New_N :=
1146 Make_Indexed_Component (Loc,
1147 Prefix => New_Copy (P),
1148 Expressions => Actuals);
1149 Set_Name (N, New_N);
1150 Set_Etype (New_N, Standard_Void_Type);
1151 Set_Parameter_Associations (N, No_List);
1152 Analyze_Call_And_Resolve;
1154 -- For the case of a reference to an element of an entry family, P is
1155 -- an indexed component whose prefix is a selected component (task and
1156 -- entry family), and whose index is the entry family index.
1158 elsif Nkind (P) = N_Indexed_Component
1159 and then Nkind (Prefix (P)) = N_Selected_Component
1160 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1161 then
1162 Analyze_Call_And_Resolve;
1164 -- If the prefix is the name of an entry family, it is a call from
1165 -- within the task body itself.
1167 elsif Nkind (P) = N_Indexed_Component
1168 and then Nkind (Prefix (P)) = N_Identifier
1169 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1170 then
1171 New_N :=
1172 Make_Selected_Component (Loc,
1173 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1174 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1175 Rewrite (Prefix (P), New_N);
1176 Analyze (P);
1177 Analyze_Call_And_Resolve;
1179 -- Anything else is an error
1181 else
1182 Error_Msg_N ("invalid procedure or entry call", N);
1183 end if;
1184 end Analyze_Procedure_Call;
1186 -------------------------------------
1187 -- Analyze_Simple_Return_Statement --
1188 -------------------------------------
1190 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1191 begin
1192 if Present (Expression (N)) then
1193 Mark_Coextensions (N, Expression (N));
1194 end if;
1196 Analyze_Return_Statement (N);
1197 end Analyze_Simple_Return_Statement;
1199 -------------------------
1200 -- Analyze_Return_Type --
1201 -------------------------
1203 procedure Analyze_Return_Type (N : Node_Id) is
1204 Designator : constant Entity_Id := Defining_Entity (N);
1205 Typ : Entity_Id := Empty;
1207 begin
1208 -- Normal case where result definition does not indicate an error
1210 if Result_Definition (N) /= Error then
1211 if Nkind (Result_Definition (N)) = N_Access_Definition then
1212 Typ := Access_Definition (N, Result_Definition (N));
1213 Set_Parent (Typ, Result_Definition (N));
1214 Set_Is_Local_Anonymous_Access (Typ);
1215 Set_Etype (Designator, Typ);
1217 -- Subtype_Mark case
1219 else
1220 Find_Type (Result_Definition (N));
1221 Typ := Entity (Result_Definition (N));
1222 Set_Etype (Designator, Typ);
1224 if Ekind (Typ) = E_Incomplete_Type
1225 and then Is_Value_Type (Typ)
1226 then
1227 null;
1229 elsif Ekind (Typ) = E_Incomplete_Type
1230 or else (Is_Class_Wide_Type (Typ)
1231 and then
1232 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1233 then
1234 Error_Msg_N
1235 ("invalid use of incomplete type", Result_Definition (N));
1236 end if;
1237 end if;
1239 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1241 Null_Exclusion_Static_Checks (N);
1243 -- Case where result definition does indicate an error
1245 else
1246 Set_Etype (Designator, Any_Type);
1247 end if;
1248 end Analyze_Return_Type;
1250 -----------------------------
1251 -- Analyze_Subprogram_Body --
1252 -----------------------------
1254 -- This procedure is called for regular subprogram bodies, generic bodies,
1255 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1256 -- specification matters, and is used to create a proper declaration for
1257 -- the subprogram, or to perform conformance checks.
1259 procedure Analyze_Subprogram_Body (N : Node_Id) is
1260 Loc : constant Source_Ptr := Sloc (N);
1261 Body_Deleted : constant Boolean := False;
1262 Body_Spec : constant Node_Id := Specification (N);
1263 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1264 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1265 Conformant : Boolean;
1266 HSS : Node_Id;
1267 Missing_Ret : Boolean;
1268 P_Ent : Entity_Id;
1269 Prot_Typ : Entity_Id := Empty;
1270 Spec_Id : Entity_Id;
1271 Spec_Decl : Node_Id := Empty;
1273 Last_Real_Spec_Entity : Entity_Id := Empty;
1274 -- When we analyze a separate spec, the entity chain ends up containing
1275 -- the formals, as well as any itypes generated during analysis of the
1276 -- default expressions for parameters, or the arguments of associated
1277 -- precondition/postcondition pragmas (which are analyzed in the context
1278 -- of the spec since they have visibility on formals).
1280 -- These entities belong with the spec and not the body. However we do
1281 -- the analysis of the body in the context of the spec (again to obtain
1282 -- visibility to the formals), and all the entities generated during
1283 -- this analysis end up also chained to the entity chain of the spec.
1284 -- But they really belong to the body, and there is circuitry to move
1285 -- them from the spec to the body.
1287 -- However, when we do this move, we don't want to move the real spec
1288 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1289 -- variable points to the last real spec entity, so we only move those
1290 -- chained beyond that point. It is initialized to Empty to deal with
1291 -- the case where there is no separate spec.
1293 procedure Check_Anonymous_Return;
1294 -- (Ada 2005): if a function returns an access type that denotes a task,
1295 -- or a type that contains tasks, we must create a master entity for
1296 -- the anonymous type, which typically will be used in an allocator
1297 -- in the body of the function.
1299 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1300 -- Look ahead to recognize a pragma that may appear after the body.
1301 -- If there is a previous spec, check that it appears in the same
1302 -- declarative part. If the pragma is Inline_Always, perform inlining
1303 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1304 -- If the body acts as a spec, and inlining is required, we create a
1305 -- subprogram declaration for it, in order to attach the body to inline.
1306 -- If pragma does not appear after the body, check whether there is
1307 -- an inline pragma before any local declarations.
1309 procedure Set_Trivial_Subprogram (N : Node_Id);
1310 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1311 -- subprogram whose body is being analyzed. N is the statement node
1312 -- causing the flag to be set, if the following statement is a return
1313 -- of an entity, we mark the entity as set in source to suppress any
1314 -- warning on the stylized use of function stubs with a dummy return.
1316 procedure Verify_Overriding_Indicator;
1317 -- If there was a previous spec, the entity has been entered in the
1318 -- current scope previously. If the body itself carries an overriding
1319 -- indicator, check that it is consistent with the known status of the
1320 -- entity.
1322 ----------------------------
1323 -- Check_Anonymous_Return --
1324 ----------------------------
1326 procedure Check_Anonymous_Return is
1327 Decl : Node_Id;
1328 Scop : Entity_Id;
1330 begin
1331 if Present (Spec_Id) then
1332 Scop := Spec_Id;
1333 else
1334 Scop := Body_Id;
1335 end if;
1337 if Ekind (Scop) = E_Function
1338 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1339 and then Has_Task (Designated_Type (Etype (Scop)))
1340 and then Expander_Active
1341 then
1342 Decl :=
1343 Make_Object_Declaration (Loc,
1344 Defining_Identifier =>
1345 Make_Defining_Identifier (Loc, Name_uMaster),
1346 Constant_Present => True,
1347 Object_Definition =>
1348 New_Reference_To (RTE (RE_Master_Id), Loc),
1349 Expression =>
1350 Make_Explicit_Dereference (Loc,
1351 New_Reference_To (RTE (RE_Current_Master), Loc)));
1353 if Present (Declarations (N)) then
1354 Prepend (Decl, Declarations (N));
1355 else
1356 Set_Declarations (N, New_List (Decl));
1357 end if;
1359 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1360 Set_Has_Master_Entity (Scop);
1361 end if;
1362 end Check_Anonymous_Return;
1364 -------------------------
1365 -- Check_Inline_Pragma --
1366 -------------------------
1368 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1369 Prag : Node_Id;
1370 Plist : List_Id;
1372 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1373 -- True when N is a pragma Inline or Inline_Awlays that applies
1374 -- to this subprogram.
1376 -----------------------
1377 -- Is_Inline_Pragma --
1378 -----------------------
1380 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1381 begin
1382 return
1383 Nkind (N) = N_Pragma
1384 and then
1385 (Pragma_Name (N) = Name_Inline_Always
1386 or else
1387 (Front_End_Inlining
1388 and then Pragma_Name (N) = Name_Inline))
1389 and then
1390 Chars
1391 (Expression (First (Pragma_Argument_Associations (N))))
1392 = Chars (Body_Id);
1393 end Is_Inline_Pragma;
1395 -- Start of processing for Check_Inline_Pragma
1397 begin
1398 if not Expander_Active then
1399 return;
1400 end if;
1402 if Is_List_Member (N)
1403 and then Present (Next (N))
1404 and then Is_Inline_Pragma (Next (N))
1405 then
1406 Prag := Next (N);
1408 elsif Nkind (N) /= N_Subprogram_Body_Stub
1409 and then Present (Declarations (N))
1410 and then Is_Inline_Pragma (First (Declarations (N)))
1411 then
1412 Prag := First (Declarations (N));
1414 else
1415 Prag := Empty;
1416 end if;
1418 if Present (Prag) then
1419 if Present (Spec_Id) then
1420 if List_Containing (N) =
1421 List_Containing (Unit_Declaration_Node (Spec_Id))
1422 then
1423 Analyze (Prag);
1424 end if;
1426 else
1427 -- Create a subprogram declaration, to make treatment uniform
1429 declare
1430 Subp : constant Entity_Id :=
1431 Make_Defining_Identifier (Loc, Chars (Body_Id));
1432 Decl : constant Node_Id :=
1433 Make_Subprogram_Declaration (Loc,
1434 Specification => New_Copy_Tree (Specification (N)));
1435 begin
1436 Set_Defining_Unit_Name (Specification (Decl), Subp);
1438 if Present (First_Formal (Body_Id)) then
1439 Plist := Copy_Parameter_List (Body_Id);
1440 Set_Parameter_Specifications
1441 (Specification (Decl), Plist);
1442 end if;
1444 Insert_Before (N, Decl);
1445 Analyze (Decl);
1446 Analyze (Prag);
1447 Set_Has_Pragma_Inline (Subp);
1449 if Pragma_Name (Prag) = Name_Inline_Always then
1450 Set_Is_Inlined (Subp);
1451 Set_Has_Pragma_Inline_Always (Subp);
1452 end if;
1454 Spec := Subp;
1455 end;
1456 end if;
1457 end if;
1458 end Check_Inline_Pragma;
1460 ----------------------------
1461 -- Set_Trivial_Subprogram --
1462 ----------------------------
1464 procedure Set_Trivial_Subprogram (N : Node_Id) is
1465 Nxt : constant Node_Id := Next (N);
1467 begin
1468 Set_Is_Trivial_Subprogram (Body_Id);
1470 if Present (Spec_Id) then
1471 Set_Is_Trivial_Subprogram (Spec_Id);
1472 end if;
1474 if Present (Nxt)
1475 and then Nkind (Nxt) = N_Simple_Return_Statement
1476 and then No (Next (Nxt))
1477 and then Present (Expression (Nxt))
1478 and then Is_Entity_Name (Expression (Nxt))
1479 then
1480 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
1481 end if;
1482 end Set_Trivial_Subprogram;
1484 ---------------------------------
1485 -- Verify_Overriding_Indicator --
1486 ---------------------------------
1488 procedure Verify_Overriding_Indicator is
1489 begin
1490 if Must_Override (Body_Spec) then
1491 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
1492 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1493 then
1494 null;
1496 elsif not Is_Overriding_Operation (Spec_Id) then
1497 Error_Msg_NE
1498 ("subprogram& is not overriding", Body_Spec, Spec_Id);
1499 end if;
1501 elsif Must_Not_Override (Body_Spec) then
1502 if Is_Overriding_Operation (Spec_Id) then
1503 Error_Msg_NE
1504 ("subprogram& overrides inherited operation",
1505 Body_Spec, Spec_Id);
1507 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
1508 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1509 then
1510 Error_Msg_NE
1511 ("subprogram & overrides predefined operator ",
1512 Body_Spec, Spec_Id);
1514 -- If this is not a primitive operation the overriding indicator
1515 -- is altogether illegal.
1517 elsif not Is_Primitive (Spec_Id) then
1518 Error_Msg_N ("overriding indicator only allowed " &
1519 "if subprogram is primitive",
1520 Body_Spec);
1521 end if;
1522 end if;
1523 end Verify_Overriding_Indicator;
1525 -- Start of processing for Analyze_Subprogram_Body
1527 begin
1528 if Debug_Flag_C then
1529 Write_Str ("==== Compiling subprogram body ");
1530 Write_Name (Chars (Body_Id));
1531 Write_Str (" from ");
1532 Write_Location (Loc);
1533 Write_Eol;
1534 end if;
1536 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1538 -- Generic subprograms are handled separately. They always have a
1539 -- generic specification. Determine whether current scope has a
1540 -- previous declaration.
1542 -- If the subprogram body is defined within an instance of the same
1543 -- name, the instance appears as a package renaming, and will be hidden
1544 -- within the subprogram.
1546 if Present (Prev_Id)
1547 and then not Is_Overloadable (Prev_Id)
1548 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
1549 or else Comes_From_Source (Prev_Id))
1550 then
1551 if Is_Generic_Subprogram (Prev_Id) then
1552 Spec_Id := Prev_Id;
1553 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1554 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1556 Analyze_Generic_Subprogram_Body (N, Spec_Id);
1557 return;
1559 else
1560 -- Previous entity conflicts with subprogram name. Attempting to
1561 -- enter name will post error.
1563 Enter_Name (Body_Id);
1564 return;
1565 end if;
1567 -- Non-generic case, find the subprogram declaration, if one was seen,
1568 -- or enter new overloaded entity in the current scope. If the
1569 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
1570 -- part of the context of one of its subunits. No need to redo the
1571 -- analysis.
1573 elsif Prev_Id = Body_Id
1574 and then Has_Completion (Body_Id)
1575 then
1576 return;
1578 else
1579 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
1581 if Nkind (N) = N_Subprogram_Body_Stub
1582 or else No (Corresponding_Spec (N))
1583 then
1584 Spec_Id := Find_Corresponding_Spec (N);
1586 -- If this is a duplicate body, no point in analyzing it
1588 if Error_Posted (N) then
1589 return;
1590 end if;
1592 -- A subprogram body should cause freezing of its own declaration,
1593 -- but if there was no previous explicit declaration, then the
1594 -- subprogram will get frozen too late (there may be code within
1595 -- the body that depends on the subprogram having been frozen,
1596 -- such as uses of extra formals), so we force it to be frozen
1597 -- here. Same holds if the body and spec are compilation units.
1599 if No (Spec_Id) then
1600 Freeze_Before (N, Body_Id);
1602 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1603 Freeze_Before (N, Spec_Id);
1604 end if;
1606 else
1607 Spec_Id := Corresponding_Spec (N);
1608 end if;
1609 end if;
1611 -- Do not inline any subprogram that contains nested subprograms, since
1612 -- the backend inlining circuit seems to generate uninitialized
1613 -- references in this case. We know this happens in the case of front
1614 -- end ZCX support, but it also appears it can happen in other cases as
1615 -- well. The backend often rejects attempts to inline in the case of
1616 -- nested procedures anyway, so little if anything is lost by this.
1617 -- Note that this is test is for the benefit of the back-end. There is
1618 -- a separate test for front-end inlining that also rejects nested
1619 -- subprograms.
1621 -- Do not do this test if errors have been detected, because in some
1622 -- error cases, this code blows up, and we don't need it anyway if
1623 -- there have been errors, since we won't get to the linker anyway.
1625 if Comes_From_Source (Body_Id)
1626 and then Serious_Errors_Detected = 0
1627 then
1628 P_Ent := Body_Id;
1629 loop
1630 P_Ent := Scope (P_Ent);
1631 exit when No (P_Ent) or else P_Ent = Standard_Standard;
1633 if Is_Subprogram (P_Ent) then
1634 Set_Is_Inlined (P_Ent, False);
1636 if Comes_From_Source (P_Ent)
1637 and then Has_Pragma_Inline (P_Ent)
1638 then
1639 Cannot_Inline
1640 ("cannot inline& (nested subprogram)?",
1641 N, P_Ent);
1642 end if;
1643 end if;
1644 end loop;
1645 end if;
1647 Check_Inline_Pragma (Spec_Id);
1649 -- Case of fully private operation in the body of the protected type.
1650 -- We must create a declaration for the subprogram, in order to attach
1651 -- the protected subprogram that will be used in internal calls.
1653 if No (Spec_Id)
1654 and then Comes_From_Source (N)
1655 and then Is_Protected_Type (Current_Scope)
1656 then
1657 declare
1658 Decl : Node_Id;
1659 Plist : List_Id;
1660 Formal : Entity_Id;
1661 New_Spec : Node_Id;
1663 begin
1664 Formal := First_Formal (Body_Id);
1666 -- The protected operation always has at least one formal, namely
1667 -- the object itself, but it is only placed in the parameter list
1668 -- if expansion is enabled.
1670 if Present (Formal)
1671 or else Expander_Active
1672 then
1673 Plist := Copy_Parameter_List (Body_Id);
1674 else
1675 Plist := No_List;
1676 end if;
1678 if Nkind (Body_Spec) = N_Procedure_Specification then
1679 New_Spec :=
1680 Make_Procedure_Specification (Loc,
1681 Defining_Unit_Name =>
1682 Make_Defining_Identifier (Sloc (Body_Id),
1683 Chars => Chars (Body_Id)),
1684 Parameter_Specifications => Plist);
1685 else
1686 New_Spec :=
1687 Make_Function_Specification (Loc,
1688 Defining_Unit_Name =>
1689 Make_Defining_Identifier (Sloc (Body_Id),
1690 Chars => Chars (Body_Id)),
1691 Parameter_Specifications => Plist,
1692 Result_Definition =>
1693 New_Occurrence_Of (Etype (Body_Id), Loc));
1694 end if;
1696 Decl :=
1697 Make_Subprogram_Declaration (Loc,
1698 Specification => New_Spec);
1699 Insert_Before (N, Decl);
1700 Spec_Id := Defining_Unit_Name (New_Spec);
1702 -- Indicate that the entity comes from source, to ensure that
1703 -- cross-reference information is properly generated. The body
1704 -- itself is rewritten during expansion, and the body entity will
1705 -- not appear in calls to the operation.
1707 Set_Comes_From_Source (Spec_Id, True);
1708 Analyze (Decl);
1709 Set_Has_Completion (Spec_Id);
1710 Set_Convention (Spec_Id, Convention_Protected);
1711 end;
1713 elsif Present (Spec_Id) then
1714 Spec_Decl := Unit_Declaration_Node (Spec_Id);
1715 Verify_Overriding_Indicator;
1717 -- In general, the spec will be frozen when we start analyzing the
1718 -- body. However, for internally generated operations, such as
1719 -- wrapper functions for inherited operations with controlling
1720 -- results, the spec may not have been frozen by the time we
1721 -- expand the freeze actions that include the bodies. In particular,
1722 -- extra formals for accessibility or for return-in-place may need
1723 -- to be generated. Freeze nodes, if any, are inserted before the
1724 -- current body.
1726 if not Is_Frozen (Spec_Id)
1727 and then Expander_Active
1728 then
1729 -- Force the generation of its freezing node to ensure proper
1730 -- management of access types in the backend.
1732 -- This is definitely needed for some cases, but it is not clear
1733 -- why, to be investigated further???
1735 Set_Has_Delayed_Freeze (Spec_Id);
1736 Insert_Actions (N, Freeze_Entity (Spec_Id, Loc));
1737 end if;
1738 end if;
1740 -- Place subprogram on scope stack, and make formals visible. If there
1741 -- is a spec, the visible entity remains that of the spec.
1743 if Present (Spec_Id) then
1744 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1746 if Is_Child_Unit (Spec_Id) then
1747 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
1748 end if;
1750 if Style_Check then
1751 Style.Check_Identifier (Body_Id, Spec_Id);
1752 end if;
1754 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1755 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1757 if Is_Abstract_Subprogram (Spec_Id) then
1758 Error_Msg_N ("an abstract subprogram cannot have a body", N);
1759 return;
1761 else
1762 Set_Convention (Body_Id, Convention (Spec_Id));
1763 Set_Has_Completion (Spec_Id);
1765 if Is_Protected_Type (Scope (Spec_Id)) then
1766 Prot_Typ := Scope (Spec_Id);
1767 end if;
1769 -- If this is a body generated for a renaming, do not check for
1770 -- full conformance. The check is redundant, because the spec of
1771 -- the body is a copy of the spec in the renaming declaration,
1772 -- and the test can lead to spurious errors on nested defaults.
1774 if Present (Spec_Decl)
1775 and then not Comes_From_Source (N)
1776 and then
1777 (Nkind (Original_Node (Spec_Decl)) =
1778 N_Subprogram_Renaming_Declaration
1779 or else (Present (Corresponding_Body (Spec_Decl))
1780 and then
1781 Nkind (Unit_Declaration_Node
1782 (Corresponding_Body (Spec_Decl))) =
1783 N_Subprogram_Renaming_Declaration))
1784 then
1785 Conformant := True;
1787 else
1788 Check_Conformance
1789 (Body_Id, Spec_Id,
1790 Fully_Conformant, True, Conformant, Body_Id);
1791 end if;
1793 -- If the body is not fully conformant, we have to decide if we
1794 -- should analyze it or not. If it has a really messed up profile
1795 -- then we probably should not analyze it, since we will get too
1796 -- many bogus messages.
1798 -- Our decision is to go ahead in the non-fully conformant case
1799 -- only if it is at least mode conformant with the spec. Note
1800 -- that the call to Check_Fully_Conformant has issued the proper
1801 -- error messages to complain about the lack of conformance.
1803 if not Conformant
1804 and then not Mode_Conformant (Body_Id, Spec_Id)
1805 then
1806 return;
1807 end if;
1808 end if;
1810 if Spec_Id /= Body_Id then
1811 Reference_Body_Formals (Spec_Id, Body_Id);
1812 end if;
1814 if Nkind (N) /= N_Subprogram_Body_Stub then
1815 Set_Corresponding_Spec (N, Spec_Id);
1817 -- Ada 2005 (AI-345): If the operation is a primitive operation
1818 -- of a concurrent type, the type of the first parameter has been
1819 -- replaced with the corresponding record, which is the proper
1820 -- run-time structure to use. However, within the body there may
1821 -- be uses of the formals that depend on primitive operations
1822 -- of the type (in particular calls in prefixed form) for which
1823 -- we need the original concurrent type. The operation may have
1824 -- several controlling formals, so the replacement must be done
1825 -- for all of them.
1827 if Comes_From_Source (Spec_Id)
1828 and then Present (First_Entity (Spec_Id))
1829 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
1830 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
1831 and then
1832 Present (Interfaces (Etype (First_Entity (Spec_Id))))
1833 and then
1834 Present
1835 (Corresponding_Concurrent_Type
1836 (Etype (First_Entity (Spec_Id))))
1837 then
1838 declare
1839 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
1840 Form : Entity_Id;
1842 begin
1843 Form := First_Formal (Spec_Id);
1844 while Present (Form) loop
1845 if Etype (Form) = Typ then
1846 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
1847 end if;
1849 Next_Formal (Form);
1850 end loop;
1851 end;
1852 end if;
1854 -- Make the formals visible, and place subprogram on scope stack.
1855 -- This is also the point at which we set Last_Real_Spec_Entity
1856 -- to mark the entities which will not be moved to the body.
1858 Install_Formals (Spec_Id);
1859 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
1860 Push_Scope (Spec_Id);
1862 -- Make sure that the subprogram is immediately visible. For
1863 -- child units that have no separate spec this is indispensable.
1864 -- Otherwise it is safe albeit redundant.
1866 Set_Is_Immediately_Visible (Spec_Id);
1867 end if;
1869 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
1870 Set_Ekind (Body_Id, E_Subprogram_Body);
1871 Set_Scope (Body_Id, Scope (Spec_Id));
1872 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
1874 -- Case of subprogram body with no previous spec
1876 else
1877 if Style_Check
1878 and then Comes_From_Source (Body_Id)
1879 and then not Suppress_Style_Checks (Body_Id)
1880 and then not In_Instance
1881 then
1882 Style.Body_With_No_Spec (N);
1883 end if;
1885 New_Overloaded_Entity (Body_Id);
1887 if Nkind (N) /= N_Subprogram_Body_Stub then
1888 Set_Acts_As_Spec (N);
1889 Generate_Definition (Body_Id);
1890 Generate_Reference
1891 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
1892 Generate_Reference_To_Formals (Body_Id);
1893 Install_Formals (Body_Id);
1894 Push_Scope (Body_Id);
1895 end if;
1896 end if;
1898 -- If the return type is an anonymous access type whose designated type
1899 -- is the limited view of a class-wide type and the non-limited view is
1900 -- available, update the return type accordingly.
1902 if Ada_Version >= Ada_05
1903 and then Comes_From_Source (N)
1904 then
1905 declare
1906 Etyp : Entity_Id;
1907 Rtyp : Entity_Id;
1909 begin
1910 Rtyp := Etype (Current_Scope);
1912 if Ekind (Rtyp) = E_Anonymous_Access_Type then
1913 Etyp := Directly_Designated_Type (Rtyp);
1915 if Is_Class_Wide_Type (Etyp)
1916 and then From_With_Type (Etyp)
1917 then
1918 Set_Directly_Designated_Type
1919 (Etype (Current_Scope), Available_View (Etyp));
1920 end if;
1921 end if;
1922 end;
1923 end if;
1925 -- If this is the proper body of a stub, we must verify that the stub
1926 -- conforms to the body, and to the previous spec if one was present.
1927 -- we know already that the body conforms to that spec. This test is
1928 -- only required for subprograms that come from source.
1930 if Nkind (Parent (N)) = N_Subunit
1931 and then Comes_From_Source (N)
1932 and then not Error_Posted (Body_Id)
1933 and then Nkind (Corresponding_Stub (Parent (N))) =
1934 N_Subprogram_Body_Stub
1935 then
1936 declare
1937 Old_Id : constant Entity_Id :=
1938 Defining_Entity
1939 (Specification (Corresponding_Stub (Parent (N))));
1941 Conformant : Boolean := False;
1943 begin
1944 if No (Spec_Id) then
1945 Check_Fully_Conformant (Body_Id, Old_Id);
1947 else
1948 Check_Conformance
1949 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
1951 if not Conformant then
1953 -- The stub was taken to be a new declaration. Indicate
1954 -- that it lacks a body.
1956 Set_Has_Completion (Old_Id, False);
1957 end if;
1958 end if;
1959 end;
1960 end if;
1962 Set_Has_Completion (Body_Id);
1963 Check_Eliminated (Body_Id);
1965 if Nkind (N) = N_Subprogram_Body_Stub then
1966 return;
1968 elsif Present (Spec_Id)
1969 and then Expander_Active
1970 and then
1971 (Has_Pragma_Inline_Always (Spec_Id)
1972 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
1973 then
1974 Build_Body_To_Inline (N, Spec_Id);
1975 end if;
1977 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
1978 -- if its specification we have to install the private withed units.
1979 -- This holds for child units as well.
1981 if Is_Compilation_Unit (Body_Id)
1982 or else Nkind (Parent (N)) = N_Compilation_Unit
1983 then
1984 Install_Private_With_Clauses (Body_Id);
1985 end if;
1987 Check_Anonymous_Return;
1989 -- Set the Protected_Formal field of each extra formal of the protected
1990 -- subprogram to reference the corresponding extra formal of the
1991 -- subprogram that implements it. For regular formals this occurs when
1992 -- the protected subprogram's declaration is expanded, but the extra
1993 -- formals don't get created until the subprogram is frozen. We need to
1994 -- do this before analyzing the protected subprogram's body so that any
1995 -- references to the original subprogram's extra formals will be changed
1996 -- refer to the implementing subprogram's formals (see Expand_Formal).
1998 if Present (Spec_Id)
1999 and then Is_Protected_Type (Scope (Spec_Id))
2000 and then Present (Protected_Body_Subprogram (Spec_Id))
2001 then
2002 declare
2003 Impl_Subp : constant Entity_Id :=
2004 Protected_Body_Subprogram (Spec_Id);
2005 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2006 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2007 begin
2008 while Present (Prot_Ext_Formal) loop
2009 pragma Assert (Present (Impl_Ext_Formal));
2010 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2011 Next_Formal_With_Extras (Prot_Ext_Formal);
2012 Next_Formal_With_Extras (Impl_Ext_Formal);
2013 end loop;
2014 end;
2015 end if;
2017 -- Now we can go on to analyze the body
2019 HSS := Handled_Statement_Sequence (N);
2020 Set_Actual_Subtypes (N, Current_Scope);
2022 -- Deal with preconditions and postconditions
2024 Process_PPCs (N, Spec_Id, Body_Id);
2026 -- Add a declaration for the Protection object, renaming declarations
2027 -- for discriminals and privals and finally a declaration for the entry
2028 -- family index (if applicable). This form of early expansion is done
2029 -- when the Expander is active because Install_Private_Data_Declarations
2030 -- references entities which were created during regular expansion.
2032 if Expander_Active
2033 and then Comes_From_Source (N)
2034 and then Present (Prot_Typ)
2035 and then Present (Spec_Id)
2036 and then not Is_Eliminated (Spec_Id)
2037 then
2038 Install_Private_Data_Declarations
2039 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2040 end if;
2042 -- Analyze the declarations (this call will analyze the precondition
2043 -- Check pragmas we prepended to the list, as well as the declaration
2044 -- of the _Postconditions procedure).
2046 Analyze_Declarations (Declarations (N));
2048 -- Check completion, and analyze the statements
2050 Check_Completion;
2051 Inspect_Deferred_Constant_Completion (Declarations (N));
2052 Analyze (HSS);
2054 -- Deal with end of scope processing for the body
2056 Process_End_Label (HSS, 't', Current_Scope);
2057 End_Scope;
2058 Check_Subprogram_Order (N);
2059 Set_Analyzed (Body_Id);
2061 -- If we have a separate spec, then the analysis of the declarations
2062 -- caused the entities in the body to be chained to the spec id, but
2063 -- we want them chained to the body id. Only the formal parameters
2064 -- end up chained to the spec id in this case.
2066 if Present (Spec_Id) then
2068 -- We must conform to the categorization of our spec
2070 Validate_Categorization_Dependency (N, Spec_Id);
2072 -- And if this is a child unit, the parent units must conform
2074 if Is_Child_Unit (Spec_Id) then
2075 Validate_Categorization_Dependency
2076 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2077 end if;
2079 -- Here is where we move entities from the spec to the body
2081 -- Case where there are entities that stay with the spec
2083 if Present (Last_Real_Spec_Entity) then
2085 -- No body entities (happens when the only real spec entities
2086 -- come from precondition and postcondition pragmas)
2088 if No (Last_Entity (Body_Id)) then
2089 Set_First_Entity
2090 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2092 -- Body entities present (formals), so chain stuff past them
2094 else
2095 Set_Next_Entity
2096 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2097 end if;
2099 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2100 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2101 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2103 -- Case where there are no spec entities, in this case there can
2104 -- be no body entities either, so just move everything.
2106 else
2107 pragma Assert (No (Last_Entity (Body_Id)));
2108 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2109 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2110 Set_First_Entity (Spec_Id, Empty);
2111 Set_Last_Entity (Spec_Id, Empty);
2112 end if;
2113 end if;
2115 -- If function, check return statements
2117 if Nkind (Body_Spec) = N_Function_Specification then
2118 declare
2119 Id : Entity_Id;
2121 begin
2122 if Present (Spec_Id) then
2123 Id := Spec_Id;
2124 else
2125 Id := Body_Id;
2126 end if;
2128 if Return_Present (Id) then
2129 Check_Returns (HSS, 'F', Missing_Ret);
2131 if Missing_Ret then
2132 Set_Has_Missing_Return (Id);
2133 end if;
2135 elsif not Is_Machine_Code_Subprogram (Id)
2136 and then not Body_Deleted
2137 then
2138 Error_Msg_N ("missing RETURN statement in function body", N);
2139 end if;
2140 end;
2142 -- If procedure with No_Return, check returns
2144 elsif Nkind (Body_Spec) = N_Procedure_Specification
2145 and then Present (Spec_Id)
2146 and then No_Return (Spec_Id)
2147 then
2148 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2149 end if;
2151 -- Now we are going to check for variables that are never modified in
2152 -- the body of the procedure. But first we deal with a special case
2153 -- where we want to modify this check. If the body of the subprogram
2154 -- starts with a raise statement or its equivalent, or if the body
2155 -- consists entirely of a null statement, then it is pretty obvious
2156 -- that it is OK to not reference the parameters. For example, this
2157 -- might be the following common idiom for a stubbed function:
2158 -- statement of the procedure raises an exception. In particular this
2159 -- deals with the common idiom of a stubbed function, which might
2160 -- appear as something like
2162 -- function F (A : Integer) return Some_Type;
2163 -- X : Some_Type;
2164 -- begin
2165 -- raise Program_Error;
2166 -- return X;
2167 -- end F;
2169 -- Here the purpose of X is simply to satisfy the annoying requirement
2170 -- in Ada that there be at least one return, and we certainly do not
2171 -- want to go posting warnings on X that it is not initialized! On
2172 -- the other hand, if X is entirely unreferenced that should still
2173 -- get a warning.
2175 -- What we do is to detect these cases, and if we find them, flag the
2176 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2177 -- suppress unwanted warnings. For the case of the function stub above
2178 -- we have a special test to set X as apparently assigned to suppress
2179 -- the warning.
2181 declare
2182 Stm : Node_Id;
2184 begin
2185 -- Skip initial labels (for one thing this occurs when we are in
2186 -- front end ZCX mode, but in any case it is irrelevant), and also
2187 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2189 Stm := First (Statements (HSS));
2190 while Nkind (Stm) = N_Label
2191 or else Nkind (Stm) in N_Push_xxx_Label
2192 loop
2193 Next (Stm);
2194 end loop;
2196 -- Do the test on the original statement before expansion
2198 declare
2199 Ostm : constant Node_Id := Original_Node (Stm);
2201 begin
2202 -- If explicit raise statement, turn on flag
2204 if Nkind (Ostm) = N_Raise_Statement then
2205 Set_Trivial_Subprogram (Stm);
2207 -- If null statement, and no following statements, turn on flag
2209 elsif Nkind (Stm) = N_Null_Statement
2210 and then Comes_From_Source (Stm)
2211 and then No (Next (Stm))
2212 then
2213 Set_Trivial_Subprogram (Stm);
2215 -- Check for explicit call cases which likely raise an exception
2217 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
2218 if Is_Entity_Name (Name (Ostm)) then
2219 declare
2220 Ent : constant Entity_Id := Entity (Name (Ostm));
2222 begin
2223 -- If the procedure is marked No_Return, then likely it
2224 -- raises an exception, but in any case it is not coming
2225 -- back here, so turn on the flag.
2227 if Ekind (Ent) = E_Procedure
2228 and then No_Return (Ent)
2229 then
2230 Set_Trivial_Subprogram (Stm);
2232 -- If the procedure name is Raise_Exception, then also
2233 -- assume that it raises an exception. The main target
2234 -- here is Ada.Exceptions.Raise_Exception, but this name
2235 -- is pretty evocative in any context! Note that the
2236 -- procedure in Ada.Exceptions is not marked No_Return
2237 -- because of the annoying case of the null exception Id
2238 -- when operating in Ada 95 mode.
2240 elsif Chars (Ent) = Name_Raise_Exception then
2241 Set_Trivial_Subprogram (Stm);
2242 end if;
2243 end;
2244 end if;
2245 end if;
2246 end;
2247 end;
2249 -- Check for variables that are never modified
2251 declare
2252 E1, E2 : Entity_Id;
2254 begin
2255 -- If there is a separate spec, then transfer Never_Set_In_Source
2256 -- flags from out parameters to the corresponding entities in the
2257 -- body. The reason we do that is we want to post error flags on
2258 -- the body entities, not the spec entities.
2260 if Present (Spec_Id) then
2261 E1 := First_Entity (Spec_Id);
2262 while Present (E1) loop
2263 if Ekind (E1) = E_Out_Parameter then
2264 E2 := First_Entity (Body_Id);
2265 while Present (E2) loop
2266 exit when Chars (E1) = Chars (E2);
2267 Next_Entity (E2);
2268 end loop;
2270 if Present (E2) then
2271 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
2272 end if;
2273 end if;
2275 Next_Entity (E1);
2276 end loop;
2277 end if;
2279 -- Check references in body unless it was deleted. Note that the
2280 -- check of Body_Deleted here is not just for efficiency, it is
2281 -- necessary to avoid junk warnings on formal parameters.
2283 if not Body_Deleted then
2284 Check_References (Body_Id);
2285 end if;
2286 end;
2287 end Analyze_Subprogram_Body;
2289 ------------------------------------
2290 -- Analyze_Subprogram_Declaration --
2291 ------------------------------------
2293 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
2294 Designator : constant Entity_Id :=
2295 Analyze_Subprogram_Specification (Specification (N));
2296 Scop : constant Entity_Id := Current_Scope;
2298 -- Start of processing for Analyze_Subprogram_Declaration
2300 begin
2301 Generate_Definition (Designator);
2303 -- Check for RCI unit subprogram declarations for illegal inlined
2304 -- subprograms and subprograms having access parameter or limited
2305 -- parameter without Read and Write attributes (RM E.2.3(12-13)).
2307 Validate_RCI_Subprogram_Declaration (N);
2309 Trace_Scope
2311 Defining_Entity (N),
2312 " Analyze subprogram spec: ");
2314 if Debug_Flag_C then
2315 Write_Str ("==== Compiling subprogram spec ");
2316 Write_Name (Chars (Designator));
2317 Write_Str (" from ");
2318 Write_Location (Sloc (N));
2319 Write_Eol;
2320 end if;
2322 New_Overloaded_Entity (Designator);
2323 Check_Delayed_Subprogram (Designator);
2325 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
2326 -- or null.
2328 if Ada_Version >= Ada_05
2329 and then Comes_From_Source (N)
2330 and then Is_Dispatching_Operation (Designator)
2331 then
2332 declare
2333 E : Entity_Id;
2334 Etyp : Entity_Id;
2336 begin
2337 if Has_Controlling_Result (Designator) then
2338 Etyp := Etype (Designator);
2340 else
2341 E := First_Entity (Designator);
2342 while Present (E)
2343 and then Is_Formal (E)
2344 and then not Is_Controlling_Formal (E)
2345 loop
2346 Next_Entity (E);
2347 end loop;
2349 Etyp := Etype (E);
2350 end if;
2352 if Is_Access_Type (Etyp) then
2353 Etyp := Directly_Designated_Type (Etyp);
2354 end if;
2356 if Is_Interface (Etyp)
2357 and then not Is_Abstract_Subprogram (Designator)
2358 and then not (Ekind (Designator) = E_Procedure
2359 and then Null_Present (Specification (N)))
2360 then
2361 Error_Msg_Name_1 := Chars (Defining_Entity (N));
2362 Error_Msg_N
2363 ("(Ada 2005) interface subprogram % must be abstract or null",
2365 end if;
2366 end;
2367 end if;
2369 -- What is the following code for, it used to be
2371 -- ??? Set_Suppress_Elaboration_Checks
2372 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
2374 -- The following seems equivalent, but a bit dubious
2376 if Elaboration_Checks_Suppressed (Designator) then
2377 Set_Kill_Elaboration_Checks (Designator);
2378 end if;
2380 if Scop /= Standard_Standard
2381 and then not Is_Child_Unit (Designator)
2382 then
2383 Set_Categorization_From_Scope (Designator, Scop);
2384 else
2385 -- For a compilation unit, check for library-unit pragmas
2387 Push_Scope (Designator);
2388 Set_Categorization_From_Pragmas (N);
2389 Validate_Categorization_Dependency (N, Designator);
2390 Pop_Scope;
2391 end if;
2393 -- For a compilation unit, set body required. This flag will only be
2394 -- reset if a valid Import or Interface pragma is processed later on.
2396 if Nkind (Parent (N)) = N_Compilation_Unit then
2397 Set_Body_Required (Parent (N), True);
2399 if Ada_Version >= Ada_05
2400 and then Nkind (Specification (N)) = N_Procedure_Specification
2401 and then Null_Present (Specification (N))
2402 then
2403 Error_Msg_N
2404 ("null procedure cannot be declared at library level", N);
2405 end if;
2406 end if;
2408 Generate_Reference_To_Formals (Designator);
2409 Check_Eliminated (Designator);
2411 -- Ada 2005: if procedure is declared with "is null" qualifier,
2412 -- it requires no body.
2414 if Nkind (Specification (N)) = N_Procedure_Specification
2415 and then Null_Present (Specification (N))
2416 then
2417 Set_Has_Completion (Designator);
2418 Set_Is_Inlined (Designator);
2420 if Is_Protected_Type (Current_Scope) then
2421 Error_Msg_N
2422 ("protected operation cannot be a null procedure", N);
2423 end if;
2424 end if;
2425 end Analyze_Subprogram_Declaration;
2427 --------------------------------------
2428 -- Analyze_Subprogram_Specification --
2429 --------------------------------------
2431 -- Reminder: N here really is a subprogram specification (not a subprogram
2432 -- declaration). This procedure is called to analyze the specification in
2433 -- both subprogram bodies and subprogram declarations (specs).
2435 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
2436 Designator : constant Entity_Id := Defining_Entity (N);
2437 Formals : constant List_Id := Parameter_Specifications (N);
2438 Formal : Entity_Id;
2439 Formal_Typ : Entity_Id;
2441 -- Start of processing for Analyze_Subprogram_Specification
2443 begin
2444 Generate_Definition (Designator);
2446 if Nkind (N) = N_Function_Specification then
2447 Set_Ekind (Designator, E_Function);
2448 Set_Mechanism (Designator, Default_Mechanism);
2450 else
2451 Set_Ekind (Designator, E_Procedure);
2452 Set_Etype (Designator, Standard_Void_Type);
2453 end if;
2455 -- Introduce new scope for analysis of the formals and the return type
2457 Set_Scope (Designator, Current_Scope);
2459 if Present (Formals) then
2460 Push_Scope (Designator);
2461 Process_Formals (Formals, N);
2463 -- Ada 2005 (AI-345): If this is an overriding operation of an
2464 -- inherited interface operation, and the controlling type is
2465 -- a synchronized type, replace the type with its corresponding
2466 -- record, to match the proper signature of an overriding operation.
2468 if Ada_Version >= Ada_05 then
2469 Formal := First_Formal (Designator);
2470 while Present (Formal) loop
2471 Formal_Typ := Etype (Formal);
2473 if Is_Concurrent_Type (Formal_Typ)
2474 and then Present (Corresponding_Record_Type (Formal_Typ))
2475 and then Present (Interfaces
2476 (Corresponding_Record_Type (Formal_Typ)))
2477 then
2478 Set_Etype (Formal,
2479 Corresponding_Record_Type (Formal_Typ));
2480 end if;
2482 Formal := Next_Formal (Formal);
2483 end loop;
2484 end if;
2486 End_Scope;
2488 elsif Nkind (N) = N_Function_Specification then
2489 Analyze_Return_Type (N);
2490 end if;
2492 if Nkind (N) = N_Function_Specification then
2493 if Nkind (Designator) = N_Defining_Operator_Symbol then
2494 Valid_Operator_Definition (Designator);
2495 end if;
2497 May_Need_Actuals (Designator);
2499 -- Ada 2005 (AI-251): If the return type is abstract, verify that
2500 -- the subprogram is abstract also. This does not apply to renaming
2501 -- declarations, where abstractness is inherited.
2502 -- In case of primitives associated with abstract interface types
2503 -- the check is applied later (see Analyze_Subprogram_Declaration).
2505 if Is_Abstract_Type (Etype (Designator))
2506 and then not Is_Interface (Etype (Designator))
2507 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2508 and then Nkind (Parent (N)) /=
2509 N_Abstract_Subprogram_Declaration
2510 and then
2511 (Nkind (Parent (N))) /= N_Formal_Abstract_Subprogram_Declaration
2512 then
2513 Error_Msg_N
2514 ("function that returns abstract type must be abstract", N);
2515 end if;
2516 end if;
2518 return Designator;
2519 end Analyze_Subprogram_Specification;
2521 --------------------------
2522 -- Build_Body_To_Inline --
2523 --------------------------
2525 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
2526 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
2527 Original_Body : Node_Id;
2528 Body_To_Analyze : Node_Id;
2529 Max_Size : constant := 10;
2530 Stat_Count : Integer := 0;
2532 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
2533 -- Check for declarations that make inlining not worthwhile
2535 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
2536 -- Check for statements that make inlining not worthwhile: any tasking
2537 -- statement, nested at any level. Keep track of total number of
2538 -- elementary statements, as a measure of acceptable size.
2540 function Has_Pending_Instantiation return Boolean;
2541 -- If some enclosing body contains instantiations that appear before the
2542 -- corresponding generic body, the enclosing body has a freeze node so
2543 -- that it can be elaborated after the generic itself. This might
2544 -- conflict with subsequent inlinings, so that it is unsafe to try to
2545 -- inline in such a case.
2547 function Has_Single_Return return Boolean;
2548 -- In general we cannot inline functions that return unconstrained type.
2549 -- However, we can handle such functions if all return statements return
2550 -- a local variable that is the only declaration in the body of the
2551 -- function. In that case the call can be replaced by that local
2552 -- variable as is done for other inlined calls.
2554 procedure Remove_Pragmas;
2555 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
2556 -- parameter has no meaning when the body is inlined and the formals
2557 -- are rewritten. Remove it from body to inline. The analysis of the
2558 -- non-inlined body will handle the pragma properly.
2560 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
2561 -- If the body of the subprogram includes a call that returns an
2562 -- unconstrained type, the secondary stack is involved, and it
2563 -- is not worth inlining.
2565 ------------------------------
2566 -- Has_Excluded_Declaration --
2567 ------------------------------
2569 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
2570 D : Node_Id;
2572 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
2573 -- Nested subprograms make a given body ineligible for inlining, but
2574 -- we make an exception for instantiations of unchecked conversion.
2575 -- The body has not been analyzed yet, so check the name, and verify
2576 -- that the visible entity with that name is the predefined unit.
2578 -----------------------------
2579 -- Is_Unchecked_Conversion --
2580 -----------------------------
2582 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
2583 Id : constant Node_Id := Name (D);
2584 Conv : Entity_Id;
2586 begin
2587 if Nkind (Id) = N_Identifier
2588 and then Chars (Id) = Name_Unchecked_Conversion
2589 then
2590 Conv := Current_Entity (Id);
2592 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
2593 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
2594 then
2595 Conv := Current_Entity (Selector_Name (Id));
2596 else
2597 return False;
2598 end if;
2600 return Present (Conv)
2601 and then Is_Predefined_File_Name
2602 (Unit_File_Name (Get_Source_Unit (Conv)))
2603 and then Is_Intrinsic_Subprogram (Conv);
2604 end Is_Unchecked_Conversion;
2606 -- Start of processing for Has_Excluded_Declaration
2608 begin
2609 D := First (Decls);
2610 while Present (D) loop
2611 if (Nkind (D) = N_Function_Instantiation
2612 and then not Is_Unchecked_Conversion (D))
2613 or else Nkind_In (D, N_Protected_Type_Declaration,
2614 N_Package_Declaration,
2615 N_Package_Instantiation,
2616 N_Subprogram_Body,
2617 N_Procedure_Instantiation,
2618 N_Task_Type_Declaration)
2619 then
2620 Cannot_Inline
2621 ("cannot inline & (non-allowed declaration)?", D, Subp);
2622 return True;
2623 end if;
2625 Next (D);
2626 end loop;
2628 return False;
2629 end Has_Excluded_Declaration;
2631 ----------------------------
2632 -- Has_Excluded_Statement --
2633 ----------------------------
2635 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
2636 S : Node_Id;
2637 E : Node_Id;
2639 begin
2640 S := First (Stats);
2641 while Present (S) loop
2642 Stat_Count := Stat_Count + 1;
2644 if Nkind_In (S, N_Abort_Statement,
2645 N_Asynchronous_Select,
2646 N_Conditional_Entry_Call,
2647 N_Delay_Relative_Statement,
2648 N_Delay_Until_Statement,
2649 N_Selective_Accept,
2650 N_Timed_Entry_Call)
2651 then
2652 Cannot_Inline
2653 ("cannot inline & (non-allowed statement)?", S, Subp);
2654 return True;
2656 elsif Nkind (S) = N_Block_Statement then
2657 if Present (Declarations (S))
2658 and then Has_Excluded_Declaration (Declarations (S))
2659 then
2660 return True;
2662 elsif Present (Handled_Statement_Sequence (S))
2663 and then
2664 (Present
2665 (Exception_Handlers (Handled_Statement_Sequence (S)))
2666 or else
2667 Has_Excluded_Statement
2668 (Statements (Handled_Statement_Sequence (S))))
2669 then
2670 return True;
2671 end if;
2673 elsif Nkind (S) = N_Case_Statement then
2674 E := First (Alternatives (S));
2675 while Present (E) loop
2676 if Has_Excluded_Statement (Statements (E)) then
2677 return True;
2678 end if;
2680 Next (E);
2681 end loop;
2683 elsif Nkind (S) = N_If_Statement then
2684 if Has_Excluded_Statement (Then_Statements (S)) then
2685 return True;
2686 end if;
2688 if Present (Elsif_Parts (S)) then
2689 E := First (Elsif_Parts (S));
2690 while Present (E) loop
2691 if Has_Excluded_Statement (Then_Statements (E)) then
2692 return True;
2693 end if;
2694 Next (E);
2695 end loop;
2696 end if;
2698 if Present (Else_Statements (S))
2699 and then Has_Excluded_Statement (Else_Statements (S))
2700 then
2701 return True;
2702 end if;
2704 elsif Nkind (S) = N_Loop_Statement
2705 and then Has_Excluded_Statement (Statements (S))
2706 then
2707 return True;
2708 end if;
2710 Next (S);
2711 end loop;
2713 return False;
2714 end Has_Excluded_Statement;
2716 -------------------------------
2717 -- Has_Pending_Instantiation --
2718 -------------------------------
2720 function Has_Pending_Instantiation return Boolean is
2721 S : Entity_Id;
2723 begin
2724 S := Current_Scope;
2725 while Present (S) loop
2726 if Is_Compilation_Unit (S)
2727 or else Is_Child_Unit (S)
2728 then
2729 return False;
2730 elsif Ekind (S) = E_Package
2731 and then Has_Forward_Instantiation (S)
2732 then
2733 return True;
2734 end if;
2736 S := Scope (S);
2737 end loop;
2739 return False;
2740 end Has_Pending_Instantiation;
2742 ------------------------
2743 -- Has_Single_Return --
2744 ------------------------
2746 function Has_Single_Return return Boolean is
2747 Return_Statement : Node_Id := Empty;
2749 function Check_Return (N : Node_Id) return Traverse_Result;
2751 ------------------
2752 -- Check_Return --
2753 ------------------
2755 function Check_Return (N : Node_Id) return Traverse_Result is
2756 begin
2757 if Nkind (N) = N_Simple_Return_Statement then
2758 if Present (Expression (N))
2759 and then Is_Entity_Name (Expression (N))
2760 then
2761 if No (Return_Statement) then
2762 Return_Statement := N;
2763 return OK;
2765 elsif Chars (Expression (N)) =
2766 Chars (Expression (Return_Statement))
2767 then
2768 return OK;
2770 else
2771 return Abandon;
2772 end if;
2774 else
2775 -- Expression has wrong form
2777 return Abandon;
2778 end if;
2780 else
2781 return OK;
2782 end if;
2783 end Check_Return;
2785 function Check_All_Returns is new Traverse_Func (Check_Return);
2787 -- Start of processing for Has_Single_Return
2789 begin
2790 return Check_All_Returns (N) = OK
2791 and then Present (Declarations (N))
2792 and then Present (First (Declarations (N)))
2793 and then Chars (Expression (Return_Statement)) =
2794 Chars (Defining_Identifier (First (Declarations (N))));
2795 end Has_Single_Return;
2797 --------------------
2798 -- Remove_Pragmas --
2799 --------------------
2801 procedure Remove_Pragmas is
2802 Decl : Node_Id;
2803 Nxt : Node_Id;
2805 begin
2806 Decl := First (Declarations (Body_To_Analyze));
2807 while Present (Decl) loop
2808 Nxt := Next (Decl);
2810 if Nkind (Decl) = N_Pragma
2811 and then (Pragma_Name (Decl) = Name_Unreferenced
2812 or else
2813 Pragma_Name (Decl) = Name_Unmodified)
2814 then
2815 Remove (Decl);
2816 end if;
2818 Decl := Nxt;
2819 end loop;
2820 end Remove_Pragmas;
2822 --------------------------
2823 -- Uses_Secondary_Stack --
2824 --------------------------
2826 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
2827 function Check_Call (N : Node_Id) return Traverse_Result;
2828 -- Look for function calls that return an unconstrained type
2830 ----------------
2831 -- Check_Call --
2832 ----------------
2834 function Check_Call (N : Node_Id) return Traverse_Result is
2835 begin
2836 if Nkind (N) = N_Function_Call
2837 and then Is_Entity_Name (Name (N))
2838 and then Is_Composite_Type (Etype (Entity (Name (N))))
2839 and then not Is_Constrained (Etype (Entity (Name (N))))
2840 then
2841 Cannot_Inline
2842 ("cannot inline & (call returns unconstrained type)?",
2843 N, Subp);
2844 return Abandon;
2845 else
2846 return OK;
2847 end if;
2848 end Check_Call;
2850 function Check_Calls is new Traverse_Func (Check_Call);
2852 begin
2853 return Check_Calls (Bod) = Abandon;
2854 end Uses_Secondary_Stack;
2856 -- Start of processing for Build_Body_To_Inline
2858 begin
2859 if Nkind (Decl) = N_Subprogram_Declaration
2860 and then Present (Body_To_Inline (Decl))
2861 then
2862 return; -- Done already.
2864 -- Functions that return unconstrained composite types require
2865 -- secondary stack handling, and cannot currently be inlined, unless
2866 -- all return statements return a local variable that is the first
2867 -- local declaration in the body.
2869 elsif Ekind (Subp) = E_Function
2870 and then not Is_Scalar_Type (Etype (Subp))
2871 and then not Is_Access_Type (Etype (Subp))
2872 and then not Is_Constrained (Etype (Subp))
2873 then
2874 if not Has_Single_Return then
2875 Cannot_Inline
2876 ("cannot inline & (unconstrained return type)?", N, Subp);
2877 return;
2878 end if;
2880 -- Ditto for functions that return controlled types, where controlled
2881 -- actions interfere in complex ways with inlining.
2883 elsif Ekind (Subp) = E_Function
2884 and then Controlled_Type (Etype (Subp))
2885 then
2886 Cannot_Inline
2887 ("cannot inline & (controlled return type)?", N, Subp);
2888 return;
2889 end if;
2891 if Present (Declarations (N))
2892 and then Has_Excluded_Declaration (Declarations (N))
2893 then
2894 return;
2895 end if;
2897 if Present (Handled_Statement_Sequence (N)) then
2898 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
2899 Cannot_Inline
2900 ("cannot inline& (exception handler)?",
2901 First (Exception_Handlers (Handled_Statement_Sequence (N))),
2902 Subp);
2903 return;
2904 elsif
2905 Has_Excluded_Statement
2906 (Statements (Handled_Statement_Sequence (N)))
2907 then
2908 return;
2909 end if;
2910 end if;
2912 -- We do not inline a subprogram that is too large, unless it is
2913 -- marked Inline_Always. This pragma does not suppress the other
2914 -- checks on inlining (forbidden declarations, handlers, etc).
2916 if Stat_Count > Max_Size
2917 and then not Has_Pragma_Inline_Always (Subp)
2918 then
2919 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
2920 return;
2921 end if;
2923 if Has_Pending_Instantiation then
2924 Cannot_Inline
2925 ("cannot inline& (forward instance within enclosing body)?",
2926 N, Subp);
2927 return;
2928 end if;
2930 -- Within an instance, the body to inline must be treated as a nested
2931 -- generic, so that the proper global references are preserved.
2933 -- Note that we do not do this at the library level, because it is not
2934 -- needed, and furthermore this causes trouble if front end inlining
2935 -- is activated (-gnatN).
2937 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
2938 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
2939 Original_Body := Copy_Generic_Node (N, Empty, True);
2940 else
2941 Original_Body := Copy_Separate_Tree (N);
2942 end if;
2944 -- We need to capture references to the formals in order to substitute
2945 -- the actuals at the point of inlining, i.e. instantiation. To treat
2946 -- the formals as globals to the body to inline, we nest it within
2947 -- a dummy parameterless subprogram, declared within the real one.
2948 -- To avoid generating an internal name (which is never public, and
2949 -- which affects serial numbers of other generated names), we use
2950 -- an internal symbol that cannot conflict with user declarations.
2952 Set_Parameter_Specifications (Specification (Original_Body), No_List);
2953 Set_Defining_Unit_Name
2954 (Specification (Original_Body),
2955 Make_Defining_Identifier (Sloc (N), Name_uParent));
2956 Set_Corresponding_Spec (Original_Body, Empty);
2958 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
2960 -- Set return type of function, which is also global and does not need
2961 -- to be resolved.
2963 if Ekind (Subp) = E_Function then
2964 Set_Result_Definition (Specification (Body_To_Analyze),
2965 New_Occurrence_Of (Etype (Subp), Sloc (N)));
2966 end if;
2968 if No (Declarations (N)) then
2969 Set_Declarations (N, New_List (Body_To_Analyze));
2970 else
2971 Append (Body_To_Analyze, Declarations (N));
2972 end if;
2974 Expander_Mode_Save_And_Set (False);
2975 Remove_Pragmas;
2977 Analyze (Body_To_Analyze);
2978 Push_Scope (Defining_Entity (Body_To_Analyze));
2979 Save_Global_References (Original_Body);
2980 End_Scope;
2981 Remove (Body_To_Analyze);
2983 Expander_Mode_Restore;
2985 -- Restore environment if previously saved
2987 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
2988 Restore_Env;
2989 end if;
2991 -- If secondary stk used there is no point in inlining. We have
2992 -- already issued the warning in this case, so nothing to do.
2994 if Uses_Secondary_Stack (Body_To_Analyze) then
2995 return;
2996 end if;
2998 Set_Body_To_Inline (Decl, Original_Body);
2999 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
3000 Set_Is_Inlined (Subp);
3001 end Build_Body_To_Inline;
3003 -------------------
3004 -- Cannot_Inline --
3005 -------------------
3007 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
3008 begin
3009 -- Do not emit warning if this is a predefined unit which is not
3010 -- the main unit. With validity checks enabled, some predefined
3011 -- subprograms may contain nested subprograms and become ineligible
3012 -- for inlining.
3014 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
3015 and then not In_Extended_Main_Source_Unit (Subp)
3016 then
3017 null;
3019 elsif Has_Pragma_Inline_Always (Subp) then
3021 -- Remove last character (question mark) to make this into an error,
3022 -- because the Inline_Always pragma cannot be obeyed.
3024 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
3026 elsif Ineffective_Inline_Warnings then
3027 Error_Msg_NE (Msg, N, Subp);
3028 end if;
3029 end Cannot_Inline;
3031 -----------------------
3032 -- Check_Conformance --
3033 -----------------------
3035 procedure Check_Conformance
3036 (New_Id : Entity_Id;
3037 Old_Id : Entity_Id;
3038 Ctype : Conformance_Type;
3039 Errmsg : Boolean;
3040 Conforms : out Boolean;
3041 Err_Loc : Node_Id := Empty;
3042 Get_Inst : Boolean := False;
3043 Skip_Controlling_Formals : Boolean := False)
3045 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
3046 -- Post error message for conformance error on given node. Two messages
3047 -- are output. The first points to the previous declaration with a
3048 -- general "no conformance" message. The second is the detailed reason,
3049 -- supplied as Msg. The parameter N provide information for a possible
3050 -- & insertion in the message, and also provides the location for
3051 -- posting the message in the absence of a specified Err_Loc location.
3053 -----------------------
3054 -- Conformance_Error --
3055 -----------------------
3057 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
3058 Enode : Node_Id;
3060 begin
3061 Conforms := False;
3063 if Errmsg then
3064 if No (Err_Loc) then
3065 Enode := N;
3066 else
3067 Enode := Err_Loc;
3068 end if;
3070 Error_Msg_Sloc := Sloc (Old_Id);
3072 case Ctype is
3073 when Type_Conformant =>
3074 Error_Msg_N
3075 ("not type conformant with declaration#!", Enode);
3077 when Mode_Conformant =>
3078 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3079 Error_Msg_N
3080 ("not mode conformant with operation inherited#!",
3081 Enode);
3082 else
3083 Error_Msg_N
3084 ("not mode conformant with declaration#!", Enode);
3085 end if;
3087 when Subtype_Conformant =>
3088 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3089 Error_Msg_N
3090 ("not subtype conformant with operation inherited#!",
3091 Enode);
3092 else
3093 Error_Msg_N
3094 ("not subtype conformant with declaration#!", Enode);
3095 end if;
3097 when Fully_Conformant =>
3098 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3099 Error_Msg_N
3100 ("not fully conformant with operation inherited#!",
3101 Enode);
3102 else
3103 Error_Msg_N
3104 ("not fully conformant with declaration#!", Enode);
3105 end if;
3106 end case;
3108 Error_Msg_NE (Msg, Enode, N);
3109 end if;
3110 end Conformance_Error;
3112 -- Local Variables
3114 Old_Type : constant Entity_Id := Etype (Old_Id);
3115 New_Type : constant Entity_Id := Etype (New_Id);
3116 Old_Formal : Entity_Id;
3117 New_Formal : Entity_Id;
3118 Access_Types_Match : Boolean;
3119 Old_Formal_Base : Entity_Id;
3120 New_Formal_Base : Entity_Id;
3122 -- Start of processing for Check_Conformance
3124 begin
3125 Conforms := True;
3127 -- We need a special case for operators, since they don't appear
3128 -- explicitly.
3130 if Ctype = Type_Conformant then
3131 if Ekind (New_Id) = E_Operator
3132 and then Operator_Matches_Spec (New_Id, Old_Id)
3133 then
3134 return;
3135 end if;
3136 end if;
3138 -- If both are functions/operators, check return types conform
3140 if Old_Type /= Standard_Void_Type
3141 and then New_Type /= Standard_Void_Type
3142 then
3144 -- If we are checking interface conformance we omit controlling
3145 -- arguments and result, because we are only checking the conformance
3146 -- of the remaining parameters.
3148 if Has_Controlling_Result (Old_Id)
3149 and then Has_Controlling_Result (New_Id)
3150 and then Skip_Controlling_Formals
3151 then
3152 null;
3154 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
3155 Conformance_Error ("\return type does not match!", New_Id);
3156 return;
3157 end if;
3159 -- Ada 2005 (AI-231): In case of anonymous access types check the
3160 -- null-exclusion and access-to-constant attributes match.
3162 if Ada_Version >= Ada_05
3163 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
3164 and then
3165 (Can_Never_Be_Null (Old_Type)
3166 /= Can_Never_Be_Null (New_Type)
3167 or else Is_Access_Constant (Etype (Old_Type))
3168 /= Is_Access_Constant (Etype (New_Type)))
3169 then
3170 Conformance_Error ("\return type does not match!", New_Id);
3171 return;
3172 end if;
3174 -- If either is a function/operator and the other isn't, error
3176 elsif Old_Type /= Standard_Void_Type
3177 or else New_Type /= Standard_Void_Type
3178 then
3179 Conformance_Error ("\functions can only match functions!", New_Id);
3180 return;
3181 end if;
3183 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
3184 -- If this is a renaming as body, refine error message to indicate that
3185 -- the conflict is with the original declaration. If the entity is not
3186 -- frozen, the conventions don't have to match, the one of the renamed
3187 -- entity is inherited.
3189 if Ctype >= Subtype_Conformant then
3190 if Convention (Old_Id) /= Convention (New_Id) then
3192 if not Is_Frozen (New_Id) then
3193 null;
3195 elsif Present (Err_Loc)
3196 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
3197 and then Present (Corresponding_Spec (Err_Loc))
3198 then
3199 Error_Msg_Name_1 := Chars (New_Id);
3200 Error_Msg_Name_2 :=
3201 Name_Ada + Convention_Id'Pos (Convention (New_Id));
3203 Conformance_Error ("\prior declaration for% has convention %!");
3205 else
3206 Conformance_Error ("\calling conventions do not match!");
3207 end if;
3209 return;
3211 elsif Is_Formal_Subprogram (Old_Id)
3212 or else Is_Formal_Subprogram (New_Id)
3213 then
3214 Conformance_Error ("\formal subprograms not allowed!");
3215 return;
3216 end if;
3217 end if;
3219 -- Deal with parameters
3221 -- Note: we use the entity information, rather than going directly
3222 -- to the specification in the tree. This is not only simpler, but
3223 -- absolutely necessary for some cases of conformance tests between
3224 -- operators, where the declaration tree simply does not exist!
3226 Old_Formal := First_Formal (Old_Id);
3227 New_Formal := First_Formal (New_Id);
3229 while Present (Old_Formal) and then Present (New_Formal) loop
3230 if Is_Controlling_Formal (Old_Formal)
3231 and then Is_Controlling_Formal (New_Formal)
3232 and then Skip_Controlling_Formals
3233 then
3234 goto Skip_Controlling_Formal;
3235 end if;
3237 if Ctype = Fully_Conformant then
3239 -- Names must match. Error message is more accurate if we do
3240 -- this before checking that the types of the formals match.
3242 if Chars (Old_Formal) /= Chars (New_Formal) then
3243 Conformance_Error ("\name & does not match!", New_Formal);
3245 -- Set error posted flag on new formal as well to stop
3246 -- junk cascaded messages in some cases.
3248 Set_Error_Posted (New_Formal);
3249 return;
3250 end if;
3251 end if;
3253 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
3254 -- case occurs whenever a subprogram is being renamed and one of its
3255 -- parameters imposes a null exclusion. For example:
3257 -- type T is null record;
3258 -- type Acc_T is access T;
3259 -- subtype Acc_T_Sub is Acc_T;
3261 -- procedure P (Obj : not null Acc_T_Sub); -- itype
3262 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
3263 -- renames P;
3265 Old_Formal_Base := Etype (Old_Formal);
3266 New_Formal_Base := Etype (New_Formal);
3268 if Get_Inst then
3269 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
3270 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
3271 end if;
3273 Access_Types_Match := Ada_Version >= Ada_05
3275 -- Ensure that this rule is only applied when New_Id is a
3276 -- renaming of Old_Id.
3278 and then Nkind (Parent (Parent (New_Id))) =
3279 N_Subprogram_Renaming_Declaration
3280 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
3281 and then Present (Entity (Name (Parent (Parent (New_Id)))))
3282 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
3284 -- Now handle the allowed access-type case
3286 and then Is_Access_Type (Old_Formal_Base)
3287 and then Is_Access_Type (New_Formal_Base)
3289 -- The type kinds must match. The only exception occurs with
3290 -- multiple generics of the form:
3292 -- generic generic
3293 -- type F is private; type A is private;
3294 -- type F_Ptr is access F; type A_Ptr is access A;
3295 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
3296 -- package F_Pack is ... package A_Pack is
3297 -- package F_Inst is
3298 -- new F_Pack (A, A_Ptr, A_P);
3300 -- When checking for conformance between the parameters of A_P
3301 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
3302 -- because the compiler has transformed A_Ptr into a subtype of
3303 -- F_Ptr. We catch this case in the code below.
3305 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
3306 or else
3307 (Is_Generic_Type (Old_Formal_Base)
3308 and then Is_Generic_Type (New_Formal_Base)
3309 and then Is_Internal (New_Formal_Base)
3310 and then Etype (Etype (New_Formal_Base)) =
3311 Old_Formal_Base))
3312 and then Directly_Designated_Type (Old_Formal_Base) =
3313 Directly_Designated_Type (New_Formal_Base)
3314 and then ((Is_Itype (Old_Formal_Base)
3315 and then Can_Never_Be_Null (Old_Formal_Base))
3316 or else
3317 (Is_Itype (New_Formal_Base)
3318 and then Can_Never_Be_Null (New_Formal_Base)));
3320 -- Types must always match. In the visible part of an instance,
3321 -- usual overloading rules for dispatching operations apply, and
3322 -- we check base types (not the actual subtypes).
3324 if In_Instance_Visible_Part
3325 and then Is_Dispatching_Operation (New_Id)
3326 then
3327 if not Conforming_Types
3328 (T1 => Base_Type (Etype (Old_Formal)),
3329 T2 => Base_Type (Etype (New_Formal)),
3330 Ctype => Ctype,
3331 Get_Inst => Get_Inst)
3332 and then not Access_Types_Match
3333 then
3334 Conformance_Error ("\type of & does not match!", New_Formal);
3335 return;
3336 end if;
3338 elsif not Conforming_Types
3339 (T1 => Old_Formal_Base,
3340 T2 => New_Formal_Base,
3341 Ctype => Ctype,
3342 Get_Inst => Get_Inst)
3343 and then not Access_Types_Match
3344 then
3345 Conformance_Error ("\type of & does not match!", New_Formal);
3346 return;
3347 end if;
3349 -- For mode conformance, mode must match
3351 if Ctype >= Mode_Conformant then
3352 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
3353 Conformance_Error ("\mode of & does not match!", New_Formal);
3354 return;
3356 -- Part of mode conformance for access types is having the same
3357 -- constant modifier.
3359 elsif Access_Types_Match
3360 and then Is_Access_Constant (Old_Formal_Base) /=
3361 Is_Access_Constant (New_Formal_Base)
3362 then
3363 Conformance_Error
3364 ("\constant modifier does not match!", New_Formal);
3365 return;
3366 end if;
3367 end if;
3369 if Ctype >= Subtype_Conformant then
3371 -- Ada 2005 (AI-231): In case of anonymous access types check
3372 -- the null-exclusion and access-to-constant attributes must
3373 -- match.
3375 if Ada_Version >= Ada_05
3376 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
3377 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
3378 and then
3379 (Can_Never_Be_Null (Old_Formal) /=
3380 Can_Never_Be_Null (New_Formal)
3381 or else
3382 Is_Access_Constant (Etype (Old_Formal)) /=
3383 Is_Access_Constant (Etype (New_Formal)))
3384 then
3385 -- It is allowed to omit the null-exclusion in case of stream
3386 -- attribute subprograms. We recognize stream subprograms
3387 -- through their TSS-generated suffix.
3389 declare
3390 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
3391 begin
3392 if TSS_Name /= TSS_Stream_Read
3393 and then TSS_Name /= TSS_Stream_Write
3394 and then TSS_Name /= TSS_Stream_Input
3395 and then TSS_Name /= TSS_Stream_Output
3396 then
3397 Conformance_Error
3398 ("\type of & does not match!", New_Formal);
3399 return;
3400 end if;
3401 end;
3402 end if;
3403 end if;
3405 -- Full conformance checks
3407 if Ctype = Fully_Conformant then
3409 -- We have checked already that names match
3411 if Parameter_Mode (Old_Formal) = E_In_Parameter then
3413 -- Check default expressions for in parameters
3415 declare
3416 NewD : constant Boolean :=
3417 Present (Default_Value (New_Formal));
3418 OldD : constant Boolean :=
3419 Present (Default_Value (Old_Formal));
3420 begin
3421 if NewD or OldD then
3423 -- The old default value has been analyzed because the
3424 -- current full declaration will have frozen everything
3425 -- before. The new default value has not been analyzed,
3426 -- so analyze it now before we check for conformance.
3428 if NewD then
3429 Push_Scope (New_Id);
3430 Preanalyze_Spec_Expression
3431 (Default_Value (New_Formal), Etype (New_Formal));
3432 End_Scope;
3433 end if;
3435 if not (NewD and OldD)
3436 or else not Fully_Conformant_Expressions
3437 (Default_Value (Old_Formal),
3438 Default_Value (New_Formal))
3439 then
3440 Conformance_Error
3441 ("\default expression for & does not match!",
3442 New_Formal);
3443 return;
3444 end if;
3445 end if;
3446 end;
3447 end if;
3448 end if;
3450 -- A couple of special checks for Ada 83 mode. These checks are
3451 -- skipped if either entity is an operator in package Standard,
3452 -- or if either old or new instance is not from the source program.
3454 if Ada_Version = Ada_83
3455 and then Sloc (Old_Id) > Standard_Location
3456 and then Sloc (New_Id) > Standard_Location
3457 and then Comes_From_Source (Old_Id)
3458 and then Comes_From_Source (New_Id)
3459 then
3460 declare
3461 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
3462 New_Param : constant Node_Id := Declaration_Node (New_Formal);
3464 begin
3465 -- Explicit IN must be present or absent in both cases. This
3466 -- test is required only in the full conformance case.
3468 if In_Present (Old_Param) /= In_Present (New_Param)
3469 and then Ctype = Fully_Conformant
3470 then
3471 Conformance_Error
3472 ("\(Ada 83) IN must appear in both declarations",
3473 New_Formal);
3474 return;
3475 end if;
3477 -- Grouping (use of comma in param lists) must be the same
3478 -- This is where we catch a misconformance like:
3480 -- A, B : Integer
3481 -- A : Integer; B : Integer
3483 -- which are represented identically in the tree except
3484 -- for the setting of the flags More_Ids and Prev_Ids.
3486 if More_Ids (Old_Param) /= More_Ids (New_Param)
3487 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
3488 then
3489 Conformance_Error
3490 ("\grouping of & does not match!", New_Formal);
3491 return;
3492 end if;
3493 end;
3494 end if;
3496 -- This label is required when skipping controlling formals
3498 <<Skip_Controlling_Formal>>
3500 Next_Formal (Old_Formal);
3501 Next_Formal (New_Formal);
3502 end loop;
3504 if Present (Old_Formal) then
3505 Conformance_Error ("\too few parameters!");
3506 return;
3508 elsif Present (New_Formal) then
3509 Conformance_Error ("\too many parameters!", New_Formal);
3510 return;
3511 end if;
3512 end Check_Conformance;
3514 -----------------------
3515 -- Check_Conventions --
3516 -----------------------
3518 procedure Check_Conventions (Typ : Entity_Id) is
3519 Ifaces_List : Elist_Id;
3521 procedure Check_Convention (Op : Entity_Id);
3522 -- Verify that the convention of inherited dispatching operation Op is
3523 -- consistent among all subprograms it overrides. In order to minimize
3524 -- the search, Search_From is utilized to designate a specific point in
3525 -- the list rather than iterating over the whole list once more.
3527 ----------------------
3528 -- Check_Convention --
3529 ----------------------
3531 procedure Check_Convention (Op : Entity_Id) is
3532 Iface_Elmt : Elmt_Id;
3533 Iface_Prim_Elmt : Elmt_Id;
3534 Iface_Prim : Entity_Id;
3536 begin
3537 Iface_Elmt := First_Elmt (Ifaces_List);
3538 while Present (Iface_Elmt) loop
3539 Iface_Prim_Elmt :=
3540 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
3541 while Present (Iface_Prim_Elmt) loop
3542 Iface_Prim := Node (Iface_Prim_Elmt);
3544 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
3545 and then Convention (Iface_Prim) /= Convention (Op)
3546 then
3547 Error_Msg_N
3548 ("inconsistent conventions in primitive operations", Typ);
3550 Error_Msg_Name_1 := Chars (Op);
3551 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
3552 Error_Msg_Sloc := Sloc (Op);
3554 if Comes_From_Source (Op) then
3555 if not Is_Overriding_Operation (Op) then
3556 Error_Msg_N ("\\primitive % defined #", Typ);
3557 else
3558 Error_Msg_N ("\\overridding operation % with " &
3559 "convention % defined #", Typ);
3560 end if;
3562 else pragma Assert (Present (Alias (Op)));
3563 Error_Msg_Sloc := Sloc (Alias (Op));
3564 Error_Msg_N ("\\inherited operation % with " &
3565 "convention % defined #", Typ);
3566 end if;
3568 Error_Msg_Name_1 := Chars (Op);
3569 Error_Msg_Name_2 :=
3570 Get_Convention_Name (Convention (Iface_Prim));
3571 Error_Msg_Sloc := Sloc (Iface_Prim);
3572 Error_Msg_N ("\\overridden operation % with " &
3573 "convention % defined #", Typ);
3575 -- Avoid cascading errors
3577 return;
3578 end if;
3580 Next_Elmt (Iface_Prim_Elmt);
3581 end loop;
3583 Next_Elmt (Iface_Elmt);
3584 end loop;
3585 end Check_Convention;
3587 -- Local variables
3589 Prim_Op : Entity_Id;
3590 Prim_Op_Elmt : Elmt_Id;
3592 -- Start of processing for Check_Conventions
3594 begin
3595 if not Has_Interfaces (Typ) then
3596 return;
3597 end if;
3599 Collect_Interfaces (Typ, Ifaces_List);
3601 -- The algorithm checks every overriding dispatching operation against
3602 -- all the corresponding overridden dispatching operations, detecting
3603 -- differences in conventions.
3605 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
3606 while Present (Prim_Op_Elmt) loop
3607 Prim_Op := Node (Prim_Op_Elmt);
3609 -- A small optimization: skip the predefined dispatching operations
3610 -- since they always have the same convention.
3612 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
3613 Check_Convention (Prim_Op);
3614 end if;
3616 Next_Elmt (Prim_Op_Elmt);
3617 end loop;
3618 end Check_Conventions;
3620 ------------------------------
3621 -- Check_Delayed_Subprogram --
3622 ------------------------------
3624 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
3625 F : Entity_Id;
3627 procedure Possible_Freeze (T : Entity_Id);
3628 -- T is the type of either a formal parameter or of the return type.
3629 -- If T is not yet frozen and needs a delayed freeze, then the
3630 -- subprogram itself must be delayed.
3632 ---------------------
3633 -- Possible_Freeze --
3634 ---------------------
3636 procedure Possible_Freeze (T : Entity_Id) is
3637 begin
3638 if Has_Delayed_Freeze (T)
3639 and then not Is_Frozen (T)
3640 then
3641 Set_Has_Delayed_Freeze (Designator);
3643 elsif Is_Access_Type (T)
3644 and then Has_Delayed_Freeze (Designated_Type (T))
3645 and then not Is_Frozen (Designated_Type (T))
3646 then
3647 Set_Has_Delayed_Freeze (Designator);
3648 end if;
3649 end Possible_Freeze;
3651 -- Start of processing for Check_Delayed_Subprogram
3653 begin
3654 -- Never need to freeze abstract subprogram
3656 if Ekind (Designator) /= E_Subprogram_Type
3657 and then Is_Abstract_Subprogram (Designator)
3658 then
3659 null;
3660 else
3661 -- Need delayed freeze if return type itself needs a delayed
3662 -- freeze and is not yet frozen.
3664 Possible_Freeze (Etype (Designator));
3665 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
3667 -- Need delayed freeze if any of the formal types themselves need
3668 -- a delayed freeze and are not yet frozen.
3670 F := First_Formal (Designator);
3671 while Present (F) loop
3672 Possible_Freeze (Etype (F));
3673 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
3674 Next_Formal (F);
3675 end loop;
3676 end if;
3678 -- Mark functions that return by reference. Note that it cannot be
3679 -- done for delayed_freeze subprograms because the underlying
3680 -- returned type may not be known yet (for private types)
3682 if not Has_Delayed_Freeze (Designator)
3683 and then Expander_Active
3684 then
3685 declare
3686 Typ : constant Entity_Id := Etype (Designator);
3687 Utyp : constant Entity_Id := Underlying_Type (Typ);
3689 begin
3690 if Is_Inherently_Limited_Type (Typ) then
3691 Set_Returns_By_Ref (Designator);
3693 elsif Present (Utyp) and then CW_Or_Controlled_Type (Utyp) then
3694 Set_Returns_By_Ref (Designator);
3695 end if;
3696 end;
3697 end if;
3698 end Check_Delayed_Subprogram;
3700 ------------------------------------
3701 -- Check_Discriminant_Conformance --
3702 ------------------------------------
3704 procedure Check_Discriminant_Conformance
3705 (N : Node_Id;
3706 Prev : Entity_Id;
3707 Prev_Loc : Node_Id)
3709 Old_Discr : Entity_Id := First_Discriminant (Prev);
3710 New_Discr : Node_Id := First (Discriminant_Specifications (N));
3711 New_Discr_Id : Entity_Id;
3712 New_Discr_Type : Entity_Id;
3714 procedure Conformance_Error (Msg : String; N : Node_Id);
3715 -- Post error message for conformance error on given node. Two messages
3716 -- are output. The first points to the previous declaration with a
3717 -- general "no conformance" message. The second is the detailed reason,
3718 -- supplied as Msg. The parameter N provide information for a possible
3719 -- & insertion in the message.
3721 -----------------------
3722 -- Conformance_Error --
3723 -----------------------
3725 procedure Conformance_Error (Msg : String; N : Node_Id) is
3726 begin
3727 Error_Msg_Sloc := Sloc (Prev_Loc);
3728 Error_Msg_N ("not fully conformant with declaration#!", N);
3729 Error_Msg_NE (Msg, N, N);
3730 end Conformance_Error;
3732 -- Start of processing for Check_Discriminant_Conformance
3734 begin
3735 while Present (Old_Discr) and then Present (New_Discr) loop
3737 New_Discr_Id := Defining_Identifier (New_Discr);
3739 -- The subtype mark of the discriminant on the full type has not
3740 -- been analyzed so we do it here. For an access discriminant a new
3741 -- type is created.
3743 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
3744 New_Discr_Type :=
3745 Access_Definition (N, Discriminant_Type (New_Discr));
3747 else
3748 Analyze (Discriminant_Type (New_Discr));
3749 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
3750 end if;
3752 if not Conforming_Types
3753 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
3754 then
3755 Conformance_Error ("type of & does not match!", New_Discr_Id);
3756 return;
3757 else
3758 -- Treat the new discriminant as an occurrence of the old one,
3759 -- for navigation purposes, and fill in some semantic
3760 -- information, for completeness.
3762 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
3763 Set_Etype (New_Discr_Id, Etype (Old_Discr));
3764 Set_Scope (New_Discr_Id, Scope (Old_Discr));
3765 end if;
3767 -- Names must match
3769 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
3770 Conformance_Error ("name & does not match!", New_Discr_Id);
3771 return;
3772 end if;
3774 -- Default expressions must match
3776 declare
3777 NewD : constant Boolean :=
3778 Present (Expression (New_Discr));
3779 OldD : constant Boolean :=
3780 Present (Expression (Parent (Old_Discr)));
3782 begin
3783 if NewD or OldD then
3785 -- The old default value has been analyzed and expanded,
3786 -- because the current full declaration will have frozen
3787 -- everything before. The new default values have not been
3788 -- expanded, so expand now to check conformance.
3790 if NewD then
3791 Preanalyze_Spec_Expression
3792 (Expression (New_Discr), New_Discr_Type);
3793 end if;
3795 if not (NewD and OldD)
3796 or else not Fully_Conformant_Expressions
3797 (Expression (Parent (Old_Discr)),
3798 Expression (New_Discr))
3800 then
3801 Conformance_Error
3802 ("default expression for & does not match!",
3803 New_Discr_Id);
3804 return;
3805 end if;
3806 end if;
3807 end;
3809 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
3811 if Ada_Version = Ada_83 then
3812 declare
3813 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
3815 begin
3816 -- Grouping (use of comma in param lists) must be the same
3817 -- This is where we catch a misconformance like:
3819 -- A,B : Integer
3820 -- A : Integer; B : Integer
3822 -- which are represented identically in the tree except
3823 -- for the setting of the flags More_Ids and Prev_Ids.
3825 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
3826 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
3827 then
3828 Conformance_Error
3829 ("grouping of & does not match!", New_Discr_Id);
3830 return;
3831 end if;
3832 end;
3833 end if;
3835 Next_Discriminant (Old_Discr);
3836 Next (New_Discr);
3837 end loop;
3839 if Present (Old_Discr) then
3840 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
3841 return;
3843 elsif Present (New_Discr) then
3844 Conformance_Error
3845 ("too many discriminants!", Defining_Identifier (New_Discr));
3846 return;
3847 end if;
3848 end Check_Discriminant_Conformance;
3850 ----------------------------
3851 -- Check_Fully_Conformant --
3852 ----------------------------
3854 procedure Check_Fully_Conformant
3855 (New_Id : Entity_Id;
3856 Old_Id : Entity_Id;
3857 Err_Loc : Node_Id := Empty)
3859 Result : Boolean;
3860 pragma Warnings (Off, Result);
3861 begin
3862 Check_Conformance
3863 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
3864 end Check_Fully_Conformant;
3866 ---------------------------
3867 -- Check_Mode_Conformant --
3868 ---------------------------
3870 procedure Check_Mode_Conformant
3871 (New_Id : Entity_Id;
3872 Old_Id : Entity_Id;
3873 Err_Loc : Node_Id := Empty;
3874 Get_Inst : Boolean := False)
3876 Result : Boolean;
3877 pragma Warnings (Off, Result);
3878 begin
3879 Check_Conformance
3880 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
3881 end Check_Mode_Conformant;
3883 --------------------------------
3884 -- Check_Overriding_Indicator --
3885 --------------------------------
3887 procedure Check_Overriding_Indicator
3888 (Subp : Entity_Id;
3889 Overridden_Subp : Entity_Id;
3890 Is_Primitive : Boolean)
3892 Decl : Node_Id;
3893 Spec : Node_Id;
3895 begin
3896 -- No overriding indicator for literals
3898 if Ekind (Subp) = E_Enumeration_Literal then
3899 return;
3901 elsif Ekind (Subp) = E_Entry then
3902 Decl := Parent (Subp);
3904 else
3905 Decl := Unit_Declaration_Node (Subp);
3906 end if;
3908 if Nkind_In (Decl, N_Subprogram_Body,
3909 N_Subprogram_Body_Stub,
3910 N_Subprogram_Declaration,
3911 N_Abstract_Subprogram_Declaration,
3912 N_Subprogram_Renaming_Declaration)
3913 then
3914 Spec := Specification (Decl);
3916 elsif Nkind (Decl) = N_Entry_Declaration then
3917 Spec := Decl;
3919 else
3920 return;
3921 end if;
3923 if Present (Overridden_Subp) then
3924 if Must_Not_Override (Spec) then
3925 Error_Msg_Sloc := Sloc (Overridden_Subp);
3927 if Ekind (Subp) = E_Entry then
3928 Error_Msg_NE
3929 ("entry & overrides inherited operation #", Spec, Subp);
3930 else
3931 Error_Msg_NE
3932 ("subprogram & overrides inherited operation #", Spec, Subp);
3933 end if;
3935 elsif Is_Subprogram (Subp) then
3936 Set_Is_Overriding_Operation (Subp);
3937 end if;
3939 -- If Subp is an operator, it may override a predefined operation.
3940 -- In that case overridden_subp is empty because of our implicit
3941 -- representation for predefined operators. We have to check whether the
3942 -- signature of Subp matches that of a predefined operator. Note that
3943 -- first argument provides the name of the operator, and the second
3944 -- argument the signature that may match that of a standard operation.
3945 -- If the indicator is overriding, then the operator must match a
3946 -- predefined signature, because we know already that there is no
3947 -- explicit overridden operation.
3949 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
3951 if Must_Not_Override (Spec) then
3952 if not Is_Primitive then
3953 Error_Msg_N
3954 ("overriding indicator only allowed "
3955 & "if subprogram is primitive", Subp);
3957 elsif Operator_Matches_Spec (Subp, Subp) then
3958 Error_Msg_NE
3959 ("subprogram & overrides predefined operator ", Spec, Subp);
3960 end if;
3962 elsif Is_Overriding_Operation (Subp) then
3963 null;
3965 elsif Must_Override (Spec) then
3966 if not Operator_Matches_Spec (Subp, Subp) then
3967 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
3969 else
3970 Set_Is_Overriding_Operation (Subp);
3971 end if;
3972 end if;
3974 elsif Must_Override (Spec) then
3975 if Ekind (Subp) = E_Entry then
3976 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
3977 else
3978 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
3979 end if;
3981 -- If the operation is marked "not overriding" and it's not primitive
3982 -- then an error is issued, unless this is an operation of a task or
3983 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
3984 -- has been specified have already been checked above.
3986 elsif Must_Not_Override (Spec)
3987 and then not Is_Primitive
3988 and then Ekind (Subp) /= E_Entry
3989 and then Ekind (Scope (Subp)) /= E_Protected_Type
3990 then
3991 Error_Msg_N
3992 ("overriding indicator only allowed if subprogram is primitive",
3993 Subp);
3994 return;
3995 end if;
3996 end Check_Overriding_Indicator;
3998 -------------------
3999 -- Check_Returns --
4000 -------------------
4002 -- Note: this procedure needs to know far too much about how the expander
4003 -- messes with exceptions. The use of the flag Exception_Junk and the
4004 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
4005 -- works, but is not very clean. It would be better if the expansion
4006 -- routines would leave Original_Node working nicely, and we could use
4007 -- Original_Node here to ignore all the peculiar expander messing ???
4009 procedure Check_Returns
4010 (HSS : Node_Id;
4011 Mode : Character;
4012 Err : out Boolean;
4013 Proc : Entity_Id := Empty)
4015 Handler : Node_Id;
4017 procedure Check_Statement_Sequence (L : List_Id);
4018 -- Internal recursive procedure to check a list of statements for proper
4019 -- termination by a return statement (or a transfer of control or a
4020 -- compound statement that is itself internally properly terminated).
4022 ------------------------------
4023 -- Check_Statement_Sequence --
4024 ------------------------------
4026 procedure Check_Statement_Sequence (L : List_Id) is
4027 Last_Stm : Node_Id;
4028 Stm : Node_Id;
4029 Kind : Node_Kind;
4031 Raise_Exception_Call : Boolean;
4032 -- Set True if statement sequence terminated by Raise_Exception call
4033 -- or a Reraise_Occurrence call.
4035 begin
4036 Raise_Exception_Call := False;
4038 -- Get last real statement
4040 Last_Stm := Last (L);
4042 -- Deal with digging out exception handler statement sequences that
4043 -- have been transformed by the local raise to goto optimization.
4044 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
4045 -- optimization has occurred, we are looking at something like:
4047 -- begin
4048 -- original stmts in block
4050 -- exception \
4051 -- when excep1 => |
4052 -- goto L1; | omitted if No_Exception_Propagation
4053 -- when excep2 => |
4054 -- goto L2; /
4055 -- end;
4057 -- goto L3; -- skip handler when exception not raised
4059 -- <<L1>> -- target label for local exception
4060 -- begin
4061 -- estmts1
4062 -- end;
4064 -- goto L3;
4066 -- <<L2>>
4067 -- begin
4068 -- estmts2
4069 -- end;
4071 -- <<L3>>
4073 -- and what we have to do is to dig out the estmts1 and estmts2
4074 -- sequences (which were the original sequences of statements in
4075 -- the exception handlers) and check them.
4077 if Nkind (Last_Stm) = N_Label
4078 and then Exception_Junk (Last_Stm)
4079 then
4080 Stm := Last_Stm;
4081 loop
4082 Prev (Stm);
4083 exit when No (Stm);
4084 exit when Nkind (Stm) /= N_Block_Statement;
4085 exit when not Exception_Junk (Stm);
4086 Prev (Stm);
4087 exit when No (Stm);
4088 exit when Nkind (Stm) /= N_Label;
4089 exit when not Exception_Junk (Stm);
4090 Check_Statement_Sequence
4091 (Statements (Handled_Statement_Sequence (Next (Stm))));
4093 Prev (Stm);
4094 Last_Stm := Stm;
4095 exit when No (Stm);
4096 exit when Nkind (Stm) /= N_Goto_Statement;
4097 exit when not Exception_Junk (Stm);
4098 end loop;
4099 end if;
4101 -- Don't count pragmas
4103 while Nkind (Last_Stm) = N_Pragma
4105 -- Don't count call to SS_Release (can happen after Raise_Exception)
4107 or else
4108 (Nkind (Last_Stm) = N_Procedure_Call_Statement
4109 and then
4110 Nkind (Name (Last_Stm)) = N_Identifier
4111 and then
4112 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
4114 -- Don't count exception junk
4116 or else
4117 (Nkind_In (Last_Stm, N_Goto_Statement,
4118 N_Label,
4119 N_Object_Declaration)
4120 and then Exception_Junk (Last_Stm))
4121 or else Nkind (Last_Stm) in N_Push_xxx_Label
4122 or else Nkind (Last_Stm) in N_Pop_xxx_Label
4123 loop
4124 Prev (Last_Stm);
4125 end loop;
4127 -- Here we have the "real" last statement
4129 Kind := Nkind (Last_Stm);
4131 -- Transfer of control, OK. Note that in the No_Return procedure
4132 -- case, we already diagnosed any explicit return statements, so
4133 -- we can treat them as OK in this context.
4135 if Is_Transfer (Last_Stm) then
4136 return;
4138 -- Check cases of explicit non-indirect procedure calls
4140 elsif Kind = N_Procedure_Call_Statement
4141 and then Is_Entity_Name (Name (Last_Stm))
4142 then
4143 -- Check call to Raise_Exception procedure which is treated
4144 -- specially, as is a call to Reraise_Occurrence.
4146 -- We suppress the warning in these cases since it is likely that
4147 -- the programmer really does not expect to deal with the case
4148 -- of Null_Occurrence, and thus would find a warning about a
4149 -- missing return curious, and raising Program_Error does not
4150 -- seem such a bad behavior if this does occur.
4152 -- Note that in the Ada 2005 case for Raise_Exception, the actual
4153 -- behavior will be to raise Constraint_Error (see AI-329).
4155 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
4156 or else
4157 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
4158 then
4159 Raise_Exception_Call := True;
4161 -- For Raise_Exception call, test first argument, if it is
4162 -- an attribute reference for a 'Identity call, then we know
4163 -- that the call cannot possibly return.
4165 declare
4166 Arg : constant Node_Id :=
4167 Original_Node (First_Actual (Last_Stm));
4168 begin
4169 if Nkind (Arg) = N_Attribute_Reference
4170 and then Attribute_Name (Arg) = Name_Identity
4171 then
4172 return;
4173 end if;
4174 end;
4175 end if;
4177 -- If statement, need to look inside if there is an else and check
4178 -- each constituent statement sequence for proper termination.
4180 elsif Kind = N_If_Statement
4181 and then Present (Else_Statements (Last_Stm))
4182 then
4183 Check_Statement_Sequence (Then_Statements (Last_Stm));
4184 Check_Statement_Sequence (Else_Statements (Last_Stm));
4186 if Present (Elsif_Parts (Last_Stm)) then
4187 declare
4188 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
4190 begin
4191 while Present (Elsif_Part) loop
4192 Check_Statement_Sequence (Then_Statements (Elsif_Part));
4193 Next (Elsif_Part);
4194 end loop;
4195 end;
4196 end if;
4198 return;
4200 -- Case statement, check each case for proper termination
4202 elsif Kind = N_Case_Statement then
4203 declare
4204 Case_Alt : Node_Id;
4205 begin
4206 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
4207 while Present (Case_Alt) loop
4208 Check_Statement_Sequence (Statements (Case_Alt));
4209 Next_Non_Pragma (Case_Alt);
4210 end loop;
4211 end;
4213 return;
4215 -- Block statement, check its handled sequence of statements
4217 elsif Kind = N_Block_Statement then
4218 declare
4219 Err1 : Boolean;
4221 begin
4222 Check_Returns
4223 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
4225 if Err1 then
4226 Err := True;
4227 end if;
4229 return;
4230 end;
4232 -- Loop statement. If there is an iteration scheme, we can definitely
4233 -- fall out of the loop. Similarly if there is an exit statement, we
4234 -- can fall out. In either case we need a following return.
4236 elsif Kind = N_Loop_Statement then
4237 if Present (Iteration_Scheme (Last_Stm))
4238 or else Has_Exit (Entity (Identifier (Last_Stm)))
4239 then
4240 null;
4242 -- A loop with no exit statement or iteration scheme is either
4243 -- an infinite loop, or it has some other exit (raise/return).
4244 -- In either case, no warning is required.
4246 else
4247 return;
4248 end if;
4250 -- Timed entry call, check entry call and delay alternatives
4252 -- Note: in expanded code, the timed entry call has been converted
4253 -- to a set of expanded statements on which the check will work
4254 -- correctly in any case.
4256 elsif Kind = N_Timed_Entry_Call then
4257 declare
4258 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4259 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
4261 begin
4262 -- If statement sequence of entry call alternative is missing,
4263 -- then we can definitely fall through, and we post the error
4264 -- message on the entry call alternative itself.
4266 if No (Statements (ECA)) then
4267 Last_Stm := ECA;
4269 -- If statement sequence of delay alternative is missing, then
4270 -- we can definitely fall through, and we post the error
4271 -- message on the delay alternative itself.
4273 -- Note: if both ECA and DCA are missing the return, then we
4274 -- post only one message, should be enough to fix the bugs.
4275 -- If not we will get a message next time on the DCA when the
4276 -- ECA is fixed!
4278 elsif No (Statements (DCA)) then
4279 Last_Stm := DCA;
4281 -- Else check both statement sequences
4283 else
4284 Check_Statement_Sequence (Statements (ECA));
4285 Check_Statement_Sequence (Statements (DCA));
4286 return;
4287 end if;
4288 end;
4290 -- Conditional entry call, check entry call and else part
4292 -- Note: in expanded code, the conditional entry call has been
4293 -- converted to a set of expanded statements on which the check
4294 -- will work correctly in any case.
4296 elsif Kind = N_Conditional_Entry_Call then
4297 declare
4298 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4300 begin
4301 -- If statement sequence of entry call alternative is missing,
4302 -- then we can definitely fall through, and we post the error
4303 -- message on the entry call alternative itself.
4305 if No (Statements (ECA)) then
4306 Last_Stm := ECA;
4308 -- Else check statement sequence and else part
4310 else
4311 Check_Statement_Sequence (Statements (ECA));
4312 Check_Statement_Sequence (Else_Statements (Last_Stm));
4313 return;
4314 end if;
4315 end;
4316 end if;
4318 -- If we fall through, issue appropriate message
4320 if Mode = 'F' then
4321 if not Raise_Exception_Call then
4322 Error_Msg_N
4323 ("?RETURN statement missing following this statement!",
4324 Last_Stm);
4325 Error_Msg_N
4326 ("\?Program_Error may be raised at run time!",
4327 Last_Stm);
4328 end if;
4330 -- Note: we set Err even though we have not issued a warning
4331 -- because we still have a case of a missing return. This is
4332 -- an extremely marginal case, probably will never be noticed
4333 -- but we might as well get it right.
4335 Err := True;
4337 -- Otherwise we have the case of a procedure marked No_Return
4339 else
4340 if not Raise_Exception_Call then
4341 Error_Msg_N
4342 ("?implied return after this statement " &
4343 "will raise Program_Error",
4344 Last_Stm);
4345 Error_Msg_NE
4346 ("\?procedure & is marked as No_Return!",
4347 Last_Stm, Proc);
4348 end if;
4350 declare
4351 RE : constant Node_Id :=
4352 Make_Raise_Program_Error (Sloc (Last_Stm),
4353 Reason => PE_Implicit_Return);
4354 begin
4355 Insert_After (Last_Stm, RE);
4356 Analyze (RE);
4357 end;
4358 end if;
4359 end Check_Statement_Sequence;
4361 -- Start of processing for Check_Returns
4363 begin
4364 Err := False;
4365 Check_Statement_Sequence (Statements (HSS));
4367 if Present (Exception_Handlers (HSS)) then
4368 Handler := First_Non_Pragma (Exception_Handlers (HSS));
4369 while Present (Handler) loop
4370 Check_Statement_Sequence (Statements (Handler));
4371 Next_Non_Pragma (Handler);
4372 end loop;
4373 end if;
4374 end Check_Returns;
4376 ----------------------------
4377 -- Check_Subprogram_Order --
4378 ----------------------------
4380 procedure Check_Subprogram_Order (N : Node_Id) is
4382 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
4383 -- This is used to check if S1 > S2 in the sense required by this
4384 -- test, for example nameab < namec, but name2 < name10.
4386 -----------------------------
4387 -- Subprogram_Name_Greater --
4388 -----------------------------
4390 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
4391 L1, L2 : Positive;
4392 N1, N2 : Natural;
4394 begin
4395 -- Remove trailing numeric parts
4397 L1 := S1'Last;
4398 while S1 (L1) in '0' .. '9' loop
4399 L1 := L1 - 1;
4400 end loop;
4402 L2 := S2'Last;
4403 while S2 (L2) in '0' .. '9' loop
4404 L2 := L2 - 1;
4405 end loop;
4407 -- If non-numeric parts non-equal, that's decisive
4409 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
4410 return False;
4412 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
4413 return True;
4415 -- If non-numeric parts equal, compare suffixed numeric parts. Note
4416 -- that a missing suffix is treated as numeric zero in this test.
4418 else
4419 N1 := 0;
4420 while L1 < S1'Last loop
4421 L1 := L1 + 1;
4422 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
4423 end loop;
4425 N2 := 0;
4426 while L2 < S2'Last loop
4427 L2 := L2 + 1;
4428 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
4429 end loop;
4431 return N1 > N2;
4432 end if;
4433 end Subprogram_Name_Greater;
4435 -- Start of processing for Check_Subprogram_Order
4437 begin
4438 -- Check body in alpha order if this is option
4440 if Style_Check
4441 and then Style_Check_Order_Subprograms
4442 and then Nkind (N) = N_Subprogram_Body
4443 and then Comes_From_Source (N)
4444 and then In_Extended_Main_Source_Unit (N)
4445 then
4446 declare
4447 LSN : String_Ptr
4448 renames Scope_Stack.Table
4449 (Scope_Stack.Last).Last_Subprogram_Name;
4451 Body_Id : constant Entity_Id :=
4452 Defining_Entity (Specification (N));
4454 begin
4455 Get_Decoded_Name_String (Chars (Body_Id));
4457 if LSN /= null then
4458 if Subprogram_Name_Greater
4459 (LSN.all, Name_Buffer (1 .. Name_Len))
4460 then
4461 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
4462 end if;
4464 Free (LSN);
4465 end if;
4467 LSN := new String'(Name_Buffer (1 .. Name_Len));
4468 end;
4469 end if;
4470 end Check_Subprogram_Order;
4472 ------------------------------
4473 -- Check_Subtype_Conformant --
4474 ------------------------------
4476 procedure Check_Subtype_Conformant
4477 (New_Id : Entity_Id;
4478 Old_Id : Entity_Id;
4479 Err_Loc : Node_Id := Empty;
4480 Skip_Controlling_Formals : Boolean := False)
4482 Result : Boolean;
4483 pragma Warnings (Off, Result);
4484 begin
4485 Check_Conformance
4486 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
4487 Skip_Controlling_Formals => Skip_Controlling_Formals);
4488 end Check_Subtype_Conformant;
4490 ---------------------------
4491 -- Check_Type_Conformant --
4492 ---------------------------
4494 procedure Check_Type_Conformant
4495 (New_Id : Entity_Id;
4496 Old_Id : Entity_Id;
4497 Err_Loc : Node_Id := Empty)
4499 Result : Boolean;
4500 pragma Warnings (Off, Result);
4501 begin
4502 Check_Conformance
4503 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
4504 end Check_Type_Conformant;
4506 ----------------------
4507 -- Conforming_Types --
4508 ----------------------
4510 function Conforming_Types
4511 (T1 : Entity_Id;
4512 T2 : Entity_Id;
4513 Ctype : Conformance_Type;
4514 Get_Inst : Boolean := False) return Boolean
4516 Type_1 : Entity_Id := T1;
4517 Type_2 : Entity_Id := T2;
4518 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
4520 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
4521 -- If neither T1 nor T2 are generic actual types, or if they are in
4522 -- different scopes (e.g. parent and child instances), then verify that
4523 -- the base types are equal. Otherwise T1 and T2 must be on the same
4524 -- subtype chain. The whole purpose of this procedure is to prevent
4525 -- spurious ambiguities in an instantiation that may arise if two
4526 -- distinct generic types are instantiated with the same actual.
4528 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
4529 -- An access parameter can designate an incomplete type. If the
4530 -- incomplete type is the limited view of a type from a limited_
4531 -- with_clause, check whether the non-limited view is available. If
4532 -- it is a (non-limited) incomplete type, get the full view.
4534 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
4535 -- Returns True if and only if either T1 denotes a limited view of T2
4536 -- or T2 denotes a limited view of T1. This can arise when the limited
4537 -- with view of a type is used in a subprogram declaration and the
4538 -- subprogram body is in the scope of a regular with clause for the
4539 -- same unit. In such a case, the two type entities can be considered
4540 -- identical for purposes of conformance checking.
4542 ----------------------
4543 -- Base_Types_Match --
4544 ----------------------
4546 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
4547 begin
4548 if T1 = T2 then
4549 return True;
4551 elsif Base_Type (T1) = Base_Type (T2) then
4553 -- The following is too permissive. A more precise test should
4554 -- check that the generic actual is an ancestor subtype of the
4555 -- other ???.
4557 return not Is_Generic_Actual_Type (T1)
4558 or else not Is_Generic_Actual_Type (T2)
4559 or else Scope (T1) /= Scope (T2);
4561 else
4562 return False;
4563 end if;
4564 end Base_Types_Match;
4566 --------------------------
4567 -- Find_Designated_Type --
4568 --------------------------
4570 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
4571 Desig : Entity_Id;
4573 begin
4574 Desig := Directly_Designated_Type (T);
4576 if Ekind (Desig) = E_Incomplete_Type then
4578 -- If regular incomplete type, get full view if available
4580 if Present (Full_View (Desig)) then
4581 Desig := Full_View (Desig);
4583 -- If limited view of a type, get non-limited view if available,
4584 -- and check again for a regular incomplete type.
4586 elsif Present (Non_Limited_View (Desig)) then
4587 Desig := Get_Full_View (Non_Limited_View (Desig));
4588 end if;
4589 end if;
4591 return Desig;
4592 end Find_Designated_Type;
4594 -------------------------------
4595 -- Matches_Limited_With_View --
4596 -------------------------------
4598 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
4599 begin
4600 -- In some cases a type imported through a limited_with clause, and
4601 -- its nonlimited view are both visible, for example in an anonymous
4602 -- access-to-class-wide type in a formal. Both entities designate the
4603 -- same type.
4605 if From_With_Type (T1)
4606 and then T2 = Available_View (T1)
4607 then
4608 return True;
4610 elsif From_With_Type (T2)
4611 and then T1 = Available_View (T2)
4612 then
4613 return True;
4615 else
4616 return False;
4617 end if;
4618 end Matches_Limited_With_View;
4620 -- Start of processing for Conforming_Types
4622 begin
4623 -- The context is an instance association for a formal
4624 -- access-to-subprogram type; the formal parameter types require
4625 -- mapping because they may denote other formal parameters of the
4626 -- generic unit.
4628 if Get_Inst then
4629 Type_1 := Get_Instance_Of (T1);
4630 Type_2 := Get_Instance_Of (T2);
4631 end if;
4633 -- If one of the types is a view of the other introduced by a limited
4634 -- with clause, treat these as conforming for all purposes.
4636 if Matches_Limited_With_View (T1, T2) then
4637 return True;
4639 elsif Base_Types_Match (Type_1, Type_2) then
4640 return Ctype <= Mode_Conformant
4641 or else Subtypes_Statically_Match (Type_1, Type_2);
4643 elsif Is_Incomplete_Or_Private_Type (Type_1)
4644 and then Present (Full_View (Type_1))
4645 and then Base_Types_Match (Full_View (Type_1), Type_2)
4646 then
4647 return Ctype <= Mode_Conformant
4648 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
4650 elsif Ekind (Type_2) = E_Incomplete_Type
4651 and then Present (Full_View (Type_2))
4652 and then Base_Types_Match (Type_1, Full_View (Type_2))
4653 then
4654 return Ctype <= Mode_Conformant
4655 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
4657 elsif Is_Private_Type (Type_2)
4658 and then In_Instance
4659 and then Present (Full_View (Type_2))
4660 and then Base_Types_Match (Type_1, Full_View (Type_2))
4661 then
4662 return Ctype <= Mode_Conformant
4663 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
4664 end if;
4666 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
4667 -- treated recursively because they carry a signature.
4669 Are_Anonymous_Access_To_Subprogram_Types :=
4670 Ekind (Type_1) = Ekind (Type_2)
4671 and then
4672 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
4673 or else
4674 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
4676 -- Test anonymous access type case. For this case, static subtype
4677 -- matching is required for mode conformance (RM 6.3.1(15)). We check
4678 -- the base types because we may have built internal subtype entities
4679 -- to handle null-excluding types (see Process_Formals).
4681 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
4682 and then
4683 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
4684 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
4685 then
4686 declare
4687 Desig_1 : Entity_Id;
4688 Desig_2 : Entity_Id;
4690 begin
4691 -- In Ada2005, access constant indicators must match for
4692 -- subtype conformance.
4694 if Ada_Version >= Ada_05
4695 and then Ctype >= Subtype_Conformant
4696 and then
4697 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
4698 then
4699 return False;
4700 end if;
4702 Desig_1 := Find_Designated_Type (Type_1);
4704 Desig_2 := Find_Designated_Type (Type_2);
4706 -- If the context is an instance association for a formal
4707 -- access-to-subprogram type; formal access parameter designated
4708 -- types require mapping because they may denote other formal
4709 -- parameters of the generic unit.
4711 if Get_Inst then
4712 Desig_1 := Get_Instance_Of (Desig_1);
4713 Desig_2 := Get_Instance_Of (Desig_2);
4714 end if;
4716 -- It is possible for a Class_Wide_Type to be introduced for an
4717 -- incomplete type, in which case there is a separate class_ wide
4718 -- type for the full view. The types conform if their Etypes
4719 -- conform, i.e. one may be the full view of the other. This can
4720 -- only happen in the context of an access parameter, other uses
4721 -- of an incomplete Class_Wide_Type are illegal.
4723 if Is_Class_Wide_Type (Desig_1)
4724 and then Is_Class_Wide_Type (Desig_2)
4725 then
4726 return
4727 Conforming_Types
4728 (Etype (Base_Type (Desig_1)),
4729 Etype (Base_Type (Desig_2)), Ctype);
4731 elsif Are_Anonymous_Access_To_Subprogram_Types then
4732 if Ada_Version < Ada_05 then
4733 return Ctype = Type_Conformant
4734 or else
4735 Subtypes_Statically_Match (Desig_1, Desig_2);
4737 -- We must check the conformance of the signatures themselves
4739 else
4740 declare
4741 Conformant : Boolean;
4742 begin
4743 Check_Conformance
4744 (Desig_1, Desig_2, Ctype, False, Conformant);
4745 return Conformant;
4746 end;
4747 end if;
4749 else
4750 return Base_Type (Desig_1) = Base_Type (Desig_2)
4751 and then (Ctype = Type_Conformant
4752 or else
4753 Subtypes_Statically_Match (Desig_1, Desig_2));
4754 end if;
4755 end;
4757 -- Otherwise definitely no match
4759 else
4760 if ((Ekind (Type_1) = E_Anonymous_Access_Type
4761 and then Is_Access_Type (Type_2))
4762 or else (Ekind (Type_2) = E_Anonymous_Access_Type
4763 and then Is_Access_Type (Type_1)))
4764 and then
4765 Conforming_Types
4766 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
4767 then
4768 May_Hide_Profile := True;
4769 end if;
4771 return False;
4772 end if;
4773 end Conforming_Types;
4775 --------------------------
4776 -- Create_Extra_Formals --
4777 --------------------------
4779 procedure Create_Extra_Formals (E : Entity_Id) is
4780 Formal : Entity_Id;
4781 First_Extra : Entity_Id := Empty;
4782 Last_Extra : Entity_Id;
4783 Formal_Type : Entity_Id;
4784 P_Formal : Entity_Id := Empty;
4786 function Add_Extra_Formal
4787 (Assoc_Entity : Entity_Id;
4788 Typ : Entity_Id;
4789 Scope : Entity_Id;
4790 Suffix : String) return Entity_Id;
4791 -- Add an extra formal to the current list of formals and extra formals.
4792 -- The extra formal is added to the end of the list of extra formals,
4793 -- and also returned as the result. These formals are always of mode IN.
4794 -- The new formal has the type Typ, is declared in Scope, and its name
4795 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
4797 ----------------------
4798 -- Add_Extra_Formal --
4799 ----------------------
4801 function Add_Extra_Formal
4802 (Assoc_Entity : Entity_Id;
4803 Typ : Entity_Id;
4804 Scope : Entity_Id;
4805 Suffix : String) return Entity_Id
4807 EF : constant Entity_Id :=
4808 Make_Defining_Identifier (Sloc (Assoc_Entity),
4809 Chars => New_External_Name (Chars (Assoc_Entity),
4810 Suffix => Suffix));
4812 begin
4813 -- A little optimization. Never generate an extra formal for the
4814 -- _init operand of an initialization procedure, since it could
4815 -- never be used.
4817 if Chars (Formal) = Name_uInit then
4818 return Empty;
4819 end if;
4821 Set_Ekind (EF, E_In_Parameter);
4822 Set_Actual_Subtype (EF, Typ);
4823 Set_Etype (EF, Typ);
4824 Set_Scope (EF, Scope);
4825 Set_Mechanism (EF, Default_Mechanism);
4826 Set_Formal_Validity (EF);
4828 if No (First_Extra) then
4829 First_Extra := EF;
4830 Set_Extra_Formals (Scope, First_Extra);
4831 end if;
4833 if Present (Last_Extra) then
4834 Set_Extra_Formal (Last_Extra, EF);
4835 end if;
4837 Last_Extra := EF;
4839 return EF;
4840 end Add_Extra_Formal;
4842 -- Start of processing for Create_Extra_Formals
4844 begin
4845 -- We never generate extra formals if expansion is not active
4846 -- because we don't need them unless we are generating code.
4848 if not Expander_Active then
4849 return;
4850 end if;
4852 -- If this is a derived subprogram then the subtypes of the parent
4853 -- subprogram's formal parameters will be used to to determine the need
4854 -- for extra formals.
4856 if Is_Overloadable (E) and then Present (Alias (E)) then
4857 P_Formal := First_Formal (Alias (E));
4858 end if;
4860 Last_Extra := Empty;
4861 Formal := First_Formal (E);
4862 while Present (Formal) loop
4863 Last_Extra := Formal;
4864 Next_Formal (Formal);
4865 end loop;
4867 -- If Extra_formals were already created, don't do it again. This
4868 -- situation may arise for subprogram types created as part of
4869 -- dispatching calls (see Expand_Dispatching_Call)
4871 if Present (Last_Extra) and then
4872 Present (Extra_Formal (Last_Extra))
4873 then
4874 return;
4875 end if;
4877 -- If the subprogram is a predefined dispatching subprogram then don't
4878 -- generate any extra constrained or accessibility level formals. In
4879 -- general we suppress these for internal subprograms (by not calling
4880 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
4881 -- generated stream attributes do get passed through because extra
4882 -- build-in-place formals are needed in some cases (limited 'Input).
4884 if Is_Predefined_Dispatching_Operation (E) then
4885 goto Test_For_BIP_Extras;
4886 end if;
4888 Formal := First_Formal (E);
4889 while Present (Formal) loop
4891 -- Create extra formal for supporting the attribute 'Constrained.
4892 -- The case of a private type view without discriminants also
4893 -- requires the extra formal if the underlying type has defaulted
4894 -- discriminants.
4896 if Ekind (Formal) /= E_In_Parameter then
4897 if Present (P_Formal) then
4898 Formal_Type := Etype (P_Formal);
4899 else
4900 Formal_Type := Etype (Formal);
4901 end if;
4903 -- Do not produce extra formals for Unchecked_Union parameters.
4904 -- Jump directly to the end of the loop.
4906 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
4907 goto Skip_Extra_Formal_Generation;
4908 end if;
4910 if not Has_Discriminants (Formal_Type)
4911 and then Ekind (Formal_Type) in Private_Kind
4912 and then Present (Underlying_Type (Formal_Type))
4913 then
4914 Formal_Type := Underlying_Type (Formal_Type);
4915 end if;
4917 if Has_Discriminants (Formal_Type)
4918 and then not Is_Constrained (Formal_Type)
4919 and then not Is_Indefinite_Subtype (Formal_Type)
4920 then
4921 Set_Extra_Constrained
4922 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "F"));
4923 end if;
4924 end if;
4926 -- Create extra formal for supporting accessibility checking. This
4927 -- is done for both anonymous access formals and formals of named
4928 -- access types that are marked as controlling formals. The latter
4929 -- case can occur when Expand_Dispatching_Call creates a subprogram
4930 -- type and substitutes the types of access-to-class-wide actuals
4931 -- for the anonymous access-to-specific-type of controlling formals.
4932 -- Base_Type is applied because in cases where there is a null
4933 -- exclusion the formal may have an access subtype.
4935 -- This is suppressed if we specifically suppress accessibility
4936 -- checks at the package level for either the subprogram, or the
4937 -- package in which it resides. However, we do not suppress it
4938 -- simply if the scope has accessibility checks suppressed, since
4939 -- this could cause trouble when clients are compiled with a
4940 -- different suppression setting. The explicit checks at the
4941 -- package level are safe from this point of view.
4943 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
4944 or else (Is_Controlling_Formal (Formal)
4945 and then Is_Access_Type (Base_Type (Etype (Formal)))))
4946 and then not
4947 (Explicit_Suppress (E, Accessibility_Check)
4948 or else
4949 Explicit_Suppress (Scope (E), Accessibility_Check))
4950 and then
4951 (No (P_Formal)
4952 or else Present (Extra_Accessibility (P_Formal)))
4953 then
4954 -- Temporary kludge: for now we avoid creating the extra formal
4955 -- for access parameters of protected operations because of
4956 -- problem with the case of internal protected calls. ???
4958 if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
4959 and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
4960 then
4961 Set_Extra_Accessibility
4962 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "F"));
4963 end if;
4964 end if;
4966 -- This label is required when skipping extra formal generation for
4967 -- Unchecked_Union parameters.
4969 <<Skip_Extra_Formal_Generation>>
4971 if Present (P_Formal) then
4972 Next_Formal (P_Formal);
4973 end if;
4975 Next_Formal (Formal);
4976 end loop;
4978 <<Test_For_BIP_Extras>>
4980 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
4981 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
4983 if Ada_Version >= Ada_05 and then Is_Build_In_Place_Function (E) then
4984 declare
4985 Result_Subt : constant Entity_Id := Etype (E);
4987 Discard : Entity_Id;
4988 pragma Warnings (Off, Discard);
4990 begin
4991 -- In the case of functions with unconstrained result subtypes,
4992 -- add a 3-state formal indicating whether the return object is
4993 -- allocated by the caller (0), or should be allocated by the
4994 -- callee on the secondary stack (1) or in the global heap (2).
4995 -- For the moment we just use Natural for the type of this formal.
4996 -- Note that this formal isn't usually needed in the case where
4997 -- the result subtype is constrained, but it is needed when the
4998 -- function has a tagged result, because generally such functions
4999 -- can be called in a dispatching context and such calls must be
5000 -- handled like calls to a class-wide function.
5002 if not Is_Constrained (Underlying_Type (Result_Subt))
5003 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5004 then
5005 Discard :=
5006 Add_Extra_Formal
5007 (E, Standard_Natural,
5008 E, BIP_Formal_Suffix (BIP_Alloc_Form));
5009 end if;
5011 -- In the case of functions whose result type has controlled
5012 -- parts, we have an extra formal of type
5013 -- System.Finalization_Implementation.Finalizable_Ptr_Ptr. That
5014 -- is, we are passing a pointer to a finalization list (which is
5015 -- itself a pointer). This extra formal is then passed along to
5016 -- Move_Final_List in case of successful completion of a return
5017 -- statement. We cannot pass an 'in out' parameter, because we
5018 -- need to update the finalization list during an abort-deferred
5019 -- region, rather than using copy-back after the function
5020 -- returns. This is true even if we are able to get away with
5021 -- having 'in out' parameters, which are normally illegal for
5022 -- functions. This formal is also needed when the function has
5023 -- a tagged result, because generally such functions can be called
5024 -- in a dispatching context and such calls must be handled like
5025 -- calls to class-wide functions.
5027 if Controlled_Type (Result_Subt)
5028 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5029 then
5030 Discard :=
5031 Add_Extra_Formal
5032 (E, RTE (RE_Finalizable_Ptr_Ptr),
5033 E, BIP_Formal_Suffix (BIP_Final_List));
5034 end if;
5036 -- If the result type contains tasks, we have two extra formals:
5037 -- the master of the tasks to be created, and the caller's
5038 -- activation chain.
5040 if Has_Task (Result_Subt) then
5041 Discard :=
5042 Add_Extra_Formal
5043 (E, RTE (RE_Master_Id),
5044 E, BIP_Formal_Suffix (BIP_Master));
5045 Discard :=
5046 Add_Extra_Formal
5047 (E, RTE (RE_Activation_Chain_Access),
5048 E, BIP_Formal_Suffix (BIP_Activation_Chain));
5049 end if;
5051 -- All build-in-place functions get an extra formal that will be
5052 -- passed the address of the return object within the caller.
5054 declare
5055 Formal_Type : constant Entity_Id :=
5056 Create_Itype
5057 (E_Anonymous_Access_Type, E,
5058 Scope_Id => Scope (E));
5059 begin
5060 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
5061 Set_Etype (Formal_Type, Formal_Type);
5062 Set_Depends_On_Private
5063 (Formal_Type, Has_Private_Component (Formal_Type));
5064 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
5065 Set_Is_Access_Constant (Formal_Type, False);
5067 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
5068 -- the designated type comes from the limited view (for
5069 -- back-end purposes).
5071 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
5073 Layout_Type (Formal_Type);
5075 Discard :=
5076 Add_Extra_Formal
5077 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
5078 end;
5079 end;
5080 end if;
5081 end Create_Extra_Formals;
5083 -----------------------------
5084 -- Enter_Overloaded_Entity --
5085 -----------------------------
5087 procedure Enter_Overloaded_Entity (S : Entity_Id) is
5088 E : Entity_Id := Current_Entity_In_Scope (S);
5089 C_E : Entity_Id := Current_Entity (S);
5091 begin
5092 if Present (E) then
5093 Set_Has_Homonym (E);
5094 Set_Has_Homonym (S);
5095 end if;
5097 Set_Is_Immediately_Visible (S);
5098 Set_Scope (S, Current_Scope);
5100 -- Chain new entity if front of homonym in current scope, so that
5101 -- homonyms are contiguous.
5103 if Present (E)
5104 and then E /= C_E
5105 then
5106 while Homonym (C_E) /= E loop
5107 C_E := Homonym (C_E);
5108 end loop;
5110 Set_Homonym (C_E, S);
5112 else
5113 E := C_E;
5114 Set_Current_Entity (S);
5115 end if;
5117 Set_Homonym (S, E);
5119 Append_Entity (S, Current_Scope);
5120 Set_Public_Status (S);
5122 if Debug_Flag_E then
5123 Write_Str ("New overloaded entity chain: ");
5124 Write_Name (Chars (S));
5126 E := S;
5127 while Present (E) loop
5128 Write_Str (" "); Write_Int (Int (E));
5129 E := Homonym (E);
5130 end loop;
5132 Write_Eol;
5133 end if;
5135 -- Generate warning for hiding
5137 if Warn_On_Hiding
5138 and then Comes_From_Source (S)
5139 and then In_Extended_Main_Source_Unit (S)
5140 then
5141 E := S;
5142 loop
5143 E := Homonym (E);
5144 exit when No (E);
5146 -- Warn unless genuine overloading
5148 if (not Is_Overloadable (E) or else Subtype_Conformant (E, S))
5149 and then (Is_Immediately_Visible (E)
5150 or else
5151 Is_Potentially_Use_Visible (S))
5152 then
5153 Error_Msg_Sloc := Sloc (E);
5154 Error_Msg_N ("declaration of & hides one#?", S);
5155 end if;
5156 end loop;
5157 end if;
5158 end Enter_Overloaded_Entity;
5160 -----------------------------
5161 -- Find_Corresponding_Spec --
5162 -----------------------------
5164 function Find_Corresponding_Spec (N : Node_Id) return Entity_Id is
5165 Spec : constant Node_Id := Specification (N);
5166 Designator : constant Entity_Id := Defining_Entity (Spec);
5168 E : Entity_Id;
5170 begin
5171 E := Current_Entity (Designator);
5172 while Present (E) loop
5174 -- We are looking for a matching spec. It must have the same scope,
5175 -- and the same name, and either be type conformant, or be the case
5176 -- of a library procedure spec and its body (which belong to one
5177 -- another regardless of whether they are type conformant or not).
5179 if Scope (E) = Current_Scope then
5180 if Current_Scope = Standard_Standard
5181 or else (Ekind (E) = Ekind (Designator)
5182 and then Type_Conformant (E, Designator))
5183 then
5184 -- Within an instantiation, we know that spec and body are
5185 -- subtype conformant, because they were subtype conformant
5186 -- in the generic. We choose the subtype-conformant entity
5187 -- here as well, to resolve spurious ambiguities in the
5188 -- instance that were not present in the generic (i.e. when
5189 -- two different types are given the same actual). If we are
5190 -- looking for a spec to match a body, full conformance is
5191 -- expected.
5193 if In_Instance then
5194 Set_Convention (Designator, Convention (E));
5196 if Nkind (N) = N_Subprogram_Body
5197 and then Present (Homonym (E))
5198 and then not Fully_Conformant (E, Designator)
5199 then
5200 goto Next_Entity;
5202 elsif not Subtype_Conformant (E, Designator) then
5203 goto Next_Entity;
5204 end if;
5205 end if;
5207 if not Has_Completion (E) then
5209 if Nkind (N) /= N_Subprogram_Body_Stub then
5210 Set_Corresponding_Spec (N, E);
5211 end if;
5213 Set_Has_Completion (E);
5214 return E;
5216 elsif Nkind (Parent (N)) = N_Subunit then
5218 -- If this is the proper body of a subunit, the completion
5219 -- flag is set when analyzing the stub.
5221 return E;
5223 -- If E is an internal function with a controlling result
5224 -- that was created for an operation inherited by a null
5225 -- extension, it may be overridden by a body without a previous
5226 -- spec (one more reason why these should be shunned). In that
5227 -- case remove the generated body, because the current one is
5228 -- the explicit overriding.
5230 elsif Ekind (E) = E_Function
5231 and then Ada_Version >= Ada_05
5232 and then not Comes_From_Source (E)
5233 and then Has_Controlling_Result (E)
5234 and then Is_Null_Extension (Etype (E))
5235 and then Comes_From_Source (Spec)
5236 then
5237 Set_Has_Completion (E, False);
5239 if Expander_Active then
5240 Remove
5241 (Unit_Declaration_Node
5242 (Corresponding_Body (Unit_Declaration_Node (E))));
5243 return E;
5245 -- If expansion is disabled, the wrapper function has not
5246 -- been generated, and this is the standard case of a late
5247 -- body overriding an inherited operation.
5249 else
5250 return Empty;
5251 end if;
5253 -- If body already exists, this is an error unless the
5254 -- previous declaration is the implicit declaration of
5255 -- a derived subprogram, or this is a spurious overloading
5256 -- in an instance.
5258 elsif No (Alias (E))
5259 and then not Is_Intrinsic_Subprogram (E)
5260 and then not In_Instance
5261 then
5262 Error_Msg_Sloc := Sloc (E);
5263 if Is_Imported (E) then
5264 Error_Msg_NE
5265 ("body not allowed for imported subprogram & declared#",
5266 N, E);
5267 else
5268 Error_Msg_NE ("duplicate body for & declared#", N, E);
5269 end if;
5270 end if;
5272 elsif Is_Child_Unit (E)
5273 and then
5274 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
5275 and then
5276 Nkind (Parent (Unit_Declaration_Node (Designator))) =
5277 N_Compilation_Unit
5278 then
5279 -- Child units cannot be overloaded, so a conformance mismatch
5280 -- between body and a previous spec is an error.
5282 Error_Msg_N
5283 ("body of child unit does not match previous declaration", N);
5284 end if;
5285 end if;
5287 <<Next_Entity>>
5288 E := Homonym (E);
5289 end loop;
5291 -- On exit, we know that no previous declaration of subprogram exists
5293 return Empty;
5294 end Find_Corresponding_Spec;
5296 ----------------------
5297 -- Fully_Conformant --
5298 ----------------------
5300 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5301 Result : Boolean;
5302 begin
5303 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
5304 return Result;
5305 end Fully_Conformant;
5307 ----------------------------------
5308 -- Fully_Conformant_Expressions --
5309 ----------------------------------
5311 function Fully_Conformant_Expressions
5312 (Given_E1 : Node_Id;
5313 Given_E2 : Node_Id) return Boolean
5315 E1 : constant Node_Id := Original_Node (Given_E1);
5316 E2 : constant Node_Id := Original_Node (Given_E2);
5317 -- We always test conformance on original nodes, since it is possible
5318 -- for analysis and/or expansion to make things look as though they
5319 -- conform when they do not, e.g. by converting 1+2 into 3.
5321 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
5322 renames Fully_Conformant_Expressions;
5324 function FCL (L1, L2 : List_Id) return Boolean;
5325 -- Compare elements of two lists for conformance. Elements have to
5326 -- be conformant, and actuals inserted as default parameters do not
5327 -- match explicit actuals with the same value.
5329 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
5330 -- Compare an operator node with a function call
5332 ---------
5333 -- FCL --
5334 ---------
5336 function FCL (L1, L2 : List_Id) return Boolean is
5337 N1, N2 : Node_Id;
5339 begin
5340 if L1 = No_List then
5341 N1 := Empty;
5342 else
5343 N1 := First (L1);
5344 end if;
5346 if L2 = No_List then
5347 N2 := Empty;
5348 else
5349 N2 := First (L2);
5350 end if;
5352 -- Compare two lists, skipping rewrite insertions (we want to
5353 -- compare the original trees, not the expanded versions!)
5355 loop
5356 if Is_Rewrite_Insertion (N1) then
5357 Next (N1);
5358 elsif Is_Rewrite_Insertion (N2) then
5359 Next (N2);
5360 elsif No (N1) then
5361 return No (N2);
5362 elsif No (N2) then
5363 return False;
5364 elsif not FCE (N1, N2) then
5365 return False;
5366 else
5367 Next (N1);
5368 Next (N2);
5369 end if;
5370 end loop;
5371 end FCL;
5373 ---------
5374 -- FCO --
5375 ---------
5377 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
5378 Actuals : constant List_Id := Parameter_Associations (Call_Node);
5379 Act : Node_Id;
5381 begin
5382 if No (Actuals)
5383 or else Entity (Op_Node) /= Entity (Name (Call_Node))
5384 then
5385 return False;
5387 else
5388 Act := First (Actuals);
5390 if Nkind (Op_Node) in N_Binary_Op then
5392 if not FCE (Left_Opnd (Op_Node), Act) then
5393 return False;
5394 end if;
5396 Next (Act);
5397 end if;
5399 return Present (Act)
5400 and then FCE (Right_Opnd (Op_Node), Act)
5401 and then No (Next (Act));
5402 end if;
5403 end FCO;
5405 -- Start of processing for Fully_Conformant_Expressions
5407 begin
5408 -- Non-conformant if paren count does not match. Note: if some idiot
5409 -- complains that we don't do this right for more than 3 levels of
5410 -- parentheses, they will be treated with the respect they deserve!
5412 if Paren_Count (E1) /= Paren_Count (E2) then
5413 return False;
5415 -- If same entities are referenced, then they are conformant even if
5416 -- they have different forms (RM 8.3.1(19-20)).
5418 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
5419 if Present (Entity (E1)) then
5420 return Entity (E1) = Entity (E2)
5421 or else (Chars (Entity (E1)) = Chars (Entity (E2))
5422 and then Ekind (Entity (E1)) = E_Discriminant
5423 and then Ekind (Entity (E2)) = E_In_Parameter);
5425 elsif Nkind (E1) = N_Expanded_Name
5426 and then Nkind (E2) = N_Expanded_Name
5427 and then Nkind (Selector_Name (E1)) = N_Character_Literal
5428 and then Nkind (Selector_Name (E2)) = N_Character_Literal
5429 then
5430 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
5432 else
5433 -- Identifiers in component associations don't always have
5434 -- entities, but their names must conform.
5436 return Nkind (E1) = N_Identifier
5437 and then Nkind (E2) = N_Identifier
5438 and then Chars (E1) = Chars (E2);
5439 end if;
5441 elsif Nkind (E1) = N_Character_Literal
5442 and then Nkind (E2) = N_Expanded_Name
5443 then
5444 return Nkind (Selector_Name (E2)) = N_Character_Literal
5445 and then Chars (E1) = Chars (Selector_Name (E2));
5447 elsif Nkind (E2) = N_Character_Literal
5448 and then Nkind (E1) = N_Expanded_Name
5449 then
5450 return Nkind (Selector_Name (E1)) = N_Character_Literal
5451 and then Chars (E2) = Chars (Selector_Name (E1));
5453 elsif Nkind (E1) in N_Op
5454 and then Nkind (E2) = N_Function_Call
5455 then
5456 return FCO (E1, E2);
5458 elsif Nkind (E2) in N_Op
5459 and then Nkind (E1) = N_Function_Call
5460 then
5461 return FCO (E2, E1);
5463 -- Otherwise we must have the same syntactic entity
5465 elsif Nkind (E1) /= Nkind (E2) then
5466 return False;
5468 -- At this point, we specialize by node type
5470 else
5471 case Nkind (E1) is
5473 when N_Aggregate =>
5474 return
5475 FCL (Expressions (E1), Expressions (E2))
5476 and then FCL (Component_Associations (E1),
5477 Component_Associations (E2));
5479 when N_Allocator =>
5480 if Nkind (Expression (E1)) = N_Qualified_Expression
5481 or else
5482 Nkind (Expression (E2)) = N_Qualified_Expression
5483 then
5484 return FCE (Expression (E1), Expression (E2));
5486 -- Check that the subtype marks and any constraints
5487 -- are conformant
5489 else
5490 declare
5491 Indic1 : constant Node_Id := Expression (E1);
5492 Indic2 : constant Node_Id := Expression (E2);
5493 Elt1 : Node_Id;
5494 Elt2 : Node_Id;
5496 begin
5497 if Nkind (Indic1) /= N_Subtype_Indication then
5498 return
5499 Nkind (Indic2) /= N_Subtype_Indication
5500 and then Entity (Indic1) = Entity (Indic2);
5502 elsif Nkind (Indic2) /= N_Subtype_Indication then
5503 return
5504 Nkind (Indic1) /= N_Subtype_Indication
5505 and then Entity (Indic1) = Entity (Indic2);
5507 else
5508 if Entity (Subtype_Mark (Indic1)) /=
5509 Entity (Subtype_Mark (Indic2))
5510 then
5511 return False;
5512 end if;
5514 Elt1 := First (Constraints (Constraint (Indic1)));
5515 Elt2 := First (Constraints (Constraint (Indic2)));
5517 while Present (Elt1) and then Present (Elt2) loop
5518 if not FCE (Elt1, Elt2) then
5519 return False;
5520 end if;
5522 Next (Elt1);
5523 Next (Elt2);
5524 end loop;
5526 return True;
5527 end if;
5528 end;
5529 end if;
5531 when N_Attribute_Reference =>
5532 return
5533 Attribute_Name (E1) = Attribute_Name (E2)
5534 and then FCL (Expressions (E1), Expressions (E2));
5536 when N_Binary_Op =>
5537 return
5538 Entity (E1) = Entity (E2)
5539 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
5540 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
5542 when N_And_Then | N_Or_Else | N_Membership_Test =>
5543 return
5544 FCE (Left_Opnd (E1), Left_Opnd (E2))
5545 and then
5546 FCE (Right_Opnd (E1), Right_Opnd (E2));
5548 when N_Character_Literal =>
5549 return
5550 Char_Literal_Value (E1) = Char_Literal_Value (E2);
5552 when N_Component_Association =>
5553 return
5554 FCL (Choices (E1), Choices (E2))
5555 and then FCE (Expression (E1), Expression (E2));
5557 when N_Conditional_Expression =>
5558 return
5559 FCL (Expressions (E1), Expressions (E2));
5561 when N_Explicit_Dereference =>
5562 return
5563 FCE (Prefix (E1), Prefix (E2));
5565 when N_Extension_Aggregate =>
5566 return
5567 FCL (Expressions (E1), Expressions (E2))
5568 and then Null_Record_Present (E1) =
5569 Null_Record_Present (E2)
5570 and then FCL (Component_Associations (E1),
5571 Component_Associations (E2));
5573 when N_Function_Call =>
5574 return
5575 FCE (Name (E1), Name (E2))
5576 and then FCL (Parameter_Associations (E1),
5577 Parameter_Associations (E2));
5579 when N_Indexed_Component =>
5580 return
5581 FCE (Prefix (E1), Prefix (E2))
5582 and then FCL (Expressions (E1), Expressions (E2));
5584 when N_Integer_Literal =>
5585 return (Intval (E1) = Intval (E2));
5587 when N_Null =>
5588 return True;
5590 when N_Operator_Symbol =>
5591 return
5592 Chars (E1) = Chars (E2);
5594 when N_Others_Choice =>
5595 return True;
5597 when N_Parameter_Association =>
5598 return
5599 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
5600 and then FCE (Explicit_Actual_Parameter (E1),
5601 Explicit_Actual_Parameter (E2));
5603 when N_Qualified_Expression =>
5604 return
5605 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
5606 and then FCE (Expression (E1), Expression (E2));
5608 when N_Range =>
5609 return
5610 FCE (Low_Bound (E1), Low_Bound (E2))
5611 and then FCE (High_Bound (E1), High_Bound (E2));
5613 when N_Real_Literal =>
5614 return (Realval (E1) = Realval (E2));
5616 when N_Selected_Component =>
5617 return
5618 FCE (Prefix (E1), Prefix (E2))
5619 and then FCE (Selector_Name (E1), Selector_Name (E2));
5621 when N_Slice =>
5622 return
5623 FCE (Prefix (E1), Prefix (E2))
5624 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
5626 when N_String_Literal =>
5627 declare
5628 S1 : constant String_Id := Strval (E1);
5629 S2 : constant String_Id := Strval (E2);
5630 L1 : constant Nat := String_Length (S1);
5631 L2 : constant Nat := String_Length (S2);
5633 begin
5634 if L1 /= L2 then
5635 return False;
5637 else
5638 for J in 1 .. L1 loop
5639 if Get_String_Char (S1, J) /=
5640 Get_String_Char (S2, J)
5641 then
5642 return False;
5643 end if;
5644 end loop;
5646 return True;
5647 end if;
5648 end;
5650 when N_Type_Conversion =>
5651 return
5652 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
5653 and then FCE (Expression (E1), Expression (E2));
5655 when N_Unary_Op =>
5656 return
5657 Entity (E1) = Entity (E2)
5658 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
5660 when N_Unchecked_Type_Conversion =>
5661 return
5662 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
5663 and then FCE (Expression (E1), Expression (E2));
5665 -- All other node types cannot appear in this context. Strictly
5666 -- we should raise a fatal internal error. Instead we just ignore
5667 -- the nodes. This means that if anyone makes a mistake in the
5668 -- expander and mucks an expression tree irretrievably, the
5669 -- result will be a failure to detect a (probably very obscure)
5670 -- case of non-conformance, which is better than bombing on some
5671 -- case where two expressions do in fact conform.
5673 when others =>
5674 return True;
5676 end case;
5677 end if;
5678 end Fully_Conformant_Expressions;
5680 ----------------------------------------
5681 -- Fully_Conformant_Discrete_Subtypes --
5682 ----------------------------------------
5684 function Fully_Conformant_Discrete_Subtypes
5685 (Given_S1 : Node_Id;
5686 Given_S2 : Node_Id) return Boolean
5688 S1 : constant Node_Id := Original_Node (Given_S1);
5689 S2 : constant Node_Id := Original_Node (Given_S2);
5691 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
5692 -- Special-case for a bound given by a discriminant, which in the body
5693 -- is replaced with the discriminal of the enclosing type.
5695 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
5696 -- Check both bounds
5698 -----------------------
5699 -- Conforming_Bounds --
5700 -----------------------
5702 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
5703 begin
5704 if Is_Entity_Name (B1)
5705 and then Is_Entity_Name (B2)
5706 and then Ekind (Entity (B1)) = E_Discriminant
5707 then
5708 return Chars (B1) = Chars (B2);
5710 else
5711 return Fully_Conformant_Expressions (B1, B2);
5712 end if;
5713 end Conforming_Bounds;
5715 -----------------------
5716 -- Conforming_Ranges --
5717 -----------------------
5719 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
5720 begin
5721 return
5722 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
5723 and then
5724 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
5725 end Conforming_Ranges;
5727 -- Start of processing for Fully_Conformant_Discrete_Subtypes
5729 begin
5730 if Nkind (S1) /= Nkind (S2) then
5731 return False;
5733 elsif Is_Entity_Name (S1) then
5734 return Entity (S1) = Entity (S2);
5736 elsif Nkind (S1) = N_Range then
5737 return Conforming_Ranges (S1, S2);
5739 elsif Nkind (S1) = N_Subtype_Indication then
5740 return
5741 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
5742 and then
5743 Conforming_Ranges
5744 (Range_Expression (Constraint (S1)),
5745 Range_Expression (Constraint (S2)));
5746 else
5747 return True;
5748 end if;
5749 end Fully_Conformant_Discrete_Subtypes;
5751 --------------------
5752 -- Install_Entity --
5753 --------------------
5755 procedure Install_Entity (E : Entity_Id) is
5756 Prev : constant Entity_Id := Current_Entity (E);
5757 begin
5758 Set_Is_Immediately_Visible (E);
5759 Set_Current_Entity (E);
5760 Set_Homonym (E, Prev);
5761 end Install_Entity;
5763 ---------------------
5764 -- Install_Formals --
5765 ---------------------
5767 procedure Install_Formals (Id : Entity_Id) is
5768 F : Entity_Id;
5769 begin
5770 F := First_Formal (Id);
5771 while Present (F) loop
5772 Install_Entity (F);
5773 Next_Formal (F);
5774 end loop;
5775 end Install_Formals;
5777 -----------------------------
5778 -- Is_Interface_Conformant --
5779 -----------------------------
5781 function Is_Interface_Conformant
5782 (Tagged_Type : Entity_Id;
5783 Iface_Prim : Entity_Id;
5784 Prim : Entity_Id) return Boolean
5786 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
5787 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
5789 begin
5790 pragma Assert (Is_Subprogram (Iface_Prim)
5791 and then Is_Subprogram (Prim)
5792 and then Is_Dispatching_Operation (Iface_Prim)
5793 and then Is_Dispatching_Operation (Prim));
5795 pragma Assert (Is_Interface (Iface)
5796 or else (Present (Alias (Iface_Prim))
5797 and then
5798 Is_Interface
5799 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
5801 if Prim = Iface_Prim
5802 or else not Is_Subprogram (Prim)
5803 or else Ekind (Prim) /= Ekind (Iface_Prim)
5804 or else not Is_Dispatching_Operation (Prim)
5805 or else Scope (Prim) /= Scope (Tagged_Type)
5806 or else No (Typ)
5807 or else Base_Type (Typ) /= Tagged_Type
5808 or else not Primitive_Names_Match (Iface_Prim, Prim)
5809 then
5810 return False;
5812 -- Case of a procedure, or a function that does not have a controlling
5813 -- result (I or access I).
5815 elsif Ekind (Iface_Prim) = E_Procedure
5816 or else Etype (Prim) = Etype (Iface_Prim)
5817 or else not Has_Controlling_Result (Prim)
5818 then
5819 return Type_Conformant (Prim, Iface_Prim,
5820 Skip_Controlling_Formals => True);
5822 -- Case of a function returning an interface, or an access to one.
5823 -- Check that the return types correspond.
5825 elsif Implements_Interface (Typ, Iface) then
5826 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
5828 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
5829 then
5830 return False;
5831 else
5832 return
5833 Type_Conformant (Prim, Iface_Prim,
5834 Skip_Controlling_Formals => True);
5835 end if;
5837 else
5838 return False;
5839 end if;
5840 end Is_Interface_Conformant;
5842 ---------------------------------
5843 -- Is_Non_Overriding_Operation --
5844 ---------------------------------
5846 function Is_Non_Overriding_Operation
5847 (Prev_E : Entity_Id;
5848 New_E : Entity_Id) return Boolean
5850 Formal : Entity_Id;
5851 F_Typ : Entity_Id;
5852 G_Typ : Entity_Id := Empty;
5854 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
5855 -- If F_Type is a derived type associated with a generic actual subtype,
5856 -- then return its Generic_Parent_Type attribute, else return Empty.
5858 function Types_Correspond
5859 (P_Type : Entity_Id;
5860 N_Type : Entity_Id) return Boolean;
5861 -- Returns true if and only if the types (or designated types in the
5862 -- case of anonymous access types) are the same or N_Type is derived
5863 -- directly or indirectly from P_Type.
5865 -----------------------------
5866 -- Get_Generic_Parent_Type --
5867 -----------------------------
5869 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
5870 G_Typ : Entity_Id;
5871 Indic : Node_Id;
5873 begin
5874 if Is_Derived_Type (F_Typ)
5875 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
5876 then
5877 -- The tree must be traversed to determine the parent subtype in
5878 -- the generic unit, which unfortunately isn't always available
5879 -- via semantic attributes. ??? (Note: The use of Original_Node
5880 -- is needed for cases where a full derived type has been
5881 -- rewritten.)
5883 Indic := Subtype_Indication
5884 (Type_Definition (Original_Node (Parent (F_Typ))));
5886 if Nkind (Indic) = N_Subtype_Indication then
5887 G_Typ := Entity (Subtype_Mark (Indic));
5888 else
5889 G_Typ := Entity (Indic);
5890 end if;
5892 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
5893 and then Present (Generic_Parent_Type (Parent (G_Typ)))
5894 then
5895 return Generic_Parent_Type (Parent (G_Typ));
5896 end if;
5897 end if;
5899 return Empty;
5900 end Get_Generic_Parent_Type;
5902 ----------------------
5903 -- Types_Correspond --
5904 ----------------------
5906 function Types_Correspond
5907 (P_Type : Entity_Id;
5908 N_Type : Entity_Id) return Boolean
5910 Prev_Type : Entity_Id := Base_Type (P_Type);
5911 New_Type : Entity_Id := Base_Type (N_Type);
5913 begin
5914 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
5915 Prev_Type := Designated_Type (Prev_Type);
5916 end if;
5918 if Ekind (New_Type) = E_Anonymous_Access_Type then
5919 New_Type := Designated_Type (New_Type);
5920 end if;
5922 if Prev_Type = New_Type then
5923 return True;
5925 elsif not Is_Class_Wide_Type (New_Type) then
5926 while Etype (New_Type) /= New_Type loop
5927 New_Type := Etype (New_Type);
5928 if New_Type = Prev_Type then
5929 return True;
5930 end if;
5931 end loop;
5932 end if;
5933 return False;
5934 end Types_Correspond;
5936 -- Start of processing for Is_Non_Overriding_Operation
5938 begin
5939 -- In the case where both operations are implicit derived subprograms
5940 -- then neither overrides the other. This can only occur in certain
5941 -- obscure cases (e.g., derivation from homographs created in a generic
5942 -- instantiation).
5944 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
5945 return True;
5947 elsif Ekind (Current_Scope) = E_Package
5948 and then Is_Generic_Instance (Current_Scope)
5949 and then In_Private_Part (Current_Scope)
5950 and then Comes_From_Source (New_E)
5951 then
5952 -- We examine the formals and result subtype of the inherited
5953 -- operation, to determine whether their type is derived from (the
5954 -- instance of) a generic type.
5956 Formal := First_Formal (Prev_E);
5958 while Present (Formal) loop
5959 F_Typ := Base_Type (Etype (Formal));
5961 if Ekind (F_Typ) = E_Anonymous_Access_Type then
5962 F_Typ := Designated_Type (F_Typ);
5963 end if;
5965 G_Typ := Get_Generic_Parent_Type (F_Typ);
5967 Next_Formal (Formal);
5968 end loop;
5970 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
5971 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
5972 end if;
5974 if No (G_Typ) then
5975 return False;
5976 end if;
5978 -- If the generic type is a private type, then the original
5979 -- operation was not overriding in the generic, because there was
5980 -- no primitive operation to override.
5982 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
5983 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
5984 N_Formal_Private_Type_Definition
5985 then
5986 return True;
5988 -- The generic parent type is the ancestor of a formal derived
5989 -- type declaration. We need to check whether it has a primitive
5990 -- operation that should be overridden by New_E in the generic.
5992 else
5993 declare
5994 P_Formal : Entity_Id;
5995 N_Formal : Entity_Id;
5996 P_Typ : Entity_Id;
5997 N_Typ : Entity_Id;
5998 P_Prim : Entity_Id;
5999 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
6001 begin
6002 while Present (Prim_Elt) loop
6003 P_Prim := Node (Prim_Elt);
6005 if Chars (P_Prim) = Chars (New_E)
6006 and then Ekind (P_Prim) = Ekind (New_E)
6007 then
6008 P_Formal := First_Formal (P_Prim);
6009 N_Formal := First_Formal (New_E);
6010 while Present (P_Formal) and then Present (N_Formal) loop
6011 P_Typ := Etype (P_Formal);
6012 N_Typ := Etype (N_Formal);
6014 if not Types_Correspond (P_Typ, N_Typ) then
6015 exit;
6016 end if;
6018 Next_Entity (P_Formal);
6019 Next_Entity (N_Formal);
6020 end loop;
6022 -- Found a matching primitive operation belonging to the
6023 -- formal ancestor type, so the new subprogram is
6024 -- overriding.
6026 if No (P_Formal)
6027 and then No (N_Formal)
6028 and then (Ekind (New_E) /= E_Function
6029 or else
6030 Types_Correspond
6031 (Etype (P_Prim), Etype (New_E)))
6032 then
6033 return False;
6034 end if;
6035 end if;
6037 Next_Elmt (Prim_Elt);
6038 end loop;
6040 -- If no match found, then the new subprogram does not
6041 -- override in the generic (nor in the instance).
6043 return True;
6044 end;
6045 end if;
6046 else
6047 return False;
6048 end if;
6049 end Is_Non_Overriding_Operation;
6051 ------------------------------
6052 -- Make_Inequality_Operator --
6053 ------------------------------
6055 -- S is the defining identifier of an equality operator. We build a
6056 -- subprogram declaration with the right signature. This operation is
6057 -- intrinsic, because it is always expanded as the negation of the
6058 -- call to the equality function.
6060 procedure Make_Inequality_Operator (S : Entity_Id) is
6061 Loc : constant Source_Ptr := Sloc (S);
6062 Decl : Node_Id;
6063 Formals : List_Id;
6064 Op_Name : Entity_Id;
6066 FF : constant Entity_Id := First_Formal (S);
6067 NF : constant Entity_Id := Next_Formal (FF);
6069 begin
6070 -- Check that equality was properly defined, ignore call if not
6072 if No (NF) then
6073 return;
6074 end if;
6076 declare
6077 A : constant Entity_Id :=
6078 Make_Defining_Identifier (Sloc (FF),
6079 Chars => Chars (FF));
6081 B : constant Entity_Id :=
6082 Make_Defining_Identifier (Sloc (NF),
6083 Chars => Chars (NF));
6085 begin
6086 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
6088 Formals := New_List (
6089 Make_Parameter_Specification (Loc,
6090 Defining_Identifier => A,
6091 Parameter_Type =>
6092 New_Reference_To (Etype (First_Formal (S)),
6093 Sloc (Etype (First_Formal (S))))),
6095 Make_Parameter_Specification (Loc,
6096 Defining_Identifier => B,
6097 Parameter_Type =>
6098 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
6099 Sloc (Etype (Next_Formal (First_Formal (S)))))));
6101 Decl :=
6102 Make_Subprogram_Declaration (Loc,
6103 Specification =>
6104 Make_Function_Specification (Loc,
6105 Defining_Unit_Name => Op_Name,
6106 Parameter_Specifications => Formals,
6107 Result_Definition =>
6108 New_Reference_To (Standard_Boolean, Loc)));
6110 -- Insert inequality right after equality if it is explicit or after
6111 -- the derived type when implicit. These entities are created only
6112 -- for visibility purposes, and eventually replaced in the course of
6113 -- expansion, so they do not need to be attached to the tree and seen
6114 -- by the back-end. Keeping them internal also avoids spurious
6115 -- freezing problems. The declaration is inserted in the tree for
6116 -- analysis, and removed afterwards. If the equality operator comes
6117 -- from an explicit declaration, attach the inequality immediately
6118 -- after. Else the equality is inherited from a derived type
6119 -- declaration, so insert inequality after that declaration.
6121 if No (Alias (S)) then
6122 Insert_After (Unit_Declaration_Node (S), Decl);
6123 elsif Is_List_Member (Parent (S)) then
6124 Insert_After (Parent (S), Decl);
6125 else
6126 Insert_After (Parent (Etype (First_Formal (S))), Decl);
6127 end if;
6129 Mark_Rewrite_Insertion (Decl);
6130 Set_Is_Intrinsic_Subprogram (Op_Name);
6131 Analyze (Decl);
6132 Remove (Decl);
6133 Set_Has_Completion (Op_Name);
6134 Set_Corresponding_Equality (Op_Name, S);
6135 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
6136 end;
6137 end Make_Inequality_Operator;
6139 ----------------------
6140 -- May_Need_Actuals --
6141 ----------------------
6143 procedure May_Need_Actuals (Fun : Entity_Id) is
6144 F : Entity_Id;
6145 B : Boolean;
6147 begin
6148 F := First_Formal (Fun);
6149 B := True;
6150 while Present (F) loop
6151 if No (Default_Value (F)) then
6152 B := False;
6153 exit;
6154 end if;
6156 Next_Formal (F);
6157 end loop;
6159 Set_Needs_No_Actuals (Fun, B);
6160 end May_Need_Actuals;
6162 ---------------------
6163 -- Mode_Conformant --
6164 ---------------------
6166 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6167 Result : Boolean;
6168 begin
6169 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
6170 return Result;
6171 end Mode_Conformant;
6173 ---------------------------
6174 -- New_Overloaded_Entity --
6175 ---------------------------
6177 procedure New_Overloaded_Entity
6178 (S : Entity_Id;
6179 Derived_Type : Entity_Id := Empty)
6181 Overridden_Subp : Entity_Id := Empty;
6182 -- Set if the current scope has an operation that is type-conformant
6183 -- with S, and becomes hidden by S.
6185 Is_Primitive_Subp : Boolean;
6186 -- Set to True if the new subprogram is primitive
6188 E : Entity_Id;
6189 -- Entity that S overrides
6191 Prev_Vis : Entity_Id := Empty;
6192 -- Predecessor of E in Homonym chain
6194 procedure Check_For_Primitive_Subprogram
6195 (Is_Primitive : out Boolean;
6196 Is_Overriding : Boolean := False);
6197 -- If the subprogram being analyzed is a primitive operation of the type
6198 -- of a formal or result, set the Has_Primitive_Operations flag on the
6199 -- type, and set Is_Primitive to True (otherwise set to False). Set the
6200 -- corresponding flag on the entity itself for later use.
6202 procedure Check_Synchronized_Overriding
6203 (Def_Id : Entity_Id;
6204 Overridden_Subp : out Entity_Id);
6205 -- First determine if Def_Id is an entry or a subprogram either defined
6206 -- in the scope of a task or protected type, or is a primitive of such
6207 -- a type. Check whether Def_Id overrides a subprogram of an interface
6208 -- implemented by the synchronized type, return the overridden entity
6209 -- or Empty.
6211 function Is_Private_Declaration (E : Entity_Id) return Boolean;
6212 -- Check that E is declared in the private part of the current package,
6213 -- or in the package body, where it may hide a previous declaration.
6214 -- We can't use In_Private_Part by itself because this flag is also
6215 -- set when freezing entities, so we must examine the place of the
6216 -- declaration in the tree, and recognize wrapper packages as well.
6218 ------------------------------------
6219 -- Check_For_Primitive_Subprogram --
6220 ------------------------------------
6222 procedure Check_For_Primitive_Subprogram
6223 (Is_Primitive : out Boolean;
6224 Is_Overriding : Boolean := False)
6226 Formal : Entity_Id;
6227 F_Typ : Entity_Id;
6228 B_Typ : Entity_Id;
6230 function Visible_Part_Type (T : Entity_Id) return Boolean;
6231 -- Returns true if T is declared in the visible part of
6232 -- the current package scope; otherwise returns false.
6233 -- Assumes that T is declared in a package.
6235 procedure Check_Private_Overriding (T : Entity_Id);
6236 -- Checks that if a primitive abstract subprogram of a visible
6237 -- abstract type is declared in a private part, then it must
6238 -- override an abstract subprogram declared in the visible part.
6239 -- Also checks that if a primitive function with a controlling
6240 -- result is declared in a private part, then it must override
6241 -- a function declared in the visible part.
6243 ------------------------------
6244 -- Check_Private_Overriding --
6245 ------------------------------
6247 procedure Check_Private_Overriding (T : Entity_Id) is
6248 begin
6249 if Ekind (Current_Scope) = E_Package
6250 and then In_Private_Part (Current_Scope)
6251 and then Visible_Part_Type (T)
6252 and then not In_Instance
6253 then
6254 if Is_Abstract_Type (T)
6255 and then Is_Abstract_Subprogram (S)
6256 and then (not Is_Overriding
6257 or else not Is_Abstract_Subprogram (E))
6258 then
6259 Error_Msg_N ("abstract subprograms must be visible "
6260 & "(RM 3.9.3(10))!", S);
6262 elsif Ekind (S) = E_Function
6263 and then Is_Tagged_Type (T)
6264 and then T = Base_Type (Etype (S))
6265 and then not Is_Overriding
6266 then
6267 Error_Msg_N
6268 ("private function with tagged result must"
6269 & " override visible-part function", S);
6270 Error_Msg_N
6271 ("\move subprogram to the visible part"
6272 & " (RM 3.9.3(10))", S);
6273 end if;
6274 end if;
6275 end Check_Private_Overriding;
6277 -----------------------
6278 -- Visible_Part_Type --
6279 -----------------------
6281 function Visible_Part_Type (T : Entity_Id) return Boolean is
6282 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
6283 N : Node_Id;
6285 begin
6286 -- If the entity is a private type, then it must be
6287 -- declared in a visible part.
6289 if Ekind (T) in Private_Kind then
6290 return True;
6291 end if;
6293 -- Otherwise, we traverse the visible part looking for its
6294 -- corresponding declaration. We cannot use the declaration
6295 -- node directly because in the private part the entity of a
6296 -- private type is the one in the full view, which does not
6297 -- indicate that it is the completion of something visible.
6299 N := First (Visible_Declarations (Specification (P)));
6300 while Present (N) loop
6301 if Nkind (N) = N_Full_Type_Declaration
6302 and then Present (Defining_Identifier (N))
6303 and then T = Defining_Identifier (N)
6304 then
6305 return True;
6307 elsif Nkind_In (N, N_Private_Type_Declaration,
6308 N_Private_Extension_Declaration)
6309 and then Present (Defining_Identifier (N))
6310 and then T = Full_View (Defining_Identifier (N))
6311 then
6312 return True;
6313 end if;
6315 Next (N);
6316 end loop;
6318 return False;
6319 end Visible_Part_Type;
6321 -- Start of processing for Check_For_Primitive_Subprogram
6323 begin
6324 Is_Primitive := False;
6326 if not Comes_From_Source (S) then
6327 null;
6329 -- If subprogram is at library level, it is not primitive operation
6331 elsif Current_Scope = Standard_Standard then
6332 null;
6334 elsif ((Ekind (Current_Scope) = E_Package
6335 or else Ekind (Current_Scope) = E_Generic_Package)
6336 and then not In_Package_Body (Current_Scope))
6337 or else Is_Overriding
6338 then
6339 -- For function, check return type
6341 if Ekind (S) = E_Function then
6342 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
6343 F_Typ := Designated_Type (Etype (S));
6344 else
6345 F_Typ := Etype (S);
6346 end if;
6348 B_Typ := Base_Type (F_Typ);
6350 if Scope (B_Typ) = Current_Scope
6351 and then not Is_Class_Wide_Type (B_Typ)
6352 and then not Is_Generic_Type (B_Typ)
6353 then
6354 Is_Primitive := True;
6355 Set_Has_Primitive_Operations (B_Typ);
6356 Set_Is_Primitive (S);
6357 Check_Private_Overriding (B_Typ);
6358 end if;
6359 end if;
6361 -- For all subprograms, check formals
6363 Formal := First_Formal (S);
6364 while Present (Formal) loop
6365 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
6366 F_Typ := Designated_Type (Etype (Formal));
6367 else
6368 F_Typ := Etype (Formal);
6369 end if;
6371 B_Typ := Base_Type (F_Typ);
6373 if Ekind (B_Typ) = E_Access_Subtype then
6374 B_Typ := Base_Type (B_Typ);
6375 end if;
6377 if Scope (B_Typ) = Current_Scope
6378 and then not Is_Class_Wide_Type (B_Typ)
6379 and then not Is_Generic_Type (B_Typ)
6380 then
6381 Is_Primitive := True;
6382 Set_Is_Primitive (S);
6383 Set_Has_Primitive_Operations (B_Typ);
6384 Check_Private_Overriding (B_Typ);
6385 end if;
6387 Next_Formal (Formal);
6388 end loop;
6389 end if;
6390 end Check_For_Primitive_Subprogram;
6392 -----------------------------------
6393 -- Check_Synchronized_Overriding --
6394 -----------------------------------
6396 procedure Check_Synchronized_Overriding
6397 (Def_Id : Entity_Id;
6398 Overridden_Subp : out Entity_Id)
6400 Ifaces_List : Elist_Id;
6401 In_Scope : Boolean;
6402 Typ : Entity_Id;
6404 function Has_Correct_Formal_Mode
6405 (Tag_Typ : Entity_Id;
6406 Subp : Entity_Id) return Boolean;
6407 -- For an overridden subprogram Subp, check whether the mode of its
6408 -- first parameter is correct depending on the kind of Tag_Typ.
6410 function Matches_Prefixed_View_Profile
6411 (Prim_Params : List_Id;
6412 Iface_Params : List_Id) return Boolean;
6413 -- Determine whether a subprogram's parameter profile Prim_Params
6414 -- matches that of a potentially overridden interface subprogram
6415 -- Iface_Params. Also determine if the type of first parameter of
6416 -- Iface_Params is an implemented interface.
6418 -----------------------------
6419 -- Has_Correct_Formal_Mode --
6420 -----------------------------
6422 function Has_Correct_Formal_Mode
6423 (Tag_Typ : Entity_Id;
6424 Subp : Entity_Id) return Boolean
6426 Formal : constant Node_Id := First_Formal (Subp);
6428 begin
6429 -- In order for an entry or a protected procedure to override, the
6430 -- first parameter of the overridden routine must be of mode
6431 -- "out", "in out" or access-to-variable.
6433 if (Ekind (Subp) = E_Entry
6434 or else Ekind (Subp) = E_Procedure)
6435 and then Is_Protected_Type (Tag_Typ)
6436 and then Ekind (Formal) /= E_In_Out_Parameter
6437 and then Ekind (Formal) /= E_Out_Parameter
6438 and then Nkind (Parameter_Type (Parent (Formal))) /=
6439 N_Access_Definition
6440 then
6441 return False;
6442 end if;
6444 -- All other cases are OK since a task entry or routine does not
6445 -- have a restriction on the mode of the first parameter of the
6446 -- overridden interface routine.
6448 return True;
6449 end Has_Correct_Formal_Mode;
6451 -----------------------------------
6452 -- Matches_Prefixed_View_Profile --
6453 -----------------------------------
6455 function Matches_Prefixed_View_Profile
6456 (Prim_Params : List_Id;
6457 Iface_Params : List_Id) return Boolean
6459 Iface_Id : Entity_Id;
6460 Iface_Param : Node_Id;
6461 Iface_Typ : Entity_Id;
6462 Prim_Id : Entity_Id;
6463 Prim_Param : Node_Id;
6464 Prim_Typ : Entity_Id;
6466 function Is_Implemented
6467 (Ifaces_List : Elist_Id;
6468 Iface : Entity_Id) return Boolean;
6469 -- Determine if Iface is implemented by the current task or
6470 -- protected type.
6472 --------------------
6473 -- Is_Implemented --
6474 --------------------
6476 function Is_Implemented
6477 (Ifaces_List : Elist_Id;
6478 Iface : Entity_Id) return Boolean
6480 Iface_Elmt : Elmt_Id;
6482 begin
6483 Iface_Elmt := First_Elmt (Ifaces_List);
6484 while Present (Iface_Elmt) loop
6485 if Node (Iface_Elmt) = Iface then
6486 return True;
6487 end if;
6489 Next_Elmt (Iface_Elmt);
6490 end loop;
6492 return False;
6493 end Is_Implemented;
6495 -- Start of processing for Matches_Prefixed_View_Profile
6497 begin
6498 Iface_Param := First (Iface_Params);
6499 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
6501 if Is_Access_Type (Iface_Typ) then
6502 Iface_Typ := Designated_Type (Iface_Typ);
6503 end if;
6505 Prim_Param := First (Prim_Params);
6507 -- The first parameter of the potentially overridden subprogram
6508 -- must be an interface implemented by Prim.
6510 if not Is_Interface (Iface_Typ)
6511 or else not Is_Implemented (Ifaces_List, Iface_Typ)
6512 then
6513 return False;
6514 end if;
6516 -- The checks on the object parameters are done, move onto the
6517 -- rest of the parameters.
6519 if not In_Scope then
6520 Prim_Param := Next (Prim_Param);
6521 end if;
6523 Iface_Param := Next (Iface_Param);
6524 while Present (Iface_Param) and then Present (Prim_Param) loop
6525 Iface_Id := Defining_Identifier (Iface_Param);
6526 Iface_Typ := Find_Parameter_Type (Iface_Param);
6528 if Is_Access_Type (Iface_Typ) then
6529 Iface_Typ := Directly_Designated_Type (Iface_Typ);
6530 end if;
6532 Prim_Id := Defining_Identifier (Prim_Param);
6533 Prim_Typ := Find_Parameter_Type (Prim_Param);
6535 if Is_Access_Type (Prim_Typ) then
6536 Prim_Typ := Directly_Designated_Type (Prim_Typ);
6537 end if;
6539 -- Case of multiple interface types inside a parameter profile
6541 -- (Obj_Param : in out Iface; ...; Param : Iface)
6543 -- If the interface type is implemented, then the matching type
6544 -- in the primitive should be the implementing record type.
6546 if Ekind (Iface_Typ) = E_Record_Type
6547 and then Is_Interface (Iface_Typ)
6548 and then Is_Implemented (Ifaces_List, Iface_Typ)
6549 then
6550 if Prim_Typ /= Typ then
6551 return False;
6552 end if;
6554 -- The two parameters must be both mode and subtype conformant
6556 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
6557 or else not
6558 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
6559 then
6560 return False;
6561 end if;
6563 Next (Iface_Param);
6564 Next (Prim_Param);
6565 end loop;
6567 -- One of the two lists contains more parameters than the other
6569 if Present (Iface_Param) or else Present (Prim_Param) then
6570 return False;
6571 end if;
6573 return True;
6574 end Matches_Prefixed_View_Profile;
6576 -- Start of processing for Check_Synchronized_Overriding
6578 begin
6579 Overridden_Subp := Empty;
6581 -- Def_Id must be an entry or a subprogram. We should skip predefined
6582 -- primitives internally generated by the frontend; however at this
6583 -- stage predefined primitives are still not fully decorated. As a
6584 -- minor optimization we skip here internally generated subprograms.
6586 if (Ekind (Def_Id) /= E_Entry
6587 and then Ekind (Def_Id) /= E_Function
6588 and then Ekind (Def_Id) /= E_Procedure)
6589 or else not Comes_From_Source (Def_Id)
6590 then
6591 return;
6592 end if;
6594 -- Search for the concurrent declaration since it contains the list
6595 -- of all implemented interfaces. In this case, the subprogram is
6596 -- declared within the scope of a protected or a task type.
6598 if Present (Scope (Def_Id))
6599 and then Is_Concurrent_Type (Scope (Def_Id))
6600 and then not Is_Generic_Actual_Type (Scope (Def_Id))
6601 then
6602 Typ := Scope (Def_Id);
6603 In_Scope := True;
6605 -- The enclosing scope is not a synchronized type and the subprogram
6606 -- has no formals
6608 elsif No (First_Formal (Def_Id)) then
6609 return;
6611 -- The subprogram has formals and hence it may be a primitive of a
6612 -- concurrent type
6614 else
6615 Typ := Etype (First_Formal (Def_Id));
6617 if Is_Access_Type (Typ) then
6618 Typ := Directly_Designated_Type (Typ);
6619 end if;
6621 if Is_Concurrent_Type (Typ)
6622 and then not Is_Generic_Actual_Type (Typ)
6623 then
6624 In_Scope := False;
6626 -- This case occurs when the concurrent type is declared within
6627 -- a generic unit. As a result the corresponding record has been
6628 -- built and used as the type of the first formal, we just have
6629 -- to retrieve the corresponding concurrent type.
6631 elsif Is_Concurrent_Record_Type (Typ)
6632 and then Present (Corresponding_Concurrent_Type (Typ))
6633 then
6634 Typ := Corresponding_Concurrent_Type (Typ);
6635 In_Scope := False;
6637 else
6638 return;
6639 end if;
6640 end if;
6642 -- There is no overriding to check if is an inherited operation in a
6643 -- type derivation on for a generic actual.
6645 Collect_Interfaces (Typ, Ifaces_List);
6647 if Is_Empty_Elmt_List (Ifaces_List) then
6648 return;
6649 end if;
6651 -- Determine whether entry or subprogram Def_Id overrides a primitive
6652 -- operation that belongs to one of the interfaces in Ifaces_List.
6654 declare
6655 Candidate : Entity_Id := Empty;
6656 Hom : Entity_Id := Empty;
6657 Iface_Typ : Entity_Id;
6658 Subp : Entity_Id := Empty;
6660 begin
6661 -- Traverse the homonym chain, looking at a potentially
6662 -- overridden subprogram that belongs to an implemented
6663 -- interface.
6665 Hom := Current_Entity_In_Scope (Def_Id);
6666 while Present (Hom) loop
6667 Subp := Hom;
6669 -- Entries can override abstract or null interface
6670 -- procedures
6672 if Ekind (Def_Id) = E_Entry
6673 and then Ekind (Subp) = E_Procedure
6674 and then Nkind (Parent (Subp)) = N_Procedure_Specification
6675 and then (Is_Abstract_Subprogram (Subp)
6676 or else Null_Present (Parent (Subp)))
6677 then
6678 while Present (Alias (Subp)) loop
6679 Subp := Alias (Subp);
6680 end loop;
6682 if Matches_Prefixed_View_Profile
6683 (Parameter_Specifications (Parent (Def_Id)),
6684 Parameter_Specifications (Parent (Subp)))
6685 then
6686 Candidate := Subp;
6688 -- Absolute match
6690 if Has_Correct_Formal_Mode (Typ, Candidate) then
6691 Overridden_Subp := Candidate;
6692 return;
6693 end if;
6694 end if;
6696 -- Procedures can override abstract or null interface
6697 -- procedures
6699 elsif Ekind (Def_Id) = E_Procedure
6700 and then Ekind (Subp) = E_Procedure
6701 and then Nkind (Parent (Subp)) = N_Procedure_Specification
6702 and then (Is_Abstract_Subprogram (Subp)
6703 or else Null_Present (Parent (Subp)))
6704 and then Matches_Prefixed_View_Profile
6705 (Parameter_Specifications (Parent (Def_Id)),
6706 Parameter_Specifications (Parent (Subp)))
6707 then
6708 Candidate := Subp;
6710 -- Absolute match
6712 if Has_Correct_Formal_Mode (Typ, Candidate) then
6713 Overridden_Subp := Candidate;
6714 return;
6715 end if;
6717 -- Functions can override abstract interface functions
6719 elsif Ekind (Def_Id) = E_Function
6720 and then Ekind (Subp) = E_Function
6721 and then Nkind (Parent (Subp)) = N_Function_Specification
6722 and then Is_Abstract_Subprogram (Subp)
6723 and then Matches_Prefixed_View_Profile
6724 (Parameter_Specifications (Parent (Def_Id)),
6725 Parameter_Specifications (Parent (Subp)))
6726 and then Etype (Result_Definition (Parent (Def_Id))) =
6727 Etype (Result_Definition (Parent (Subp)))
6728 then
6729 Overridden_Subp := Subp;
6730 return;
6731 end if;
6733 Hom := Homonym (Hom);
6734 end loop;
6736 -- After examining all candidates for overriding, we are
6737 -- left with the best match which is a mode incompatible
6738 -- interface routine. Do not emit an error if the Expander
6739 -- is active since this error will be detected later on
6740 -- after all concurrent types are expanded and all wrappers
6741 -- are built. This check is meant for spec-only
6742 -- compilations.
6744 if Present (Candidate)
6745 and then not Expander_Active
6746 then
6747 Iface_Typ :=
6748 Find_Parameter_Type (Parent (First_Formal (Candidate)));
6750 -- Def_Id is primitive of a protected type, declared
6751 -- inside the type, and the candidate is primitive of a
6752 -- limited or synchronized interface.
6754 if In_Scope
6755 and then Is_Protected_Type (Typ)
6756 and then
6757 (Is_Limited_Interface (Iface_Typ)
6758 or else Is_Protected_Interface (Iface_Typ)
6759 or else Is_Synchronized_Interface (Iface_Typ)
6760 or else Is_Task_Interface (Iface_Typ))
6761 then
6762 -- Must reword this message, comma before to in -gnatj
6763 -- mode ???
6765 Error_Msg_NE
6766 ("first formal of & must be of mode `OUT`, `IN OUT`"
6767 & " or access-to-variable", Typ, Candidate);
6768 Error_Msg_N
6769 ("\to be overridden by protected procedure or entry "
6770 & "(RM 9.4(11.9/2))", Typ);
6771 end if;
6772 end if;
6774 Overridden_Subp := Candidate;
6775 return;
6776 end;
6777 end Check_Synchronized_Overriding;
6779 ----------------------------
6780 -- Is_Private_Declaration --
6781 ----------------------------
6783 function Is_Private_Declaration (E : Entity_Id) return Boolean is
6784 Priv_Decls : List_Id;
6785 Decl : constant Node_Id := Unit_Declaration_Node (E);
6787 begin
6788 if Is_Package_Or_Generic_Package (Current_Scope)
6789 and then In_Private_Part (Current_Scope)
6790 then
6791 Priv_Decls :=
6792 Private_Declarations (
6793 Specification (Unit_Declaration_Node (Current_Scope)));
6795 return In_Package_Body (Current_Scope)
6796 or else
6797 (Is_List_Member (Decl)
6798 and then List_Containing (Decl) = Priv_Decls)
6799 or else (Nkind (Parent (Decl)) = N_Package_Specification
6800 and then not Is_Compilation_Unit (
6801 Defining_Entity (Parent (Decl)))
6802 and then List_Containing (Parent (Parent (Decl)))
6803 = Priv_Decls);
6804 else
6805 return False;
6806 end if;
6807 end Is_Private_Declaration;
6809 -- Start of processing for New_Overloaded_Entity
6811 begin
6812 -- We need to look for an entity that S may override. This must be a
6813 -- homonym in the current scope, so we look for the first homonym of
6814 -- S in the current scope as the starting point for the search.
6816 E := Current_Entity_In_Scope (S);
6818 -- If there is no homonym then this is definitely not overriding
6820 if No (E) then
6821 Enter_Overloaded_Entity (S);
6822 Check_Dispatching_Operation (S, Empty);
6823 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
6825 -- If subprogram has an explicit declaration, check whether it
6826 -- has an overriding indicator.
6828 if Comes_From_Source (S) then
6829 Check_Synchronized_Overriding (S, Overridden_Subp);
6830 Check_Overriding_Indicator
6831 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
6832 end if;
6834 -- If there is a homonym that is not overloadable, then we have an
6835 -- error, except for the special cases checked explicitly below.
6837 elsif not Is_Overloadable (E) then
6839 -- Check for spurious conflict produced by a subprogram that has the
6840 -- same name as that of the enclosing generic package. The conflict
6841 -- occurs within an instance, between the subprogram and the renaming
6842 -- declaration for the package. After the subprogram, the package
6843 -- renaming declaration becomes hidden.
6845 if Ekind (E) = E_Package
6846 and then Present (Renamed_Object (E))
6847 and then Renamed_Object (E) = Current_Scope
6848 and then Nkind (Parent (Renamed_Object (E))) =
6849 N_Package_Specification
6850 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
6851 then
6852 Set_Is_Hidden (E);
6853 Set_Is_Immediately_Visible (E, False);
6854 Enter_Overloaded_Entity (S);
6855 Set_Homonym (S, Homonym (E));
6856 Check_Dispatching_Operation (S, Empty);
6857 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
6859 -- If the subprogram is implicit it is hidden by the previous
6860 -- declaration. However if it is dispatching, it must appear in the
6861 -- dispatch table anyway, because it can be dispatched to even if it
6862 -- cannot be called directly.
6864 elsif Present (Alias (S))
6865 and then not Comes_From_Source (S)
6866 then
6867 Set_Scope (S, Current_Scope);
6869 if Is_Dispatching_Operation (Alias (S)) then
6870 Check_Dispatching_Operation (S, Empty);
6871 end if;
6873 return;
6875 else
6876 Error_Msg_Sloc := Sloc (E);
6878 -- Generate message, with useful additional warning if in generic
6880 if Is_Generic_Unit (E) then
6881 Error_Msg_N ("previous generic unit cannot be overloaded", S);
6882 Error_Msg_N ("\& conflicts with declaration#", S);
6883 else
6884 Error_Msg_N ("& conflicts with declaration#", S);
6885 end if;
6887 return;
6888 end if;
6890 -- E exists and is overloadable
6892 else
6893 -- Ada 2005 (AI-251): Derivation of abstract interface primitives
6894 -- need no check against the homonym chain. They are directly added
6895 -- to the list of primitive operations of Derived_Type.
6897 if Ada_Version >= Ada_05
6898 and then Present (Derived_Type)
6899 and then Is_Dispatching_Operation (Alias (S))
6900 and then Present (Find_Dispatching_Type (Alias (S)))
6901 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
6902 then
6903 goto Add_New_Entity;
6904 end if;
6906 Check_Synchronized_Overriding (S, Overridden_Subp);
6908 -- Loop through E and its homonyms to determine if any of them is
6909 -- the candidate for overriding by S.
6911 while Present (E) loop
6913 -- Definitely not interesting if not in the current scope
6915 if Scope (E) /= Current_Scope then
6916 null;
6918 -- Check if we have type conformance
6920 elsif Type_Conformant (E, S) then
6922 -- If the old and new entities have the same profile and one
6923 -- is not the body of the other, then this is an error, unless
6924 -- one of them is implicitly declared.
6926 -- There are some cases when both can be implicit, for example
6927 -- when both a literal and a function that overrides it are
6928 -- inherited in a derivation, or when an inherited operation
6929 -- of a tagged full type overrides the inherited operation of
6930 -- a private extension. Ada 83 had a special rule for the
6931 -- literal case. In Ada95, the later implicit operation hides
6932 -- the former, and the literal is always the former. In the
6933 -- odd case where both are derived operations declared at the
6934 -- same point, both operations should be declared, and in that
6935 -- case we bypass the following test and proceed to the next
6936 -- part (this can only occur for certain obscure cases
6937 -- involving homographs in instances and can't occur for
6938 -- dispatching operations ???). Note that the following
6939 -- condition is less than clear. For example, it's not at all
6940 -- clear why there's a test for E_Entry here. ???
6942 if Present (Alias (S))
6943 and then (No (Alias (E))
6944 or else Comes_From_Source (E)
6945 or else Is_Dispatching_Operation (E))
6946 and then
6947 (Ekind (E) = E_Entry
6948 or else Ekind (E) /= E_Enumeration_Literal)
6949 then
6950 -- When an derived operation is overloaded it may be due to
6951 -- the fact that the full view of a private extension
6952 -- re-inherits. It has to be dealt with.
6954 if Is_Package_Or_Generic_Package (Current_Scope)
6955 and then In_Private_Part (Current_Scope)
6956 then
6957 Check_Operation_From_Private_View (S, E);
6958 end if;
6960 -- In any case the implicit operation remains hidden by
6961 -- the existing declaration, which is overriding.
6963 Set_Is_Overriding_Operation (E);
6965 if Comes_From_Source (E) then
6966 Check_Overriding_Indicator (E, S, Is_Primitive => False);
6968 -- Indicate that E overrides the operation from which
6969 -- S is inherited.
6971 if Present (Alias (S)) then
6972 Set_Overridden_Operation (E, Alias (S));
6973 else
6974 Set_Overridden_Operation (E, S);
6975 end if;
6976 end if;
6978 return;
6980 -- Within an instance, the renaming declarations for
6981 -- actual subprograms may become ambiguous, but they do
6982 -- not hide each other.
6984 elsif Ekind (E) /= E_Entry
6985 and then not Comes_From_Source (E)
6986 and then not Is_Generic_Instance (E)
6987 and then (Present (Alias (E))
6988 or else Is_Intrinsic_Subprogram (E))
6989 and then (not In_Instance
6990 or else No (Parent (E))
6991 or else Nkind (Unit_Declaration_Node (E)) /=
6992 N_Subprogram_Renaming_Declaration)
6993 then
6994 -- A subprogram child unit is not allowed to override
6995 -- an inherited subprogram (10.1.1(20)).
6997 if Is_Child_Unit (S) then
6998 Error_Msg_N
6999 ("child unit overrides inherited subprogram in parent",
7001 return;
7002 end if;
7004 if Is_Non_Overriding_Operation (E, S) then
7005 Enter_Overloaded_Entity (S);
7006 if No (Derived_Type)
7007 or else Is_Tagged_Type (Derived_Type)
7008 then
7009 Check_Dispatching_Operation (S, Empty);
7010 end if;
7012 return;
7013 end if;
7015 -- E is a derived operation or an internal operator which
7016 -- is being overridden. Remove E from further visibility.
7017 -- Furthermore, if E is a dispatching operation, it must be
7018 -- replaced in the list of primitive operations of its type
7019 -- (see Override_Dispatching_Operation).
7021 Overridden_Subp := E;
7023 declare
7024 Prev : Entity_Id;
7026 begin
7027 Prev := First_Entity (Current_Scope);
7029 while Present (Prev)
7030 and then Next_Entity (Prev) /= E
7031 loop
7032 Next_Entity (Prev);
7033 end loop;
7035 -- It is possible for E to be in the current scope and
7036 -- yet not in the entity chain. This can only occur in a
7037 -- generic context where E is an implicit concatenation
7038 -- in the formal part, because in a generic body the
7039 -- entity chain starts with the formals.
7041 pragma Assert
7042 (Present (Prev) or else Chars (E) = Name_Op_Concat);
7044 -- E must be removed both from the entity_list of the
7045 -- current scope, and from the visibility chain
7047 if Debug_Flag_E then
7048 Write_Str ("Override implicit operation ");
7049 Write_Int (Int (E));
7050 Write_Eol;
7051 end if;
7053 -- If E is a predefined concatenation, it stands for four
7054 -- different operations. As a result, a single explicit
7055 -- declaration does not hide it. In a possible ambiguous
7056 -- situation, Disambiguate chooses the user-defined op,
7057 -- so it is correct to retain the previous internal one.
7059 if Chars (E) /= Name_Op_Concat
7060 or else Ekind (E) /= E_Operator
7061 then
7062 -- For nondispatching derived operations that are
7063 -- overridden by a subprogram declared in the private
7064 -- part of a package, we retain the derived
7065 -- subprogram but mark it as not immediately visible.
7066 -- If the derived operation was declared in the
7067 -- visible part then this ensures that it will still
7068 -- be visible outside the package with the proper
7069 -- signature (calls from outside must also be
7070 -- directed to this version rather than the
7071 -- overriding one, unlike the dispatching case).
7072 -- Calls from inside the package will still resolve
7073 -- to the overriding subprogram since the derived one
7074 -- is marked as not visible within the package.
7076 -- If the private operation is dispatching, we achieve
7077 -- the overriding by keeping the implicit operation
7078 -- but setting its alias to be the overriding one. In
7079 -- this fashion the proper body is executed in all
7080 -- cases, but the original signature is used outside
7081 -- of the package.
7083 -- If the overriding is not in the private part, we
7084 -- remove the implicit operation altogether.
7086 if Is_Private_Declaration (S) then
7088 if not Is_Dispatching_Operation (E) then
7089 Set_Is_Immediately_Visible (E, False);
7090 else
7091 -- Work done in Override_Dispatching_Operation,
7092 -- so nothing else need to be done here.
7094 null;
7095 end if;
7097 else
7098 -- Find predecessor of E in Homonym chain
7100 if E = Current_Entity (E) then
7101 Prev_Vis := Empty;
7102 else
7103 Prev_Vis := Current_Entity (E);
7104 while Homonym (Prev_Vis) /= E loop
7105 Prev_Vis := Homonym (Prev_Vis);
7106 end loop;
7107 end if;
7109 if Prev_Vis /= Empty then
7111 -- Skip E in the visibility chain
7113 Set_Homonym (Prev_Vis, Homonym (E));
7115 else
7116 Set_Name_Entity_Id (Chars (E), Homonym (E));
7117 end if;
7119 Set_Next_Entity (Prev, Next_Entity (E));
7121 if No (Next_Entity (Prev)) then
7122 Set_Last_Entity (Current_Scope, Prev);
7123 end if;
7125 end if;
7126 end if;
7128 Enter_Overloaded_Entity (S);
7129 Set_Is_Overriding_Operation (S);
7130 Check_Overriding_Indicator (S, E, Is_Primitive => True);
7132 -- Indicate that S overrides the operation from which
7133 -- E is inherited.
7135 if Comes_From_Source (S) then
7136 if Present (Alias (E)) then
7137 Set_Overridden_Operation (S, Alias (E));
7138 else
7139 Set_Overridden_Operation (S, E);
7140 end if;
7141 end if;
7143 if Is_Dispatching_Operation (E) then
7145 -- An overriding dispatching subprogram inherits the
7146 -- convention of the overridden subprogram (by
7147 -- AI-117).
7149 Set_Convention (S, Convention (E));
7150 Check_Dispatching_Operation (S, E);
7152 else
7153 Check_Dispatching_Operation (S, Empty);
7154 end if;
7156 Check_For_Primitive_Subprogram
7157 (Is_Primitive_Subp, Is_Overriding => True);
7158 goto Check_Inequality;
7159 end;
7161 -- Apparent redeclarations in instances can occur when two
7162 -- formal types get the same actual type. The subprograms in
7163 -- in the instance are legal, even if not callable from the
7164 -- outside. Calls from within are disambiguated elsewhere.
7165 -- For dispatching operations in the visible part, the usual
7166 -- rules apply, and operations with the same profile are not
7167 -- legal (B830001).
7169 elsif (In_Instance_Visible_Part
7170 and then not Is_Dispatching_Operation (E))
7171 or else In_Instance_Not_Visible
7172 then
7173 null;
7175 -- Here we have a real error (identical profile)
7177 else
7178 Error_Msg_Sloc := Sloc (E);
7180 -- Avoid cascaded errors if the entity appears in
7181 -- subsequent calls.
7183 Set_Scope (S, Current_Scope);
7185 -- Generate error, with extra useful warning for the case
7186 -- of a generic instance with no completion.
7188 if Is_Generic_Instance (S)
7189 and then not Has_Completion (E)
7190 then
7191 Error_Msg_N
7192 ("instantiation cannot provide body for&", S);
7193 Error_Msg_N ("\& conflicts with declaration#", S);
7194 else
7195 Error_Msg_N ("& conflicts with declaration#", S);
7196 end if;
7198 return;
7199 end if;
7201 else
7202 -- If one subprogram has an access parameter and the other
7203 -- a parameter of an access type, calls to either might be
7204 -- ambiguous. Verify that parameters match except for the
7205 -- access parameter.
7207 if May_Hide_Profile then
7208 declare
7209 F1 : Entity_Id;
7210 F2 : Entity_Id;
7211 begin
7212 F1 := First_Formal (S);
7213 F2 := First_Formal (E);
7214 while Present (F1) and then Present (F2) loop
7215 if Is_Access_Type (Etype (F1)) then
7216 if not Is_Access_Type (Etype (F2))
7217 or else not Conforming_Types
7218 (Designated_Type (Etype (F1)),
7219 Designated_Type (Etype (F2)),
7220 Type_Conformant)
7221 then
7222 May_Hide_Profile := False;
7223 end if;
7225 elsif
7226 not Conforming_Types
7227 (Etype (F1), Etype (F2), Type_Conformant)
7228 then
7229 May_Hide_Profile := False;
7230 end if;
7232 Next_Formal (F1);
7233 Next_Formal (F2);
7234 end loop;
7236 if May_Hide_Profile
7237 and then No (F1)
7238 and then No (F2)
7239 then
7240 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
7241 end if;
7242 end;
7243 end if;
7244 end if;
7246 E := Homonym (E);
7247 end loop;
7249 <<Add_New_Entity>>
7251 -- On exit, we know that S is a new entity
7253 Enter_Overloaded_Entity (S);
7254 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7255 Check_Overriding_Indicator
7256 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7258 -- If S is a derived operation for an untagged type then by
7259 -- definition it's not a dispatching operation (even if the parent
7260 -- operation was dispatching), so we don't call
7261 -- Check_Dispatching_Operation in that case.
7263 if No (Derived_Type)
7264 or else Is_Tagged_Type (Derived_Type)
7265 then
7266 Check_Dispatching_Operation (S, Empty);
7267 end if;
7268 end if;
7270 -- If this is a user-defined equality operator that is not a derived
7271 -- subprogram, create the corresponding inequality. If the operation is
7272 -- dispatching, the expansion is done elsewhere, and we do not create
7273 -- an explicit inequality operation.
7275 <<Check_Inequality>>
7276 if Chars (S) = Name_Op_Eq
7277 and then Etype (S) = Standard_Boolean
7278 and then Present (Parent (S))
7279 and then not Is_Dispatching_Operation (S)
7280 then
7281 Make_Inequality_Operator (S);
7282 end if;
7283 end New_Overloaded_Entity;
7285 ---------------------
7286 -- Process_Formals --
7287 ---------------------
7289 procedure Process_Formals
7290 (T : List_Id;
7291 Related_Nod : Node_Id)
7293 Param_Spec : Node_Id;
7294 Formal : Entity_Id;
7295 Formal_Type : Entity_Id;
7296 Default : Node_Id;
7297 Ptype : Entity_Id;
7299 Num_Out_Params : Nat := 0;
7300 First_Out_Param : Entity_Id := Empty;
7301 -- Used for setting Is_Only_Out_Parameter
7303 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
7304 -- Check whether the default has a class-wide type. After analysis the
7305 -- default has the type of the formal, so we must also check explicitly
7306 -- for an access attribute.
7308 ---------------------------
7309 -- Is_Class_Wide_Default --
7310 ---------------------------
7312 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
7313 begin
7314 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
7315 or else (Nkind (D) = N_Attribute_Reference
7316 and then Attribute_Name (D) = Name_Access
7317 and then Is_Class_Wide_Type (Etype (Prefix (D))));
7318 end Is_Class_Wide_Default;
7320 -- Start of processing for Process_Formals
7322 begin
7323 -- In order to prevent premature use of the formals in the same formal
7324 -- part, the Ekind is left undefined until all default expressions are
7325 -- analyzed. The Ekind is established in a separate loop at the end.
7327 Param_Spec := First (T);
7328 while Present (Param_Spec) loop
7329 Formal := Defining_Identifier (Param_Spec);
7330 Set_Never_Set_In_Source (Formal, True);
7331 Enter_Name (Formal);
7333 -- Case of ordinary parameters
7335 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
7336 Find_Type (Parameter_Type (Param_Spec));
7337 Ptype := Parameter_Type (Param_Spec);
7339 if Ptype = Error then
7340 goto Continue;
7341 end if;
7343 Formal_Type := Entity (Ptype);
7345 if Is_Incomplete_Type (Formal_Type)
7346 or else
7347 (Is_Class_Wide_Type (Formal_Type)
7348 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
7349 then
7350 -- Ada 2005 (AI-326): Tagged incomplete types allowed
7352 if Is_Tagged_Type (Formal_Type) then
7353 null;
7355 -- Special handling of Value_Type for CIL case
7357 elsif Is_Value_Type (Formal_Type) then
7358 null;
7360 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
7361 N_Access_Procedure_Definition)
7362 then
7363 Error_Msg_N ("invalid use of incomplete type", Param_Spec);
7365 -- An incomplete type that is not tagged is allowed in an
7366 -- access-to-subprogram type only if it is a local declaration
7367 -- with a forthcoming completion (3.10.1 (9.2/2)).
7369 elsif Scope (Formal_Type) /= Scope (Current_Scope) then
7370 Error_Msg_N
7371 ("invalid use of limited view of type", Param_Spec);
7372 end if;
7374 elsif Ekind (Formal_Type) = E_Void then
7375 Error_Msg_NE ("premature use of&",
7376 Parameter_Type (Param_Spec), Formal_Type);
7377 end if;
7379 -- Ada 2005 (AI-231): Create and decorate an internal subtype
7380 -- declaration corresponding to the null-excluding type of the
7381 -- formal in the enclosing scope. Finally, replace the parameter
7382 -- type of the formal with the internal subtype.
7384 if Ada_Version >= Ada_05
7385 and then Null_Exclusion_Present (Param_Spec)
7386 then
7387 if not Is_Access_Type (Formal_Type) then
7388 Error_Msg_N
7389 ("`NOT NULL` allowed only for an access type", Param_Spec);
7391 else
7392 if Can_Never_Be_Null (Formal_Type)
7393 and then Comes_From_Source (Related_Nod)
7394 then
7395 Error_Msg_NE
7396 ("`NOT NULL` not allowed (& already excludes null)",
7397 Param_Spec,
7398 Formal_Type);
7399 end if;
7401 Formal_Type :=
7402 Create_Null_Excluding_Itype
7403 (T => Formal_Type,
7404 Related_Nod => Related_Nod,
7405 Scope_Id => Scope (Current_Scope));
7407 -- If the designated type of the itype is an itype we
7408 -- decorate it with the Has_Delayed_Freeze attribute to
7409 -- avoid problems with the backend.
7411 -- Example:
7412 -- type T is access procedure;
7413 -- procedure Op (O : not null T);
7415 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
7416 Set_Has_Delayed_Freeze (Formal_Type);
7417 end if;
7418 end if;
7419 end if;
7421 -- An access formal type
7423 else
7424 Formal_Type :=
7425 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
7427 -- No need to continue if we already notified errors
7429 if not Present (Formal_Type) then
7430 return;
7431 end if;
7433 -- Ada 2005 (AI-254)
7435 declare
7436 AD : constant Node_Id :=
7437 Access_To_Subprogram_Definition
7438 (Parameter_Type (Param_Spec));
7439 begin
7440 if Present (AD) and then Protected_Present (AD) then
7441 Formal_Type :=
7442 Replace_Anonymous_Access_To_Protected_Subprogram
7443 (Param_Spec);
7444 end if;
7445 end;
7446 end if;
7448 Set_Etype (Formal, Formal_Type);
7449 Default := Expression (Param_Spec);
7451 if Present (Default) then
7452 if Out_Present (Param_Spec) then
7453 Error_Msg_N
7454 ("default initialization only allowed for IN parameters",
7455 Param_Spec);
7456 end if;
7458 -- Do the special preanalysis of the expression (see section on
7459 -- "Handling of Default Expressions" in the spec of package Sem).
7461 Preanalyze_Spec_Expression (Default, Formal_Type);
7463 -- An access to constant cannot be the default for
7464 -- an access parameter that is an access to variable.
7466 if Ekind (Formal_Type) = E_Anonymous_Access_Type
7467 and then not Is_Access_Constant (Formal_Type)
7468 and then Is_Access_Type (Etype (Default))
7469 and then Is_Access_Constant (Etype (Default))
7470 then
7471 Error_Msg_N
7472 ("formal that is access to variable cannot be initialized " &
7473 "with an access-to-constant expression", Default);
7474 end if;
7476 -- Check that the designated type of an access parameter's default
7477 -- is not a class-wide type unless the parameter's designated type
7478 -- is also class-wide.
7480 if Ekind (Formal_Type) = E_Anonymous_Access_Type
7481 and then not From_With_Type (Formal_Type)
7482 and then Is_Class_Wide_Default (Default)
7483 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
7484 then
7485 Error_Msg_N
7486 ("access to class-wide expression not allowed here", Default);
7487 end if;
7488 end if;
7490 -- Ada 2005 (AI-231): Static checks
7492 if Ada_Version >= Ada_05
7493 and then Is_Access_Type (Etype (Formal))
7494 and then Can_Never_Be_Null (Etype (Formal))
7495 then
7496 Null_Exclusion_Static_Checks (Param_Spec);
7497 end if;
7499 <<Continue>>
7500 Next (Param_Spec);
7501 end loop;
7503 -- If this is the formal part of a function specification, analyze the
7504 -- subtype mark in the context where the formals are visible but not
7505 -- yet usable, and may hide outer homographs.
7507 if Nkind (Related_Nod) = N_Function_Specification then
7508 Analyze_Return_Type (Related_Nod);
7509 end if;
7511 -- Now set the kind (mode) of each formal
7513 Param_Spec := First (T);
7515 while Present (Param_Spec) loop
7516 Formal := Defining_Identifier (Param_Spec);
7517 Set_Formal_Mode (Formal);
7519 if Ekind (Formal) = E_In_Parameter then
7520 Set_Default_Value (Formal, Expression (Param_Spec));
7522 if Present (Expression (Param_Spec)) then
7523 Default := Expression (Param_Spec);
7525 if Is_Scalar_Type (Etype (Default)) then
7526 if Nkind
7527 (Parameter_Type (Param_Spec)) /= N_Access_Definition
7528 then
7529 Formal_Type := Entity (Parameter_Type (Param_Spec));
7531 else
7532 Formal_Type := Access_Definition
7533 (Related_Nod, Parameter_Type (Param_Spec));
7534 end if;
7536 Apply_Scalar_Range_Check (Default, Formal_Type);
7537 end if;
7538 end if;
7540 elsif Ekind (Formal) = E_Out_Parameter then
7541 Num_Out_Params := Num_Out_Params + 1;
7543 if Num_Out_Params = 1 then
7544 First_Out_Param := Formal;
7545 end if;
7547 elsif Ekind (Formal) = E_In_Out_Parameter then
7548 Num_Out_Params := Num_Out_Params + 1;
7549 end if;
7551 Next (Param_Spec);
7552 end loop;
7554 if Present (First_Out_Param) and then Num_Out_Params = 1 then
7555 Set_Is_Only_Out_Parameter (First_Out_Param);
7556 end if;
7557 end Process_Formals;
7559 ------------------
7560 -- Process_PPCs --
7561 ------------------
7563 procedure Process_PPCs
7564 (N : Node_Id;
7565 Spec_Id : Entity_Id;
7566 Body_Id : Entity_Id)
7568 Loc : constant Source_Ptr := Sloc (N);
7569 Prag : Node_Id;
7570 Plist : List_Id := No_List;
7571 Subp : Entity_Id;
7572 Parms : List_Id;
7574 function Grab_PPC (Nam : Name_Id) return Node_Id;
7575 -- Prag contains an analyzed precondition or postcondition pragma.
7576 -- This function copies the pragma, changes it to the corresponding
7577 -- Check pragma and returns the Check pragma as the result. The
7578 -- argument Nam is either Name_Precondition or Name_Postcondition.
7580 --------------
7581 -- Grab_PPC --
7582 --------------
7584 function Grab_PPC (Nam : Name_Id) return Node_Id is
7585 CP : constant Node_Id := New_Copy_Tree (Prag);
7587 begin
7588 -- Set Analyzed to false, since we want to reanalyze the check
7589 -- procedure. Note that it is only at the outer level that we
7590 -- do this fiddling, for the spec cases, the already preanalyzed
7591 -- parameters are not affected.
7593 Set_Analyzed (CP, False);
7595 -- Change pragma into corresponding pragma Check
7597 Prepend_To (Pragma_Argument_Associations (CP),
7598 Make_Pragma_Argument_Association (Sloc (Prag),
7599 Expression =>
7600 Make_Identifier (Loc,
7601 Chars => Nam)));
7602 Set_Pragma_Identifier (CP,
7603 Make_Identifier (Sloc (Prag),
7604 Chars => Name_Check));
7606 return CP;
7607 end Grab_PPC;
7609 -- Start of processing for Process_PPCs
7611 begin
7612 -- Grab preconditions from spec
7614 if Present (Spec_Id) then
7616 -- Loop through PPC pragmas from spec. Note that preconditions from
7617 -- the body will be analyzed and converted when we scan the body
7618 -- declarations below.
7620 Prag := Spec_PPC_List (Spec_Id);
7621 while Present (Prag) loop
7622 if Pragma_Name (Prag) = Name_Precondition
7623 and then PPC_Enabled (Prag)
7624 then
7625 -- Add pragma Check at the start of the declarations of N.
7626 -- Note that this processing reverses the order of the list,
7627 -- which is what we want since new entries were chained to
7628 -- the head of the list.
7630 Prepend (Grab_PPC (Name_Precondition), Declarations (N));
7631 end if;
7633 Prag := Next_Pragma (Prag);
7634 end loop;
7635 end if;
7637 -- Build postconditions procedure if needed and prepend the following
7638 -- declaration to the start of the declarations for the subprogram.
7640 -- procedure _postconditions [(_Result : resulttype)] is
7641 -- begin
7642 -- pragma Check (Postcondition, condition [,message]);
7643 -- pragma Check (Postcondition, condition [,message]);
7644 -- ...
7645 -- end;
7647 -- First we deal with the postconditions in the body
7649 if Is_Non_Empty_List (Declarations (N)) then
7651 -- Loop through declarations
7653 Prag := First (Declarations (N));
7654 while Present (Prag) loop
7655 if Nkind (Prag) = N_Pragma then
7657 -- If pragma, capture if enabled postcondition, else ignore
7659 if Pragma_Name (Prag) = Name_Postcondition
7660 and then Check_Enabled (Name_Postcondition)
7661 then
7662 if Plist = No_List then
7663 Plist := Empty_List;
7664 end if;
7666 Analyze (Prag);
7667 Append (Grab_PPC (Name_Postcondition), Plist);
7668 end if;
7670 Next (Prag);
7672 -- Not a pragma, if comes from source, then end scan
7674 elsif Comes_From_Source (Prag) then
7675 exit;
7677 -- Skip stuff not coming from source
7679 else
7680 Next (Prag);
7681 end if;
7682 end loop;
7683 end if;
7685 -- Now deal with any postconditions from the spec
7687 if Present (Spec_Id) then
7689 -- Loop through PPC pragmas from spec
7691 Prag := Spec_PPC_List (Spec_Id);
7692 while Present (Prag) loop
7693 if Pragma_Name (Prag) = Name_Postcondition
7694 and then PPC_Enabled (Prag)
7695 then
7696 if Plist = No_List then
7697 Plist := Empty_List;
7698 end if;
7700 Append (Grab_PPC (Name_Postcondition), Plist);
7701 end if;
7703 Prag := Next_Pragma (Prag);
7704 end loop;
7705 end if;
7707 -- If we had any postconditions, build the procedure
7709 if Present (Plist) then
7710 Subp := Defining_Entity (N);
7712 if Etype (Subp) /= Standard_Void_Type then
7713 Parms := New_List (
7714 Make_Parameter_Specification (Loc,
7715 Defining_Identifier =>
7716 Make_Defining_Identifier (Loc,
7717 Chars => Name_uResult),
7718 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
7719 else
7720 Parms := No_List;
7721 end if;
7723 Prepend_To (Declarations (N),
7724 Make_Subprogram_Body (Loc,
7725 Specification =>
7726 Make_Procedure_Specification (Loc,
7727 Defining_Unit_Name =>
7728 Make_Defining_Identifier (Loc,
7729 Chars => Name_uPostconditions),
7730 Parameter_Specifications => Parms),
7732 Declarations => Empty_List,
7734 Handled_Statement_Sequence =>
7735 Make_Handled_Sequence_Of_Statements (Loc,
7736 Statements => Plist)));
7738 if Present (Spec_Id) then
7739 Set_Has_Postconditions (Spec_Id);
7740 else
7741 Set_Has_Postconditions (Body_Id);
7742 end if;
7743 end if;
7744 end Process_PPCs;
7746 ----------------------------
7747 -- Reference_Body_Formals --
7748 ----------------------------
7750 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
7751 Fs : Entity_Id;
7752 Fb : Entity_Id;
7754 begin
7755 if Error_Posted (Spec) then
7756 return;
7757 end if;
7759 -- Iterate over both lists. They may be of different lengths if the two
7760 -- specs are not conformant.
7762 Fs := First_Formal (Spec);
7763 Fb := First_Formal (Bod);
7764 while Present (Fs) and then Present (Fb) loop
7765 Generate_Reference (Fs, Fb, 'b');
7767 if Style_Check then
7768 Style.Check_Identifier (Fb, Fs);
7769 end if;
7771 Set_Spec_Entity (Fb, Fs);
7772 Set_Referenced (Fs, False);
7773 Next_Formal (Fs);
7774 Next_Formal (Fb);
7775 end loop;
7776 end Reference_Body_Formals;
7778 -------------------------
7779 -- Set_Actual_Subtypes --
7780 -------------------------
7782 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
7783 Loc : constant Source_Ptr := Sloc (N);
7784 Decl : Node_Id;
7785 Formal : Entity_Id;
7786 T : Entity_Id;
7787 First_Stmt : Node_Id := Empty;
7788 AS_Needed : Boolean;
7790 begin
7791 -- If this is an empty initialization procedure, no need to create
7792 -- actual subtypes (small optimization).
7794 if Ekind (Subp) = E_Procedure
7795 and then Is_Null_Init_Proc (Subp)
7796 then
7797 return;
7798 end if;
7800 Formal := First_Formal (Subp);
7801 while Present (Formal) loop
7802 T := Etype (Formal);
7804 -- We never need an actual subtype for a constrained formal
7806 if Is_Constrained (T) then
7807 AS_Needed := False;
7809 -- If we have unknown discriminants, then we do not need an actual
7810 -- subtype, or more accurately we cannot figure it out! Note that
7811 -- all class-wide types have unknown discriminants.
7813 elsif Has_Unknown_Discriminants (T) then
7814 AS_Needed := False;
7816 -- At this stage we have an unconstrained type that may need an
7817 -- actual subtype. For sure the actual subtype is needed if we have
7818 -- an unconstrained array type.
7820 elsif Is_Array_Type (T) then
7821 AS_Needed := True;
7823 -- The only other case needing an actual subtype is an unconstrained
7824 -- record type which is an IN parameter (we cannot generate actual
7825 -- subtypes for the OUT or IN OUT case, since an assignment can
7826 -- change the discriminant values. However we exclude the case of
7827 -- initialization procedures, since discriminants are handled very
7828 -- specially in this context, see the section entitled "Handling of
7829 -- Discriminants" in Einfo.
7831 -- We also exclude the case of Discrim_SO_Functions (functions used
7832 -- in front end layout mode for size/offset values), since in such
7833 -- functions only discriminants are referenced, and not only are such
7834 -- subtypes not needed, but they cannot always be generated, because
7835 -- of order of elaboration issues.
7837 elsif Is_Record_Type (T)
7838 and then Ekind (Formal) = E_In_Parameter
7839 and then Chars (Formal) /= Name_uInit
7840 and then not Is_Unchecked_Union (T)
7841 and then not Is_Discrim_SO_Function (Subp)
7842 then
7843 AS_Needed := True;
7845 -- All other cases do not need an actual subtype
7847 else
7848 AS_Needed := False;
7849 end if;
7851 -- Generate actual subtypes for unconstrained arrays and
7852 -- unconstrained discriminated records.
7854 if AS_Needed then
7855 if Nkind (N) = N_Accept_Statement then
7857 -- If expansion is active, The formal is replaced by a local
7858 -- variable that renames the corresponding entry of the
7859 -- parameter block, and it is this local variable that may
7860 -- require an actual subtype.
7862 if Expander_Active then
7863 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
7864 else
7865 Decl := Build_Actual_Subtype (T, Formal);
7866 end if;
7868 if Present (Handled_Statement_Sequence (N)) then
7869 First_Stmt :=
7870 First (Statements (Handled_Statement_Sequence (N)));
7871 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
7872 Mark_Rewrite_Insertion (Decl);
7873 else
7874 -- If the accept statement has no body, there will be no
7875 -- reference to the actuals, so no need to compute actual
7876 -- subtypes.
7878 return;
7879 end if;
7881 else
7882 Decl := Build_Actual_Subtype (T, Formal);
7883 Prepend (Decl, Declarations (N));
7884 Mark_Rewrite_Insertion (Decl);
7885 end if;
7887 -- The declaration uses the bounds of an existing object, and
7888 -- therefore needs no constraint checks.
7890 Analyze (Decl, Suppress => All_Checks);
7892 -- We need to freeze manually the generated type when it is
7893 -- inserted anywhere else than in a declarative part.
7895 if Present (First_Stmt) then
7896 Insert_List_Before_And_Analyze (First_Stmt,
7897 Freeze_Entity (Defining_Identifier (Decl), Loc));
7898 end if;
7900 if Nkind (N) = N_Accept_Statement
7901 and then Expander_Active
7902 then
7903 Set_Actual_Subtype (Renamed_Object (Formal),
7904 Defining_Identifier (Decl));
7905 else
7906 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
7907 end if;
7908 end if;
7910 Next_Formal (Formal);
7911 end loop;
7912 end Set_Actual_Subtypes;
7914 ---------------------
7915 -- Set_Formal_Mode --
7916 ---------------------
7918 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
7919 Spec : constant Node_Id := Parent (Formal_Id);
7921 begin
7922 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
7923 -- since we ensure that corresponding actuals are always valid at the
7924 -- point of the call.
7926 if Out_Present (Spec) then
7927 if Ekind (Scope (Formal_Id)) = E_Function
7928 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
7929 then
7930 Error_Msg_N ("functions can only have IN parameters", Spec);
7931 Set_Ekind (Formal_Id, E_In_Parameter);
7933 elsif In_Present (Spec) then
7934 Set_Ekind (Formal_Id, E_In_Out_Parameter);
7936 else
7937 Set_Ekind (Formal_Id, E_Out_Parameter);
7938 Set_Never_Set_In_Source (Formal_Id, True);
7939 Set_Is_True_Constant (Formal_Id, False);
7940 Set_Current_Value (Formal_Id, Empty);
7941 end if;
7943 else
7944 Set_Ekind (Formal_Id, E_In_Parameter);
7945 end if;
7947 -- Set Is_Known_Non_Null for access parameters since the language
7948 -- guarantees that access parameters are always non-null. We also set
7949 -- Can_Never_Be_Null, since there is no way to change the value.
7951 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
7953 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
7954 -- null; In Ada 2005, only if then null_exclusion is explicit.
7956 if Ada_Version < Ada_05
7957 or else Can_Never_Be_Null (Etype (Formal_Id))
7958 then
7959 Set_Is_Known_Non_Null (Formal_Id);
7960 Set_Can_Never_Be_Null (Formal_Id);
7961 end if;
7963 -- Ada 2005 (AI-231): Null-exclusion access subtype
7965 elsif Is_Access_Type (Etype (Formal_Id))
7966 and then Can_Never_Be_Null (Etype (Formal_Id))
7967 then
7968 Set_Is_Known_Non_Null (Formal_Id);
7969 end if;
7971 Set_Mechanism (Formal_Id, Default_Mechanism);
7972 Set_Formal_Validity (Formal_Id);
7973 end Set_Formal_Mode;
7975 -------------------------
7976 -- Set_Formal_Validity --
7977 -------------------------
7979 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
7980 begin
7981 -- If no validity checking, then we cannot assume anything about the
7982 -- validity of parameters, since we do not know there is any checking
7983 -- of the validity on the call side.
7985 if not Validity_Checks_On then
7986 return;
7988 -- If validity checking for parameters is enabled, this means we are
7989 -- not supposed to make any assumptions about argument values.
7991 elsif Validity_Check_Parameters then
7992 return;
7994 -- If we are checking in parameters, we will assume that the caller is
7995 -- also checking parameters, so we can assume the parameter is valid.
7997 elsif Ekind (Formal_Id) = E_In_Parameter
7998 and then Validity_Check_In_Params
7999 then
8000 Set_Is_Known_Valid (Formal_Id, True);
8002 -- Similar treatment for IN OUT parameters
8004 elsif Ekind (Formal_Id) = E_In_Out_Parameter
8005 and then Validity_Check_In_Out_Params
8006 then
8007 Set_Is_Known_Valid (Formal_Id, True);
8008 end if;
8009 end Set_Formal_Validity;
8011 ------------------------
8012 -- Subtype_Conformant --
8013 ------------------------
8015 function Subtype_Conformant
8016 (New_Id : Entity_Id;
8017 Old_Id : Entity_Id;
8018 Skip_Controlling_Formals : Boolean := False) return Boolean
8020 Result : Boolean;
8021 begin
8022 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
8023 Skip_Controlling_Formals => Skip_Controlling_Formals);
8024 return Result;
8025 end Subtype_Conformant;
8027 ---------------------
8028 -- Type_Conformant --
8029 ---------------------
8031 function Type_Conformant
8032 (New_Id : Entity_Id;
8033 Old_Id : Entity_Id;
8034 Skip_Controlling_Formals : Boolean := False) return Boolean
8036 Result : Boolean;
8037 begin
8038 May_Hide_Profile := False;
8040 Check_Conformance
8041 (New_Id, Old_Id, Type_Conformant, False, Result,
8042 Skip_Controlling_Formals => Skip_Controlling_Formals);
8043 return Result;
8044 end Type_Conformant;
8046 -------------------------------
8047 -- Valid_Operator_Definition --
8048 -------------------------------
8050 procedure Valid_Operator_Definition (Designator : Entity_Id) is
8051 N : Integer := 0;
8052 F : Entity_Id;
8053 Id : constant Name_Id := Chars (Designator);
8054 N_OK : Boolean;
8056 begin
8057 F := First_Formal (Designator);
8058 while Present (F) loop
8059 N := N + 1;
8061 if Present (Default_Value (F)) then
8062 Error_Msg_N
8063 ("default values not allowed for operator parameters",
8064 Parent (F));
8065 end if;
8067 Next_Formal (F);
8068 end loop;
8070 -- Verify that user-defined operators have proper number of arguments
8071 -- First case of operators which can only be unary
8073 if Id = Name_Op_Not
8074 or else Id = Name_Op_Abs
8075 then
8076 N_OK := (N = 1);
8078 -- Case of operators which can be unary or binary
8080 elsif Id = Name_Op_Add
8081 or Id = Name_Op_Subtract
8082 then
8083 N_OK := (N in 1 .. 2);
8085 -- All other operators can only be binary
8087 else
8088 N_OK := (N = 2);
8089 end if;
8091 if not N_OK then
8092 Error_Msg_N
8093 ("incorrect number of arguments for operator", Designator);
8094 end if;
8096 if Id = Name_Op_Ne
8097 and then Base_Type (Etype (Designator)) = Standard_Boolean
8098 and then not Is_Intrinsic_Subprogram (Designator)
8099 then
8100 Error_Msg_N
8101 ("explicit definition of inequality not allowed", Designator);
8102 end if;
8103 end Valid_Operator_Definition;
8105 end Sem_Ch6;