PR target/16201
[official-gcc.git] / gcc / ada / sem_ch6.adb
blobbc069fa406538dc077607d1776461bf312a2f828
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-2004, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch7; use Exp_Ch7;
35 with Fname; use Fname;
36 with Freeze; use Freeze;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
39 with Lib; use Lib;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Output; use Output;
44 with Rtsfind; use Rtsfind;
45 with Sem; use Sem;
46 with Sem_Cat; use Sem_Cat;
47 with Sem_Ch3; use Sem_Ch3;
48 with Sem_Ch4; use Sem_Ch4;
49 with Sem_Ch5; use Sem_Ch5;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch10; use Sem_Ch10;
52 with Sem_Ch12; use Sem_Ch12;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Dist; use Sem_Dist;
55 with Sem_Elim; use Sem_Elim;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Mech; use Sem_Mech;
58 with Sem_Prag; use Sem_Prag;
59 with Sem_Res; use Sem_Res;
60 with Sem_Util; use Sem_Util;
61 with Sem_Type; use Sem_Type;
62 with Sem_Warn; use Sem_Warn;
63 with Sinput; use Sinput;
64 with Stand; use Stand;
65 with Sinfo; use Sinfo;
66 with Sinfo.CN; use Sinfo.CN;
67 with Snames; use Snames;
68 with Stringt; use Stringt;
69 with Style;
70 with Stylesw; use Stylesw;
71 with Tbuild; use Tbuild;
72 with Uintp; use Uintp;
73 with Urealp; use Urealp;
74 with Validsw; use Validsw;
76 package body Sem_Ch6 is
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
83 -- Analyze a generic subprogram body. N is the body to be analyzed,
84 -- and Gen_Id is the defining entity Id for the corresponding spec.
86 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
87 -- If a subprogram has pragma Inline and inlining is active, use generic
88 -- machinery to build an unexpanded body for the subprogram. This body is
89 -- subsequenty used for inline expansions at call sites. If subprogram can
90 -- be inlined (depending on size and nature of local declarations) this
91 -- function returns true. Otherwise subprogram body is treated normally.
92 -- If proper warnings are enabled and the subprogram contains a construct
93 -- that cannot be inlined, the offending construct is flagged accordingly.
95 type Conformance_Type is
96 (Type_Conformant, Mode_Conformant, Subtype_Conformant, Fully_Conformant);
97 -- Conformance type used for following call, meaning matches the
98 -- RM definitions of the corresponding terms.
100 procedure Check_Conformance
101 (New_Id : Entity_Id;
102 Old_Id : Entity_Id;
103 Ctype : Conformance_Type;
104 Errmsg : Boolean;
105 Conforms : out Boolean;
106 Err_Loc : Node_Id := Empty;
107 Get_Inst : Boolean := False);
108 -- Given two entities, this procedure checks that the profiles associated
109 -- with these entities meet the conformance criterion given by the third
110 -- parameter. If they conform, Conforms is set True and control returns
111 -- to the caller. If they do not conform, Conforms is set to False, and
112 -- in addition, if Errmsg is True on the call, proper messages are output
113 -- to complain about the conformance failure. If Err_Loc is non_Empty
114 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
115 -- error messages are placed on the appropriate part of the construct
116 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
117 -- against a formal access-to-subprogram type so Get_Instance_Of must
118 -- be called.
120 procedure Check_Overriding_Operation
121 (N : Node_Id;
122 Subp : Entity_Id);
123 -- Check that a subprogram with a pragma Overriding or Optional_Overriding
124 -- is legal. This check is performed here rather than in Sem_Prag because
125 -- the pragma must follow immediately the declaration, and can be treated
126 -- as part of the declaration itself, as described in AI-218.
128 procedure Check_Subprogram_Order (N : Node_Id);
129 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
130 -- the alpha ordering rule for N if this ordering requirement applicable.
132 procedure Check_Returns
133 (HSS : Node_Id;
134 Mode : Character;
135 Err : out Boolean);
136 -- Called to check for missing return statements in a function body,
137 -- or for returns present in a procedure body which has No_Return set.
138 -- L is the handled statement sequence for the subprogram body. This
139 -- procedure checks all flow paths to make sure they either have a
140 -- return (Mode = 'F') or do not have a return (Mode = 'P'). The flag
141 -- Err is set if there are any control paths not explicitly terminated
142 -- by a return in the function case, and is True otherwise.
144 function Conforming_Types
145 (T1 : Entity_Id;
146 T2 : Entity_Id;
147 Ctype : Conformance_Type;
148 Get_Inst : Boolean := False) return Boolean;
149 -- Check that two formal parameter types conform, checking both
150 -- for equality of base types, and where required statically
151 -- matching subtypes, depending on the setting of Ctype.
153 procedure Enter_Overloaded_Entity (S : Entity_Id);
154 -- This procedure makes S, a new overloaded entity, into the first
155 -- visible entity with that name.
157 procedure Install_Entity (E : Entity_Id);
158 -- Make single entity visible. Used for generic formals as well
160 procedure Install_Formals (Id : Entity_Id);
161 -- On entry to a subprogram body, make the formals visible. Note
162 -- that simply placing the subprogram on the scope stack is not
163 -- sufficient: the formals must become the current entities for
164 -- their names.
166 function Is_Non_Overriding_Operation
167 (Prev_E : Entity_Id;
168 New_E : Entity_Id) return Boolean;
169 -- Enforce the rule given in 12.3(18): a private operation in an instance
170 -- overrides an inherited operation only if the corresponding operation
171 -- was overriding in the generic. This can happen for primitive operations
172 -- of types derived (in the generic unit) from formal private or formal
173 -- derived types.
175 procedure Make_Inequality_Operator (S : Entity_Id);
176 -- Create the declaration for an inequality operator that is implicitly
177 -- created by a user-defined equality operator that yields a boolean.
179 procedure May_Need_Actuals (Fun : Entity_Id);
180 -- Flag functions that can be called without parameters, i.e. those that
181 -- have no parameters, or those for which defaults exist for all parameters
183 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id);
184 -- If there is a separate spec for a subprogram or generic subprogram,
185 -- the formals of the body are treated as references to the corresponding
186 -- formals of the spec. This reference does not count as an actual use of
187 -- the formal, in order to diagnose formals that are unused in the body.
189 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
190 -- Formal_Id is an formal parameter entity. This procedure deals with
191 -- setting the proper validity status for this entity, which depends
192 -- on the kind of parameter and the validity checking mode.
194 ---------------------------------------------
195 -- Analyze_Abstract_Subprogram_Declaration --
196 ---------------------------------------------
198 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
199 Designator : constant Entity_Id :=
200 Analyze_Subprogram_Specification (Specification (N));
201 Scop : constant Entity_Id := Current_Scope;
203 begin
204 Generate_Definition (Designator);
205 Set_Is_Abstract (Designator);
206 New_Overloaded_Entity (Designator);
207 Check_Delayed_Subprogram (Designator);
209 Set_Categorization_From_Scope (Designator, Scop);
211 if Ekind (Scope (Designator)) = E_Protected_Type then
212 Error_Msg_N
213 ("abstract subprogram not allowed in protected type", N);
214 end if;
216 Generate_Reference_To_Formals (Designator);
217 end Analyze_Abstract_Subprogram_Declaration;
219 ----------------------------
220 -- Analyze_Function_Call --
221 ----------------------------
223 procedure Analyze_Function_Call (N : Node_Id) is
224 P : constant Node_Id := Name (N);
225 L : constant List_Id := Parameter_Associations (N);
226 Actual : Node_Id;
228 begin
229 Analyze (P);
231 -- If error analyzing name, then set Any_Type as result type and return
233 if Etype (P) = Any_Type then
234 Set_Etype (N, Any_Type);
235 return;
236 end if;
238 -- Otherwise analyze the parameters
240 if Present (L) then
241 Actual := First (L);
243 while Present (Actual) loop
244 Analyze (Actual);
245 Check_Parameterless_Call (Actual);
246 Next (Actual);
247 end loop;
248 end if;
250 Analyze_Call (N);
251 end Analyze_Function_Call;
253 -------------------------------------
254 -- Analyze_Generic_Subprogram_Body --
255 -------------------------------------
257 procedure Analyze_Generic_Subprogram_Body
258 (N : Node_Id;
259 Gen_Id : Entity_Id)
261 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
262 Kind : constant Entity_Kind := Ekind (Gen_Id);
263 Body_Id : Entity_Id;
264 New_N : Node_Id;
265 Spec : Node_Id;
267 begin
268 -- Copy body and disable expansion while analyzing the generic
269 -- For a stub, do not copy the stub (which would load the proper body),
270 -- this will be done when the proper body is analyzed.
272 if Nkind (N) /= N_Subprogram_Body_Stub then
273 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
274 Rewrite (N, New_N);
275 Start_Generic;
276 end if;
278 Spec := Specification (N);
280 -- Within the body of the generic, the subprogram is callable, and
281 -- behaves like the corresponding non-generic unit.
283 Body_Id := Defining_Entity (Spec);
285 if Kind = E_Generic_Procedure
286 and then Nkind (Spec) /= N_Procedure_Specification
287 then
288 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
289 return;
291 elsif Kind = E_Generic_Function
292 and then Nkind (Spec) /= N_Function_Specification
293 then
294 Error_Msg_N ("invalid body for generic function ", Body_Id);
295 return;
296 end if;
298 Set_Corresponding_Body (Gen_Decl, Body_Id);
300 if Has_Completion (Gen_Id)
301 and then Nkind (Parent (N)) /= N_Subunit
302 then
303 Error_Msg_N ("duplicate generic body", N);
304 return;
305 else
306 Set_Has_Completion (Gen_Id);
307 end if;
309 if Nkind (N) = N_Subprogram_Body_Stub then
310 Set_Ekind (Defining_Entity (Specification (N)), Kind);
311 else
312 Set_Corresponding_Spec (N, Gen_Id);
313 end if;
315 if Nkind (Parent (N)) = N_Compilation_Unit then
316 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
317 end if;
319 -- Make generic parameters immediately visible in the body. They are
320 -- needed to process the formals declarations. Then make the formals
321 -- visible in a separate step.
323 New_Scope (Gen_Id);
325 declare
326 E : Entity_Id;
327 First_Ent : Entity_Id;
329 begin
330 First_Ent := First_Entity (Gen_Id);
332 E := First_Ent;
333 while Present (E) and then not Is_Formal (E) loop
334 Install_Entity (E);
335 Next_Entity (E);
336 end loop;
338 Set_Use (Generic_Formal_Declarations (Gen_Decl));
340 -- Now generic formals are visible, and the specification can be
341 -- analyzed, for subsequent conformance check.
343 Body_Id := Analyze_Subprogram_Specification (Spec);
345 -- Make formal parameters visible
347 if Present (E) then
349 -- E is the first formal parameter, we loop through the formals
350 -- installing them so that they will be visible.
352 Set_First_Entity (Gen_Id, E);
353 while Present (E) loop
354 Install_Entity (E);
355 Next_Formal (E);
356 end loop;
357 end if;
359 -- Visible generic entity is callable within its own body
361 Set_Ekind (Gen_Id, Ekind (Body_Id));
362 Set_Ekind (Body_Id, E_Subprogram_Body);
363 Set_Convention (Body_Id, Convention (Gen_Id));
364 Set_Scope (Body_Id, Scope (Gen_Id));
365 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
367 if Nkind (N) = N_Subprogram_Body_Stub then
369 -- No body to analyze, so restore state of generic unit
371 Set_Ekind (Gen_Id, Kind);
372 Set_Ekind (Body_Id, Kind);
374 if Present (First_Ent) then
375 Set_First_Entity (Gen_Id, First_Ent);
376 end if;
378 End_Scope;
379 return;
380 end if;
382 -- If this is a compilation unit, it must be made visible
383 -- explicitly, because the compilation of the declaration,
384 -- unlike other library unit declarations, does not. If it
385 -- is not a unit, the following is redundant but harmless.
387 Set_Is_Immediately_Visible (Gen_Id);
388 Reference_Body_Formals (Gen_Id, Body_Id);
390 Set_Actual_Subtypes (N, Current_Scope);
391 Analyze_Declarations (Declarations (N));
392 Check_Completion;
393 Analyze (Handled_Statement_Sequence (N));
395 Save_Global_References (Original_Node (N));
397 -- Prior to exiting the scope, include generic formals again
398 -- (if any are present) in the set of local entities.
400 if Present (First_Ent) then
401 Set_First_Entity (Gen_Id, First_Ent);
402 end if;
404 Check_References (Gen_Id);
405 end;
407 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
408 End_Scope;
409 Check_Subprogram_Order (N);
411 -- Outside of its body, unit is generic again
413 Set_Ekind (Gen_Id, Kind);
414 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
415 Style.Check_Identifier (Body_Id, Gen_Id);
416 End_Generic;
417 end Analyze_Generic_Subprogram_Body;
419 -----------------------------
420 -- Analyze_Operator_Symbol --
421 -----------------------------
423 -- An operator symbol such as "+" or "and" may appear in context where
424 -- the literal denotes an entity name, such as "+"(x, y) or in a
425 -- context when it is just a string, as in (conjunction = "or"). In
426 -- these cases the parser generates this node, and the semantics does
427 -- the disambiguation. Other such case are actuals in an instantiation,
428 -- the generic unit in an instantiation, and pragma arguments.
430 procedure Analyze_Operator_Symbol (N : Node_Id) is
431 Par : constant Node_Id := Parent (N);
433 begin
434 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
435 or else Nkind (Par) = N_Function_Instantiation
436 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
437 or else (Nkind (Par) = N_Pragma_Argument_Association
438 and then not Is_Pragma_String_Literal (Par))
439 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
440 or else (Nkind (Par) = N_Attribute_Reference
441 and then Attribute_Name (Par) /= Name_Value)
442 then
443 Find_Direct_Name (N);
445 else
446 Change_Operator_Symbol_To_String_Literal (N);
447 Analyze (N);
448 end if;
449 end Analyze_Operator_Symbol;
451 -----------------------------------
452 -- Analyze_Parameter_Association --
453 -----------------------------------
455 procedure Analyze_Parameter_Association (N : Node_Id) is
456 begin
457 Analyze (Explicit_Actual_Parameter (N));
458 end Analyze_Parameter_Association;
460 ----------------------------
461 -- Analyze_Procedure_Call --
462 ----------------------------
464 procedure Analyze_Procedure_Call (N : Node_Id) is
465 Loc : constant Source_Ptr := Sloc (N);
466 P : constant Node_Id := Name (N);
467 Actuals : constant List_Id := Parameter_Associations (N);
468 Actual : Node_Id;
469 New_N : Node_Id;
471 procedure Analyze_Call_And_Resolve;
472 -- Do Analyze and Resolve calls for procedure call
474 ------------------------------
475 -- Analyze_Call_And_Resolve --
476 ------------------------------
478 procedure Analyze_Call_And_Resolve is
479 begin
480 if Nkind (N) = N_Procedure_Call_Statement then
481 Analyze_Call (N);
482 Resolve (N, Standard_Void_Type);
483 else
484 Analyze (N);
485 end if;
486 end Analyze_Call_And_Resolve;
488 -- Start of processing for Analyze_Procedure_Call
490 begin
491 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
492 -- a procedure call or an entry call. The prefix may denote an access
493 -- to subprogram type, in which case an implicit dereference applies.
494 -- If the prefix is an indexed component (without implicit defererence)
495 -- then the construct denotes a call to a member of an entire family.
496 -- If the prefix is a simple name, it may still denote a call to a
497 -- parameterless member of an entry family. Resolution of these various
498 -- interpretations is delicate.
500 Analyze (P);
502 -- If error analyzing prefix, then set Any_Type as result and return
504 if Etype (P) = Any_Type then
505 Set_Etype (N, Any_Type);
506 return;
507 end if;
509 -- Otherwise analyze the parameters
511 if Present (Actuals) then
512 Actual := First (Actuals);
514 while Present (Actual) loop
515 Analyze (Actual);
516 Check_Parameterless_Call (Actual);
517 Next (Actual);
518 end loop;
519 end if;
521 -- Special processing for Elab_Spec and Elab_Body calls
523 if Nkind (P) = N_Attribute_Reference
524 and then (Attribute_Name (P) = Name_Elab_Spec
525 or else Attribute_Name (P) = Name_Elab_Body)
526 then
527 if Present (Actuals) then
528 Error_Msg_N
529 ("no parameters allowed for this call", First (Actuals));
530 return;
531 end if;
533 Set_Etype (N, Standard_Void_Type);
534 Set_Analyzed (N);
536 elsif Is_Entity_Name (P)
537 and then Is_Record_Type (Etype (Entity (P)))
538 and then Remote_AST_I_Dereference (P)
539 then
540 return;
542 elsif Is_Entity_Name (P)
543 and then Ekind (Entity (P)) /= E_Entry_Family
544 then
545 if Is_Access_Type (Etype (P))
546 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
547 and then No (Actuals)
548 and then Comes_From_Source (N)
549 then
550 Error_Msg_N ("missing explicit dereference in call", N);
551 end if;
553 Analyze_Call_And_Resolve;
555 -- If the prefix is the simple name of an entry family, this is
556 -- a parameterless call from within the task body itself.
558 elsif Is_Entity_Name (P)
559 and then Nkind (P) = N_Identifier
560 and then Ekind (Entity (P)) = E_Entry_Family
561 and then Present (Actuals)
562 and then No (Next (First (Actuals)))
563 then
564 -- Can be call to parameterless entry family. What appears to be
565 -- the sole argument is in fact the entry index. Rewrite prefix
566 -- of node accordingly. Source representation is unchanged by this
567 -- transformation.
569 New_N :=
570 Make_Indexed_Component (Loc,
571 Prefix =>
572 Make_Selected_Component (Loc,
573 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
574 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
575 Expressions => Actuals);
576 Set_Name (N, New_N);
577 Set_Etype (New_N, Standard_Void_Type);
578 Set_Parameter_Associations (N, No_List);
579 Analyze_Call_And_Resolve;
581 elsif Nkind (P) = N_Explicit_Dereference then
582 if Ekind (Etype (P)) = E_Subprogram_Type then
583 Analyze_Call_And_Resolve;
584 else
585 Error_Msg_N ("expect access to procedure in call", P);
586 end if;
588 -- The name can be a selected component or an indexed component
589 -- that yields an access to subprogram. Such a prefix is legal if
590 -- the call has parameter associations.
592 elsif Is_Access_Type (Etype (P))
593 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
594 then
595 if Present (Actuals) then
596 Analyze_Call_And_Resolve;
597 else
598 Error_Msg_N ("missing explicit dereference in call ", N);
599 end if;
601 -- If not an access to subprogram, then the prefix must resolve to
602 -- the name of an entry, entry family, or protected operation.
604 -- For the case of a simple entry call, P is a selected component
605 -- where the prefix is the task and the selector name is the entry.
606 -- A call to a protected procedure will have the same syntax. If
607 -- the protected object contains overloaded operations, the entity
608 -- may appear as a function, the context will select the operation
609 -- whose type is Void.
611 elsif Nkind (P) = N_Selected_Component
612 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
613 or else
614 Ekind (Entity (Selector_Name (P))) = E_Procedure
615 or else
616 Ekind (Entity (Selector_Name (P))) = E_Function)
617 then
618 Analyze_Call_And_Resolve;
620 elsif Nkind (P) = N_Selected_Component
621 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
622 and then Present (Actuals)
623 and then No (Next (First (Actuals)))
624 then
625 -- Can be call to parameterless entry family. What appears to be
626 -- the sole argument is in fact the entry index. Rewrite prefix
627 -- of node accordingly. Source representation is unchanged by this
628 -- transformation.
630 New_N :=
631 Make_Indexed_Component (Loc,
632 Prefix => New_Copy (P),
633 Expressions => Actuals);
634 Set_Name (N, New_N);
635 Set_Etype (New_N, Standard_Void_Type);
636 Set_Parameter_Associations (N, No_List);
637 Analyze_Call_And_Resolve;
639 -- For the case of a reference to an element of an entry family, P is
640 -- an indexed component whose prefix is a selected component (task and
641 -- entry family), and whose index is the entry family index.
643 elsif Nkind (P) = N_Indexed_Component
644 and then Nkind (Prefix (P)) = N_Selected_Component
645 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
646 then
647 Analyze_Call_And_Resolve;
649 -- If the prefix is the name of an entry family, it is a call from
650 -- within the task body itself.
652 elsif Nkind (P) = N_Indexed_Component
653 and then Nkind (Prefix (P)) = N_Identifier
654 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
655 then
656 New_N :=
657 Make_Selected_Component (Loc,
658 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
659 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
660 Rewrite (Prefix (P), New_N);
661 Analyze (P);
662 Analyze_Call_And_Resolve;
664 -- Anything else is an error
666 else
667 Error_Msg_N ("Invalid procedure or entry call", N);
668 end if;
669 end Analyze_Procedure_Call;
671 ------------------------------
672 -- Analyze_Return_Statement --
673 ------------------------------
675 procedure Analyze_Return_Statement (N : Node_Id) is
676 Loc : constant Source_Ptr := Sloc (N);
677 Expr : Node_Id;
678 Scope_Id : Entity_Id;
679 Kind : Entity_Kind;
680 R_Type : Entity_Id;
682 begin
683 -- Find subprogram or accept statement enclosing the return statement
685 Scope_Id := Empty;
686 for J in reverse 0 .. Scope_Stack.Last loop
687 Scope_Id := Scope_Stack.Table (J).Entity;
688 exit when Ekind (Scope_Id) /= E_Block and then
689 Ekind (Scope_Id) /= E_Loop;
690 end loop;
692 pragma Assert (Present (Scope_Id));
694 Kind := Ekind (Scope_Id);
695 Expr := Expression (N);
697 if Kind /= E_Function
698 and then Kind /= E_Generic_Function
699 and then Kind /= E_Procedure
700 and then Kind /= E_Generic_Procedure
701 and then Kind /= E_Entry
702 and then Kind /= E_Entry_Family
703 then
704 Error_Msg_N ("illegal context for return statement", N);
706 elsif Present (Expr) then
707 if Kind = E_Function or else Kind = E_Generic_Function then
708 Set_Return_Present (Scope_Id);
709 R_Type := Etype (Scope_Id);
710 Set_Return_Type (N, R_Type);
711 Analyze_And_Resolve (Expr, R_Type);
713 if (Is_Class_Wide_Type (Etype (Expr))
714 or else Is_Dynamically_Tagged (Expr))
715 and then not Is_Class_Wide_Type (R_Type)
716 then
717 Error_Msg_N
718 ("dynamically tagged expression not allowed!", Expr);
719 end if;
721 Apply_Constraint_Check (Expr, R_Type);
723 -- ??? A real run-time accessibility check is needed
724 -- in cases involving dereferences of access parameters.
725 -- For now we just check the static cases.
727 if Is_Return_By_Reference_Type (Etype (Scope_Id))
728 and then Object_Access_Level (Expr)
729 > Subprogram_Access_Level (Scope_Id)
730 then
731 Rewrite (N,
732 Make_Raise_Program_Error (Loc,
733 Reason => PE_Accessibility_Check_Failed));
734 Analyze (N);
736 Error_Msg_N
737 ("cannot return a local value by reference?", N);
738 Error_Msg_NE
739 ("& will be raised at run time?!",
740 N, Standard_Program_Error);
741 end if;
743 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
744 Error_Msg_N ("procedure cannot return value (use function)", N);
746 else
747 Error_Msg_N ("accept statement cannot return value", N);
748 end if;
750 -- No expression present
752 else
753 if Kind = E_Function or Kind = E_Generic_Function then
754 Error_Msg_N ("missing expression in return from function", N);
755 end if;
757 if (Ekind (Scope_Id) = E_Procedure
758 or else Ekind (Scope_Id) = E_Generic_Procedure)
759 and then No_Return (Scope_Id)
760 then
761 Error_Msg_N
762 ("RETURN statement not allowed (No_Return)", N);
763 end if;
764 end if;
766 Check_Unreachable_Code (N);
767 end Analyze_Return_Statement;
769 -----------------------------
770 -- Analyze_Subprogram_Body --
771 -----------------------------
773 -- This procedure is called for regular subprogram bodies, generic bodies,
774 -- and for subprogram stubs of both kinds. In the case of stubs, only the
775 -- specification matters, and is used to create a proper declaration for
776 -- the subprogram, or to perform conformance checks.
778 procedure Analyze_Subprogram_Body (N : Node_Id) is
779 Loc : constant Source_Ptr := Sloc (N);
780 Body_Spec : constant Node_Id := Specification (N);
781 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
782 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
783 Body_Deleted : constant Boolean := False;
785 HSS : Node_Id;
786 Spec_Id : Entity_Id;
787 Spec_Decl : Node_Id := Empty;
788 Last_Formal : Entity_Id := Empty;
789 Conformant : Boolean;
790 Missing_Ret : Boolean;
791 P_Ent : Entity_Id;
793 procedure Check_Following_Pragma;
794 -- If front-end inlining is enabled, look ahead to recognize a pragma
795 -- that may appear after the body.
797 procedure Check_Following_Pragma is
798 Prag : Node_Id;
800 begin
801 if Front_End_Inlining
802 and then Is_List_Member (N)
803 and then Present (Spec_Decl)
804 and then List_Containing (N) = List_Containing (Spec_Decl)
805 then
806 Prag := Next (N);
808 if Present (Prag)
809 and then Nkind (Prag) = N_Pragma
810 and then Get_Pragma_Id (Chars (Prag)) = Pragma_Inline
811 and then
812 Chars
813 (Expression (First (Pragma_Argument_Associations (Prag))))
814 = Chars (Body_Id)
815 then
816 Analyze (Prag);
817 end if;
818 end if;
819 end Check_Following_Pragma;
821 -- Start of processing for Analyze_Subprogram_Body
823 begin
824 if Debug_Flag_C then
825 Write_Str ("==== Compiling subprogram body ");
826 Write_Name (Chars (Body_Id));
827 Write_Str (" from ");
828 Write_Location (Loc);
829 Write_Eol;
830 end if;
832 Trace_Scope (N, Body_Id, " Analyze subprogram");
834 -- Generic subprograms are handled separately. They always have
835 -- a generic specification. Determine whether current scope has
836 -- a previous declaration.
838 -- If the subprogram body is defined within an instance of the
839 -- same name, the instance appears as a package renaming, and
840 -- will be hidden within the subprogram.
842 if Present (Prev_Id)
843 and then not Is_Overloadable (Prev_Id)
844 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
845 or else Comes_From_Source (Prev_Id))
846 then
847 if Is_Generic_Subprogram (Prev_Id) then
848 Spec_Id := Prev_Id;
849 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
850 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
852 Analyze_Generic_Subprogram_Body (N, Spec_Id);
853 return;
855 else
856 -- Previous entity conflicts with subprogram name.
857 -- Attempting to enter name will post error.
859 Enter_Name (Body_Id);
860 return;
861 end if;
863 -- Non-generic case, find the subprogram declaration, if one was
864 -- seen, or enter new overloaded entity in the current scope.
865 -- If the current_entity is the body_id itself, the unit is being
866 -- analyzed as part of the context of one of its subunits. No need
867 -- to redo the analysis.
869 elsif Prev_Id = Body_Id
870 and then Has_Completion (Body_Id)
871 then
872 return;
874 else
875 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
877 if Nkind (N) = N_Subprogram_Body_Stub
878 or else No (Corresponding_Spec (N))
879 then
880 Spec_Id := Find_Corresponding_Spec (N);
882 -- If this is a duplicate body, no point in analyzing it
884 if Error_Posted (N) then
885 return;
886 end if;
888 -- A subprogram body should cause freezing of its own
889 -- declaration, but if there was no previous explicit
890 -- declaration, then the subprogram will get frozen too
891 -- late (there may be code within the body that depends
892 -- on the subprogram having been frozen, such as uses of
893 -- extra formals), so we force it to be frozen here.
894 -- Same holds if the body and the spec are compilation units.
896 if No (Spec_Id) then
897 Freeze_Before (N, Body_Id);
899 elsif Nkind (Parent (N)) = N_Compilation_Unit then
900 Freeze_Before (N, Spec_Id);
901 end if;
902 else
903 Spec_Id := Corresponding_Spec (N);
904 end if;
905 end if;
907 -- Do not inline any subprogram that contains nested subprograms,
908 -- since the backend inlining circuit seems to generate uninitialized
909 -- references in this case. We know this happens in the case of front
910 -- end ZCX support, but it also appears it can happen in other cases
911 -- as well. The backend often rejects attempts to inline in the case
912 -- of nested procedures anyway, so little if anything is lost by this.
914 -- Do not do this test if errors have been detected, because in some
915 -- error cases, this code blows up, and we don't need it anyway if
916 -- there have been errors, since we won't get to the linker anyway.
918 if Serious_Errors_Detected = 0 then
919 P_Ent := Body_Id;
920 loop
921 P_Ent := Scope (P_Ent);
922 exit when No (P_Ent) or else P_Ent = Standard_Standard;
924 if Is_Subprogram (P_Ent) then
925 Set_Is_Inlined (P_Ent, False);
927 if Comes_From_Source (P_Ent)
928 and then Has_Pragma_Inline (P_Ent)
929 then
930 Cannot_Inline
931 ("cannot inline& (nested subprogram)?",
932 N, P_Ent);
933 end if;
934 end if;
935 end loop;
936 end if;
938 -- Case of fully private operation in the body of the protected type.
939 -- We must create a declaration for the subprogram, in order to attach
940 -- the protected subprogram that will be used in internal calls.
942 if No (Spec_Id)
943 and then Comes_From_Source (N)
944 and then Is_Protected_Type (Current_Scope)
945 then
946 declare
947 Decl : Node_Id;
948 Plist : List_Id;
949 Formal : Entity_Id;
950 New_Spec : Node_Id;
952 begin
953 Formal := First_Formal (Body_Id);
955 -- The protected operation always has at least one formal,
956 -- namely the object itself, but it is only placed in the
957 -- parameter list if expansion is enabled.
959 if Present (Formal)
960 or else Expander_Active
961 then
962 Plist := New_List;
964 else
965 Plist := No_List;
966 end if;
968 while Present (Formal) loop
969 Append
970 (Make_Parameter_Specification (Loc,
971 Defining_Identifier =>
972 Make_Defining_Identifier (Sloc (Formal),
973 Chars => Chars (Formal)),
974 In_Present => In_Present (Parent (Formal)),
975 Out_Present => Out_Present (Parent (Formal)),
976 Parameter_Type =>
977 New_Reference_To (Etype (Formal), Loc),
978 Expression =>
979 New_Copy_Tree (Expression (Parent (Formal)))),
980 Plist);
982 Next_Formal (Formal);
983 end loop;
985 if Nkind (Body_Spec) = N_Procedure_Specification then
986 New_Spec :=
987 Make_Procedure_Specification (Loc,
988 Defining_Unit_Name =>
989 Make_Defining_Identifier (Sloc (Body_Id),
990 Chars => Chars (Body_Id)),
991 Parameter_Specifications => Plist);
992 else
993 New_Spec :=
994 Make_Function_Specification (Loc,
995 Defining_Unit_Name =>
996 Make_Defining_Identifier (Sloc (Body_Id),
997 Chars => Chars (Body_Id)),
998 Parameter_Specifications => Plist,
999 Subtype_Mark => New_Occurrence_Of (Etype (Body_Id), Loc));
1000 end if;
1002 Decl :=
1003 Make_Subprogram_Declaration (Loc,
1004 Specification => New_Spec);
1005 Insert_Before (N, Decl);
1006 Spec_Id := Defining_Unit_Name (New_Spec);
1008 -- Indicate that the entity comes from source, to ensure that
1009 -- cross-reference information is properly generated.
1010 -- The body itself is rewritten during expansion, and the
1011 -- body entity will not appear in calls to the operation.
1013 Set_Comes_From_Source (Spec_Id, True);
1014 Analyze (Decl);
1015 Set_Has_Completion (Spec_Id);
1016 Set_Convention (Spec_Id, Convention_Protected);
1017 end;
1019 elsif Present (Spec_Id) then
1020 Spec_Decl := Unit_Declaration_Node (Spec_Id);
1021 end if;
1023 -- Place subprogram on scope stack, and make formals visible. If there
1024 -- is a spec, the visible entity remains that of the spec.
1026 if Present (Spec_Id) then
1027 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1028 if Style_Check then
1029 Style.Check_Identifier (Body_Id, Spec_Id);
1030 end if;
1032 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1033 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1035 if Is_Abstract (Spec_Id) then
1036 Error_Msg_N ("an abstract subprogram cannot have a body", N);
1037 return;
1038 else
1039 Set_Convention (Body_Id, Convention (Spec_Id));
1040 Set_Has_Completion (Spec_Id);
1042 if Is_Protected_Type (Scope (Spec_Id)) then
1043 Set_Privals_Chain (Spec_Id, New_Elmt_List);
1044 end if;
1046 -- If this is a body generated for a renaming, do not check for
1047 -- full conformance. The check is redundant, because the spec of
1048 -- the body is a copy of the spec in the renaming declaration,
1049 -- and the test can lead to spurious errors on nested defaults.
1051 if Present (Spec_Decl)
1052 and then not Comes_From_Source (N)
1053 and then
1054 (Nkind (Original_Node (Spec_Decl)) =
1055 N_Subprogram_Renaming_Declaration
1056 or else (Present (Corresponding_Body (Spec_Decl))
1057 and then
1058 Nkind (Unit_Declaration_Node
1059 (Corresponding_Body (Spec_Decl))) =
1060 N_Subprogram_Renaming_Declaration))
1061 then
1062 Conformant := True;
1063 else
1064 Check_Conformance
1065 (Body_Id, Spec_Id,
1066 Fully_Conformant, True, Conformant, Body_Id);
1067 end if;
1069 -- If the body is not fully conformant, we have to decide if we
1070 -- should analyze it or not. If it has a really messed up profile
1071 -- then we probably should not analyze it, since we will get too
1072 -- many bogus messages.
1074 -- Our decision is to go ahead in the non-fully conformant case
1075 -- only if it is at least mode conformant with the spec. Note
1076 -- that the call to Check_Fully_Conformant has issued the proper
1077 -- error messages to complain about the lack of conformance.
1079 if not Conformant
1080 and then not Mode_Conformant (Body_Id, Spec_Id)
1081 then
1082 return;
1083 end if;
1084 end if;
1086 if Spec_Id /= Body_Id then
1087 Reference_Body_Formals (Spec_Id, Body_Id);
1088 end if;
1090 if Nkind (N) /= N_Subprogram_Body_Stub then
1091 Set_Corresponding_Spec (N, Spec_Id);
1092 Install_Formals (Spec_Id);
1093 Last_Formal := Last_Entity (Spec_Id);
1094 New_Scope (Spec_Id);
1096 -- Make sure that the subprogram is immediately visible. For
1097 -- child units that have no separate spec this is indispensable.
1098 -- Otherwise it is safe albeit redundant.
1100 Set_Is_Immediately_Visible (Spec_Id);
1101 end if;
1103 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
1104 Set_Ekind (Body_Id, E_Subprogram_Body);
1105 Set_Scope (Body_Id, Scope (Spec_Id));
1107 -- Case of subprogram body with no previous spec
1109 else
1110 if Style_Check
1111 and then Comes_From_Source (Body_Id)
1112 and then not Suppress_Style_Checks (Body_Id)
1113 and then not In_Instance
1114 then
1115 Style.Body_With_No_Spec (N);
1116 end if;
1118 New_Overloaded_Entity (Body_Id);
1120 if Nkind (N) /= N_Subprogram_Body_Stub then
1121 Set_Acts_As_Spec (N);
1122 Generate_Definition (Body_Id);
1123 Generate_Reference
1124 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
1125 Generate_Reference_To_Formals (Body_Id);
1126 Install_Formals (Body_Id);
1127 New_Scope (Body_Id);
1128 end if;
1129 end if;
1131 -- If this is the proper body of a stub, we must verify that the stub
1132 -- conforms to the body, and to the previous spec if one was present.
1133 -- we know already that the body conforms to that spec. This test is
1134 -- only required for subprograms that come from source.
1136 if Nkind (Parent (N)) = N_Subunit
1137 and then Comes_From_Source (N)
1138 and then not Error_Posted (Body_Id)
1139 and then Nkind (Corresponding_Stub (Parent (N))) =
1140 N_Subprogram_Body_Stub
1141 then
1142 declare
1143 Old_Id : constant Entity_Id :=
1144 Defining_Entity
1145 (Specification (Corresponding_Stub (Parent (N))));
1147 Conformant : Boolean := False;
1149 begin
1150 if No (Spec_Id) then
1151 Check_Fully_Conformant (Body_Id, Old_Id);
1153 else
1154 Check_Conformance
1155 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
1157 if not Conformant then
1159 -- The stub was taken to be a new declaration. Indicate
1160 -- that it lacks a body.
1162 Set_Has_Completion (Old_Id, False);
1163 end if;
1164 end if;
1165 end;
1166 end if;
1168 Set_Has_Completion (Body_Id);
1169 Check_Eliminated (Body_Id);
1171 if Nkind (N) = N_Subprogram_Body_Stub then
1172 return;
1174 elsif Present (Spec_Id)
1175 and then Expander_Active
1176 then
1177 Check_Following_Pragma;
1179 if Is_Always_Inlined (Spec_Id)
1180 or else (Has_Pragma_Inline (Spec_Id) and then Front_End_Inlining)
1181 then
1182 Build_Body_To_Inline (N, Spec_Id);
1183 end if;
1184 end if;
1186 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
1187 -- if its specification we have to install the private withed units.
1189 if Is_Compilation_Unit (Body_Id)
1190 and then Scope (Body_Id) = Standard_Standard
1191 then
1192 Install_Private_With_Clauses (Body_Id);
1193 end if;
1195 -- Now we can go on to analyze the body
1197 HSS := Handled_Statement_Sequence (N);
1198 Set_Actual_Subtypes (N, Current_Scope);
1199 Analyze_Declarations (Declarations (N));
1200 Check_Completion;
1201 Analyze (HSS);
1202 Process_End_Label (HSS, 't', Current_Scope);
1203 End_Scope;
1204 Check_Subprogram_Order (N);
1205 Set_Analyzed (Body_Id);
1207 -- If we have a separate spec, then the analysis of the declarations
1208 -- caused the entities in the body to be chained to the spec id, but
1209 -- we want them chained to the body id. Only the formal parameters
1210 -- end up chained to the spec id in this case.
1212 if Present (Spec_Id) then
1214 -- If a parent unit is categorized, the context of a subunit
1215 -- must conform to the categorization. Conversely, if a child
1216 -- unit is categorized, the parents themselves must conform.
1218 if Nkind (Parent (N)) = N_Subunit then
1219 Validate_Categorization_Dependency (N, Spec_Id);
1221 elsif Is_Child_Unit (Spec_Id) then
1222 Validate_Categorization_Dependency
1223 (Unit_Declaration_Node (Spec_Id), Spec_Id);
1224 end if;
1226 if Present (Last_Formal) then
1227 Set_Next_Entity
1228 (Last_Entity (Body_Id), Next_Entity (Last_Formal));
1229 Set_Next_Entity (Last_Formal, Empty);
1230 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1231 Set_Last_Entity (Spec_Id, Last_Formal);
1233 else
1234 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1235 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1236 Set_First_Entity (Spec_Id, Empty);
1237 Set_Last_Entity (Spec_Id, Empty);
1238 end if;
1239 end if;
1241 -- If function, check return statements
1243 if Nkind (Body_Spec) = N_Function_Specification then
1244 declare
1245 Id : Entity_Id;
1247 begin
1248 if Present (Spec_Id) then
1249 Id := Spec_Id;
1250 else
1251 Id := Body_Id;
1252 end if;
1254 if Return_Present (Id) then
1255 Check_Returns (HSS, 'F', Missing_Ret);
1257 if Missing_Ret then
1258 Set_Has_Missing_Return (Id);
1259 end if;
1261 elsif not Is_Machine_Code_Subprogram (Id)
1262 and then not Body_Deleted
1263 then
1264 Error_Msg_N ("missing RETURN statement in function body", N);
1265 end if;
1266 end;
1268 -- If procedure with No_Return, check returns
1270 elsif Nkind (Body_Spec) = N_Procedure_Specification
1271 and then Present (Spec_Id)
1272 and then No_Return (Spec_Id)
1273 then
1274 Check_Returns (HSS, 'P', Missing_Ret);
1275 end if;
1277 -- Now we are going to check for variables that are never modified
1278 -- in the body of the procedure. We omit these checks if the first
1279 -- statement of the procedure raises an exception. In particular
1280 -- this deals with the common idiom of a stubbed function, which
1281 -- might appear as something like
1283 -- function F (A : Integer) return Some_Type;
1284 -- X : Some_Type;
1285 -- begin
1286 -- raise Program_Error;
1287 -- return X;
1288 -- end F;
1290 -- Here the purpose of X is simply to satisfy the (annoying)
1291 -- requirement in Ada that there be at least one return, and
1292 -- we certainly do not want to go posting warnings on X that
1293 -- it is not initialized!
1295 declare
1296 Stm : Node_Id := First (Statements (HSS));
1298 begin
1299 -- Skip an initial label (for one thing this occurs when we
1300 -- are in front end ZCX mode, but in any case it is irrelevant).
1302 if Nkind (Stm) = N_Label then
1303 Next (Stm);
1304 end if;
1306 -- Do the test on the original statement before expansion
1308 declare
1309 Ostm : constant Node_Id := Original_Node (Stm);
1311 begin
1312 -- If explicit raise statement, return with no checks
1314 if Nkind (Ostm) = N_Raise_Statement then
1315 return;
1317 -- Check for explicit call cases which likely raise an exception
1319 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
1320 if Is_Entity_Name (Name (Ostm)) then
1321 declare
1322 Ent : constant Entity_Id := Entity (Name (Ostm));
1324 begin
1325 -- If the procedure is marked No_Return, then likely it
1326 -- raises an exception, but in any case it is not coming
1327 -- back here, so no need to check beyond the call.
1329 if Ekind (Ent) = E_Procedure
1330 and then No_Return (Ent)
1331 then
1332 return;
1334 -- If the procedure name is Raise_Exception, then also
1335 -- assume that it raises an exception. The main target
1336 -- here is Ada.Exceptions.Raise_Exception, but this name
1337 -- is pretty evocative in any context! Note that the
1338 -- procedure in Ada.Exceptions is not marked No_Return
1339 -- because of the annoying case of the null exception Id.
1341 elsif Chars (Ent) = Name_Raise_Exception then
1342 return;
1343 end if;
1344 end;
1345 end if;
1346 end if;
1347 end;
1348 end;
1350 -- Check for variables that are never modified
1352 declare
1353 E1, E2 : Entity_Id;
1355 begin
1356 -- If there is a separate spec, then transfer Never_Set_In_Source
1357 -- flags from out parameters to the corresponding entities in the
1358 -- body. The reason we do that is we want to post error flags on
1359 -- the body entities, not the spec entities.
1361 if Present (Spec_Id) then
1362 E1 := First_Entity (Spec_Id);
1364 while Present (E1) loop
1365 if Ekind (E1) = E_Out_Parameter then
1366 E2 := First_Entity (Body_Id);
1367 while Present (E2) loop
1368 exit when Chars (E1) = Chars (E2);
1369 Next_Entity (E2);
1370 end loop;
1372 if Present (E2) then
1373 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
1374 end if;
1375 end if;
1377 Next_Entity (E1);
1378 end loop;
1379 end if;
1381 -- Check references in body unless it was deleted. Note that the
1382 -- check of Body_Deleted here is not just for efficiency, it is
1383 -- necessary to avoid junk warnings on formal parameters.
1385 if not Body_Deleted then
1386 Check_References (Body_Id);
1387 end if;
1388 end;
1389 end Analyze_Subprogram_Body;
1391 ------------------------------------
1392 -- Analyze_Subprogram_Declaration --
1393 ------------------------------------
1395 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
1396 Designator : constant Entity_Id :=
1397 Analyze_Subprogram_Specification (Specification (N));
1398 Scop : constant Entity_Id := Current_Scope;
1400 -- Start of processing for Analyze_Subprogram_Declaration
1402 begin
1403 Generate_Definition (Designator);
1405 -- Check for RCI unit subprogram declarations against in-lined
1406 -- subprograms and subprograms having access parameter or limited
1407 -- parameter without Read and Write (RM E.2.3(12-13)).
1409 Validate_RCI_Subprogram_Declaration (N);
1411 Trace_Scope
1413 Defining_Entity (N),
1414 " Analyze subprogram spec. ");
1416 if Debug_Flag_C then
1417 Write_Str ("==== Compiling subprogram spec ");
1418 Write_Name (Chars (Designator));
1419 Write_Str (" from ");
1420 Write_Location (Sloc (N));
1421 Write_Eol;
1422 end if;
1424 New_Overloaded_Entity (Designator);
1425 Check_Delayed_Subprogram (Designator);
1427 -- What is the following code for, it used to be
1429 -- ??? Set_Suppress_Elaboration_Checks
1430 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
1432 -- The following seems equivalent, but a bit dubious
1434 if Elaboration_Checks_Suppressed (Designator) then
1435 Set_Kill_Elaboration_Checks (Designator);
1436 end if;
1438 if Scop /= Standard_Standard
1439 and then not Is_Child_Unit (Designator)
1440 then
1441 Set_Categorization_From_Scope (Designator, Scop);
1442 else
1443 -- For a compilation unit, check for library-unit pragmas
1445 New_Scope (Designator);
1446 Set_Categorization_From_Pragmas (N);
1447 Validate_Categorization_Dependency (N, Designator);
1448 Pop_Scope;
1449 end if;
1451 -- For a compilation unit, set body required. This flag will only be
1452 -- reset if a valid Import or Interface pragma is processed later on.
1454 if Nkind (Parent (N)) = N_Compilation_Unit then
1455 Set_Body_Required (Parent (N), True);
1456 end if;
1458 Generate_Reference_To_Formals (Designator);
1459 Check_Eliminated (Designator);
1461 if Comes_From_Source (N)
1462 and then Is_List_Member (N)
1463 then
1464 Check_Overriding_Operation (N, Designator);
1465 end if;
1467 end Analyze_Subprogram_Declaration;
1469 --------------------------------------
1470 -- Analyze_Subprogram_Specification --
1471 --------------------------------------
1473 -- Reminder: N here really is a subprogram specification (not a subprogram
1474 -- declaration). This procedure is called to analyze the specification in
1475 -- both subprogram bodies and subprogram declarations (specs).
1477 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
1478 Designator : constant Entity_Id := Defining_Entity (N);
1479 Formals : constant List_Id := Parameter_Specifications (N);
1480 Typ : Entity_Id;
1482 begin
1483 Generate_Definition (Designator);
1485 if Nkind (N) = N_Function_Specification then
1486 Set_Ekind (Designator, E_Function);
1487 Set_Mechanism (Designator, Default_Mechanism);
1489 if Subtype_Mark (N) /= Error then
1490 Find_Type (Subtype_Mark (N));
1491 Typ := Entity (Subtype_Mark (N));
1492 Set_Etype (Designator, Typ);
1494 if Ekind (Typ) = E_Incomplete_Type
1495 or else (Is_Class_Wide_Type (Typ)
1496 and then
1497 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1498 then
1499 Error_Msg_N
1500 ("invalid use of incomplete type", Subtype_Mark (N));
1501 end if;
1503 else
1504 Set_Etype (Designator, Any_Type);
1505 end if;
1507 else
1508 Set_Ekind (Designator, E_Procedure);
1509 Set_Etype (Designator, Standard_Void_Type);
1510 end if;
1512 if Present (Formals) then
1513 Set_Scope (Designator, Current_Scope);
1514 New_Scope (Designator);
1515 Process_Formals (Formals, N);
1516 End_Scope;
1517 end if;
1519 if Nkind (N) = N_Function_Specification then
1520 if Nkind (Designator) = N_Defining_Operator_Symbol then
1521 Valid_Operator_Definition (Designator);
1522 end if;
1524 May_Need_Actuals (Designator);
1526 if Is_Abstract (Etype (Designator))
1527 and then Nkind (Parent (N)) /= N_Abstract_Subprogram_Declaration
1528 then
1529 Error_Msg_N
1530 ("function that returns abstract type must be abstract", N);
1531 end if;
1532 end if;
1534 return Designator;
1535 end Analyze_Subprogram_Specification;
1537 --------------------------
1538 -- Build_Body_To_Inline --
1539 --------------------------
1541 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
1542 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
1543 Original_Body : Node_Id;
1544 Body_To_Analyze : Node_Id;
1545 Max_Size : constant := 10;
1546 Stat_Count : Integer := 0;
1548 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
1549 -- Check for declarations that make inlining not worthwhile
1551 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
1552 -- Check for statements that make inlining not worthwhile: any
1553 -- tasking statement, nested at any level. Keep track of total
1554 -- number of elementary statements, as a measure of acceptable size.
1556 function Has_Pending_Instantiation return Boolean;
1557 -- If some enclosing body contains instantiations that appear before
1558 -- the corresponding generic body, the enclosing body has a freeze node
1559 -- so that it can be elaborated after the generic itself. This might
1560 -- conflict with subsequent inlinings, so that it is unsafe to try to
1561 -- inline in such a case.
1563 procedure Remove_Pragmas;
1564 -- A pragma Unreferenced that mentions a formal parameter has no
1565 -- meaning when the body is inlined and the formals are rewritten.
1566 -- Remove it from body to inline. The analysis of the non-inlined
1567 -- body will handle the pragma properly.
1569 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
1570 -- If the body of the subprogram includes a call that returns an
1571 -- unconstrained type, the secondary stack is involved, and it
1572 -- is not worth inlining.
1574 ------------------------------
1575 -- Has_Excluded_Declaration --
1576 ------------------------------
1578 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
1579 D : Node_Id;
1581 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
1582 -- Nested subprograms make a given body ineligible for inlining,
1583 -- but we make an exception for instantiations of unchecked
1584 -- conversion. The body has not been analyzed yet, so we check
1585 -- the name, and verify that the visible entity with that name is
1586 -- the predefined unit.
1588 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
1589 Id : constant Node_Id := Name (D);
1590 Conv : Entity_Id;
1592 begin
1593 if Nkind (Id) = N_Identifier
1594 and then Chars (Id) = Name_Unchecked_Conversion
1595 then
1596 Conv := Current_Entity (Id);
1598 elsif Nkind (Id) = N_Selected_Component
1599 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
1600 then
1601 Conv := Current_Entity (Selector_Name (Id));
1603 else
1604 return False;
1605 end if;
1607 return
1608 Present (Conv)
1609 and then Scope (Conv) = Standard_Standard
1610 and then Is_Intrinsic_Subprogram (Conv);
1611 end Is_Unchecked_Conversion;
1613 -- Start of processing for Has_Excluded_Declaration
1615 begin
1616 D := First (Decls);
1618 while Present (D) loop
1619 if (Nkind (D) = N_Function_Instantiation
1620 and then not Is_Unchecked_Conversion (D))
1621 or else Nkind (D) = N_Protected_Type_Declaration
1622 or else Nkind (D) = N_Package_Declaration
1623 or else Nkind (D) = N_Package_Instantiation
1624 or else Nkind (D) = N_Subprogram_Body
1625 or else Nkind (D) = N_Procedure_Instantiation
1626 or else Nkind (D) = N_Task_Type_Declaration
1627 then
1628 Cannot_Inline
1629 ("cannot inline & (non-allowed declaration)?", D, Subp);
1630 return True;
1631 end if;
1633 Next (D);
1634 end loop;
1636 return False;
1637 end Has_Excluded_Declaration;
1639 ----------------------------
1640 -- Has_Excluded_Statement --
1641 ----------------------------
1643 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
1644 S : Node_Id;
1645 E : Node_Id;
1647 begin
1648 S := First (Stats);
1650 while Present (S) loop
1651 Stat_Count := Stat_Count + 1;
1653 if Nkind (S) = N_Abort_Statement
1654 or else Nkind (S) = N_Asynchronous_Select
1655 or else Nkind (S) = N_Conditional_Entry_Call
1656 or else Nkind (S) = N_Delay_Relative_Statement
1657 or else Nkind (S) = N_Delay_Until_Statement
1658 or else Nkind (S) = N_Selective_Accept
1659 or else Nkind (S) = N_Timed_Entry_Call
1660 then
1661 Cannot_Inline
1662 ("cannot inline & (non-allowed statement)?", S, Subp);
1663 return True;
1665 elsif Nkind (S) = N_Block_Statement then
1666 if Present (Declarations (S))
1667 and then Has_Excluded_Declaration (Declarations (S))
1668 then
1669 return True;
1671 elsif Present (Handled_Statement_Sequence (S))
1672 and then
1673 (Present
1674 (Exception_Handlers (Handled_Statement_Sequence (S)))
1675 or else
1676 Has_Excluded_Statement
1677 (Statements (Handled_Statement_Sequence (S))))
1678 then
1679 return True;
1680 end if;
1682 elsif Nkind (S) = N_Case_Statement then
1683 E := First (Alternatives (S));
1685 while Present (E) loop
1686 if Has_Excluded_Statement (Statements (E)) then
1687 return True;
1688 end if;
1690 Next (E);
1691 end loop;
1693 elsif Nkind (S) = N_If_Statement then
1694 if Has_Excluded_Statement (Then_Statements (S)) then
1695 return True;
1696 end if;
1698 if Present (Elsif_Parts (S)) then
1699 E := First (Elsif_Parts (S));
1701 while Present (E) loop
1702 if Has_Excluded_Statement (Then_Statements (E)) then
1703 return True;
1704 end if;
1705 Next (E);
1706 end loop;
1707 end if;
1709 if Present (Else_Statements (S))
1710 and then Has_Excluded_Statement (Else_Statements (S))
1711 then
1712 return True;
1713 end if;
1715 elsif Nkind (S) = N_Loop_Statement
1716 and then Has_Excluded_Statement (Statements (S))
1717 then
1718 return True;
1719 end if;
1721 Next (S);
1722 end loop;
1724 return False;
1725 end Has_Excluded_Statement;
1727 -------------------------------
1728 -- Has_Pending_Instantiation --
1729 -------------------------------
1731 function Has_Pending_Instantiation return Boolean is
1732 S : Entity_Id := Current_Scope;
1734 begin
1735 while Present (S) loop
1736 if Is_Compilation_Unit (S)
1737 or else Is_Child_Unit (S)
1738 then
1739 return False;
1740 elsif Ekind (S) = E_Package
1741 and then Has_Forward_Instantiation (S)
1742 then
1743 return True;
1744 end if;
1746 S := Scope (S);
1747 end loop;
1749 return False;
1750 end Has_Pending_Instantiation;
1752 --------------------
1753 -- Remove_Pragmas --
1754 --------------------
1756 procedure Remove_Pragmas is
1757 Decl : Node_Id;
1758 Nxt : Node_Id;
1760 begin
1761 Decl := First (Declarations (Body_To_Analyze));
1762 while Present (Decl) loop
1763 Nxt := Next (Decl);
1765 if Nkind (Decl) = N_Pragma
1766 and then Chars (Decl) = Name_Unreferenced
1767 then
1768 Remove (Decl);
1769 end if;
1771 Decl := Nxt;
1772 end loop;
1773 end Remove_Pragmas;
1775 --------------------------
1776 -- Uses_Secondary_Stack --
1777 --------------------------
1779 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
1780 function Check_Call (N : Node_Id) return Traverse_Result;
1781 -- Look for function calls that return an unconstrained type
1783 ----------------
1784 -- Check_Call --
1785 ----------------
1787 function Check_Call (N : Node_Id) return Traverse_Result is
1788 begin
1789 if Nkind (N) = N_Function_Call
1790 and then Is_Entity_Name (Name (N))
1791 and then Is_Composite_Type (Etype (Entity (Name (N))))
1792 and then not Is_Constrained (Etype (Entity (Name (N))))
1793 then
1794 Cannot_Inline
1795 ("cannot inline & (call returns unconstrained type)?",
1796 N, Subp);
1797 return Abandon;
1798 else
1799 return OK;
1800 end if;
1801 end Check_Call;
1803 function Check_Calls is new Traverse_Func (Check_Call);
1805 begin
1806 return Check_Calls (Bod) = Abandon;
1807 end Uses_Secondary_Stack;
1809 -- Start of processing for Build_Body_To_Inline
1811 begin
1812 if Nkind (Decl) = N_Subprogram_Declaration
1813 and then Present (Body_To_Inline (Decl))
1814 then
1815 return; -- Done already.
1817 -- Functions that return unconstrained composite types will require
1818 -- secondary stack handling, and cannot currently be inlined.
1819 -- Ditto for functions that return controlled types, where controlled
1820 -- actions interfere in complex ways with inlining.
1822 elsif Ekind (Subp) = E_Function
1823 and then not Is_Scalar_Type (Etype (Subp))
1824 and then not Is_Access_Type (Etype (Subp))
1825 and then not Is_Constrained (Etype (Subp))
1826 then
1827 Cannot_Inline
1828 ("cannot inline & (unconstrained return type)?", N, Subp);
1829 return;
1831 elsif Ekind (Subp) = E_Function
1832 and then Controlled_Type (Etype (Subp))
1833 then
1834 Cannot_Inline
1835 ("cannot inline & (controlled return type)?", N, Subp);
1836 return;
1837 end if;
1839 if Present (Declarations (N))
1840 and then Has_Excluded_Declaration (Declarations (N))
1841 then
1842 return;
1843 end if;
1845 if Present (Handled_Statement_Sequence (N)) then
1846 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
1847 Cannot_Inline
1848 ("cannot inline& (exception handler)?",
1849 First (Exception_Handlers (Handled_Statement_Sequence (N))),
1850 Subp);
1851 return;
1852 elsif
1853 Has_Excluded_Statement
1854 (Statements (Handled_Statement_Sequence (N)))
1855 then
1856 return;
1857 end if;
1858 end if;
1860 -- We do not inline a subprogram that is too large, unless it is
1861 -- marked Inline_Always. This pragma does not suppress the other
1862 -- checks on inlining (forbidden declarations, handlers, etc).
1864 if Stat_Count > Max_Size
1865 and then not Is_Always_Inlined (Subp)
1866 then
1867 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
1868 return;
1869 end if;
1871 if Has_Pending_Instantiation then
1872 Cannot_Inline
1873 ("cannot inline& (forward instance within enclosing body)?",
1874 N, Subp);
1875 return;
1876 end if;
1878 -- Within an instance, the body to inline must be treated as a nested
1879 -- generic, so that the proper global references are preserved.
1881 if In_Instance then
1882 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1883 Original_Body := Copy_Generic_Node (N, Empty, True);
1884 else
1885 Original_Body := Copy_Separate_Tree (N);
1886 end if;
1888 -- We need to capture references to the formals in order to substitute
1889 -- the actuals at the point of inlining, i.e. instantiation. To treat
1890 -- the formals as globals to the body to inline, we nest it within
1891 -- a dummy parameterless subprogram, declared within the real one.
1892 -- To avoid generating an internal name (which is never public, and
1893 -- which affects serial numbers of other generated names), we use
1894 -- an internal symbol that cannot conflict with user declarations.
1896 Set_Parameter_Specifications (Specification (Original_Body), No_List);
1897 Set_Defining_Unit_Name
1898 (Specification (Original_Body),
1899 Make_Defining_Identifier (Sloc (N), Name_uParent));
1900 Set_Corresponding_Spec (Original_Body, Empty);
1902 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
1904 -- Set return type of function, which is also global and does not need
1905 -- to be resolved.
1907 if Ekind (Subp) = E_Function then
1908 Set_Subtype_Mark (Specification (Body_To_Analyze),
1909 New_Occurrence_Of (Etype (Subp), Sloc (N)));
1910 end if;
1912 if No (Declarations (N)) then
1913 Set_Declarations (N, New_List (Body_To_Analyze));
1914 else
1915 Append (Body_To_Analyze, Declarations (N));
1916 end if;
1918 Expander_Mode_Save_And_Set (False);
1919 Remove_Pragmas;
1921 Analyze (Body_To_Analyze);
1922 New_Scope (Defining_Entity (Body_To_Analyze));
1923 Save_Global_References (Original_Body);
1924 End_Scope;
1925 Remove (Body_To_Analyze);
1927 Expander_Mode_Restore;
1929 if In_Instance then
1930 Restore_Env;
1931 end if;
1933 -- If secondary stk used there is no point in inlining. We have
1934 -- already issued the warning in this case, so nothing to do.
1936 if Uses_Secondary_Stack (Body_To_Analyze) then
1937 return;
1938 end if;
1940 Set_Body_To_Inline (Decl, Original_Body);
1941 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
1942 Set_Is_Inlined (Subp);
1943 end Build_Body_To_Inline;
1945 -------------------
1946 -- Cannot_Inline --
1947 -------------------
1949 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
1950 begin
1951 -- Do not emit warning if this is a predefined unit which is not
1952 -- the main unit. With validity checks enabled, some predefined
1953 -- subprograms may contain nested subprograms and become ineligible
1954 -- for inlining.
1956 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
1957 and then not In_Extended_Main_Source_Unit (Subp)
1958 then
1959 null;
1961 elsif Is_Always_Inlined (Subp) then
1963 -- Remove last character (question mark) to make this into an error,
1964 -- because the Inline_Always pragma cannot be obeyed.
1966 Error_Msg_NE (Msg (1 .. Msg'Length - 1), N, Subp);
1968 elsif Ineffective_Inline_Warnings then
1969 Error_Msg_NE (Msg, N, Subp);
1970 end if;
1971 end Cannot_Inline;
1973 -----------------------
1974 -- Check_Conformance --
1975 -----------------------
1977 procedure Check_Conformance
1978 (New_Id : Entity_Id;
1979 Old_Id : Entity_Id;
1980 Ctype : Conformance_Type;
1981 Errmsg : Boolean;
1982 Conforms : out Boolean;
1983 Err_Loc : Node_Id := Empty;
1984 Get_Inst : Boolean := False)
1986 Old_Type : constant Entity_Id := Etype (Old_Id);
1987 New_Type : constant Entity_Id := Etype (New_Id);
1988 Old_Formal : Entity_Id;
1989 New_Formal : Entity_Id;
1991 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
1992 -- Post error message for conformance error on given node.
1993 -- Two messages are output. The first points to the previous
1994 -- declaration with a general "no conformance" message.
1995 -- The second is the detailed reason, supplied as Msg. The
1996 -- parameter N provide information for a possible & insertion
1997 -- in the message, and also provides the location for posting
1998 -- the message in the absence of a specified Err_Loc location.
2000 -----------------------
2001 -- Conformance_Error --
2002 -----------------------
2004 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
2005 Enode : Node_Id;
2007 begin
2008 Conforms := False;
2010 if Errmsg then
2011 if No (Err_Loc) then
2012 Enode := N;
2013 else
2014 Enode := Err_Loc;
2015 end if;
2017 Error_Msg_Sloc := Sloc (Old_Id);
2019 case Ctype is
2020 when Type_Conformant =>
2021 Error_Msg_N
2022 ("not type conformant with declaration#!", Enode);
2024 when Mode_Conformant =>
2025 Error_Msg_N
2026 ("not mode conformant with declaration#!", Enode);
2028 when Subtype_Conformant =>
2029 Error_Msg_N
2030 ("not subtype conformant with declaration#!", Enode);
2032 when Fully_Conformant =>
2033 Error_Msg_N
2034 ("not fully conformant with declaration#!", Enode);
2035 end case;
2037 Error_Msg_NE (Msg, Enode, N);
2038 end if;
2039 end Conformance_Error;
2041 -- Start of processing for Check_Conformance
2043 begin
2044 Conforms := True;
2046 -- We need a special case for operators, since they don't
2047 -- appear explicitly.
2049 if Ctype = Type_Conformant then
2050 if Ekind (New_Id) = E_Operator
2051 and then Operator_Matches_Spec (New_Id, Old_Id)
2052 then
2053 return;
2054 end if;
2055 end if;
2057 -- If both are functions/operators, check return types conform
2059 if Old_Type /= Standard_Void_Type
2060 and then New_Type /= Standard_Void_Type
2061 then
2062 if not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
2063 Conformance_Error ("return type does not match!", New_Id);
2064 return;
2065 end if;
2067 -- If either is a function/operator and the other isn't, error
2069 elsif Old_Type /= Standard_Void_Type
2070 or else New_Type /= Standard_Void_Type
2071 then
2072 Conformance_Error ("functions can only match functions!", New_Id);
2073 return;
2074 end if;
2076 -- In subtype conformant case, conventions must match (RM 6.3.1(16))
2077 -- If this is a renaming as body, refine error message to indicate that
2078 -- the conflict is with the original declaration. If the entity is not
2079 -- frozen, the conventions don't have to match, the one of the renamed
2080 -- entity is inherited.
2082 if Ctype >= Subtype_Conformant then
2083 if Convention (Old_Id) /= Convention (New_Id) then
2085 if not Is_Frozen (New_Id) then
2086 null;
2088 elsif Present (Err_Loc)
2089 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
2090 and then Present (Corresponding_Spec (Err_Loc))
2091 then
2092 Error_Msg_Name_1 := Chars (New_Id);
2093 Error_Msg_Name_2 :=
2094 Name_Ada + Convention_Id'Pos (Convention (New_Id));
2096 Conformance_Error ("prior declaration for% has convention %!");
2098 else
2099 Conformance_Error ("calling conventions do not match!");
2100 end if;
2102 return;
2104 elsif Is_Formal_Subprogram (Old_Id)
2105 or else Is_Formal_Subprogram (New_Id)
2106 then
2107 Conformance_Error ("formal subprograms not allowed!");
2108 return;
2109 end if;
2110 end if;
2112 -- Deal with parameters
2114 -- Note: we use the entity information, rather than going directly
2115 -- to the specification in the tree. This is not only simpler, but
2116 -- absolutely necessary for some cases of conformance tests between
2117 -- operators, where the declaration tree simply does not exist!
2119 Old_Formal := First_Formal (Old_Id);
2120 New_Formal := First_Formal (New_Id);
2122 while Present (Old_Formal) and then Present (New_Formal) loop
2123 if Ctype = Fully_Conformant then
2125 -- Names must match. Error message is more accurate if we do
2126 -- this before checking that the types of the formals match.
2128 if Chars (Old_Formal) /= Chars (New_Formal) then
2129 Conformance_Error ("name & does not match!", New_Formal);
2131 -- Set error posted flag on new formal as well to stop
2132 -- junk cascaded messages in some cases.
2134 Set_Error_Posted (New_Formal);
2135 return;
2136 end if;
2137 end if;
2139 -- Types must always match. In the visible part of an instance,
2140 -- usual overloading rules for dispatching operations apply, and
2141 -- we check base types (not the actual subtypes).
2143 if In_Instance_Visible_Part
2144 and then Is_Dispatching_Operation (New_Id)
2145 then
2146 if not Conforming_Types
2147 (Base_Type (Etype (Old_Formal)),
2148 Base_Type (Etype (New_Formal)), Ctype, Get_Inst)
2149 then
2150 Conformance_Error ("type of & does not match!", New_Formal);
2151 return;
2152 end if;
2154 elsif not Conforming_Types
2155 (Etype (Old_Formal), Etype (New_Formal), Ctype, Get_Inst)
2156 then
2157 Conformance_Error ("type of & does not match!", New_Formal);
2158 return;
2159 end if;
2161 -- For mode conformance, mode must match
2163 if Ctype >= Mode_Conformant
2164 and then Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal)
2165 then
2166 Conformance_Error ("mode of & does not match!", New_Formal);
2167 return;
2168 end if;
2170 -- Full conformance checks
2172 if Ctype = Fully_Conformant then
2174 -- We have checked already that names match.
2175 -- Check default expressions for in parameters
2177 if Parameter_Mode (Old_Formal) = E_In_Parameter then
2178 declare
2179 NewD : constant Boolean :=
2180 Present (Default_Value (New_Formal));
2181 OldD : constant Boolean :=
2182 Present (Default_Value (Old_Formal));
2183 begin
2184 if NewD or OldD then
2186 -- The old default value has been analyzed because
2187 -- the current full declaration will have frozen
2188 -- everything before. The new default values have not
2189 -- been analyzed, so analyze them now before we check
2190 -- for conformance.
2192 if NewD then
2193 New_Scope (New_Id);
2194 Analyze_Per_Use_Expression
2195 (Default_Value (New_Formal), Etype (New_Formal));
2196 End_Scope;
2197 end if;
2199 if not (NewD and OldD)
2200 or else not Fully_Conformant_Expressions
2201 (Default_Value (Old_Formal),
2202 Default_Value (New_Formal))
2203 then
2204 Conformance_Error
2205 ("default expression for & does not match!",
2206 New_Formal);
2207 return;
2208 end if;
2209 end if;
2210 end;
2211 end if;
2212 end if;
2214 -- A couple of special checks for Ada 83 mode. These checks are
2215 -- skipped if either entity is an operator in package Standard.
2216 -- or if either old or new instance is not from the source program.
2218 if Ada_Version = Ada_83
2219 and then Sloc (Old_Id) > Standard_Location
2220 and then Sloc (New_Id) > Standard_Location
2221 and then Comes_From_Source (Old_Id)
2222 and then Comes_From_Source (New_Id)
2223 then
2224 declare
2225 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
2226 New_Param : constant Node_Id := Declaration_Node (New_Formal);
2228 begin
2229 -- Explicit IN must be present or absent in both cases. This
2230 -- test is required only in the full conformance case.
2232 if In_Present (Old_Param) /= In_Present (New_Param)
2233 and then Ctype = Fully_Conformant
2234 then
2235 Conformance_Error
2236 ("(Ada 83) IN must appear in both declarations",
2237 New_Formal);
2238 return;
2239 end if;
2241 -- Grouping (use of comma in param lists) must be the same
2242 -- This is where we catch a misconformance like:
2244 -- A,B : Integer
2245 -- A : Integer; B : Integer
2247 -- which are represented identically in the tree except
2248 -- for the setting of the flags More_Ids and Prev_Ids.
2250 if More_Ids (Old_Param) /= More_Ids (New_Param)
2251 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
2252 then
2253 Conformance_Error
2254 ("grouping of & does not match!", New_Formal);
2255 return;
2256 end if;
2257 end;
2258 end if;
2260 Next_Formal (Old_Formal);
2261 Next_Formal (New_Formal);
2262 end loop;
2264 if Present (Old_Formal) then
2265 Conformance_Error ("too few parameters!");
2266 return;
2268 elsif Present (New_Formal) then
2269 Conformance_Error ("too many parameters!", New_Formal);
2270 return;
2271 end if;
2273 end Check_Conformance;
2275 ------------------------------
2276 -- Check_Delayed_Subprogram --
2277 ------------------------------
2279 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
2280 F : Entity_Id;
2282 procedure Possible_Freeze (T : Entity_Id);
2283 -- T is the type of either a formal parameter or of the return type.
2284 -- If T is not yet frozen and needs a delayed freeze, then the
2285 -- subprogram itself must be delayed.
2287 procedure Possible_Freeze (T : Entity_Id) is
2288 begin
2289 if Has_Delayed_Freeze (T)
2290 and then not Is_Frozen (T)
2291 then
2292 Set_Has_Delayed_Freeze (Designator);
2294 elsif Is_Access_Type (T)
2295 and then Has_Delayed_Freeze (Designated_Type (T))
2296 and then not Is_Frozen (Designated_Type (T))
2297 then
2298 Set_Has_Delayed_Freeze (Designator);
2299 end if;
2300 end Possible_Freeze;
2302 -- Start of processing for Check_Delayed_Subprogram
2304 begin
2305 -- Never need to freeze abstract subprogram
2307 if Is_Abstract (Designator) then
2308 null;
2309 else
2310 -- Need delayed freeze if return type itself needs a delayed
2311 -- freeze and is not yet frozen.
2313 Possible_Freeze (Etype (Designator));
2314 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
2316 -- Need delayed freeze if any of the formal types themselves need
2317 -- a delayed freeze and are not yet frozen.
2319 F := First_Formal (Designator);
2320 while Present (F) loop
2321 Possible_Freeze (Etype (F));
2322 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
2323 Next_Formal (F);
2324 end loop;
2325 end if;
2327 -- Mark functions that return by reference. Note that it cannot be
2328 -- done for delayed_freeze subprograms because the underlying
2329 -- returned type may not be known yet (for private types)
2331 if not Has_Delayed_Freeze (Designator)
2332 and then Expander_Active
2333 then
2334 declare
2335 Typ : constant Entity_Id := Etype (Designator);
2336 Utyp : constant Entity_Id := Underlying_Type (Typ);
2338 begin
2339 if Is_Return_By_Reference_Type (Typ) then
2340 Set_Returns_By_Ref (Designator);
2342 elsif Present (Utyp) and then Controlled_Type (Utyp) then
2343 Set_Returns_By_Ref (Designator);
2344 end if;
2345 end;
2346 end if;
2347 end Check_Delayed_Subprogram;
2349 ------------------------------------
2350 -- Check_Discriminant_Conformance --
2351 ------------------------------------
2353 procedure Check_Discriminant_Conformance
2354 (N : Node_Id;
2355 Prev : Entity_Id;
2356 Prev_Loc : Node_Id)
2358 Old_Discr : Entity_Id := First_Discriminant (Prev);
2359 New_Discr : Node_Id := First (Discriminant_Specifications (N));
2360 New_Discr_Id : Entity_Id;
2361 New_Discr_Type : Entity_Id;
2363 procedure Conformance_Error (Msg : String; N : Node_Id);
2364 -- Post error message for conformance error on given node.
2365 -- Two messages are output. The first points to the previous
2366 -- declaration with a general "no conformance" message.
2367 -- The second is the detailed reason, supplied as Msg. The
2368 -- parameter N provide information for a possible & insertion
2369 -- in the message.
2371 -----------------------
2372 -- Conformance_Error --
2373 -----------------------
2375 procedure Conformance_Error (Msg : String; N : Node_Id) is
2376 begin
2377 Error_Msg_Sloc := Sloc (Prev_Loc);
2378 Error_Msg_N ("not fully conformant with declaration#!", N);
2379 Error_Msg_NE (Msg, N, N);
2380 end Conformance_Error;
2382 -- Start of processing for Check_Discriminant_Conformance
2384 begin
2385 while Present (Old_Discr) and then Present (New_Discr) loop
2387 New_Discr_Id := Defining_Identifier (New_Discr);
2389 -- The subtype mark of the discriminant on the full type
2390 -- has not been analyzed so we do it here. For an access
2391 -- discriminant a new type is created.
2393 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
2394 New_Discr_Type :=
2395 Access_Definition (N, Discriminant_Type (New_Discr));
2397 else
2398 Analyze (Discriminant_Type (New_Discr));
2399 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
2400 end if;
2402 if not Conforming_Types
2403 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
2404 then
2405 Conformance_Error ("type of & does not match!", New_Discr_Id);
2406 return;
2407 else
2408 -- Treat the new discriminant as an occurrence of the old
2409 -- one, for navigation purposes, and fill in some semantic
2410 -- information, for completeness.
2412 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
2413 Set_Etype (New_Discr_Id, Etype (Old_Discr));
2414 Set_Scope (New_Discr_Id, Scope (Old_Discr));
2415 end if;
2417 -- Names must match
2419 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
2420 Conformance_Error ("name & does not match!", New_Discr_Id);
2421 return;
2422 end if;
2424 -- Default expressions must match
2426 declare
2427 NewD : constant Boolean :=
2428 Present (Expression (New_Discr));
2429 OldD : constant Boolean :=
2430 Present (Expression (Parent (Old_Discr)));
2432 begin
2433 if NewD or OldD then
2435 -- The old default value has been analyzed and expanded,
2436 -- because the current full declaration will have frozen
2437 -- everything before. The new default values have not
2438 -- been expanded, so expand now to check conformance.
2440 if NewD then
2441 Analyze_Per_Use_Expression
2442 (Expression (New_Discr), New_Discr_Type);
2443 end if;
2445 if not (NewD and OldD)
2446 or else not Fully_Conformant_Expressions
2447 (Expression (Parent (Old_Discr)),
2448 Expression (New_Discr))
2450 then
2451 Conformance_Error
2452 ("default expression for & does not match!",
2453 New_Discr_Id);
2454 return;
2455 end if;
2456 end if;
2457 end;
2459 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
2461 if Ada_Version = Ada_83 then
2462 declare
2463 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
2465 begin
2466 -- Grouping (use of comma in param lists) must be the same
2467 -- This is where we catch a misconformance like:
2469 -- A,B : Integer
2470 -- A : Integer; B : Integer
2472 -- which are represented identically in the tree except
2473 -- for the setting of the flags More_Ids and Prev_Ids.
2475 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
2476 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
2477 then
2478 Conformance_Error
2479 ("grouping of & does not match!", New_Discr_Id);
2480 return;
2481 end if;
2482 end;
2483 end if;
2485 Next_Discriminant (Old_Discr);
2486 Next (New_Discr);
2487 end loop;
2489 if Present (Old_Discr) then
2490 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
2491 return;
2493 elsif Present (New_Discr) then
2494 Conformance_Error
2495 ("too many discriminants!", Defining_Identifier (New_Discr));
2496 return;
2497 end if;
2498 end Check_Discriminant_Conformance;
2500 ----------------------------
2501 -- Check_Fully_Conformant --
2502 ----------------------------
2504 procedure Check_Fully_Conformant
2505 (New_Id : Entity_Id;
2506 Old_Id : Entity_Id;
2507 Err_Loc : Node_Id := Empty)
2509 Result : Boolean;
2511 begin
2512 Check_Conformance
2513 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
2514 end Check_Fully_Conformant;
2516 ---------------------------
2517 -- Check_Mode_Conformant --
2518 ---------------------------
2520 procedure Check_Mode_Conformant
2521 (New_Id : Entity_Id;
2522 Old_Id : Entity_Id;
2523 Err_Loc : Node_Id := Empty;
2524 Get_Inst : Boolean := False)
2526 Result : Boolean;
2528 begin
2529 Check_Conformance
2530 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
2531 end Check_Mode_Conformant;
2533 --------------------------------
2534 -- Check_Overriding_Operation --
2535 --------------------------------
2537 procedure Check_Overriding_Operation
2538 (N : Node_Id;
2539 Subp : Entity_Id)
2541 Arg1 : Node_Id;
2542 Decl : Node_Id;
2543 Has_Pragma : Boolean := False;
2545 begin
2546 -- See whether there is an overriding pragma immediately following
2547 -- the declaration. Intervening pragmas, such as Inline, are allowed.
2549 Decl := Next (N);
2550 while Present (Decl)
2551 and then Nkind (Decl) = N_Pragma
2552 loop
2553 if Chars (Decl) = Name_Overriding
2554 or else Chars (Decl) = Name_Optional_Overriding
2555 then
2556 -- For now disable the use of these pragmas, until the ARG
2557 -- finalizes the design of this feature.
2559 Error_Msg_N ("?unrecognized pragma", Decl);
2561 if not Is_Overriding_Operation (Subp) then
2563 -- Before emitting an error message, check whether this
2564 -- may override an operation that is not yet visible, as
2565 -- in the case of a derivation of a private operation in
2566 -- a child unit. Such an operation is introduced with a
2567 -- different name, but its alias is the parent operation.
2569 declare
2570 E : Entity_Id;
2572 begin
2573 E := First_Entity (Current_Scope);
2575 while Present (E) loop
2576 if Ekind (E) = Ekind (Subp)
2577 and then not Comes_From_Source (E)
2578 and then Present (Alias (E))
2579 and then Chars (Alias (E)) = Chars (Subp)
2580 and then In_Open_Scopes (Scope (Alias (E)))
2581 then
2582 exit;
2583 else
2584 Next_Entity (E);
2585 end if;
2586 end loop;
2588 if No (E) then
2589 Error_Msg_NE
2590 ("& must override an inherited operation",
2591 Decl, Subp);
2592 end if;
2593 end;
2594 end if;
2596 -- Verify syntax of pragma
2598 Arg1 := First (Pragma_Argument_Associations (Decl));
2600 if Present (Arg1) then
2601 if not Is_Entity_Name (Expression (Arg1)) then
2602 Error_Msg_N ("pragma applies to local subprogram", Decl);
2604 elsif Chars (Expression (Arg1)) /= Chars (Subp) then
2605 Error_Msg_N
2606 ("pragma must apply to preceding subprogram", Decl);
2608 elsif Present (Next (Arg1)) then
2609 Error_Msg_N ("illegal pragma format", Decl);
2610 end if;
2611 end if;
2613 Set_Analyzed (Decl);
2614 Has_Pragma := True;
2615 exit;
2616 end if;
2618 Next (Decl);
2619 end loop;
2621 if not Has_Pragma
2622 and then Explicit_Overriding
2623 and then Is_Overriding_Operation (Subp)
2624 then
2625 Error_Msg_NE ("Missing overriding pragma for&", Subp, Subp);
2626 end if;
2627 end Check_Overriding_Operation;
2629 -------------------
2630 -- Check_Returns --
2631 -------------------
2633 procedure Check_Returns
2634 (HSS : Node_Id;
2635 Mode : Character;
2636 Err : out Boolean)
2638 Handler : Node_Id;
2640 procedure Check_Statement_Sequence (L : List_Id);
2641 -- Internal recursive procedure to check a list of statements for proper
2642 -- termination by a return statement (or a transfer of control or a
2643 -- compound statement that is itself internally properly terminated).
2645 ------------------------------
2646 -- Check_Statement_Sequence --
2647 ------------------------------
2649 procedure Check_Statement_Sequence (L : List_Id) is
2650 Last_Stm : Node_Id;
2651 Kind : Node_Kind;
2653 Raise_Exception_Call : Boolean;
2654 -- Set True if statement sequence terminated by Raise_Exception call
2655 -- or a Reraise_Occurrence call.
2657 begin
2658 Raise_Exception_Call := False;
2660 -- Get last real statement
2662 Last_Stm := Last (L);
2664 -- Don't count pragmas
2666 while Nkind (Last_Stm) = N_Pragma
2668 -- Don't count call to SS_Release (can happen after Raise_Exception)
2670 or else
2671 (Nkind (Last_Stm) = N_Procedure_Call_Statement
2672 and then
2673 Nkind (Name (Last_Stm)) = N_Identifier
2674 and then
2675 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
2677 -- Don't count exception junk
2679 or else
2680 ((Nkind (Last_Stm) = N_Goto_Statement
2681 or else Nkind (Last_Stm) = N_Label
2682 or else Nkind (Last_Stm) = N_Object_Declaration)
2683 and then Exception_Junk (Last_Stm))
2684 loop
2685 Prev (Last_Stm);
2686 end loop;
2688 -- Here we have the "real" last statement
2690 Kind := Nkind (Last_Stm);
2692 -- Transfer of control, OK. Note that in the No_Return procedure
2693 -- case, we already diagnosed any explicit return statements, so
2694 -- we can treat them as OK in this context.
2696 if Is_Transfer (Last_Stm) then
2697 return;
2699 -- Check cases of explicit non-indirect procedure calls
2701 elsif Kind = N_Procedure_Call_Statement
2702 and then Is_Entity_Name (Name (Last_Stm))
2703 then
2704 -- Check call to Raise_Exception procedure which is treated
2705 -- specially, as is a call to Reraise_Occurrence.
2707 -- We suppress the warning in these cases since it is likely that
2708 -- the programmer really does not expect to deal with the case
2709 -- of Null_Occurrence, and thus would find a warning about a
2710 -- missing return curious, and raising Program_Error does not
2711 -- seem such a bad behavior if this does occur.
2713 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
2714 or else
2715 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
2716 then
2717 Raise_Exception_Call := True;
2719 -- For Raise_Exception call, test first argument, if it is
2720 -- an attribute reference for a 'Identity call, then we know
2721 -- that the call cannot possibly return.
2723 declare
2724 Arg : constant Node_Id :=
2725 Original_Node (First_Actual (Last_Stm));
2727 begin
2728 if Nkind (Arg) = N_Attribute_Reference
2729 and then Attribute_Name (Arg) = Name_Identity
2730 then
2731 return;
2732 end if;
2733 end;
2734 end if;
2736 -- If statement, need to look inside if there is an else and check
2737 -- each constituent statement sequence for proper termination.
2739 elsif Kind = N_If_Statement
2740 and then Present (Else_Statements (Last_Stm))
2741 then
2742 Check_Statement_Sequence (Then_Statements (Last_Stm));
2743 Check_Statement_Sequence (Else_Statements (Last_Stm));
2745 if Present (Elsif_Parts (Last_Stm)) then
2746 declare
2747 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
2749 begin
2750 while Present (Elsif_Part) loop
2751 Check_Statement_Sequence (Then_Statements (Elsif_Part));
2752 Next (Elsif_Part);
2753 end loop;
2754 end;
2755 end if;
2757 return;
2759 -- Case statement, check each case for proper termination
2761 elsif Kind = N_Case_Statement then
2762 declare
2763 Case_Alt : Node_Id;
2765 begin
2766 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
2767 while Present (Case_Alt) loop
2768 Check_Statement_Sequence (Statements (Case_Alt));
2769 Next_Non_Pragma (Case_Alt);
2770 end loop;
2771 end;
2773 return;
2775 -- Block statement, check its handled sequence of statements
2777 elsif Kind = N_Block_Statement then
2778 declare
2779 Err1 : Boolean;
2781 begin
2782 Check_Returns
2783 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
2785 if Err1 then
2786 Err := True;
2787 end if;
2789 return;
2790 end;
2792 -- Loop statement. If there is an iteration scheme, we can definitely
2793 -- fall out of the loop. Similarly if there is an exit statement, we
2794 -- can fall out. In either case we need a following return.
2796 elsif Kind = N_Loop_Statement then
2797 if Present (Iteration_Scheme (Last_Stm))
2798 or else Has_Exit (Entity (Identifier (Last_Stm)))
2799 then
2800 null;
2802 -- A loop with no exit statement or iteration scheme if either
2803 -- an inifite loop, or it has some other exit (raise/return).
2804 -- In either case, no warning is required.
2806 else
2807 return;
2808 end if;
2810 -- Timed entry call, check entry call and delay alternatives
2812 -- Note: in expanded code, the timed entry call has been converted
2813 -- to a set of expanded statements on which the check will work
2814 -- correctly in any case.
2816 elsif Kind = N_Timed_Entry_Call then
2817 declare
2818 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
2819 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
2821 begin
2822 -- If statement sequence of entry call alternative is missing,
2823 -- then we can definitely fall through, and we post the error
2824 -- message on the entry call alternative itself.
2826 if No (Statements (ECA)) then
2827 Last_Stm := ECA;
2829 -- If statement sequence of delay alternative is missing, then
2830 -- we can definitely fall through, and we post the error
2831 -- message on the delay alternative itself.
2833 -- Note: if both ECA and DCA are missing the return, then we
2834 -- post only one message, should be enough to fix the bugs.
2835 -- If not we will get a message next time on the DCA when the
2836 -- ECA is fixed!
2838 elsif No (Statements (DCA)) then
2839 Last_Stm := DCA;
2841 -- Else check both statement sequences
2843 else
2844 Check_Statement_Sequence (Statements (ECA));
2845 Check_Statement_Sequence (Statements (DCA));
2846 return;
2847 end if;
2848 end;
2850 -- Conditional entry call, check entry call and else part
2852 -- Note: in expanded code, the conditional entry call has been
2853 -- converted to a set of expanded statements on which the check
2854 -- will work correctly in any case.
2856 elsif Kind = N_Conditional_Entry_Call then
2857 declare
2858 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
2860 begin
2861 -- If statement sequence of entry call alternative is missing,
2862 -- then we can definitely fall through, and we post the error
2863 -- message on the entry call alternative itself.
2865 if No (Statements (ECA)) then
2866 Last_Stm := ECA;
2868 -- Else check statement sequence and else part
2870 else
2871 Check_Statement_Sequence (Statements (ECA));
2872 Check_Statement_Sequence (Else_Statements (Last_Stm));
2873 return;
2874 end if;
2875 end;
2876 end if;
2878 -- If we fall through, issue appropriate message
2880 if Mode = 'F' then
2882 if not Raise_Exception_Call then
2883 Error_Msg_N
2884 ("?RETURN statement missing following this statement!",
2885 Last_Stm);
2886 Error_Msg_N
2887 ("\?Program_Error may be raised at run time",
2888 Last_Stm);
2889 end if;
2891 -- Note: we set Err even though we have not issued a warning
2892 -- because we still have a case of a missing return. This is
2893 -- an extremely marginal case, probably will never be noticed
2894 -- but we might as well get it right.
2896 Err := True;
2898 else
2899 Error_Msg_N
2900 ("implied return after this statement not allowed (No_Return)",
2901 Last_Stm);
2902 end if;
2903 end Check_Statement_Sequence;
2905 -- Start of processing for Check_Returns
2907 begin
2908 Err := False;
2909 Check_Statement_Sequence (Statements (HSS));
2911 if Present (Exception_Handlers (HSS)) then
2912 Handler := First_Non_Pragma (Exception_Handlers (HSS));
2913 while Present (Handler) loop
2914 Check_Statement_Sequence (Statements (Handler));
2915 Next_Non_Pragma (Handler);
2916 end loop;
2917 end if;
2918 end Check_Returns;
2920 ----------------------------
2921 -- Check_Subprogram_Order --
2922 ----------------------------
2924 procedure Check_Subprogram_Order (N : Node_Id) is
2926 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
2927 -- This is used to check if S1 > S2 in the sense required by this
2928 -- test, for example nameab < namec, but name2 < name10.
2930 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
2931 L1, L2 : Positive;
2932 N1, N2 : Natural;
2934 begin
2935 -- Remove trailing numeric parts
2937 L1 := S1'Last;
2938 while S1 (L1) in '0' .. '9' loop
2939 L1 := L1 - 1;
2940 end loop;
2942 L2 := S2'Last;
2943 while S2 (L2) in '0' .. '9' loop
2944 L2 := L2 - 1;
2945 end loop;
2947 -- If non-numeric parts non-equal, that's decisive
2949 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
2950 return False;
2952 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
2953 return True;
2955 -- If non-numeric parts equal, compare suffixed numeric parts. Note
2956 -- that a missing suffix is treated as numeric zero in this test.
2958 else
2959 N1 := 0;
2960 while L1 < S1'Last loop
2961 L1 := L1 + 1;
2962 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
2963 end loop;
2965 N2 := 0;
2966 while L2 < S2'Last loop
2967 L2 := L2 + 1;
2968 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
2969 end loop;
2971 return N1 > N2;
2972 end if;
2973 end Subprogram_Name_Greater;
2975 -- Start of processing for Check_Subprogram_Order
2977 begin
2978 -- Check body in alpha order if this is option
2980 if Style_Check
2981 and then Style_Check_Order_Subprograms
2982 and then Nkind (N) = N_Subprogram_Body
2983 and then Comes_From_Source (N)
2984 and then In_Extended_Main_Source_Unit (N)
2985 then
2986 declare
2987 LSN : String_Ptr
2988 renames Scope_Stack.Table
2989 (Scope_Stack.Last).Last_Subprogram_Name;
2991 Body_Id : constant Entity_Id :=
2992 Defining_Entity (Specification (N));
2994 begin
2995 Get_Decoded_Name_String (Chars (Body_Id));
2997 if LSN /= null then
2998 if Subprogram_Name_Greater
2999 (LSN.all, Name_Buffer (1 .. Name_Len))
3000 then
3001 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
3002 end if;
3004 Free (LSN);
3005 end if;
3007 LSN := new String'(Name_Buffer (1 .. Name_Len));
3008 end;
3009 end if;
3010 end Check_Subprogram_Order;
3012 ------------------------------
3013 -- Check_Subtype_Conformant --
3014 ------------------------------
3016 procedure Check_Subtype_Conformant
3017 (New_Id : Entity_Id;
3018 Old_Id : Entity_Id;
3019 Err_Loc : Node_Id := Empty)
3021 Result : Boolean;
3023 begin
3024 Check_Conformance
3025 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc);
3026 end Check_Subtype_Conformant;
3028 ---------------------------
3029 -- Check_Type_Conformant --
3030 ---------------------------
3032 procedure Check_Type_Conformant
3033 (New_Id : Entity_Id;
3034 Old_Id : Entity_Id;
3035 Err_Loc : Node_Id := Empty)
3037 Result : Boolean;
3039 begin
3040 Check_Conformance
3041 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
3042 end Check_Type_Conformant;
3044 ----------------------
3045 -- Conforming_Types --
3046 ----------------------
3048 function Conforming_Types
3049 (T1 : Entity_Id;
3050 T2 : Entity_Id;
3051 Ctype : Conformance_Type;
3052 Get_Inst : Boolean := False) return Boolean
3054 Type_1 : Entity_Id := T1;
3055 Type_2 : Entity_Id := T2;
3056 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
3058 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
3059 -- If neither T1 nor T2 are generic actual types, or if they are
3060 -- in different scopes (e.g. parent and child instances), then verify
3061 -- that the base types are equal. Otherwise T1 and T2 must be
3062 -- on the same subtype chain. The whole purpose of this procedure
3063 -- is to prevent spurious ambiguities in an instantiation that may
3064 -- arise if two distinct generic types are instantiated with the
3065 -- same actual.
3067 ----------------------
3068 -- Base_Types_Match --
3069 ----------------------
3071 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
3072 begin
3073 if T1 = T2 then
3074 return True;
3076 elsif Base_Type (T1) = Base_Type (T2) then
3078 -- The following is too permissive. A more precise test must
3079 -- check that the generic actual is an ancestor subtype of the
3080 -- other ???.
3082 return not Is_Generic_Actual_Type (T1)
3083 or else not Is_Generic_Actual_Type (T2)
3084 or else Scope (T1) /= Scope (T2);
3086 -- In some cases a type imported through a limited_with clause,
3087 -- and its non-limited view are both visible, for example in an
3088 -- anonymous access_to_classwide type in a formal. Both entities
3089 -- designate the same type.
3091 elsif From_With_Type (T1)
3092 and then Ekind (T1) = E_Incomplete_Type
3093 and then T2 = Non_Limited_View (T1)
3094 then
3095 return True;
3097 else
3098 return False;
3099 end if;
3100 end Base_Types_Match;
3102 begin
3103 -- The context is an instance association for a formal
3104 -- access-to-subprogram type; the formal parameter types
3105 -- require mapping because they may denote other formal
3106 -- parameters of the generic unit.
3108 if Get_Inst then
3109 Type_1 := Get_Instance_Of (T1);
3110 Type_2 := Get_Instance_Of (T2);
3111 end if;
3113 -- First see if base types match
3115 if Base_Types_Match (Type_1, Type_2) then
3116 return Ctype <= Mode_Conformant
3117 or else Subtypes_Statically_Match (Type_1, Type_2);
3119 elsif Is_Incomplete_Or_Private_Type (Type_1)
3120 and then Present (Full_View (Type_1))
3121 and then Base_Types_Match (Full_View (Type_1), Type_2)
3122 then
3123 return Ctype <= Mode_Conformant
3124 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
3126 elsif Ekind (Type_2) = E_Incomplete_Type
3127 and then Present (Full_View (Type_2))
3128 and then Base_Types_Match (Type_1, Full_View (Type_2))
3129 then
3130 return Ctype <= Mode_Conformant
3131 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3133 elsif Is_Private_Type (Type_2)
3134 and then In_Instance
3135 and then Present (Full_View (Type_2))
3136 and then Base_Types_Match (Type_1, Full_View (Type_2))
3137 then
3138 return Ctype <= Mode_Conformant
3139 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3140 end if;
3142 -- Ada 2005 (AI-254): Detect anonymous access to subprogram types
3144 Are_Anonymous_Access_To_Subprogram_Types :=
3146 -- Case 1: Anonymous access to subprogram types
3148 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
3149 and then Ekind (Type_2) = E_Anonymous_Access_Subprogram_Type)
3151 -- Case 2: Anonymous access to PROTECTED subprogram types. In this
3152 -- case the anonymous type_declaration has been replaced by an
3153 -- occurrence of an internal access to subprogram type declaration
3154 -- available through the Original_Access_Type attribute
3156 or else
3157 (Ekind (Type_1) = E_Access_Protected_Subprogram_Type
3158 and then Ekind (Type_2) = E_Access_Protected_Subprogram_Type
3159 and then not Comes_From_Source (Type_1)
3160 and then not Comes_From_Source (Type_2)
3161 and then Present (Original_Access_Type (Type_1))
3162 and then Present (Original_Access_Type (Type_2))
3163 and then Ekind (Original_Access_Type (Type_1)) =
3164 E_Anonymous_Access_Protected_Subprogram_Type
3165 and then Ekind (Original_Access_Type (Type_2)) =
3166 E_Anonymous_Access_Protected_Subprogram_Type);
3168 -- Test anonymous access type case. For this case, static subtype
3169 -- matching is required for mode conformance (RM 6.3.1(15))
3171 if (Ekind (Type_1) = E_Anonymous_Access_Type
3172 and then Ekind (Type_2) = E_Anonymous_Access_Type)
3173 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
3174 then
3175 declare
3176 Desig_1 : Entity_Id;
3177 Desig_2 : Entity_Id;
3179 begin
3180 Desig_1 := Directly_Designated_Type (Type_1);
3182 -- An access parameter can designate an incomplete type
3184 if Ekind (Desig_1) = E_Incomplete_Type
3185 and then Present (Full_View (Desig_1))
3186 then
3187 Desig_1 := Full_View (Desig_1);
3188 end if;
3190 Desig_2 := Directly_Designated_Type (Type_2);
3192 if Ekind (Desig_2) = E_Incomplete_Type
3193 and then Present (Full_View (Desig_2))
3194 then
3195 Desig_2 := Full_View (Desig_2);
3196 end if;
3198 -- The context is an instance association for a formal
3199 -- access-to-subprogram type; formal access parameter
3200 -- designated types require mapping because they may
3201 -- denote other formal parameters of the generic unit.
3203 if Get_Inst then
3204 Desig_1 := Get_Instance_Of (Desig_1);
3205 Desig_2 := Get_Instance_Of (Desig_2);
3206 end if;
3208 -- It is possible for a Class_Wide_Type to be introduced for
3209 -- an incomplete type, in which case there is a separate class_
3210 -- wide type for the full view. The types conform if their
3211 -- Etypes conform, i.e. one may be the full view of the other.
3212 -- This can only happen in the context of an access parameter,
3213 -- other uses of an incomplete Class_Wide_Type are illegal.
3215 if Is_Class_Wide_Type (Desig_1)
3216 and then Is_Class_Wide_Type (Desig_2)
3217 then
3218 return
3219 Conforming_Types
3220 (Etype (Base_Type (Desig_1)),
3221 Etype (Base_Type (Desig_2)), Ctype);
3223 elsif Are_Anonymous_Access_To_Subprogram_Types then
3224 return Ctype = Type_Conformant
3225 or else
3226 Subtypes_Statically_Match (Desig_1, Desig_2);
3228 else
3229 return Base_Type (Desig_1) = Base_Type (Desig_2)
3230 and then (Ctype = Type_Conformant
3231 or else
3232 Subtypes_Statically_Match (Desig_1, Desig_2));
3233 end if;
3234 end;
3236 -- Otherwise definitely no match
3238 else
3239 return False;
3240 end if;
3242 end Conforming_Types;
3244 --------------------------
3245 -- Create_Extra_Formals --
3246 --------------------------
3248 procedure Create_Extra_Formals (E : Entity_Id) is
3249 Formal : Entity_Id;
3250 Last_Extra : Entity_Id;
3251 Formal_Type : Entity_Id;
3252 P_Formal : Entity_Id := Empty;
3254 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id;
3255 -- Add an extra formal, associated with the current Formal. The
3256 -- extra formal is added to the list of extra formals, and also
3257 -- returned as the result. These formals are always of mode IN.
3259 ----------------------
3260 -- Add_Extra_Formal --
3261 ----------------------
3263 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id is
3264 EF : constant Entity_Id :=
3265 Make_Defining_Identifier (Sloc (Formal),
3266 Chars => New_External_Name (Chars (Formal), 'F'));
3268 begin
3269 -- We never generate extra formals if expansion is not active
3270 -- because we don't need them unless we are generating code.
3272 if not Expander_Active then
3273 return Empty;
3274 end if;
3276 -- A little optimization. Never generate an extra formal for
3277 -- the _init operand of an initialization procedure, since it
3278 -- could never be used.
3280 if Chars (Formal) = Name_uInit then
3281 return Empty;
3282 end if;
3284 Set_Ekind (EF, E_In_Parameter);
3285 Set_Actual_Subtype (EF, Typ);
3286 Set_Etype (EF, Typ);
3287 Set_Scope (EF, Scope (Formal));
3288 Set_Mechanism (EF, Default_Mechanism);
3289 Set_Formal_Validity (EF);
3291 Set_Extra_Formal (Last_Extra, EF);
3292 Last_Extra := EF;
3293 return EF;
3294 end Add_Extra_Formal;
3296 -- Start of processing for Create_Extra_Formals
3298 begin
3299 -- If this is a derived subprogram then the subtypes of the
3300 -- parent subprogram's formal parameters will be used to
3301 -- to determine the need for extra formals.
3303 if Is_Overloadable (E) and then Present (Alias (E)) then
3304 P_Formal := First_Formal (Alias (E));
3305 end if;
3307 Last_Extra := Empty;
3308 Formal := First_Formal (E);
3309 while Present (Formal) loop
3310 Last_Extra := Formal;
3311 Next_Formal (Formal);
3312 end loop;
3314 -- If Extra_formals where already created, don't do it again
3315 -- This situation may arise for subprogram types created as part
3316 -- of dispatching calls (see Expand_Dispatch_Call)
3318 if Present (Last_Extra) and then
3319 Present (Extra_Formal (Last_Extra))
3320 then
3321 return;
3322 end if;
3324 Formal := First_Formal (E);
3326 while Present (Formal) loop
3328 -- Create extra formal for supporting the attribute 'Constrained.
3329 -- The case of a private type view without discriminants also
3330 -- requires the extra formal if the underlying type has defaulted
3331 -- discriminants.
3333 if Ekind (Formal) /= E_In_Parameter then
3334 if Present (P_Formal) then
3335 Formal_Type := Etype (P_Formal);
3336 else
3337 Formal_Type := Etype (Formal);
3338 end if;
3340 -- Do not produce extra formals for Unchecked_Union parameters.
3341 -- Jump directly to the end of the loop.
3343 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
3344 goto Skip_Extra_Formal_Generation;
3345 end if;
3347 if not Has_Discriminants (Formal_Type)
3348 and then Ekind (Formal_Type) in Private_Kind
3349 and then Present (Underlying_Type (Formal_Type))
3350 then
3351 Formal_Type := Underlying_Type (Formal_Type);
3352 end if;
3354 if Has_Discriminants (Formal_Type)
3355 and then
3356 ((not Is_Constrained (Formal_Type)
3357 and then not Is_Indefinite_Subtype (Formal_Type))
3358 or else Present (Extra_Formal (Formal)))
3359 then
3360 Set_Extra_Constrained
3361 (Formal, Add_Extra_Formal (Standard_Boolean));
3362 end if;
3363 end if;
3365 -- Create extra formal for supporting accessibility checking
3367 -- This is suppressed if we specifically suppress accessibility
3368 -- checks at the pacage level for either the subprogram, or the
3369 -- package in which it resides. However, we do not suppress it
3370 -- simply if the scope has accessibility checks suppressed, since
3371 -- this could cause trouble when clients are compiled with a
3372 -- different suppression setting. The explicit checks at the
3373 -- package level are safe from this point of view.
3375 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
3376 and then not
3377 (Explicit_Suppress (E, Accessibility_Check)
3378 or else
3379 Explicit_Suppress (Scope (E), Accessibility_Check))
3380 and then
3381 (not Present (P_Formal)
3382 or else Present (Extra_Accessibility (P_Formal)))
3383 then
3384 -- Temporary kludge: for now we avoid creating the extra
3385 -- formal for access parameters of protected operations
3386 -- because of problem with the case of internal protected
3387 -- calls. ???
3389 if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
3390 and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
3391 then
3392 Set_Extra_Accessibility
3393 (Formal, Add_Extra_Formal (Standard_Natural));
3394 end if;
3395 end if;
3397 if Present (P_Formal) then
3398 Next_Formal (P_Formal);
3399 end if;
3401 -- This label is required when skipping extra formal generation for
3402 -- Unchecked_Union parameters.
3404 <<Skip_Extra_Formal_Generation>>
3406 Next_Formal (Formal);
3407 end loop;
3408 end Create_Extra_Formals;
3410 -----------------------------
3411 -- Enter_Overloaded_Entity --
3412 -----------------------------
3414 procedure Enter_Overloaded_Entity (S : Entity_Id) is
3415 E : Entity_Id := Current_Entity_In_Scope (S);
3416 C_E : Entity_Id := Current_Entity (S);
3418 begin
3419 if Present (E) then
3420 Set_Has_Homonym (E);
3421 Set_Has_Homonym (S);
3422 end if;
3424 Set_Is_Immediately_Visible (S);
3425 Set_Scope (S, Current_Scope);
3427 -- Chain new entity if front of homonym in current scope, so that
3428 -- homonyms are contiguous.
3430 if Present (E)
3431 and then E /= C_E
3432 then
3433 while Homonym (C_E) /= E loop
3434 C_E := Homonym (C_E);
3435 end loop;
3437 Set_Homonym (C_E, S);
3439 else
3440 E := C_E;
3441 Set_Current_Entity (S);
3442 end if;
3444 Set_Homonym (S, E);
3446 Append_Entity (S, Current_Scope);
3447 Set_Public_Status (S);
3449 if Debug_Flag_E then
3450 Write_Str ("New overloaded entity chain: ");
3451 Write_Name (Chars (S));
3452 E := S;
3454 while Present (E) loop
3455 Write_Str (" "); Write_Int (Int (E));
3456 E := Homonym (E);
3457 end loop;
3459 Write_Eol;
3460 end if;
3462 -- Generate warning for hiding
3464 if Warn_On_Hiding
3465 and then Comes_From_Source (S)
3466 and then In_Extended_Main_Source_Unit (S)
3467 then
3468 E := S;
3469 loop
3470 E := Homonym (E);
3471 exit when No (E);
3473 -- Warn unless genuine overloading
3475 if (not Is_Overloadable (E))
3476 or else Subtype_Conformant (E, S)
3477 then
3478 Error_Msg_Sloc := Sloc (E);
3479 Error_Msg_N ("declaration of & hides one#?", S);
3480 end if;
3481 end loop;
3482 end if;
3483 end Enter_Overloaded_Entity;
3485 -----------------------------
3486 -- Find_Corresponding_Spec --
3487 -----------------------------
3489 function Find_Corresponding_Spec (N : Node_Id) return Entity_Id is
3490 Spec : constant Node_Id := Specification (N);
3491 Designator : constant Entity_Id := Defining_Entity (Spec);
3493 E : Entity_Id;
3495 begin
3496 E := Current_Entity (Designator);
3498 while Present (E) loop
3500 -- We are looking for a matching spec. It must have the same scope,
3501 -- and the same name, and either be type conformant, or be the case
3502 -- of a library procedure spec and its body (which belong to one
3503 -- another regardless of whether they are type conformant or not).
3505 if Scope (E) = Current_Scope then
3506 if Current_Scope = Standard_Standard
3507 or else (Ekind (E) = Ekind (Designator)
3508 and then Type_Conformant (E, Designator))
3509 then
3510 -- Within an instantiation, we know that spec and body are
3511 -- subtype conformant, because they were subtype conformant
3512 -- in the generic. We choose the subtype-conformant entity
3513 -- here as well, to resolve spurious ambiguities in the
3514 -- instance that were not present in the generic (i.e. when
3515 -- two different types are given the same actual). If we are
3516 -- looking for a spec to match a body, full conformance is
3517 -- expected.
3519 if In_Instance then
3520 Set_Convention (Designator, Convention (E));
3522 if Nkind (N) = N_Subprogram_Body
3523 and then Present (Homonym (E))
3524 and then not Fully_Conformant (E, Designator)
3525 then
3526 goto Next_Entity;
3528 elsif not Subtype_Conformant (E, Designator) then
3529 goto Next_Entity;
3530 end if;
3531 end if;
3533 if not Has_Completion (E) then
3535 if Nkind (N) /= N_Subprogram_Body_Stub then
3536 Set_Corresponding_Spec (N, E);
3537 end if;
3539 Set_Has_Completion (E);
3540 return E;
3542 elsif Nkind (Parent (N)) = N_Subunit then
3544 -- If this is the proper body of a subunit, the completion
3545 -- flag is set when analyzing the stub.
3547 return E;
3549 -- If body already exists, this is an error unless the
3550 -- previous declaration is the implicit declaration of
3551 -- a derived subprogram, or this is a spurious overloading
3552 -- in an instance.
3554 elsif No (Alias (E))
3555 and then not Is_Intrinsic_Subprogram (E)
3556 and then not In_Instance
3557 then
3558 Error_Msg_Sloc := Sloc (E);
3559 if Is_Imported (E) then
3560 Error_Msg_NE
3561 ("body not allowed for imported subprogram & declared#",
3562 N, E);
3563 else
3564 Error_Msg_NE ("duplicate body for & declared#", N, E);
3565 end if;
3566 end if;
3568 elsif Is_Child_Unit (E)
3569 and then
3570 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
3571 and then
3572 Nkind (Parent (Unit_Declaration_Node (Designator)))
3573 = N_Compilation_Unit
3574 then
3576 -- Child units cannot be overloaded, so a conformance mismatch
3577 -- between body and a previous spec is an error.
3579 Error_Msg_N
3580 ("body of child unit does not match previous declaration", N);
3581 end if;
3582 end if;
3584 <<Next_Entity>>
3585 E := Homonym (E);
3586 end loop;
3588 -- On exit, we know that no previous declaration of subprogram exists
3590 return Empty;
3591 end Find_Corresponding_Spec;
3593 ----------------------
3594 -- Fully_Conformant --
3595 ----------------------
3597 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
3598 Result : Boolean;
3600 begin
3601 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
3602 return Result;
3603 end Fully_Conformant;
3605 ----------------------------------
3606 -- Fully_Conformant_Expressions --
3607 ----------------------------------
3609 function Fully_Conformant_Expressions
3610 (Given_E1 : Node_Id;
3611 Given_E2 : Node_Id) return Boolean
3613 E1 : constant Node_Id := Original_Node (Given_E1);
3614 E2 : constant Node_Id := Original_Node (Given_E2);
3615 -- We always test conformance on original nodes, since it is possible
3616 -- for analysis and/or expansion to make things look as though they
3617 -- conform when they do not, e.g. by converting 1+2 into 3.
3619 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
3620 renames Fully_Conformant_Expressions;
3622 function FCL (L1, L2 : List_Id) return Boolean;
3623 -- Compare elements of two lists for conformance. Elements have to
3624 -- be conformant, and actuals inserted as default parameters do not
3625 -- match explicit actuals with the same value.
3627 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
3628 -- Compare an operator node with a function call
3630 ---------
3631 -- FCL --
3632 ---------
3634 function FCL (L1, L2 : List_Id) return Boolean is
3635 N1, N2 : Node_Id;
3637 begin
3638 if L1 = No_List then
3639 N1 := Empty;
3640 else
3641 N1 := First (L1);
3642 end if;
3644 if L2 = No_List then
3645 N2 := Empty;
3646 else
3647 N2 := First (L2);
3648 end if;
3650 -- Compare two lists, skipping rewrite insertions (we want to
3651 -- compare the original trees, not the expanded versions!)
3653 loop
3654 if Is_Rewrite_Insertion (N1) then
3655 Next (N1);
3656 elsif Is_Rewrite_Insertion (N2) then
3657 Next (N2);
3658 elsif No (N1) then
3659 return No (N2);
3660 elsif No (N2) then
3661 return False;
3662 elsif not FCE (N1, N2) then
3663 return False;
3664 else
3665 Next (N1);
3666 Next (N2);
3667 end if;
3668 end loop;
3669 end FCL;
3671 ---------
3672 -- FCO --
3673 ---------
3675 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
3676 Actuals : constant List_Id := Parameter_Associations (Call_Node);
3677 Act : Node_Id;
3679 begin
3680 if No (Actuals)
3681 or else Entity (Op_Node) /= Entity (Name (Call_Node))
3682 then
3683 return False;
3685 else
3686 Act := First (Actuals);
3688 if Nkind (Op_Node) in N_Binary_Op then
3690 if not FCE (Left_Opnd (Op_Node), Act) then
3691 return False;
3692 end if;
3694 Next (Act);
3695 end if;
3697 return Present (Act)
3698 and then FCE (Right_Opnd (Op_Node), Act)
3699 and then No (Next (Act));
3700 end if;
3701 end FCO;
3703 -- Start of processing for Fully_Conformant_Expressions
3705 begin
3706 -- Non-conformant if paren count does not match. Note: if some idiot
3707 -- complains that we don't do this right for more than 3 levels of
3708 -- parentheses, they will be treated with the respect they deserve :-)
3710 if Paren_Count (E1) /= Paren_Count (E2) then
3711 return False;
3713 -- If same entities are referenced, then they are conformant
3714 -- even if they have different forms (RM 8.3.1(19-20)).
3716 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
3717 if Present (Entity (E1)) then
3718 return Entity (E1) = Entity (E2)
3719 or else (Chars (Entity (E1)) = Chars (Entity (E2))
3720 and then Ekind (Entity (E1)) = E_Discriminant
3721 and then Ekind (Entity (E2)) = E_In_Parameter);
3723 elsif Nkind (E1) = N_Expanded_Name
3724 and then Nkind (E2) = N_Expanded_Name
3725 and then Nkind (Selector_Name (E1)) = N_Character_Literal
3726 and then Nkind (Selector_Name (E2)) = N_Character_Literal
3727 then
3728 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
3730 else
3731 -- Identifiers in component associations don't always have
3732 -- entities, but their names must conform.
3734 return Nkind (E1) = N_Identifier
3735 and then Nkind (E2) = N_Identifier
3736 and then Chars (E1) = Chars (E2);
3737 end if;
3739 elsif Nkind (E1) = N_Character_Literal
3740 and then Nkind (E2) = N_Expanded_Name
3741 then
3742 return Nkind (Selector_Name (E2)) = N_Character_Literal
3743 and then Chars (E1) = Chars (Selector_Name (E2));
3745 elsif Nkind (E2) = N_Character_Literal
3746 and then Nkind (E1) = N_Expanded_Name
3747 then
3748 return Nkind (Selector_Name (E1)) = N_Character_Literal
3749 and then Chars (E2) = Chars (Selector_Name (E1));
3751 elsif Nkind (E1) in N_Op
3752 and then Nkind (E2) = N_Function_Call
3753 then
3754 return FCO (E1, E2);
3756 elsif Nkind (E2) in N_Op
3757 and then Nkind (E1) = N_Function_Call
3758 then
3759 return FCO (E2, E1);
3761 -- Otherwise we must have the same syntactic entity
3763 elsif Nkind (E1) /= Nkind (E2) then
3764 return False;
3766 -- At this point, we specialize by node type
3768 else
3769 case Nkind (E1) is
3771 when N_Aggregate =>
3772 return
3773 FCL (Expressions (E1), Expressions (E2))
3774 and then FCL (Component_Associations (E1),
3775 Component_Associations (E2));
3777 when N_Allocator =>
3778 if Nkind (Expression (E1)) = N_Qualified_Expression
3779 or else
3780 Nkind (Expression (E2)) = N_Qualified_Expression
3781 then
3782 return FCE (Expression (E1), Expression (E2));
3784 -- Check that the subtype marks and any constraints
3785 -- are conformant
3787 else
3788 declare
3789 Indic1 : constant Node_Id := Expression (E1);
3790 Indic2 : constant Node_Id := Expression (E2);
3791 Elt1 : Node_Id;
3792 Elt2 : Node_Id;
3794 begin
3795 if Nkind (Indic1) /= N_Subtype_Indication then
3796 return
3797 Nkind (Indic2) /= N_Subtype_Indication
3798 and then Entity (Indic1) = Entity (Indic2);
3800 elsif Nkind (Indic2) /= N_Subtype_Indication then
3801 return
3802 Nkind (Indic1) /= N_Subtype_Indication
3803 and then Entity (Indic1) = Entity (Indic2);
3805 else
3806 if Entity (Subtype_Mark (Indic1)) /=
3807 Entity (Subtype_Mark (Indic2))
3808 then
3809 return False;
3810 end if;
3812 Elt1 := First (Constraints (Constraint (Indic1)));
3813 Elt2 := First (Constraints (Constraint (Indic2)));
3815 while Present (Elt1) and then Present (Elt2) loop
3816 if not FCE (Elt1, Elt2) then
3817 return False;
3818 end if;
3820 Next (Elt1);
3821 Next (Elt2);
3822 end loop;
3824 return True;
3825 end if;
3826 end;
3827 end if;
3829 when N_Attribute_Reference =>
3830 return
3831 Attribute_Name (E1) = Attribute_Name (E2)
3832 and then FCL (Expressions (E1), Expressions (E2));
3834 when N_Binary_Op =>
3835 return
3836 Entity (E1) = Entity (E2)
3837 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
3838 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
3840 when N_And_Then | N_Or_Else | N_In | N_Not_In =>
3841 return
3842 FCE (Left_Opnd (E1), Left_Opnd (E2))
3843 and then
3844 FCE (Right_Opnd (E1), Right_Opnd (E2));
3846 when N_Character_Literal =>
3847 return
3848 Char_Literal_Value (E1) = Char_Literal_Value (E2);
3850 when N_Component_Association =>
3851 return
3852 FCL (Choices (E1), Choices (E2))
3853 and then FCE (Expression (E1), Expression (E2));
3855 when N_Conditional_Expression =>
3856 return
3857 FCL (Expressions (E1), Expressions (E2));
3859 when N_Explicit_Dereference =>
3860 return
3861 FCE (Prefix (E1), Prefix (E2));
3863 when N_Extension_Aggregate =>
3864 return
3865 FCL (Expressions (E1), Expressions (E2))
3866 and then Null_Record_Present (E1) =
3867 Null_Record_Present (E2)
3868 and then FCL (Component_Associations (E1),
3869 Component_Associations (E2));
3871 when N_Function_Call =>
3872 return
3873 FCE (Name (E1), Name (E2))
3874 and then FCL (Parameter_Associations (E1),
3875 Parameter_Associations (E2));
3877 when N_Indexed_Component =>
3878 return
3879 FCE (Prefix (E1), Prefix (E2))
3880 and then FCL (Expressions (E1), Expressions (E2));
3882 when N_Integer_Literal =>
3883 return (Intval (E1) = Intval (E2));
3885 when N_Null =>
3886 return True;
3888 when N_Operator_Symbol =>
3889 return
3890 Chars (E1) = Chars (E2);
3892 when N_Others_Choice =>
3893 return True;
3895 when N_Parameter_Association =>
3896 return
3897 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
3898 and then FCE (Explicit_Actual_Parameter (E1),
3899 Explicit_Actual_Parameter (E2));
3901 when N_Qualified_Expression =>
3902 return
3903 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3904 and then FCE (Expression (E1), Expression (E2));
3906 when N_Range =>
3907 return
3908 FCE (Low_Bound (E1), Low_Bound (E2))
3909 and then FCE (High_Bound (E1), High_Bound (E2));
3911 when N_Real_Literal =>
3912 return (Realval (E1) = Realval (E2));
3914 when N_Selected_Component =>
3915 return
3916 FCE (Prefix (E1), Prefix (E2))
3917 and then FCE (Selector_Name (E1), Selector_Name (E2));
3919 when N_Slice =>
3920 return
3921 FCE (Prefix (E1), Prefix (E2))
3922 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
3924 when N_String_Literal =>
3925 declare
3926 S1 : constant String_Id := Strval (E1);
3927 S2 : constant String_Id := Strval (E2);
3928 L1 : constant Nat := String_Length (S1);
3929 L2 : constant Nat := String_Length (S2);
3931 begin
3932 if L1 /= L2 then
3933 return False;
3935 else
3936 for J in 1 .. L1 loop
3937 if Get_String_Char (S1, J) /=
3938 Get_String_Char (S2, J)
3939 then
3940 return False;
3941 end if;
3942 end loop;
3944 return True;
3945 end if;
3946 end;
3948 when N_Type_Conversion =>
3949 return
3950 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3951 and then FCE (Expression (E1), Expression (E2));
3953 when N_Unary_Op =>
3954 return
3955 Entity (E1) = Entity (E2)
3956 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
3958 when N_Unchecked_Type_Conversion =>
3959 return
3960 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3961 and then FCE (Expression (E1), Expression (E2));
3963 -- All other node types cannot appear in this context. Strictly
3964 -- we should raise a fatal internal error. Instead we just ignore
3965 -- the nodes. This means that if anyone makes a mistake in the
3966 -- expander and mucks an expression tree irretrievably, the
3967 -- result will be a failure to detect a (probably very obscure)
3968 -- case of non-conformance, which is better than bombing on some
3969 -- case where two expressions do in fact conform.
3971 when others =>
3972 return True;
3974 end case;
3975 end if;
3976 end Fully_Conformant_Expressions;
3978 ----------------------------------------
3979 -- Fully_Conformant_Discrete_Subtypes --
3980 ----------------------------------------
3982 function Fully_Conformant_Discrete_Subtypes
3983 (Given_S1 : Node_Id;
3984 Given_S2 : Node_Id) return Boolean
3986 S1 : constant Node_Id := Original_Node (Given_S1);
3987 S2 : constant Node_Id := Original_Node (Given_S2);
3989 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
3990 -- Special-case for a bound given by a discriminant, which in the
3991 -- body is replaced with the discriminal of the enclosing type.
3993 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
3994 -- Check both bounds
3996 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
3997 begin
3998 if Is_Entity_Name (B1)
3999 and then Is_Entity_Name (B2)
4000 and then Ekind (Entity (B1)) = E_Discriminant
4001 then
4002 return Chars (B1) = Chars (B2);
4004 else
4005 return Fully_Conformant_Expressions (B1, B2);
4006 end if;
4007 end Conforming_Bounds;
4009 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
4010 begin
4011 return
4012 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
4013 and then
4014 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
4015 end Conforming_Ranges;
4017 -- Start of processing for Fully_Conformant_Discrete_Subtypes
4019 begin
4020 if Nkind (S1) /= Nkind (S2) then
4021 return False;
4023 elsif Is_Entity_Name (S1) then
4024 return Entity (S1) = Entity (S2);
4026 elsif Nkind (S1) = N_Range then
4027 return Conforming_Ranges (S1, S2);
4029 elsif Nkind (S1) = N_Subtype_Indication then
4030 return
4031 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
4032 and then
4033 Conforming_Ranges
4034 (Range_Expression (Constraint (S1)),
4035 Range_Expression (Constraint (S2)));
4036 else
4037 return True;
4038 end if;
4039 end Fully_Conformant_Discrete_Subtypes;
4041 --------------------
4042 -- Install_Entity --
4043 --------------------
4045 procedure Install_Entity (E : Entity_Id) is
4046 Prev : constant Entity_Id := Current_Entity (E);
4048 begin
4049 Set_Is_Immediately_Visible (E);
4050 Set_Current_Entity (E);
4051 Set_Homonym (E, Prev);
4052 end Install_Entity;
4054 ---------------------
4055 -- Install_Formals --
4056 ---------------------
4058 procedure Install_Formals (Id : Entity_Id) is
4059 F : Entity_Id;
4061 begin
4062 F := First_Formal (Id);
4064 while Present (F) loop
4065 Install_Entity (F);
4066 Next_Formal (F);
4067 end loop;
4068 end Install_Formals;
4070 ---------------------------------
4071 -- Is_Non_Overriding_Operation --
4072 ---------------------------------
4074 function Is_Non_Overriding_Operation
4075 (Prev_E : Entity_Id;
4076 New_E : Entity_Id) return Boolean
4078 Formal : Entity_Id;
4079 F_Typ : Entity_Id;
4080 G_Typ : Entity_Id := Empty;
4082 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
4083 -- If F_Type is a derived type associated with a generic actual
4084 -- subtype, then return its Generic_Parent_Type attribute, else
4085 -- return Empty.
4087 function Types_Correspond
4088 (P_Type : Entity_Id;
4089 N_Type : Entity_Id) return Boolean;
4090 -- Returns true if and only if the types (or designated types
4091 -- in the case of anonymous access types) are the same or N_Type
4092 -- is derived directly or indirectly from P_Type.
4094 -----------------------------
4095 -- Get_Generic_Parent_Type --
4096 -----------------------------
4098 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
4099 G_Typ : Entity_Id;
4100 Indic : Node_Id;
4102 begin
4103 if Is_Derived_Type (F_Typ)
4104 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
4105 then
4106 -- The tree must be traversed to determine the parent
4107 -- subtype in the generic unit, which unfortunately isn't
4108 -- always available via semantic attributes. ???
4109 -- (Note: The use of Original_Node is needed for cases
4110 -- where a full derived type has been rewritten.)
4112 Indic := Subtype_Indication
4113 (Type_Definition (Original_Node (Parent (F_Typ))));
4115 if Nkind (Indic) = N_Subtype_Indication then
4116 G_Typ := Entity (Subtype_Mark (Indic));
4117 else
4118 G_Typ := Entity (Indic);
4119 end if;
4121 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
4122 and then Present (Generic_Parent_Type (Parent (G_Typ)))
4123 then
4124 return Generic_Parent_Type (Parent (G_Typ));
4125 end if;
4126 end if;
4128 return Empty;
4129 end Get_Generic_Parent_Type;
4131 ----------------------
4132 -- Types_Correspond --
4133 ----------------------
4135 function Types_Correspond
4136 (P_Type : Entity_Id;
4137 N_Type : Entity_Id) return Boolean
4139 Prev_Type : Entity_Id := Base_Type (P_Type);
4140 New_Type : Entity_Id := Base_Type (N_Type);
4142 begin
4143 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
4144 Prev_Type := Designated_Type (Prev_Type);
4145 end if;
4147 if Ekind (New_Type) = E_Anonymous_Access_Type then
4148 New_Type := Designated_Type (New_Type);
4149 end if;
4151 if Prev_Type = New_Type then
4152 return True;
4154 elsif not Is_Class_Wide_Type (New_Type) then
4155 while Etype (New_Type) /= New_Type loop
4156 New_Type := Etype (New_Type);
4157 if New_Type = Prev_Type then
4158 return True;
4159 end if;
4160 end loop;
4161 end if;
4162 return False;
4163 end Types_Correspond;
4165 -- Start of processing for Is_Non_Overriding_Operation
4167 begin
4168 -- In the case where both operations are implicit derived
4169 -- subprograms then neither overrides the other. This can
4170 -- only occur in certain obscure cases (e.g., derivation
4171 -- from homographs created in a generic instantiation).
4173 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
4174 return True;
4176 elsif Ekind (Current_Scope) = E_Package
4177 and then Is_Generic_Instance (Current_Scope)
4178 and then In_Private_Part (Current_Scope)
4179 and then Comes_From_Source (New_E)
4180 then
4181 -- We examine the formals and result subtype of the inherited
4182 -- operation, to determine whether their type is derived from
4183 -- (the instance of) a generic type.
4185 Formal := First_Formal (Prev_E);
4187 while Present (Formal) loop
4188 F_Typ := Base_Type (Etype (Formal));
4190 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4191 F_Typ := Designated_Type (F_Typ);
4192 end if;
4194 G_Typ := Get_Generic_Parent_Type (F_Typ);
4196 Next_Formal (Formal);
4197 end loop;
4199 if not Present (G_Typ) and then Ekind (Prev_E) = E_Function then
4200 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
4201 end if;
4203 if No (G_Typ) then
4204 return False;
4205 end if;
4207 -- If the generic type is a private type, then the original
4208 -- operation was not overriding in the generic, because there was
4209 -- no primitive operation to override.
4211 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
4212 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
4213 N_Formal_Private_Type_Definition
4214 then
4215 return True;
4217 -- The generic parent type is the ancestor of a formal derived
4218 -- type declaration. We need to check whether it has a primitive
4219 -- operation that should be overridden by New_E in the generic.
4221 else
4222 declare
4223 P_Formal : Entity_Id;
4224 N_Formal : Entity_Id;
4225 P_Typ : Entity_Id;
4226 N_Typ : Entity_Id;
4227 P_Prim : Entity_Id;
4228 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
4230 begin
4231 while Present (Prim_Elt) loop
4232 P_Prim := Node (Prim_Elt);
4234 if Chars (P_Prim) = Chars (New_E)
4235 and then Ekind (P_Prim) = Ekind (New_E)
4236 then
4237 P_Formal := First_Formal (P_Prim);
4238 N_Formal := First_Formal (New_E);
4239 while Present (P_Formal) and then Present (N_Formal) loop
4240 P_Typ := Etype (P_Formal);
4241 N_Typ := Etype (N_Formal);
4243 if not Types_Correspond (P_Typ, N_Typ) then
4244 exit;
4245 end if;
4247 Next_Entity (P_Formal);
4248 Next_Entity (N_Formal);
4249 end loop;
4251 -- Found a matching primitive operation belonging to
4252 -- the formal ancestor type, so the new subprogram
4253 -- is overriding.
4255 if not Present (P_Formal)
4256 and then not Present (N_Formal)
4257 and then (Ekind (New_E) /= E_Function
4258 or else
4259 Types_Correspond
4260 (Etype (P_Prim), Etype (New_E)))
4261 then
4262 return False;
4263 end if;
4264 end if;
4266 Next_Elmt (Prim_Elt);
4267 end loop;
4269 -- If no match found, then the new subprogram does
4270 -- not override in the generic (nor in the instance).
4272 return True;
4273 end;
4274 end if;
4275 else
4276 return False;
4277 end if;
4278 end Is_Non_Overriding_Operation;
4280 ------------------------------
4281 -- Make_Inequality_Operator --
4282 ------------------------------
4284 -- S is the defining identifier of an equality operator. We build a
4285 -- subprogram declaration with the right signature. This operation is
4286 -- intrinsic, because it is always expanded as the negation of the
4287 -- call to the equality function.
4289 procedure Make_Inequality_Operator (S : Entity_Id) is
4290 Loc : constant Source_Ptr := Sloc (S);
4291 Decl : Node_Id;
4292 Formals : List_Id;
4293 Op_Name : Entity_Id;
4295 A : Entity_Id;
4296 B : Entity_Id;
4298 begin
4299 -- Check that equality was properly defined
4301 if No (Next_Formal (First_Formal (S))) then
4302 return;
4303 end if;
4305 A := Make_Defining_Identifier (Loc, Chars (First_Formal (S)));
4306 B := Make_Defining_Identifier (Loc,
4307 Chars (Next_Formal (First_Formal (S))));
4309 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
4311 Formals := New_List (
4312 Make_Parameter_Specification (Loc,
4313 Defining_Identifier => A,
4314 Parameter_Type =>
4315 New_Reference_To (Etype (First_Formal (S)), Loc)),
4317 Make_Parameter_Specification (Loc,
4318 Defining_Identifier => B,
4319 Parameter_Type =>
4320 New_Reference_To (Etype (Next_Formal (First_Formal (S))), Loc)));
4322 Decl :=
4323 Make_Subprogram_Declaration (Loc,
4324 Specification =>
4325 Make_Function_Specification (Loc,
4326 Defining_Unit_Name => Op_Name,
4327 Parameter_Specifications => Formals,
4328 Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)));
4330 -- Insert inequality right after equality if it is explicit or after
4331 -- the derived type when implicit. These entities are created only
4332 -- for visibility purposes, and eventually replaced in the course of
4333 -- expansion, so they do not need to be attached to the tree and seen
4334 -- by the back-end. Keeping them internal also avoids spurious freezing
4335 -- problems. The parent field is set simply to make analysis safe.
4337 if No (Alias (S)) then
4338 Set_Parent (Decl, Parent (Unit_Declaration_Node (S)));
4339 else
4340 Set_Parent (Decl, Parent (Parent (Etype (First_Formal (S)))));
4341 end if;
4343 Mark_Rewrite_Insertion (Decl);
4344 Set_Is_Intrinsic_Subprogram (Op_Name);
4345 Analyze (Decl);
4346 Set_Has_Completion (Op_Name);
4347 Set_Corresponding_Equality (Op_Name, S);
4348 Set_Is_Abstract (Op_Name, Is_Abstract (S));
4350 end Make_Inequality_Operator;
4352 ----------------------
4353 -- May_Need_Actuals --
4354 ----------------------
4356 procedure May_Need_Actuals (Fun : Entity_Id) is
4357 F : Entity_Id;
4358 B : Boolean;
4360 begin
4361 F := First_Formal (Fun);
4362 B := True;
4364 while Present (F) loop
4365 if No (Default_Value (F)) then
4366 B := False;
4367 exit;
4368 end if;
4370 Next_Formal (F);
4371 end loop;
4373 Set_Needs_No_Actuals (Fun, B);
4374 end May_Need_Actuals;
4376 ---------------------
4377 -- Mode_Conformant --
4378 ---------------------
4380 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
4381 Result : Boolean;
4383 begin
4384 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
4385 return Result;
4386 end Mode_Conformant;
4388 ---------------------------
4389 -- New_Overloaded_Entity --
4390 ---------------------------
4392 procedure New_Overloaded_Entity
4393 (S : Entity_Id;
4394 Derived_Type : Entity_Id := Empty)
4396 E : Entity_Id;
4397 -- Entity that S overrides
4399 Prev_Vis : Entity_Id := Empty;
4400 -- Needs comment ???
4402 function Is_Private_Declaration (E : Entity_Id) return Boolean;
4403 -- Check that E is declared in the private part of the current package,
4404 -- or in the package body, where it may hide a previous declaration.
4405 -- We can't use In_Private_Part by itself because this flag is also
4406 -- set when freezing entities, so we must examine the place of the
4407 -- declaration in the tree, and recognize wrapper packages as well.
4409 procedure Maybe_Primitive_Operation (Overriding : Boolean := False);
4410 -- If the subprogram being analyzed is a primitive operation of
4411 -- the type of one of its formals, set the corresponding flag.
4413 ----------------------------
4414 -- Is_Private_Declaration --
4415 ----------------------------
4417 function Is_Private_Declaration (E : Entity_Id) return Boolean is
4418 Priv_Decls : List_Id;
4419 Decl : constant Node_Id := Unit_Declaration_Node (E);
4421 begin
4422 if Is_Package (Current_Scope)
4423 and then In_Private_Part (Current_Scope)
4424 then
4425 Priv_Decls :=
4426 Private_Declarations (
4427 Specification (Unit_Declaration_Node (Current_Scope)));
4429 return In_Package_Body (Current_Scope)
4430 or else List_Containing (Decl) = Priv_Decls
4431 or else (Nkind (Parent (Decl)) = N_Package_Specification
4432 and then not Is_Compilation_Unit (
4433 Defining_Entity (Parent (Decl)))
4434 and then List_Containing (Parent (Parent (Decl)))
4435 = Priv_Decls);
4436 else
4437 return False;
4438 end if;
4439 end Is_Private_Declaration;
4441 -------------------------------
4442 -- Maybe_Primitive_Operation --
4443 -------------------------------
4445 procedure Maybe_Primitive_Operation (Overriding : Boolean := False) is
4446 Formal : Entity_Id;
4447 F_Typ : Entity_Id;
4448 B_Typ : Entity_Id;
4450 function Visible_Part_Type (T : Entity_Id) return Boolean;
4451 -- Returns true if T is declared in the visible part of
4452 -- the current package scope; otherwise returns false.
4453 -- Assumes that T is declared in a package.
4455 procedure Check_Private_Overriding (T : Entity_Id);
4456 -- Checks that if a primitive abstract subprogram of a visible
4457 -- abstract type is declared in a private part, then it must
4458 -- override an abstract subprogram declared in the visible part.
4459 -- Also checks that if a primitive function with a controlling
4460 -- result is declared in a private part, then it must override
4461 -- a function declared in the visible part.
4463 ------------------------------
4464 -- Check_Private_Overriding --
4465 ------------------------------
4467 procedure Check_Private_Overriding (T : Entity_Id) is
4468 begin
4469 if Ekind (Current_Scope) = E_Package
4470 and then In_Private_Part (Current_Scope)
4471 and then Visible_Part_Type (T)
4472 and then not In_Instance
4473 then
4474 if Is_Abstract (T)
4475 and then Is_Abstract (S)
4476 and then (not Overriding or else not Is_Abstract (E))
4477 then
4478 Error_Msg_N ("abstract subprograms must be visible "
4479 & "('R'M 3.9.3(10))!", S);
4481 elsif Ekind (S) = E_Function
4482 and then Is_Tagged_Type (T)
4483 and then T = Base_Type (Etype (S))
4484 and then not Overriding
4485 then
4486 Error_Msg_N
4487 ("private function with tagged result must"
4488 & " override visible-part function", S);
4489 Error_Msg_N
4490 ("\move subprogram to the visible part"
4491 & " ('R'M 3.9.3(10))", S);
4492 end if;
4493 end if;
4494 end Check_Private_Overriding;
4496 -----------------------
4497 -- Visible_Part_Type --
4498 -----------------------
4500 function Visible_Part_Type (T : Entity_Id) return Boolean is
4501 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
4502 N : Node_Id;
4504 begin
4505 -- If the entity is a private type, then it must be
4506 -- declared in a visible part.
4508 if Ekind (T) in Private_Kind then
4509 return True;
4510 end if;
4512 -- Otherwise, we traverse the visible part looking for its
4513 -- corresponding declaration. We cannot use the declaration
4514 -- node directly because in the private part the entity of a
4515 -- private type is the one in the full view, which does not
4516 -- indicate that it is the completion of something visible.
4518 N := First (Visible_Declarations (Specification (P)));
4519 while Present (N) loop
4520 if Nkind (N) = N_Full_Type_Declaration
4521 and then Present (Defining_Identifier (N))
4522 and then T = Defining_Identifier (N)
4523 then
4524 return True;
4526 elsif (Nkind (N) = N_Private_Type_Declaration
4527 or else
4528 Nkind (N) = N_Private_Extension_Declaration)
4529 and then Present (Defining_Identifier (N))
4530 and then T = Full_View (Defining_Identifier (N))
4531 then
4532 return True;
4533 end if;
4535 Next (N);
4536 end loop;
4538 return False;
4539 end Visible_Part_Type;
4541 -- Start of processing for Maybe_Primitive_Operation
4543 begin
4544 if not Comes_From_Source (S) then
4545 null;
4547 -- If the subprogram is at library level, it is not a
4548 -- primitive operation.
4550 elsif Current_Scope = Standard_Standard then
4551 null;
4553 elsif (Ekind (Current_Scope) = E_Package
4554 and then not In_Package_Body (Current_Scope))
4555 or else Overriding
4556 then
4557 -- For function, check return type
4559 if Ekind (S) = E_Function then
4560 B_Typ := Base_Type (Etype (S));
4562 if Scope (B_Typ) = Current_Scope then
4563 Set_Has_Primitive_Operations (B_Typ);
4564 Check_Private_Overriding (B_Typ);
4565 end if;
4566 end if;
4568 -- For all subprograms, check formals
4570 Formal := First_Formal (S);
4571 while Present (Formal) loop
4572 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
4573 F_Typ := Designated_Type (Etype (Formal));
4574 else
4575 F_Typ := Etype (Formal);
4576 end if;
4578 B_Typ := Base_Type (F_Typ);
4580 if Scope (B_Typ) = Current_Scope then
4581 Set_Has_Primitive_Operations (B_Typ);
4582 Check_Private_Overriding (B_Typ);
4583 end if;
4585 Next_Formal (Formal);
4586 end loop;
4587 end if;
4588 end Maybe_Primitive_Operation;
4590 -- Start of processing for New_Overloaded_Entity
4592 begin
4593 -- We need to look for an entity that S may override. This must be a
4594 -- homonym in the current scope, so we look for the first homonym of
4595 -- S in the current scope as the starting point for the search.
4597 E := Current_Entity_In_Scope (S);
4599 -- If there is no homonym then this is definitely not overriding
4601 if No (E) then
4602 Enter_Overloaded_Entity (S);
4603 Check_Dispatching_Operation (S, Empty);
4604 Maybe_Primitive_Operation;
4606 -- If there is a homonym that is not overloadable, then we have an
4607 -- error, except for the special cases checked explicitly below.
4609 elsif not Is_Overloadable (E) then
4611 -- Check for spurious conflict produced by a subprogram that has the
4612 -- same name as that of the enclosing generic package. The conflict
4613 -- occurs within an instance, between the subprogram and the renaming
4614 -- declaration for the package. After the subprogram, the package
4615 -- renaming declaration becomes hidden.
4617 if Ekind (E) = E_Package
4618 and then Present (Renamed_Object (E))
4619 and then Renamed_Object (E) = Current_Scope
4620 and then Nkind (Parent (Renamed_Object (E))) =
4621 N_Package_Specification
4622 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
4623 then
4624 Set_Is_Hidden (E);
4625 Set_Is_Immediately_Visible (E, False);
4626 Enter_Overloaded_Entity (S);
4627 Set_Homonym (S, Homonym (E));
4628 Check_Dispatching_Operation (S, Empty);
4630 -- If the subprogram is implicit it is hidden by the previous
4631 -- declaration. However if it is dispatching, it must appear in
4632 -- the dispatch table anyway, because it can be dispatched to
4633 -- even if it cannot be called directly.
4635 elsif Present (Alias (S))
4636 and then not Comes_From_Source (S)
4637 then
4638 Set_Scope (S, Current_Scope);
4640 if Is_Dispatching_Operation (Alias (S)) then
4641 Check_Dispatching_Operation (S, Empty);
4642 end if;
4644 return;
4646 else
4647 Error_Msg_Sloc := Sloc (E);
4648 Error_Msg_N ("& conflicts with declaration#", S);
4650 -- Useful additional warning
4652 if Is_Generic_Unit (E) then
4653 Error_Msg_N ("\previous generic unit cannot be overloaded", S);
4654 end if;
4656 return;
4657 end if;
4659 -- E exists and is overloadable
4661 else
4662 -- Loop through E and its homonyms to determine if any of them
4663 -- is the candidate for overriding by S.
4665 while Present (E) loop
4667 -- Definitely not interesting if not in the current scope
4669 if Scope (E) /= Current_Scope then
4670 null;
4672 -- Check if we have type conformance
4674 elsif Type_Conformant (E, S) then
4676 -- If the old and new entities have the same profile and
4677 -- one is not the body of the other, then this is an error,
4678 -- unless one of them is implicitly declared.
4680 -- There are some cases when both can be implicit, for example
4681 -- when both a literal and a function that overrides it are
4682 -- inherited in a derivation, or when an inhertited operation
4683 -- of a tagged full type overrides the ineherited operation of
4684 -- a private extension. Ada 83 had a special rule for the
4685 -- the literal case. In Ada95, the later implicit operation
4686 -- hides the former, and the literal is always the former.
4687 -- In the odd case where both are derived operations declared
4688 -- at the same point, both operations should be declared,
4689 -- and in that case we bypass the following test and proceed
4690 -- to the next part (this can only occur for certain obscure
4691 -- cases involving homographs in instances and can't occur for
4692 -- dispatching operations ???). Note that the following
4693 -- condition is less than clear. For example, it's not at
4694 -- all clear why there's a test for E_Entry here. ???
4696 if Present (Alias (S))
4697 and then (No (Alias (E))
4698 or else Comes_From_Source (E)
4699 or else Is_Dispatching_Operation (E))
4700 and then
4701 (Ekind (E) = E_Entry
4702 or else Ekind (E) /= E_Enumeration_Literal)
4703 then
4704 -- When an derived operation is overloaded it may be due
4705 -- to the fact that the full view of a private extension
4706 -- re-inherits. It has to be dealt with.
4708 if Is_Package (Current_Scope)
4709 and then In_Private_Part (Current_Scope)
4710 then
4711 Check_Operation_From_Private_View (S, E);
4712 end if;
4714 -- In any case the implicit operation remains hidden by
4715 -- the existing declaration, which is overriding.
4717 Set_Is_Overriding_Operation (E);
4718 return;
4720 -- Within an instance, the renaming declarations for
4721 -- actual subprograms may become ambiguous, but they do
4722 -- not hide each other.
4724 elsif Ekind (E) /= E_Entry
4725 and then not Comes_From_Source (E)
4726 and then not Is_Generic_Instance (E)
4727 and then (Present (Alias (E))
4728 or else Is_Intrinsic_Subprogram (E))
4729 and then (not In_Instance
4730 or else No (Parent (E))
4731 or else Nkind (Unit_Declaration_Node (E)) /=
4732 N_Subprogram_Renaming_Declaration)
4733 then
4734 -- A subprogram child unit is not allowed to override
4735 -- an inherited subprogram (10.1.1(20)).
4737 if Is_Child_Unit (S) then
4738 Error_Msg_N
4739 ("child unit overrides inherited subprogram in parent",
4741 return;
4742 end if;
4744 if Is_Non_Overriding_Operation (E, S) then
4745 Enter_Overloaded_Entity (S);
4746 if not Present (Derived_Type)
4747 or else Is_Tagged_Type (Derived_Type)
4748 then
4749 Check_Dispatching_Operation (S, Empty);
4750 end if;
4752 return;
4753 end if;
4755 -- E is a derived operation or an internal operator which
4756 -- is being overridden. Remove E from further visibility.
4757 -- Furthermore, if E is a dispatching operation, it must be
4758 -- replaced in the list of primitive operations of its type
4759 -- (see Override_Dispatching_Operation).
4761 declare
4762 Prev : Entity_Id;
4764 begin
4765 Prev := First_Entity (Current_Scope);
4767 while Present (Prev)
4768 and then Next_Entity (Prev) /= E
4769 loop
4770 Next_Entity (Prev);
4771 end loop;
4773 -- It is possible for E to be in the current scope and
4774 -- yet not in the entity chain. This can only occur in a
4775 -- generic context where E is an implicit concatenation
4776 -- in the formal part, because in a generic body the
4777 -- entity chain starts with the formals.
4779 pragma Assert
4780 (Present (Prev) or else Chars (E) = Name_Op_Concat);
4782 -- E must be removed both from the entity_list of the
4783 -- current scope, and from the visibility chain
4785 if Debug_Flag_E then
4786 Write_Str ("Override implicit operation ");
4787 Write_Int (Int (E));
4788 Write_Eol;
4789 end if;
4791 -- If E is a predefined concatenation, it stands for four
4792 -- different operations. As a result, a single explicit
4793 -- declaration does not hide it. In a possible ambiguous
4794 -- situation, Disambiguate chooses the user-defined op,
4795 -- so it is correct to retain the previous internal one.
4797 if Chars (E) /= Name_Op_Concat
4798 or else Ekind (E) /= E_Operator
4799 then
4800 -- For nondispatching derived operations that are
4801 -- overridden by a subprogram declared in the private
4802 -- part of a package, we retain the derived subprogram
4803 -- but mark it as not immediately visible. If the
4804 -- derived operation was declared in the visible part
4805 -- then this ensures that it will still be visible
4806 -- outside the package with the proper signature
4807 -- (calls from outside must also be directed to this
4808 -- version rather than the overriding one, unlike the
4809 -- dispatching case). Calls from inside the package
4810 -- will still resolve to the overriding subprogram
4811 -- since the derived one is marked as not visible
4812 -- within the package.
4814 -- If the private operation is dispatching, we achieve
4815 -- the overriding by keeping the implicit operation
4816 -- but setting its alias to be the overring one. In
4817 -- this fashion the proper body is executed in all
4818 -- cases, but the original signature is used outside
4819 -- of the package.
4821 -- If the overriding is not in the private part, we
4822 -- remove the implicit operation altogether.
4824 if Is_Private_Declaration (S) then
4826 if not Is_Dispatching_Operation (E) then
4827 Set_Is_Immediately_Visible (E, False);
4828 else
4829 -- Work done in Override_Dispatching_Operation,
4830 -- so nothing else need to be done here.
4832 null;
4833 end if;
4835 else
4836 -- Find predecessor of E in Homonym chain
4838 if E = Current_Entity (E) then
4839 Prev_Vis := Empty;
4840 else
4841 Prev_Vis := Current_Entity (E);
4842 while Homonym (Prev_Vis) /= E loop
4843 Prev_Vis := Homonym (Prev_Vis);
4844 end loop;
4845 end if;
4847 if Prev_Vis /= Empty then
4849 -- Skip E in the visibility chain
4851 Set_Homonym (Prev_Vis, Homonym (E));
4853 else
4854 Set_Name_Entity_Id (Chars (E), Homonym (E));
4855 end if;
4857 Set_Next_Entity (Prev, Next_Entity (E));
4859 if No (Next_Entity (Prev)) then
4860 Set_Last_Entity (Current_Scope, Prev);
4861 end if;
4863 end if;
4864 end if;
4866 Enter_Overloaded_Entity (S);
4867 Set_Is_Overriding_Operation (S);
4869 if Is_Dispatching_Operation (E) then
4871 -- An overriding dispatching subprogram inherits
4872 -- the convention of the overridden subprogram
4873 -- (by AI-117).
4875 Set_Convention (S, Convention (E));
4877 Check_Dispatching_Operation (S, E);
4878 else
4879 Check_Dispatching_Operation (S, Empty);
4880 end if;
4882 Maybe_Primitive_Operation (Overriding => True);
4883 goto Check_Inequality;
4884 end;
4886 -- Apparent redeclarations in instances can occur when two
4887 -- formal types get the same actual type. The subprograms in
4888 -- in the instance are legal, even if not callable from the
4889 -- outside. Calls from within are disambiguated elsewhere.
4890 -- For dispatching operations in the visible part, the usual
4891 -- rules apply, and operations with the same profile are not
4892 -- legal (B830001).
4894 elsif (In_Instance_Visible_Part
4895 and then not Is_Dispatching_Operation (E))
4896 or else In_Instance_Not_Visible
4897 then
4898 null;
4900 -- Here we have a real error (identical profile)
4902 else
4903 Error_Msg_Sloc := Sloc (E);
4905 -- Avoid cascaded errors if the entity appears in
4906 -- subsequent calls.
4908 Set_Scope (S, Current_Scope);
4910 Error_Msg_N ("& conflicts with declaration#", S);
4912 if Is_Generic_Instance (S)
4913 and then not Has_Completion (E)
4914 then
4915 Error_Msg_N
4916 ("\instantiation cannot provide body for it", S);
4917 end if;
4919 return;
4920 end if;
4922 else
4923 null;
4924 end if;
4926 Prev_Vis := E;
4927 E := Homonym (E);
4928 end loop;
4930 -- On exit, we know that S is a new entity
4932 Enter_Overloaded_Entity (S);
4933 Maybe_Primitive_Operation;
4935 -- If S is a derived operation for an untagged type then
4936 -- by definition it's not a dispatching operation (even
4937 -- if the parent operation was dispatching), so we don't
4938 -- call Check_Dispatching_Operation in that case.
4940 if not Present (Derived_Type)
4941 or else Is_Tagged_Type (Derived_Type)
4942 then
4943 Check_Dispatching_Operation (S, Empty);
4944 end if;
4945 end if;
4947 -- If this is a user-defined equality operator that is not
4948 -- a derived subprogram, create the corresponding inequality.
4949 -- If the operation is dispatching, the expansion is done
4950 -- elsewhere, and we do not create an explicit inequality
4951 -- operation.
4953 <<Check_Inequality>>
4954 if Chars (S) = Name_Op_Eq
4955 and then Etype (S) = Standard_Boolean
4956 and then Present (Parent (S))
4957 and then not Is_Dispatching_Operation (S)
4958 then
4959 Make_Inequality_Operator (S);
4960 end if;
4961 end New_Overloaded_Entity;
4963 ---------------------
4964 -- Process_Formals --
4965 ---------------------
4967 procedure Process_Formals
4968 (T : List_Id;
4969 Related_Nod : Node_Id)
4971 Param_Spec : Node_Id;
4972 Formal : Entity_Id;
4973 Formal_Type : Entity_Id;
4974 Default : Node_Id;
4975 Ptype : Entity_Id;
4977 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
4978 -- Check whether the default has a class-wide type. After analysis
4979 -- the default has the type of the formal, so we must also check
4980 -- explicitly for an access attribute.
4982 ---------------------------
4983 -- Is_Class_Wide_Default --
4984 ---------------------------
4986 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
4987 begin
4988 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
4989 or else (Nkind (D) = N_Attribute_Reference
4990 and then Attribute_Name (D) = Name_Access
4991 and then Is_Class_Wide_Type (Etype (Prefix (D))));
4992 end Is_Class_Wide_Default;
4994 -- Start of processing for Process_Formals
4996 begin
4997 -- In order to prevent premature use of the formals in the same formal
4998 -- part, the Ekind is left undefined until all default expressions are
4999 -- analyzed. The Ekind is established in a separate loop at the end.
5001 Param_Spec := First (T);
5003 while Present (Param_Spec) loop
5005 Formal := Defining_Identifier (Param_Spec);
5006 Enter_Name (Formal);
5008 -- Case of ordinary parameters
5010 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
5011 Find_Type (Parameter_Type (Param_Spec));
5012 Ptype := Parameter_Type (Param_Spec);
5014 if Ptype = Error then
5015 goto Continue;
5016 end if;
5018 Formal_Type := Entity (Ptype);
5020 if Ekind (Formal_Type) = E_Incomplete_Type
5021 or else (Is_Class_Wide_Type (Formal_Type)
5022 and then Ekind (Root_Type (Formal_Type)) =
5023 E_Incomplete_Type)
5024 then
5025 -- Ada 2005 (AI-50217): Incomplete tagged types that are made
5026 -- visible by a limited with_clause are valid formal types.
5028 if From_With_Type (Formal_Type)
5029 and then Is_Tagged_Type (Formal_Type)
5030 then
5031 null;
5033 elsif Nkind (Parent (T)) /= N_Access_Function_Definition
5034 and then Nkind (Parent (T)) /= N_Access_Procedure_Definition
5035 then
5036 Error_Msg_N ("invalid use of incomplete type", Param_Spec);
5037 end if;
5039 elsif Ekind (Formal_Type) = E_Void then
5040 Error_Msg_NE ("premature use of&",
5041 Parameter_Type (Param_Spec), Formal_Type);
5042 end if;
5044 -- Ada 2005 (AI-231): Create and decorate an internal subtype
5045 -- declaration corresponding to the null-excluding type of the
5046 -- formal in the enclosing scope. In addition, replace the
5047 -- parameter type of the formal to this internal subtype.
5049 if Null_Exclusion_Present (Param_Spec) then
5050 declare
5051 Loc : constant Source_Ptr := Sloc (Param_Spec);
5053 Anon : constant Entity_Id :=
5054 Make_Defining_Identifier (Loc,
5055 Chars => New_Internal_Name ('S'));
5057 Curr_Scope : constant Scope_Stack_Entry :=
5058 Scope_Stack.Table (Scope_Stack.Last);
5060 Ptype : constant Node_Id := Parameter_Type (Param_Spec);
5061 Decl : Node_Id;
5062 P : Node_Id := Parent (Parent (Related_Nod));
5064 begin
5065 Set_Is_Internal (Anon);
5067 Decl :=
5068 Make_Subtype_Declaration (Loc,
5069 Defining_Identifier => Anon,
5070 Null_Exclusion_Present => True,
5071 Subtype_Indication =>
5072 New_Occurrence_Of (Etype (Ptype), Loc));
5074 -- Propagate the null-excluding attribute to the new entity
5076 if Null_Exclusion_Present (Param_Spec) then
5077 Set_Null_Exclusion_Present (Param_Spec, False);
5078 Set_Can_Never_Be_Null (Anon);
5079 end if;
5081 Mark_Rewrite_Insertion (Decl);
5083 -- Insert the new declaration in the nearest enclosing scope
5085 while not Has_Declarations (P) loop
5086 P := Parent (P);
5087 end loop;
5089 Prepend (Decl, Declarations (P));
5091 Rewrite (Ptype, New_Occurrence_Of (Anon, Loc));
5092 Mark_Rewrite_Insertion (Ptype);
5094 -- Analyze the new declaration in the context of the
5095 -- enclosing scope
5097 Scope_Stack.Decrement_Last;
5098 Analyze (Decl);
5099 Scope_Stack.Append (Curr_Scope);
5101 Formal_Type := Anon;
5102 end;
5103 end if;
5105 -- Ada 2005 (AI-231): Static checks
5107 if Null_Exclusion_Present (Param_Spec)
5108 or else Can_Never_Be_Null (Entity (Ptype))
5109 then
5110 Null_Exclusion_Static_Checks (Param_Spec);
5111 end if;
5113 -- An access formal type
5115 else
5116 Formal_Type :=
5117 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
5119 -- Ada 2005 (AI-254)
5121 declare
5122 AD : constant Node_Id :=
5123 Access_To_Subprogram_Definition
5124 (Parameter_Type (Param_Spec));
5125 begin
5126 if Present (AD) and then Protected_Present (AD) then
5127 Formal_Type :=
5128 Replace_Anonymous_Access_To_Protected_Subprogram
5129 (Param_Spec, Formal_Type);
5130 end if;
5131 end;
5132 end if;
5134 Set_Etype (Formal, Formal_Type);
5135 Default := Expression (Param_Spec);
5137 if Present (Default) then
5138 if Out_Present (Param_Spec) then
5139 Error_Msg_N
5140 ("default initialization only allowed for IN parameters",
5141 Param_Spec);
5142 end if;
5144 -- Do the special preanalysis of the expression (see section on
5145 -- "Handling of Default Expressions" in the spec of package Sem).
5147 Analyze_Per_Use_Expression (Default, Formal_Type);
5149 -- Check that the designated type of an access parameter's
5150 -- default is not a class-wide type unless the parameter's
5151 -- designated type is also class-wide.
5153 if Ekind (Formal_Type) = E_Anonymous_Access_Type
5154 and then Is_Class_Wide_Default (Default)
5155 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
5156 then
5157 Error_Msg_N
5158 ("access to class-wide expression not allowed here", Default);
5159 end if;
5160 end if;
5162 <<Continue>>
5163 Next (Param_Spec);
5164 end loop;
5166 -- Now set the kind (mode) of each formal
5168 Param_Spec := First (T);
5170 while Present (Param_Spec) loop
5171 Formal := Defining_Identifier (Param_Spec);
5172 Set_Formal_Mode (Formal);
5174 if Ekind (Formal) = E_In_Parameter then
5175 Set_Default_Value (Formal, Expression (Param_Spec));
5177 if Present (Expression (Param_Spec)) then
5178 Default := Expression (Param_Spec);
5180 if Is_Scalar_Type (Etype (Default)) then
5181 if Nkind
5182 (Parameter_Type (Param_Spec)) /= N_Access_Definition
5183 then
5184 Formal_Type := Entity (Parameter_Type (Param_Spec));
5186 else
5187 Formal_Type := Access_Definition
5188 (Related_Nod, Parameter_Type (Param_Spec));
5189 end if;
5191 Apply_Scalar_Range_Check (Default, Formal_Type);
5192 end if;
5193 end if;
5194 end if;
5196 Next (Param_Spec);
5197 end loop;
5199 end Process_Formals;
5201 ----------------------------
5202 -- Reference_Body_Formals --
5203 ----------------------------
5205 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
5206 Fs : Entity_Id;
5207 Fb : Entity_Id;
5209 begin
5210 if Error_Posted (Spec) then
5211 return;
5212 end if;
5214 Fs := First_Formal (Spec);
5215 Fb := First_Formal (Bod);
5217 while Present (Fs) loop
5218 Generate_Reference (Fs, Fb, 'b');
5220 if Style_Check then
5221 Style.Check_Identifier (Fb, Fs);
5222 end if;
5224 Set_Spec_Entity (Fb, Fs);
5225 Set_Referenced (Fs, False);
5226 Next_Formal (Fs);
5227 Next_Formal (Fb);
5228 end loop;
5229 end Reference_Body_Formals;
5231 -------------------------
5232 -- Set_Actual_Subtypes --
5233 -------------------------
5235 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
5236 Loc : constant Source_Ptr := Sloc (N);
5237 Decl : Node_Id;
5238 Formal : Entity_Id;
5239 T : Entity_Id;
5240 First_Stmt : Node_Id := Empty;
5241 AS_Needed : Boolean;
5243 begin
5244 -- If this is an emtpy initialization procedure, no need to create
5245 -- actual subtypes (small optimization).
5247 if Ekind (Subp) = E_Procedure
5248 and then Is_Null_Init_Proc (Subp)
5249 then
5250 return;
5251 end if;
5253 Formal := First_Formal (Subp);
5254 while Present (Formal) loop
5255 T := Etype (Formal);
5257 -- We never need an actual subtype for a constrained formal
5259 if Is_Constrained (T) then
5260 AS_Needed := False;
5262 -- If we have unknown discriminants, then we do not need an
5263 -- actual subtype, or more accurately we cannot figure it out!
5264 -- Note that all class-wide types have unknown discriminants.
5266 elsif Has_Unknown_Discriminants (T) then
5267 AS_Needed := False;
5269 -- At this stage we have an unconstrained type that may need
5270 -- an actual subtype. For sure the actual subtype is needed
5271 -- if we have an unconstrained array type.
5273 elsif Is_Array_Type (T) then
5274 AS_Needed := True;
5276 -- The only other case which needs an actual subtype is an
5277 -- unconstrained record type which is an IN parameter (we
5278 -- cannot generate actual subtypes for the OUT or IN OUT case,
5279 -- since an assignment can change the discriminant values.
5280 -- However we exclude the case of initialization procedures,
5281 -- since discriminants are handled very specially in this context,
5282 -- see the section entitled "Handling of Discriminants" in Einfo.
5283 -- We also exclude the case of Discrim_SO_Functions (functions
5284 -- used in front end layout mode for size/offset values), since
5285 -- in such functions only discriminants are referenced, and not
5286 -- only are such subtypes not needed, but they cannot always
5287 -- be generated, because of order of elaboration issues.
5289 elsif Is_Record_Type (T)
5290 and then Ekind (Formal) = E_In_Parameter
5291 and then Chars (Formal) /= Name_uInit
5292 and then not Is_Unchecked_Union (T)
5293 and then not Is_Discrim_SO_Function (Subp)
5294 then
5295 AS_Needed := True;
5297 -- All other cases do not need an actual subtype
5299 else
5300 AS_Needed := False;
5301 end if;
5303 -- Generate actual subtypes for unconstrained arrays and
5304 -- unconstrained discriminated records.
5306 if AS_Needed then
5307 if Nkind (N) = N_Accept_Statement then
5309 -- If expansion is active, The formal is replaced by a local
5310 -- variable that renames the corresponding entry of the
5311 -- parameter block, and it is this local variable that may
5312 -- require an actual subtype.
5314 if Expander_Active then
5315 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
5316 else
5317 Decl := Build_Actual_Subtype (T, Formal);
5318 end if;
5320 if Present (Handled_Statement_Sequence (N)) then
5321 First_Stmt :=
5322 First (Statements (Handled_Statement_Sequence (N)));
5323 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
5324 Mark_Rewrite_Insertion (Decl);
5325 else
5326 -- If the accept statement has no body, there will be
5327 -- no reference to the actuals, so no need to compute
5328 -- actual subtypes.
5330 return;
5331 end if;
5333 else
5334 Decl := Build_Actual_Subtype (T, Formal);
5335 Prepend (Decl, Declarations (N));
5336 Mark_Rewrite_Insertion (Decl);
5337 end if;
5339 -- The declaration uses the bounds of an existing object,
5340 -- and therefore needs no constraint checks.
5342 Analyze (Decl, Suppress => All_Checks);
5344 -- We need to freeze manually the generated type when it is
5345 -- inserted anywhere else than in a declarative part.
5347 if Present (First_Stmt) then
5348 Insert_List_Before_And_Analyze (First_Stmt,
5349 Freeze_Entity (Defining_Identifier (Decl), Loc));
5350 end if;
5352 if Nkind (N) = N_Accept_Statement
5353 and then Expander_Active
5354 then
5355 Set_Actual_Subtype (Renamed_Object (Formal),
5356 Defining_Identifier (Decl));
5357 else
5358 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
5359 end if;
5360 end if;
5362 Next_Formal (Formal);
5363 end loop;
5364 end Set_Actual_Subtypes;
5366 ---------------------
5367 -- Set_Formal_Mode --
5368 ---------------------
5370 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
5371 Spec : constant Node_Id := Parent (Formal_Id);
5373 begin
5374 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
5375 -- since we ensure that corresponding actuals are always valid at the
5376 -- point of the call.
5378 if Out_Present (Spec) then
5379 if Ekind (Scope (Formal_Id)) = E_Function
5380 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
5381 then
5382 Error_Msg_N ("functions can only have IN parameters", Spec);
5383 Set_Ekind (Formal_Id, E_In_Parameter);
5385 elsif In_Present (Spec) then
5386 Set_Ekind (Formal_Id, E_In_Out_Parameter);
5388 else
5389 Set_Ekind (Formal_Id, E_Out_Parameter);
5390 Set_Never_Set_In_Source (Formal_Id, True);
5391 Set_Is_True_Constant (Formal_Id, False);
5392 Set_Current_Value (Formal_Id, Empty);
5393 end if;
5395 else
5396 Set_Ekind (Formal_Id, E_In_Parameter);
5397 end if;
5399 -- Set Is_Known_Non_Null for access parameters since the language
5400 -- guarantees that access parameters are always non-null. We also
5401 -- set Can_Never_Be_Null, since there is no way to change the value.
5403 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
5405 -- Ada 2005 (AI-231): This behaviour has been modified in Ada 2005.
5406 -- It is only forced if the null_exclusion appears.
5408 if Ada_Version < Ada_05
5409 or else Null_Exclusion_Present (Spec)
5410 then
5411 Set_Is_Known_Non_Null (Formal_Id);
5412 Set_Can_Never_Be_Null (Formal_Id);
5413 end if;
5414 end if;
5416 Set_Mechanism (Formal_Id, Default_Mechanism);
5417 Set_Formal_Validity (Formal_Id);
5418 end Set_Formal_Mode;
5420 -------------------------
5421 -- Set_Formal_Validity --
5422 -------------------------
5424 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
5425 begin
5426 -- If no validity checking, then we cannot assume anything about
5427 -- the validity of parameters, since we do not know there is any
5428 -- checking of the validity on the call side.
5430 if not Validity_Checks_On then
5431 return;
5433 -- If validity checking for parameters is enabled, this means we are
5434 -- not supposed to make any assumptions about argument values.
5436 elsif Validity_Check_Parameters then
5437 return;
5439 -- If we are checking in parameters, we will assume that the caller is
5440 -- also checking parameters, so we can assume the parameter is valid.
5442 elsif Ekind (Formal_Id) = E_In_Parameter
5443 and then Validity_Check_In_Params
5444 then
5445 Set_Is_Known_Valid (Formal_Id, True);
5447 -- Similar treatment for IN OUT parameters
5449 elsif Ekind (Formal_Id) = E_In_Out_Parameter
5450 and then Validity_Check_In_Out_Params
5451 then
5452 Set_Is_Known_Valid (Formal_Id, True);
5453 end if;
5454 end Set_Formal_Validity;
5456 ------------------------
5457 -- Subtype_Conformant --
5458 ------------------------
5460 function Subtype_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5461 Result : Boolean;
5463 begin
5464 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result);
5465 return Result;
5466 end Subtype_Conformant;
5468 ---------------------
5469 -- Type_Conformant --
5470 ---------------------
5472 function Type_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5473 Result : Boolean;
5474 begin
5475 Check_Conformance (New_Id, Old_Id, Type_Conformant, False, Result);
5476 return Result;
5477 end Type_Conformant;
5479 -------------------------------
5480 -- Valid_Operator_Definition --
5481 -------------------------------
5483 procedure Valid_Operator_Definition (Designator : Entity_Id) is
5484 N : Integer := 0;
5485 F : Entity_Id;
5486 Id : constant Name_Id := Chars (Designator);
5487 N_OK : Boolean;
5489 begin
5490 F := First_Formal (Designator);
5492 while Present (F) loop
5493 N := N + 1;
5495 if Present (Default_Value (F)) then
5496 Error_Msg_N
5497 ("default values not allowed for operator parameters",
5498 Parent (F));
5499 end if;
5501 Next_Formal (F);
5502 end loop;
5504 -- Verify that user-defined operators have proper number of arguments
5505 -- First case of operators which can only be unary
5507 if Id = Name_Op_Not
5508 or else Id = Name_Op_Abs
5509 then
5510 N_OK := (N = 1);
5512 -- Case of operators which can be unary or binary
5514 elsif Id = Name_Op_Add
5515 or Id = Name_Op_Subtract
5516 then
5517 N_OK := (N in 1 .. 2);
5519 -- All other operators can only be binary
5521 else
5522 N_OK := (N = 2);
5523 end if;
5525 if not N_OK then
5526 Error_Msg_N
5527 ("incorrect number of arguments for operator", Designator);
5528 end if;
5530 if Id = Name_Op_Ne
5531 and then Base_Type (Etype (Designator)) = Standard_Boolean
5532 and then not Is_Intrinsic_Subprogram (Designator)
5533 then
5534 Error_Msg_N
5535 ("explicit definition of inequality not allowed", Designator);
5536 end if;
5537 end Valid_Operator_Definition;
5539 end Sem_Ch6;