PR c++/3637
[official-gcc.git] / gcc / ada / sem_ch6.adb
blobdcec5bae7e1e5af5ec997cc78f64a07a87cac6e5
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- $Revision$
10 -- --
11 -- Copyright (C) 1992-2001, Free Software Foundation, Inc. --
12 -- --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
23 -- --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 -- --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Checks; use Checks;
31 with Debug; use Debug;
32 with Einfo; use Einfo;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Expander; use Expander;
36 with Exp_Ch7; use Exp_Ch7;
37 with Fname; use Fname;
38 with Freeze; use Freeze;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Lib; use Lib;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Output; use Output;
46 with Rtsfind; use Rtsfind;
47 with Sem; use Sem;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch4; use Sem_Ch4;
51 with Sem_Ch5; use Sem_Ch5;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch12; use Sem_Ch12;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Dist; use Sem_Dist;
56 with Sem_Elim; use Sem_Elim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Mech; use Sem_Mech;
59 with Sem_Prag; use Sem_Prag;
60 with Sem_Res; use Sem_Res;
61 with Sem_Util; use Sem_Util;
62 with Sem_Type; use Sem_Type;
63 with Sem_Warn; use Sem_Warn;
64 with Sinput; use Sinput;
65 with Stand; use Stand;
66 with Sinfo; use Sinfo;
67 with Sinfo.CN; use Sinfo.CN;
68 with Snames; use Snames;
69 with Stringt; use Stringt;
70 with Style;
71 with Stylesw; use Stylesw;
72 with Tbuild; use Tbuild;
73 with Uintp; use Uintp;
74 with Urealp; use Urealp;
75 with Validsw; use Validsw;
77 package body Sem_Ch6 is
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
84 -- Analyze a generic subprogram body
86 function Build_Body_To_Inline
87 (N : Node_Id;
88 Subp : Entity_Id;
89 Orig_Body : Node_Id)
90 return Boolean;
91 -- If a subprogram has pragma Inline and inlining is active, use generic
92 -- machinery to build an unexpanded body for the subprogram. This body is
93 -- subsequenty used for inline expansions at call sites. If subprogram can
94 -- be inlined (depending on size and nature of local declarations) this
95 -- function returns true. Otherwise subprogram body is treated normally.
97 type Conformance_Type is
98 (Type_Conformant, Mode_Conformant, Subtype_Conformant, Fully_Conformant);
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_Subprogram_Order (N : Node_Id);
121 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
122 -- the alpha ordering rule for N if this ordering requirement applicable.
124 function Is_Non_Overriding_Operation
125 (Prev_E : Entity_Id;
126 New_E : Entity_Id)
127 return Boolean;
128 -- Enforce the rule given in 12.3(18): a private operation in an instance
129 -- overrides an inherited operation only if the corresponding operation
130 -- was overriding in the generic. This can happen for primitive operations
131 -- of types derived (in the generic unit) from formal private or formal
132 -- derived types.
134 procedure Check_Returns
135 (HSS : Node_Id;
136 Mode : Character;
137 Err : out Boolean);
138 -- Called to check for missing return statements in a function body,
139 -- or for returns present in a procedure body which has No_Return set.
140 -- L is the handled statement sequence for the subprogram body. This
141 -- procedure checks all flow paths to make sure they either have a
142 -- return (Mode = 'F') or do not have a return (Mode = 'P'). The flag
143 -- Err is set if there are any control paths not explicitly terminated
144 -- by a return in the function case, and is True otherwise.
146 function Conforming_Types
147 (T1 : Entity_Id;
148 T2 : Entity_Id;
149 Ctype : Conformance_Type;
150 Get_Inst : Boolean := False)
151 return Boolean;
152 -- Check that two formal parameter types conform, checking both
153 -- for equality of base types, and where required statically
154 -- matching subtypes, depending on the setting of Ctype.
156 procedure Enter_Overloaded_Entity (S : Entity_Id);
157 -- This procedure makes S, a new overloaded entity, into the first
158 -- visible entity with that name.
160 procedure Install_Entity (E : Entity_Id);
161 -- Make single entity visible. Used for generic formals as well.
163 procedure Install_Formals (Id : Entity_Id);
164 -- On entry to a subprogram body, make the formals visible. Note
165 -- that simply placing the subprogram on the scope stack is not
166 -- sufficient: the formals must become the current entities for
167 -- their names.
169 procedure Make_Inequality_Operator (S : Entity_Id);
170 -- Create the declaration for an inequality operator that is implicitly
171 -- created by a user-defined equality operator that yields a boolean.
173 procedure May_Need_Actuals (Fun : Entity_Id);
174 -- Flag functions that can be called without parameters, i.e. those that
175 -- have no parameters, or those for which defaults exist for all parameters
177 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
178 -- Formal_Id is an formal parameter entity. This procedure deals with
179 -- setting the proper validity status for this entity, which depends
180 -- on the kind of parameter and the validity checking mode.
182 ---------------------------------------------
183 -- Analyze_Abstract_Subprogram_Declaration --
184 ---------------------------------------------
186 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
187 Designator : constant Entity_Id := Analyze_Spec (Specification (N));
188 Scop : constant Entity_Id := Current_Scope;
190 begin
191 Generate_Definition (Designator);
192 Set_Is_Abstract (Designator);
193 New_Overloaded_Entity (Designator);
194 Check_Delayed_Subprogram (Designator);
196 Set_Is_Pure (Designator,
197 Is_Pure (Scop) and then Is_Library_Level_Entity (Designator));
198 Set_Is_Remote_Call_Interface (
199 Designator, Is_Remote_Call_Interface (Scop));
200 Set_Is_Remote_Types (Designator, Is_Remote_Types (Scop));
202 if Ekind (Scope (Designator)) = E_Protected_Type then
203 Error_Msg_N
204 ("abstract subprogram not allowed in protected type", N);
205 end if;
206 end Analyze_Abstract_Subprogram_Declaration;
208 ----------------------------
209 -- Analyze_Function_Call --
210 ----------------------------
212 procedure Analyze_Function_Call (N : Node_Id) is
213 P : constant Node_Id := Name (N);
214 L : constant List_Id := Parameter_Associations (N);
215 Actual : Node_Id;
217 begin
218 Analyze (P);
220 -- If error analyzing name, then set Any_Type as result type and return
222 if Etype (P) = Any_Type then
223 Set_Etype (N, Any_Type);
224 return;
225 end if;
227 -- Otherwise analyze the parameters
229 if Present (L) then
230 Actual := First (L);
232 while Present (Actual) loop
233 Analyze (Actual);
234 Check_Parameterless_Call (Actual);
235 Next (Actual);
236 end loop;
237 end if;
239 Analyze_Call (N);
241 end Analyze_Function_Call;
243 -------------------------------------
244 -- Analyze_Generic_Subprogram_Body --
245 -------------------------------------
247 procedure Analyze_Generic_Subprogram_Body
248 (N : Node_Id;
249 Gen_Id : Entity_Id)
251 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
252 Spec : Node_Id;
253 Kind : constant Entity_Kind := Ekind (Gen_Id);
254 Nam : Entity_Id;
255 New_N : Node_Id;
257 begin
258 -- Copy body and disable expansion while analyzing the generic
259 -- For a stub, do not copy the stub (which would load the proper body),
260 -- this will be done when the proper body is analyzed.
262 if Nkind (N) /= N_Subprogram_Body_Stub then
263 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
264 Rewrite (N, New_N);
265 Start_Generic;
266 end if;
268 Spec := Specification (N);
270 -- Within the body of the generic, the subprogram is callable, and
271 -- behaves like the corresponding non-generic unit.
273 Nam := Defining_Entity (Spec);
275 if Kind = E_Generic_Procedure
276 and then Nkind (Spec) /= N_Procedure_Specification
277 then
278 Error_Msg_N ("invalid body for generic procedure ", Nam);
279 return;
281 elsif Kind = E_Generic_Function
282 and then Nkind (Spec) /= N_Function_Specification
283 then
284 Error_Msg_N ("invalid body for generic function ", Nam);
285 return;
286 end if;
288 Set_Corresponding_Body (Gen_Decl, Nam);
290 if Has_Completion (Gen_Id)
291 and then Nkind (Parent (N)) /= N_Subunit
292 then
293 Error_Msg_N ("duplicate generic body", N);
294 return;
295 else
296 Set_Has_Completion (Gen_Id);
297 end if;
299 if Nkind (N) = N_Subprogram_Body_Stub then
300 Set_Ekind (Defining_Entity (Specification (N)), Kind);
301 else
302 Set_Corresponding_Spec (N, Gen_Id);
303 end if;
305 if Nkind (Parent (N)) = N_Compilation_Unit then
306 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
307 end if;
309 -- Make generic parameters immediately visible in the body. They are
310 -- needed to process the formals declarations. Then make the formals
311 -- visible in a separate step.
313 New_Scope (Gen_Id);
315 declare
316 E : Entity_Id;
317 First_Ent : Entity_Id;
319 begin
320 First_Ent := First_Entity (Gen_Id);
322 E := First_Ent;
323 while Present (E) and then not Is_Formal (E) loop
324 Install_Entity (E);
325 Next_Entity (E);
326 end loop;
328 Set_Use (Generic_Formal_Declarations (Gen_Decl));
330 -- Now generic formals are visible, and the specification can be
331 -- analyzed, for subsequent conformance check.
333 Nam := Analyze_Spec (Spec);
335 if Nkind (N) = N_Subprogram_Body_Stub then
337 -- Nothing to do if no body to process
339 Set_Ekind (Nam, Kind);
340 End_Scope;
341 return;
342 end if;
344 if Present (E) then
346 -- E is the first formal parameter, which must be the first
347 -- entity in the subprogram body.
349 Set_First_Entity (Gen_Id, E);
351 -- Now make formal parameters visible
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 (Nam));
362 Set_Convention (Nam, Convention (Gen_Id));
363 Set_Scope (Nam, Scope (Gen_Id));
364 Check_Fully_Conformant (Nam, Gen_Id, Nam);
366 -- If this is a compilation unit, it must be made visible
367 -- explicitly, because the compilation of the declaration,
368 -- unlike other library unit declarations, does not. If it
369 -- is not a unit, the following is redundant but harmless.
371 Set_Is_Immediately_Visible (Gen_Id);
373 Set_Actual_Subtypes (N, Current_Scope);
374 Analyze_Declarations (Declarations (N));
375 Check_Completion;
376 Analyze (Handled_Statement_Sequence (N));
378 Save_Global_References (Original_Node (N));
380 -- Prior to exiting the scope, include generic formals again
381 -- (if any are present) in the set of local entities.
383 if Present (First_Ent) then
384 Set_First_Entity (Gen_Id, First_Ent);
385 end if;
387 end;
389 End_Scope;
390 Check_Subprogram_Order (N);
392 -- Outside of its body, unit is generic again.
394 Set_Ekind (Gen_Id, Kind);
395 Set_Ekind (Nam, E_Subprogram_Body);
396 Generate_Reference (Gen_Id, Nam, 'b');
397 Style.Check_Identifier (Nam, Gen_Id);
398 End_Generic;
400 end Analyze_Generic_Subprogram_Body;
402 -----------------------------
403 -- Analyze_Operator_Symbol --
404 -----------------------------
406 -- An operator symbol such as "+" or "and" may appear in context where
407 -- the literal denotes an entity name, such as "+"(x, y) or in a
408 -- context when it is just a string, as in (conjunction = "or"). In
409 -- these cases the parser generates this node, and the semantics does
410 -- the disambiguation. Other such case are actuals in an instantiation,
411 -- the generic unit in an instantiation, and pragma arguments.
413 procedure Analyze_Operator_Symbol (N : Node_Id) is
414 Par : constant Node_Id := Parent (N);
416 begin
417 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
418 or else Nkind (Par) = N_Function_Instantiation
419 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
420 or else (Nkind (Par) = N_Pragma_Argument_Association
421 and then not Is_Pragma_String_Literal (Par))
422 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
423 or else (Nkind (Par) = N_Attribute_Reference
424 and then Attribute_Name (Par) /= Name_Value)
425 then
426 Find_Direct_Name (N);
428 else
429 Change_Operator_Symbol_To_String_Literal (N);
430 Analyze (N);
431 end if;
432 end Analyze_Operator_Symbol;
434 -----------------------------------
435 -- Analyze_Parameter_Association --
436 -----------------------------------
438 procedure Analyze_Parameter_Association (N : Node_Id) is
439 begin
440 Analyze (Explicit_Actual_Parameter (N));
441 end Analyze_Parameter_Association;
443 ----------------------------
444 -- Analyze_Procedure_Call --
445 ----------------------------
447 procedure Analyze_Procedure_Call (N : Node_Id) is
448 Loc : constant Source_Ptr := Sloc (N);
449 P : constant Node_Id := Name (N);
450 Actuals : constant List_Id := Parameter_Associations (N);
451 Actual : Node_Id;
452 New_N : Node_Id;
454 procedure Analyze_Call_And_Resolve;
455 -- Do Analyze and Resolve calls for procedure call
457 procedure Analyze_Call_And_Resolve is
458 begin
459 if Nkind (N) = N_Procedure_Call_Statement then
460 Analyze_Call (N);
461 Resolve (N, Standard_Void_Type);
462 else
463 Analyze (N);
464 end if;
465 end Analyze_Call_And_Resolve;
467 -- Start of processing for Analyze_Procedure_Call
469 begin
470 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
471 -- a procedure call or an entry call. The prefix may denote an access
472 -- to subprogram type, in which case an implicit dereference applies.
473 -- If the prefix is an indexed component (without implicit defererence)
474 -- then the construct denotes a call to a member of an entire family.
475 -- If the prefix is a simple name, it may still denote a call to a
476 -- parameterless member of an entry family. Resolution of these various
477 -- interpretations is delicate.
479 Analyze (P);
481 -- If error analyzing prefix, then set Any_Type as result and return
483 if Etype (P) = Any_Type then
484 Set_Etype (N, Any_Type);
485 return;
486 end if;
488 -- Otherwise analyze the parameters
490 if Present (Actuals) then
491 Actual := First (Actuals);
493 while Present (Actual) loop
494 Analyze (Actual);
495 Check_Parameterless_Call (Actual);
496 Next (Actual);
497 end loop;
498 end if;
500 -- Special processing for Elab_Spec and Elab_Body calls
502 if Nkind (P) = N_Attribute_Reference
503 and then (Attribute_Name (P) = Name_Elab_Spec
504 or else Attribute_Name (P) = Name_Elab_Body)
505 then
506 if Present (Actuals) then
507 Error_Msg_N
508 ("no parameters allowed for this call", First (Actuals));
509 return;
510 end if;
512 Set_Etype (N, Standard_Void_Type);
513 Set_Analyzed (N);
515 elsif Is_Entity_Name (P)
516 and then Is_Record_Type (Etype (Entity (P)))
517 and then Remote_AST_I_Dereference (P)
518 then
519 return;
521 elsif Is_Entity_Name (P)
522 and then Ekind (Entity (P)) /= E_Entry_Family
523 then
524 if Is_Access_Type (Etype (P))
525 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
526 and then No (Actuals)
527 and then Comes_From_Source (N)
528 then
529 Error_Msg_N ("missing explicit dereference in call", N);
530 end if;
532 Analyze_Call_And_Resolve;
534 -- If the prefix is the simple name of an entry family, this is
535 -- a parameterless call from within the task body itself.
537 elsif Is_Entity_Name (P)
538 and then Nkind (P) = N_Identifier
539 and then Ekind (Entity (P)) = E_Entry_Family
540 and then Present (Actuals)
541 and then No (Next (First (Actuals)))
542 then
543 -- Can be call to parameterless entry family. What appears to be
544 -- the sole argument is in fact the entry index. Rewrite prefix
545 -- of node accordingly. Source representation is unchanged by this
546 -- transformation.
548 New_N :=
549 Make_Indexed_Component (Loc,
550 Prefix =>
551 Make_Selected_Component (Loc,
552 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
553 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
554 Expressions => Actuals);
555 Set_Name (N, New_N);
556 Set_Etype (New_N, Standard_Void_Type);
557 Set_Parameter_Associations (N, No_List);
558 Analyze_Call_And_Resolve;
560 elsif Nkind (P) = N_Explicit_Dereference then
561 if Ekind (Etype (P)) = E_Subprogram_Type then
562 Analyze_Call_And_Resolve;
563 else
564 Error_Msg_N ("expect access to procedure in call", P);
565 end if;
567 -- The name can be a selected component or an indexed component
568 -- that yields an access to subprogram. Such a prefix is legal if
569 -- the call has parameter associations.
571 elsif Is_Access_Type (Etype (P))
572 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
573 then
574 if Present (Actuals) then
575 Analyze_Call_And_Resolve;
576 else
577 Error_Msg_N ("missing explicit dereference in call ", N);
578 end if;
580 -- If not an access to subprogram, then the prefix must resolve to
581 -- the name of an entry, entry family, or protected operation.
583 -- For the case of a simple entry call, P is a selected component
584 -- where the prefix is the task and the selector name is the entry.
585 -- A call to a protected procedure will have the same syntax. If
586 -- the protected object contains overloaded operations, the entity
587 -- may appear as a function, the context will select the operation
588 -- whose type is Void.
590 elsif Nkind (P) = N_Selected_Component
591 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
592 or else
593 Ekind (Entity (Selector_Name (P))) = E_Procedure
594 or else
595 Ekind (Entity (Selector_Name (P))) = E_Function)
596 then
597 Analyze_Call_And_Resolve;
599 elsif Nkind (P) = N_Selected_Component
600 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
601 and then Present (Actuals)
602 and then No (Next (First (Actuals)))
603 then
604 -- Can be call to parameterless entry family. What appears to be
605 -- the sole argument is in fact the entry index. Rewrite prefix
606 -- of node accordingly. Source representation is unchanged by this
607 -- transformation.
609 New_N :=
610 Make_Indexed_Component (Loc,
611 Prefix => New_Copy (P),
612 Expressions => Actuals);
613 Set_Name (N, New_N);
614 Set_Etype (New_N, Standard_Void_Type);
615 Set_Parameter_Associations (N, No_List);
616 Analyze_Call_And_Resolve;
618 -- For the case of a reference to an element of an entry family, P is
619 -- an indexed component whose prefix is a selected component (task and
620 -- entry family), and whose index is the entry family index.
622 elsif Nkind (P) = N_Indexed_Component
623 and then Nkind (Prefix (P)) = N_Selected_Component
624 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
625 then
626 Analyze_Call_And_Resolve;
628 -- If the prefix is the name of an entry family, it is a call from
629 -- within the task body itself.
631 elsif Nkind (P) = N_Indexed_Component
632 and then Nkind (Prefix (P)) = N_Identifier
633 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
634 then
635 New_N :=
636 Make_Selected_Component (Loc,
637 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
638 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
639 Rewrite (Prefix (P), New_N);
640 Analyze (P);
641 Analyze_Call_And_Resolve;
643 -- Anything else is an error.
645 else
646 Error_Msg_N ("Invalid procedure or entry call", N);
647 end if;
648 end Analyze_Procedure_Call;
650 ------------------------------
651 -- Analyze_Return_Statement --
652 ------------------------------
654 procedure Analyze_Return_Statement (N : Node_Id) is
655 Loc : constant Source_Ptr := Sloc (N);
656 Expr : Node_Id;
657 Scope_Id : Entity_Id;
658 Kind : Entity_Kind;
659 R_Type : Entity_Id;
661 begin
662 -- Find subprogram or accept statement enclosing the return statement
664 Scope_Id := Empty;
665 for J in reverse 0 .. Scope_Stack.Last loop
666 Scope_Id := Scope_Stack.Table (J).Entity;
667 exit when Ekind (Scope_Id) /= E_Block and then
668 Ekind (Scope_Id) /= E_Loop;
669 end loop;
671 pragma Assert (Present (Scope_Id));
673 Kind := Ekind (Scope_Id);
674 Expr := Expression (N);
676 if Kind /= E_Function
677 and then Kind /= E_Generic_Function
678 and then Kind /= E_Procedure
679 and then Kind /= E_Generic_Procedure
680 and then Kind /= E_Entry
681 and then Kind /= E_Entry_Family
682 then
683 Error_Msg_N ("illegal context for return statement", N);
685 elsif Present (Expr) then
686 if Kind = E_Function or else Kind = E_Generic_Function then
687 Set_Return_Present (Scope_Id);
688 R_Type := Etype (Scope_Id);
689 Set_Return_Type (N, R_Type);
690 Analyze_And_Resolve (Expr, R_Type);
692 if (Is_Class_Wide_Type (Etype (Expr))
693 or else Is_Dynamically_Tagged (Expr))
694 and then not Is_Class_Wide_Type (R_Type)
695 then
696 Error_Msg_N
697 ("dynamically tagged expression not allowed!", Expr);
698 end if;
700 Apply_Constraint_Check (Expr, R_Type);
702 -- ??? A real run-time accessibility check is needed
703 -- in cases involving dereferences of access parameters.
704 -- For now we just check the static cases.
706 if Is_Return_By_Reference_Type (Etype (Scope_Id))
707 and then Object_Access_Level (Expr)
708 > Subprogram_Access_Level (Scope_Id)
709 then
710 Rewrite (N, Make_Raise_Program_Error (Loc));
711 Analyze (N);
713 Error_Msg_N
714 ("cannot return a local value by reference?", N);
715 Error_Msg_NE
716 ("& will be raised at run time?!",
717 N, Standard_Program_Error);
718 end if;
720 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
721 Error_Msg_N ("procedure cannot return value (use function)", N);
723 else
724 Error_Msg_N ("accept statement cannot return value", N);
725 end if;
727 -- No expression present
729 else
730 if Kind = E_Function or Kind = E_Generic_Function then
731 Error_Msg_N ("missing expression in return from function", N);
732 end if;
734 if (Ekind (Scope_Id) = E_Procedure
735 or else Ekind (Scope_Id) = E_Generic_Procedure)
736 and then No_Return (Scope_Id)
737 then
738 Error_Msg_N
739 ("RETURN statement not allowed (No_Return)", N);
740 end if;
741 end if;
743 Check_Unreachable_Code (N);
744 end Analyze_Return_Statement;
746 ------------------
747 -- Analyze_Spec --
748 ------------------
750 function Analyze_Spec (N : Node_Id) return Entity_Id is
751 Designator : constant Entity_Id := Defining_Entity (N);
752 Formals : constant List_Id := Parameter_Specifications (N);
753 Typ : Entity_Id;
755 begin
756 Generate_Definition (Designator);
758 if Nkind (N) = N_Function_Specification then
759 Set_Ekind (Designator, E_Function);
760 Set_Mechanism (Designator, Default_Mechanism);
762 if Subtype_Mark (N) /= Error then
763 Find_Type (Subtype_Mark (N));
764 Typ := Entity (Subtype_Mark (N));
765 Set_Etype (Designator, Typ);
767 if (Ekind (Typ) = E_Incomplete_Type
768 or else (Is_Class_Wide_Type (Typ)
769 and then
770 Ekind (Root_Type (Typ)) = E_Incomplete_Type))
771 then
772 Error_Msg_N
773 ("invalid use of incomplete type", Subtype_Mark (N));
774 end if;
776 else
777 Set_Etype (Designator, Any_Type);
778 end if;
780 else
781 Set_Ekind (Designator, E_Procedure);
782 Set_Etype (Designator, Standard_Void_Type);
783 end if;
785 if Present (Formals) then
786 Set_Scope (Designator, Current_Scope);
787 New_Scope (Designator);
788 Process_Formals (Designator, Formals, N);
789 End_Scope;
790 end if;
792 if Nkind (N) = N_Function_Specification then
793 if Nkind (Designator) = N_Defining_Operator_Symbol then
794 Valid_Operator_Definition (Designator);
795 end if;
797 May_Need_Actuals (Designator);
799 if Is_Abstract (Etype (Designator))
800 and then Nkind (Parent (N)) /= N_Abstract_Subprogram_Declaration
801 then
802 Error_Msg_N
803 ("function that returns abstract type must be abstract", N);
804 end if;
805 end if;
807 return Designator;
808 end Analyze_Spec;
810 -----------------------------
811 -- Analyze_Subprogram_Body --
812 -----------------------------
814 -- This procedure is called for regular subprogram bodies, generic bodies,
815 -- and for subprogram stubs of both kinds. In the case of stubs, only the
816 -- specification matters, and is used to create a proper declaration for
817 -- the subprogram, or to perform conformance checks.
819 procedure Analyze_Subprogram_Body (N : Node_Id) is
820 Loc : constant Source_Ptr := Sloc (N);
821 Body_Spec : constant Node_Id := Specification (N);
822 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
823 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
825 HSS : Node_Id;
826 Spec_Id : Entity_Id;
827 Spec_Decl : Node_Id := Empty;
828 Last_Formal : Entity_Id := Empty;
829 Conformant : Boolean;
830 Missing_Ret : Boolean;
831 Body_Deleted : Boolean := False;
833 begin
834 if Debug_Flag_C then
835 Write_Str ("==== Compiling subprogram body ");
836 Write_Name (Chars (Body_Id));
837 Write_Str (" from ");
838 Write_Location (Loc);
839 Write_Eol;
840 end if;
842 Trace_Scope (N, Body_Id, " Analyze subprogram");
844 -- Generic subprograms are handled separately. They always have
845 -- a generic specification. Determine whether current scope has
846 -- a previous declaration.
848 -- If the subprogram body is defined within an instance of the
849 -- same name, the instance appears as a package renaming, and
850 -- will be hidden within the subprogram.
852 if Present (Prev_Id)
853 and then not Is_Overloadable (Prev_Id)
854 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
855 or else Comes_From_Source (Prev_Id))
856 then
857 if Ekind (Prev_Id) = E_Generic_Procedure
858 or else Ekind (Prev_Id) = E_Generic_Function
859 then
860 Spec_Id := Prev_Id;
861 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
862 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
864 Analyze_Generic_Subprogram_Body (N, Spec_Id);
865 return;
867 else
868 -- Previous entity conflicts with subprogram name.
869 -- Attempting to enter name will post error.
871 Enter_Name (Body_Id);
872 return;
873 end if;
875 -- Non-generic case, find the subprogram declaration, if one was
876 -- seen, or enter new overloaded entity in the current scope.
877 -- If the current_entity is the body_id itself, the unit is being
878 -- analyzed as part of the context of one of its subunits. No need
879 -- to redo the analysis.
881 elsif Prev_Id = Body_Id
882 and then Has_Completion (Body_Id)
883 then
884 return;
886 else
887 Body_Id := Analyze_Spec (Body_Spec);
889 if Nkind (N) = N_Subprogram_Body_Stub
890 or else No (Corresponding_Spec (N))
891 then
892 Spec_Id := Find_Corresponding_Spec (N);
894 -- If this is a duplicate body, no point in analyzing it
896 if Error_Posted (N) then
897 return;
898 end if;
900 -- A subprogram body should cause freezing of its own
901 -- declaration, but if there was no previous explicit
902 -- declaration, then the subprogram will get frozen too
903 -- late (there may be code within the body that depends
904 -- on the subprogram having been frozen, such as uses of
905 -- extra formals), so we force it to be frozen here.
906 -- Same holds if the body and the spec are compilation units.
908 if No (Spec_Id) then
909 Freeze_Before (N, Body_Id);
911 elsif Nkind (Parent (N)) = N_Compilation_Unit then
912 Freeze_Before (N, Spec_Id);
913 end if;
914 else
915 Spec_Id := Corresponding_Spec (N);
916 end if;
917 end if;
919 if No (Spec_Id)
920 and then Comes_From_Source (N)
921 and then Is_Protected_Type (Current_Scope)
922 then
923 -- Fully private operation in the body of the protected type. We
924 -- must create a declaration for the subprogram, in order to attach
925 -- the protected subprogram that will be used in internal calls.
927 declare
928 Decl : Node_Id;
929 Plist : List_Id;
930 Formal : Entity_Id;
931 New_Spec : Node_Id;
933 begin
934 Formal := First_Formal (Body_Id);
936 -- The protected operation always has at least one formal,
937 -- namely the object itself, but it is only placed in the
938 -- parameter list if expansion is enabled.
940 if Present (Formal)
941 or else Expander_Active
942 then
943 Plist := New_List;
945 else
946 Plist := No_List;
947 end if;
949 while Present (Formal) loop
950 Append
951 (Make_Parameter_Specification (Loc,
952 Defining_Identifier =>
953 Make_Defining_Identifier (Sloc (Formal),
954 Chars => Chars (Formal)),
955 In_Present => In_Present (Parent (Formal)),
956 Out_Present => Out_Present (Parent (Formal)),
957 Parameter_Type =>
958 New_Reference_To (Etype (Formal), Loc),
959 Expression =>
960 New_Copy_Tree (Expression (Parent (Formal)))),
961 Plist);
963 Next_Formal (Formal);
964 end loop;
966 if Nkind (Body_Spec) = N_Procedure_Specification then
967 New_Spec :=
968 Make_Procedure_Specification (Loc,
969 Defining_Unit_Name =>
970 Make_Defining_Identifier (Sloc (Body_Id),
971 Chars => Chars (Body_Id)),
972 Parameter_Specifications => Plist);
973 else
974 New_Spec :=
975 Make_Function_Specification (Loc,
976 Defining_Unit_Name =>
977 Make_Defining_Identifier (Sloc (Body_Id),
978 Chars => Chars (Body_Id)),
979 Parameter_Specifications => Plist,
980 Subtype_Mark => New_Occurrence_Of (Etype (Body_Id), Loc));
981 end if;
983 Decl :=
984 Make_Subprogram_Declaration (Loc,
985 Specification => New_Spec);
986 Insert_Before (N, Decl);
987 Analyze (Decl);
988 Spec_Id := Defining_Unit_Name (New_Spec);
989 Set_Has_Completion (Spec_Id);
990 Set_Convention (Spec_Id, Convention_Protected);
991 end;
993 elsif Present (Spec_Id) then
994 Spec_Decl := Unit_Declaration_Node (Spec_Id);
995 end if;
997 -- Place subprogram on scope stack, and make formals visible. If there
998 -- is a spec, the visible entity remains that of the spec.
1000 if Present (Spec_Id) then
1001 Generate_Reference (Spec_Id, Body_Id, 'b');
1002 Style.Check_Identifier (Body_Id, Spec_Id);
1004 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1005 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1007 if Is_Abstract (Spec_Id) then
1008 Error_Msg_N ("an abstract subprogram cannot have a body", N);
1009 return;
1010 else
1011 Set_Convention (Body_Id, Convention (Spec_Id));
1012 Set_Has_Completion (Spec_Id);
1014 if Is_Protected_Type (Scope (Spec_Id)) then
1015 Set_Privals_Chain (Spec_Id, New_Elmt_List);
1016 end if;
1018 -- If this is a body generated for a renaming, do not check for
1019 -- full conformance. The check is redundant, because the spec of
1020 -- the body is a copy of the spec in the renaming declaration,
1021 -- and the test can lead to spurious errors on nested defaults.
1023 if Present (Spec_Decl)
1024 and then Nkind (Original_Node (Spec_Decl)) =
1025 N_Subprogram_Renaming_Declaration
1026 and then not Comes_From_Source (N)
1027 then
1028 Conformant := True;
1029 else
1030 Check_Conformance
1031 (Body_Id, Spec_Id,
1032 Fully_Conformant, True, Conformant, Body_Id);
1033 end if;
1035 -- If the body is not fully conformant, we have to decide if we
1036 -- should analyze it or not. If it has a really messed up profile
1037 -- then we probably should not analyze it, since we will get too
1038 -- many bogus messages.
1040 -- Our decision is to go ahead in the non-fully conformant case
1041 -- only if it is at least mode conformant with the spec. Note
1042 -- that the call to Check_Fully_Conformant has issued the proper
1043 -- error messages to complain about the lack of conformance.
1045 if not Conformant
1046 and then not Mode_Conformant (Body_Id, Spec_Id)
1047 then
1048 return;
1049 end if;
1050 end if;
1052 -- Generate references from body formals to spec formals
1053 -- and also set the Spec_Entity fields for all formals
1055 if Spec_Id /= Body_Id then
1056 declare
1057 Fs : Entity_Id;
1058 Fb : Entity_Id;
1060 begin
1061 Fs := First_Formal (Spec_Id);
1062 Fb := First_Formal (Body_Id);
1063 while Present (Fs) loop
1064 Generate_Reference (Fs, Fb, 'b');
1065 Style.Check_Identifier (Fb, Fs);
1066 Set_Spec_Entity (Fb, Fs);
1067 Next_Formal (Fs);
1068 Next_Formal (Fb);
1069 end loop;
1070 end;
1071 end if;
1073 if Nkind (N) /= N_Subprogram_Body_Stub then
1074 Set_Corresponding_Spec (N, Spec_Id);
1075 Install_Formals (Spec_Id);
1076 Last_Formal := Last_Entity (Spec_Id);
1077 New_Scope (Spec_Id);
1079 -- Make sure that the subprogram is immediately visible. For
1080 -- child units that have no separate spec this is indispensable.
1081 -- Otherwise it is safe albeit redundant.
1083 Set_Is_Immediately_Visible (Spec_Id);
1084 end if;
1086 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
1087 Set_Ekind (Body_Id, E_Subprogram_Body);
1088 Set_Scope (Body_Id, Scope (Spec_Id));
1090 -- Case of subprogram body with no previous spec
1092 else
1093 if Style_Check
1094 and then Comes_From_Source (Body_Id)
1095 and then not Suppress_Style_Checks (Body_Id)
1096 and then not In_Instance
1097 then
1098 Style.Body_With_No_Spec (N);
1099 end if;
1101 New_Overloaded_Entity (Body_Id);
1103 if Nkind (N) /= N_Subprogram_Body_Stub then
1104 Set_Acts_As_Spec (N);
1105 Generate_Definition (Body_Id);
1106 Install_Formals (Body_Id);
1107 New_Scope (Body_Id);
1108 end if;
1109 end if;
1111 -- If this is the proper body of a stub, we must verify that the stub
1112 -- conforms to the body, and to the previous spec if one was present.
1113 -- we know already that the body conforms to that spec. This test is
1114 -- only required for subprograms that come from source.
1116 if Nkind (Parent (N)) = N_Subunit
1117 and then Comes_From_Source (N)
1118 and then not Error_Posted (Body_Id)
1119 then
1120 declare
1121 Conformant : Boolean := False;
1122 Old_Id : Entity_Id :=
1123 Defining_Entity
1124 (Specification (Corresponding_Stub (Parent (N))));
1126 begin
1127 if No (Spec_Id) then
1128 Check_Fully_Conformant (Body_Id, Old_Id);
1130 else
1131 Check_Conformance
1132 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
1134 if not Conformant then
1136 -- The stub was taken to be a new declaration. Indicate
1137 -- that it lacks a body.
1139 Set_Has_Completion (Old_Id, False);
1140 end if;
1141 end if;
1142 end;
1143 end if;
1145 Set_Has_Completion (Body_Id);
1146 Check_Eliminated (Body_Id);
1148 if Nkind (N) = N_Subprogram_Body_Stub then
1149 return;
1151 elsif Present (Spec_Id)
1152 and then Expander_Active
1153 and then Has_Pragma_Inline (Spec_Id)
1154 and then (Front_End_Inlining
1155 or else
1156 (No_Run_Time and then Is_Always_Inlined (Spec_Id)))
1157 then
1158 if Build_Body_To_Inline (N, Spec_Id, Copy_Separate_Tree (N)) then
1159 null;
1160 end if;
1161 end if;
1163 -- Here we have a real body, not a stub. First step is to null out
1164 -- the subprogram body if we have the special case of no run time
1165 -- mode with a predefined unit, and the subprogram is not marked
1166 -- as Inline_Always. The reason is that we should never call such
1167 -- a routine in no run time mode, and it may in general have some
1168 -- statements that we cannot handle in no run time mode.
1170 -- ASIS note: we do a replace here, because we are really NOT going
1171 -- to analyze the original body and declarations at all, so it is
1172 -- useless to keep them around, we really are obliterating the body,
1173 -- basically creating a specialized no run time version on the fly
1174 -- in which the bodies *are* null.
1176 if No_Run_Time
1177 and then Present (Spec_Id)
1178 and then Is_Predefined_File_Name
1179 (Unit_File_Name (Get_Source_Unit (Loc)))
1180 and then not Is_Always_Inlined (Spec_Id)
1181 then
1182 Replace (N,
1183 Make_Subprogram_Body (Loc,
1184 Specification => Specification (N),
1185 Declarations => Empty_List,
1186 Handled_Statement_Sequence =>
1187 Make_Handled_Sequence_Of_Statements (Loc,
1188 Statements => New_List (
1189 Make_Null_Statement (Loc)),
1190 End_Label =>
1191 End_Label (Handled_Statement_Sequence (N)))));
1192 Set_Corresponding_Spec (N, Spec_Id);
1193 Body_Deleted := True;
1194 end if;
1196 -- Now we can go on to analyze the body
1198 HSS := Handled_Statement_Sequence (N);
1199 Set_Actual_Subtypes (N, Current_Scope);
1200 Analyze_Declarations (Declarations (N));
1201 Check_Completion;
1202 Analyze (HSS);
1203 Process_End_Label (HSS, 't');
1204 End_Scope;
1205 Check_Subprogram_Order (N);
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 -- Don't worry about checking for variables that are never modified
1278 -- if the first statement of the body is a raise statement, since
1279 -- we assume this is some kind of stub. We ignore a label generated
1280 -- by the exception stuff for the purpose of this test.
1282 declare
1283 Stm : Node_Id := First (Statements (HSS));
1285 begin
1286 if Nkind (Stm) = N_Label then
1287 Next (Stm);
1288 end if;
1290 if Nkind (Original_Node (Stm)) = N_Raise_Statement then
1291 return;
1292 end if;
1293 end;
1295 -- Check for variables that are never modified
1297 declare
1298 E1, E2 : Entity_Id;
1300 begin
1301 -- If there is a separate spec, then transfer Not_Source_Assigned
1302 -- flags from out parameters to the corresponding entities in the
1303 -- body. The reason we do that is we want to post error flags on
1304 -- the body entities, not the spec entities.
1306 if Present (Spec_Id) then
1307 E1 := First_Entity (Spec_Id);
1309 while Present (E1) loop
1310 if Ekind (E1) = E_Out_Parameter then
1311 E2 := First_Entity (Body_Id);
1313 loop
1314 -- If no matching body entity, then we already had
1315 -- a detected error of some kind, so just forget
1316 -- about worrying about these warnings.
1318 if No (E2) then
1319 return;
1320 end if;
1322 exit when Chars (E1) = Chars (E2);
1323 Next_Entity (E2);
1324 end loop;
1326 Set_Not_Source_Assigned (E2, Not_Source_Assigned (E1));
1327 end if;
1329 Next_Entity (E1);
1330 end loop;
1331 end if;
1333 -- Check references in body unless it was deleted. Note that the
1334 -- check of Body_Deleted here is not just for efficiency, it is
1335 -- necessary to avoid junk warnings on formal parameters.
1337 if not Body_Deleted then
1338 Check_References (Body_Id);
1339 end if;
1340 end;
1341 end Analyze_Subprogram_Body;
1343 ------------------------------------
1344 -- Analyze_Subprogram_Declaration --
1345 ------------------------------------
1347 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
1348 Designator : constant Entity_Id := Analyze_Spec (Specification (N));
1349 Scop : constant Entity_Id := Current_Scope;
1351 -- Start of processing for Analyze_Subprogram_Declaration
1353 begin
1354 Generate_Definition (Designator);
1356 -- Check for RCI unit subprogram declarations against in-lined
1357 -- subprograms and subprograms having access parameter or limited
1358 -- parameter without Read and Write (RM E.2.3(12-13)).
1360 Validate_RCI_Subprogram_Declaration (N);
1362 Trace_Scope
1364 Defining_Entity (N),
1365 " Analyze subprogram spec. ");
1367 if Debug_Flag_C then
1368 Write_Str ("==== Compiling subprogram spec ");
1369 Write_Name (Chars (Designator));
1370 Write_Str (" from ");
1371 Write_Location (Sloc (N));
1372 Write_Eol;
1373 end if;
1375 New_Overloaded_Entity (Designator);
1376 Check_Delayed_Subprogram (Designator);
1377 Set_Suppress_Elaboration_Checks
1378 (Designator, Elaboration_Checks_Suppressed (Designator));
1380 if Scop /= Standard_Standard
1381 and then not Is_Child_Unit (Designator)
1382 then
1383 Set_Is_Pure (Designator,
1384 Is_Pure (Scop) and then Is_Library_Level_Entity (Designator));
1385 Set_Is_Remote_Call_Interface (
1386 Designator, Is_Remote_Call_Interface (Scop));
1387 Set_Is_Remote_Types (Designator, Is_Remote_Types (Scop));
1389 else
1390 -- For a compilation unit, check for library-unit pragmas.
1392 New_Scope (Designator);
1393 Set_Categorization_From_Pragmas (N);
1394 Validate_Categorization_Dependency (N, Designator);
1395 Pop_Scope;
1396 end if;
1398 -- For a compilation unit, set body required. This flag will only be
1399 -- reset if a valid Import or Interface pragma is processed later on.
1401 if Nkind (Parent (N)) = N_Compilation_Unit then
1402 Set_Body_Required (Parent (N), True);
1403 end if;
1405 Check_Eliminated (Designator);
1406 end Analyze_Subprogram_Declaration;
1408 --------------------------
1409 -- Build_Body_To_Inline --
1410 --------------------------
1412 function Build_Body_To_Inline
1413 (N : Node_Id;
1414 Subp : Entity_Id;
1415 Orig_Body : Node_Id) return Boolean
1417 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
1418 Original_Body : Node_Id;
1419 Body_To_Analyze : Node_Id;
1420 Max_Size : constant := 10;
1421 Stat_Count : Integer := 0;
1423 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
1424 -- Check for declarations that make inlining not worthwhile.
1426 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
1427 -- Check for statements that make inlining not worthwhile: any
1428 -- tasking statement, nested at any level. Keep track of total
1429 -- number of elementary statements, as a measure of acceptable size.
1431 function Has_Pending_Instantiation return Boolean;
1432 -- If some enclosing body contains instantiations that appear before
1433 -- the corresponding generic body, the enclosing body has a freeze node
1434 -- so that it can be elaborated after the generic itself. This might
1435 -- conflict with subsequent inlinings, so that it is unsafe to try to
1436 -- inline in such a case.
1438 -------------------
1439 -- Cannot_Inline --
1440 -------------------
1442 procedure Cannot_Inline (Msg : String; N : Node_Id);
1443 -- If subprogram has pragma Inline_Always, it is an error if
1444 -- it cannot be inlined. Otherwise, emit a warning.
1446 procedure Cannot_Inline (Msg : String; N : Node_Id) is
1447 begin
1448 if Is_Always_Inlined (Subp) then
1449 Error_Msg_NE (Msg (1 .. Msg'Length - 1), N, Subp);
1451 elsif Ineffective_Inline_Warnings then
1452 Error_Msg_NE (Msg, N, Subp);
1453 end if;
1454 end Cannot_Inline;
1456 ------------------------------
1457 -- Has_Excluded_Declaration --
1458 ------------------------------
1460 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
1461 D : Node_Id;
1463 begin
1464 D := First (Decls);
1466 while Present (D) loop
1467 if Nkind (D) = N_Function_Instantiation
1468 or else Nkind (D) = N_Protected_Type_Declaration
1469 or else Nkind (D) = N_Package_Declaration
1470 or else Nkind (D) = N_Package_Instantiation
1471 or else Nkind (D) = N_Subprogram_Body
1472 or else Nkind (D) = N_Procedure_Instantiation
1473 or else Nkind (D) = N_Task_Type_Declaration
1474 then
1475 Cannot_Inline
1476 ("\declaration prevents front-end inlining of&?", D);
1477 return True;
1478 end if;
1480 Next (D);
1481 end loop;
1483 return False;
1485 end Has_Excluded_Declaration;
1487 ----------------------------
1488 -- Has_Excluded_Statement --
1489 ----------------------------
1491 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
1492 S : Node_Id;
1493 E : Node_Id;
1495 begin
1496 S := First (Stats);
1498 while Present (S) loop
1499 Stat_Count := Stat_Count + 1;
1501 if Nkind (S) = N_Abort_Statement
1502 or else Nkind (S) = N_Asynchronous_Select
1503 or else Nkind (S) = N_Conditional_Entry_Call
1504 or else Nkind (S) = N_Delay_Relative_Statement
1505 or else Nkind (S) = N_Delay_Until_Statement
1506 or else Nkind (S) = N_Selective_Accept
1507 or else Nkind (S) = N_Timed_Entry_Call
1508 then
1509 Cannot_Inline
1510 ("\statement prevents front-end inlining of&?", S);
1511 return True;
1513 elsif Nkind (S) = N_Block_Statement then
1514 if Present (Declarations (S))
1515 and then Has_Excluded_Declaration (Declarations (S))
1516 then
1517 return True;
1519 elsif Present (Handled_Statement_Sequence (S))
1520 and then
1521 (Present
1522 (Exception_Handlers (Handled_Statement_Sequence (S)))
1523 or else
1524 Has_Excluded_Statement
1525 (Statements (Handled_Statement_Sequence (S))))
1526 then
1527 return True;
1528 end if;
1530 elsif Nkind (S) = N_Case_Statement then
1531 E := First (Alternatives (S));
1533 while Present (E) loop
1534 if Has_Excluded_Statement (Statements (E)) then
1535 return True;
1536 end if;
1538 Next (E);
1539 end loop;
1541 elsif Nkind (S) = N_If_Statement then
1542 if Has_Excluded_Statement (Then_Statements (S)) then
1543 return True;
1544 end if;
1546 if Present (Elsif_Parts (S)) then
1547 E := First (Elsif_Parts (S));
1549 while Present (E) loop
1550 if Has_Excluded_Statement (Then_Statements (E)) then
1551 return True;
1552 end if;
1553 Next (E);
1554 end loop;
1555 end if;
1557 if Present (Else_Statements (S))
1558 and then Has_Excluded_Statement (Else_Statements (S))
1559 then
1560 return True;
1561 end if;
1563 elsif Nkind (S) = N_Loop_Statement
1564 and then Has_Excluded_Statement (Statements (S))
1565 then
1566 return True;
1567 end if;
1569 Next (S);
1570 end loop;
1572 return False;
1573 end Has_Excluded_Statement;
1575 -------------------------------
1576 -- Has_Pending_Instantiation --
1577 -------------------------------
1579 function Has_Pending_Instantiation return Boolean is
1580 S : Entity_Id := Current_Scope;
1582 begin
1583 while Present (S) loop
1584 if Is_Compilation_Unit (S)
1585 or else Is_Child_Unit (S)
1586 then
1587 return False;
1588 elsif Ekind (S) = E_Package
1589 and then Has_Forward_Instantiation (S)
1590 then
1591 return True;
1592 end if;
1594 S := Scope (S);
1595 end loop;
1597 return False;
1598 end Has_Pending_Instantiation;
1600 -- Start of processing for Build_Body_To_Inline
1602 begin
1603 if Nkind (Decl) = N_Subprogram_Declaration
1604 and then Present (Body_To_Inline (Decl))
1605 then
1606 return True; -- Done already.
1608 -- Functions that return unconstrained composite types will require
1609 -- secondary stack handling, and cannot currently be inlined.
1611 elsif Ekind (Subp) = E_Function
1612 and then not Is_Scalar_Type (Etype (Subp))
1613 and then not Is_Access_Type (Etype (Subp))
1614 and then not Is_Constrained (Etype (Subp))
1615 then
1616 Cannot_Inline
1617 ("unconstrained return type prevents front-end inlining of&?", N);
1618 return False;
1619 end if;
1621 -- We need to capture references to the formals in order to substitute
1622 -- the actuals at the point of inlining, i.e. instantiation. To treat
1623 -- the formals as globals to the body to inline, we nest it within
1624 -- a dummy parameterless subprogram, declared within the real one.
1626 Original_Body := Orig_Body;
1628 -- Within an instance, the current tree is already the result of
1629 -- a generic copy, and not what we need for subsequent inlining.
1630 -- We create the required body by doing an instantiating copy, to
1631 -- obtain the proper partially analyzed tree.
1633 if In_Instance then
1634 if No (Generic_Parent (Specification (N))) then
1635 return False;
1637 elsif Is_Child_Unit (Scope (Current_Scope)) then
1638 return False;
1640 elsif Scope (Current_Scope) = Cunit_Entity (Main_Unit) then
1642 -- compiling an instantiation. There is no point in generating
1643 -- bodies to inline, because they will not be used.
1645 return False;
1647 else
1648 Body_To_Analyze :=
1649 Copy_Generic_Node
1650 (Generic_Parent (Specification (N)), Empty,
1651 Instantiating => True);
1652 end if;
1653 else
1654 Body_To_Analyze :=
1655 Copy_Generic_Node (Original_Body, Empty,
1656 Instantiating => False);
1657 end if;
1659 Set_Parameter_Specifications (Specification (Original_Body), No_List);
1660 Set_Defining_Unit_Name (Specification (Original_Body),
1661 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('S')));
1662 Set_Corresponding_Spec (Original_Body, Empty);
1664 if Ekind (Subp) = E_Function then
1665 Set_Subtype_Mark (Specification (Original_Body),
1666 New_Occurrence_Of (Etype (Subp), Sloc (N)));
1667 end if;
1669 if Present (Declarations (Orig_Body))
1670 and then Has_Excluded_Declaration (Declarations (Orig_Body))
1671 then
1672 return False;
1673 end if;
1675 if Present (Handled_Statement_Sequence (N)) then
1677 (Present (Exception_Handlers (Handled_Statement_Sequence (N))))
1678 then
1679 Cannot_Inline ("handler prevents front-end inlining of&?",
1680 First (Exception_Handlers (Handled_Statement_Sequence (N))));
1681 return False;
1682 elsif
1683 Has_Excluded_Statement
1684 (Statements (Handled_Statement_Sequence (N)))
1685 then
1686 return False;
1687 end if;
1688 end if;
1690 -- We do not inline a subprogram that is too large, unless it is
1691 -- marked Inline_Always. This pragma does not suppress the other
1692 -- checks on inlining (forbidden declarations, handlers, etc).
1694 if Stat_Count > Max_Size
1695 and then not Is_Always_Inlined (Subp)
1696 then
1697 Cannot_Inline ("body is too large for front-end inlining of&?", N);
1698 return False;
1699 end if;
1701 if Has_Pending_Instantiation then
1702 Cannot_Inline
1703 ("cannot inline& because of forward instance within enclosing body",
1705 return False;
1706 end if;
1708 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
1710 -- Set return type of function, which is also global and does not need
1711 -- to be resolved.
1713 if Ekind (Subp) = E_Function then
1714 Set_Subtype_Mark (Specification (Body_To_Analyze),
1715 New_Occurrence_Of (Etype (Subp), Sloc (N)));
1716 end if;
1718 if No (Declarations (N)) then
1719 Set_Declarations (N, New_List (Body_To_Analyze));
1720 else
1721 Append (Body_To_Analyze, Declarations (N));
1722 end if;
1724 Expander_Mode_Save_And_Set (False);
1726 Analyze (Body_To_Analyze);
1727 New_Scope (Defining_Entity (Body_To_Analyze));
1728 Save_Global_References (Original_Body);
1729 End_Scope;
1730 Remove (Body_To_Analyze);
1732 Expander_Mode_Restore;
1733 Set_Body_To_Inline (Decl, Original_Body);
1734 Set_Is_Inlined (Subp);
1735 return True;
1737 end Build_Body_To_Inline;
1739 -----------------------
1740 -- Check_Conformance --
1741 -----------------------
1743 procedure Check_Conformance
1744 (New_Id : Entity_Id;
1745 Old_Id : Entity_Id;
1746 Ctype : Conformance_Type;
1747 Errmsg : Boolean;
1748 Conforms : out Boolean;
1749 Err_Loc : Node_Id := Empty;
1750 Get_Inst : Boolean := False)
1752 Old_Type : constant Entity_Id := Etype (Old_Id);
1753 New_Type : constant Entity_Id := Etype (New_Id);
1754 Old_Formal : Entity_Id;
1755 New_Formal : Entity_Id;
1757 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
1758 -- Post error message for conformance error on given node.
1759 -- Two messages are output. The first points to the previous
1760 -- declaration with a general "no conformance" message.
1761 -- The second is the detailed reason, supplied as Msg. The
1762 -- parameter N provide information for a possible & insertion
1763 -- in the message, and also provides the location for posting
1764 -- the message in the absence of a specified Err_Loc location.
1766 -----------------------
1767 -- Conformance_Error --
1768 -----------------------
1770 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
1771 Enode : Node_Id;
1773 begin
1774 Conforms := False;
1776 if Errmsg then
1777 if No (Err_Loc) then
1778 Enode := N;
1779 else
1780 Enode := Err_Loc;
1781 end if;
1783 Error_Msg_Sloc := Sloc (Old_Id);
1785 case Ctype is
1786 when Type_Conformant =>
1787 Error_Msg_N
1788 ("not type conformant with declaration#!", Enode);
1790 when Mode_Conformant =>
1791 Error_Msg_N
1792 ("not mode conformant with declaration#!", Enode);
1794 when Subtype_Conformant =>
1795 Error_Msg_N
1796 ("not subtype conformant with declaration#!", Enode);
1798 when Fully_Conformant =>
1799 Error_Msg_N
1800 ("not fully conformant with declaration#!", Enode);
1801 end case;
1803 Error_Msg_NE (Msg, Enode, N);
1804 end if;
1805 end Conformance_Error;
1807 -- Start of processing for Check_Conformance
1809 begin
1810 Conforms := True;
1812 -- We need a special case for operators, since they don't
1813 -- appear explicitly.
1815 if Ctype = Type_Conformant then
1816 if Ekind (New_Id) = E_Operator
1817 and then Operator_Matches_Spec (New_Id, Old_Id)
1818 then
1819 return;
1820 end if;
1821 end if;
1823 -- If both are functions/operators, check return types conform
1825 if Old_Type /= Standard_Void_Type
1826 and then New_Type /= Standard_Void_Type
1827 then
1828 if not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
1829 Conformance_Error ("return type does not match!", New_Id);
1830 return;
1831 end if;
1833 -- If either is a function/operator and the other isn't, error
1835 elsif Old_Type /= Standard_Void_Type
1836 or else New_Type /= Standard_Void_Type
1837 then
1838 Conformance_Error ("functions can only match functions!", New_Id);
1839 return;
1840 end if;
1842 -- In subtype conformant case, conventions must match (RM 6.3.1(16))
1843 -- If this is a renaming as body, refine error message to indicate that
1844 -- the conflict is with the original declaration. If the entity is not
1845 -- frozen, the conventions don't have to match, the one of the renamed
1846 -- entity is inherited.
1848 if Ctype >= Subtype_Conformant then
1850 if Convention (Old_Id) /= Convention (New_Id) then
1852 if not Is_Frozen (New_Id) then
1853 null;
1855 elsif Present (Err_Loc)
1856 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
1857 and then Present (Corresponding_Spec (Err_Loc))
1858 then
1859 Error_Msg_Name_1 := Chars (New_Id);
1860 Error_Msg_Name_2 :=
1861 Name_Ada + Convention_Id'Pos (Convention (New_Id));
1863 Conformance_Error ("prior declaration for% has convention %!");
1865 else
1866 Conformance_Error ("calling conventions do not match!");
1867 end if;
1869 return;
1871 elsif Is_Formal_Subprogram (Old_Id)
1872 or else Is_Formal_Subprogram (New_Id)
1873 then
1874 Conformance_Error ("formal subprograms not allowed!");
1875 return;
1876 end if;
1877 end if;
1879 -- Deal with parameters
1881 -- Note: we use the entity information, rather than going directly
1882 -- to the specification in the tree. This is not only simpler, but
1883 -- absolutely necessary for some cases of conformance tests between
1884 -- operators, where the declaration tree simply does not exist!
1886 Old_Formal := First_Formal (Old_Id);
1887 New_Formal := First_Formal (New_Id);
1889 while Present (Old_Formal) and then Present (New_Formal) loop
1891 -- Types must always match. In the visible part of an instance,
1892 -- usual overloading rules for dispatching operations apply, and
1893 -- we check base types (not the actual subtypes).
1895 if In_Instance_Visible_Part
1896 and then Is_Dispatching_Operation (New_Id)
1897 then
1898 if not Conforming_Types
1899 (Base_Type (Etype (Old_Formal)),
1900 Base_Type (Etype (New_Formal)), Ctype, Get_Inst)
1901 then
1902 Conformance_Error ("type of & does not match!", New_Formal);
1903 return;
1904 end if;
1906 elsif not Conforming_Types
1907 (Etype (Old_Formal), Etype (New_Formal), Ctype, Get_Inst)
1908 then
1909 Conformance_Error ("type of & does not match!", New_Formal);
1910 return;
1911 end if;
1913 -- For mode conformance, mode must match
1915 if Ctype >= Mode_Conformant
1916 and then Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal)
1917 then
1918 Conformance_Error ("mode of & does not match!", New_Formal);
1919 return;
1920 end if;
1922 -- Full conformance checks
1924 if Ctype = Fully_Conformant then
1926 -- Names must match
1928 if Chars (Old_Formal) /= Chars (New_Formal) then
1929 Conformance_Error ("name & does not match!", New_Formal);
1930 return;
1932 -- And default expressions for in parameters
1934 elsif Parameter_Mode (Old_Formal) = E_In_Parameter then
1935 declare
1936 NewD : constant Boolean :=
1937 Present (Default_Value (New_Formal));
1938 OldD : constant Boolean :=
1939 Present (Default_Value (Old_Formal));
1940 begin
1941 if NewD or OldD then
1943 -- The old default value has been analyzed and expanded,
1944 -- because the current full declaration will have frozen
1945 -- everything before. The new default values have not
1946 -- been expanded, so expand now to check conformance.
1948 if NewD then
1949 New_Scope (New_Id);
1950 Analyze_Default_Expression
1951 (Default_Value (New_Formal), Etype (New_Formal));
1952 End_Scope;
1953 end if;
1955 if not (NewD and OldD)
1956 or else not Fully_Conformant_Expressions
1957 (Default_Value (Old_Formal),
1958 Default_Value (New_Formal))
1959 then
1960 Conformance_Error
1961 ("default expression for & does not match!",
1962 New_Formal);
1963 return;
1964 end if;
1965 end if;
1966 end;
1967 end if;
1968 end if;
1970 -- A couple of special checks for Ada 83 mode. These checks are
1971 -- skipped if either entity is an operator in package Standard.
1972 -- or if either old or new instance is not from the source program.
1974 if Ada_83
1975 and then Sloc (Old_Id) > Standard_Location
1976 and then Sloc (New_Id) > Standard_Location
1977 and then Comes_From_Source (Old_Id)
1978 and then Comes_From_Source (New_Id)
1979 then
1980 declare
1981 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
1982 New_Param : constant Node_Id := Declaration_Node (New_Formal);
1984 begin
1985 -- Explicit IN must be present or absent in both cases. This
1986 -- test is required only in the full conformance case.
1988 if In_Present (Old_Param) /= In_Present (New_Param)
1989 and then Ctype = Fully_Conformant
1990 then
1991 Conformance_Error
1992 ("(Ada 83) IN must appear in both declarations",
1993 New_Formal);
1994 return;
1995 end if;
1997 -- Grouping (use of comma in param lists) must be the same
1998 -- This is where we catch a misconformance like:
2000 -- A,B : Integer
2001 -- A : Integer; B : Integer
2003 -- which are represented identically in the tree except
2004 -- for the setting of the flags More_Ids and Prev_Ids.
2006 if More_Ids (Old_Param) /= More_Ids (New_Param)
2007 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
2008 then
2009 Conformance_Error
2010 ("grouping of & does not match!", New_Formal);
2011 return;
2012 end if;
2013 end;
2014 end if;
2016 Next_Formal (Old_Formal);
2017 Next_Formal (New_Formal);
2018 end loop;
2020 if Present (Old_Formal) then
2021 Conformance_Error ("too few parameters!");
2022 return;
2024 elsif Present (New_Formal) then
2025 Conformance_Error ("too many parameters!", New_Formal);
2026 return;
2027 end if;
2029 end Check_Conformance;
2031 ------------------------------
2032 -- Check_Delayed_Subprogram --
2033 ------------------------------
2035 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
2036 F : Entity_Id;
2038 procedure Possible_Freeze (T : Entity_Id);
2039 -- T is the type of either a formal parameter or of the return type.
2040 -- If T is not yet frozen and needs a delayed freeze, then the
2041 -- subprogram itself must be delayed.
2043 procedure Possible_Freeze (T : Entity_Id) is
2044 begin
2045 if Has_Delayed_Freeze (T)
2046 and then not Is_Frozen (T)
2047 then
2048 Set_Has_Delayed_Freeze (Designator);
2050 elsif Is_Access_Type (T)
2051 and then Has_Delayed_Freeze (Designated_Type (T))
2052 and then not Is_Frozen (Designated_Type (T))
2053 then
2054 Set_Has_Delayed_Freeze (Designator);
2055 end if;
2056 end Possible_Freeze;
2058 -- Start of processing for Check_Delayed_Subprogram
2060 begin
2061 -- Never need to freeze abstract subprogram
2063 if Is_Abstract (Designator) then
2064 null;
2065 else
2066 -- Need delayed freeze if return type itself needs a delayed
2067 -- freeze and is not yet frozen.
2069 Possible_Freeze (Etype (Designator));
2070 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
2072 -- Need delayed freeze if any of the formal types themselves need
2073 -- a delayed freeze and are not yet frozen.
2075 F := First_Formal (Designator);
2076 while Present (F) loop
2077 Possible_Freeze (Etype (F));
2078 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
2079 Next_Formal (F);
2080 end loop;
2081 end if;
2083 -- Mark functions that return by reference. Note that it cannot be
2084 -- done for delayed_freeze subprograms because the underlying
2085 -- returned type may not be known yet (for private types)
2087 if not Has_Delayed_Freeze (Designator)
2088 and then Expander_Active
2089 then
2090 declare
2091 Typ : constant Entity_Id := Etype (Designator);
2092 Utyp : constant Entity_Id := Underlying_Type (Typ);
2094 begin
2095 if Is_Return_By_Reference_Type (Typ) then
2096 Set_Returns_By_Ref (Designator);
2098 elsif Present (Utyp) and then Controlled_Type (Utyp) then
2099 Set_Returns_By_Ref (Designator);
2100 end if;
2101 end;
2102 end if;
2103 end Check_Delayed_Subprogram;
2105 ------------------------------------
2106 -- Check_Discriminant_Conformance --
2107 ------------------------------------
2109 procedure Check_Discriminant_Conformance
2110 (N : Node_Id;
2111 Prev : Entity_Id;
2112 Prev_Loc : Node_Id)
2114 Old_Discr : Entity_Id := First_Discriminant (Prev);
2115 New_Discr : Node_Id := First (Discriminant_Specifications (N));
2116 New_Discr_Id : Entity_Id;
2117 New_Discr_Type : Entity_Id;
2119 procedure Conformance_Error (Msg : String; N : Node_Id);
2120 -- Post error message for conformance error on given node.
2121 -- Two messages are output. The first points to the previous
2122 -- declaration with a general "no conformance" message.
2123 -- The second is the detailed reason, supplied as Msg. The
2124 -- parameter N provide information for a possible & insertion
2125 -- in the message.
2127 -----------------------
2128 -- Conformance_Error --
2129 -----------------------
2131 procedure Conformance_Error (Msg : String; N : Node_Id) is
2132 begin
2133 Error_Msg_Sloc := Sloc (Prev_Loc);
2134 Error_Msg_N ("not fully conformant with declaration#!", N);
2135 Error_Msg_NE (Msg, N, N);
2136 end Conformance_Error;
2138 -- Start of processing for Check_Discriminant_Conformance
2140 begin
2141 while Present (Old_Discr) and then Present (New_Discr) loop
2143 New_Discr_Id := Defining_Identifier (New_Discr);
2145 -- The subtype mark of the discriminant on the full type
2146 -- has not been analyzed so we do it here. For an access
2147 -- discriminant a new type is created.
2149 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
2150 New_Discr_Type :=
2151 Access_Definition (N, Discriminant_Type (New_Discr));
2153 else
2154 Analyze (Discriminant_Type (New_Discr));
2155 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
2156 end if;
2158 if not Conforming_Types
2159 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
2160 then
2161 Conformance_Error ("type of & does not match!", New_Discr_Id);
2162 return;
2163 end if;
2165 -- Names must match
2167 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
2168 Conformance_Error ("name & does not match!", New_Discr_Id);
2169 return;
2170 end if;
2172 -- Default expressions must match
2174 declare
2175 NewD : constant Boolean :=
2176 Present (Expression (New_Discr));
2177 OldD : constant Boolean :=
2178 Present (Expression (Parent (Old_Discr)));
2180 begin
2181 if NewD or OldD then
2183 -- The old default value has been analyzed and expanded,
2184 -- because the current full declaration will have frozen
2185 -- everything before. The new default values have not
2186 -- been expanded, so expand now to check conformance.
2188 if NewD then
2189 Analyze_Default_Expression
2190 (Expression (New_Discr), New_Discr_Type);
2191 end if;
2193 if not (NewD and OldD)
2194 or else not Fully_Conformant_Expressions
2195 (Expression (Parent (Old_Discr)),
2196 Expression (New_Discr))
2198 then
2199 Conformance_Error
2200 ("default expression for & does not match!",
2201 New_Discr_Id);
2202 return;
2203 end if;
2204 end if;
2205 end;
2207 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
2209 if Ada_83 then
2210 declare
2211 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
2213 begin
2214 -- Grouping (use of comma in param lists) must be the same
2215 -- This is where we catch a misconformance like:
2217 -- A,B : Integer
2218 -- A : Integer; B : Integer
2220 -- which are represented identically in the tree except
2221 -- for the setting of the flags More_Ids and Prev_Ids.
2223 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
2224 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
2225 then
2226 Conformance_Error
2227 ("grouping of & does not match!", New_Discr_Id);
2228 return;
2229 end if;
2230 end;
2231 end if;
2233 Next_Discriminant (Old_Discr);
2234 Next (New_Discr);
2235 end loop;
2237 if Present (Old_Discr) then
2238 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
2239 return;
2241 elsif Present (New_Discr) then
2242 Conformance_Error
2243 ("too many discriminants!", Defining_Identifier (New_Discr));
2244 return;
2245 end if;
2246 end Check_Discriminant_Conformance;
2248 ----------------------------
2249 -- Check_Fully_Conformant --
2250 ----------------------------
2252 procedure Check_Fully_Conformant
2253 (New_Id : Entity_Id;
2254 Old_Id : Entity_Id;
2255 Err_Loc : Node_Id := Empty)
2257 Result : Boolean;
2259 begin
2260 Check_Conformance
2261 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
2262 end Check_Fully_Conformant;
2264 ---------------------------
2265 -- Check_Mode_Conformant --
2266 ---------------------------
2268 procedure Check_Mode_Conformant
2269 (New_Id : Entity_Id;
2270 Old_Id : Entity_Id;
2271 Err_Loc : Node_Id := Empty;
2272 Get_Inst : Boolean := False)
2274 Result : Boolean;
2276 begin
2277 Check_Conformance
2278 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
2279 end Check_Mode_Conformant;
2281 -------------------
2282 -- Check_Returns --
2283 -------------------
2285 procedure Check_Returns
2286 (HSS : Node_Id;
2287 Mode : Character;
2288 Err : out Boolean)
2290 Handler : Node_Id;
2292 procedure Check_Statement_Sequence (L : List_Id);
2293 -- Internal recursive procedure to check a list of statements for proper
2294 -- termination by a return statement (or a transfer of control or a
2295 -- compound statement that is itself internally properly terminated).
2297 ------------------------------
2298 -- Check_Statement_Sequence --
2299 ------------------------------
2301 procedure Check_Statement_Sequence (L : List_Id) is
2302 Last_Stm : Node_Id;
2303 Kind : Node_Kind;
2305 Raise_Exception_Call : Boolean;
2306 -- Set True if statement sequence terminated by Raise_Exception call
2307 -- or a Reraise_Occurrence call.
2309 begin
2310 Raise_Exception_Call := False;
2312 -- Get last real statement
2314 Last_Stm := Last (L);
2316 -- Don't count pragmas
2318 while Nkind (Last_Stm) = N_Pragma
2320 -- Don't count call to SS_Release (can happen after Raise_Exception)
2322 or else
2323 (Nkind (Last_Stm) = N_Procedure_Call_Statement
2324 and then
2325 Nkind (Name (Last_Stm)) = N_Identifier
2326 and then
2327 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
2329 -- Don't count exception junk
2331 or else
2332 ((Nkind (Last_Stm) = N_Goto_Statement
2333 or else Nkind (Last_Stm) = N_Label
2334 or else Nkind (Last_Stm) = N_Object_Declaration)
2335 and then Exception_Junk (Last_Stm))
2336 loop
2337 Prev (Last_Stm);
2338 end loop;
2340 -- Here we have the "real" last statement
2342 Kind := Nkind (Last_Stm);
2344 -- Transfer of control, OK. Note that in the No_Return procedure
2345 -- case, we already diagnosed any explicit return statements, so
2346 -- we can treat them as OK in this context.
2348 if Is_Transfer (Last_Stm) then
2349 return;
2351 -- Check cases of explicit non-indirect procedure calls
2353 elsif Kind = N_Procedure_Call_Statement
2354 and then Is_Entity_Name (Name (Last_Stm))
2355 then
2356 -- Check call to Raise_Exception procedure which is treated
2357 -- specially, as is a call to Reraise_Occurrence.
2359 -- We suppress the warning in these cases since it is likely that
2360 -- the programmer really does not expect to deal with the case
2361 -- of Null_Occurrence, and thus would find a warning about a
2362 -- missing return curious, and raising Program_Error does not
2363 -- seem such a bad behavior if this does occur.
2365 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
2366 or else
2367 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
2368 then
2369 Raise_Exception_Call := True;
2371 -- For Raise_Exception call, test first argument, if it is
2372 -- an attribute reference for a 'Identity call, then we know
2373 -- that the call cannot possibly return.
2375 declare
2376 Arg : constant Node_Id :=
2377 Original_Node (First_Actual (Last_Stm));
2379 begin
2380 if Nkind (Arg) = N_Attribute_Reference
2381 and then Attribute_Name (Arg) = Name_Identity
2382 then
2383 return;
2384 end if;
2385 end;
2386 end if;
2388 -- If statement, need to look inside if there is an else and check
2389 -- each constituent statement sequence for proper termination.
2391 elsif Kind = N_If_Statement
2392 and then Present (Else_Statements (Last_Stm))
2393 then
2394 Check_Statement_Sequence (Then_Statements (Last_Stm));
2395 Check_Statement_Sequence (Else_Statements (Last_Stm));
2397 if Present (Elsif_Parts (Last_Stm)) then
2398 declare
2399 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
2401 begin
2402 while Present (Elsif_Part) loop
2403 Check_Statement_Sequence (Then_Statements (Elsif_Part));
2404 Next (Elsif_Part);
2405 end loop;
2406 end;
2407 end if;
2409 return;
2411 -- Case statement, check each case for proper termination
2413 elsif Kind = N_Case_Statement then
2414 declare
2415 Case_Alt : Node_Id;
2417 begin
2418 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
2419 while Present (Case_Alt) loop
2420 Check_Statement_Sequence (Statements (Case_Alt));
2421 Next_Non_Pragma (Case_Alt);
2422 end loop;
2423 end;
2425 return;
2427 -- Block statement, check its handled sequence of statements
2429 elsif Kind = N_Block_Statement then
2430 declare
2431 Err1 : Boolean;
2433 begin
2434 Check_Returns
2435 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
2437 if Err1 then
2438 Err := True;
2439 end if;
2441 return;
2442 end;
2444 -- Loop statement. If there is an iteration scheme, we can definitely
2445 -- fall out of the loop. Similarly if there is an exit statement, we
2446 -- can fall out. In either case we need a following return.
2448 elsif Kind = N_Loop_Statement then
2449 if Present (Iteration_Scheme (Last_Stm))
2450 or else Has_Exit (Entity (Identifier (Last_Stm)))
2451 then
2452 null;
2454 -- A loop with no exit statement or iteration scheme if either
2455 -- an inifite loop, or it has some other exit (raise/return).
2456 -- In either case, no warning is required.
2458 else
2459 return;
2460 end if;
2462 -- Timed entry call, check entry call and delay alternatives
2464 -- Note: in expanded code, the timed entry call has been converted
2465 -- to a set of expanded statements on which the check will work
2466 -- correctly in any case.
2468 elsif Kind = N_Timed_Entry_Call then
2469 declare
2470 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
2471 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
2473 begin
2474 -- If statement sequence of entry call alternative is missing,
2475 -- then we can definitely fall through, and we post the error
2476 -- message on the entry call alternative itself.
2478 if No (Statements (ECA)) then
2479 Last_Stm := ECA;
2481 -- If statement sequence of delay alternative is missing, then
2482 -- we can definitely fall through, and we post the error
2483 -- message on the delay alternative itself.
2485 -- Note: if both ECA and DCA are missing the return, then we
2486 -- post only one message, should be enough to fix the bugs.
2487 -- If not we will get a message next time on the DCA when the
2488 -- ECA is fixed!
2490 elsif No (Statements (DCA)) then
2491 Last_Stm := DCA;
2493 -- Else check both statement sequences
2495 else
2496 Check_Statement_Sequence (Statements (ECA));
2497 Check_Statement_Sequence (Statements (DCA));
2498 return;
2499 end if;
2500 end;
2502 -- Conditional entry call, check entry call and else part
2504 -- Note: in expanded code, the conditional entry call has been
2505 -- converted to a set of expanded statements on which the check
2506 -- will work correctly in any case.
2508 elsif Kind = N_Conditional_Entry_Call then
2509 declare
2510 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
2512 begin
2513 -- If statement sequence of entry call alternative is missing,
2514 -- then we can definitely fall through, and we post the error
2515 -- message on the entry call alternative itself.
2517 if No (Statements (ECA)) then
2518 Last_Stm := ECA;
2520 -- Else check statement sequence and else part
2522 else
2523 Check_Statement_Sequence (Statements (ECA));
2524 Check_Statement_Sequence (Else_Statements (Last_Stm));
2525 return;
2526 end if;
2527 end;
2528 end if;
2530 -- If we fall through, issue appropriate message
2532 if Mode = 'F' then
2534 if not Raise_Exception_Call then
2535 Error_Msg_N
2536 ("?RETURN statement missing following this statement!",
2537 Last_Stm);
2538 Error_Msg_N
2539 ("\?Program_Error may be raised at run time",
2540 Last_Stm);
2541 end if;
2543 -- Note: we set Err even though we have not issued a warning
2544 -- because we still have a case of a missing return. This is
2545 -- an extremely marginal case, probably will never be noticed
2546 -- but we might as well get it right.
2548 Err := True;
2550 else
2551 Error_Msg_N
2552 ("implied return after this statement not allowed (No_Return)",
2553 Last_Stm);
2554 end if;
2555 end Check_Statement_Sequence;
2557 -- Start of processing for Check_Returns
2559 begin
2560 Err := False;
2561 Check_Statement_Sequence (Statements (HSS));
2563 if Present (Exception_Handlers (HSS)) then
2564 Handler := First_Non_Pragma (Exception_Handlers (HSS));
2565 while Present (Handler) loop
2566 Check_Statement_Sequence (Statements (Handler));
2567 Next_Non_Pragma (Handler);
2568 end loop;
2569 end if;
2570 end Check_Returns;
2572 ----------------------------
2573 -- Check_Subprogram_Order --
2574 ----------------------------
2576 procedure Check_Subprogram_Order (N : Node_Id) is
2578 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
2579 -- This is used to check if S1 > S2 in the sense required by this
2580 -- test, for example nameab < namec, but name2 < name10.
2582 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
2583 L1, L2 : Positive;
2584 N1, N2 : Natural;
2586 begin
2587 -- Remove trailing numeric parts
2589 L1 := S1'Last;
2590 while S1 (L1) in '0' .. '9' loop
2591 L1 := L1 - 1;
2592 end loop;
2594 L2 := S2'Last;
2595 while S2 (L2) in '0' .. '9' loop
2596 L2 := L2 - 1;
2597 end loop;
2599 -- If non-numeric parts non-equal, that's decisive
2601 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
2602 return False;
2604 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
2605 return True;
2607 -- If non-numeric parts equal, compare suffixed numeric parts. Note
2608 -- that a missing suffix is treated as numeric zero in this test.
2610 else
2611 N1 := 0;
2612 while L1 < S1'Last loop
2613 L1 := L1 + 1;
2614 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
2615 end loop;
2617 N2 := 0;
2618 while L2 < S2'Last loop
2619 L2 := L2 + 1;
2620 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
2621 end loop;
2623 return N1 > N2;
2624 end if;
2625 end Subprogram_Name_Greater;
2627 -- Start of processing for Check_Subprogram_Order
2629 begin
2630 -- Check body in alpha order if this is option
2632 if Style_Check_Subprogram_Order
2633 and then Nkind (N) = N_Subprogram_Body
2634 and then Comes_From_Source (N)
2635 and then In_Extended_Main_Source_Unit (N)
2636 then
2637 declare
2638 LSN : String_Ptr
2639 renames Scope_Stack.Table
2640 (Scope_Stack.Last).Last_Subprogram_Name;
2642 Body_Id : constant Entity_Id :=
2643 Defining_Entity (Specification (N));
2645 begin
2646 Get_Decoded_Name_String (Chars (Body_Id));
2648 if LSN /= null then
2649 if Subprogram_Name_Greater
2650 (LSN.all, Name_Buffer (1 .. Name_Len))
2651 then
2652 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
2653 end if;
2655 Free (LSN);
2656 end if;
2658 LSN := new String'(Name_Buffer (1 .. Name_Len));
2659 end;
2660 end if;
2661 end Check_Subprogram_Order;
2663 ------------------------------
2664 -- Check_Subtype_Conformant --
2665 ------------------------------
2667 procedure Check_Subtype_Conformant
2668 (New_Id : Entity_Id;
2669 Old_Id : Entity_Id;
2670 Err_Loc : Node_Id := Empty)
2672 Result : Boolean;
2674 begin
2675 Check_Conformance
2676 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc);
2677 end Check_Subtype_Conformant;
2679 ---------------------------
2680 -- Check_Type_Conformant --
2681 ---------------------------
2683 procedure Check_Type_Conformant
2684 (New_Id : Entity_Id;
2685 Old_Id : Entity_Id;
2686 Err_Loc : Node_Id := Empty)
2688 Result : Boolean;
2690 begin
2691 Check_Conformance
2692 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
2693 end Check_Type_Conformant;
2695 ----------------------
2696 -- Conforming_Types --
2697 ----------------------
2699 function Conforming_Types
2700 (T1 : Entity_Id;
2701 T2 : Entity_Id;
2702 Ctype : Conformance_Type;
2703 Get_Inst : Boolean := False)
2704 return Boolean
2706 Type_1 : Entity_Id := T1;
2707 Type_2 : Entity_Id := T2;
2709 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
2710 -- If neither T1 nor T2 are generic actual types, then verify
2711 -- that the base types are equal. Otherwise T1 and T2 must be
2712 -- on the same subtype chain. The whole purpose of this procedure
2713 -- is to prevent spurious ambiguities in an instantiation that may
2714 -- arise if two distinct generic types are instantiated with the
2715 -- same actual.
2717 ----------------------
2718 -- Base_Types_Match --
2719 ----------------------
2721 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
2722 begin
2723 if T1 = T2 then
2724 return True;
2726 elsif Base_Type (T1) = Base_Type (T2) then
2728 -- The following is too permissive. A more precise test must
2729 -- check that the generic actual is an ancestor subtype of the
2730 -- other ???.
2732 return not Is_Generic_Actual_Type (T1)
2733 or else not Is_Generic_Actual_Type (T2);
2735 else
2736 return False;
2737 end if;
2738 end Base_Types_Match;
2740 begin
2741 -- The context is an instance association for a formal
2742 -- access-to-subprogram type; the formal parameter types
2743 -- require mapping because they may denote other formal
2744 -- parameters of the generic unit.
2746 if Get_Inst then
2747 Type_1 := Get_Instance_Of (T1);
2748 Type_2 := Get_Instance_Of (T2);
2749 end if;
2751 -- First see if base types match
2753 if Base_Types_Match (Type_1, Type_2) then
2754 return Ctype <= Mode_Conformant
2755 or else Subtypes_Statically_Match (Type_1, Type_2);
2757 elsif Is_Incomplete_Or_Private_Type (Type_1)
2758 and then Present (Full_View (Type_1))
2759 and then Base_Types_Match (Full_View (Type_1), Type_2)
2760 then
2761 return Ctype <= Mode_Conformant
2762 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
2764 elsif Ekind (Type_2) = E_Incomplete_Type
2765 and then Present (Full_View (Type_2))
2766 and then Base_Types_Match (Type_1, Full_View (Type_2))
2767 then
2768 return Ctype <= Mode_Conformant
2769 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
2770 end if;
2772 -- Test anonymous access type case. For this case, static subtype
2773 -- matching is required for mode conformance (RM 6.3.1(15))
2775 if Ekind (Type_1) = E_Anonymous_Access_Type
2776 and then Ekind (Type_2) = E_Anonymous_Access_Type
2777 then
2778 declare
2779 Desig_1 : Entity_Id;
2780 Desig_2 : Entity_Id;
2782 begin
2783 Desig_1 := Directly_Designated_Type (Type_1);
2785 -- An access parameter can designate an incomplete type.
2787 if Ekind (Desig_1) = E_Incomplete_Type
2788 and then Present (Full_View (Desig_1))
2789 then
2790 Desig_1 := Full_View (Desig_1);
2791 end if;
2793 Desig_2 := Directly_Designated_Type (Type_2);
2795 if Ekind (Desig_2) = E_Incomplete_Type
2796 and then Present (Full_View (Desig_2))
2797 then
2798 Desig_2 := Full_View (Desig_2);
2799 end if;
2801 -- The context is an instance association for a formal
2802 -- access-to-subprogram type; formal access parameter
2803 -- designated types require mapping because they may
2804 -- denote other formal parameters of the generic unit.
2806 if Get_Inst then
2807 Desig_1 := Get_Instance_Of (Desig_1);
2808 Desig_2 := Get_Instance_Of (Desig_2);
2809 end if;
2811 -- It is possible for a Class_Wide_Type to be introduced for
2812 -- an incomplete type, in which case there is a separate class_
2813 -- wide type for the full view. The types conform if their
2814 -- Etypes conform, i.e. one may be the full view of the other.
2815 -- This can only happen in the context of an access parameter,
2816 -- other uses of an incomplete Class_Wide_Type are illegal.
2818 if Ekind (Desig_1) = E_Class_Wide_Type
2819 and then Ekind (Desig_2) = E_Class_Wide_Type
2820 then
2821 return
2822 Conforming_Types (Etype (Desig_1), Etype (Desig_2), Ctype);
2823 else
2824 return Base_Type (Desig_1) = Base_Type (Desig_2)
2825 and then (Ctype = Type_Conformant
2826 or else
2827 Subtypes_Statically_Match (Desig_1, Desig_2));
2828 end if;
2829 end;
2831 -- Otherwise definitely no match
2833 else
2834 return False;
2835 end if;
2837 end Conforming_Types;
2839 --------------------------
2840 -- Create_Extra_Formals --
2841 --------------------------
2843 procedure Create_Extra_Formals (E : Entity_Id) is
2844 Formal : Entity_Id;
2845 Last_Formal : Entity_Id;
2846 Last_Extra : Entity_Id;
2847 Formal_Type : Entity_Id;
2848 P_Formal : Entity_Id := Empty;
2850 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id;
2851 -- Add an extra formal, associated with the current Formal. The
2852 -- extra formal is added to the list of extra formals, and also
2853 -- returned as the result. These formals are always of mode IN.
2855 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id is
2856 EF : constant Entity_Id :=
2857 Make_Defining_Identifier (Sloc (Formal),
2858 Chars => New_External_Name (Chars (Formal), 'F'));
2860 begin
2861 -- We never generate extra formals if expansion is not active
2862 -- because we don't need them unless we are generating code.
2864 if not Expander_Active then
2865 return Empty;
2866 end if;
2868 -- A little optimization. Never generate an extra formal for
2869 -- the _init operand of an initialization procedure, since it
2870 -- could never be used.
2872 if Chars (Formal) = Name_uInit then
2873 return Empty;
2874 end if;
2876 Set_Ekind (EF, E_In_Parameter);
2877 Set_Actual_Subtype (EF, Typ);
2878 Set_Etype (EF, Typ);
2879 Set_Scope (EF, Scope (Formal));
2880 Set_Mechanism (EF, Default_Mechanism);
2881 Set_Formal_Validity (EF);
2883 Set_Extra_Formal (Last_Extra, EF);
2884 Last_Extra := EF;
2885 return EF;
2886 end Add_Extra_Formal;
2888 -- Start of processing for Create_Extra_Formals
2890 begin
2891 -- If this is a derived subprogram then the subtypes of the
2892 -- parent subprogram's formal parameters will be used to
2893 -- to determine the need for extra formals.
2895 if Is_Overloadable (E) and then Present (Alias (E)) then
2896 P_Formal := First_Formal (Alias (E));
2897 end if;
2899 Last_Extra := Empty;
2900 Formal := First_Formal (E);
2901 while Present (Formal) loop
2902 Last_Extra := Formal;
2903 Next_Formal (Formal);
2904 end loop;
2906 -- If Extra_formals where already created, don't do it again
2907 -- This situation may arise for subprogram types created as part
2908 -- of dispatching calls (see Expand_Dispatch_Call)
2910 if Present (Last_Extra) and then
2911 Present (Extra_Formal (Last_Extra))
2912 then
2913 return;
2914 end if;
2916 Formal := First_Formal (E);
2918 while Present (Formal) loop
2920 -- Create extra formal for supporting the attribute 'Constrained.
2921 -- The case of a private type view without discriminants also
2922 -- requires the extra formal if the underlying type has defaulted
2923 -- discriminants.
2925 if Ekind (Formal) /= E_In_Parameter then
2926 if Present (P_Formal) then
2927 Formal_Type := Etype (P_Formal);
2928 else
2929 Formal_Type := Etype (Formal);
2930 end if;
2932 if not Has_Discriminants (Formal_Type)
2933 and then Ekind (Formal_Type) in Private_Kind
2934 and then Present (Underlying_Type (Formal_Type))
2935 then
2936 Formal_Type := Underlying_Type (Formal_Type);
2937 end if;
2939 if Has_Discriminants (Formal_Type)
2940 and then
2941 ((not Is_Constrained (Formal_Type)
2942 and then not Is_Indefinite_Subtype (Formal_Type))
2943 or else Present (Extra_Formal (Formal)))
2944 then
2945 Set_Extra_Constrained
2946 (Formal, Add_Extra_Formal (Standard_Boolean));
2947 end if;
2948 end if;
2950 -- Create extra formal for supporting accessibility checking
2952 -- This is suppressed if we specifically suppress accessibility
2953 -- checks for either the subprogram, or the package in which it
2954 -- resides. However, we do not suppress it simply if the scope
2955 -- has accessibility checks suppressed, since this could cause
2956 -- trouble when clients are compiled with a different suppression
2957 -- setting. The explicit checks are safe from this point of view.
2959 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2960 and then not
2961 (Suppress_Accessibility_Checks (E)
2962 or else
2963 Suppress_Accessibility_Checks (Scope (E)))
2964 and then
2965 (not Present (P_Formal)
2966 or else Present (Extra_Accessibility (P_Formal)))
2967 then
2968 -- Temporary kludge: for now we avoid creating the extra
2969 -- formal for access parameters of protected operations
2970 -- because of problem with the case of internal protected
2971 -- calls. ???
2973 if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
2974 and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
2975 then
2976 Set_Extra_Accessibility
2977 (Formal, Add_Extra_Formal (Standard_Natural));
2978 end if;
2979 end if;
2981 if Present (P_Formal) then
2982 Next_Formal (P_Formal);
2983 end if;
2985 Last_Formal := Formal;
2986 Next_Formal (Formal);
2987 end loop;
2988 end Create_Extra_Formals;
2990 -----------------------------
2991 -- Enter_Overloaded_Entity --
2992 -----------------------------
2994 procedure Enter_Overloaded_Entity (S : Entity_Id) is
2995 E : Entity_Id := Current_Entity_In_Scope (S);
2996 C_E : Entity_Id := Current_Entity (S);
2998 begin
2999 if Present (E) then
3000 Set_Has_Homonym (E);
3001 Set_Has_Homonym (S);
3002 end if;
3004 Set_Is_Immediately_Visible (S);
3005 Set_Scope (S, Current_Scope);
3007 -- Chain new entity if front of homonym in current scope, so that
3008 -- homonyms are contiguous.
3010 if Present (E)
3011 and then E /= C_E
3012 then
3013 while Homonym (C_E) /= E loop
3014 C_E := Homonym (C_E);
3015 end loop;
3017 Set_Homonym (C_E, S);
3019 else
3020 E := C_E;
3021 Set_Current_Entity (S);
3022 end if;
3024 Set_Homonym (S, E);
3026 Append_Entity (S, Current_Scope);
3027 Set_Public_Status (S);
3029 if Debug_Flag_E then
3030 Write_Str ("New overloaded entity chain: ");
3031 Write_Name (Chars (S));
3032 E := S;
3034 while Present (E) loop
3035 Write_Str (" "); Write_Int (Int (E));
3036 E := Homonym (E);
3037 end loop;
3039 Write_Eol;
3040 end if;
3042 -- Generate warning for hiding
3044 if Warn_On_Hiding
3045 and then Comes_From_Source (S)
3046 and then In_Extended_Main_Source_Unit (S)
3047 then
3048 E := S;
3049 loop
3050 E := Homonym (E);
3051 exit when No (E);
3053 -- Warn unless genuine overloading
3055 if (not Is_Overloadable (E))
3056 or else Subtype_Conformant (E, S)
3057 then
3058 Error_Msg_Sloc := Sloc (E);
3059 Error_Msg_N ("declaration of & hides one#?", S);
3060 end if;
3061 end loop;
3062 end if;
3063 end Enter_Overloaded_Entity;
3065 -----------------------------
3066 -- Find_Corresponding_Spec --
3067 -----------------------------
3069 function Find_Corresponding_Spec (N : Node_Id) return Entity_Id is
3070 Spec : constant Node_Id := Specification (N);
3071 Designator : constant Entity_Id := Defining_Entity (Spec);
3073 E : Entity_Id;
3075 begin
3076 E := Current_Entity (Designator);
3078 while Present (E) loop
3080 -- We are looking for a matching spec. It must have the same scope,
3081 -- and the same name, and either be type conformant, or be the case
3082 -- of a library procedure spec and its body (which belong to one
3083 -- another regardless of whether they are type conformant or not).
3085 if Scope (E) = Current_Scope then
3086 if (Current_Scope = Standard_Standard
3087 or else (Ekind (E) = Ekind (Designator)
3088 and then
3089 Type_Conformant (E, Designator)))
3090 then
3091 -- Within an instantiation, we know that spec and body are
3092 -- subtype conformant, because they were subtype conformant
3093 -- in the generic. We choose the subtype-conformant entity
3094 -- here as well, to resolve spurious ambiguities in the
3095 -- instance that were not present in the generic (i.e. when
3096 -- two different types are given the same actual). If we are
3097 -- looking for a spec to match a body, full conformance is
3098 -- expected.
3100 if In_Instance then
3101 Set_Convention (Designator, Convention (E));
3103 if Nkind (N) = N_Subprogram_Body
3104 and then Present (Homonym (E))
3105 and then not Fully_Conformant (E, Designator)
3106 then
3107 goto Next_Entity;
3109 elsif not Subtype_Conformant (E, Designator) then
3110 goto Next_Entity;
3111 end if;
3112 end if;
3114 if not Has_Completion (E) then
3116 if Nkind (N) /= N_Subprogram_Body_Stub then
3117 Set_Corresponding_Spec (N, E);
3118 end if;
3120 Set_Has_Completion (E);
3121 return E;
3123 elsif Nkind (Parent (N)) = N_Subunit then
3125 -- If this is the proper body of a subunit, the completion
3126 -- flag is set when analyzing the stub.
3128 return E;
3130 -- If body already exists, this is an error unless the
3131 -- previous declaration is the implicit declaration of
3132 -- a derived subprogram, or this is a spurious overloading
3133 -- in an instance.
3135 elsif No (Alias (E))
3136 and then not Is_Intrinsic_Subprogram (E)
3137 and then not In_Instance
3138 then
3139 Error_Msg_Sloc := Sloc (E);
3140 Error_Msg_NE ("duplicate body for & declared#", N, E);
3141 end if;
3143 elsif Is_Child_Unit (E)
3144 and then
3145 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
3146 and then
3147 Nkind (Parent (Unit_Declaration_Node (Designator)))
3148 = N_Compilation_Unit
3149 then
3151 -- Child units cannot be overloaded, so a conformance mismatch
3152 -- between body and a previous spec is an error.
3154 Error_Msg_N
3155 ("body of child unit does not match previous declaration", N);
3156 end if;
3157 end if;
3159 <<Next_Entity>>
3160 E := Homonym (E);
3161 end loop;
3163 -- On exit, we know that no previous declaration of subprogram exists
3165 return Empty;
3166 end Find_Corresponding_Spec;
3168 ----------------------
3169 -- Fully_Conformant --
3170 ----------------------
3172 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
3173 Result : Boolean;
3175 begin
3176 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
3177 return Result;
3178 end Fully_Conformant;
3180 ----------------------------------
3181 -- Fully_Conformant_Expressions --
3182 ----------------------------------
3184 function Fully_Conformant_Expressions
3185 (Given_E1 : Node_Id;
3186 Given_E2 : Node_Id)
3187 return Boolean
3189 E1 : constant Node_Id := Original_Node (Given_E1);
3190 E2 : constant Node_Id := Original_Node (Given_E2);
3191 -- We always test conformance on original nodes, since it is possible
3192 -- for analysis and/or expansion to make things look as though they
3193 -- conform when they do not, e.g. by converting 1+2 into 3.
3195 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
3196 renames Fully_Conformant_Expressions;
3198 function FCL (L1, L2 : List_Id) return Boolean;
3199 -- Compare elements of two lists for conformance. Elements have to
3200 -- be conformant, and actuals inserted as default parameters do not
3201 -- match explicit actuals with the same value.
3203 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
3204 -- Compare an operator node with a function call.
3206 ---------
3207 -- FCL --
3208 ---------
3210 function FCL (L1, L2 : List_Id) return Boolean is
3211 N1, N2 : Node_Id;
3213 begin
3214 if L1 = No_List then
3215 N1 := Empty;
3216 else
3217 N1 := First (L1);
3218 end if;
3220 if L2 = No_List then
3221 N2 := Empty;
3222 else
3223 N2 := First (L2);
3224 end if;
3226 -- Compare two lists, skipping rewrite insertions (we want to
3227 -- compare the original trees, not the expanded versions!)
3229 loop
3230 if Is_Rewrite_Insertion (N1) then
3231 Next (N1);
3232 elsif Is_Rewrite_Insertion (N2) then
3233 Next (N2);
3234 elsif No (N1) then
3235 return No (N2);
3236 elsif No (N2) then
3237 return False;
3238 elsif not FCE (N1, N2) then
3239 return False;
3240 else
3241 Next (N1);
3242 Next (N2);
3243 end if;
3244 end loop;
3245 end FCL;
3247 ---------
3248 -- FCO --
3249 ---------
3251 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
3252 Actuals : constant List_Id := Parameter_Associations (Call_Node);
3253 Act : Node_Id;
3255 begin
3256 if No (Actuals)
3257 or else Entity (Op_Node) /= Entity (Name (Call_Node))
3258 then
3259 return False;
3261 else
3262 Act := First (Actuals);
3264 if Nkind (Op_Node) in N_Binary_Op then
3266 if not FCE (Left_Opnd (Op_Node), Act) then
3267 return False;
3268 end if;
3270 Next (Act);
3271 end if;
3273 return Present (Act)
3274 and then FCE (Right_Opnd (Op_Node), Act)
3275 and then No (Next (Act));
3276 end if;
3277 end FCO;
3279 -- Start of processing for Fully_Conformant_Expressions
3281 begin
3282 -- Non-conformant if paren count does not match. Note: if some idiot
3283 -- complains that we don't do this right for more than 3 levels of
3284 -- parentheses, they will be treated with the respect they deserve :-)
3286 if Paren_Count (E1) /= Paren_Count (E2) then
3287 return False;
3289 -- If same entities are referenced, then they are conformant
3290 -- even if they have different forms (RM 8.3.1(19-20)).
3292 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
3293 if Present (Entity (E1)) then
3294 return Entity (E1) = Entity (E2)
3295 or else (Chars (Entity (E1)) = Chars (Entity (E2))
3296 and then Ekind (Entity (E1)) = E_Discriminant
3297 and then Ekind (Entity (E2)) = E_In_Parameter);
3299 elsif Nkind (E1) = N_Expanded_Name
3300 and then Nkind (E2) = N_Expanded_Name
3301 and then Nkind (Selector_Name (E1)) = N_Character_Literal
3302 and then Nkind (Selector_Name (E2)) = N_Character_Literal
3303 then
3304 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
3306 else
3307 -- Identifiers in component associations don't always have
3308 -- entities, but their names must conform.
3310 return Nkind (E1) = N_Identifier
3311 and then Nkind (E2) = N_Identifier
3312 and then Chars (E1) = Chars (E2);
3313 end if;
3315 elsif Nkind (E1) = N_Character_Literal
3316 and then Nkind (E2) = N_Expanded_Name
3317 then
3318 return Nkind (Selector_Name (E2)) = N_Character_Literal
3319 and then Chars (E1) = Chars (Selector_Name (E2));
3321 elsif Nkind (E2) = N_Character_Literal
3322 and then Nkind (E1) = N_Expanded_Name
3323 then
3324 return Nkind (Selector_Name (E1)) = N_Character_Literal
3325 and then Chars (E2) = Chars (Selector_Name (E1));
3327 elsif Nkind (E1) in N_Op
3328 and then Nkind (E2) = N_Function_Call
3329 then
3330 return FCO (E1, E2);
3332 elsif Nkind (E2) in N_Op
3333 and then Nkind (E1) = N_Function_Call
3334 then
3335 return FCO (E2, E1);
3337 -- Otherwise we must have the same syntactic entity
3339 elsif Nkind (E1) /= Nkind (E2) then
3340 return False;
3342 -- At this point, we specialize by node type
3344 else
3345 case Nkind (E1) is
3347 when N_Aggregate =>
3348 return
3349 FCL (Expressions (E1), Expressions (E2))
3350 and then FCL (Component_Associations (E1),
3351 Component_Associations (E2));
3353 when N_Allocator =>
3354 if Nkind (Expression (E1)) = N_Qualified_Expression
3355 or else
3356 Nkind (Expression (E2)) = N_Qualified_Expression
3357 then
3358 return FCE (Expression (E1), Expression (E2));
3360 -- Check that the subtype marks and any constraints
3361 -- are conformant
3363 else
3364 declare
3365 Indic1 : constant Node_Id := Expression (E1);
3366 Indic2 : constant Node_Id := Expression (E2);
3367 Elt1 : Node_Id;
3368 Elt2 : Node_Id;
3370 begin
3371 if Nkind (Indic1) /= N_Subtype_Indication then
3372 return
3373 Nkind (Indic2) /= N_Subtype_Indication
3374 and then Entity (Indic1) = Entity (Indic2);
3376 elsif Nkind (Indic2) /= N_Subtype_Indication then
3377 return
3378 Nkind (Indic1) /= N_Subtype_Indication
3379 and then Entity (Indic1) = Entity (Indic2);
3381 else
3382 if Entity (Subtype_Mark (Indic1)) /=
3383 Entity (Subtype_Mark (Indic2))
3384 then
3385 return False;
3386 end if;
3388 Elt1 := First (Constraints (Constraint (Indic1)));
3389 Elt2 := First (Constraints (Constraint (Indic2)));
3391 while Present (Elt1) and then Present (Elt2) loop
3392 if not FCE (Elt1, Elt2) then
3393 return False;
3394 end if;
3396 Next (Elt1);
3397 Next (Elt2);
3398 end loop;
3400 return True;
3401 end if;
3402 end;
3403 end if;
3405 when N_Attribute_Reference =>
3406 return
3407 Attribute_Name (E1) = Attribute_Name (E2)
3408 and then FCL (Expressions (E1), Expressions (E2));
3410 when N_Binary_Op =>
3411 return
3412 Entity (E1) = Entity (E2)
3413 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
3414 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
3416 when N_And_Then | N_Or_Else | N_In | N_Not_In =>
3417 return
3418 FCE (Left_Opnd (E1), Left_Opnd (E2))
3419 and then
3420 FCE (Right_Opnd (E1), Right_Opnd (E2));
3422 when N_Character_Literal =>
3423 return
3424 Char_Literal_Value (E1) = Char_Literal_Value (E2);
3426 when N_Component_Association =>
3427 return
3428 FCL (Choices (E1), Choices (E2))
3429 and then FCE (Expression (E1), Expression (E2));
3431 when N_Conditional_Expression =>
3432 return
3433 FCL (Expressions (E1), Expressions (E2));
3435 when N_Explicit_Dereference =>
3436 return
3437 FCE (Prefix (E1), Prefix (E2));
3439 when N_Extension_Aggregate =>
3440 return
3441 FCL (Expressions (E1), Expressions (E2))
3442 and then Null_Record_Present (E1) =
3443 Null_Record_Present (E2)
3444 and then FCL (Component_Associations (E1),
3445 Component_Associations (E2));
3447 when N_Function_Call =>
3448 return
3449 FCE (Name (E1), Name (E2))
3450 and then FCL (Parameter_Associations (E1),
3451 Parameter_Associations (E2));
3453 when N_Indexed_Component =>
3454 return
3455 FCE (Prefix (E1), Prefix (E2))
3456 and then FCL (Expressions (E1), Expressions (E2));
3458 when N_Integer_Literal =>
3459 return (Intval (E1) = Intval (E2));
3461 when N_Null =>
3462 return True;
3464 when N_Operator_Symbol =>
3465 return
3466 Chars (E1) = Chars (E2);
3468 when N_Others_Choice =>
3469 return True;
3471 when N_Parameter_Association =>
3472 return
3474 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
3475 and then FCE (Explicit_Actual_Parameter (E1),
3476 Explicit_Actual_Parameter (E2));
3478 when N_Qualified_Expression =>
3479 return
3480 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3481 and then FCE (Expression (E1), Expression (E2));
3483 when N_Range =>
3484 return
3485 FCE (Low_Bound (E1), Low_Bound (E2))
3486 and then FCE (High_Bound (E1), High_Bound (E2));
3488 when N_Real_Literal =>
3489 return (Realval (E1) = Realval (E2));
3491 when N_Selected_Component =>
3492 return
3493 FCE (Prefix (E1), Prefix (E2))
3494 and then FCE (Selector_Name (E1), Selector_Name (E2));
3496 when N_Slice =>
3497 return
3498 FCE (Prefix (E1), Prefix (E2))
3499 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
3501 when N_String_Literal =>
3502 declare
3503 S1 : constant String_Id := Strval (E1);
3504 S2 : constant String_Id := Strval (E2);
3505 L1 : constant Nat := String_Length (S1);
3506 L2 : constant Nat := String_Length (S2);
3508 begin
3509 if L1 /= L2 then
3510 return False;
3512 else
3513 for J in 1 .. L1 loop
3514 if Get_String_Char (S1, J) /=
3515 Get_String_Char (S2, J)
3516 then
3517 return False;
3518 end if;
3519 end loop;
3521 return True;
3522 end if;
3523 end;
3525 when N_Type_Conversion =>
3526 return
3527 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3528 and then FCE (Expression (E1), Expression (E2));
3530 when N_Unary_Op =>
3531 return
3532 Entity (E1) = Entity (E2)
3533 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
3535 when N_Unchecked_Type_Conversion =>
3536 return
3537 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3538 and then FCE (Expression (E1), Expression (E2));
3540 -- All other node types cannot appear in this context. Strictly
3541 -- we should raise a fatal internal error. Instead we just ignore
3542 -- the nodes. This means that if anyone makes a mistake in the
3543 -- expander and mucks an expression tree irretrievably, the
3544 -- result will be a failure to detect a (probably very obscure)
3545 -- case of non-conformance, which is better than bombing on some
3546 -- case where two expressions do in fact conform.
3548 when others =>
3549 return True;
3551 end case;
3552 end if;
3553 end Fully_Conformant_Expressions;
3555 --------------------
3556 -- Install_Entity --
3557 --------------------
3559 procedure Install_Entity (E : Entity_Id) is
3560 Prev : constant Entity_Id := Current_Entity (E);
3562 begin
3563 Set_Is_Immediately_Visible (E);
3564 Set_Current_Entity (E);
3565 Set_Homonym (E, Prev);
3566 end Install_Entity;
3568 ---------------------
3569 -- Install_Formals --
3570 ---------------------
3572 procedure Install_Formals (Id : Entity_Id) is
3573 F : Entity_Id;
3575 begin
3576 F := First_Formal (Id);
3578 while Present (F) loop
3579 Install_Entity (F);
3580 Next_Formal (F);
3581 end loop;
3582 end Install_Formals;
3584 ---------------------------------
3585 -- Is_Non_Overriding_Operation --
3586 ---------------------------------
3588 function Is_Non_Overriding_Operation
3589 (Prev_E : Entity_Id;
3590 New_E : Entity_Id)
3591 return Boolean
3593 Formal : Entity_Id;
3594 F_Typ : Entity_Id;
3595 G_Typ : Entity_Id := Empty;
3597 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
3598 -- If F_Type is a derived type associated with a generic actual
3599 -- subtype, then return its Generic_Parent_Type attribute, else
3600 -- return Empty.
3602 function Types_Correspond
3603 (P_Type : Entity_Id;
3604 N_Type : Entity_Id)
3605 return Boolean;
3606 -- Returns true if and only if the types (or designated types
3607 -- in the case of anonymous access types) are the same or N_Type
3608 -- is derived directly or indirectly from P_Type.
3610 -----------------------------
3611 -- Get_Generic_Parent_Type --
3612 -----------------------------
3614 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
3615 G_Typ : Entity_Id;
3616 Indic : Node_Id;
3618 begin
3619 if Is_Derived_Type (F_Typ)
3620 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
3621 then
3622 -- The tree must be traversed to determine the parent
3623 -- subtype in the generic unit, which unfortunately isn't
3624 -- always available via semantic attributes. ???
3625 -- (Note: The use of Original_Node is needed for cases
3626 -- where a full derived type has been rewritten.)
3628 Indic := Subtype_Indication
3629 (Type_Definition (Original_Node (Parent (F_Typ))));
3631 if Nkind (Indic) = N_Subtype_Indication then
3632 G_Typ := Entity (Subtype_Mark (Indic));
3633 else
3634 G_Typ := Entity (Indic);
3635 end if;
3637 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
3638 and then Present (Generic_Parent_Type (Parent (G_Typ)))
3639 then
3640 return Generic_Parent_Type (Parent (G_Typ));
3641 end if;
3642 end if;
3644 return Empty;
3645 end Get_Generic_Parent_Type;
3647 ----------------------
3648 -- Types_Correspond --
3649 ----------------------
3651 function Types_Correspond
3652 (P_Type : Entity_Id;
3653 N_Type : Entity_Id)
3654 return Boolean
3656 Prev_Type : Entity_Id := Base_Type (P_Type);
3657 New_Type : Entity_Id := Base_Type (N_Type);
3659 begin
3660 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
3661 Prev_Type := Designated_Type (Prev_Type);
3662 end if;
3664 if Ekind (New_Type) = E_Anonymous_Access_Type then
3665 New_Type := Designated_Type (New_Type);
3666 end if;
3668 if Prev_Type = New_Type then
3669 return True;
3671 elsif not Is_Class_Wide_Type (New_Type) then
3672 while Etype (New_Type) /= New_Type loop
3673 New_Type := Etype (New_Type);
3674 if New_Type = Prev_Type then
3675 return True;
3676 end if;
3677 end loop;
3678 end if;
3679 return False;
3680 end Types_Correspond;
3682 -- Start of processing for Is_Non_Overriding_Operation
3684 begin
3685 -- In the case where both operations are implicit derived
3686 -- subprograms then neither overrides the other. This can
3687 -- only occur in certain obscure cases (e.g., derivation
3688 -- from homographs created in a generic instantiation).
3690 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
3691 return True;
3693 elsif Ekind (Current_Scope) = E_Package
3694 and then Is_Generic_Instance (Current_Scope)
3695 and then In_Private_Part (Current_Scope)
3696 and then Comes_From_Source (New_E)
3697 then
3698 -- We examine the formals and result subtype of the inherited
3699 -- operation, to determine whether their type is derived from
3700 -- (the instance of) a generic type.
3702 Formal := First_Formal (Prev_E);
3704 while Present (Formal) loop
3705 F_Typ := Base_Type (Etype (Formal));
3707 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3708 F_Typ := Designated_Type (F_Typ);
3709 end if;
3711 G_Typ := Get_Generic_Parent_Type (F_Typ);
3713 Next_Formal (Formal);
3714 end loop;
3716 if not Present (G_Typ) and then Ekind (Prev_E) = E_Function then
3717 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
3718 end if;
3720 if No (G_Typ) then
3721 return False;
3722 end if;
3724 -- If the generic type is a private type, then the original
3725 -- operation was not overriding in the generic, because there was
3726 -- no primitive operation to override.
3728 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
3729 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
3730 N_Formal_Private_Type_Definition
3731 then
3732 return True;
3734 -- The generic parent type is the ancestor of a formal derived
3735 -- type declaration. We need to check whether it has a primitive
3736 -- operation that should be overridden by New_E in the generic.
3738 else
3739 declare
3740 P_Formal : Entity_Id;
3741 N_Formal : Entity_Id;
3742 P_Typ : Entity_Id;
3743 N_Typ : Entity_Id;
3744 P_Prim : Entity_Id;
3745 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
3747 begin
3748 while Present (Prim_Elt) loop
3749 P_Prim := Node (Prim_Elt);
3750 if Chars (P_Prim) = Chars (New_E)
3751 and then Ekind (P_Prim) = Ekind (New_E)
3752 then
3753 P_Formal := First_Formal (P_Prim);
3754 N_Formal := First_Formal (New_E);
3755 while Present (P_Formal) and then Present (N_Formal) loop
3756 P_Typ := Etype (P_Formal);
3757 N_Typ := Etype (N_Formal);
3759 if not Types_Correspond (P_Typ, N_Typ) then
3760 exit;
3761 end if;
3763 Next_Entity (P_Formal);
3764 Next_Entity (N_Formal);
3765 end loop;
3767 -- Found a matching primitive operation belonging to
3768 -- the formal ancestor type, so the new subprogram
3769 -- is overriding.
3771 if not Present (P_Formal)
3772 and then not Present (N_Formal)
3773 and then (Ekind (New_E) /= E_Function
3774 or else
3775 Types_Correspond
3776 (Etype (P_Prim), Etype (New_E)))
3777 then
3778 return False;
3779 end if;
3780 end if;
3782 Next_Elmt (Prim_Elt);
3783 end loop;
3785 -- If no match found, then the new subprogram does
3786 -- not override in the generic (nor in the instance).
3788 return True;
3789 end;
3790 end if;
3791 else
3792 return False;
3793 end if;
3794 end Is_Non_Overriding_Operation;
3796 ------------------------------
3797 -- Make_Inequality_Operator --
3798 ------------------------------
3800 -- S is the defining identifier of an equality operator. We build a
3801 -- subprogram declaration with the right signature. This operation is
3802 -- intrinsic, because it is always expanded as the negation of the
3803 -- call to the equality function.
3805 procedure Make_Inequality_Operator (S : Entity_Id) is
3806 Loc : constant Source_Ptr := Sloc (S);
3807 Decl : Node_Id;
3808 Formals : List_Id;
3809 Op_Name : Entity_Id;
3811 A : Entity_Id;
3812 B : Entity_Id;
3814 begin
3815 -- Check that equality was properly defined.
3817 if No (Next_Formal (First_Formal (S))) then
3818 return;
3819 end if;
3821 A := Make_Defining_Identifier (Loc, Chars (First_Formal (S)));
3822 B := Make_Defining_Identifier (Loc,
3823 Chars (Next_Formal (First_Formal (S))));
3825 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
3827 Formals := New_List (
3828 Make_Parameter_Specification (Loc,
3829 Defining_Identifier => A,
3830 Parameter_Type =>
3831 New_Reference_To (Etype (First_Formal (S)), Loc)),
3833 Make_Parameter_Specification (Loc,
3834 Defining_Identifier => B,
3835 Parameter_Type =>
3836 New_Reference_To (Etype (Next_Formal (First_Formal (S))), Loc)));
3838 Decl :=
3839 Make_Subprogram_Declaration (Loc,
3840 Specification =>
3841 Make_Function_Specification (Loc,
3842 Defining_Unit_Name => Op_Name,
3843 Parameter_Specifications => Formals,
3844 Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)));
3846 -- Insert inequality right after equality if it is explicit or after
3847 -- the derived type when implicit. These entities are created only
3848 -- for visibility purposes, and eventually replaced in the course of
3849 -- expansion, so they do not need to be attached to the tree and seen
3850 -- by the back-end. Keeping them internal also avoids spurious freezing
3851 -- problems. The parent field is set simply to make analysis safe.
3853 if No (Alias (S)) then
3854 Set_Parent (Decl, Parent (Unit_Declaration_Node (S)));
3855 else
3856 Set_Parent (Decl, Parent (Parent (Etype (First_Formal (S)))));
3857 end if;
3859 Mark_Rewrite_Insertion (Decl);
3860 Set_Is_Intrinsic_Subprogram (Op_Name);
3861 Analyze (Decl);
3862 Set_Has_Completion (Op_Name);
3863 Set_Corresponding_Equality (Op_Name, S);
3864 Set_Is_Abstract (Op_Name, Is_Abstract (S));
3866 end Make_Inequality_Operator;
3868 ----------------------
3869 -- May_Need_Actuals --
3870 ----------------------
3872 procedure May_Need_Actuals (Fun : Entity_Id) is
3873 F : Entity_Id;
3874 B : Boolean;
3876 begin
3877 F := First_Formal (Fun);
3878 B := True;
3880 while Present (F) loop
3881 if No (Default_Value (F)) then
3882 B := False;
3883 exit;
3884 end if;
3886 Next_Formal (F);
3887 end loop;
3889 Set_Needs_No_Actuals (Fun, B);
3890 end May_Need_Actuals;
3892 ---------------------
3893 -- Mode_Conformant --
3894 ---------------------
3896 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
3897 Result : Boolean;
3899 begin
3900 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
3901 return Result;
3902 end Mode_Conformant;
3904 ---------------------------
3905 -- New_Overloaded_Entity --
3906 ---------------------------
3908 procedure New_Overloaded_Entity
3909 (S : Entity_Id;
3910 Derived_Type : Entity_Id := Empty)
3912 E : Entity_Id := Current_Entity_In_Scope (S);
3913 Prev_Vis : Entity_Id := Empty;
3915 function Is_Private_Declaration (E : Entity_Id) return Boolean;
3916 -- Check that E is declared in the private part of the current package,
3917 -- or in the package body, where it may hide a previous declaration.
3918 -- We can' use In_Private_Part by itself because this flag is also
3919 -- set when freezing entities, so we must examine the place of the
3920 -- declaration in the tree, and recognize wrapper packages as well.
3922 procedure Maybe_Primitive_Operation (Overriding : Boolean := False);
3923 -- If the subprogram being analyzed is a primitive operation of
3924 -- the type of one of its formals, set the corresponding flag.
3926 ----------------------------
3927 -- Is_Private_Declaration --
3928 ----------------------------
3930 function Is_Private_Declaration (E : Entity_Id) return Boolean is
3931 Priv_Decls : List_Id;
3932 Decl : constant Node_Id := Unit_Declaration_Node (E);
3934 begin
3935 if Is_Package (Current_Scope)
3936 and then In_Private_Part (Current_Scope)
3937 then
3938 Priv_Decls :=
3939 Private_Declarations (
3940 Specification (Unit_Declaration_Node (Current_Scope)));
3942 return In_Package_Body (Current_Scope)
3943 or else List_Containing (Decl) = Priv_Decls
3944 or else (Nkind (Parent (Decl)) = N_Package_Specification
3945 and then not Is_Compilation_Unit (
3946 Defining_Entity (Parent (Decl)))
3947 and then List_Containing (Parent (Parent (Decl)))
3948 = Priv_Decls);
3949 else
3950 return False;
3951 end if;
3952 end Is_Private_Declaration;
3954 -------------------------------
3955 -- Maybe_Primitive_Operation --
3956 -------------------------------
3958 procedure Maybe_Primitive_Operation (Overriding : Boolean := False) is
3959 Formal : Entity_Id;
3960 F_Typ : Entity_Id;
3962 function Visible_Part_Type (T : Entity_Id) return Boolean;
3963 -- Returns true if T is declared in the visible part of
3964 -- the current package scope; otherwise returns false.
3965 -- Assumes that T is declared in a package.
3967 procedure Check_Private_Overriding (T : Entity_Id);
3968 -- Checks that if a primitive abstract subprogram of a visible
3969 -- abstract type is declared in a private part, then it must
3970 -- override an abstract subprogram declared in the visible part.
3971 -- Also checks that if a primitive function with a controlling
3972 -- result is declared in a private part, then it must override
3973 -- a function declared in the visible part.
3975 ------------------------------
3976 -- Check_Private_Overriding --
3977 ------------------------------
3979 procedure Check_Private_Overriding (T : Entity_Id) is
3980 begin
3981 if Ekind (Current_Scope) = E_Package
3982 and then In_Private_Part (Current_Scope)
3983 and then Visible_Part_Type (T)
3984 and then not In_Instance
3985 then
3986 if Is_Abstract (T)
3987 and then Is_Abstract (S)
3988 and then (not Overriding or else not Is_Abstract (E))
3989 then
3990 Error_Msg_N ("abstract subprograms must be visible "
3991 & "('R'M 3.9.3(10))!", S);
3993 elsif Ekind (S) = E_Function
3994 and then Is_Tagged_Type (T)
3995 and then T = Base_Type (Etype (S))
3996 and then not Overriding
3997 then
3998 Error_Msg_N
3999 ("private function with tagged result must"
4000 & " override visible-part function", S);
4001 Error_Msg_N
4002 ("\move subprogram to the visible part"
4003 & " ('R'M 3.9.3(10))", S);
4004 end if;
4005 end if;
4006 end Check_Private_Overriding;
4008 -----------------------
4009 -- Visible_Part_Type --
4010 -----------------------
4012 function Visible_Part_Type (T : Entity_Id) return Boolean is
4013 P : Node_Id := Unit_Declaration_Node (Scope (T));
4014 N : Node_Id := First (Visible_Declarations (Specification (P)));
4016 begin
4017 -- If the entity is a private type, then it must be
4018 -- declared in a visible part.
4020 if Ekind (T) in Private_Kind then
4021 return True;
4022 end if;
4024 -- Otherwise, we traverse the visible part looking for its
4025 -- corresponding declaration. We cannot use the declaration
4026 -- node directly because in the private part the entity of a
4027 -- private type is the one in the full view, which does not
4028 -- indicate that it is the completion of something visible.
4030 while Present (N) loop
4031 if Nkind (N) = N_Full_Type_Declaration
4032 and then Present (Defining_Identifier (N))
4033 and then T = Defining_Identifier (N)
4034 then
4035 return True;
4037 elsif (Nkind (N) = N_Private_Type_Declaration
4038 or else
4039 Nkind (N) = N_Private_Extension_Declaration)
4040 and then Present (Defining_Identifier (N))
4041 and then T = Full_View (Defining_Identifier (N))
4042 then
4043 return True;
4044 end if;
4046 Next (N);
4047 end loop;
4049 return False;
4050 end Visible_Part_Type;
4052 -- Start of processing for Maybe_Primitive_Operation
4054 begin
4055 if not Comes_From_Source (S) then
4056 null;
4058 elsif (Ekind (Current_Scope) = E_Package
4059 and then not In_Package_Body (Current_Scope))
4060 or else Overriding
4061 then
4063 if Ekind (S) = E_Function
4064 and then Scope (Base_Type (Etype (S))) = Current_Scope
4065 then
4066 Set_Has_Primitive_Operations (Base_Type (Etype (S)));
4067 Check_Private_Overriding (Base_Type (Etype (S)));
4068 end if;
4070 Formal := First_Formal (S);
4072 while Present (Formal) loop
4073 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
4074 F_Typ := Designated_Type (Etype (Formal));
4075 else
4076 F_Typ := Etype (Formal);
4077 end if;
4079 if Scope (Base_Type (F_Typ)) = Current_Scope then
4080 Set_Has_Primitive_Operations (Base_Type (F_Typ));
4081 Check_Private_Overriding (Base_Type (F_Typ));
4082 end if;
4084 Next_Formal (Formal);
4085 end loop;
4087 end if;
4088 end Maybe_Primitive_Operation;
4090 -- Start of processing for New_Overloaded_Entity
4092 begin
4093 if No (E) then
4094 Enter_Overloaded_Entity (S);
4095 Check_Dispatching_Operation (S, Empty);
4096 Maybe_Primitive_Operation;
4098 elsif not Is_Overloadable (E) then
4100 -- Check for spurious conflict produced by a subprogram that has the
4101 -- same name as that of the enclosing generic package. The conflict
4102 -- occurs within an instance, between the subprogram and the renaming
4103 -- declaration for the package. After the subprogram, the package
4104 -- renaming declaration becomes hidden.
4106 if Ekind (E) = E_Package
4107 and then Present (Renamed_Object (E))
4108 and then Renamed_Object (E) = Current_Scope
4109 and then Nkind (Parent (Renamed_Object (E))) =
4110 N_Package_Specification
4111 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
4112 then
4113 Set_Is_Hidden (E);
4114 Set_Is_Immediately_Visible (E, False);
4115 Enter_Overloaded_Entity (S);
4116 Set_Homonym (S, Homonym (E));
4117 Check_Dispatching_Operation (S, Empty);
4119 -- If the subprogram is implicit it is hidden by the previous
4120 -- declaration. However if it is dispatching, it must appear in
4121 -- the dispatch table anyway, because it can be dispatched to
4122 -- even if it cannot be called directly.
4124 elsif Present (Alias (S))
4125 and then not Comes_From_Source (S)
4126 then
4127 Set_Scope (S, Current_Scope);
4129 if Is_Dispatching_Operation (Alias (S)) then
4130 Check_Dispatching_Operation (S, Empty);
4131 end if;
4133 return;
4135 else
4136 Error_Msg_Sloc := Sloc (E);
4137 Error_Msg_N ("& conflicts with declaration#", S);
4139 -- Useful additional warning.
4141 if Is_Generic_Unit (E) then
4142 Error_Msg_N ("\previous generic unit cannot be overloaded", S);
4143 end if;
4145 return;
4146 end if;
4148 else
4149 -- E exists and is overloadable. Determine whether S is the body
4150 -- of E, a new overloaded entity with a different signature, or
4151 -- an error altogether.
4153 while Present (E) loop
4154 if Scope (E) /= Current_Scope then
4155 null;
4157 elsif Type_Conformant (E, S) then
4159 -- If the old and new entities have the same profile and
4160 -- one is not the body of the other, then this is an error,
4161 -- unless one of them is implicitly declared.
4163 -- There are some cases when both can be implicit, for example
4164 -- when both a literal and a function that overrides it are
4165 -- inherited in a derivation, or when an inhertited operation
4166 -- of a tagged full type overrides the ineherited operation of
4167 -- a private extension. Ada 83 had a special rule for the
4168 -- the literal case. In Ada95, the later implicit operation
4169 -- hides the former, and the literal is always the former.
4170 -- In the odd case where both are derived operations declared
4171 -- at the same point, both operations should be declared,
4172 -- and in that case we bypass the following test and proceed
4173 -- to the next part (this can only occur for certain obscure
4174 -- cases involving homographs in instances and can't occur for
4175 -- dispatching operations ???). Note that the following
4176 -- condition is less than clear. For example, it's not at
4177 -- all clear why there's a test for E_Entry here. ???
4179 if Present (Alias (S))
4180 and then (No (Alias (E))
4181 or else Comes_From_Source (E)
4182 or else Is_Dispatching_Operation (E))
4183 and then
4184 (Ekind (E) = E_Entry
4185 or else Ekind (E) /= E_Enumeration_Literal)
4186 then
4187 -- When an derived operation is overloaded it may be due
4188 -- to the fact that the full view of a private extension
4189 -- re-inherits. It has to be dealt with.
4191 if Is_Package (Current_Scope)
4192 and then In_Private_Part (Current_Scope)
4193 then
4194 Check_Operation_From_Private_View (S, E);
4195 end if;
4197 -- In any case the implicit operation remains hidden by
4198 -- the existing declaration.
4200 return;
4202 -- Within an instance, the renaming declarations for
4203 -- actual subprograms may become ambiguous, but they do
4204 -- not hide each other.
4206 elsif Ekind (E) /= E_Entry
4207 and then not Comes_From_Source (E)
4208 and then not Is_Generic_Instance (E)
4209 and then (Present (Alias (E))
4210 or else Is_Intrinsic_Subprogram (E))
4211 and then (not In_Instance
4212 or else No (Parent (E))
4213 or else Nkind (Unit_Declaration_Node (E)) /=
4214 N_Subprogram_Renaming_Declaration)
4215 then
4216 -- A subprogram child unit is not allowed to override
4217 -- an inherited subprogram (10.1.1(20)).
4219 if Is_Child_Unit (S) then
4220 Error_Msg_N
4221 ("child unit overrides inherited subprogram in parent",
4223 return;
4224 end if;
4226 if Is_Non_Overriding_Operation (E, S) then
4227 Enter_Overloaded_Entity (S);
4228 if not Present (Derived_Type)
4229 or else Is_Tagged_Type (Derived_Type)
4230 then
4231 Check_Dispatching_Operation (S, Empty);
4232 end if;
4234 return;
4235 end if;
4237 -- E is a derived operation or an internal operator which
4238 -- is being overridden. Remove E from further visibility.
4239 -- Furthermore, if E is a dispatching operation, it must be
4240 -- replaced in the list of primitive operations of its type
4241 -- (see Override_Dispatching_Operation).
4243 declare
4244 Prev : Entity_Id;
4246 begin
4247 Prev := First_Entity (Current_Scope);
4249 while Present (Prev)
4250 and then Next_Entity (Prev) /= E
4251 loop
4252 Next_Entity (Prev);
4253 end loop;
4255 -- It is possible for E to be in the current scope and
4256 -- yet not in the entity chain. This can only occur in a
4257 -- generic context where E is an implicit concatenation
4258 -- in the formal part, because in a generic body the
4259 -- entity chain starts with the formals.
4261 pragma Assert
4262 (Present (Prev) or else Chars (E) = Name_Op_Concat);
4264 -- E must be removed both from the entity_list of the
4265 -- current scope, and from the visibility chain
4267 if Debug_Flag_E then
4268 Write_Str ("Override implicit operation ");
4269 Write_Int (Int (E));
4270 Write_Eol;
4271 end if;
4273 -- If E is a predefined concatenation, it stands for four
4274 -- different operations. As a result, a single explicit
4275 -- declaration does not hide it. In a possible ambiguous
4276 -- situation, Disambiguate chooses the user-defined op,
4277 -- so it is correct to retain the previous internal one.
4279 if Chars (E) /= Name_Op_Concat
4280 or else Ekind (E) /= E_Operator
4281 then
4282 -- For nondispatching derived operations that are
4283 -- overridden by a subprogram declared in the private
4284 -- part of a package, we retain the derived subprogram
4285 -- but mark it as not immediately visible. If the
4286 -- derived operation was declared in the visible part
4287 -- then this ensures that it will still be visible
4288 -- outside the package with the proper signature
4289 -- (calls from outside must also be directed to this
4290 -- version rather than the overriding one, unlike the
4291 -- dispatching case). Calls from inside the package
4292 -- will still resolve to the overriding subprogram
4293 -- since the derived one is marked as not visible
4294 -- within the package.
4296 -- If the private operation is dispatching, we achieve
4297 -- the overriding by keeping the implicit operation
4298 -- but setting its alias to be the overring one. In
4299 -- this fashion the proper body is executed in all
4300 -- cases, but the original signature is used outside
4301 -- of the package.
4303 -- If the overriding is not in the private part, we
4304 -- remove the implicit operation altogether.
4306 if Is_Private_Declaration (S) then
4308 if not Is_Dispatching_Operation (E) then
4309 Set_Is_Immediately_Visible (E, False);
4310 else
4312 -- work done in Override_Dispatching_Operation.
4314 null;
4315 end if;
4316 else
4318 -- Find predecessor of E in Homonym chain.
4320 if E = Current_Entity (E) then
4321 Prev_Vis := Empty;
4322 else
4323 Prev_Vis := Current_Entity (E);
4324 while Homonym (Prev_Vis) /= E loop
4325 Prev_Vis := Homonym (Prev_Vis);
4326 end loop;
4327 end if;
4329 if Prev_Vis /= Empty then
4331 -- Skip E in the visibility chain
4333 Set_Homonym (Prev_Vis, Homonym (E));
4335 else
4336 Set_Name_Entity_Id (Chars (E), Homonym (E));
4337 end if;
4339 Set_Next_Entity (Prev, Next_Entity (E));
4341 if No (Next_Entity (Prev)) then
4342 Set_Last_Entity (Current_Scope, Prev);
4343 end if;
4345 end if;
4346 end if;
4348 Enter_Overloaded_Entity (S);
4350 if Is_Dispatching_Operation (E) then
4351 -- An overriding dispatching subprogram inherits
4352 -- the convention of the overridden subprogram
4353 -- (by AI-117).
4355 Set_Convention (S, Convention (E));
4357 Check_Dispatching_Operation (S, E);
4358 else
4359 Check_Dispatching_Operation (S, Empty);
4360 end if;
4362 Maybe_Primitive_Operation (Overriding => True);
4363 goto Check_Inequality;
4364 end;
4366 -- Apparent redeclarations in instances can occur when two
4367 -- formal types get the same actual type. The subprograms in
4368 -- in the instance are legal, even if not callable from the
4369 -- outside. Calls from within are disambiguated elsewhere.
4370 -- For dispatching operations in the visible part, the usual
4371 -- rules apply, and operations with the same profile are not
4372 -- legal (B830001).
4374 elsif (In_Instance_Visible_Part
4375 and then not Is_Dispatching_Operation (E))
4376 or else In_Instance_Not_Visible
4377 then
4378 null;
4380 -- Here we have a real error (identical profile)
4382 else
4383 Error_Msg_Sloc := Sloc (E);
4385 -- Avoid cascaded errors if the entity appears in
4386 -- subsequent calls.
4388 Set_Scope (S, Current_Scope);
4390 Error_Msg_N ("& conflicts with declaration#", S);
4392 if Is_Generic_Instance (S)
4393 and then not Has_Completion (E)
4394 then
4395 Error_Msg_N
4396 ("\instantiation cannot provide body for it", S);
4397 end if;
4399 return;
4400 end if;
4402 else
4403 null;
4404 end if;
4406 Prev_Vis := E;
4407 E := Homonym (E);
4408 end loop;
4410 -- On exit, we know that S is a new entity
4412 Enter_Overloaded_Entity (S);
4413 Maybe_Primitive_Operation;
4415 -- If S is a derived operation for an untagged type then
4416 -- by definition it's not a dispatching operation (even
4417 -- if the parent operation was dispatching), so we don't
4418 -- call Check_Dispatching_Operation in that case.
4420 if not Present (Derived_Type)
4421 or else Is_Tagged_Type (Derived_Type)
4422 then
4423 Check_Dispatching_Operation (S, Empty);
4424 end if;
4425 end if;
4427 -- If this is a user-defined equality operator that is not
4428 -- a derived subprogram, create the corresponding inequality.
4429 -- If the operation is dispatching, the expansion is done
4430 -- elsewhere, and we do not create an explicit inequality
4431 -- operation.
4433 <<Check_Inequality>>
4434 if Chars (S) = Name_Op_Eq
4435 and then Etype (S) = Standard_Boolean
4436 and then Present (Parent (S))
4437 and then not Is_Dispatching_Operation (S)
4438 then
4439 Make_Inequality_Operator (S);
4440 end if;
4442 end New_Overloaded_Entity;
4444 ---------------------
4445 -- Process_Formals --
4446 ---------------------
4448 procedure Process_Formals
4449 (S : Entity_Id;
4450 T : List_Id;
4451 Related_Nod : Node_Id)
4453 Param_Spec : Node_Id;
4454 Formal : Entity_Id;
4455 Formal_Type : Entity_Id;
4456 Default : Node_Id;
4457 Ptype : Entity_Id;
4459 begin
4460 -- In order to prevent premature use of the formals in the same formal
4461 -- part, the Ekind is left undefined until all default expressions are
4462 -- analyzed. The Ekind is established in a separate loop at the end.
4464 Param_Spec := First (T);
4466 while Present (Param_Spec) loop
4468 Formal := Defining_Identifier (Param_Spec);
4469 Enter_Name (Formal);
4471 -- Case of ordinary parameters
4473 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
4474 Find_Type (Parameter_Type (Param_Spec));
4475 Ptype := Parameter_Type (Param_Spec);
4477 if Ptype = Error then
4478 goto Continue;
4479 end if;
4481 Formal_Type := Entity (Ptype);
4483 if Ekind (Formal_Type) = E_Incomplete_Type
4484 or else (Is_Class_Wide_Type (Formal_Type)
4485 and then Ekind (Root_Type (Formal_Type)) =
4486 E_Incomplete_Type)
4487 then
4488 if Nkind (Parent (T)) /= N_Access_Function_Definition
4489 and then Nkind (Parent (T)) /= N_Access_Procedure_Definition
4490 then
4491 Error_Msg_N ("invalid use of incomplete type", Param_Spec);
4492 end if;
4494 elsif Ekind (Formal_Type) = E_Void then
4495 Error_Msg_NE ("premature use of&",
4496 Parameter_Type (Param_Spec), Formal_Type);
4497 end if;
4499 -- An access formal type
4501 else
4502 Formal_Type :=
4503 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
4504 end if;
4506 Set_Etype (Formal, Formal_Type);
4508 Default := Expression (Param_Spec);
4510 if Present (Default) then
4511 if Out_Present (Param_Spec) then
4512 Error_Msg_N
4513 ("default initialization only allowed for IN parameters",
4514 Param_Spec);
4515 end if;
4517 -- Do the special preanalysis of the expression (see section on
4518 -- "Handling of Default Expressions" in the spec of package Sem).
4520 Analyze_Default_Expression (Default, Formal_Type);
4522 -- Check that the designated type of an access parameter's
4523 -- default is not a class-wide type unless the parameter's
4524 -- designated type is also class-wide.
4526 if Ekind (Formal_Type) = E_Anonymous_Access_Type
4527 and then Is_Class_Wide_Type (Designated_Type (Etype (Default)))
4528 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
4529 then
4530 Wrong_Type (Default, Formal_Type);
4531 end if;
4532 end if;
4534 <<Continue>>
4535 Next (Param_Spec);
4536 end loop;
4538 -- Now set the kind (mode) of each formal
4540 Param_Spec := First (T);
4542 while Present (Param_Spec) loop
4543 Formal := Defining_Identifier (Param_Spec);
4544 Set_Formal_Mode (Formal);
4546 if Ekind (Formal) = E_In_Parameter then
4547 Set_Default_Value (Formal, Expression (Param_Spec));
4549 if Present (Expression (Param_Spec)) then
4550 Default := Expression (Param_Spec);
4552 if Is_Scalar_Type (Etype (Default)) then
4553 if Nkind
4554 (Parameter_Type (Param_Spec)) /= N_Access_Definition
4555 then
4556 Formal_Type := Entity (Parameter_Type (Param_Spec));
4558 else
4559 Formal_Type := Access_Definition
4560 (Related_Nod, Parameter_Type (Param_Spec));
4561 end if;
4563 Apply_Scalar_Range_Check (Default, Formal_Type);
4564 end if;
4566 end if;
4567 end if;
4569 Next (Param_Spec);
4570 end loop;
4572 end Process_Formals;
4574 -------------------------
4575 -- Set_Actual_Subtypes --
4576 -------------------------
4578 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
4579 Loc : constant Source_Ptr := Sloc (N);
4580 Decl : Node_Id;
4581 Formal : Entity_Id;
4582 T : Entity_Id;
4583 First_Stmt : Node_Id := Empty;
4584 AS_Needed : Boolean;
4586 begin
4587 Formal := First_Formal (Subp);
4588 while Present (Formal) loop
4589 T := Etype (Formal);
4591 -- We never need an actual subtype for a constrained formal.
4593 if Is_Constrained (T) then
4594 AS_Needed := False;
4596 -- If we have unknown discriminants, then we do not need an
4597 -- actual subtype, or more accurately we cannot figure it out!
4598 -- Note that all class-wide types have unknown discriminants.
4600 elsif Has_Unknown_Discriminants (T) then
4601 AS_Needed := False;
4603 -- At this stage we have an unconstrained type that may need
4604 -- an actual subtype. For sure the actual subtype is needed
4605 -- if we have an unconstrained array type.
4607 elsif Is_Array_Type (T) then
4608 AS_Needed := True;
4610 -- The only other case which needs an actual subtype is an
4611 -- unconstrained record type which is an IN parameter (we
4612 -- cannot generate actual subtypes for the OUT or IN OUT case,
4613 -- since an assignment can change the discriminant values.
4614 -- However we exclude the case of initialization procedures,
4615 -- since discriminants are handled very specially in this context,
4616 -- see the section entitled "Handling of Discriminants" in Einfo.
4617 -- We also exclude the case of Discrim_SO_Functions (functions
4618 -- used in front end layout mode for size/offset values), since
4619 -- in such functions only discriminants are referenced, and not
4620 -- only are such subtypes not needed, but they cannot always
4621 -- be generated, because of order of elaboration issues.
4623 elsif Is_Record_Type (T)
4624 and then Ekind (Formal) = E_In_Parameter
4625 and then Chars (Formal) /= Name_uInit
4626 and then not Is_Discrim_SO_Function (Subp)
4627 then
4628 AS_Needed := True;
4630 -- All other cases do not need an actual subtype
4632 else
4633 AS_Needed := False;
4634 end if;
4636 -- Generate actual subtypes for unconstrained arrays and
4637 -- unconstrained discriminated records.
4639 if AS_Needed then
4640 Decl := Build_Actual_Subtype (T, Formal);
4642 if Nkind (N) = N_Accept_Statement then
4643 if Present (Handled_Statement_Sequence (N)) then
4644 First_Stmt :=
4645 First (Statements (Handled_Statement_Sequence (N)));
4646 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
4647 Mark_Rewrite_Insertion (Decl);
4648 else
4649 -- If the accept statement has no body, there will be
4650 -- no reference to the actuals, so no need to compute
4651 -- actual subtypes.
4653 return;
4654 end if;
4656 else
4657 Prepend (Decl, Declarations (N));
4658 Mark_Rewrite_Insertion (Decl);
4659 end if;
4661 Analyze (Decl);
4663 -- We need to freeze manually the generated type when it is
4664 -- inserted anywhere else than in a declarative part.
4666 if Present (First_Stmt) then
4667 Insert_List_Before_And_Analyze (First_Stmt,
4668 Freeze_Entity (Defining_Identifier (Decl), Loc));
4669 end if;
4671 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
4672 end if;
4674 Next_Formal (Formal);
4675 end loop;
4676 end Set_Actual_Subtypes;
4678 ---------------------
4679 -- Set_Formal_Mode --
4680 ---------------------
4682 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
4683 Spec : constant Node_Id := Parent (Formal_Id);
4685 begin
4686 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
4687 -- since we ensure that corresponding actuals are always valid at the
4688 -- point of the call.
4690 if Out_Present (Spec) then
4692 if Ekind (Scope (Formal_Id)) = E_Function
4693 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
4694 then
4695 Error_Msg_N ("functions can only have IN parameters", Spec);
4696 Set_Ekind (Formal_Id, E_In_Parameter);
4698 elsif In_Present (Spec) then
4699 Set_Ekind (Formal_Id, E_In_Out_Parameter);
4701 else
4702 Set_Ekind (Formal_Id, E_Out_Parameter);
4703 Set_Not_Source_Assigned (Formal_Id);
4704 end if;
4706 else
4707 Set_Ekind (Formal_Id, E_In_Parameter);
4708 end if;
4710 Set_Mechanism (Formal_Id, Default_Mechanism);
4711 Set_Formal_Validity (Formal_Id);
4712 end Set_Formal_Mode;
4714 -------------------------
4715 -- Set_Formal_Validity --
4716 -------------------------
4718 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
4719 begin
4720 -- If in full validity checking mode, then we can assume that
4721 -- an IN or IN OUT parameter is valid (see Exp_Ch5.Expand_Call)
4723 if not Validity_Checks_On then
4724 return;
4726 elsif Ekind (Formal_Id) = E_In_Parameter
4727 and then Validity_Check_In_Params
4728 then
4729 Set_Is_Known_Valid (Formal_Id, True);
4731 elsif Ekind (Formal_Id) = E_In_Out_Parameter
4732 and then Validity_Check_In_Out_Params
4733 then
4734 Set_Is_Known_Valid (Formal_Id, True);
4735 end if;
4736 end Set_Formal_Validity;
4738 ------------------------
4739 -- Subtype_Conformant --
4740 ------------------------
4742 function Subtype_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
4743 Result : Boolean;
4745 begin
4746 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result);
4747 return Result;
4748 end Subtype_Conformant;
4750 ---------------------
4751 -- Type_Conformant --
4752 ---------------------
4754 function Type_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
4755 Result : Boolean;
4757 begin
4758 Check_Conformance (New_Id, Old_Id, Type_Conformant, False, Result);
4759 return Result;
4760 end Type_Conformant;
4762 -------------------------------
4763 -- Valid_Operator_Definition --
4764 -------------------------------
4766 procedure Valid_Operator_Definition (Designator : Entity_Id) is
4767 N : Integer := 0;
4768 F : Entity_Id;
4769 Id : constant Name_Id := Chars (Designator);
4770 N_OK : Boolean;
4772 begin
4773 F := First_Formal (Designator);
4775 while Present (F) loop
4776 N := N + 1;
4778 if Present (Default_Value (F)) then
4779 Error_Msg_N
4780 ("default values not allowed for operator parameters",
4781 Parent (F));
4782 end if;
4784 Next_Formal (F);
4785 end loop;
4787 -- Verify that user-defined operators have proper number of arguments
4788 -- First case of operators which can only be unary
4790 if Id = Name_Op_Not
4791 or else Id = Name_Op_Abs
4792 then
4793 N_OK := (N = 1);
4795 -- Case of operators which can be unary or binary
4797 elsif Id = Name_Op_Add
4798 or Id = Name_Op_Subtract
4799 then
4800 N_OK := (N in 1 .. 2);
4802 -- All other operators can only be binary
4804 else
4805 N_OK := (N = 2);
4806 end if;
4808 if not N_OK then
4809 Error_Msg_N
4810 ("incorrect number of arguments for operator", Designator);
4811 end if;
4813 if Id = Name_Op_Ne
4814 and then Base_Type (Etype (Designator)) = Standard_Boolean
4815 and then not Is_Intrinsic_Subprogram (Designator)
4816 then
4817 Error_Msg_N
4818 ("explicit definition of inequality not allowed", Designator);
4819 end if;
4820 end Valid_Operator_Definition;
4822 end Sem_Ch6;