Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / ada / sem_ch6.adb
blob45a2015850746c3b423f3c933106ba0dcd908899
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2005, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch7; use Exp_Ch7;
35 with Fname; use Fname;
36 with Freeze; use Freeze;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
39 with Lib; use Lib;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Output; use Output;
44 with Rtsfind; use Rtsfind;
45 with Sem; use Sem;
46 with Sem_Cat; use Sem_Cat;
47 with Sem_Ch3; use Sem_Ch3;
48 with Sem_Ch4; use Sem_Ch4;
49 with Sem_Ch5; use Sem_Ch5;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch10; use Sem_Ch10;
52 with Sem_Ch12; use Sem_Ch12;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Dist; use Sem_Dist;
55 with Sem_Elim; use Sem_Elim;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Mech; use Sem_Mech;
58 with Sem_Prag; use Sem_Prag;
59 with Sem_Res; use Sem_Res;
60 with Sem_Util; use Sem_Util;
61 with Sem_Type; use Sem_Type;
62 with Sem_Warn; use Sem_Warn;
63 with Sinput; use Sinput;
64 with Stand; use Stand;
65 with Sinfo; use Sinfo;
66 with Sinfo.CN; use Sinfo.CN;
67 with Snames; use Snames;
68 with Stringt; use Stringt;
69 with Style;
70 with Stylesw; use Stylesw;
71 with Tbuild; use Tbuild;
72 with Uintp; use Uintp;
73 with Urealp; use Urealp;
74 with Validsw; use Validsw;
76 package body Sem_Ch6 is
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Analyze_Return_Type (N : Node_Id);
83 -- Subsidiary to Process_Formals: analyze subtype mark in function
84 -- specification, in a context where the formals are visible and hide
85 -- outer homographs.
87 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
88 -- Analyze a generic subprogram body. N is the body to be analyzed, and
89 -- Gen_Id is the defining entity Id for the corresponding spec.
91 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
92 -- If a subprogram has pragma Inline and inlining is active, use generic
93 -- machinery to build an unexpanded body for the subprogram. This body is
94 -- subsequenty used for inline expansions at call sites. If subprogram can
95 -- be inlined (depending on size and nature of local declarations) this
96 -- function returns true. Otherwise subprogram body is treated normally.
97 -- If proper warnings are enabled and the subprogram contains a construct
98 -- that cannot be inlined, the offending construct is flagged accordingly.
100 type Conformance_Type is
101 (Type_Conformant, Mode_Conformant, Subtype_Conformant, Fully_Conformant);
102 -- Conformance type used for following call, meaning matches the
103 -- RM definitions of the corresponding terms.
105 procedure Check_Conformance
106 (New_Id : Entity_Id;
107 Old_Id : Entity_Id;
108 Ctype : Conformance_Type;
109 Errmsg : Boolean;
110 Conforms : out Boolean;
111 Err_Loc : Node_Id := Empty;
112 Get_Inst : Boolean := False);
113 -- Given two entities, this procedure checks that the profiles associated
114 -- with these entities meet the conformance criterion given by the third
115 -- parameter. If they conform, Conforms is set True and control returns
116 -- to the caller. If they do not conform, Conforms is set to False, and
117 -- in addition, if Errmsg is True on the call, proper messages are output
118 -- to complain about the conformance failure. If Err_Loc is non_Empty
119 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
120 -- error messages are placed on the appropriate part of the construct
121 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
122 -- against a formal access-to-subprogram type so Get_Instance_Of must
123 -- be called.
125 procedure Check_Overriding_Operation
126 (N : Node_Id;
127 Subp : Entity_Id);
128 -- Check that a subprogram with a pragma Overriding or Optional_Overriding
129 -- is legal. This check is performed here rather than in Sem_Prag because
130 -- the pragma must follow immediately the declaration, and can be treated
131 -- as part of the declaration itself, as described in AI-218.
133 procedure Check_Subprogram_Order (N : Node_Id);
134 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
135 -- the alpha ordering rule for N if this ordering requirement applicable.
137 procedure Check_Returns
138 (HSS : Node_Id;
139 Mode : Character;
140 Err : out Boolean);
141 -- Called to check for missing return statements in a function body, or
142 -- for returns present in a procedure body which has No_Return set. L is
143 -- the handled statement sequence for the subprogram body. This procedure
144 -- checks all flow paths to make sure they either have return (Mode = 'F')
145 -- or do not have a return (Mode = 'P'). The flag Err is set if there are
146 -- any control paths not explicitly terminated by a return in the function
147 -- case, and is True otherwise.
149 function Conforming_Types
150 (T1 : Entity_Id;
151 T2 : Entity_Id;
152 Ctype : Conformance_Type;
153 Get_Inst : Boolean := False) return Boolean;
154 -- Check that two formal parameter types conform, checking both for
155 -- equality of base types, and where required statically matching
156 -- subtypes, depending on the setting of Ctype.
158 procedure Enter_Overloaded_Entity (S : Entity_Id);
159 -- This procedure makes S, a new overloaded entity, into the first visible
160 -- entity with that name.
162 procedure Install_Entity (E : Entity_Id);
163 -- Make single entity visible. Used for generic formals as well
165 procedure Install_Formals (Id : Entity_Id);
166 -- On entry to a subprogram body, make the formals visible. Note that
167 -- simply placing the subprogram on the scope stack is not sufficient:
168 -- the formals must become the current entities for their names.
170 function Is_Non_Overriding_Operation
171 (Prev_E : Entity_Id;
172 New_E : Entity_Id) return Boolean;
173 -- Enforce the rule given in 12.3(18): a private operation in an instance
174 -- overrides an inherited operation only if the corresponding operation
175 -- was overriding in the generic. This can happen for primitive operations
176 -- of types derived (in the generic unit) from formal private or formal
177 -- derived types.
179 procedure Make_Inequality_Operator (S : Entity_Id);
180 -- Create the declaration for an inequality operator that is implicitly
181 -- created by a user-defined equality operator that yields a boolean.
183 procedure May_Need_Actuals (Fun : Entity_Id);
184 -- Flag functions that can be called without parameters, i.e. those that
185 -- have no parameters, or those for which defaults exist for all parameters
187 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id);
188 -- If there is a separate spec for a subprogram or generic subprogram, the
189 -- formals of the body are treated as references to the corresponding
190 -- formals of the spec. This reference does not count as an actual use of
191 -- the formal, in order to diagnose formals that are unused in the body.
193 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
194 -- Formal_Id is an formal parameter entity. This procedure deals with
195 -- setting the proper validity status for this entity, which depends
196 -- on the kind of parameter and the validity checking mode.
198 ---------------------------------------------
199 -- Analyze_Abstract_Subprogram_Declaration --
200 ---------------------------------------------
202 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
203 Designator : constant Entity_Id :=
204 Analyze_Subprogram_Specification (Specification (N));
205 Scop : constant Entity_Id := Current_Scope;
207 begin
208 Generate_Definition (Designator);
209 Set_Is_Abstract (Designator);
210 New_Overloaded_Entity (Designator);
211 Check_Delayed_Subprogram (Designator);
213 Set_Categorization_From_Scope (Designator, Scop);
215 if Ekind (Scope (Designator)) = E_Protected_Type then
216 Error_Msg_N
217 ("abstract subprogram not allowed in protected type", N);
218 end if;
220 Generate_Reference_To_Formals (Designator);
221 end Analyze_Abstract_Subprogram_Declaration;
223 ----------------------------
224 -- Analyze_Function_Call --
225 ----------------------------
227 procedure Analyze_Function_Call (N : Node_Id) is
228 P : constant Node_Id := Name (N);
229 L : constant List_Id := Parameter_Associations (N);
230 Actual : Node_Id;
232 begin
233 Analyze (P);
235 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
236 -- as B(A, X). If the rewriting is successful, the call has been
237 -- analyzed and we just return.
239 if Nkind (P) = N_Selected_Component
240 and then Name (N) /= P
241 and then Is_Rewrite_Substitution (N)
242 and then Present (Etype (N))
243 then
244 return;
245 end if;
247 -- If error analyzing name, then set Any_Type as result type and return
249 if Etype (P) = Any_Type then
250 Set_Etype (N, Any_Type);
251 return;
252 end if;
254 -- Otherwise analyze the parameters
256 if Present (L) then
257 Actual := First (L);
259 while Present (Actual) loop
260 Analyze (Actual);
261 Check_Parameterless_Call (Actual);
262 Next (Actual);
263 end loop;
264 end if;
266 Analyze_Call (N);
267 end Analyze_Function_Call;
269 -------------------------------------
270 -- Analyze_Generic_Subprogram_Body --
271 -------------------------------------
273 procedure Analyze_Generic_Subprogram_Body
274 (N : Node_Id;
275 Gen_Id : Entity_Id)
277 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
278 Kind : constant Entity_Kind := Ekind (Gen_Id);
279 Body_Id : Entity_Id;
280 New_N : Node_Id;
281 Spec : Node_Id;
283 begin
284 -- Copy body and disable expansion while analyzing the generic For a
285 -- stub, do not copy the stub (which would load the proper body), this
286 -- will be done when the proper body is analyzed.
288 if Nkind (N) /= N_Subprogram_Body_Stub then
289 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
290 Rewrite (N, New_N);
291 Start_Generic;
292 end if;
294 Spec := Specification (N);
296 -- Within the body of the generic, the subprogram is callable, and
297 -- behaves like the corresponding non-generic unit.
299 Body_Id := Defining_Entity (Spec);
301 if Kind = E_Generic_Procedure
302 and then Nkind (Spec) /= N_Procedure_Specification
303 then
304 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
305 return;
307 elsif Kind = E_Generic_Function
308 and then Nkind (Spec) /= N_Function_Specification
309 then
310 Error_Msg_N ("invalid body for generic function ", Body_Id);
311 return;
312 end if;
314 Set_Corresponding_Body (Gen_Decl, Body_Id);
316 if Has_Completion (Gen_Id)
317 and then Nkind (Parent (N)) /= N_Subunit
318 then
319 Error_Msg_N ("duplicate generic body", N);
320 return;
321 else
322 Set_Has_Completion (Gen_Id);
323 end if;
325 if Nkind (N) = N_Subprogram_Body_Stub then
326 Set_Ekind (Defining_Entity (Specification (N)), Kind);
327 else
328 Set_Corresponding_Spec (N, Gen_Id);
329 end if;
331 if Nkind (Parent (N)) = N_Compilation_Unit then
332 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
333 end if;
335 -- Make generic parameters immediately visible in the body. They are
336 -- needed to process the formals declarations. Then make the formals
337 -- visible in a separate step.
339 New_Scope (Gen_Id);
341 declare
342 E : Entity_Id;
343 First_Ent : Entity_Id;
345 begin
346 First_Ent := First_Entity (Gen_Id);
348 E := First_Ent;
349 while Present (E) and then not Is_Formal (E) loop
350 Install_Entity (E);
351 Next_Entity (E);
352 end loop;
354 Set_Use (Generic_Formal_Declarations (Gen_Decl));
356 -- Now generic formals are visible, and the specification can be
357 -- analyzed, for subsequent conformance check.
359 Body_Id := Analyze_Subprogram_Specification (Spec);
361 -- Make formal parameters visible
363 if Present (E) then
365 -- E is the first formal parameter, we loop through the formals
366 -- installing them so that they will be visible.
368 Set_First_Entity (Gen_Id, E);
369 while Present (E) loop
370 Install_Entity (E);
371 Next_Formal (E);
372 end loop;
373 end if;
375 -- Visible generic entity is callable within its own body
377 Set_Ekind (Gen_Id, Ekind (Body_Id));
378 Set_Ekind (Body_Id, E_Subprogram_Body);
379 Set_Convention (Body_Id, Convention (Gen_Id));
380 Set_Scope (Body_Id, Scope (Gen_Id));
381 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
383 if Nkind (N) = N_Subprogram_Body_Stub then
385 -- No body to analyze, so restore state of generic unit
387 Set_Ekind (Gen_Id, Kind);
388 Set_Ekind (Body_Id, Kind);
390 if Present (First_Ent) then
391 Set_First_Entity (Gen_Id, First_Ent);
392 end if;
394 End_Scope;
395 return;
396 end if;
398 -- If this is a compilation unit, it must be made visible explicitly,
399 -- because the compilation of the declaration, unlike other library
400 -- unit declarations, does not. If it is not a unit, the following
401 -- is redundant but harmless.
403 Set_Is_Immediately_Visible (Gen_Id);
404 Reference_Body_Formals (Gen_Id, Body_Id);
406 Set_Actual_Subtypes (N, Current_Scope);
407 Analyze_Declarations (Declarations (N));
408 Check_Completion;
409 Analyze (Handled_Statement_Sequence (N));
411 Save_Global_References (Original_Node (N));
413 -- Prior to exiting the scope, include generic formals again (if any
414 -- are present) in the set of local entities.
416 if Present (First_Ent) then
417 Set_First_Entity (Gen_Id, First_Ent);
418 end if;
420 Check_References (Gen_Id);
421 end;
423 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
424 End_Scope;
425 Check_Subprogram_Order (N);
427 -- Outside of its body, unit is generic again
429 Set_Ekind (Gen_Id, Kind);
430 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
431 Style.Check_Identifier (Body_Id, Gen_Id);
432 End_Generic;
433 end Analyze_Generic_Subprogram_Body;
435 -----------------------------
436 -- Analyze_Operator_Symbol --
437 -----------------------------
439 -- An operator symbol such as "+" or "and" may appear in context where the
440 -- literal denotes an entity name, such as "+"(x, y) or in context when it
441 -- is just a string, as in (conjunction = "or"). In these cases the parser
442 -- generates this node, and the semantics does the disambiguation. Other
443 -- such case are actuals in an instantiation, the generic unit in an
444 -- instantiation, and pragma arguments.
446 procedure Analyze_Operator_Symbol (N : Node_Id) is
447 Par : constant Node_Id := Parent (N);
449 begin
450 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
451 or else Nkind (Par) = N_Function_Instantiation
452 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
453 or else (Nkind (Par) = N_Pragma_Argument_Association
454 and then not Is_Pragma_String_Literal (Par))
455 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
456 or else (Nkind (Par) = N_Attribute_Reference
457 and then Attribute_Name (Par) /= Name_Value)
458 then
459 Find_Direct_Name (N);
461 else
462 Change_Operator_Symbol_To_String_Literal (N);
463 Analyze (N);
464 end if;
465 end Analyze_Operator_Symbol;
467 -----------------------------------
468 -- Analyze_Parameter_Association --
469 -----------------------------------
471 procedure Analyze_Parameter_Association (N : Node_Id) is
472 begin
473 Analyze (Explicit_Actual_Parameter (N));
474 end Analyze_Parameter_Association;
476 ----------------------------
477 -- Analyze_Procedure_Call --
478 ----------------------------
480 procedure Analyze_Procedure_Call (N : Node_Id) is
481 Loc : constant Source_Ptr := Sloc (N);
482 P : constant Node_Id := Name (N);
483 Actuals : constant List_Id := Parameter_Associations (N);
484 Actual : Node_Id;
485 New_N : Node_Id;
487 procedure Analyze_Call_And_Resolve;
488 -- Do Analyze and Resolve calls for procedure call
490 ------------------------------
491 -- Analyze_Call_And_Resolve --
492 ------------------------------
494 procedure Analyze_Call_And_Resolve is
495 begin
496 if Nkind (N) = N_Procedure_Call_Statement then
497 Analyze_Call (N);
498 Resolve (N, Standard_Void_Type);
499 else
500 Analyze (N);
501 end if;
502 end Analyze_Call_And_Resolve;
504 -- Start of processing for Analyze_Procedure_Call
506 begin
507 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
508 -- a procedure call or an entry call. The prefix may denote an access
509 -- to subprogram type, in which case an implicit dereference applies.
510 -- If the prefix is an indexed component (without implicit defererence)
511 -- then the construct denotes a call to a member of an entire family.
512 -- If the prefix is a simple name, it may still denote a call to a
513 -- parameterless member of an entry family. Resolution of these various
514 -- interpretations is delicate.
516 Analyze (P);
518 -- If error analyzing prefix, then set Any_Type as result and return
520 if Etype (P) = Any_Type then
521 Set_Etype (N, Any_Type);
522 return;
523 end if;
525 -- Otherwise analyze the parameters
527 if Present (Actuals) then
528 Actual := First (Actuals);
530 while Present (Actual) loop
531 Analyze (Actual);
532 Check_Parameterless_Call (Actual);
533 Next (Actual);
534 end loop;
535 end if;
537 -- Special processing for Elab_Spec and Elab_Body calls
539 if Nkind (P) = N_Attribute_Reference
540 and then (Attribute_Name (P) = Name_Elab_Spec
541 or else Attribute_Name (P) = Name_Elab_Body)
542 then
543 if Present (Actuals) then
544 Error_Msg_N
545 ("no parameters allowed for this call", First (Actuals));
546 return;
547 end if;
549 Set_Etype (N, Standard_Void_Type);
550 Set_Analyzed (N);
552 elsif Is_Entity_Name (P)
553 and then Is_Record_Type (Etype (Entity (P)))
554 and then Remote_AST_I_Dereference (P)
555 then
556 return;
558 elsif Is_Entity_Name (P)
559 and then Ekind (Entity (P)) /= E_Entry_Family
560 then
561 if Is_Access_Type (Etype (P))
562 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
563 and then No (Actuals)
564 and then Comes_From_Source (N)
565 then
566 Error_Msg_N ("missing explicit dereference in call", N);
567 end if;
569 Analyze_Call_And_Resolve;
571 -- If the prefix is the simple name of an entry family, this is
572 -- a parameterless call from within the task body itself.
574 elsif Is_Entity_Name (P)
575 and then Nkind (P) = N_Identifier
576 and then Ekind (Entity (P)) = E_Entry_Family
577 and then Present (Actuals)
578 and then No (Next (First (Actuals)))
579 then
580 -- Can be call to parameterless entry family. What appears to be the
581 -- sole argument is in fact the entry index. Rewrite prefix of node
582 -- accordingly. Source representation is unchanged by this
583 -- transformation.
585 New_N :=
586 Make_Indexed_Component (Loc,
587 Prefix =>
588 Make_Selected_Component (Loc,
589 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
590 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
591 Expressions => Actuals);
592 Set_Name (N, New_N);
593 Set_Etype (New_N, Standard_Void_Type);
594 Set_Parameter_Associations (N, No_List);
595 Analyze_Call_And_Resolve;
597 elsif Nkind (P) = N_Explicit_Dereference then
598 if Ekind (Etype (P)) = E_Subprogram_Type then
599 Analyze_Call_And_Resolve;
600 else
601 Error_Msg_N ("expect access to procedure in call", P);
602 end if;
604 -- The name can be a selected component or an indexed component that
605 -- yields an access to subprogram. Such a prefix is legal if the call
606 -- has parameter associations.
608 elsif Is_Access_Type (Etype (P))
609 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
610 then
611 if Present (Actuals) then
612 Analyze_Call_And_Resolve;
613 else
614 Error_Msg_N ("missing explicit dereference in call ", N);
615 end if;
617 -- If not an access to subprogram, then the prefix must resolve to the
618 -- name of an entry, entry family, or protected operation.
620 -- For the case of a simple entry call, P is a selected component where
621 -- the prefix is the task and the selector name is the entry. A call to
622 -- a protected procedure will have the same syntax. If the protected
623 -- object contains overloaded operations, the entity may appear as a
624 -- function, the context will select the operation whose type is Void.
626 elsif Nkind (P) = N_Selected_Component
627 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
628 or else
629 Ekind (Entity (Selector_Name (P))) = E_Procedure
630 or else
631 Ekind (Entity (Selector_Name (P))) = E_Function)
632 then
633 Analyze_Call_And_Resolve;
635 elsif Nkind (P) = N_Selected_Component
636 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
637 and then Present (Actuals)
638 and then No (Next (First (Actuals)))
639 then
640 -- Can be call to parameterless entry family. What appears to be the
641 -- sole argument is in fact the entry index. Rewrite prefix of node
642 -- accordingly. Source representation is unchanged by this
643 -- transformation.
645 New_N :=
646 Make_Indexed_Component (Loc,
647 Prefix => New_Copy (P),
648 Expressions => Actuals);
649 Set_Name (N, New_N);
650 Set_Etype (New_N, Standard_Void_Type);
651 Set_Parameter_Associations (N, No_List);
652 Analyze_Call_And_Resolve;
654 -- For the case of a reference to an element of an entry family, P is
655 -- an indexed component whose prefix is a selected component (task and
656 -- entry family), and whose index is the entry family index.
658 elsif Nkind (P) = N_Indexed_Component
659 and then Nkind (Prefix (P)) = N_Selected_Component
660 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
661 then
662 Analyze_Call_And_Resolve;
664 -- If the prefix is the name of an entry family, it is a call from
665 -- within the task body itself.
667 elsif Nkind (P) = N_Indexed_Component
668 and then Nkind (Prefix (P)) = N_Identifier
669 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
670 then
671 New_N :=
672 Make_Selected_Component (Loc,
673 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
674 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
675 Rewrite (Prefix (P), New_N);
676 Analyze (P);
677 Analyze_Call_And_Resolve;
679 -- Anything else is an error
681 else
682 Error_Msg_N ("Invalid procedure or entry call", N);
683 end if;
684 end Analyze_Procedure_Call;
686 ------------------------------
687 -- Analyze_Return_Statement --
688 ------------------------------
690 procedure Analyze_Return_Statement (N : Node_Id) is
691 Loc : constant Source_Ptr := Sloc (N);
692 Expr : Node_Id;
693 Scope_Id : Entity_Id;
694 Kind : Entity_Kind;
695 R_Type : Entity_Id;
697 begin
698 -- Find subprogram or accept statement enclosing the return statement
700 Scope_Id := Empty;
701 for J in reverse 0 .. Scope_Stack.Last loop
702 Scope_Id := Scope_Stack.Table (J).Entity;
703 exit when Ekind (Scope_Id) /= E_Block and then
704 Ekind (Scope_Id) /= E_Loop;
705 end loop;
707 pragma Assert (Present (Scope_Id));
709 Kind := Ekind (Scope_Id);
710 Expr := Expression (N);
712 if Kind /= E_Function
713 and then Kind /= E_Generic_Function
714 and then Kind /= E_Procedure
715 and then Kind /= E_Generic_Procedure
716 and then Kind /= E_Entry
717 and then Kind /= E_Entry_Family
718 then
719 Error_Msg_N ("illegal context for return statement", N);
721 elsif Present (Expr) then
722 if Kind = E_Function or else Kind = E_Generic_Function then
723 Set_Return_Present (Scope_Id);
724 R_Type := Etype (Scope_Id);
725 Set_Return_Type (N, R_Type);
726 Analyze_And_Resolve (Expr, R_Type);
728 if (Is_Class_Wide_Type (Etype (Expr))
729 or else Is_Dynamically_Tagged (Expr))
730 and then not Is_Class_Wide_Type (R_Type)
731 then
732 Error_Msg_N
733 ("dynamically tagged expression not allowed!", Expr);
734 end if;
736 Apply_Constraint_Check (Expr, R_Type);
738 -- ??? A real run-time accessibility check is needed in cases
739 -- involving dereferences of access parameters. For now we just
740 -- check the static cases.
742 if Is_Return_By_Reference_Type (Etype (Scope_Id))
743 and then Object_Access_Level (Expr)
744 > Subprogram_Access_Level (Scope_Id)
745 then
746 Rewrite (N,
747 Make_Raise_Program_Error (Loc,
748 Reason => PE_Accessibility_Check_Failed));
749 Analyze (N);
751 Error_Msg_N
752 ("cannot return a local value by reference?", N);
753 Error_Msg_NE
754 ("& will be raised at run time?!",
755 N, Standard_Program_Error);
756 end if;
758 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
759 Error_Msg_N ("procedure cannot return value (use function)", N);
761 else
762 Error_Msg_N ("accept statement cannot return value", N);
763 end if;
765 -- No expression present
767 else
768 if Kind = E_Function or Kind = E_Generic_Function then
769 Error_Msg_N ("missing expression in return from function", N);
770 end if;
772 if (Ekind (Scope_Id) = E_Procedure
773 or else Ekind (Scope_Id) = E_Generic_Procedure)
774 and then No_Return (Scope_Id)
775 then
776 Error_Msg_N
777 ("RETURN statement not allowed (No_Return)", N);
778 end if;
779 end if;
781 Check_Unreachable_Code (N);
782 end Analyze_Return_Statement;
784 -------------------------
785 -- Analyze_Return_Type --
786 -------------------------
788 procedure Analyze_Return_Type (N : Node_Id) is
789 Designator : constant Entity_Id := Defining_Entity (N);
790 Typ : Entity_Id := Empty;
792 begin
793 if Subtype_Mark (N) /= Error then
794 Find_Type (Subtype_Mark (N));
795 Typ := Entity (Subtype_Mark (N));
796 Set_Etype (Designator, Typ);
798 if Ekind (Typ) = E_Incomplete_Type
799 or else (Is_Class_Wide_Type (Typ)
800 and then
801 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
802 then
803 Error_Msg_N
804 ("invalid use of incomplete type", Subtype_Mark (N));
805 end if;
807 else
808 Set_Etype (Designator, Any_Type);
809 end if;
810 end Analyze_Return_Type;
812 -----------------------------
813 -- Analyze_Subprogram_Body --
814 -----------------------------
816 -- This procedure is called for regular subprogram bodies, generic bodies,
817 -- and for subprogram stubs of both kinds. In the case of stubs, only the
818 -- specification matters, and is used to create a proper declaration for
819 -- the subprogram, or to perform conformance checks.
821 procedure Analyze_Subprogram_Body (N : Node_Id) is
822 Loc : constant Source_Ptr := Sloc (N);
823 Body_Spec : constant Node_Id := Specification (N);
824 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
825 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
826 Body_Deleted : constant Boolean := False;
828 HSS : Node_Id;
829 Spec_Id : Entity_Id;
830 Spec_Decl : Node_Id := Empty;
831 Last_Formal : Entity_Id := Empty;
832 Conformant : Boolean;
833 Missing_Ret : Boolean;
834 P_Ent : Entity_Id;
836 procedure Check_Following_Pragma;
837 -- If front-end inlining is enabled, look ahead to recognize a pragma
838 -- that may appear after the body.
840 procedure Check_Following_Pragma is
841 Prag : Node_Id;
843 begin
844 if Front_End_Inlining
845 and then Is_List_Member (N)
846 and then Present (Spec_Decl)
847 and then List_Containing (N) = List_Containing (Spec_Decl)
848 then
849 Prag := Next (N);
851 if Present (Prag)
852 and then Nkind (Prag) = N_Pragma
853 and then Get_Pragma_Id (Chars (Prag)) = Pragma_Inline
854 and then
855 Chars
856 (Expression (First (Pragma_Argument_Associations (Prag))))
857 = Chars (Body_Id)
858 then
859 Analyze (Prag);
860 end if;
861 end if;
862 end Check_Following_Pragma;
864 -- Start of processing for Analyze_Subprogram_Body
866 begin
867 if Debug_Flag_C then
868 Write_Str ("==== Compiling subprogram body ");
869 Write_Name (Chars (Body_Id));
870 Write_Str (" from ");
871 Write_Location (Loc);
872 Write_Eol;
873 end if;
875 Trace_Scope (N, Body_Id, " Analyze subprogram");
877 -- Generic subprograms are handled separately. They always have a
878 -- generic specification. Determine whether current scope has a
879 -- previous declaration.
881 -- If the subprogram body is defined within an instance of the same
882 -- name, the instance appears as a package renaming, and will be hidden
883 -- within the subprogram.
885 if Present (Prev_Id)
886 and then not Is_Overloadable (Prev_Id)
887 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
888 or else Comes_From_Source (Prev_Id))
889 then
890 if Is_Generic_Subprogram (Prev_Id) then
891 Spec_Id := Prev_Id;
892 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
893 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
895 Analyze_Generic_Subprogram_Body (N, Spec_Id);
896 return;
898 else
899 -- Previous entity conflicts with subprogram name. Attempting to
900 -- enter name will post error.
902 Enter_Name (Body_Id);
903 return;
904 end if;
906 -- Non-generic case, find the subprogram declaration, if one was seen,
907 -- or enter new overloaded entity in the current scope. If the
908 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
909 -- part of the context of one of its subunits. No need to redo the
910 -- analysis.
912 elsif Prev_Id = Body_Id
913 and then Has_Completion (Body_Id)
914 then
915 return;
917 else
918 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
920 if Nkind (N) = N_Subprogram_Body_Stub
921 or else No (Corresponding_Spec (N))
922 then
923 Spec_Id := Find_Corresponding_Spec (N);
925 -- If this is a duplicate body, no point in analyzing it
927 if Error_Posted (N) then
928 return;
929 end if;
931 -- A subprogram body should cause freezing of its own declaration,
932 -- but if there was no previous explicit declaration, then the
933 -- subprogram will get frozen too late (there may be code within
934 -- the body that depends on the subprogram having been frozen,
935 -- such as uses of extra formals), so we force it to be frozen
936 -- here. Same holds if the body and the spec are compilation
937 -- units.
939 if No (Spec_Id) then
940 Freeze_Before (N, Body_Id);
942 elsif Nkind (Parent (N)) = N_Compilation_Unit then
943 Freeze_Before (N, Spec_Id);
944 end if;
945 else
946 Spec_Id := Corresponding_Spec (N);
947 end if;
948 end if;
950 -- Do not inline any subprogram that contains nested subprograms, since
951 -- the backend inlining circuit seems to generate uninitialized
952 -- references in this case. We know this happens in the case of front
953 -- end ZCX support, but it also appears it can happen in other cases as
954 -- well. The backend often rejects attempts to inline in the case of
955 -- nested procedures anyway, so little if anything is lost by this.
956 -- Note that this is test is for the benefit of the back-end. There is
957 -- a separate test for front-end inlining that also rejects nested
958 -- subprograms.
960 -- Do not do this test if errors have been detected, because in some
961 -- error cases, this code blows up, and we don't need it anyway if
962 -- there have been errors, since we won't get to the linker anyway.
964 if Comes_From_Source (Body_Id)
965 and then Serious_Errors_Detected = 0
966 then
967 P_Ent := Body_Id;
968 loop
969 P_Ent := Scope (P_Ent);
970 exit when No (P_Ent) or else P_Ent = Standard_Standard;
972 if Is_Subprogram (P_Ent) then
973 Set_Is_Inlined (P_Ent, False);
975 if Comes_From_Source (P_Ent)
976 and then Has_Pragma_Inline (P_Ent)
977 then
978 Cannot_Inline
979 ("cannot inline& (nested subprogram)?",
980 N, P_Ent);
981 end if;
982 end if;
983 end loop;
984 end if;
986 -- Case of fully private operation in the body of the protected type.
987 -- We must create a declaration for the subprogram, in order to attach
988 -- the protected subprogram that will be used in internal calls.
990 if No (Spec_Id)
991 and then Comes_From_Source (N)
992 and then Is_Protected_Type (Current_Scope)
993 then
994 declare
995 Decl : Node_Id;
996 Plist : List_Id;
997 Formal : Entity_Id;
998 New_Spec : Node_Id;
1000 begin
1001 Formal := First_Formal (Body_Id);
1003 -- The protected operation always has at least one formal, namely
1004 -- the object itself, but it is only placed in the parameter list
1005 -- if expansion is enabled.
1007 if Present (Formal)
1008 or else Expander_Active
1009 then
1010 Plist := New_List;
1012 else
1013 Plist := No_List;
1014 end if;
1016 while Present (Formal) loop
1017 Append
1018 (Make_Parameter_Specification (Loc,
1019 Defining_Identifier =>
1020 Make_Defining_Identifier (Sloc (Formal),
1021 Chars => Chars (Formal)),
1022 In_Present => In_Present (Parent (Formal)),
1023 Out_Present => Out_Present (Parent (Formal)),
1024 Parameter_Type =>
1025 New_Reference_To (Etype (Formal), Loc),
1026 Expression =>
1027 New_Copy_Tree (Expression (Parent (Formal)))),
1028 Plist);
1030 Next_Formal (Formal);
1031 end loop;
1033 if Nkind (Body_Spec) = N_Procedure_Specification then
1034 New_Spec :=
1035 Make_Procedure_Specification (Loc,
1036 Defining_Unit_Name =>
1037 Make_Defining_Identifier (Sloc (Body_Id),
1038 Chars => Chars (Body_Id)),
1039 Parameter_Specifications => Plist);
1040 else
1041 New_Spec :=
1042 Make_Function_Specification (Loc,
1043 Defining_Unit_Name =>
1044 Make_Defining_Identifier (Sloc (Body_Id),
1045 Chars => Chars (Body_Id)),
1046 Parameter_Specifications => Plist,
1047 Subtype_Mark => New_Occurrence_Of (Etype (Body_Id), Loc));
1048 end if;
1050 Decl :=
1051 Make_Subprogram_Declaration (Loc,
1052 Specification => New_Spec);
1053 Insert_Before (N, Decl);
1054 Spec_Id := Defining_Unit_Name (New_Spec);
1056 -- Indicate that the entity comes from source, to ensure that
1057 -- cross-reference information is properly generated. The body
1058 -- itself is rewritten during expansion, and the body entity will
1059 -- not appear in calls to the operation.
1061 Set_Comes_From_Source (Spec_Id, True);
1062 Analyze (Decl);
1063 Set_Has_Completion (Spec_Id);
1064 Set_Convention (Spec_Id, Convention_Protected);
1065 end;
1067 elsif Present (Spec_Id) then
1068 Spec_Decl := Unit_Declaration_Node (Spec_Id);
1069 end if;
1071 -- Place subprogram on scope stack, and make formals visible. If there
1072 -- is a spec, the visible entity remains that of the spec.
1074 if Present (Spec_Id) then
1075 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1076 if Style_Check then
1077 Style.Check_Identifier (Body_Id, Spec_Id);
1078 end if;
1080 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1081 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1083 if Is_Abstract (Spec_Id) then
1084 Error_Msg_N ("an abstract subprogram cannot have a body", N);
1085 return;
1086 else
1087 Set_Convention (Body_Id, Convention (Spec_Id));
1088 Set_Has_Completion (Spec_Id);
1090 if Is_Protected_Type (Scope (Spec_Id)) then
1091 Set_Privals_Chain (Spec_Id, New_Elmt_List);
1092 end if;
1094 -- If this is a body generated for a renaming, do not check for
1095 -- full conformance. The check is redundant, because the spec of
1096 -- the body is a copy of the spec in the renaming declaration,
1097 -- and the test can lead to spurious errors on nested defaults.
1099 if Present (Spec_Decl)
1100 and then not Comes_From_Source (N)
1101 and then
1102 (Nkind (Original_Node (Spec_Decl)) =
1103 N_Subprogram_Renaming_Declaration
1104 or else (Present (Corresponding_Body (Spec_Decl))
1105 and then
1106 Nkind (Unit_Declaration_Node
1107 (Corresponding_Body (Spec_Decl))) =
1108 N_Subprogram_Renaming_Declaration))
1109 then
1110 Conformant := True;
1111 else
1112 Check_Conformance
1113 (Body_Id, Spec_Id,
1114 Fully_Conformant, True, Conformant, Body_Id);
1115 end if;
1117 -- If the body is not fully conformant, we have to decide if we
1118 -- should analyze it or not. If it has a really messed up profile
1119 -- then we probably should not analyze it, since we will get too
1120 -- many bogus messages.
1122 -- Our decision is to go ahead in the non-fully conformant case
1123 -- only if it is at least mode conformant with the spec. Note
1124 -- that the call to Check_Fully_Conformant has issued the proper
1125 -- error messages to complain about the lack of conformance.
1127 if not Conformant
1128 and then not Mode_Conformant (Body_Id, Spec_Id)
1129 then
1130 return;
1131 end if;
1132 end if;
1134 if Spec_Id /= Body_Id then
1135 Reference_Body_Formals (Spec_Id, Body_Id);
1136 end if;
1138 if Nkind (N) /= N_Subprogram_Body_Stub then
1139 Set_Corresponding_Spec (N, Spec_Id);
1140 Install_Formals (Spec_Id);
1141 Last_Formal := Last_Entity (Spec_Id);
1142 New_Scope (Spec_Id);
1144 -- Make sure that the subprogram is immediately visible. For
1145 -- child units that have no separate spec this is indispensable.
1146 -- Otherwise it is safe albeit redundant.
1148 Set_Is_Immediately_Visible (Spec_Id);
1149 end if;
1151 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
1152 Set_Ekind (Body_Id, E_Subprogram_Body);
1153 Set_Scope (Body_Id, Scope (Spec_Id));
1155 -- Case of subprogram body with no previous spec
1157 else
1158 if Style_Check
1159 and then Comes_From_Source (Body_Id)
1160 and then not Suppress_Style_Checks (Body_Id)
1161 and then not In_Instance
1162 then
1163 Style.Body_With_No_Spec (N);
1164 end if;
1166 New_Overloaded_Entity (Body_Id);
1168 if Nkind (N) /= N_Subprogram_Body_Stub then
1169 Set_Acts_As_Spec (N);
1170 Generate_Definition (Body_Id);
1171 Generate_Reference
1172 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
1173 Generate_Reference_To_Formals (Body_Id);
1174 Install_Formals (Body_Id);
1175 New_Scope (Body_Id);
1176 end if;
1177 end if;
1179 -- If this is the proper body of a stub, we must verify that the stub
1180 -- conforms to the body, and to the previous spec if one was present.
1181 -- we know already that the body conforms to that spec. This test is
1182 -- only required for subprograms that come from source.
1184 if Nkind (Parent (N)) = N_Subunit
1185 and then Comes_From_Source (N)
1186 and then not Error_Posted (Body_Id)
1187 and then Nkind (Corresponding_Stub (Parent (N))) =
1188 N_Subprogram_Body_Stub
1189 then
1190 declare
1191 Old_Id : constant Entity_Id :=
1192 Defining_Entity
1193 (Specification (Corresponding_Stub (Parent (N))));
1195 Conformant : Boolean := False;
1197 begin
1198 if No (Spec_Id) then
1199 Check_Fully_Conformant (Body_Id, Old_Id);
1201 else
1202 Check_Conformance
1203 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
1205 if not Conformant then
1207 -- The stub was taken to be a new declaration. Indicate
1208 -- that it lacks a body.
1210 Set_Has_Completion (Old_Id, False);
1211 end if;
1212 end if;
1213 end;
1214 end if;
1216 Set_Has_Completion (Body_Id);
1217 Check_Eliminated (Body_Id);
1219 if Nkind (N) = N_Subprogram_Body_Stub then
1220 return;
1222 elsif Present (Spec_Id)
1223 and then Expander_Active
1224 then
1225 Check_Following_Pragma;
1227 if Is_Always_Inlined (Spec_Id)
1228 or else (Has_Pragma_Inline (Spec_Id) and then Front_End_Inlining)
1229 then
1230 Build_Body_To_Inline (N, Spec_Id);
1231 end if;
1232 end if;
1234 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
1235 -- if its specification we have to install the private withed units.
1237 if Is_Compilation_Unit (Body_Id)
1238 and then Scope (Body_Id) = Standard_Standard
1239 then
1240 Install_Private_With_Clauses (Body_Id);
1241 end if;
1243 -- Now we can go on to analyze the body
1245 HSS := Handled_Statement_Sequence (N);
1246 Set_Actual_Subtypes (N, Current_Scope);
1247 Analyze_Declarations (Declarations (N));
1248 Check_Completion;
1249 Analyze (HSS);
1250 Process_End_Label (HSS, 't', Current_Scope);
1251 End_Scope;
1252 Check_Subprogram_Order (N);
1253 Set_Analyzed (Body_Id);
1255 -- If we have a separate spec, then the analysis of the declarations
1256 -- caused the entities in the body to be chained to the spec id, but
1257 -- we want them chained to the body id. Only the formal parameters
1258 -- end up chained to the spec id in this case.
1260 if Present (Spec_Id) then
1262 -- If a parent unit is categorized, the context of a subunit must
1263 -- conform to the categorization. Conversely, if a child unit is
1264 -- categorized, the parents themselves must conform.
1266 if Nkind (Parent (N)) = N_Subunit then
1267 Validate_Categorization_Dependency (N, Spec_Id);
1269 elsif Is_Child_Unit (Spec_Id) then
1270 Validate_Categorization_Dependency
1271 (Unit_Declaration_Node (Spec_Id), Spec_Id);
1272 end if;
1274 if Present (Last_Formal) then
1275 Set_Next_Entity
1276 (Last_Entity (Body_Id), Next_Entity (Last_Formal));
1277 Set_Next_Entity (Last_Formal, Empty);
1278 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1279 Set_Last_Entity (Spec_Id, Last_Formal);
1281 else
1282 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1283 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1284 Set_First_Entity (Spec_Id, Empty);
1285 Set_Last_Entity (Spec_Id, Empty);
1286 end if;
1287 end if;
1289 -- If function, check return statements
1291 if Nkind (Body_Spec) = N_Function_Specification then
1292 declare
1293 Id : Entity_Id;
1295 begin
1296 if Present (Spec_Id) then
1297 Id := Spec_Id;
1298 else
1299 Id := Body_Id;
1300 end if;
1302 if Return_Present (Id) then
1303 Check_Returns (HSS, 'F', Missing_Ret);
1305 if Missing_Ret then
1306 Set_Has_Missing_Return (Id);
1307 end if;
1309 elsif not Is_Machine_Code_Subprogram (Id)
1310 and then not Body_Deleted
1311 then
1312 Error_Msg_N ("missing RETURN statement in function body", N);
1313 end if;
1314 end;
1316 -- If procedure with No_Return, check returns
1318 elsif Nkind (Body_Spec) = N_Procedure_Specification
1319 and then Present (Spec_Id)
1320 and then No_Return (Spec_Id)
1321 then
1322 Check_Returns (HSS, 'P', Missing_Ret);
1323 end if;
1325 -- Now we are going to check for variables that are never modified in
1326 -- the body of the procedure. We omit these checks if the first
1327 -- statement of the procedure raises an exception. In particular this
1328 -- deals with the common idiom of a stubbed function, which might
1329 -- appear as something like
1331 -- function F (A : Integer) return Some_Type;
1332 -- X : Some_Type;
1333 -- begin
1334 -- raise Program_Error;
1335 -- return X;
1336 -- end F;
1338 -- Here the purpose of X is simply to satisfy the (annoying)
1339 -- requirement in Ada that there be at least one return, and we
1340 -- certainly do not want to go posting warnings on X that it is not
1341 -- initialized!
1343 declare
1344 Stm : Node_Id := First (Statements (HSS));
1346 begin
1347 -- Skip an initial label (for one thing this occurs when we are in
1348 -- front end ZCX mode, but in any case it is irrelevant).
1350 if Nkind (Stm) = N_Label then
1351 Next (Stm);
1352 end if;
1354 -- Do the test on the original statement before expansion
1356 declare
1357 Ostm : constant Node_Id := Original_Node (Stm);
1359 begin
1360 -- If explicit raise statement, return with no checks
1362 if Nkind (Ostm) = N_Raise_Statement then
1363 return;
1365 -- Check for explicit call cases which likely raise an exception
1367 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
1368 if Is_Entity_Name (Name (Ostm)) then
1369 declare
1370 Ent : constant Entity_Id := Entity (Name (Ostm));
1372 begin
1373 -- If the procedure is marked No_Return, then likely it
1374 -- raises an exception, but in any case it is not coming
1375 -- back here, so no need to check beyond the call.
1377 if Ekind (Ent) = E_Procedure
1378 and then No_Return (Ent)
1379 then
1380 return;
1382 -- If the procedure name is Raise_Exception, then also
1383 -- assume that it raises an exception. The main target
1384 -- here is Ada.Exceptions.Raise_Exception, but this name
1385 -- is pretty evocative in any context! Note that the
1386 -- procedure in Ada.Exceptions is not marked No_Return
1387 -- because of the annoying case of the null exception Id.
1389 elsif Chars (Ent) = Name_Raise_Exception then
1390 return;
1391 end if;
1392 end;
1393 end if;
1394 end if;
1395 end;
1396 end;
1398 -- Check for variables that are never modified
1400 declare
1401 E1, E2 : Entity_Id;
1403 begin
1404 -- If there is a separate spec, then transfer Never_Set_In_Source
1405 -- flags from out parameters to the corresponding entities in the
1406 -- body. The reason we do that is we want to post error flags on
1407 -- the body entities, not the spec entities.
1409 if Present (Spec_Id) then
1410 E1 := First_Entity (Spec_Id);
1412 while Present (E1) loop
1413 if Ekind (E1) = E_Out_Parameter then
1414 E2 := First_Entity (Body_Id);
1415 while Present (E2) loop
1416 exit when Chars (E1) = Chars (E2);
1417 Next_Entity (E2);
1418 end loop;
1420 if Present (E2) then
1421 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
1422 end if;
1423 end if;
1425 Next_Entity (E1);
1426 end loop;
1427 end if;
1429 -- Check references in body unless it was deleted. Note that the
1430 -- check of Body_Deleted here is not just for efficiency, it is
1431 -- necessary to avoid junk warnings on formal parameters.
1433 if not Body_Deleted then
1434 Check_References (Body_Id);
1435 end if;
1436 end;
1437 end Analyze_Subprogram_Body;
1439 ------------------------------------
1440 -- Analyze_Subprogram_Declaration --
1441 ------------------------------------
1443 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
1444 Designator : constant Entity_Id :=
1445 Analyze_Subprogram_Specification (Specification (N));
1446 Scop : constant Entity_Id := Current_Scope;
1448 -- Start of processing for Analyze_Subprogram_Declaration
1450 begin
1451 Generate_Definition (Designator);
1453 -- Check for RCI unit subprogram declarations against in-lined
1454 -- subprograms and subprograms having access parameter or limited
1455 -- parameter without Read and Write (RM E.2.3(12-13)).
1457 Validate_RCI_Subprogram_Declaration (N);
1459 Trace_Scope
1461 Defining_Entity (N),
1462 " Analyze subprogram spec. ");
1464 if Debug_Flag_C then
1465 Write_Str ("==== Compiling subprogram spec ");
1466 Write_Name (Chars (Designator));
1467 Write_Str (" from ");
1468 Write_Location (Sloc (N));
1469 Write_Eol;
1470 end if;
1472 New_Overloaded_Entity (Designator);
1473 Check_Delayed_Subprogram (Designator);
1475 -- What is the following code for, it used to be
1477 -- ??? Set_Suppress_Elaboration_Checks
1478 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
1480 -- The following seems equivalent, but a bit dubious
1482 if Elaboration_Checks_Suppressed (Designator) then
1483 Set_Kill_Elaboration_Checks (Designator);
1484 end if;
1486 if Scop /= Standard_Standard
1487 and then not Is_Child_Unit (Designator)
1488 then
1489 Set_Categorization_From_Scope (Designator, Scop);
1490 else
1491 -- For a compilation unit, check for library-unit pragmas
1493 New_Scope (Designator);
1494 Set_Categorization_From_Pragmas (N);
1495 Validate_Categorization_Dependency (N, Designator);
1496 Pop_Scope;
1497 end if;
1499 -- For a compilation unit, set body required. This flag will only be
1500 -- reset if a valid Import or Interface pragma is processed later on.
1502 if Nkind (Parent (N)) = N_Compilation_Unit then
1503 Set_Body_Required (Parent (N), True);
1504 end if;
1506 Generate_Reference_To_Formals (Designator);
1507 Check_Eliminated (Designator);
1509 if Comes_From_Source (N)
1510 and then Is_List_Member (N)
1511 then
1512 Check_Overriding_Operation (N, Designator);
1513 end if;
1515 end Analyze_Subprogram_Declaration;
1517 --------------------------------------
1518 -- Analyze_Subprogram_Specification --
1519 --------------------------------------
1521 -- Reminder: N here really is a subprogram specification (not a subprogram
1522 -- declaration). This procedure is called to analyze the specification in
1523 -- both subprogram bodies and subprogram declarations (specs).
1525 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
1526 Designator : constant Entity_Id := Defining_Entity (N);
1527 Formals : constant List_Id := Parameter_Specifications (N);
1529 begin
1530 Generate_Definition (Designator);
1532 if Nkind (N) = N_Function_Specification then
1533 Set_Ekind (Designator, E_Function);
1534 Set_Mechanism (Designator, Default_Mechanism);
1536 else
1537 Set_Ekind (Designator, E_Procedure);
1538 Set_Etype (Designator, Standard_Void_Type);
1539 end if;
1541 -- Introduce new scope for analysis of the formals and of the
1542 -- return type.
1544 Set_Scope (Designator, Current_Scope);
1546 if Present (Formals) then
1547 New_Scope (Designator);
1548 Process_Formals (Formals, N);
1549 End_Scope;
1551 elsif Nkind (N) = N_Function_Specification then
1552 Analyze_Return_Type (N);
1553 end if;
1555 if Nkind (N) = N_Function_Specification then
1556 if Nkind (Designator) = N_Defining_Operator_Symbol then
1557 Valid_Operator_Definition (Designator);
1558 end if;
1560 May_Need_Actuals (Designator);
1562 if Is_Abstract (Etype (Designator))
1563 and then Nkind (Parent (N))
1564 /= N_Abstract_Subprogram_Declaration
1565 and then (Nkind (Parent (N)))
1566 /= N_Formal_Abstract_Subprogram_Declaration
1567 and then (Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1568 or else not Is_Entity_Name (Name (Parent (N)))
1569 or else not Is_Abstract (Entity (Name (Parent (N)))))
1570 then
1571 Error_Msg_N
1572 ("function that returns abstract type must be abstract", N);
1573 end if;
1574 end if;
1576 return Designator;
1577 end Analyze_Subprogram_Specification;
1579 --------------------------
1580 -- Build_Body_To_Inline --
1581 --------------------------
1583 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
1584 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
1585 Original_Body : Node_Id;
1586 Body_To_Analyze : Node_Id;
1587 Max_Size : constant := 10;
1588 Stat_Count : Integer := 0;
1590 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
1591 -- Check for declarations that make inlining not worthwhile
1593 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
1594 -- Check for statements that make inlining not worthwhile: any tasking
1595 -- statement, nested at any level. Keep track of total number of
1596 -- elementary statements, as a measure of acceptable size.
1598 function Has_Pending_Instantiation return Boolean;
1599 -- If some enclosing body contains instantiations that appear before
1600 -- the corresponding generic body, the enclosing body has a freeze node
1601 -- so that it can be elaborated after the generic itself. This might
1602 -- conflict with subsequent inlinings, so that it is unsafe to try to
1603 -- inline in such a case.
1605 procedure Remove_Pragmas;
1606 -- A pragma Unreferenced that mentions a formal parameter has no
1607 -- meaning when the body is inlined and the formals are rewritten.
1608 -- Remove it from body to inline. The analysis of the non-inlined body
1609 -- will handle the pragma properly.
1611 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
1612 -- If the body of the subprogram includes a call that returns an
1613 -- unconstrained type, the secondary stack is involved, and it
1614 -- is not worth inlining.
1616 ------------------------------
1617 -- Has_Excluded_Declaration --
1618 ------------------------------
1620 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
1621 D : Node_Id;
1623 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
1624 -- Nested subprograms make a given body ineligible for inlining, but
1625 -- we make an exception for instantiations of unchecked conversion.
1626 -- The body has not been analyzed yet, so check the name, and verify
1627 -- that the visible entity with that name is the predefined unit.
1629 -----------------------------
1630 -- Is_Unchecked_Conversion --
1631 -----------------------------
1633 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
1634 Id : constant Node_Id := Name (D);
1635 Conv : Entity_Id;
1637 begin
1638 if Nkind (Id) = N_Identifier
1639 and then Chars (Id) = Name_Unchecked_Conversion
1640 then
1641 Conv := Current_Entity (Id);
1643 elsif Nkind (Id) = N_Selected_Component
1644 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
1645 then
1646 Conv := Current_Entity (Selector_Name (Id));
1648 else
1649 return False;
1650 end if;
1652 return
1653 Present (Conv)
1654 and then Scope (Conv) = Standard_Standard
1655 and then Is_Intrinsic_Subprogram (Conv);
1656 end Is_Unchecked_Conversion;
1658 -- Start of processing for Has_Excluded_Declaration
1660 begin
1661 D := First (Decls);
1663 while Present (D) loop
1664 if (Nkind (D) = N_Function_Instantiation
1665 and then not Is_Unchecked_Conversion (D))
1666 or else Nkind (D) = N_Protected_Type_Declaration
1667 or else Nkind (D) = N_Package_Declaration
1668 or else Nkind (D) = N_Package_Instantiation
1669 or else Nkind (D) = N_Subprogram_Body
1670 or else Nkind (D) = N_Procedure_Instantiation
1671 or else Nkind (D) = N_Task_Type_Declaration
1672 then
1673 Cannot_Inline
1674 ("cannot inline & (non-allowed declaration)?", D, Subp);
1675 return True;
1676 end if;
1678 Next (D);
1679 end loop;
1681 return False;
1682 end Has_Excluded_Declaration;
1684 ----------------------------
1685 -- Has_Excluded_Statement --
1686 ----------------------------
1688 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
1689 S : Node_Id;
1690 E : Node_Id;
1692 begin
1693 S := First (Stats);
1695 while Present (S) loop
1696 Stat_Count := Stat_Count + 1;
1698 if Nkind (S) = N_Abort_Statement
1699 or else Nkind (S) = N_Asynchronous_Select
1700 or else Nkind (S) = N_Conditional_Entry_Call
1701 or else Nkind (S) = N_Delay_Relative_Statement
1702 or else Nkind (S) = N_Delay_Until_Statement
1703 or else Nkind (S) = N_Selective_Accept
1704 or else Nkind (S) = N_Timed_Entry_Call
1705 then
1706 Cannot_Inline
1707 ("cannot inline & (non-allowed statement)?", S, Subp);
1708 return True;
1710 elsif Nkind (S) = N_Block_Statement then
1711 if Present (Declarations (S))
1712 and then Has_Excluded_Declaration (Declarations (S))
1713 then
1714 return True;
1716 elsif Present (Handled_Statement_Sequence (S))
1717 and then
1718 (Present
1719 (Exception_Handlers (Handled_Statement_Sequence (S)))
1720 or else
1721 Has_Excluded_Statement
1722 (Statements (Handled_Statement_Sequence (S))))
1723 then
1724 return True;
1725 end if;
1727 elsif Nkind (S) = N_Case_Statement then
1728 E := First (Alternatives (S));
1729 while Present (E) loop
1730 if Has_Excluded_Statement (Statements (E)) then
1731 return True;
1732 end if;
1734 Next (E);
1735 end loop;
1737 elsif Nkind (S) = N_If_Statement then
1738 if Has_Excluded_Statement (Then_Statements (S)) then
1739 return True;
1740 end if;
1742 if Present (Elsif_Parts (S)) then
1743 E := First (Elsif_Parts (S));
1744 while Present (E) loop
1745 if Has_Excluded_Statement (Then_Statements (E)) then
1746 return True;
1747 end if;
1748 Next (E);
1749 end loop;
1750 end if;
1752 if Present (Else_Statements (S))
1753 and then Has_Excluded_Statement (Else_Statements (S))
1754 then
1755 return True;
1756 end if;
1758 elsif Nkind (S) = N_Loop_Statement
1759 and then Has_Excluded_Statement (Statements (S))
1760 then
1761 return True;
1762 end if;
1764 Next (S);
1765 end loop;
1767 return False;
1768 end Has_Excluded_Statement;
1770 -------------------------------
1771 -- Has_Pending_Instantiation --
1772 -------------------------------
1774 function Has_Pending_Instantiation return Boolean is
1775 S : Entity_Id := Current_Scope;
1777 begin
1778 while Present (S) loop
1779 if Is_Compilation_Unit (S)
1780 or else Is_Child_Unit (S)
1781 then
1782 return False;
1783 elsif Ekind (S) = E_Package
1784 and then Has_Forward_Instantiation (S)
1785 then
1786 return True;
1787 end if;
1789 S := Scope (S);
1790 end loop;
1792 return False;
1793 end Has_Pending_Instantiation;
1795 --------------------
1796 -- Remove_Pragmas --
1797 --------------------
1799 procedure Remove_Pragmas is
1800 Decl : Node_Id;
1801 Nxt : Node_Id;
1803 begin
1804 Decl := First (Declarations (Body_To_Analyze));
1805 while Present (Decl) loop
1806 Nxt := Next (Decl);
1808 if Nkind (Decl) = N_Pragma
1809 and then Chars (Decl) = Name_Unreferenced
1810 then
1811 Remove (Decl);
1812 end if;
1814 Decl := Nxt;
1815 end loop;
1816 end Remove_Pragmas;
1818 --------------------------
1819 -- Uses_Secondary_Stack --
1820 --------------------------
1822 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
1823 function Check_Call (N : Node_Id) return Traverse_Result;
1824 -- Look for function calls that return an unconstrained type
1826 ----------------
1827 -- Check_Call --
1828 ----------------
1830 function Check_Call (N : Node_Id) return Traverse_Result is
1831 begin
1832 if Nkind (N) = N_Function_Call
1833 and then Is_Entity_Name (Name (N))
1834 and then Is_Composite_Type (Etype (Entity (Name (N))))
1835 and then not Is_Constrained (Etype (Entity (Name (N))))
1836 then
1837 Cannot_Inline
1838 ("cannot inline & (call returns unconstrained type)?",
1839 N, Subp);
1840 return Abandon;
1841 else
1842 return OK;
1843 end if;
1844 end Check_Call;
1846 function Check_Calls is new Traverse_Func (Check_Call);
1848 begin
1849 return Check_Calls (Bod) = Abandon;
1850 end Uses_Secondary_Stack;
1852 -- Start of processing for Build_Body_To_Inline
1854 begin
1855 if Nkind (Decl) = N_Subprogram_Declaration
1856 and then Present (Body_To_Inline (Decl))
1857 then
1858 return; -- Done already.
1860 -- Functions that return unconstrained composite types will require
1861 -- secondary stack handling, and cannot currently be inlined.
1862 -- Ditto for functions that return controlled types, where controlled
1863 -- actions interfere in complex ways with inlining.
1865 elsif Ekind (Subp) = E_Function
1866 and then not Is_Scalar_Type (Etype (Subp))
1867 and then not Is_Access_Type (Etype (Subp))
1868 and then not Is_Constrained (Etype (Subp))
1869 then
1870 Cannot_Inline
1871 ("cannot inline & (unconstrained return type)?", N, Subp);
1872 return;
1874 elsif Ekind (Subp) = E_Function
1875 and then Controlled_Type (Etype (Subp))
1876 then
1877 Cannot_Inline
1878 ("cannot inline & (controlled return type)?", N, Subp);
1879 return;
1880 end if;
1882 if Present (Declarations (N))
1883 and then Has_Excluded_Declaration (Declarations (N))
1884 then
1885 return;
1886 end if;
1888 if Present (Handled_Statement_Sequence (N)) then
1889 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
1890 Cannot_Inline
1891 ("cannot inline& (exception handler)?",
1892 First (Exception_Handlers (Handled_Statement_Sequence (N))),
1893 Subp);
1894 return;
1895 elsif
1896 Has_Excluded_Statement
1897 (Statements (Handled_Statement_Sequence (N)))
1898 then
1899 return;
1900 end if;
1901 end if;
1903 -- We do not inline a subprogram that is too large, unless it is
1904 -- marked Inline_Always. This pragma does not suppress the other
1905 -- checks on inlining (forbidden declarations, handlers, etc).
1907 if Stat_Count > Max_Size
1908 and then not Is_Always_Inlined (Subp)
1909 then
1910 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
1911 return;
1912 end if;
1914 if Has_Pending_Instantiation then
1915 Cannot_Inline
1916 ("cannot inline& (forward instance within enclosing body)?",
1917 N, Subp);
1918 return;
1919 end if;
1921 -- Within an instance, the body to inline must be treated as a nested
1922 -- generic, so that the proper global references are preserved.
1924 if In_Instance then
1925 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1926 Original_Body := Copy_Generic_Node (N, Empty, True);
1927 else
1928 Original_Body := Copy_Separate_Tree (N);
1929 end if;
1931 -- We need to capture references to the formals in order to substitute
1932 -- the actuals at the point of inlining, i.e. instantiation. To treat
1933 -- the formals as globals to the body to inline, we nest it within
1934 -- a dummy parameterless subprogram, declared within the real one.
1935 -- To avoid generating an internal name (which is never public, and
1936 -- which affects serial numbers of other generated names), we use
1937 -- an internal symbol that cannot conflict with user declarations.
1939 Set_Parameter_Specifications (Specification (Original_Body), No_List);
1940 Set_Defining_Unit_Name
1941 (Specification (Original_Body),
1942 Make_Defining_Identifier (Sloc (N), Name_uParent));
1943 Set_Corresponding_Spec (Original_Body, Empty);
1945 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
1947 -- Set return type of function, which is also global and does not need
1948 -- to be resolved.
1950 if Ekind (Subp) = E_Function then
1951 Set_Subtype_Mark (Specification (Body_To_Analyze),
1952 New_Occurrence_Of (Etype (Subp), Sloc (N)));
1953 end if;
1955 if No (Declarations (N)) then
1956 Set_Declarations (N, New_List (Body_To_Analyze));
1957 else
1958 Append (Body_To_Analyze, Declarations (N));
1959 end if;
1961 Expander_Mode_Save_And_Set (False);
1962 Remove_Pragmas;
1964 Analyze (Body_To_Analyze);
1965 New_Scope (Defining_Entity (Body_To_Analyze));
1966 Save_Global_References (Original_Body);
1967 End_Scope;
1968 Remove (Body_To_Analyze);
1970 Expander_Mode_Restore;
1972 if In_Instance then
1973 Restore_Env;
1974 end if;
1976 -- If secondary stk used there is no point in inlining. We have
1977 -- already issued the warning in this case, so nothing to do.
1979 if Uses_Secondary_Stack (Body_To_Analyze) then
1980 return;
1981 end if;
1983 Set_Body_To_Inline (Decl, Original_Body);
1984 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
1985 Set_Is_Inlined (Subp);
1986 end Build_Body_To_Inline;
1988 -------------------
1989 -- Cannot_Inline --
1990 -------------------
1992 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
1993 begin
1994 -- Do not emit warning if this is a predefined unit which is not
1995 -- the main unit. With validity checks enabled, some predefined
1996 -- subprograms may contain nested subprograms and become ineligible
1997 -- for inlining.
1999 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
2000 and then not In_Extended_Main_Source_Unit (Subp)
2001 then
2002 null;
2004 elsif Is_Always_Inlined (Subp) then
2006 -- Remove last character (question mark) to make this into an error,
2007 -- because the Inline_Always pragma cannot be obeyed.
2009 Error_Msg_NE (Msg (1 .. Msg'Length - 1), N, Subp);
2011 elsif Ineffective_Inline_Warnings then
2012 Error_Msg_NE (Msg, N, Subp);
2013 end if;
2014 end Cannot_Inline;
2016 -----------------------
2017 -- Check_Conformance --
2018 -----------------------
2020 procedure Check_Conformance
2021 (New_Id : Entity_Id;
2022 Old_Id : Entity_Id;
2023 Ctype : Conformance_Type;
2024 Errmsg : Boolean;
2025 Conforms : out Boolean;
2026 Err_Loc : Node_Id := Empty;
2027 Get_Inst : Boolean := False)
2029 Old_Type : constant Entity_Id := Etype (Old_Id);
2030 New_Type : constant Entity_Id := Etype (New_Id);
2031 Old_Formal : Entity_Id;
2032 New_Formal : Entity_Id;
2034 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
2035 -- Post error message for conformance error on given node. Two messages
2036 -- are output. The first points to the previous declaration with a
2037 -- general "no conformance" message. The second is the detailed reason,
2038 -- supplied as Msg. The parameter N provide information for a possible
2039 -- & insertion in the message, and also provides the location for
2040 -- posting the message in the absence of a specified Err_Loc location.
2042 -----------------------
2043 -- Conformance_Error --
2044 -----------------------
2046 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
2047 Enode : Node_Id;
2049 begin
2050 Conforms := False;
2052 if Errmsg then
2053 if No (Err_Loc) then
2054 Enode := N;
2055 else
2056 Enode := Err_Loc;
2057 end if;
2059 Error_Msg_Sloc := Sloc (Old_Id);
2061 case Ctype is
2062 when Type_Conformant =>
2063 Error_Msg_N
2064 ("not type conformant with declaration#!", Enode);
2066 when Mode_Conformant =>
2067 Error_Msg_N
2068 ("not mode conformant with declaration#!", Enode);
2070 when Subtype_Conformant =>
2071 Error_Msg_N
2072 ("not subtype conformant with declaration#!", Enode);
2074 when Fully_Conformant =>
2075 Error_Msg_N
2076 ("not fully conformant with declaration#!", Enode);
2077 end case;
2079 Error_Msg_NE (Msg, Enode, N);
2080 end if;
2081 end Conformance_Error;
2083 -- Start of processing for Check_Conformance
2085 begin
2086 Conforms := True;
2088 -- We need a special case for operators, since they don't appear
2089 -- explicitly.
2091 if Ctype = Type_Conformant then
2092 if Ekind (New_Id) = E_Operator
2093 and then Operator_Matches_Spec (New_Id, Old_Id)
2094 then
2095 return;
2096 end if;
2097 end if;
2099 -- If both are functions/operators, check return types conform
2101 if Old_Type /= Standard_Void_Type
2102 and then New_Type /= Standard_Void_Type
2103 then
2104 if not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
2105 Conformance_Error ("return type does not match!", New_Id);
2106 return;
2107 end if;
2109 -- If either is a function/operator and the other isn't, error
2111 elsif Old_Type /= Standard_Void_Type
2112 or else New_Type /= Standard_Void_Type
2113 then
2114 Conformance_Error ("functions can only match functions!", New_Id);
2115 return;
2116 end if;
2118 -- In subtype conformant case, conventions must match (RM 6.3.1(16))
2119 -- If this is a renaming as body, refine error message to indicate that
2120 -- the conflict is with the original declaration. If the entity is not
2121 -- frozen, the conventions don't have to match, the one of the renamed
2122 -- entity is inherited.
2124 if Ctype >= Subtype_Conformant then
2125 if Convention (Old_Id) /= Convention (New_Id) then
2127 if not Is_Frozen (New_Id) then
2128 null;
2130 elsif Present (Err_Loc)
2131 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
2132 and then Present (Corresponding_Spec (Err_Loc))
2133 then
2134 Error_Msg_Name_1 := Chars (New_Id);
2135 Error_Msg_Name_2 :=
2136 Name_Ada + Convention_Id'Pos (Convention (New_Id));
2138 Conformance_Error ("prior declaration for% has convention %!");
2140 else
2141 Conformance_Error ("calling conventions do not match!");
2142 end if;
2144 return;
2146 elsif Is_Formal_Subprogram (Old_Id)
2147 or else Is_Formal_Subprogram (New_Id)
2148 then
2149 Conformance_Error ("formal subprograms not allowed!");
2150 return;
2151 end if;
2152 end if;
2154 -- Deal with parameters
2156 -- Note: we use the entity information, rather than going directly
2157 -- to the specification in the tree. This is not only simpler, but
2158 -- absolutely necessary for some cases of conformance tests between
2159 -- operators, where the declaration tree simply does not exist!
2161 Old_Formal := First_Formal (Old_Id);
2162 New_Formal := First_Formal (New_Id);
2164 while Present (Old_Formal) and then Present (New_Formal) loop
2165 if Ctype = Fully_Conformant then
2167 -- Names must match. Error message is more accurate if we do
2168 -- this before checking that the types of the formals match.
2170 if Chars (Old_Formal) /= Chars (New_Formal) then
2171 Conformance_Error ("name & does not match!", New_Formal);
2173 -- Set error posted flag on new formal as well to stop
2174 -- junk cascaded messages in some cases.
2176 Set_Error_Posted (New_Formal);
2177 return;
2178 end if;
2179 end if;
2181 -- Types must always match. In the visible part of an instance,
2182 -- usual overloading rules for dispatching operations apply, and
2183 -- we check base types (not the actual subtypes).
2185 if In_Instance_Visible_Part
2186 and then Is_Dispatching_Operation (New_Id)
2187 then
2188 if not Conforming_Types
2189 (Base_Type (Etype (Old_Formal)),
2190 Base_Type (Etype (New_Formal)), Ctype, Get_Inst)
2191 then
2192 Conformance_Error ("type of & does not match!", New_Formal);
2193 return;
2194 end if;
2196 elsif not Conforming_Types
2197 (Etype (Old_Formal), Etype (New_Formal), Ctype, Get_Inst)
2198 then
2199 Conformance_Error ("type of & does not match!", New_Formal);
2200 return;
2201 end if;
2203 -- For mode conformance, mode must match
2205 if Ctype >= Mode_Conformant
2206 and then Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal)
2207 then
2208 Conformance_Error ("mode of & does not match!", New_Formal);
2209 return;
2210 end if;
2212 -- Full conformance checks
2214 if Ctype = Fully_Conformant then
2216 -- We have checked already that names match. Check default
2217 -- expressions for in parameters
2219 if Parameter_Mode (Old_Formal) = E_In_Parameter then
2220 declare
2221 NewD : constant Boolean :=
2222 Present (Default_Value (New_Formal));
2223 OldD : constant Boolean :=
2224 Present (Default_Value (Old_Formal));
2225 begin
2226 if NewD or OldD then
2228 -- The old default value has been analyzed because the
2229 -- current full declaration will have frozen everything
2230 -- before. The new default values have not been
2231 -- analyzed, so analyze them now before we check for
2232 -- conformance.
2234 if NewD then
2235 New_Scope (New_Id);
2236 Analyze_Per_Use_Expression
2237 (Default_Value (New_Formal), Etype (New_Formal));
2238 End_Scope;
2239 end if;
2241 if not (NewD and OldD)
2242 or else not Fully_Conformant_Expressions
2243 (Default_Value (Old_Formal),
2244 Default_Value (New_Formal))
2245 then
2246 Conformance_Error
2247 ("default expression for & does not match!",
2248 New_Formal);
2249 return;
2250 end if;
2251 end if;
2252 end;
2253 end if;
2254 end if;
2256 -- A couple of special checks for Ada 83 mode. These checks are
2257 -- skipped if either entity is an operator in package Standard.
2258 -- or if either old or new instance is not from the source program.
2260 if Ada_Version = Ada_83
2261 and then Sloc (Old_Id) > Standard_Location
2262 and then Sloc (New_Id) > Standard_Location
2263 and then Comes_From_Source (Old_Id)
2264 and then Comes_From_Source (New_Id)
2265 then
2266 declare
2267 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
2268 New_Param : constant Node_Id := Declaration_Node (New_Formal);
2270 begin
2271 -- Explicit IN must be present or absent in both cases. This
2272 -- test is required only in the full conformance case.
2274 if In_Present (Old_Param) /= In_Present (New_Param)
2275 and then Ctype = Fully_Conformant
2276 then
2277 Conformance_Error
2278 ("(Ada 83) IN must appear in both declarations",
2279 New_Formal);
2280 return;
2281 end if;
2283 -- Grouping (use of comma in param lists) must be the same
2284 -- This is where we catch a misconformance like:
2286 -- A,B : Integer
2287 -- A : Integer; B : Integer
2289 -- which are represented identically in the tree except
2290 -- for the setting of the flags More_Ids and Prev_Ids.
2292 if More_Ids (Old_Param) /= More_Ids (New_Param)
2293 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
2294 then
2295 Conformance_Error
2296 ("grouping of & does not match!", New_Formal);
2297 return;
2298 end if;
2299 end;
2300 end if;
2302 Next_Formal (Old_Formal);
2303 Next_Formal (New_Formal);
2304 end loop;
2306 if Present (Old_Formal) then
2307 Conformance_Error ("too few parameters!");
2308 return;
2310 elsif Present (New_Formal) then
2311 Conformance_Error ("too many parameters!", New_Formal);
2312 return;
2313 end if;
2315 end Check_Conformance;
2317 ------------------------------
2318 -- Check_Delayed_Subprogram --
2319 ------------------------------
2321 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
2322 F : Entity_Id;
2324 procedure Possible_Freeze (T : Entity_Id);
2325 -- T is the type of either a formal parameter or of the return type.
2326 -- If T is not yet frozen and needs a delayed freeze, then the
2327 -- subprogram itself must be delayed.
2329 ---------------------
2330 -- Possible_Freeze --
2331 ---------------------
2333 procedure Possible_Freeze (T : Entity_Id) is
2334 begin
2335 if Has_Delayed_Freeze (T)
2336 and then not Is_Frozen (T)
2337 then
2338 Set_Has_Delayed_Freeze (Designator);
2340 elsif Is_Access_Type (T)
2341 and then Has_Delayed_Freeze (Designated_Type (T))
2342 and then not Is_Frozen (Designated_Type (T))
2343 then
2344 Set_Has_Delayed_Freeze (Designator);
2345 end if;
2346 end Possible_Freeze;
2348 -- Start of processing for Check_Delayed_Subprogram
2350 begin
2351 -- Never need to freeze abstract subprogram
2353 if Is_Abstract (Designator) then
2354 null;
2355 else
2356 -- Need delayed freeze if return type itself needs a delayed
2357 -- freeze and is not yet frozen.
2359 Possible_Freeze (Etype (Designator));
2360 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
2362 -- Need delayed freeze if any of the formal types themselves need
2363 -- a delayed freeze and are not yet frozen.
2365 F := First_Formal (Designator);
2366 while Present (F) loop
2367 Possible_Freeze (Etype (F));
2368 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
2369 Next_Formal (F);
2370 end loop;
2371 end if;
2373 -- Mark functions that return by reference. Note that it cannot be
2374 -- done for delayed_freeze subprograms because the underlying
2375 -- returned type may not be known yet (for private types)
2377 if not Has_Delayed_Freeze (Designator)
2378 and then Expander_Active
2379 then
2380 declare
2381 Typ : constant Entity_Id := Etype (Designator);
2382 Utyp : constant Entity_Id := Underlying_Type (Typ);
2384 begin
2385 if Is_Return_By_Reference_Type (Typ) then
2386 Set_Returns_By_Ref (Designator);
2388 elsif Present (Utyp) and then Controlled_Type (Utyp) then
2389 Set_Returns_By_Ref (Designator);
2390 end if;
2391 end;
2392 end if;
2393 end Check_Delayed_Subprogram;
2395 ------------------------------------
2396 -- Check_Discriminant_Conformance --
2397 ------------------------------------
2399 procedure Check_Discriminant_Conformance
2400 (N : Node_Id;
2401 Prev : Entity_Id;
2402 Prev_Loc : Node_Id)
2404 Old_Discr : Entity_Id := First_Discriminant (Prev);
2405 New_Discr : Node_Id := First (Discriminant_Specifications (N));
2406 New_Discr_Id : Entity_Id;
2407 New_Discr_Type : Entity_Id;
2409 procedure Conformance_Error (Msg : String; N : Node_Id);
2410 -- Post error message for conformance error on given node. Two messages
2411 -- are output. The first points to the previous declaration with a
2412 -- general "no conformance" message. The second is the detailed reason,
2413 -- supplied as Msg. The parameter N provide information for a possible
2414 -- & insertion in the message.
2416 -----------------------
2417 -- Conformance_Error --
2418 -----------------------
2420 procedure Conformance_Error (Msg : String; N : Node_Id) is
2421 begin
2422 Error_Msg_Sloc := Sloc (Prev_Loc);
2423 Error_Msg_N ("not fully conformant with declaration#!", N);
2424 Error_Msg_NE (Msg, N, N);
2425 end Conformance_Error;
2427 -- Start of processing for Check_Discriminant_Conformance
2429 begin
2430 while Present (Old_Discr) and then Present (New_Discr) loop
2432 New_Discr_Id := Defining_Identifier (New_Discr);
2434 -- The subtype mark of the discriminant on the full type has not
2435 -- been analyzed so we do it here. For an access discriminant a new
2436 -- type is created.
2438 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
2439 New_Discr_Type :=
2440 Access_Definition (N, Discriminant_Type (New_Discr));
2442 else
2443 Analyze (Discriminant_Type (New_Discr));
2444 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
2445 end if;
2447 if not Conforming_Types
2448 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
2449 then
2450 Conformance_Error ("type of & does not match!", New_Discr_Id);
2451 return;
2452 else
2453 -- Treat the new discriminant as an occurrence of the old one,
2454 -- for navigation purposes, and fill in some semantic
2455 -- information, for completeness.
2457 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
2458 Set_Etype (New_Discr_Id, Etype (Old_Discr));
2459 Set_Scope (New_Discr_Id, Scope (Old_Discr));
2460 end if;
2462 -- Names must match
2464 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
2465 Conformance_Error ("name & does not match!", New_Discr_Id);
2466 return;
2467 end if;
2469 -- Default expressions must match
2471 declare
2472 NewD : constant Boolean :=
2473 Present (Expression (New_Discr));
2474 OldD : constant Boolean :=
2475 Present (Expression (Parent (Old_Discr)));
2477 begin
2478 if NewD or OldD then
2480 -- The old default value has been analyzed and expanded,
2481 -- because the current full declaration will have frozen
2482 -- everything before. The new default values have not been
2483 -- expanded, so expand now to check conformance.
2485 if NewD then
2486 Analyze_Per_Use_Expression
2487 (Expression (New_Discr), New_Discr_Type);
2488 end if;
2490 if not (NewD and OldD)
2491 or else not Fully_Conformant_Expressions
2492 (Expression (Parent (Old_Discr)),
2493 Expression (New_Discr))
2495 then
2496 Conformance_Error
2497 ("default expression for & does not match!",
2498 New_Discr_Id);
2499 return;
2500 end if;
2501 end if;
2502 end;
2504 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
2506 if Ada_Version = Ada_83 then
2507 declare
2508 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
2510 begin
2511 -- Grouping (use of comma in param lists) must be the same
2512 -- This is where we catch a misconformance like:
2514 -- A,B : Integer
2515 -- A : Integer; B : Integer
2517 -- which are represented identically in the tree except
2518 -- for the setting of the flags More_Ids and Prev_Ids.
2520 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
2521 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
2522 then
2523 Conformance_Error
2524 ("grouping of & does not match!", New_Discr_Id);
2525 return;
2526 end if;
2527 end;
2528 end if;
2530 Next_Discriminant (Old_Discr);
2531 Next (New_Discr);
2532 end loop;
2534 if Present (Old_Discr) then
2535 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
2536 return;
2538 elsif Present (New_Discr) then
2539 Conformance_Error
2540 ("too many discriminants!", Defining_Identifier (New_Discr));
2541 return;
2542 end if;
2543 end Check_Discriminant_Conformance;
2545 ----------------------------
2546 -- Check_Fully_Conformant --
2547 ----------------------------
2549 procedure Check_Fully_Conformant
2550 (New_Id : Entity_Id;
2551 Old_Id : Entity_Id;
2552 Err_Loc : Node_Id := Empty)
2554 Result : Boolean;
2556 begin
2557 Check_Conformance
2558 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
2559 end Check_Fully_Conformant;
2561 ---------------------------
2562 -- Check_Mode_Conformant --
2563 ---------------------------
2565 procedure Check_Mode_Conformant
2566 (New_Id : Entity_Id;
2567 Old_Id : Entity_Id;
2568 Err_Loc : Node_Id := Empty;
2569 Get_Inst : Boolean := False)
2571 Result : Boolean;
2573 begin
2574 Check_Conformance
2575 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
2576 end Check_Mode_Conformant;
2578 --------------------------------
2579 -- Check_Overriding_Operation --
2580 --------------------------------
2582 procedure Check_Overriding_Operation
2583 (N : Node_Id;
2584 Subp : Entity_Id)
2586 Arg1 : Node_Id;
2587 Decl : Node_Id;
2588 Has_Pragma : Boolean := False;
2590 begin
2591 -- See whether there is an overriding pragma immediately following
2592 -- the declaration. Intervening pragmas, such as Inline, are allowed.
2594 Decl := Next (N);
2595 while Present (Decl)
2596 and then Nkind (Decl) = N_Pragma
2597 loop
2598 if Chars (Decl) = Name_Overriding
2599 or else Chars (Decl) = Name_Optional_Overriding
2600 then
2601 -- For now disable the use of these pragmas, until the ARG
2602 -- finalizes the design of this feature.
2604 Error_Msg_N ("?unrecognized pragma", Decl);
2606 if not Is_Overriding_Operation (Subp) then
2608 -- Before emitting an error message, check whether this
2609 -- may override an operation that is not yet visible, as
2610 -- in the case of a derivation of a private operation in
2611 -- a child unit. Such an operation is introduced with a
2612 -- different name, but its alias is the parent operation.
2614 declare
2615 E : Entity_Id;
2617 begin
2618 E := First_Entity (Current_Scope);
2620 while Present (E) loop
2621 if Ekind (E) = Ekind (Subp)
2622 and then not Comes_From_Source (E)
2623 and then Present (Alias (E))
2624 and then Chars (Alias (E)) = Chars (Subp)
2625 and then In_Open_Scopes (Scope (Alias (E)))
2626 then
2627 exit;
2628 else
2629 Next_Entity (E);
2630 end if;
2631 end loop;
2633 if No (E) then
2634 Error_Msg_NE
2635 ("& must override an inherited operation",
2636 Decl, Subp);
2637 end if;
2638 end;
2639 end if;
2641 -- Verify syntax of pragma
2643 Arg1 := First (Pragma_Argument_Associations (Decl));
2645 if Present (Arg1) then
2646 if not Is_Entity_Name (Expression (Arg1)) then
2647 Error_Msg_N ("pragma applies to local subprogram", Decl);
2649 elsif Chars (Expression (Arg1)) /= Chars (Subp) then
2650 Error_Msg_N
2651 ("pragma must apply to preceding subprogram", Decl);
2653 elsif Present (Next (Arg1)) then
2654 Error_Msg_N ("illegal pragma format", Decl);
2655 end if;
2656 end if;
2658 Set_Analyzed (Decl);
2659 Has_Pragma := True;
2660 exit;
2661 end if;
2663 Next (Decl);
2664 end loop;
2666 if not Has_Pragma
2667 and then Explicit_Overriding
2668 and then Is_Overriding_Operation (Subp)
2669 then
2670 Error_Msg_NE ("Missing overriding pragma for&", Subp, Subp);
2671 end if;
2672 end Check_Overriding_Operation;
2674 -------------------
2675 -- Check_Returns --
2676 -------------------
2678 procedure Check_Returns
2679 (HSS : Node_Id;
2680 Mode : Character;
2681 Err : out Boolean)
2683 Handler : Node_Id;
2685 procedure Check_Statement_Sequence (L : List_Id);
2686 -- Internal recursive procedure to check a list of statements for proper
2687 -- termination by a return statement (or a transfer of control or a
2688 -- compound statement that is itself internally properly terminated).
2690 ------------------------------
2691 -- Check_Statement_Sequence --
2692 ------------------------------
2694 procedure Check_Statement_Sequence (L : List_Id) is
2695 Last_Stm : Node_Id;
2696 Kind : Node_Kind;
2698 Raise_Exception_Call : Boolean;
2699 -- Set True if statement sequence terminated by Raise_Exception call
2700 -- or a Reraise_Occurrence call.
2702 begin
2703 Raise_Exception_Call := False;
2705 -- Get last real statement
2707 Last_Stm := Last (L);
2709 -- Don't count pragmas
2711 while Nkind (Last_Stm) = N_Pragma
2713 -- Don't count call to SS_Release (can happen after Raise_Exception)
2715 or else
2716 (Nkind (Last_Stm) = N_Procedure_Call_Statement
2717 and then
2718 Nkind (Name (Last_Stm)) = N_Identifier
2719 and then
2720 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
2722 -- Don't count exception junk
2724 or else
2725 ((Nkind (Last_Stm) = N_Goto_Statement
2726 or else Nkind (Last_Stm) = N_Label
2727 or else Nkind (Last_Stm) = N_Object_Declaration)
2728 and then Exception_Junk (Last_Stm))
2729 loop
2730 Prev (Last_Stm);
2731 end loop;
2733 -- Here we have the "real" last statement
2735 Kind := Nkind (Last_Stm);
2737 -- Transfer of control, OK. Note that in the No_Return procedure
2738 -- case, we already diagnosed any explicit return statements, so
2739 -- we can treat them as OK in this context.
2741 if Is_Transfer (Last_Stm) then
2742 return;
2744 -- Check cases of explicit non-indirect procedure calls
2746 elsif Kind = N_Procedure_Call_Statement
2747 and then Is_Entity_Name (Name (Last_Stm))
2748 then
2749 -- Check call to Raise_Exception procedure which is treated
2750 -- specially, as is a call to Reraise_Occurrence.
2752 -- We suppress the warning in these cases since it is likely that
2753 -- the programmer really does not expect to deal with the case
2754 -- of Null_Occurrence, and thus would find a warning about a
2755 -- missing return curious, and raising Program_Error does not
2756 -- seem such a bad behavior if this does occur.
2758 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
2759 or else
2760 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
2761 then
2762 Raise_Exception_Call := True;
2764 -- For Raise_Exception call, test first argument, if it is
2765 -- an attribute reference for a 'Identity call, then we know
2766 -- that the call cannot possibly return.
2768 declare
2769 Arg : constant Node_Id :=
2770 Original_Node (First_Actual (Last_Stm));
2772 begin
2773 if Nkind (Arg) = N_Attribute_Reference
2774 and then Attribute_Name (Arg) = Name_Identity
2775 then
2776 return;
2777 end if;
2778 end;
2779 end if;
2781 -- If statement, need to look inside if there is an else and check
2782 -- each constituent statement sequence for proper termination.
2784 elsif Kind = N_If_Statement
2785 and then Present (Else_Statements (Last_Stm))
2786 then
2787 Check_Statement_Sequence (Then_Statements (Last_Stm));
2788 Check_Statement_Sequence (Else_Statements (Last_Stm));
2790 if Present (Elsif_Parts (Last_Stm)) then
2791 declare
2792 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
2794 begin
2795 while Present (Elsif_Part) loop
2796 Check_Statement_Sequence (Then_Statements (Elsif_Part));
2797 Next (Elsif_Part);
2798 end loop;
2799 end;
2800 end if;
2802 return;
2804 -- Case statement, check each case for proper termination
2806 elsif Kind = N_Case_Statement then
2807 declare
2808 Case_Alt : Node_Id;
2810 begin
2811 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
2812 while Present (Case_Alt) loop
2813 Check_Statement_Sequence (Statements (Case_Alt));
2814 Next_Non_Pragma (Case_Alt);
2815 end loop;
2816 end;
2818 return;
2820 -- Block statement, check its handled sequence of statements
2822 elsif Kind = N_Block_Statement then
2823 declare
2824 Err1 : Boolean;
2826 begin
2827 Check_Returns
2828 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
2830 if Err1 then
2831 Err := True;
2832 end if;
2834 return;
2835 end;
2837 -- Loop statement. If there is an iteration scheme, we can definitely
2838 -- fall out of the loop. Similarly if there is an exit statement, we
2839 -- can fall out. In either case we need a following return.
2841 elsif Kind = N_Loop_Statement then
2842 if Present (Iteration_Scheme (Last_Stm))
2843 or else Has_Exit (Entity (Identifier (Last_Stm)))
2844 then
2845 null;
2847 -- A loop with no exit statement or iteration scheme if either
2848 -- an inifite loop, or it has some other exit (raise/return).
2849 -- In either case, no warning is required.
2851 else
2852 return;
2853 end if;
2855 -- Timed entry call, check entry call and delay alternatives
2857 -- Note: in expanded code, the timed entry call has been converted
2858 -- to a set of expanded statements on which the check will work
2859 -- correctly in any case.
2861 elsif Kind = N_Timed_Entry_Call then
2862 declare
2863 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
2864 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
2866 begin
2867 -- If statement sequence of entry call alternative is missing,
2868 -- then we can definitely fall through, and we post the error
2869 -- message on the entry call alternative itself.
2871 if No (Statements (ECA)) then
2872 Last_Stm := ECA;
2874 -- If statement sequence of delay alternative is missing, then
2875 -- we can definitely fall through, and we post the error
2876 -- message on the delay alternative itself.
2878 -- Note: if both ECA and DCA are missing the return, then we
2879 -- post only one message, should be enough to fix the bugs.
2880 -- If not we will get a message next time on the DCA when the
2881 -- ECA is fixed!
2883 elsif No (Statements (DCA)) then
2884 Last_Stm := DCA;
2886 -- Else check both statement sequences
2888 else
2889 Check_Statement_Sequence (Statements (ECA));
2890 Check_Statement_Sequence (Statements (DCA));
2891 return;
2892 end if;
2893 end;
2895 -- Conditional entry call, check entry call and else part
2897 -- Note: in expanded code, the conditional entry call has been
2898 -- converted to a set of expanded statements on which the check
2899 -- will work correctly in any case.
2901 elsif Kind = N_Conditional_Entry_Call then
2902 declare
2903 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
2905 begin
2906 -- If statement sequence of entry call alternative is missing,
2907 -- then we can definitely fall through, and we post the error
2908 -- message on the entry call alternative itself.
2910 if No (Statements (ECA)) then
2911 Last_Stm := ECA;
2913 -- Else check statement sequence and else part
2915 else
2916 Check_Statement_Sequence (Statements (ECA));
2917 Check_Statement_Sequence (Else_Statements (Last_Stm));
2918 return;
2919 end if;
2920 end;
2921 end if;
2923 -- If we fall through, issue appropriate message
2925 if Mode = 'F' then
2927 if not Raise_Exception_Call then
2928 Error_Msg_N
2929 ("?RETURN statement missing following this statement!",
2930 Last_Stm);
2931 Error_Msg_N
2932 ("\?Program_Error may be raised at run time",
2933 Last_Stm);
2934 end if;
2936 -- Note: we set Err even though we have not issued a warning
2937 -- because we still have a case of a missing return. This is
2938 -- an extremely marginal case, probably will never be noticed
2939 -- but we might as well get it right.
2941 Err := True;
2943 else
2944 Error_Msg_N
2945 ("implied return after this statement not allowed (No_Return)",
2946 Last_Stm);
2947 end if;
2948 end Check_Statement_Sequence;
2950 -- Start of processing for Check_Returns
2952 begin
2953 Err := False;
2954 Check_Statement_Sequence (Statements (HSS));
2956 if Present (Exception_Handlers (HSS)) then
2957 Handler := First_Non_Pragma (Exception_Handlers (HSS));
2958 while Present (Handler) loop
2959 Check_Statement_Sequence (Statements (Handler));
2960 Next_Non_Pragma (Handler);
2961 end loop;
2962 end if;
2963 end Check_Returns;
2965 ----------------------------
2966 -- Check_Subprogram_Order --
2967 ----------------------------
2969 procedure Check_Subprogram_Order (N : Node_Id) is
2971 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
2972 -- This is used to check if S1 > S2 in the sense required by this
2973 -- test, for example nameab < namec, but name2 < name10.
2975 -----------------------------
2976 -- Subprogram_Name_Greater --
2977 -----------------------------
2979 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
2980 L1, L2 : Positive;
2981 N1, N2 : Natural;
2983 begin
2984 -- Remove trailing numeric parts
2986 L1 := S1'Last;
2987 while S1 (L1) in '0' .. '9' loop
2988 L1 := L1 - 1;
2989 end loop;
2991 L2 := S2'Last;
2992 while S2 (L2) in '0' .. '9' loop
2993 L2 := L2 - 1;
2994 end loop;
2996 -- If non-numeric parts non-equal, that's decisive
2998 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
2999 return False;
3001 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
3002 return True;
3004 -- If non-numeric parts equal, compare suffixed numeric parts. Note
3005 -- that a missing suffix is treated as numeric zero in this test.
3007 else
3008 N1 := 0;
3009 while L1 < S1'Last loop
3010 L1 := L1 + 1;
3011 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
3012 end loop;
3014 N2 := 0;
3015 while L2 < S2'Last loop
3016 L2 := L2 + 1;
3017 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
3018 end loop;
3020 return N1 > N2;
3021 end if;
3022 end Subprogram_Name_Greater;
3024 -- Start of processing for Check_Subprogram_Order
3026 begin
3027 -- Check body in alpha order if this is option
3029 if Style_Check
3030 and then Style_Check_Order_Subprograms
3031 and then Nkind (N) = N_Subprogram_Body
3032 and then Comes_From_Source (N)
3033 and then In_Extended_Main_Source_Unit (N)
3034 then
3035 declare
3036 LSN : String_Ptr
3037 renames Scope_Stack.Table
3038 (Scope_Stack.Last).Last_Subprogram_Name;
3040 Body_Id : constant Entity_Id :=
3041 Defining_Entity (Specification (N));
3043 begin
3044 Get_Decoded_Name_String (Chars (Body_Id));
3046 if LSN /= null then
3047 if Subprogram_Name_Greater
3048 (LSN.all, Name_Buffer (1 .. Name_Len))
3049 then
3050 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
3051 end if;
3053 Free (LSN);
3054 end if;
3056 LSN := new String'(Name_Buffer (1 .. Name_Len));
3057 end;
3058 end if;
3059 end Check_Subprogram_Order;
3061 ------------------------------
3062 -- Check_Subtype_Conformant --
3063 ------------------------------
3065 procedure Check_Subtype_Conformant
3066 (New_Id : Entity_Id;
3067 Old_Id : Entity_Id;
3068 Err_Loc : Node_Id := Empty)
3070 Result : Boolean;
3071 begin
3072 Check_Conformance
3073 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc);
3074 end Check_Subtype_Conformant;
3076 ---------------------------
3077 -- Check_Type_Conformant --
3078 ---------------------------
3080 procedure Check_Type_Conformant
3081 (New_Id : Entity_Id;
3082 Old_Id : Entity_Id;
3083 Err_Loc : Node_Id := Empty)
3085 Result : Boolean;
3086 begin
3087 Check_Conformance
3088 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
3089 end Check_Type_Conformant;
3091 ----------------------
3092 -- Conforming_Types --
3093 ----------------------
3095 function Conforming_Types
3096 (T1 : Entity_Id;
3097 T2 : Entity_Id;
3098 Ctype : Conformance_Type;
3099 Get_Inst : Boolean := False) return Boolean
3101 Type_1 : Entity_Id := T1;
3102 Type_2 : Entity_Id := T2;
3103 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
3105 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
3106 -- If neither T1 nor T2 are generic actual types, or if they are
3107 -- in different scopes (e.g. parent and child instances), then verify
3108 -- that the base types are equal. Otherwise T1 and T2 must be
3109 -- on the same subtype chain. The whole purpose of this procedure
3110 -- is to prevent spurious ambiguities in an instantiation that may
3111 -- arise if two distinct generic types are instantiated with the
3112 -- same actual.
3114 ----------------------
3115 -- Base_Types_Match --
3116 ----------------------
3118 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
3119 begin
3120 if T1 = T2 then
3121 return True;
3123 elsif Base_Type (T1) = Base_Type (T2) then
3125 -- The following is too permissive. A more precise test must
3126 -- check that the generic actual is an ancestor subtype of the
3127 -- other ???.
3129 return not Is_Generic_Actual_Type (T1)
3130 or else not Is_Generic_Actual_Type (T2)
3131 or else Scope (T1) /= Scope (T2);
3133 -- In some cases a type imported through a limited_with clause,
3134 -- and its non-limited view are both visible, for example in an
3135 -- anonymous access_to_classwide type in a formal. Both entities
3136 -- designate the same type.
3138 elsif From_With_Type (T1)
3139 and then Ekind (T1) = E_Incomplete_Type
3140 and then T2 = Non_Limited_View (T1)
3141 then
3142 return True;
3144 else
3145 return False;
3146 end if;
3147 end Base_Types_Match;
3149 begin
3150 -- The context is an instance association for a formal
3151 -- access-to-subprogram type; the formal parameter types require
3152 -- mapping because they may denote other formal parameters of the
3153 -- generic unit.
3155 if Get_Inst then
3156 Type_1 := Get_Instance_Of (T1);
3157 Type_2 := Get_Instance_Of (T2);
3158 end if;
3160 -- First see if base types match
3162 if Base_Types_Match (Type_1, Type_2) then
3163 return Ctype <= Mode_Conformant
3164 or else Subtypes_Statically_Match (Type_1, Type_2);
3166 elsif Is_Incomplete_Or_Private_Type (Type_1)
3167 and then Present (Full_View (Type_1))
3168 and then Base_Types_Match (Full_View (Type_1), Type_2)
3169 then
3170 return Ctype <= Mode_Conformant
3171 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
3173 elsif Ekind (Type_2) = E_Incomplete_Type
3174 and then Present (Full_View (Type_2))
3175 and then Base_Types_Match (Type_1, Full_View (Type_2))
3176 then
3177 return Ctype <= Mode_Conformant
3178 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3180 elsif Is_Private_Type (Type_2)
3181 and then In_Instance
3182 and then Present (Full_View (Type_2))
3183 and then Base_Types_Match (Type_1, Full_View (Type_2))
3184 then
3185 return Ctype <= Mode_Conformant
3186 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3187 end if;
3189 -- Ada 2005 (AI-254): Detect anonymous access to subprogram types
3191 Are_Anonymous_Access_To_Subprogram_Types :=
3193 -- Case 1: Anonymous access to subprogram types
3195 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
3196 and then Ekind (Type_2) = E_Anonymous_Access_Subprogram_Type)
3198 -- Case 2: Anonymous access to PROTECTED subprogram types. In this
3199 -- case the anonymous type_declaration has been replaced by an
3200 -- occurrence of an internal access to subprogram type declaration
3201 -- available through the Original_Access_Type attribute
3203 or else
3204 (Ekind (Type_1) = E_Access_Protected_Subprogram_Type
3205 and then Ekind (Type_2) = E_Access_Protected_Subprogram_Type
3206 and then not Comes_From_Source (Type_1)
3207 and then not Comes_From_Source (Type_2)
3208 and then Present (Original_Access_Type (Type_1))
3209 and then Present (Original_Access_Type (Type_2))
3210 and then Ekind (Original_Access_Type (Type_1)) =
3211 E_Anonymous_Access_Protected_Subprogram_Type
3212 and then Ekind (Original_Access_Type (Type_2)) =
3213 E_Anonymous_Access_Protected_Subprogram_Type);
3215 -- Test anonymous access type case. For this case, static subtype
3216 -- matching is required for mode conformance (RM 6.3.1(15))
3218 if (Ekind (Type_1) = E_Anonymous_Access_Type
3219 and then Ekind (Type_2) = E_Anonymous_Access_Type)
3220 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
3221 then
3222 declare
3223 Desig_1 : Entity_Id;
3224 Desig_2 : Entity_Id;
3226 begin
3227 Desig_1 := Directly_Designated_Type (Type_1);
3229 -- An access parameter can designate an incomplete type
3231 if Ekind (Desig_1) = E_Incomplete_Type
3232 and then Present (Full_View (Desig_1))
3233 then
3234 Desig_1 := Full_View (Desig_1);
3235 end if;
3237 Desig_2 := Directly_Designated_Type (Type_2);
3239 if Ekind (Desig_2) = E_Incomplete_Type
3240 and then Present (Full_View (Desig_2))
3241 then
3242 Desig_2 := Full_View (Desig_2);
3243 end if;
3245 -- The context is an instance association for a formal
3246 -- access-to-subprogram type; formal access parameter designated
3247 -- types require mapping because they may denote other formal
3248 -- parameters of the generic unit.
3250 if Get_Inst then
3251 Desig_1 := Get_Instance_Of (Desig_1);
3252 Desig_2 := Get_Instance_Of (Desig_2);
3253 end if;
3255 -- It is possible for a Class_Wide_Type to be introduced for an
3256 -- incomplete type, in which case there is a separate class_ wide
3257 -- type for the full view. The types conform if their Etypes
3258 -- conform, i.e. one may be the full view of the other. This can
3259 -- only happen in the context of an access parameter, other uses
3260 -- of an incomplete Class_Wide_Type are illegal.
3262 if Is_Class_Wide_Type (Desig_1)
3263 and then Is_Class_Wide_Type (Desig_2)
3264 then
3265 return
3266 Conforming_Types
3267 (Etype (Base_Type (Desig_1)),
3268 Etype (Base_Type (Desig_2)), Ctype);
3270 elsif Are_Anonymous_Access_To_Subprogram_Types then
3271 return Ctype = Type_Conformant
3272 or else
3273 Subtypes_Statically_Match (Desig_1, Desig_2);
3275 else
3276 return Base_Type (Desig_1) = Base_Type (Desig_2)
3277 and then (Ctype = Type_Conformant
3278 or else
3279 Subtypes_Statically_Match (Desig_1, Desig_2));
3280 end if;
3281 end;
3283 -- Otherwise definitely no match
3285 else
3286 return False;
3287 end if;
3289 end Conforming_Types;
3291 --------------------------
3292 -- Create_Extra_Formals --
3293 --------------------------
3295 procedure Create_Extra_Formals (E : Entity_Id) is
3296 Formal : Entity_Id;
3297 Last_Extra : Entity_Id;
3298 Formal_Type : Entity_Id;
3299 P_Formal : Entity_Id := Empty;
3301 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id;
3302 -- Add an extra formal, associated with the current Formal. The extra
3303 -- formal is added to the list of extra formals, and also returned as
3304 -- the result. These formals are always of mode IN.
3306 ----------------------
3307 -- Add_Extra_Formal --
3308 ----------------------
3310 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id is
3311 EF : constant Entity_Id :=
3312 Make_Defining_Identifier (Sloc (Formal),
3313 Chars => New_External_Name (Chars (Formal), 'F'));
3315 begin
3316 -- We never generate extra formals if expansion is not active
3317 -- because we don't need them unless we are generating code.
3319 if not Expander_Active then
3320 return Empty;
3321 end if;
3323 -- A little optimization. Never generate an extra formal for the
3324 -- _init operand of an initialization procedure, since it could
3325 -- never be used.
3327 if Chars (Formal) = Name_uInit then
3328 return Empty;
3329 end if;
3331 Set_Ekind (EF, E_In_Parameter);
3332 Set_Actual_Subtype (EF, Typ);
3333 Set_Etype (EF, Typ);
3334 Set_Scope (EF, Scope (Formal));
3335 Set_Mechanism (EF, Default_Mechanism);
3336 Set_Formal_Validity (EF);
3338 Set_Extra_Formal (Last_Extra, EF);
3339 Last_Extra := EF;
3340 return EF;
3341 end Add_Extra_Formal;
3343 -- Start of processing for Create_Extra_Formals
3345 begin
3346 -- If this is a derived subprogram then the subtypes of the parent
3347 -- subprogram's formal parameters will be used to to determine the need
3348 -- for extra formals.
3350 if Is_Overloadable (E) and then Present (Alias (E)) then
3351 P_Formal := First_Formal (Alias (E));
3352 end if;
3354 Last_Extra := Empty;
3355 Formal := First_Formal (E);
3356 while Present (Formal) loop
3357 Last_Extra := Formal;
3358 Next_Formal (Formal);
3359 end loop;
3361 -- If Extra_formals where already created, don't do it again. This
3362 -- situation may arise for subprogram types created as part of
3363 -- dispatching calls (see Expand_Dispatching_Call)
3365 if Present (Last_Extra) and then
3366 Present (Extra_Formal (Last_Extra))
3367 then
3368 return;
3369 end if;
3371 Formal := First_Formal (E);
3373 while Present (Formal) loop
3375 -- Create extra formal for supporting the attribute 'Constrained.
3376 -- The case of a private type view without discriminants also
3377 -- requires the extra formal if the underlying type has defaulted
3378 -- discriminants.
3380 if Ekind (Formal) /= E_In_Parameter then
3381 if Present (P_Formal) then
3382 Formal_Type := Etype (P_Formal);
3383 else
3384 Formal_Type := Etype (Formal);
3385 end if;
3387 -- Do not produce extra formals for Unchecked_Union parameters.
3388 -- Jump directly to the end of the loop.
3390 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
3391 goto Skip_Extra_Formal_Generation;
3392 end if;
3394 if not Has_Discriminants (Formal_Type)
3395 and then Ekind (Formal_Type) in Private_Kind
3396 and then Present (Underlying_Type (Formal_Type))
3397 then
3398 Formal_Type := Underlying_Type (Formal_Type);
3399 end if;
3401 if Has_Discriminants (Formal_Type)
3402 and then
3403 ((not Is_Constrained (Formal_Type)
3404 and then not Is_Indefinite_Subtype (Formal_Type))
3405 or else Present (Extra_Formal (Formal)))
3406 then
3407 Set_Extra_Constrained
3408 (Formal, Add_Extra_Formal (Standard_Boolean));
3409 end if;
3410 end if;
3412 -- Create extra formal for supporting accessibility checking
3414 -- This is suppressed if we specifically suppress accessibility
3415 -- checks at the pacage level for either the subprogram, or the
3416 -- package in which it resides. However, we do not suppress it
3417 -- simply if the scope has accessibility checks suppressed, since
3418 -- this could cause trouble when clients are compiled with a
3419 -- different suppression setting. The explicit checks at the
3420 -- package level are safe from this point of view.
3422 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
3423 and then not
3424 (Explicit_Suppress (E, Accessibility_Check)
3425 or else
3426 Explicit_Suppress (Scope (E), Accessibility_Check))
3427 and then
3428 (not Present (P_Formal)
3429 or else Present (Extra_Accessibility (P_Formal)))
3430 then
3431 -- Temporary kludge: for now we avoid creating the extra formal
3432 -- for access parameters of protected operations because of
3433 -- problem with the case of internal protected calls. ???
3435 if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
3436 and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
3437 then
3438 Set_Extra_Accessibility
3439 (Formal, Add_Extra_Formal (Standard_Natural));
3440 end if;
3441 end if;
3443 if Present (P_Formal) then
3444 Next_Formal (P_Formal);
3445 end if;
3447 -- This label is required when skipping extra formal generation for
3448 -- Unchecked_Union parameters.
3450 <<Skip_Extra_Formal_Generation>>
3452 Next_Formal (Formal);
3453 end loop;
3454 end Create_Extra_Formals;
3456 -----------------------------
3457 -- Enter_Overloaded_Entity --
3458 -----------------------------
3460 procedure Enter_Overloaded_Entity (S : Entity_Id) is
3461 E : Entity_Id := Current_Entity_In_Scope (S);
3462 C_E : Entity_Id := Current_Entity (S);
3464 begin
3465 if Present (E) then
3466 Set_Has_Homonym (E);
3467 Set_Has_Homonym (S);
3468 end if;
3470 Set_Is_Immediately_Visible (S);
3471 Set_Scope (S, Current_Scope);
3473 -- Chain new entity if front of homonym in current scope, so that
3474 -- homonyms are contiguous.
3476 if Present (E)
3477 and then E /= C_E
3478 then
3479 while Homonym (C_E) /= E loop
3480 C_E := Homonym (C_E);
3481 end loop;
3483 Set_Homonym (C_E, S);
3485 else
3486 E := C_E;
3487 Set_Current_Entity (S);
3488 end if;
3490 Set_Homonym (S, E);
3492 Append_Entity (S, Current_Scope);
3493 Set_Public_Status (S);
3495 if Debug_Flag_E then
3496 Write_Str ("New overloaded entity chain: ");
3497 Write_Name (Chars (S));
3499 E := S;
3500 while Present (E) loop
3501 Write_Str (" "); Write_Int (Int (E));
3502 E := Homonym (E);
3503 end loop;
3505 Write_Eol;
3506 end if;
3508 -- Generate warning for hiding
3510 if Warn_On_Hiding
3511 and then Comes_From_Source (S)
3512 and then In_Extended_Main_Source_Unit (S)
3513 then
3514 E := S;
3515 loop
3516 E := Homonym (E);
3517 exit when No (E);
3519 -- Warn unless genuine overloading
3521 if (not Is_Overloadable (E))
3522 or else Subtype_Conformant (E, S)
3523 then
3524 Error_Msg_Sloc := Sloc (E);
3525 Error_Msg_N ("declaration of & hides one#?", S);
3526 end if;
3527 end loop;
3528 end if;
3529 end Enter_Overloaded_Entity;
3531 -----------------------------
3532 -- Find_Corresponding_Spec --
3533 -----------------------------
3535 function Find_Corresponding_Spec (N : Node_Id) return Entity_Id is
3536 Spec : constant Node_Id := Specification (N);
3537 Designator : constant Entity_Id := Defining_Entity (Spec);
3539 E : Entity_Id;
3541 begin
3542 E := Current_Entity (Designator);
3544 while Present (E) loop
3546 -- We are looking for a matching spec. It must have the same scope,
3547 -- and the same name, and either be type conformant, or be the case
3548 -- of a library procedure spec and its body (which belong to one
3549 -- another regardless of whether they are type conformant or not).
3551 if Scope (E) = Current_Scope then
3552 if Current_Scope = Standard_Standard
3553 or else (Ekind (E) = Ekind (Designator)
3554 and then Type_Conformant (E, Designator))
3555 then
3556 -- Within an instantiation, we know that spec and body are
3557 -- subtype conformant, because they were subtype conformant
3558 -- in the generic. We choose the subtype-conformant entity
3559 -- here as well, to resolve spurious ambiguities in the
3560 -- instance that were not present in the generic (i.e. when
3561 -- two different types are given the same actual). If we are
3562 -- looking for a spec to match a body, full conformance is
3563 -- expected.
3565 if In_Instance then
3566 Set_Convention (Designator, Convention (E));
3568 if Nkind (N) = N_Subprogram_Body
3569 and then Present (Homonym (E))
3570 and then not Fully_Conformant (E, Designator)
3571 then
3572 goto Next_Entity;
3574 elsif not Subtype_Conformant (E, Designator) then
3575 goto Next_Entity;
3576 end if;
3577 end if;
3579 if not Has_Completion (E) then
3581 if Nkind (N) /= N_Subprogram_Body_Stub then
3582 Set_Corresponding_Spec (N, E);
3583 end if;
3585 Set_Has_Completion (E);
3586 return E;
3588 elsif Nkind (Parent (N)) = N_Subunit then
3590 -- If this is the proper body of a subunit, the completion
3591 -- flag is set when analyzing the stub.
3593 return E;
3595 -- If body already exists, this is an error unless the
3596 -- previous declaration is the implicit declaration of
3597 -- a derived subprogram, or this is a spurious overloading
3598 -- in an instance.
3600 elsif No (Alias (E))
3601 and then not Is_Intrinsic_Subprogram (E)
3602 and then not In_Instance
3603 then
3604 Error_Msg_Sloc := Sloc (E);
3605 if Is_Imported (E) then
3606 Error_Msg_NE
3607 ("body not allowed for imported subprogram & declared#",
3608 N, E);
3609 else
3610 Error_Msg_NE ("duplicate body for & declared#", N, E);
3611 end if;
3612 end if;
3614 elsif Is_Child_Unit (E)
3615 and then
3616 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
3617 and then
3618 Nkind (Parent (Unit_Declaration_Node (Designator)))
3619 = N_Compilation_Unit
3620 then
3622 -- Child units cannot be overloaded, so a conformance mismatch
3623 -- between body and a previous spec is an error.
3625 Error_Msg_N
3626 ("body of child unit does not match previous declaration", N);
3627 end if;
3628 end if;
3630 <<Next_Entity>>
3631 E := Homonym (E);
3632 end loop;
3634 -- On exit, we know that no previous declaration of subprogram exists
3636 return Empty;
3637 end Find_Corresponding_Spec;
3639 ----------------------
3640 -- Fully_Conformant --
3641 ----------------------
3643 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
3644 Result : Boolean;
3646 begin
3647 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
3648 return Result;
3649 end Fully_Conformant;
3651 ----------------------------------
3652 -- Fully_Conformant_Expressions --
3653 ----------------------------------
3655 function Fully_Conformant_Expressions
3656 (Given_E1 : Node_Id;
3657 Given_E2 : Node_Id) return Boolean
3659 E1 : constant Node_Id := Original_Node (Given_E1);
3660 E2 : constant Node_Id := Original_Node (Given_E2);
3661 -- We always test conformance on original nodes, since it is possible
3662 -- for analysis and/or expansion to make things look as though they
3663 -- conform when they do not, e.g. by converting 1+2 into 3.
3665 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
3666 renames Fully_Conformant_Expressions;
3668 function FCL (L1, L2 : List_Id) return Boolean;
3669 -- Compare elements of two lists for conformance. Elements have to
3670 -- be conformant, and actuals inserted as default parameters do not
3671 -- match explicit actuals with the same value.
3673 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
3674 -- Compare an operator node with a function call
3676 ---------
3677 -- FCL --
3678 ---------
3680 function FCL (L1, L2 : List_Id) return Boolean is
3681 N1, N2 : Node_Id;
3683 begin
3684 if L1 = No_List then
3685 N1 := Empty;
3686 else
3687 N1 := First (L1);
3688 end if;
3690 if L2 = No_List then
3691 N2 := Empty;
3692 else
3693 N2 := First (L2);
3694 end if;
3696 -- Compare two lists, skipping rewrite insertions (we want to
3697 -- compare the original trees, not the expanded versions!)
3699 loop
3700 if Is_Rewrite_Insertion (N1) then
3701 Next (N1);
3702 elsif Is_Rewrite_Insertion (N2) then
3703 Next (N2);
3704 elsif No (N1) then
3705 return No (N2);
3706 elsif No (N2) then
3707 return False;
3708 elsif not FCE (N1, N2) then
3709 return False;
3710 else
3711 Next (N1);
3712 Next (N2);
3713 end if;
3714 end loop;
3715 end FCL;
3717 ---------
3718 -- FCO --
3719 ---------
3721 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
3722 Actuals : constant List_Id := Parameter_Associations (Call_Node);
3723 Act : Node_Id;
3725 begin
3726 if No (Actuals)
3727 or else Entity (Op_Node) /= Entity (Name (Call_Node))
3728 then
3729 return False;
3731 else
3732 Act := First (Actuals);
3734 if Nkind (Op_Node) in N_Binary_Op then
3736 if not FCE (Left_Opnd (Op_Node), Act) then
3737 return False;
3738 end if;
3740 Next (Act);
3741 end if;
3743 return Present (Act)
3744 and then FCE (Right_Opnd (Op_Node), Act)
3745 and then No (Next (Act));
3746 end if;
3747 end FCO;
3749 -- Start of processing for Fully_Conformant_Expressions
3751 begin
3752 -- Non-conformant if paren count does not match. Note: if some idiot
3753 -- complains that we don't do this right for more than 3 levels of
3754 -- parentheses, they will be treated with the respect they deserve :-)
3756 if Paren_Count (E1) /= Paren_Count (E2) then
3757 return False;
3759 -- If same entities are referenced, then they are conformant even if
3760 -- they have different forms (RM 8.3.1(19-20)).
3762 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
3763 if Present (Entity (E1)) then
3764 return Entity (E1) = Entity (E2)
3765 or else (Chars (Entity (E1)) = Chars (Entity (E2))
3766 and then Ekind (Entity (E1)) = E_Discriminant
3767 and then Ekind (Entity (E2)) = E_In_Parameter);
3769 elsif Nkind (E1) = N_Expanded_Name
3770 and then Nkind (E2) = N_Expanded_Name
3771 and then Nkind (Selector_Name (E1)) = N_Character_Literal
3772 and then Nkind (Selector_Name (E2)) = N_Character_Literal
3773 then
3774 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
3776 else
3777 -- Identifiers in component associations don't always have
3778 -- entities, but their names must conform.
3780 return Nkind (E1) = N_Identifier
3781 and then Nkind (E2) = N_Identifier
3782 and then Chars (E1) = Chars (E2);
3783 end if;
3785 elsif Nkind (E1) = N_Character_Literal
3786 and then Nkind (E2) = N_Expanded_Name
3787 then
3788 return Nkind (Selector_Name (E2)) = N_Character_Literal
3789 and then Chars (E1) = Chars (Selector_Name (E2));
3791 elsif Nkind (E2) = N_Character_Literal
3792 and then Nkind (E1) = N_Expanded_Name
3793 then
3794 return Nkind (Selector_Name (E1)) = N_Character_Literal
3795 and then Chars (E2) = Chars (Selector_Name (E1));
3797 elsif Nkind (E1) in N_Op
3798 and then Nkind (E2) = N_Function_Call
3799 then
3800 return FCO (E1, E2);
3802 elsif Nkind (E2) in N_Op
3803 and then Nkind (E1) = N_Function_Call
3804 then
3805 return FCO (E2, E1);
3807 -- Otherwise we must have the same syntactic entity
3809 elsif Nkind (E1) /= Nkind (E2) then
3810 return False;
3812 -- At this point, we specialize by node type
3814 else
3815 case Nkind (E1) is
3817 when N_Aggregate =>
3818 return
3819 FCL (Expressions (E1), Expressions (E2))
3820 and then FCL (Component_Associations (E1),
3821 Component_Associations (E2));
3823 when N_Allocator =>
3824 if Nkind (Expression (E1)) = N_Qualified_Expression
3825 or else
3826 Nkind (Expression (E2)) = N_Qualified_Expression
3827 then
3828 return FCE (Expression (E1), Expression (E2));
3830 -- Check that the subtype marks and any constraints
3831 -- are conformant
3833 else
3834 declare
3835 Indic1 : constant Node_Id := Expression (E1);
3836 Indic2 : constant Node_Id := Expression (E2);
3837 Elt1 : Node_Id;
3838 Elt2 : Node_Id;
3840 begin
3841 if Nkind (Indic1) /= N_Subtype_Indication then
3842 return
3843 Nkind (Indic2) /= N_Subtype_Indication
3844 and then Entity (Indic1) = Entity (Indic2);
3846 elsif Nkind (Indic2) /= N_Subtype_Indication then
3847 return
3848 Nkind (Indic1) /= N_Subtype_Indication
3849 and then Entity (Indic1) = Entity (Indic2);
3851 else
3852 if Entity (Subtype_Mark (Indic1)) /=
3853 Entity (Subtype_Mark (Indic2))
3854 then
3855 return False;
3856 end if;
3858 Elt1 := First (Constraints (Constraint (Indic1)));
3859 Elt2 := First (Constraints (Constraint (Indic2)));
3861 while Present (Elt1) and then Present (Elt2) loop
3862 if not FCE (Elt1, Elt2) then
3863 return False;
3864 end if;
3866 Next (Elt1);
3867 Next (Elt2);
3868 end loop;
3870 return True;
3871 end if;
3872 end;
3873 end if;
3875 when N_Attribute_Reference =>
3876 return
3877 Attribute_Name (E1) = Attribute_Name (E2)
3878 and then FCL (Expressions (E1), Expressions (E2));
3880 when N_Binary_Op =>
3881 return
3882 Entity (E1) = Entity (E2)
3883 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
3884 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
3886 when N_And_Then | N_Or_Else | N_In | N_Not_In =>
3887 return
3888 FCE (Left_Opnd (E1), Left_Opnd (E2))
3889 and then
3890 FCE (Right_Opnd (E1), Right_Opnd (E2));
3892 when N_Character_Literal =>
3893 return
3894 Char_Literal_Value (E1) = Char_Literal_Value (E2);
3896 when N_Component_Association =>
3897 return
3898 FCL (Choices (E1), Choices (E2))
3899 and then FCE (Expression (E1), Expression (E2));
3901 when N_Conditional_Expression =>
3902 return
3903 FCL (Expressions (E1), Expressions (E2));
3905 when N_Explicit_Dereference =>
3906 return
3907 FCE (Prefix (E1), Prefix (E2));
3909 when N_Extension_Aggregate =>
3910 return
3911 FCL (Expressions (E1), Expressions (E2))
3912 and then Null_Record_Present (E1) =
3913 Null_Record_Present (E2)
3914 and then FCL (Component_Associations (E1),
3915 Component_Associations (E2));
3917 when N_Function_Call =>
3918 return
3919 FCE (Name (E1), Name (E2))
3920 and then FCL (Parameter_Associations (E1),
3921 Parameter_Associations (E2));
3923 when N_Indexed_Component =>
3924 return
3925 FCE (Prefix (E1), Prefix (E2))
3926 and then FCL (Expressions (E1), Expressions (E2));
3928 when N_Integer_Literal =>
3929 return (Intval (E1) = Intval (E2));
3931 when N_Null =>
3932 return True;
3934 when N_Operator_Symbol =>
3935 return
3936 Chars (E1) = Chars (E2);
3938 when N_Others_Choice =>
3939 return True;
3941 when N_Parameter_Association =>
3942 return
3943 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
3944 and then FCE (Explicit_Actual_Parameter (E1),
3945 Explicit_Actual_Parameter (E2));
3947 when N_Qualified_Expression =>
3948 return
3949 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3950 and then FCE (Expression (E1), Expression (E2));
3952 when N_Range =>
3953 return
3954 FCE (Low_Bound (E1), Low_Bound (E2))
3955 and then FCE (High_Bound (E1), High_Bound (E2));
3957 when N_Real_Literal =>
3958 return (Realval (E1) = Realval (E2));
3960 when N_Selected_Component =>
3961 return
3962 FCE (Prefix (E1), Prefix (E2))
3963 and then FCE (Selector_Name (E1), Selector_Name (E2));
3965 when N_Slice =>
3966 return
3967 FCE (Prefix (E1), Prefix (E2))
3968 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
3970 when N_String_Literal =>
3971 declare
3972 S1 : constant String_Id := Strval (E1);
3973 S2 : constant String_Id := Strval (E2);
3974 L1 : constant Nat := String_Length (S1);
3975 L2 : constant Nat := String_Length (S2);
3977 begin
3978 if L1 /= L2 then
3979 return False;
3981 else
3982 for J in 1 .. L1 loop
3983 if Get_String_Char (S1, J) /=
3984 Get_String_Char (S2, J)
3985 then
3986 return False;
3987 end if;
3988 end loop;
3990 return True;
3991 end if;
3992 end;
3994 when N_Type_Conversion =>
3995 return
3996 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3997 and then FCE (Expression (E1), Expression (E2));
3999 when N_Unary_Op =>
4000 return
4001 Entity (E1) = Entity (E2)
4002 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
4004 when N_Unchecked_Type_Conversion =>
4005 return
4006 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4007 and then FCE (Expression (E1), Expression (E2));
4009 -- All other node types cannot appear in this context. Strictly
4010 -- we should raise a fatal internal error. Instead we just ignore
4011 -- the nodes. This means that if anyone makes a mistake in the
4012 -- expander and mucks an expression tree irretrievably, the
4013 -- result will be a failure to detect a (probably very obscure)
4014 -- case of non-conformance, which is better than bombing on some
4015 -- case where two expressions do in fact conform.
4017 when others =>
4018 return True;
4020 end case;
4021 end if;
4022 end Fully_Conformant_Expressions;
4024 ----------------------------------------
4025 -- Fully_Conformant_Discrete_Subtypes --
4026 ----------------------------------------
4028 function Fully_Conformant_Discrete_Subtypes
4029 (Given_S1 : Node_Id;
4030 Given_S2 : Node_Id) return Boolean
4032 S1 : constant Node_Id := Original_Node (Given_S1);
4033 S2 : constant Node_Id := Original_Node (Given_S2);
4035 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
4036 -- Special-case for a bound given by a discriminant, which in the body
4037 -- is replaced with the discriminal of the enclosing type.
4039 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
4040 -- Check both bounds
4042 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
4043 begin
4044 if Is_Entity_Name (B1)
4045 and then Is_Entity_Name (B2)
4046 and then Ekind (Entity (B1)) = E_Discriminant
4047 then
4048 return Chars (B1) = Chars (B2);
4050 else
4051 return Fully_Conformant_Expressions (B1, B2);
4052 end if;
4053 end Conforming_Bounds;
4055 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
4056 begin
4057 return
4058 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
4059 and then
4060 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
4061 end Conforming_Ranges;
4063 -- Start of processing for Fully_Conformant_Discrete_Subtypes
4065 begin
4066 if Nkind (S1) /= Nkind (S2) then
4067 return False;
4069 elsif Is_Entity_Name (S1) then
4070 return Entity (S1) = Entity (S2);
4072 elsif Nkind (S1) = N_Range then
4073 return Conforming_Ranges (S1, S2);
4075 elsif Nkind (S1) = N_Subtype_Indication then
4076 return
4077 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
4078 and then
4079 Conforming_Ranges
4080 (Range_Expression (Constraint (S1)),
4081 Range_Expression (Constraint (S2)));
4082 else
4083 return True;
4084 end if;
4085 end Fully_Conformant_Discrete_Subtypes;
4087 --------------------
4088 -- Install_Entity --
4089 --------------------
4091 procedure Install_Entity (E : Entity_Id) is
4092 Prev : constant Entity_Id := Current_Entity (E);
4094 begin
4095 Set_Is_Immediately_Visible (E);
4096 Set_Current_Entity (E);
4097 Set_Homonym (E, Prev);
4098 end Install_Entity;
4100 ---------------------
4101 -- Install_Formals --
4102 ---------------------
4104 procedure Install_Formals (Id : Entity_Id) is
4105 F : Entity_Id;
4107 begin
4108 F := First_Formal (Id);
4110 while Present (F) loop
4111 Install_Entity (F);
4112 Next_Formal (F);
4113 end loop;
4114 end Install_Formals;
4116 ---------------------------------
4117 -- Is_Non_Overriding_Operation --
4118 ---------------------------------
4120 function Is_Non_Overriding_Operation
4121 (Prev_E : Entity_Id;
4122 New_E : Entity_Id) return Boolean
4124 Formal : Entity_Id;
4125 F_Typ : Entity_Id;
4126 G_Typ : Entity_Id := Empty;
4128 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
4129 -- If F_Type is a derived type associated with a generic actual
4130 -- subtype, then return its Generic_Parent_Type attribute, else return
4131 -- Empty.
4133 function Types_Correspond
4134 (P_Type : Entity_Id;
4135 N_Type : Entity_Id) return Boolean;
4136 -- Returns true if and only if the types (or designated types in the
4137 -- case of anonymous access types) are the same or N_Type is derived
4138 -- directly or indirectly from P_Type.
4140 -----------------------------
4141 -- Get_Generic_Parent_Type --
4142 -----------------------------
4144 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
4145 G_Typ : Entity_Id;
4146 Indic : Node_Id;
4148 begin
4149 if Is_Derived_Type (F_Typ)
4150 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
4151 then
4152 -- The tree must be traversed to determine the parent subtype in
4153 -- the generic unit, which unfortunately isn't always available
4154 -- via semantic attributes. ??? (Note: The use of Original_Node
4155 -- is needed for cases where a full derived type has been
4156 -- rewritten.)
4158 Indic := Subtype_Indication
4159 (Type_Definition (Original_Node (Parent (F_Typ))));
4161 if Nkind (Indic) = N_Subtype_Indication then
4162 G_Typ := Entity (Subtype_Mark (Indic));
4163 else
4164 G_Typ := Entity (Indic);
4165 end if;
4167 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
4168 and then Present (Generic_Parent_Type (Parent (G_Typ)))
4169 then
4170 return Generic_Parent_Type (Parent (G_Typ));
4171 end if;
4172 end if;
4174 return Empty;
4175 end Get_Generic_Parent_Type;
4177 ----------------------
4178 -- Types_Correspond --
4179 ----------------------
4181 function Types_Correspond
4182 (P_Type : Entity_Id;
4183 N_Type : Entity_Id) return Boolean
4185 Prev_Type : Entity_Id := Base_Type (P_Type);
4186 New_Type : Entity_Id := Base_Type (N_Type);
4188 begin
4189 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
4190 Prev_Type := Designated_Type (Prev_Type);
4191 end if;
4193 if Ekind (New_Type) = E_Anonymous_Access_Type then
4194 New_Type := Designated_Type (New_Type);
4195 end if;
4197 if Prev_Type = New_Type then
4198 return True;
4200 elsif not Is_Class_Wide_Type (New_Type) then
4201 while Etype (New_Type) /= New_Type loop
4202 New_Type := Etype (New_Type);
4203 if New_Type = Prev_Type then
4204 return True;
4205 end if;
4206 end loop;
4207 end if;
4208 return False;
4209 end Types_Correspond;
4211 -- Start of processing for Is_Non_Overriding_Operation
4213 begin
4214 -- In the case where both operations are implicit derived subprograms
4215 -- then neither overrides the other. This can only occur in certain
4216 -- obscure cases (e.g., derivation from homographs created in a generic
4217 -- instantiation).
4219 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
4220 return True;
4222 elsif Ekind (Current_Scope) = E_Package
4223 and then Is_Generic_Instance (Current_Scope)
4224 and then In_Private_Part (Current_Scope)
4225 and then Comes_From_Source (New_E)
4226 then
4227 -- We examine the formals and result subtype of the inherited
4228 -- operation, to determine whether their type is derived from (the
4229 -- instance of) a generic type.
4231 Formal := First_Formal (Prev_E);
4233 while Present (Formal) loop
4234 F_Typ := Base_Type (Etype (Formal));
4236 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4237 F_Typ := Designated_Type (F_Typ);
4238 end if;
4240 G_Typ := Get_Generic_Parent_Type (F_Typ);
4242 Next_Formal (Formal);
4243 end loop;
4245 if not Present (G_Typ) and then Ekind (Prev_E) = E_Function then
4246 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
4247 end if;
4249 if No (G_Typ) then
4250 return False;
4251 end if;
4253 -- If the generic type is a private type, then the original
4254 -- operation was not overriding in the generic, because there was
4255 -- no primitive operation to override.
4257 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
4258 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
4259 N_Formal_Private_Type_Definition
4260 then
4261 return True;
4263 -- The generic parent type is the ancestor of a formal derived
4264 -- type declaration. We need to check whether it has a primitive
4265 -- operation that should be overridden by New_E in the generic.
4267 else
4268 declare
4269 P_Formal : Entity_Id;
4270 N_Formal : Entity_Id;
4271 P_Typ : Entity_Id;
4272 N_Typ : Entity_Id;
4273 P_Prim : Entity_Id;
4274 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
4276 begin
4277 while Present (Prim_Elt) loop
4278 P_Prim := Node (Prim_Elt);
4280 if Chars (P_Prim) = Chars (New_E)
4281 and then Ekind (P_Prim) = Ekind (New_E)
4282 then
4283 P_Formal := First_Formal (P_Prim);
4284 N_Formal := First_Formal (New_E);
4285 while Present (P_Formal) and then Present (N_Formal) loop
4286 P_Typ := Etype (P_Formal);
4287 N_Typ := Etype (N_Formal);
4289 if not Types_Correspond (P_Typ, N_Typ) then
4290 exit;
4291 end if;
4293 Next_Entity (P_Formal);
4294 Next_Entity (N_Formal);
4295 end loop;
4297 -- Found a matching primitive operation belonging to the
4298 -- formal ancestor type, so the new subprogram is
4299 -- overriding.
4301 if not Present (P_Formal)
4302 and then not Present (N_Formal)
4303 and then (Ekind (New_E) /= E_Function
4304 or else
4305 Types_Correspond
4306 (Etype (P_Prim), Etype (New_E)))
4307 then
4308 return False;
4309 end if;
4310 end if;
4312 Next_Elmt (Prim_Elt);
4313 end loop;
4315 -- If no match found, then the new subprogram does not
4316 -- override in the generic (nor in the instance).
4318 return True;
4319 end;
4320 end if;
4321 else
4322 return False;
4323 end if;
4324 end Is_Non_Overriding_Operation;
4326 ------------------------------
4327 -- Make_Inequality_Operator --
4328 ------------------------------
4330 -- S is the defining identifier of an equality operator. We build a
4331 -- subprogram declaration with the right signature. This operation is
4332 -- intrinsic, because it is always expanded as the negation of the
4333 -- call to the equality function.
4335 procedure Make_Inequality_Operator (S : Entity_Id) is
4336 Loc : constant Source_Ptr := Sloc (S);
4337 Decl : Node_Id;
4338 Formals : List_Id;
4339 Op_Name : Entity_Id;
4341 A : Entity_Id;
4342 B : Entity_Id;
4344 begin
4345 -- Check that equality was properly defined
4347 if No (Next_Formal (First_Formal (S))) then
4348 return;
4349 end if;
4351 A := Make_Defining_Identifier (Loc, Chars (First_Formal (S)));
4352 B := Make_Defining_Identifier (Loc,
4353 Chars (Next_Formal (First_Formal (S))));
4355 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
4357 Formals := New_List (
4358 Make_Parameter_Specification (Loc,
4359 Defining_Identifier => A,
4360 Parameter_Type =>
4361 New_Reference_To (Etype (First_Formal (S)), Loc)),
4363 Make_Parameter_Specification (Loc,
4364 Defining_Identifier => B,
4365 Parameter_Type =>
4366 New_Reference_To (Etype (Next_Formal (First_Formal (S))), Loc)));
4368 Decl :=
4369 Make_Subprogram_Declaration (Loc,
4370 Specification =>
4371 Make_Function_Specification (Loc,
4372 Defining_Unit_Name => Op_Name,
4373 Parameter_Specifications => Formals,
4374 Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)));
4376 -- Insert inequality right after equality if it is explicit or after
4377 -- the derived type when implicit. These entities are created only
4378 -- for visibility purposes, and eventually replaced in the course of
4379 -- expansion, so they do not need to be attached to the tree and seen
4380 -- by the back-end. Keeping them internal also avoids spurious freezing
4381 -- problems. The parent field is set simply to make analysis safe.
4383 if No (Alias (S)) then
4384 Set_Parent (Decl, Parent (Unit_Declaration_Node (S)));
4385 else
4386 Set_Parent (Decl, Parent (Parent (Etype (First_Formal (S)))));
4387 end if;
4389 Mark_Rewrite_Insertion (Decl);
4390 Set_Is_Intrinsic_Subprogram (Op_Name);
4391 Analyze (Decl);
4392 Set_Has_Completion (Op_Name);
4393 Set_Corresponding_Equality (Op_Name, S);
4394 Set_Is_Abstract (Op_Name, Is_Abstract (S));
4396 end Make_Inequality_Operator;
4398 ----------------------
4399 -- May_Need_Actuals --
4400 ----------------------
4402 procedure May_Need_Actuals (Fun : Entity_Id) is
4403 F : Entity_Id;
4404 B : Boolean;
4406 begin
4407 F := First_Formal (Fun);
4408 B := True;
4410 while Present (F) loop
4411 if No (Default_Value (F)) then
4412 B := False;
4413 exit;
4414 end if;
4416 Next_Formal (F);
4417 end loop;
4419 Set_Needs_No_Actuals (Fun, B);
4420 end May_Need_Actuals;
4422 ---------------------
4423 -- Mode_Conformant --
4424 ---------------------
4426 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
4427 Result : Boolean;
4428 begin
4429 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
4430 return Result;
4431 end Mode_Conformant;
4433 ---------------------------
4434 -- New_Overloaded_Entity --
4435 ---------------------------
4437 procedure New_Overloaded_Entity
4438 (S : Entity_Id;
4439 Derived_Type : Entity_Id := Empty)
4441 E : Entity_Id;
4442 -- Entity that S overrides
4444 Prev_Vis : Entity_Id := Empty;
4445 -- Needs comment ???
4447 function Is_Private_Declaration (E : Entity_Id) return Boolean;
4448 -- Check that E is declared in the private part of the current package,
4449 -- or in the package body, where it may hide a previous declaration.
4450 -- We can't use In_Private_Part by itself because this flag is also
4451 -- set when freezing entities, so we must examine the place of the
4452 -- declaration in the tree, and recognize wrapper packages as well.
4454 procedure Maybe_Primitive_Operation (Is_Overriding : Boolean := False);
4455 -- If the subprogram being analyzed is a primitive operation of
4456 -- the type of one of its formals, set the corresponding flag.
4458 ----------------------------
4459 -- Is_Private_Declaration --
4460 ----------------------------
4462 function Is_Private_Declaration (E : Entity_Id) return Boolean is
4463 Priv_Decls : List_Id;
4464 Decl : constant Node_Id := Unit_Declaration_Node (E);
4466 begin
4467 if Is_Package (Current_Scope)
4468 and then In_Private_Part (Current_Scope)
4469 then
4470 Priv_Decls :=
4471 Private_Declarations (
4472 Specification (Unit_Declaration_Node (Current_Scope)));
4474 return In_Package_Body (Current_Scope)
4475 or else List_Containing (Decl) = Priv_Decls
4476 or else (Nkind (Parent (Decl)) = N_Package_Specification
4477 and then not Is_Compilation_Unit (
4478 Defining_Entity (Parent (Decl)))
4479 and then List_Containing (Parent (Parent (Decl)))
4480 = Priv_Decls);
4481 else
4482 return False;
4483 end if;
4484 end Is_Private_Declaration;
4486 -------------------------------
4487 -- Maybe_Primitive_Operation --
4488 -------------------------------
4490 procedure Maybe_Primitive_Operation (Is_Overriding : Boolean := False) is
4491 Formal : Entity_Id;
4492 F_Typ : Entity_Id;
4493 B_Typ : Entity_Id;
4495 function Visible_Part_Type (T : Entity_Id) return Boolean;
4496 -- Returns true if T is declared in the visible part of
4497 -- the current package scope; otherwise returns false.
4498 -- Assumes that T is declared in a package.
4500 procedure Check_Private_Overriding (T : Entity_Id);
4501 -- Checks that if a primitive abstract subprogram of a visible
4502 -- abstract type is declared in a private part, then it must
4503 -- override an abstract subprogram declared in the visible part.
4504 -- Also checks that if a primitive function with a controlling
4505 -- result is declared in a private part, then it must override
4506 -- a function declared in the visible part.
4508 ------------------------------
4509 -- Check_Private_Overriding --
4510 ------------------------------
4512 procedure Check_Private_Overriding (T : Entity_Id) is
4513 begin
4514 if Ekind (Current_Scope) = E_Package
4515 and then In_Private_Part (Current_Scope)
4516 and then Visible_Part_Type (T)
4517 and then not In_Instance
4518 then
4519 if Is_Abstract (T)
4520 and then Is_Abstract (S)
4521 and then (not Is_Overriding or else not Is_Abstract (E))
4522 then
4523 Error_Msg_N ("abstract subprograms must be visible "
4524 & "('R'M 3.9.3(10))!", S);
4526 elsif Ekind (S) = E_Function
4527 and then Is_Tagged_Type (T)
4528 and then T = Base_Type (Etype (S))
4529 and then not Is_Overriding
4530 then
4531 Error_Msg_N
4532 ("private function with tagged result must"
4533 & " override visible-part function", S);
4534 Error_Msg_N
4535 ("\move subprogram to the visible part"
4536 & " ('R'M 3.9.3(10))", S);
4537 end if;
4538 end if;
4539 end Check_Private_Overriding;
4541 -----------------------
4542 -- Visible_Part_Type --
4543 -----------------------
4545 function Visible_Part_Type (T : Entity_Id) return Boolean is
4546 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
4547 N : Node_Id;
4549 begin
4550 -- If the entity is a private type, then it must be
4551 -- declared in a visible part.
4553 if Ekind (T) in Private_Kind then
4554 return True;
4555 end if;
4557 -- Otherwise, we traverse the visible part looking for its
4558 -- corresponding declaration. We cannot use the declaration
4559 -- node directly because in the private part the entity of a
4560 -- private type is the one in the full view, which does not
4561 -- indicate that it is the completion of something visible.
4563 N := First (Visible_Declarations (Specification (P)));
4564 while Present (N) loop
4565 if Nkind (N) = N_Full_Type_Declaration
4566 and then Present (Defining_Identifier (N))
4567 and then T = Defining_Identifier (N)
4568 then
4569 return True;
4571 elsif (Nkind (N) = N_Private_Type_Declaration
4572 or else
4573 Nkind (N) = N_Private_Extension_Declaration)
4574 and then Present (Defining_Identifier (N))
4575 and then T = Full_View (Defining_Identifier (N))
4576 then
4577 return True;
4578 end if;
4580 Next (N);
4581 end loop;
4583 return False;
4584 end Visible_Part_Type;
4586 -- Start of processing for Maybe_Primitive_Operation
4588 begin
4589 if not Comes_From_Source (S) then
4590 null;
4592 -- If the subprogram is at library level, it is not primitive
4593 -- operation.
4595 elsif Current_Scope = Standard_Standard then
4596 null;
4598 elsif (Ekind (Current_Scope) = E_Package
4599 and then not In_Package_Body (Current_Scope))
4600 or else Is_Overriding
4601 then
4602 -- For function, check return type
4604 if Ekind (S) = E_Function then
4605 B_Typ := Base_Type (Etype (S));
4607 if Scope (B_Typ) = Current_Scope then
4608 Set_Has_Primitive_Operations (B_Typ);
4609 Check_Private_Overriding (B_Typ);
4610 end if;
4611 end if;
4613 -- For all subprograms, check formals
4615 Formal := First_Formal (S);
4616 while Present (Formal) loop
4617 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
4618 F_Typ := Designated_Type (Etype (Formal));
4619 else
4620 F_Typ := Etype (Formal);
4621 end if;
4623 B_Typ := Base_Type (F_Typ);
4625 if Scope (B_Typ) = Current_Scope then
4626 Set_Has_Primitive_Operations (B_Typ);
4627 Check_Private_Overriding (B_Typ);
4628 end if;
4630 Next_Formal (Formal);
4631 end loop;
4632 end if;
4633 end Maybe_Primitive_Operation;
4635 -- Start of processing for New_Overloaded_Entity
4637 begin
4638 -- We need to look for an entity that S may override. This must be a
4639 -- homonym in the current scope, so we look for the first homonym of
4640 -- S in the current scope as the starting point for the search.
4642 E := Current_Entity_In_Scope (S);
4644 -- If there is no homonym then this is definitely not overriding
4646 if No (E) then
4647 Enter_Overloaded_Entity (S);
4648 Check_Dispatching_Operation (S, Empty);
4649 Maybe_Primitive_Operation;
4651 -- If there is a homonym that is not overloadable, then we have an
4652 -- error, except for the special cases checked explicitly below.
4654 elsif not Is_Overloadable (E) then
4656 -- Check for spurious conflict produced by a subprogram that has the
4657 -- same name as that of the enclosing generic package. The conflict
4658 -- occurs within an instance, between the subprogram and the renaming
4659 -- declaration for the package. After the subprogram, the package
4660 -- renaming declaration becomes hidden.
4662 if Ekind (E) = E_Package
4663 and then Present (Renamed_Object (E))
4664 and then Renamed_Object (E) = Current_Scope
4665 and then Nkind (Parent (Renamed_Object (E))) =
4666 N_Package_Specification
4667 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
4668 then
4669 Set_Is_Hidden (E);
4670 Set_Is_Immediately_Visible (E, False);
4671 Enter_Overloaded_Entity (S);
4672 Set_Homonym (S, Homonym (E));
4673 Check_Dispatching_Operation (S, Empty);
4675 -- If the subprogram is implicit it is hidden by the previous
4676 -- declaration. However if it is dispatching, it must appear in the
4677 -- dispatch table anyway, because it can be dispatched to even if it
4678 -- cannot be called directly.
4680 elsif Present (Alias (S))
4681 and then not Comes_From_Source (S)
4682 then
4683 Set_Scope (S, Current_Scope);
4685 if Is_Dispatching_Operation (Alias (S)) then
4686 Check_Dispatching_Operation (S, Empty);
4687 end if;
4689 return;
4691 else
4692 Error_Msg_Sloc := Sloc (E);
4693 Error_Msg_N ("& conflicts with declaration#", S);
4695 -- Useful additional warning
4697 if Is_Generic_Unit (E) then
4698 Error_Msg_N ("\previous generic unit cannot be overloaded", S);
4699 end if;
4701 return;
4702 end if;
4704 -- E exists and is overloadable
4706 else
4707 -- Loop through E and its homonyms to determine if any of them is
4708 -- the candidate for overriding by S.
4710 while Present (E) loop
4712 -- Definitely not interesting if not in the current scope
4714 if Scope (E) /= Current_Scope then
4715 null;
4717 -- Check if we have type conformance
4719 elsif Type_Conformant (E, S) then
4721 -- If the old and new entities have the same profile and one
4722 -- is not the body of the other, then this is an error, unless
4723 -- one of them is implicitly declared.
4725 -- There are some cases when both can be implicit, for example
4726 -- when both a literal and a function that overrides it are
4727 -- inherited in a derivation, or when an inhertited operation
4728 -- of a tagged full type overrides the ineherited operation of
4729 -- a private extension. Ada 83 had a special rule for the the
4730 -- literal case. In Ada95, the later implicit operation hides
4731 -- the former, and the literal is always the former. In the
4732 -- odd case where both are derived operations declared at the
4733 -- same point, both operations should be declared, and in that
4734 -- case we bypass the following test and proceed to the next
4735 -- part (this can only occur for certain obscure cases
4736 -- involving homographs in instances and can't occur for
4737 -- dispatching operations ???). Note that the following
4738 -- condition is less than clear. For example, it's not at all
4739 -- clear why there's a test for E_Entry here. ???
4741 if Present (Alias (S))
4742 and then (No (Alias (E))
4743 or else Comes_From_Source (E)
4744 or else Is_Dispatching_Operation (E))
4745 and then
4746 (Ekind (E) = E_Entry
4747 or else Ekind (E) /= E_Enumeration_Literal)
4748 then
4749 -- When an derived operation is overloaded it may be due to
4750 -- the fact that the full view of a private extension
4751 -- re-inherits. It has to be dealt with.
4753 if Is_Package (Current_Scope)
4754 and then In_Private_Part (Current_Scope)
4755 then
4756 Check_Operation_From_Private_View (S, E);
4757 end if;
4759 -- In any case the implicit operation remains hidden by
4760 -- the existing declaration, which is overriding.
4762 Set_Is_Overriding_Operation (E);
4763 return;
4765 -- Within an instance, the renaming declarations for
4766 -- actual subprograms may become ambiguous, but they do
4767 -- not hide each other.
4769 elsif Ekind (E) /= E_Entry
4770 and then not Comes_From_Source (E)
4771 and then not Is_Generic_Instance (E)
4772 and then (Present (Alias (E))
4773 or else Is_Intrinsic_Subprogram (E))
4774 and then (not In_Instance
4775 or else No (Parent (E))
4776 or else Nkind (Unit_Declaration_Node (E)) /=
4777 N_Subprogram_Renaming_Declaration)
4778 then
4779 -- A subprogram child unit is not allowed to override
4780 -- an inherited subprogram (10.1.1(20)).
4782 if Is_Child_Unit (S) then
4783 Error_Msg_N
4784 ("child unit overrides inherited subprogram in parent",
4786 return;
4787 end if;
4789 if Is_Non_Overriding_Operation (E, S) then
4790 Enter_Overloaded_Entity (S);
4791 if not Present (Derived_Type)
4792 or else Is_Tagged_Type (Derived_Type)
4793 then
4794 Check_Dispatching_Operation (S, Empty);
4795 end if;
4797 return;
4798 end if;
4800 -- E is a derived operation or an internal operator which
4801 -- is being overridden. Remove E from further visibility.
4802 -- Furthermore, if E is a dispatching operation, it must be
4803 -- replaced in the list of primitive operations of its type
4804 -- (see Override_Dispatching_Operation).
4806 declare
4807 Prev : Entity_Id;
4809 begin
4810 Prev := First_Entity (Current_Scope);
4812 while Present (Prev)
4813 and then Next_Entity (Prev) /= E
4814 loop
4815 Next_Entity (Prev);
4816 end loop;
4818 -- It is possible for E to be in the current scope and
4819 -- yet not in the entity chain. This can only occur in a
4820 -- generic context where E is an implicit concatenation
4821 -- in the formal part, because in a generic body the
4822 -- entity chain starts with the formals.
4824 pragma Assert
4825 (Present (Prev) or else Chars (E) = Name_Op_Concat);
4827 -- E must be removed both from the entity_list of the
4828 -- current scope, and from the visibility chain
4830 if Debug_Flag_E then
4831 Write_Str ("Override implicit operation ");
4832 Write_Int (Int (E));
4833 Write_Eol;
4834 end if;
4836 -- If E is a predefined concatenation, it stands for four
4837 -- different operations. As a result, a single explicit
4838 -- declaration does not hide it. In a possible ambiguous
4839 -- situation, Disambiguate chooses the user-defined op,
4840 -- so it is correct to retain the previous internal one.
4842 if Chars (E) /= Name_Op_Concat
4843 or else Ekind (E) /= E_Operator
4844 then
4845 -- For nondispatching derived operations that are
4846 -- overridden by a subprogram declared in the private
4847 -- part of a package, we retain the derived
4848 -- subprogram but mark it as not immediately visible.
4849 -- If the derived operation was declared in the
4850 -- visible part then this ensures that it will still
4851 -- be visible outside the package with the proper
4852 -- signature (calls from outside must also be
4853 -- directed to this version rather than the
4854 -- overriding one, unlike the dispatching case).
4855 -- Calls from inside the package will still resolve
4856 -- to the overriding subprogram since the derived one
4857 -- is marked as not visible within the package.
4859 -- If the private operation is dispatching, we achieve
4860 -- the overriding by keeping the implicit operation
4861 -- but setting its alias to be the overring one. In
4862 -- this fashion the proper body is executed in all
4863 -- cases, but the original signature is used outside
4864 -- of the package.
4866 -- If the overriding is not in the private part, we
4867 -- remove the implicit operation altogether.
4869 if Is_Private_Declaration (S) then
4871 if not Is_Dispatching_Operation (E) then
4872 Set_Is_Immediately_Visible (E, False);
4873 else
4874 -- Work done in Override_Dispatching_Operation,
4875 -- so nothing else need to be done here.
4877 null;
4878 end if;
4880 else
4881 -- Find predecessor of E in Homonym chain
4883 if E = Current_Entity (E) then
4884 Prev_Vis := Empty;
4885 else
4886 Prev_Vis := Current_Entity (E);
4887 while Homonym (Prev_Vis) /= E loop
4888 Prev_Vis := Homonym (Prev_Vis);
4889 end loop;
4890 end if;
4892 if Prev_Vis /= Empty then
4894 -- Skip E in the visibility chain
4896 Set_Homonym (Prev_Vis, Homonym (E));
4898 else
4899 Set_Name_Entity_Id (Chars (E), Homonym (E));
4900 end if;
4902 Set_Next_Entity (Prev, Next_Entity (E));
4904 if No (Next_Entity (Prev)) then
4905 Set_Last_Entity (Current_Scope, Prev);
4906 end if;
4908 end if;
4909 end if;
4911 Enter_Overloaded_Entity (S);
4912 Set_Is_Overriding_Operation (S);
4914 if Is_Dispatching_Operation (E) then
4916 -- An overriding dispatching subprogram inherits the
4917 -- convention of the overridden subprogram (by
4918 -- AI-117).
4920 Set_Convention (S, Convention (E));
4922 Check_Dispatching_Operation (S, E);
4923 else
4924 Check_Dispatching_Operation (S, Empty);
4925 end if;
4927 Maybe_Primitive_Operation (Is_Overriding => True);
4928 goto Check_Inequality;
4929 end;
4931 -- Apparent redeclarations in instances can occur when two
4932 -- formal types get the same actual type. The subprograms in
4933 -- in the instance are legal, even if not callable from the
4934 -- outside. Calls from within are disambiguated elsewhere.
4935 -- For dispatching operations in the visible part, the usual
4936 -- rules apply, and operations with the same profile are not
4937 -- legal (B830001).
4939 elsif (In_Instance_Visible_Part
4940 and then not Is_Dispatching_Operation (E))
4941 or else In_Instance_Not_Visible
4942 then
4943 null;
4945 -- Here we have a real error (identical profile)
4947 else
4948 Error_Msg_Sloc := Sloc (E);
4950 -- Avoid cascaded errors if the entity appears in
4951 -- subsequent calls.
4953 Set_Scope (S, Current_Scope);
4955 Error_Msg_N ("& conflicts with declaration#", S);
4957 if Is_Generic_Instance (S)
4958 and then not Has_Completion (E)
4959 then
4960 Error_Msg_N
4961 ("\instantiation cannot provide body for it", S);
4962 end if;
4964 return;
4965 end if;
4967 else
4968 null;
4969 end if;
4971 Prev_Vis := E;
4972 E := Homonym (E);
4973 end loop;
4975 -- On exit, we know that S is a new entity
4977 Enter_Overloaded_Entity (S);
4978 Maybe_Primitive_Operation;
4980 -- If S is a derived operation for an untagged type then by
4981 -- definition it's not a dispatching operation (even if the parent
4982 -- operation was dispatching), so we don't call
4983 -- Check_Dispatching_Operation in that case.
4985 if not Present (Derived_Type)
4986 or else Is_Tagged_Type (Derived_Type)
4987 then
4988 Check_Dispatching_Operation (S, Empty);
4989 end if;
4990 end if;
4992 -- If this is a user-defined equality operator that is not a derived
4993 -- subprogram, create the corresponding inequality. If the operation is
4994 -- dispatching, the expansion is done elsewhere, and we do not create
4995 -- an explicit inequality operation.
4997 <<Check_Inequality>>
4998 if Chars (S) = Name_Op_Eq
4999 and then Etype (S) = Standard_Boolean
5000 and then Present (Parent (S))
5001 and then not Is_Dispatching_Operation (S)
5002 then
5003 Make_Inequality_Operator (S);
5004 end if;
5005 end New_Overloaded_Entity;
5007 ---------------------
5008 -- Process_Formals --
5009 ---------------------
5011 procedure Process_Formals
5012 (T : List_Id;
5013 Related_Nod : Node_Id)
5015 Param_Spec : Node_Id;
5016 Formal : Entity_Id;
5017 Formal_Type : Entity_Id;
5018 Default : Node_Id;
5019 Ptype : Entity_Id;
5021 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
5022 -- Check whether the default has a class-wide type. After analysis the
5023 -- default has the type of the formal, so we must also check explicitly
5024 -- for an access attribute.
5026 ---------------------------
5027 -- Is_Class_Wide_Default --
5028 ---------------------------
5030 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
5031 begin
5032 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
5033 or else (Nkind (D) = N_Attribute_Reference
5034 and then Attribute_Name (D) = Name_Access
5035 and then Is_Class_Wide_Type (Etype (Prefix (D))));
5036 end Is_Class_Wide_Default;
5038 -- Start of processing for Process_Formals
5040 begin
5041 -- In order to prevent premature use of the formals in the same formal
5042 -- part, the Ekind is left undefined until all default expressions are
5043 -- analyzed. The Ekind is established in a separate loop at the end.
5045 Param_Spec := First (T);
5047 while Present (Param_Spec) loop
5049 Formal := Defining_Identifier (Param_Spec);
5050 Enter_Name (Formal);
5052 -- Case of ordinary parameters
5054 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
5055 Find_Type (Parameter_Type (Param_Spec));
5056 Ptype := Parameter_Type (Param_Spec);
5058 if Ptype = Error then
5059 goto Continue;
5060 end if;
5062 Formal_Type := Entity (Ptype);
5064 if Ekind (Formal_Type) = E_Incomplete_Type
5065 or else (Is_Class_Wide_Type (Formal_Type)
5066 and then Ekind (Root_Type (Formal_Type)) =
5067 E_Incomplete_Type)
5068 then
5069 -- Ada 2005 (AI-50217): Incomplete tagged types that are made
5070 -- visible by a limited with_clause are valid formal types.
5072 if From_With_Type (Formal_Type)
5073 and then Is_Tagged_Type (Formal_Type)
5074 then
5075 null;
5077 elsif Nkind (Parent (T)) /= N_Access_Function_Definition
5078 and then Nkind (Parent (T)) /= N_Access_Procedure_Definition
5079 then
5080 Error_Msg_N ("invalid use of incomplete type", Param_Spec);
5081 end if;
5083 elsif Ekind (Formal_Type) = E_Void then
5084 Error_Msg_NE ("premature use of&",
5085 Parameter_Type (Param_Spec), Formal_Type);
5086 end if;
5088 -- Ada 2005 (AI-231): Create and decorate an internal subtype
5089 -- declaration corresponding to the null-excluding type of the
5090 -- formal in the enclosing scope. In addition, replace the
5091 -- parameter type of the formal to this internal subtype.
5093 if Null_Exclusion_Present (Param_Spec) then
5094 declare
5095 Loc : constant Source_Ptr := Sloc (Param_Spec);
5097 Anon : constant Entity_Id :=
5098 Make_Defining_Identifier (Loc,
5099 Chars => New_Internal_Name ('S'));
5101 Curr_Scope : constant Scope_Stack_Entry :=
5102 Scope_Stack.Table (Scope_Stack.Last);
5104 Ptype : constant Node_Id := Parameter_Type (Param_Spec);
5105 Decl : Node_Id;
5106 P : Node_Id := Parent (Parent (Related_Nod));
5108 begin
5109 Set_Is_Internal (Anon);
5111 Decl :=
5112 Make_Subtype_Declaration (Loc,
5113 Defining_Identifier => Anon,
5114 Null_Exclusion_Present => True,
5115 Subtype_Indication =>
5116 New_Occurrence_Of (Etype (Ptype), Loc));
5118 -- Propagate the null-excluding attribute to the new entity
5120 if Null_Exclusion_Present (Param_Spec) then
5121 Set_Null_Exclusion_Present (Param_Spec, False);
5122 Set_Can_Never_Be_Null (Anon);
5123 end if;
5125 Mark_Rewrite_Insertion (Decl);
5127 -- Insert the new declaration in the nearest enclosing scope
5129 while not Has_Declarations (P) loop
5130 P := Parent (P);
5131 end loop;
5133 Prepend (Decl, Declarations (P));
5135 Rewrite (Ptype, New_Occurrence_Of (Anon, Loc));
5136 Mark_Rewrite_Insertion (Ptype);
5138 -- Analyze the new declaration in the context of the
5139 -- enclosing scope
5141 Scope_Stack.Decrement_Last;
5142 Analyze (Decl);
5143 Scope_Stack.Append (Curr_Scope);
5145 Formal_Type := Anon;
5146 end;
5147 end if;
5149 -- Ada 2005 (AI-231): Static checks
5151 if Null_Exclusion_Present (Param_Spec)
5152 or else Can_Never_Be_Null (Entity (Ptype))
5153 then
5154 Null_Exclusion_Static_Checks (Param_Spec);
5155 end if;
5157 -- An access formal type
5159 else
5160 Formal_Type :=
5161 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
5163 -- Ada 2005 (AI-254)
5165 declare
5166 AD : constant Node_Id :=
5167 Access_To_Subprogram_Definition
5168 (Parameter_Type (Param_Spec));
5169 begin
5170 if Present (AD) and then Protected_Present (AD) then
5171 Formal_Type :=
5172 Replace_Anonymous_Access_To_Protected_Subprogram
5173 (Param_Spec, Formal_Type);
5174 end if;
5175 end;
5176 end if;
5178 Set_Etype (Formal, Formal_Type);
5179 Default := Expression (Param_Spec);
5181 if Present (Default) then
5182 if Out_Present (Param_Spec) then
5183 Error_Msg_N
5184 ("default initialization only allowed for IN parameters",
5185 Param_Spec);
5186 end if;
5188 -- Do the special preanalysis of the expression (see section on
5189 -- "Handling of Default Expressions" in the spec of package Sem).
5191 Analyze_Per_Use_Expression (Default, Formal_Type);
5193 -- Check that the designated type of an access parameter's
5194 -- default is not a class-wide type unless the parameter's
5195 -- designated type is also class-wide.
5197 if Ekind (Formal_Type) = E_Anonymous_Access_Type
5198 and then Is_Class_Wide_Default (Default)
5199 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
5200 then
5201 Error_Msg_N
5202 ("access to class-wide expression not allowed here", Default);
5203 end if;
5204 end if;
5206 <<Continue>>
5207 Next (Param_Spec);
5208 end loop;
5210 -- If this is the formal part of a function specification, analyze the
5211 -- subtype mark in the context where the formals are visible but not
5212 -- yet usable, and may hide outer homographs.
5214 if Nkind (Related_Nod) = N_Function_Specification then
5215 Analyze_Return_Type (Related_Nod);
5216 end if;
5218 -- Now set the kind (mode) of each formal
5220 Param_Spec := First (T);
5222 while Present (Param_Spec) loop
5223 Formal := Defining_Identifier (Param_Spec);
5224 Set_Formal_Mode (Formal);
5226 if Ekind (Formal) = E_In_Parameter then
5227 Set_Default_Value (Formal, Expression (Param_Spec));
5229 if Present (Expression (Param_Spec)) then
5230 Default := Expression (Param_Spec);
5232 if Is_Scalar_Type (Etype (Default)) then
5233 if Nkind
5234 (Parameter_Type (Param_Spec)) /= N_Access_Definition
5235 then
5236 Formal_Type := Entity (Parameter_Type (Param_Spec));
5238 else
5239 Formal_Type := Access_Definition
5240 (Related_Nod, Parameter_Type (Param_Spec));
5241 end if;
5243 Apply_Scalar_Range_Check (Default, Formal_Type);
5244 end if;
5245 end if;
5246 end if;
5248 Next (Param_Spec);
5249 end loop;
5251 end Process_Formals;
5253 ----------------------------
5254 -- Reference_Body_Formals --
5255 ----------------------------
5257 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
5258 Fs : Entity_Id;
5259 Fb : Entity_Id;
5261 begin
5262 if Error_Posted (Spec) then
5263 return;
5264 end if;
5266 Fs := First_Formal (Spec);
5267 Fb := First_Formal (Bod);
5269 while Present (Fs) loop
5270 Generate_Reference (Fs, Fb, 'b');
5272 if Style_Check then
5273 Style.Check_Identifier (Fb, Fs);
5274 end if;
5276 Set_Spec_Entity (Fb, Fs);
5277 Set_Referenced (Fs, False);
5278 Next_Formal (Fs);
5279 Next_Formal (Fb);
5280 end loop;
5281 end Reference_Body_Formals;
5283 -------------------------
5284 -- Set_Actual_Subtypes --
5285 -------------------------
5287 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
5288 Loc : constant Source_Ptr := Sloc (N);
5289 Decl : Node_Id;
5290 Formal : Entity_Id;
5291 T : Entity_Id;
5292 First_Stmt : Node_Id := Empty;
5293 AS_Needed : Boolean;
5295 begin
5296 -- If this is an emtpy initialization procedure, no need to create
5297 -- actual subtypes (small optimization).
5299 if Ekind (Subp) = E_Procedure
5300 and then Is_Null_Init_Proc (Subp)
5301 then
5302 return;
5303 end if;
5305 Formal := First_Formal (Subp);
5306 while Present (Formal) loop
5307 T := Etype (Formal);
5309 -- We never need an actual subtype for a constrained formal
5311 if Is_Constrained (T) then
5312 AS_Needed := False;
5314 -- If we have unknown discriminants, then we do not need an actual
5315 -- subtype, or more accurately we cannot figure it out! Note that
5316 -- all class-wide types have unknown discriminants.
5318 elsif Has_Unknown_Discriminants (T) then
5319 AS_Needed := False;
5321 -- At this stage we have an unconstrained type that may need an
5322 -- actual subtype. For sure the actual subtype is needed if we have
5323 -- an unconstrained array type.
5325 elsif Is_Array_Type (T) then
5326 AS_Needed := True;
5328 -- The only other case which needs an actual subtype is an
5329 -- unconstrained record type which is an IN parameter (we cannot
5330 -- generate actual subtypes for the OUT or IN OUT case, since an
5331 -- assignment can change the discriminant values. However we exclude
5332 -- the case of initialization procedures, since discriminants are
5333 -- handled very specially in this context, see the section entitled
5334 -- "Handling of Discriminants" in Einfo. We also exclude the case of
5335 -- Discrim_SO_Functions (functions used in front end layout mode for
5336 -- size/offset values), since in such functions only discriminants
5337 -- are referenced, and not only are such subtypes not needed, but
5338 -- they cannot always be generated, because of order of elaboration
5339 -- issues.
5341 elsif Is_Record_Type (T)
5342 and then Ekind (Formal) = E_In_Parameter
5343 and then Chars (Formal) /= Name_uInit
5344 and then not Is_Unchecked_Union (T)
5345 and then not Is_Discrim_SO_Function (Subp)
5346 then
5347 AS_Needed := True;
5349 -- All other cases do not need an actual subtype
5351 else
5352 AS_Needed := False;
5353 end if;
5355 -- Generate actual subtypes for unconstrained arrays and
5356 -- unconstrained discriminated records.
5358 if AS_Needed then
5359 if Nkind (N) = N_Accept_Statement then
5361 -- If expansion is active, The formal is replaced by a local
5362 -- variable that renames the corresponding entry of the
5363 -- parameter block, and it is this local variable that may
5364 -- require an actual subtype.
5366 if Expander_Active then
5367 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
5368 else
5369 Decl := Build_Actual_Subtype (T, Formal);
5370 end if;
5372 if Present (Handled_Statement_Sequence (N)) then
5373 First_Stmt :=
5374 First (Statements (Handled_Statement_Sequence (N)));
5375 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
5376 Mark_Rewrite_Insertion (Decl);
5377 else
5378 -- If the accept statement has no body, there will be no
5379 -- reference to the actuals, so no need to compute actual
5380 -- subtypes.
5382 return;
5383 end if;
5385 else
5386 Decl := Build_Actual_Subtype (T, Formal);
5387 Prepend (Decl, Declarations (N));
5388 Mark_Rewrite_Insertion (Decl);
5389 end if;
5391 -- The declaration uses the bounds of an existing object, and
5392 -- therefore needs no constraint checks.
5394 Analyze (Decl, Suppress => All_Checks);
5396 -- We need to freeze manually the generated type when it is
5397 -- inserted anywhere else than in a declarative part.
5399 if Present (First_Stmt) then
5400 Insert_List_Before_And_Analyze (First_Stmt,
5401 Freeze_Entity (Defining_Identifier (Decl), Loc));
5402 end if;
5404 if Nkind (N) = N_Accept_Statement
5405 and then Expander_Active
5406 then
5407 Set_Actual_Subtype (Renamed_Object (Formal),
5408 Defining_Identifier (Decl));
5409 else
5410 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
5411 end if;
5412 end if;
5414 Next_Formal (Formal);
5415 end loop;
5416 end Set_Actual_Subtypes;
5418 ---------------------
5419 -- Set_Formal_Mode --
5420 ---------------------
5422 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
5423 Spec : constant Node_Id := Parent (Formal_Id);
5425 begin
5426 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
5427 -- since we ensure that corresponding actuals are always valid at the
5428 -- point of the call.
5430 if Out_Present (Spec) then
5431 if Ekind (Scope (Formal_Id)) = E_Function
5432 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
5433 then
5434 Error_Msg_N ("functions can only have IN parameters", Spec);
5435 Set_Ekind (Formal_Id, E_In_Parameter);
5437 elsif In_Present (Spec) then
5438 Set_Ekind (Formal_Id, E_In_Out_Parameter);
5440 else
5441 Set_Ekind (Formal_Id, E_Out_Parameter);
5442 Set_Never_Set_In_Source (Formal_Id, True);
5443 Set_Is_True_Constant (Formal_Id, False);
5444 Set_Current_Value (Formal_Id, Empty);
5445 end if;
5447 else
5448 Set_Ekind (Formal_Id, E_In_Parameter);
5449 end if;
5451 -- Set Is_Known_Non_Null for access parameters since the language
5452 -- guarantees that access parameters are always non-null. We also set
5453 -- Can_Never_Be_Null, since there is no way to change the value.
5455 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
5457 -- Ada 2005 (AI-231): This behaviour has been modified in Ada 2005.
5458 -- It is only forced if the null_exclusion appears.
5460 if Ada_Version < Ada_05
5461 or else Null_Exclusion_Present (Spec)
5462 then
5463 Set_Is_Known_Non_Null (Formal_Id);
5464 Set_Can_Never_Be_Null (Formal_Id);
5465 end if;
5466 end if;
5468 Set_Mechanism (Formal_Id, Default_Mechanism);
5469 Set_Formal_Validity (Formal_Id);
5470 end Set_Formal_Mode;
5472 -------------------------
5473 -- Set_Formal_Validity --
5474 -------------------------
5476 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
5477 begin
5478 -- If no validity checking, then we cannot assume anything about the
5479 -- validity of parameters, since we do not know there is any checking
5480 -- of the validity on the call side.
5482 if not Validity_Checks_On then
5483 return;
5485 -- If validity checking for parameters is enabled, this means we are
5486 -- not supposed to make any assumptions about argument values.
5488 elsif Validity_Check_Parameters then
5489 return;
5491 -- If we are checking in parameters, we will assume that the caller is
5492 -- also checking parameters, so we can assume the parameter is valid.
5494 elsif Ekind (Formal_Id) = E_In_Parameter
5495 and then Validity_Check_In_Params
5496 then
5497 Set_Is_Known_Valid (Formal_Id, True);
5499 -- Similar treatment for IN OUT parameters
5501 elsif Ekind (Formal_Id) = E_In_Out_Parameter
5502 and then Validity_Check_In_Out_Params
5503 then
5504 Set_Is_Known_Valid (Formal_Id, True);
5505 end if;
5506 end Set_Formal_Validity;
5508 ------------------------
5509 -- Subtype_Conformant --
5510 ------------------------
5512 function Subtype_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5513 Result : Boolean;
5515 begin
5516 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result);
5517 return Result;
5518 end Subtype_Conformant;
5520 ---------------------
5521 -- Type_Conformant --
5522 ---------------------
5524 function Type_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5525 Result : Boolean;
5526 begin
5527 Check_Conformance (New_Id, Old_Id, Type_Conformant, False, Result);
5528 return Result;
5529 end Type_Conformant;
5531 -------------------------------
5532 -- Valid_Operator_Definition --
5533 -------------------------------
5535 procedure Valid_Operator_Definition (Designator : Entity_Id) is
5536 N : Integer := 0;
5537 F : Entity_Id;
5538 Id : constant Name_Id := Chars (Designator);
5539 N_OK : Boolean;
5541 begin
5542 F := First_Formal (Designator);
5544 while Present (F) loop
5545 N := N + 1;
5547 if Present (Default_Value (F)) then
5548 Error_Msg_N
5549 ("default values not allowed for operator parameters",
5550 Parent (F));
5551 end if;
5553 Next_Formal (F);
5554 end loop;
5556 -- Verify that user-defined operators have proper number of arguments
5557 -- First case of operators which can only be unary
5559 if Id = Name_Op_Not
5560 or else Id = Name_Op_Abs
5561 then
5562 N_OK := (N = 1);
5564 -- Case of operators which can be unary or binary
5566 elsif Id = Name_Op_Add
5567 or Id = Name_Op_Subtract
5568 then
5569 N_OK := (N in 1 .. 2);
5571 -- All other operators can only be binary
5573 else
5574 N_OK := (N = 2);
5575 end if;
5577 if not N_OK then
5578 Error_Msg_N
5579 ("incorrect number of arguments for operator", Designator);
5580 end if;
5582 if Id = Name_Op_Ne
5583 and then Base_Type (Etype (Designator)) = Standard_Boolean
5584 and then not Is_Intrinsic_Subprogram (Designator)
5585 then
5586 Error_Msg_N
5587 ("explicit definition of inequality not allowed", Designator);
5588 end if;
5589 end Valid_Operator_Definition;
5591 end Sem_Ch6;