* tree-loop-linear.c: Don't include varray.h.
[official-gcc.git] / gcc / ada / sem_ch6.adb
blob33696df5200a6a28e5972e105153649bcf0c2d73
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-2006, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 Exp_Tss; use Exp_Tss;
36 with Fname; use Fname;
37 with Freeze; use Freeze;
38 with Itypes; use Itypes;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Lib; use Lib;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Output; use Output;
46 with Rtsfind; use Rtsfind;
47 with Sem; use Sem;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch4; use Sem_Ch4;
51 with Sem_Ch5; use Sem_Ch5;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch10; use Sem_Ch10;
54 with Sem_Ch12; use Sem_Ch12;
55 with Sem_Disp; use Sem_Disp;
56 with Sem_Dist; use Sem_Dist;
57 with Sem_Elim; use Sem_Elim;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Mech; use Sem_Mech;
60 with Sem_Prag; use Sem_Prag;
61 with Sem_Res; use Sem_Res;
62 with Sem_Util; use Sem_Util;
63 with Sem_Type; use Sem_Type;
64 with Sem_Warn; use Sem_Warn;
65 with Sinput; use Sinput;
66 with Stand; use Stand;
67 with Sinfo; use Sinfo;
68 with Sinfo.CN; use Sinfo.CN;
69 with Snames; use Snames;
70 with Stringt; use Stringt;
71 with Style;
72 with Stylesw; use Stylesw;
73 with Tbuild; use Tbuild;
74 with Uintp; use Uintp;
75 with Urealp; use Urealp;
76 with Validsw; use Validsw;
78 package body Sem_Ch6 is
80 -- The following flag is used to indicate that two formals in two
81 -- subprograms being checked for conformance differ only in that one is
82 -- an access parameter while the other is of a general access type with
83 -- the same designated type. In this case, if the rest of the signatures
84 -- match, a call to either subprogram may be ambiguous, which is worth
85 -- a warning. The flag is set in Compatible_Types, and the warning emitted
86 -- in New_Overloaded_Entity.
88 May_Hide_Profile : Boolean := False;
90 -----------------------
91 -- Local Subprograms --
92 -----------------------
94 procedure Analyze_Return_Type (N : Node_Id);
95 -- Subsidiary to Process_Formals: analyze subtype mark in function
96 -- specification, in a context where the formals are visible and hide
97 -- outer homographs.
99 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
100 -- Analyze a generic subprogram body. N is the body to be analyzed, and
101 -- Gen_Id is the defining entity Id for the corresponding spec.
103 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
104 -- If a subprogram has pragma Inline and inlining is active, use generic
105 -- machinery to build an unexpanded body for the subprogram. This body is
106 -- subsequenty used for inline expansions at call sites. If subprogram can
107 -- be inlined (depending on size and nature of local declarations) this
108 -- function returns true. Otherwise subprogram body is treated normally.
109 -- If proper warnings are enabled and the subprogram contains a construct
110 -- that cannot be inlined, the offending construct is flagged accordingly.
112 type Conformance_Type is
113 (Type_Conformant, Mode_Conformant, Subtype_Conformant, Fully_Conformant);
114 -- Conformance type used for following call, meaning matches the
115 -- RM definitions of the corresponding terms.
117 procedure Check_Conformance
118 (New_Id : Entity_Id;
119 Old_Id : Entity_Id;
120 Ctype : Conformance_Type;
121 Errmsg : Boolean;
122 Conforms : out Boolean;
123 Err_Loc : Node_Id := Empty;
124 Get_Inst : Boolean := False;
125 Skip_Controlling_Formals : Boolean := False);
126 -- Given two entities, this procedure checks that the profiles associated
127 -- with these entities meet the conformance criterion given by the third
128 -- parameter. If they conform, Conforms is set True and control returns
129 -- to the caller. If they do not conform, Conforms is set to False, and
130 -- in addition, if Errmsg is True on the call, proper messages are output
131 -- to complain about the conformance failure. If Err_Loc is non_Empty
132 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
133 -- error messages are placed on the appropriate part of the construct
134 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
135 -- against a formal access-to-subprogram type so Get_Instance_Of must
136 -- be called.
138 procedure Check_Overriding_Indicator
139 (Subp : Entity_Id;
140 Does_Override : Boolean);
141 -- Verify the consistency of an overriding_indicator given for subprogram
142 -- declaration, body, renaming, or instantiation. The flag Does_Override
143 -- is set if the scope into which we are introducing the subprogram
144 -- contains a type-conformant subprogram that becomes hidden by the new
145 -- subprogram.
147 procedure Check_Subprogram_Order (N : Node_Id);
148 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
149 -- the alpha ordering rule for N if this ordering requirement applicable.
151 procedure Check_Returns
152 (HSS : Node_Id;
153 Mode : Character;
154 Err : out Boolean;
155 Proc : Entity_Id := Empty);
156 -- Called to check for missing return statements in a function body, or for
157 -- returns present in a procedure body which has No_Return set. L is the
158 -- handled statement sequence for the subprogram body. This procedure
159 -- checks all flow paths to make sure they either have return (Mode = 'F',
160 -- used for functions) or do not have a return (Mode = 'P', used for
161 -- No_Return procedures). The flag Err is set if there are any control
162 -- paths not explicitly terminated by a return in the function case, and is
163 -- True otherwise. Proc is the entity for the procedure case and is used
164 -- in posting the warning message.
166 function Conforming_Types
167 (T1 : Entity_Id;
168 T2 : Entity_Id;
169 Ctype : Conformance_Type;
170 Get_Inst : Boolean := False) return Boolean;
171 -- Check that two formal parameter types conform, checking both for
172 -- equality of base types, and where required statically matching
173 -- subtypes, depending on the setting of Ctype.
175 procedure Enter_Overloaded_Entity (S : Entity_Id);
176 -- This procedure makes S, a new overloaded entity, into the first visible
177 -- entity with that name.
179 procedure Install_Entity (E : Entity_Id);
180 -- Make single entity visible. Used for generic formals as well
182 procedure Install_Formals (Id : Entity_Id);
183 -- On entry to a subprogram body, make the formals visible. Note that
184 -- simply placing the subprogram on the scope stack is not sufficient:
185 -- the formals must become the current entities for their names.
187 function Is_Non_Overriding_Operation
188 (Prev_E : Entity_Id;
189 New_E : Entity_Id) return Boolean;
190 -- Enforce the rule given in 12.3(18): a private operation in an instance
191 -- overrides an inherited operation only if the corresponding operation
192 -- was overriding in the generic. This can happen for primitive operations
193 -- of types derived (in the generic unit) from formal private or formal
194 -- derived types.
196 procedure Make_Inequality_Operator (S : Entity_Id);
197 -- Create the declaration for an inequality operator that is implicitly
198 -- created by a user-defined equality operator that yields a boolean.
200 procedure May_Need_Actuals (Fun : Entity_Id);
201 -- Flag functions that can be called without parameters, i.e. those that
202 -- have no parameters, or those for which defaults exist for all parameters
204 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id);
205 -- If there is a separate spec for a subprogram or generic subprogram, the
206 -- formals of the body are treated as references to the corresponding
207 -- formals of the spec. This reference does not count as an actual use of
208 -- the formal, in order to diagnose formals that are unused in the body.
210 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
211 -- Formal_Id is an formal parameter entity. This procedure deals with
212 -- setting the proper validity status for this entity, which depends
213 -- on the kind of parameter and the validity checking mode.
215 ---------------------------------------------
216 -- Analyze_Abstract_Subprogram_Declaration --
217 ---------------------------------------------
219 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
220 Designator : constant Entity_Id :=
221 Analyze_Subprogram_Specification (Specification (N));
222 Scop : constant Entity_Id := Current_Scope;
224 begin
225 Generate_Definition (Designator);
226 Set_Is_Abstract (Designator);
227 New_Overloaded_Entity (Designator);
228 Check_Delayed_Subprogram (Designator);
230 Set_Categorization_From_Scope (Designator, Scop);
232 if Ekind (Scope (Designator)) = E_Protected_Type then
233 Error_Msg_N
234 ("abstract subprogram not allowed in protected type", N);
235 end if;
237 Generate_Reference_To_Formals (Designator);
238 end Analyze_Abstract_Subprogram_Declaration;
240 ----------------------------
241 -- Analyze_Function_Call --
242 ----------------------------
244 procedure Analyze_Function_Call (N : Node_Id) is
245 P : constant Node_Id := Name (N);
246 L : constant List_Id := Parameter_Associations (N);
247 Actual : Node_Id;
249 begin
250 Analyze (P);
252 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
253 -- as B (A, X). If the rewriting is successful, the call has been
254 -- analyzed and we just return.
256 if Nkind (P) = N_Selected_Component
257 and then Name (N) /= P
258 and then Is_Rewrite_Substitution (N)
259 and then Present (Etype (N))
260 then
261 return;
262 end if;
264 -- If error analyzing name, then set Any_Type as result type and return
266 if Etype (P) = Any_Type then
267 Set_Etype (N, Any_Type);
268 return;
269 end if;
271 -- Otherwise analyze the parameters
273 if Present (L) then
274 Actual := First (L);
275 while Present (Actual) loop
276 Analyze (Actual);
277 Check_Parameterless_Call (Actual);
278 Next (Actual);
279 end loop;
280 end if;
282 Analyze_Call (N);
283 end Analyze_Function_Call;
285 -------------------------------------
286 -- Analyze_Generic_Subprogram_Body --
287 -------------------------------------
289 procedure Analyze_Generic_Subprogram_Body
290 (N : Node_Id;
291 Gen_Id : Entity_Id)
293 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
294 Kind : constant Entity_Kind := Ekind (Gen_Id);
295 Body_Id : Entity_Id;
296 New_N : Node_Id;
297 Spec : Node_Id;
299 begin
300 -- Copy body and disable expansion while analyzing the generic For a
301 -- stub, do not copy the stub (which would load the proper body), this
302 -- will be done when the proper body is analyzed.
304 if Nkind (N) /= N_Subprogram_Body_Stub then
305 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
306 Rewrite (N, New_N);
307 Start_Generic;
308 end if;
310 Spec := Specification (N);
312 -- Within the body of the generic, the subprogram is callable, and
313 -- behaves like the corresponding non-generic unit.
315 Body_Id := Defining_Entity (Spec);
317 if Kind = E_Generic_Procedure
318 and then Nkind (Spec) /= N_Procedure_Specification
319 then
320 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
321 return;
323 elsif Kind = E_Generic_Function
324 and then Nkind (Spec) /= N_Function_Specification
325 then
326 Error_Msg_N ("invalid body for generic function ", Body_Id);
327 return;
328 end if;
330 Set_Corresponding_Body (Gen_Decl, Body_Id);
332 if Has_Completion (Gen_Id)
333 and then Nkind (Parent (N)) /= N_Subunit
334 then
335 Error_Msg_N ("duplicate generic body", N);
336 return;
337 else
338 Set_Has_Completion (Gen_Id);
339 end if;
341 if Nkind (N) = N_Subprogram_Body_Stub then
342 Set_Ekind (Defining_Entity (Specification (N)), Kind);
343 else
344 Set_Corresponding_Spec (N, Gen_Id);
345 end if;
347 if Nkind (Parent (N)) = N_Compilation_Unit then
348 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
349 end if;
351 -- Make generic parameters immediately visible in the body. They are
352 -- needed to process the formals declarations. Then make the formals
353 -- visible in a separate step.
355 New_Scope (Gen_Id);
357 declare
358 E : Entity_Id;
359 First_Ent : Entity_Id;
361 begin
362 First_Ent := First_Entity (Gen_Id);
364 E := First_Ent;
365 while Present (E) and then not Is_Formal (E) loop
366 Install_Entity (E);
367 Next_Entity (E);
368 end loop;
370 Set_Use (Generic_Formal_Declarations (Gen_Decl));
372 -- Now generic formals are visible, and the specification can be
373 -- analyzed, for subsequent conformance check.
375 Body_Id := Analyze_Subprogram_Specification (Spec);
377 -- Make formal parameters visible
379 if Present (E) then
381 -- E is the first formal parameter, we loop through the formals
382 -- installing them so that they will be visible.
384 Set_First_Entity (Gen_Id, E);
385 while Present (E) loop
386 Install_Entity (E);
387 Next_Formal (E);
388 end loop;
389 end if;
391 -- Visible generic entity is callable within its own body
393 Set_Ekind (Gen_Id, Ekind (Body_Id));
394 Set_Ekind (Body_Id, E_Subprogram_Body);
395 Set_Convention (Body_Id, Convention (Gen_Id));
396 Set_Scope (Body_Id, Scope (Gen_Id));
397 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
399 if Nkind (N) = N_Subprogram_Body_Stub then
401 -- No body to analyze, so restore state of generic unit
403 Set_Ekind (Gen_Id, Kind);
404 Set_Ekind (Body_Id, Kind);
406 if Present (First_Ent) then
407 Set_First_Entity (Gen_Id, First_Ent);
408 end if;
410 End_Scope;
411 return;
412 end if;
414 -- If this is a compilation unit, it must be made visible explicitly,
415 -- because the compilation of the declaration, unlike other library
416 -- unit declarations, does not. If it is not a unit, the following
417 -- is redundant but harmless.
419 Set_Is_Immediately_Visible (Gen_Id);
420 Reference_Body_Formals (Gen_Id, Body_Id);
422 Set_Actual_Subtypes (N, Current_Scope);
423 Analyze_Declarations (Declarations (N));
424 Check_Completion;
425 Analyze (Handled_Statement_Sequence (N));
427 Save_Global_References (Original_Node (N));
429 -- Prior to exiting the scope, include generic formals again (if any
430 -- are present) in the set of local entities.
432 if Present (First_Ent) then
433 Set_First_Entity (Gen_Id, First_Ent);
434 end if;
436 Check_References (Gen_Id);
437 end;
439 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
440 End_Scope;
441 Check_Subprogram_Order (N);
443 -- Outside of its body, unit is generic again
445 Set_Ekind (Gen_Id, Kind);
446 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
447 Style.Check_Identifier (Body_Id, Gen_Id);
448 End_Generic;
449 end Analyze_Generic_Subprogram_Body;
451 -----------------------------
452 -- Analyze_Operator_Symbol --
453 -----------------------------
455 -- An operator symbol such as "+" or "and" may appear in context where the
456 -- literal denotes an entity name, such as "+"(x, y) or in context when it
457 -- is just a string, as in (conjunction = "or"). In these cases the parser
458 -- generates this node, and the semantics does the disambiguation. Other
459 -- such case are actuals in an instantiation, the generic unit in an
460 -- instantiation, and pragma arguments.
462 procedure Analyze_Operator_Symbol (N : Node_Id) is
463 Par : constant Node_Id := Parent (N);
465 begin
466 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
467 or else Nkind (Par) = N_Function_Instantiation
468 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
469 or else (Nkind (Par) = N_Pragma_Argument_Association
470 and then not Is_Pragma_String_Literal (Par))
471 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
472 or else (Nkind (Par) = N_Attribute_Reference
473 and then Attribute_Name (Par) /= Name_Value)
474 then
475 Find_Direct_Name (N);
477 else
478 Change_Operator_Symbol_To_String_Literal (N);
479 Analyze (N);
480 end if;
481 end Analyze_Operator_Symbol;
483 -----------------------------------
484 -- Analyze_Parameter_Association --
485 -----------------------------------
487 procedure Analyze_Parameter_Association (N : Node_Id) is
488 begin
489 Analyze (Explicit_Actual_Parameter (N));
490 end Analyze_Parameter_Association;
492 ----------------------------
493 -- Analyze_Procedure_Call --
494 ----------------------------
496 procedure Analyze_Procedure_Call (N : Node_Id) is
497 Loc : constant Source_Ptr := Sloc (N);
498 P : constant Node_Id := Name (N);
499 Actuals : constant List_Id := Parameter_Associations (N);
500 Actual : Node_Id;
501 New_N : Node_Id;
503 procedure Analyze_Call_And_Resolve;
504 -- Do Analyze and Resolve calls for procedure call
506 ------------------------------
507 -- Analyze_Call_And_Resolve --
508 ------------------------------
510 procedure Analyze_Call_And_Resolve is
511 begin
512 if Nkind (N) = N_Procedure_Call_Statement then
513 Analyze_Call (N);
514 Resolve (N, Standard_Void_Type);
515 else
516 Analyze (N);
517 end if;
518 end Analyze_Call_And_Resolve;
520 -- Start of processing for Analyze_Procedure_Call
522 begin
523 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
524 -- a procedure call or an entry call. The prefix may denote an access
525 -- to subprogram type, in which case an implicit dereference applies.
526 -- If the prefix is an indexed component (without implicit defererence)
527 -- then the construct denotes a call to a member of an entire family.
528 -- If the prefix is a simple name, it may still denote a call to a
529 -- parameterless member of an entry family. Resolution of these various
530 -- interpretations is delicate.
532 Analyze (P);
534 -- If this is a call of the form Obj.Op, the call may have been
535 -- analyzed and possibly rewritten into a block, in which case
536 -- we are done.
538 if Analyzed (N) then
539 return;
540 end if;
542 -- If error analyzing prefix, then set Any_Type as result and return
544 if Etype (P) = Any_Type then
545 Set_Etype (N, Any_Type);
546 return;
547 end if;
549 -- Otherwise analyze the parameters
551 if Present (Actuals) then
552 Actual := First (Actuals);
554 while Present (Actual) loop
555 Analyze (Actual);
556 Check_Parameterless_Call (Actual);
557 Next (Actual);
558 end loop;
559 end if;
561 -- Special processing for Elab_Spec and Elab_Body calls
563 if Nkind (P) = N_Attribute_Reference
564 and then (Attribute_Name (P) = Name_Elab_Spec
565 or else Attribute_Name (P) = Name_Elab_Body)
566 then
567 if Present (Actuals) then
568 Error_Msg_N
569 ("no parameters allowed for this call", First (Actuals));
570 return;
571 end if;
573 Set_Etype (N, Standard_Void_Type);
574 Set_Analyzed (N);
576 elsif Is_Entity_Name (P)
577 and then Is_Record_Type (Etype (Entity (P)))
578 and then Remote_AST_I_Dereference (P)
579 then
580 return;
582 elsif Is_Entity_Name (P)
583 and then Ekind (Entity (P)) /= E_Entry_Family
584 then
585 if Is_Access_Type (Etype (P))
586 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
587 and then No (Actuals)
588 and then Comes_From_Source (N)
589 then
590 Error_Msg_N ("missing explicit dereference in call", N);
591 end if;
593 Analyze_Call_And_Resolve;
595 -- If the prefix is the simple name of an entry family, this is
596 -- a parameterless call from within the task body itself.
598 elsif Is_Entity_Name (P)
599 and then Nkind (P) = N_Identifier
600 and then Ekind (Entity (P)) = E_Entry_Family
601 and then Present (Actuals)
602 and then No (Next (First (Actuals)))
603 then
604 -- Can be call to parameterless entry family. What appears to be the
605 -- sole argument is in fact the entry index. Rewrite prefix of node
606 -- accordingly. Source representation is unchanged by this
607 -- transformation.
609 New_N :=
610 Make_Indexed_Component (Loc,
611 Prefix =>
612 Make_Selected_Component (Loc,
613 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
614 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
615 Expressions => Actuals);
616 Set_Name (N, New_N);
617 Set_Etype (New_N, Standard_Void_Type);
618 Set_Parameter_Associations (N, No_List);
619 Analyze_Call_And_Resolve;
621 elsif Nkind (P) = N_Explicit_Dereference then
622 if Ekind (Etype (P)) = E_Subprogram_Type then
623 Analyze_Call_And_Resolve;
624 else
625 Error_Msg_N ("expect access to procedure in call", P);
626 end if;
628 -- The name can be a selected component or an indexed component that
629 -- yields an access to subprogram. Such a prefix is legal if the call
630 -- has parameter associations.
632 elsif Is_Access_Type (Etype (P))
633 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
634 then
635 if Present (Actuals) then
636 Analyze_Call_And_Resolve;
637 else
638 Error_Msg_N ("missing explicit dereference in call ", N);
639 end if;
641 -- If not an access to subprogram, then the prefix must resolve to the
642 -- name of an entry, entry family, or protected operation.
644 -- For the case of a simple entry call, P is a selected component where
645 -- the prefix is the task and the selector name is the entry. A call to
646 -- a protected procedure will have the same syntax. If the protected
647 -- object contains overloaded operations, the entity may appear as a
648 -- function, the context will select the operation whose type is Void.
650 elsif Nkind (P) = N_Selected_Component
651 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
652 or else
653 Ekind (Entity (Selector_Name (P))) = E_Procedure
654 or else
655 Ekind (Entity (Selector_Name (P))) = E_Function)
656 then
657 Analyze_Call_And_Resolve;
659 elsif Nkind (P) = N_Selected_Component
660 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
661 and then Present (Actuals)
662 and then No (Next (First (Actuals)))
663 then
664 -- Can be call to parameterless entry family. What appears to be the
665 -- sole argument is in fact the entry index. Rewrite prefix of node
666 -- accordingly. Source representation is unchanged by this
667 -- transformation.
669 New_N :=
670 Make_Indexed_Component (Loc,
671 Prefix => New_Copy (P),
672 Expressions => Actuals);
673 Set_Name (N, New_N);
674 Set_Etype (New_N, Standard_Void_Type);
675 Set_Parameter_Associations (N, No_List);
676 Analyze_Call_And_Resolve;
678 -- For the case of a reference to an element of an entry family, P is
679 -- an indexed component whose prefix is a selected component (task and
680 -- entry family), and whose index is the entry family index.
682 elsif Nkind (P) = N_Indexed_Component
683 and then Nkind (Prefix (P)) = N_Selected_Component
684 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
685 then
686 Analyze_Call_And_Resolve;
688 -- If the prefix is the name of an entry family, it is a call from
689 -- within the task body itself.
691 elsif Nkind (P) = N_Indexed_Component
692 and then Nkind (Prefix (P)) = N_Identifier
693 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
694 then
695 New_N :=
696 Make_Selected_Component (Loc,
697 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
698 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
699 Rewrite (Prefix (P), New_N);
700 Analyze (P);
701 Analyze_Call_And_Resolve;
703 -- Anything else is an error
705 else
706 Error_Msg_N ("invalid procedure or entry call", N);
707 end if;
708 end Analyze_Procedure_Call;
710 ------------------------------
711 -- Analyze_Return_Statement --
712 ------------------------------
714 procedure Analyze_Return_Statement (N : Node_Id) is
715 Loc : constant Source_Ptr := Sloc (N);
716 Expr : Node_Id;
717 Scope_Id : Entity_Id;
718 Kind : Entity_Kind;
719 R_Type : Entity_Id;
721 begin
722 -- Find subprogram or accept statement enclosing the return statement
724 Scope_Id := Empty;
725 for J in reverse 0 .. Scope_Stack.Last loop
726 Scope_Id := Scope_Stack.Table (J).Entity;
727 exit when Ekind (Scope_Id) /= E_Block and then
728 Ekind (Scope_Id) /= E_Loop;
729 end loop;
731 pragma Assert (Present (Scope_Id));
733 Kind := Ekind (Scope_Id);
734 Expr := Expression (N);
736 if Kind /= E_Function
737 and then Kind /= E_Generic_Function
738 and then Kind /= E_Procedure
739 and then Kind /= E_Generic_Procedure
740 and then Kind /= E_Entry
741 and then Kind /= E_Entry_Family
742 then
743 Error_Msg_N ("illegal context for return statement", N);
745 elsif Present (Expr) then
746 if Kind = E_Function or else Kind = E_Generic_Function then
747 Set_Return_Present (Scope_Id);
748 R_Type := Etype (Scope_Id);
749 Set_Return_Type (N, R_Type);
750 Analyze_And_Resolve (Expr, R_Type);
752 -- Ada 2005 (AI-318-02): When the result type is an anonymous
753 -- access type, apply an implicit conversion of the expression
754 -- to that type to force appropriate static and run-time
755 -- accessibility checks.
757 if Ada_Version >= Ada_05
758 and then Ekind (R_Type) = E_Anonymous_Access_Type
759 then
760 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
761 Analyze_And_Resolve (Expr, R_Type);
762 end if;
764 if (Is_Class_Wide_Type (Etype (Expr))
765 or else Is_Dynamically_Tagged (Expr))
766 and then not Is_Class_Wide_Type (R_Type)
767 then
768 Error_Msg_N
769 ("dynamically tagged expression not allowed!", Expr);
770 end if;
772 Apply_Constraint_Check (Expr, R_Type);
774 -- Ada 2005 (AI-318-02): Return-by-reference types have been
775 -- removed and replaced by anonymous access results. This is
776 -- an incompatibility with Ada 95. Not clear whether this
777 -- should be enforced yet or perhaps controllable with a
778 -- special switch. ???
780 -- if Ada_Version >= Ada_05
781 -- and then Is_Limited_Type (R_Type)
782 -- and then Nkind (Expr) /= N_Aggregate
783 -- and then Nkind (Expr) /= N_Extension_Aggregate
784 -- and then Nkind (Expr) /= N_Function_Call
785 -- then
786 -- Error_Msg_N
787 -- ("(Ada 2005) illegal operand for limited return", N);
788 -- end if;
790 -- ??? A real run-time accessibility check is needed in cases
791 -- involving dereferences of access parameters. For now we just
792 -- check the static cases.
794 if Is_Return_By_Reference_Type (Etype (Scope_Id))
795 and then Object_Access_Level (Expr)
796 > Subprogram_Access_Level (Scope_Id)
797 then
798 Rewrite (N,
799 Make_Raise_Program_Error (Loc,
800 Reason => PE_Accessibility_Check_Failed));
801 Analyze (N);
803 Error_Msg_N
804 ("cannot return a local value by reference?", N);
805 Error_Msg_NE
806 ("\& will be raised at run time?",
807 N, Standard_Program_Error);
808 end if;
810 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
811 Error_Msg_N ("procedure cannot return value (use function)", N);
813 else
814 Error_Msg_N ("accept statement cannot return value", N);
815 end if;
817 -- No expression present
819 else
820 if Kind = E_Function or Kind = E_Generic_Function then
821 Error_Msg_N ("missing expression in return from function", N);
822 end if;
824 if (Ekind (Scope_Id) = E_Procedure
825 or else Ekind (Scope_Id) = E_Generic_Procedure)
826 and then No_Return (Scope_Id)
827 then
828 Error_Msg_N
829 ("RETURN statement not allowed (No_Return)", N);
830 end if;
831 end if;
833 Check_Unreachable_Code (N);
834 end Analyze_Return_Statement;
836 -------------------------
837 -- Analyze_Return_Type --
838 -------------------------
840 procedure Analyze_Return_Type (N : Node_Id) is
841 Designator : constant Entity_Id := Defining_Entity (N);
842 Typ : Entity_Id := Empty;
844 begin
845 if Result_Definition (N) /= Error then
846 if Nkind (Result_Definition (N)) = N_Access_Definition then
847 Typ := Access_Definition (N, Result_Definition (N));
848 Set_Parent (Typ, Result_Definition (N));
849 Set_Is_Local_Anonymous_Access (Typ);
850 Set_Etype (Designator, Typ);
852 -- Ada 2005 (AI-231): Static checks
854 -- Null_Exclusion_Static_Checks needs to be extended to handle
855 -- null exclusion checks for function specifications. ???
857 -- if Null_Exclusion_Present (N) then
858 -- Null_Exclusion_Static_Checks (Param_Spec);
859 -- end if;
861 -- Subtype_Mark case
863 else
864 Find_Type (Result_Definition (N));
865 Typ := Entity (Result_Definition (N));
866 Set_Etype (Designator, Typ);
868 if Ekind (Typ) = E_Incomplete_Type
869 or else (Is_Class_Wide_Type (Typ)
870 and then
871 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
872 then
873 Error_Msg_N
874 ("invalid use of incomplete type", Result_Definition (N));
875 end if;
876 end if;
878 else
879 Set_Etype (Designator, Any_Type);
880 end if;
881 end Analyze_Return_Type;
883 -----------------------------
884 -- Analyze_Subprogram_Body --
885 -----------------------------
887 -- This procedure is called for regular subprogram bodies, generic bodies,
888 -- and for subprogram stubs of both kinds. In the case of stubs, only the
889 -- specification matters, and is used to create a proper declaration for
890 -- the subprogram, or to perform conformance checks.
892 procedure Analyze_Subprogram_Body (N : Node_Id) is
893 Loc : constant Source_Ptr := Sloc (N);
894 Body_Spec : constant Node_Id := Specification (N);
895 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
896 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
897 Body_Deleted : constant Boolean := False;
899 HSS : Node_Id;
900 Spec_Id : Entity_Id;
901 Spec_Decl : Node_Id := Empty;
902 Last_Formal : Entity_Id := Empty;
903 Conformant : Boolean;
904 Missing_Ret : Boolean;
905 P_Ent : Entity_Id;
907 procedure Check_Inline_Pragma (Spec : in out Node_Id);
908 -- Look ahead to recognize a pragma that may appear after the body.
909 -- If there is a previous spec, check that it appears in the same
910 -- declarative part. If the pragma is Inline_Always, perform inlining
911 -- unconditionally, otherwise only if Front_End_Inlining is requested.
912 -- If the body acts as a spec, and inlining is required, we create a
913 -- subprogram declaration for it, in order to attach the body to inline.
915 procedure Copy_Parameter_List (Plist : List_Id);
916 -- Comment required ???
918 procedure Verify_Overriding_Indicator;
919 -- If there was a previous spec, the entity has been entered in the
920 -- current scope previously. If the body itself carries an overriding
921 -- indicator, check that it is consistent with the known status of the
922 -- entity.
924 -------------------------
925 -- Check_Inline_Pragma --
926 -------------------------
928 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
929 Prag : Node_Id;
930 Plist : List_Id;
932 begin
933 if not Expander_Active then
934 return;
935 end if;
937 if Is_List_Member (N)
938 and then Present (Next (N))
939 and then Nkind (Next (N)) = N_Pragma
940 then
941 Prag := Next (N);
943 if Nkind (Prag) = N_Pragma
944 and then
945 (Get_Pragma_Id (Chars (Prag)) = Pragma_Inline_Always
946 or else
947 (Front_End_Inlining
948 and then Get_Pragma_Id (Chars (Prag)) = Pragma_Inline))
949 and then
950 Chars
951 (Expression (First (Pragma_Argument_Associations (Prag))))
952 = Chars (Body_Id)
953 then
954 Prag := Next (N);
955 else
956 Prag := Empty;
957 end if;
958 else
959 Prag := Empty;
960 end if;
962 if Present (Prag) then
963 if Present (Spec_Id) then
964 if List_Containing (N) =
965 List_Containing (Unit_Declaration_Node (Spec_Id))
966 then
967 Analyze (Prag);
968 end if;
970 else
971 -- Create a subprogram declaration, to make treatment uniform
973 declare
974 Subp : constant Entity_Id :=
975 Make_Defining_Identifier (Loc, Chars (Body_Id));
976 Decl : constant Node_Id :=
977 Make_Subprogram_Declaration (Loc,
978 Specification => New_Copy_Tree (Specification (N)));
979 begin
980 Set_Defining_Unit_Name (Specification (Decl), Subp);
982 if Present (First_Formal (Body_Id)) then
983 Plist := New_List;
984 Copy_Parameter_List (Plist);
985 Set_Parameter_Specifications
986 (Specification (Decl), Plist);
987 end if;
989 Insert_Before (N, Decl);
990 Analyze (Decl);
991 Analyze (Prag);
992 Set_Has_Pragma_Inline (Subp);
994 if Get_Pragma_Id (Chars (Prag)) = Pragma_Inline_Always then
995 Set_Is_Inlined (Subp);
996 Set_Next_Rep_Item (Prag, First_Rep_Item (Subp));
997 Set_First_Rep_Item (Subp, Prag);
998 end if;
1000 Spec := Subp;
1001 end;
1002 end if;
1003 end if;
1004 end Check_Inline_Pragma;
1006 -------------------------
1007 -- Copy_Parameter_List --
1008 -------------------------
1010 procedure Copy_Parameter_List (Plist : List_Id) is
1011 Formal : Entity_Id;
1013 begin
1014 Formal := First_Formal (Body_Id);
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;
1032 end Copy_Parameter_List;
1034 ---------------------------------
1035 -- Verify_Overriding_Indicator --
1036 ---------------------------------
1038 procedure Verify_Overriding_Indicator is
1039 begin
1040 if Must_Override (Body_Spec)
1041 and then not Is_Overriding_Operation (Spec_Id)
1042 then
1043 Error_Msg_NE
1044 ("subprogram& is not overriding", Body_Spec, Spec_Id);
1046 elsif Must_Not_Override (Body_Spec)
1047 and then Is_Overriding_Operation (Spec_Id)
1048 then
1049 Error_Msg_NE
1050 ("subprogram& overrides inherited operation",
1051 Body_Spec, Spec_Id);
1052 end if;
1053 end Verify_Overriding_Indicator;
1055 -- Start of processing for Analyze_Subprogram_Body
1057 begin
1058 if Debug_Flag_C then
1059 Write_Str ("==== Compiling subprogram body ");
1060 Write_Name (Chars (Body_Id));
1061 Write_Str (" from ");
1062 Write_Location (Loc);
1063 Write_Eol;
1064 end if;
1066 Trace_Scope (N, Body_Id, " Analyze subprogram");
1068 -- Generic subprograms are handled separately. They always have a
1069 -- generic specification. Determine whether current scope has a
1070 -- previous declaration.
1072 -- If the subprogram body is defined within an instance of the same
1073 -- name, the instance appears as a package renaming, and will be hidden
1074 -- within the subprogram.
1076 if Present (Prev_Id)
1077 and then not Is_Overloadable (Prev_Id)
1078 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
1079 or else Comes_From_Source (Prev_Id))
1080 then
1081 if Is_Generic_Subprogram (Prev_Id) then
1082 Spec_Id := Prev_Id;
1083 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1084 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1086 Analyze_Generic_Subprogram_Body (N, Spec_Id);
1087 return;
1089 else
1090 -- Previous entity conflicts with subprogram name. Attempting to
1091 -- enter name will post error.
1093 Enter_Name (Body_Id);
1094 return;
1095 end if;
1097 -- Non-generic case, find the subprogram declaration, if one was seen,
1098 -- or enter new overloaded entity in the current scope. If the
1099 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
1100 -- part of the context of one of its subunits. No need to redo the
1101 -- analysis.
1103 elsif Prev_Id = Body_Id
1104 and then Has_Completion (Body_Id)
1105 then
1106 return;
1108 else
1109 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
1111 if Nkind (N) = N_Subprogram_Body_Stub
1112 or else No (Corresponding_Spec (N))
1113 then
1114 Spec_Id := Find_Corresponding_Spec (N);
1116 -- If this is a duplicate body, no point in analyzing it
1118 if Error_Posted (N) then
1119 return;
1120 end if;
1122 -- A subprogram body should cause freezing of its own declaration,
1123 -- but if there was no previous explicit declaration, then the
1124 -- subprogram will get frozen too late (there may be code within
1125 -- the body that depends on the subprogram having been frozen,
1126 -- such as uses of extra formals), so we force it to be frozen
1127 -- here. Same holds if the body and the spec are compilation
1128 -- units.
1130 if No (Spec_Id) then
1131 Freeze_Before (N, Body_Id);
1133 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1134 Freeze_Before (N, Spec_Id);
1135 end if;
1136 else
1137 Spec_Id := Corresponding_Spec (N);
1138 end if;
1139 end if;
1141 -- Do not inline any subprogram that contains nested subprograms, since
1142 -- the backend inlining circuit seems to generate uninitialized
1143 -- references in this case. We know this happens in the case of front
1144 -- end ZCX support, but it also appears it can happen in other cases as
1145 -- well. The backend often rejects attempts to inline in the case of
1146 -- nested procedures anyway, so little if anything is lost by this.
1147 -- Note that this is test is for the benefit of the back-end. There is
1148 -- a separate test for front-end inlining that also rejects nested
1149 -- subprograms.
1151 -- Do not do this test if errors have been detected, because in some
1152 -- error cases, this code blows up, and we don't need it anyway if
1153 -- there have been errors, since we won't get to the linker anyway.
1155 if Comes_From_Source (Body_Id)
1156 and then Serious_Errors_Detected = 0
1157 then
1158 P_Ent := Body_Id;
1159 loop
1160 P_Ent := Scope (P_Ent);
1161 exit when No (P_Ent) or else P_Ent = Standard_Standard;
1163 if Is_Subprogram (P_Ent) then
1164 Set_Is_Inlined (P_Ent, False);
1166 if Comes_From_Source (P_Ent)
1167 and then Has_Pragma_Inline (P_Ent)
1168 then
1169 Cannot_Inline
1170 ("cannot inline& (nested subprogram)?",
1171 N, P_Ent);
1172 end if;
1173 end if;
1174 end loop;
1175 end if;
1177 Check_Inline_Pragma (Spec_Id);
1179 -- Case of fully private operation in the body of the protected type.
1180 -- We must create a declaration for the subprogram, in order to attach
1181 -- the protected subprogram that will be used in internal calls.
1183 if No (Spec_Id)
1184 and then Comes_From_Source (N)
1185 and then Is_Protected_Type (Current_Scope)
1186 then
1187 declare
1188 Decl : Node_Id;
1189 Plist : List_Id;
1190 Formal : Entity_Id;
1191 New_Spec : Node_Id;
1193 begin
1194 Formal := First_Formal (Body_Id);
1196 -- The protected operation always has at least one formal, namely
1197 -- the object itself, but it is only placed in the parameter list
1198 -- if expansion is enabled.
1200 if Present (Formal)
1201 or else Expander_Active
1202 then
1203 Plist := New_List;
1205 else
1206 Plist := No_List;
1207 end if;
1209 Copy_Parameter_List (Plist);
1211 if Nkind (Body_Spec) = N_Procedure_Specification then
1212 New_Spec :=
1213 Make_Procedure_Specification (Loc,
1214 Defining_Unit_Name =>
1215 Make_Defining_Identifier (Sloc (Body_Id),
1216 Chars => Chars (Body_Id)),
1217 Parameter_Specifications => Plist);
1218 else
1219 New_Spec :=
1220 Make_Function_Specification (Loc,
1221 Defining_Unit_Name =>
1222 Make_Defining_Identifier (Sloc (Body_Id),
1223 Chars => Chars (Body_Id)),
1224 Parameter_Specifications => Plist,
1225 Result_Definition =>
1226 New_Occurrence_Of (Etype (Body_Id), Loc));
1227 end if;
1229 Decl :=
1230 Make_Subprogram_Declaration (Loc,
1231 Specification => New_Spec);
1232 Insert_Before (N, Decl);
1233 Spec_Id := Defining_Unit_Name (New_Spec);
1235 -- Indicate that the entity comes from source, to ensure that
1236 -- cross-reference information is properly generated. The body
1237 -- itself is rewritten during expansion, and the body entity will
1238 -- not appear in calls to the operation.
1240 Set_Comes_From_Source (Spec_Id, True);
1241 Analyze (Decl);
1242 Set_Has_Completion (Spec_Id);
1243 Set_Convention (Spec_Id, Convention_Protected);
1244 end;
1246 elsif Present (Spec_Id) then
1247 Spec_Decl := Unit_Declaration_Node (Spec_Id);
1248 Verify_Overriding_Indicator;
1249 end if;
1251 -- Place subprogram on scope stack, and make formals visible. If there
1252 -- is a spec, the visible entity remains that of the spec.
1254 if Present (Spec_Id) then
1255 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1257 if Is_Child_Unit (Spec_Id) then
1258 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
1259 end if;
1261 if Style_Check then
1262 Style.Check_Identifier (Body_Id, Spec_Id);
1263 end if;
1265 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1266 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1268 if Is_Abstract (Spec_Id) then
1269 Error_Msg_N ("an abstract subprogram cannot have a body", N);
1270 return;
1271 else
1272 Set_Convention (Body_Id, Convention (Spec_Id));
1273 Set_Has_Completion (Spec_Id);
1275 if Is_Protected_Type (Scope (Spec_Id)) then
1276 Set_Privals_Chain (Spec_Id, New_Elmt_List);
1277 end if;
1279 -- If this is a body generated for a renaming, do not check for
1280 -- full conformance. The check is redundant, because the spec of
1281 -- the body is a copy of the spec in the renaming declaration,
1282 -- and the test can lead to spurious errors on nested defaults.
1284 if Present (Spec_Decl)
1285 and then not Comes_From_Source (N)
1286 and then
1287 (Nkind (Original_Node (Spec_Decl)) =
1288 N_Subprogram_Renaming_Declaration
1289 or else (Present (Corresponding_Body (Spec_Decl))
1290 and then
1291 Nkind (Unit_Declaration_Node
1292 (Corresponding_Body (Spec_Decl))) =
1293 N_Subprogram_Renaming_Declaration))
1294 then
1295 Conformant := True;
1296 else
1297 Check_Conformance
1298 (Body_Id, Spec_Id,
1299 Fully_Conformant, True, Conformant, Body_Id);
1300 end if;
1302 -- If the body is not fully conformant, we have to decide if we
1303 -- should analyze it or not. If it has a really messed up profile
1304 -- then we probably should not analyze it, since we will get too
1305 -- many bogus messages.
1307 -- Our decision is to go ahead in the non-fully conformant case
1308 -- only if it is at least mode conformant with the spec. Note
1309 -- that the call to Check_Fully_Conformant has issued the proper
1310 -- error messages to complain about the lack of conformance.
1312 if not Conformant
1313 and then not Mode_Conformant (Body_Id, Spec_Id)
1314 then
1315 return;
1316 end if;
1317 end if;
1319 if Spec_Id /= Body_Id then
1320 Reference_Body_Formals (Spec_Id, Body_Id);
1321 end if;
1323 if Nkind (N) /= N_Subprogram_Body_Stub then
1324 Set_Corresponding_Spec (N, Spec_Id);
1326 -- Ada 2005 (AI-345): Restore the correct Etype: here we undo the
1327 -- work done by Analyze_Subprogram_Specification to allow the
1328 -- overriding of task, protected and interface primitives.
1330 if Comes_From_Source (Spec_Id)
1331 and then Present (First_Entity (Spec_Id))
1332 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
1333 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
1334 and then Present (Abstract_Interfaces
1335 (Etype (First_Entity (Spec_Id))))
1336 and then Present (Corresponding_Concurrent_Type
1337 (Etype (First_Entity (Spec_Id))))
1338 then
1339 Set_Etype (First_Entity (Spec_Id),
1340 Corresponding_Concurrent_Type
1341 (Etype (First_Entity (Spec_Id))));
1342 end if;
1344 -- Ada 2005: A formal that is an access parameter may have a
1345 -- designated type imported through a limited_with clause, while
1346 -- the body has a regular with clause. Update the types of the
1347 -- formals accordingly, so that the non-limited view of each type
1348 -- is available in the body. We have already verified that the
1349 -- declarations are type-conformant.
1351 if Ada_Version >= Ada_05 then
1352 declare
1353 F_Spec : Entity_Id;
1354 F_Body : Entity_Id;
1356 begin
1357 F_Spec := First_Formal (Spec_Id);
1358 F_Body := First_Formal (Body_Id);
1360 while Present (F_Spec) loop
1361 if Ekind (Etype (F_Spec)) = E_Anonymous_Access_Type
1362 and then
1363 From_With_Type (Designated_Type (Etype (F_Spec)))
1364 then
1365 Set_Etype (F_Spec, Etype (F_Body));
1366 end if;
1368 Next_Formal (F_Spec);
1369 Next_Formal (F_Body);
1370 end loop;
1371 end;
1372 end if;
1374 -- Now make the formals visible, and place subprogram
1375 -- on scope stack.
1377 Install_Formals (Spec_Id);
1378 Last_Formal := Last_Entity (Spec_Id);
1379 New_Scope (Spec_Id);
1381 -- Make sure that the subprogram is immediately visible. For
1382 -- child units that have no separate spec this is indispensable.
1383 -- Otherwise it is safe albeit redundant.
1385 Set_Is_Immediately_Visible (Spec_Id);
1386 end if;
1388 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
1389 Set_Ekind (Body_Id, E_Subprogram_Body);
1390 Set_Scope (Body_Id, Scope (Spec_Id));
1392 -- Case of subprogram body with no previous spec
1394 else
1395 if Style_Check
1396 and then Comes_From_Source (Body_Id)
1397 and then not Suppress_Style_Checks (Body_Id)
1398 and then not In_Instance
1399 then
1400 Style.Body_With_No_Spec (N);
1401 end if;
1403 New_Overloaded_Entity (Body_Id);
1405 if Nkind (N) /= N_Subprogram_Body_Stub then
1406 Set_Acts_As_Spec (N);
1407 Generate_Definition (Body_Id);
1408 Generate_Reference
1409 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
1410 Generate_Reference_To_Formals (Body_Id);
1411 Install_Formals (Body_Id);
1412 New_Scope (Body_Id);
1413 end if;
1414 end if;
1416 -- If this is the proper body of a stub, we must verify that the stub
1417 -- conforms to the body, and to the previous spec if one was present.
1418 -- we know already that the body conforms to that spec. This test is
1419 -- only required for subprograms that come from source.
1421 if Nkind (Parent (N)) = N_Subunit
1422 and then Comes_From_Source (N)
1423 and then not Error_Posted (Body_Id)
1424 and then Nkind (Corresponding_Stub (Parent (N))) =
1425 N_Subprogram_Body_Stub
1426 then
1427 declare
1428 Old_Id : constant Entity_Id :=
1429 Defining_Entity
1430 (Specification (Corresponding_Stub (Parent (N))));
1432 Conformant : Boolean := False;
1434 begin
1435 if No (Spec_Id) then
1436 Check_Fully_Conformant (Body_Id, Old_Id);
1438 else
1439 Check_Conformance
1440 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
1442 if not Conformant then
1444 -- The stub was taken to be a new declaration. Indicate
1445 -- that it lacks a body.
1447 Set_Has_Completion (Old_Id, False);
1448 end if;
1449 end if;
1450 end;
1451 end if;
1453 Set_Has_Completion (Body_Id);
1454 Check_Eliminated (Body_Id);
1456 if Nkind (N) = N_Subprogram_Body_Stub then
1457 return;
1459 elsif Present (Spec_Id)
1460 and then Expander_Active
1461 and then
1462 (Is_Always_Inlined (Spec_Id)
1463 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
1464 then
1465 Build_Body_To_Inline (N, Spec_Id);
1466 end if;
1468 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
1469 -- if its specification we have to install the private withed units.
1471 if Is_Compilation_Unit (Body_Id)
1472 and then Scope (Body_Id) = Standard_Standard
1473 then
1474 Install_Private_With_Clauses (Body_Id);
1475 end if;
1477 -- Now we can go on to analyze the body
1479 HSS := Handled_Statement_Sequence (N);
1480 Set_Actual_Subtypes (N, Current_Scope);
1481 Analyze_Declarations (Declarations (N));
1482 Check_Completion;
1483 Analyze (HSS);
1484 Process_End_Label (HSS, 't', Current_Scope);
1485 End_Scope;
1486 Check_Subprogram_Order (N);
1487 Set_Analyzed (Body_Id);
1489 -- If we have a separate spec, then the analysis of the declarations
1490 -- caused the entities in the body to be chained to the spec id, but
1491 -- we want them chained to the body id. Only the formal parameters
1492 -- end up chained to the spec id in this case.
1494 if Present (Spec_Id) then
1496 -- We must conform to the categorization of our spec
1498 Validate_Categorization_Dependency (N, Spec_Id);
1500 -- And if this is a child unit, the parent units must conform
1502 if Is_Child_Unit (Spec_Id) then
1503 Validate_Categorization_Dependency
1504 (Unit_Declaration_Node (Spec_Id), Spec_Id);
1505 end if;
1507 if Present (Last_Formal) then
1508 Set_Next_Entity
1509 (Last_Entity (Body_Id), Next_Entity (Last_Formal));
1510 Set_Next_Entity (Last_Formal, Empty);
1511 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1512 Set_Last_Entity (Spec_Id, Last_Formal);
1514 else
1515 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1516 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1517 Set_First_Entity (Spec_Id, Empty);
1518 Set_Last_Entity (Spec_Id, Empty);
1519 end if;
1520 end if;
1522 -- If function, check return statements
1524 if Nkind (Body_Spec) = N_Function_Specification then
1525 declare
1526 Id : Entity_Id;
1528 begin
1529 if Present (Spec_Id) then
1530 Id := Spec_Id;
1531 else
1532 Id := Body_Id;
1533 end if;
1535 if Return_Present (Id) then
1536 Check_Returns (HSS, 'F', Missing_Ret);
1538 if Missing_Ret then
1539 Set_Has_Missing_Return (Id);
1540 end if;
1542 elsif not Is_Machine_Code_Subprogram (Id)
1543 and then not Body_Deleted
1544 then
1545 Error_Msg_N ("missing RETURN statement in function body", N);
1546 end if;
1547 end;
1549 -- If procedure with No_Return, check returns
1551 elsif Nkind (Body_Spec) = N_Procedure_Specification
1552 and then Present (Spec_Id)
1553 and then No_Return (Spec_Id)
1554 then
1555 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
1556 end if;
1558 -- Now we are going to check for variables that are never modified in
1559 -- the body of the procedure. We omit these checks if the first
1560 -- statement of the procedure raises an exception. In particular this
1561 -- deals with the common idiom of a stubbed function, which might
1562 -- appear as something like
1564 -- function F (A : Integer) return Some_Type;
1565 -- X : Some_Type;
1566 -- begin
1567 -- raise Program_Error;
1568 -- return X;
1569 -- end F;
1571 -- Here the purpose of X is simply to satisfy the (annoying)
1572 -- requirement in Ada that there be at least one return, and we
1573 -- certainly do not want to go posting warnings on X that it is not
1574 -- initialized!
1576 declare
1577 Stm : Node_Id := First (Statements (HSS));
1579 begin
1580 -- Skip an initial label (for one thing this occurs when we are in
1581 -- front end ZCX mode, but in any case it is irrelevant).
1583 if Nkind (Stm) = N_Label then
1584 Next (Stm);
1585 end if;
1587 -- Do the test on the original statement before expansion
1589 declare
1590 Ostm : constant Node_Id := Original_Node (Stm);
1592 begin
1593 -- If explicit raise statement, return with no checks
1595 if Nkind (Ostm) = N_Raise_Statement then
1596 return;
1598 -- Check for explicit call cases which likely raise an exception
1600 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
1601 if Is_Entity_Name (Name (Ostm)) then
1602 declare
1603 Ent : constant Entity_Id := Entity (Name (Ostm));
1605 begin
1606 -- If the procedure is marked No_Return, then likely it
1607 -- raises an exception, but in any case it is not coming
1608 -- back here, so no need to check beyond the call.
1610 if Ekind (Ent) = E_Procedure
1611 and then No_Return (Ent)
1612 then
1613 return;
1615 -- If the procedure name is Raise_Exception, then also
1616 -- assume that it raises an exception. The main target
1617 -- here is Ada.Exceptions.Raise_Exception, but this name
1618 -- is pretty evocative in any context! Note that the
1619 -- procedure in Ada.Exceptions is not marked No_Return
1620 -- because of the annoying case of the null exception Id.
1622 elsif Chars (Ent) = Name_Raise_Exception then
1623 return;
1624 end if;
1625 end;
1626 end if;
1627 end if;
1628 end;
1629 end;
1631 -- Check for variables that are never modified
1633 declare
1634 E1, E2 : Entity_Id;
1636 begin
1637 -- If there is a separate spec, then transfer Never_Set_In_Source
1638 -- flags from out parameters to the corresponding entities in the
1639 -- body. The reason we do that is we want to post error flags on
1640 -- the body entities, not the spec entities.
1642 if Present (Spec_Id) then
1643 E1 := First_Entity (Spec_Id);
1645 while Present (E1) loop
1646 if Ekind (E1) = E_Out_Parameter then
1647 E2 := First_Entity (Body_Id);
1648 while Present (E2) loop
1649 exit when Chars (E1) = Chars (E2);
1650 Next_Entity (E2);
1651 end loop;
1653 if Present (E2) then
1654 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
1655 end if;
1656 end if;
1658 Next_Entity (E1);
1659 end loop;
1660 end if;
1662 -- Check references in body unless it was deleted. Note that the
1663 -- check of Body_Deleted here is not just for efficiency, it is
1664 -- necessary to avoid junk warnings on formal parameters.
1666 if not Body_Deleted then
1667 Check_References (Body_Id);
1668 end if;
1669 end;
1670 end Analyze_Subprogram_Body;
1672 ------------------------------------
1673 -- Analyze_Subprogram_Declaration --
1674 ------------------------------------
1676 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
1677 Designator : constant Entity_Id :=
1678 Analyze_Subprogram_Specification (Specification (N));
1679 Scop : constant Entity_Id := Current_Scope;
1681 -- Start of processing for Analyze_Subprogram_Declaration
1683 begin
1684 Generate_Definition (Designator);
1686 -- Check for RCI unit subprogram declarations against in-lined
1687 -- subprograms and subprograms having access parameter or limited
1688 -- parameter without Read and Write (RM E.2.3(12-13)).
1690 Validate_RCI_Subprogram_Declaration (N);
1692 Trace_Scope
1694 Defining_Entity (N),
1695 " Analyze subprogram spec. ");
1697 if Debug_Flag_C then
1698 Write_Str ("==== Compiling subprogram spec ");
1699 Write_Name (Chars (Designator));
1700 Write_Str (" from ");
1701 Write_Location (Sloc (N));
1702 Write_Eol;
1703 end if;
1705 New_Overloaded_Entity (Designator);
1706 Check_Delayed_Subprogram (Designator);
1708 -- What is the following code for, it used to be
1710 -- ??? Set_Suppress_Elaboration_Checks
1711 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
1713 -- The following seems equivalent, but a bit dubious
1715 if Elaboration_Checks_Suppressed (Designator) then
1716 Set_Kill_Elaboration_Checks (Designator);
1717 end if;
1719 if Scop /= Standard_Standard
1720 and then not Is_Child_Unit (Designator)
1721 then
1722 Set_Categorization_From_Scope (Designator, Scop);
1723 else
1724 -- For a compilation unit, check for library-unit pragmas
1726 New_Scope (Designator);
1727 Set_Categorization_From_Pragmas (N);
1728 Validate_Categorization_Dependency (N, Designator);
1729 Pop_Scope;
1730 end if;
1732 -- For a compilation unit, set body required. This flag will only be
1733 -- reset if a valid Import or Interface pragma is processed later on.
1735 if Nkind (Parent (N)) = N_Compilation_Unit then
1736 Set_Body_Required (Parent (N), True);
1738 if Ada_Version >= Ada_05
1739 and then Nkind (Specification (N)) = N_Procedure_Specification
1740 and then Null_Present (Specification (N))
1741 then
1742 Error_Msg_N
1743 ("null procedure cannot be declared at library level", N);
1744 end if;
1745 end if;
1747 Generate_Reference_To_Formals (Designator);
1748 Check_Eliminated (Designator);
1750 -- Ada 2005: if procedure is declared with "is null" qualifier,
1751 -- it requires no body.
1753 if Nkind (Specification (N)) = N_Procedure_Specification
1754 and then Null_Present (Specification (N))
1755 then
1756 Set_Has_Completion (Designator);
1757 Set_Is_Inlined (Designator);
1758 end if;
1759 end Analyze_Subprogram_Declaration;
1761 --------------------------------------
1762 -- Analyze_Subprogram_Specification --
1763 --------------------------------------
1765 -- Reminder: N here really is a subprogram specification (not a subprogram
1766 -- declaration). This procedure is called to analyze the specification in
1767 -- both subprogram bodies and subprogram declarations (specs).
1769 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
1770 Designator : constant Entity_Id := Defining_Entity (N);
1771 Formals : constant List_Id := Parameter_Specifications (N);
1773 function Has_Interface_Formals (T : List_Id) return Boolean;
1774 -- Ada 2005 (AI-251): Returns true if some non class-wide interface
1775 -- formal is found.
1777 ---------------------------
1778 -- Has_Interface_Formals --
1779 ---------------------------
1781 function Has_Interface_Formals (T : List_Id) return Boolean is
1782 Param_Spec : Node_Id;
1783 Formal : Entity_Id;
1785 begin
1786 Param_Spec := First (T);
1788 while Present (Param_Spec) loop
1789 Formal := Defining_Identifier (Param_Spec);
1791 if Is_Class_Wide_Type (Etype (Formal)) then
1792 null;
1794 elsif Is_Interface (Etype (Formal)) then
1795 return True;
1796 end if;
1798 Next (Param_Spec);
1799 end loop;
1801 return False;
1802 end Has_Interface_Formals;
1804 -- Start of processing for Analyze_Subprogram_Specification
1806 begin
1807 Generate_Definition (Designator);
1809 if Nkind (N) = N_Function_Specification then
1810 Set_Ekind (Designator, E_Function);
1811 Set_Mechanism (Designator, Default_Mechanism);
1813 else
1814 Set_Ekind (Designator, E_Procedure);
1815 Set_Etype (Designator, Standard_Void_Type);
1816 end if;
1818 -- Introduce new scope for analysis of the formals and of the
1819 -- return type.
1821 Set_Scope (Designator, Current_Scope);
1823 if Present (Formals) then
1824 New_Scope (Designator);
1825 Process_Formals (Formals, N);
1827 -- Ada 2005 (AI-345): Allow overriding primitives of protected
1828 -- interfaces by means of normal subprograms. For this purpose
1829 -- temporarily use the corresponding record type as the etype
1830 -- of the first formal.
1832 if Ada_Version >= Ada_05
1833 and then Comes_From_Source (Designator)
1834 and then Present (First_Entity (Designator))
1835 and then (Ekind (Etype (First_Entity (Designator)))
1836 = E_Protected_Type
1837 or else
1838 Ekind (Etype (First_Entity (Designator)))
1839 = E_Task_Type)
1840 and then Present (Corresponding_Record_Type
1841 (Etype (First_Entity (Designator))))
1842 and then Present (Abstract_Interfaces
1843 (Corresponding_Record_Type
1844 (Etype (First_Entity (Designator)))))
1845 then
1846 Set_Etype (First_Entity (Designator),
1847 Corresponding_Record_Type (Etype (First_Entity (Designator))));
1848 end if;
1850 End_Scope;
1852 elsif Nkind (N) = N_Function_Specification then
1853 Analyze_Return_Type (N);
1854 end if;
1856 if Nkind (N) = N_Function_Specification then
1857 if Nkind (Designator) = N_Defining_Operator_Symbol then
1858 Valid_Operator_Definition (Designator);
1859 end if;
1861 May_Need_Actuals (Designator);
1863 if Is_Abstract (Etype (Designator))
1864 and then Nkind (Parent (N))
1865 /= N_Abstract_Subprogram_Declaration
1866 and then (Nkind (Parent (N)))
1867 /= N_Formal_Abstract_Subprogram_Declaration
1868 and then (Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1869 or else not Is_Entity_Name (Name (Parent (N)))
1870 or else not Is_Abstract (Entity (Name (Parent (N)))))
1871 then
1872 Error_Msg_N
1873 ("function that returns abstract type must be abstract", N);
1874 end if;
1875 end if;
1877 if Ada_Version >= Ada_05
1878 and then Comes_From_Source (N)
1879 and then Nkind (Parent (N)) /= N_Abstract_Subprogram_Declaration
1880 and then (Nkind (N) /= N_Procedure_Specification
1881 or else
1882 not Null_Present (N))
1883 and then Has_Interface_Formals (Formals)
1884 then
1885 Error_Msg_Name_1 := Chars (Defining_Unit_Name
1886 (Specification (Parent (N))));
1887 Error_Msg_N
1888 ("(Ada 2005) interface subprogram % must be abstract or null", N);
1889 end if;
1891 return Designator;
1892 end Analyze_Subprogram_Specification;
1894 --------------------------
1895 -- Build_Body_To_Inline --
1896 --------------------------
1898 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
1899 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
1900 Original_Body : Node_Id;
1901 Body_To_Analyze : Node_Id;
1902 Max_Size : constant := 10;
1903 Stat_Count : Integer := 0;
1905 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
1906 -- Check for declarations that make inlining not worthwhile
1908 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
1909 -- Check for statements that make inlining not worthwhile: any tasking
1910 -- statement, nested at any level. Keep track of total number of
1911 -- elementary statements, as a measure of acceptable size.
1913 function Has_Pending_Instantiation return Boolean;
1914 -- If some enclosing body contains instantiations that appear before
1915 -- the corresponding generic body, the enclosing body has a freeze node
1916 -- so that it can be elaborated after the generic itself. This might
1917 -- conflict with subsequent inlinings, so that it is unsafe to try to
1918 -- inline in such a case.
1920 function Has_Single_Return return Boolean;
1921 -- In general we cannot inline functions that return unconstrained
1922 -- type. However, we can handle such functions if all return statements
1923 -- return a local variable that is the only declaration in the body
1924 -- of the function. In that case the call can be replaced by that
1925 -- local variable as is done for other inlined calls.
1927 procedure Remove_Pragmas;
1928 -- A pragma Unreferenced that mentions a formal parameter has no
1929 -- meaning when the body is inlined and the formals are rewritten.
1930 -- Remove it from body to inline. The analysis of the non-inlined body
1931 -- will handle the pragma properly.
1933 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
1934 -- If the body of the subprogram includes a call that returns an
1935 -- unconstrained type, the secondary stack is involved, and it
1936 -- is not worth inlining.
1938 ------------------------------
1939 -- Has_Excluded_Declaration --
1940 ------------------------------
1942 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
1943 D : Node_Id;
1945 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
1946 -- Nested subprograms make a given body ineligible for inlining, but
1947 -- we make an exception for instantiations of unchecked conversion.
1948 -- The body has not been analyzed yet, so check the name, and verify
1949 -- that the visible entity with that name is the predefined unit.
1951 -----------------------------
1952 -- Is_Unchecked_Conversion --
1953 -----------------------------
1955 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
1956 Id : constant Node_Id := Name (D);
1957 Conv : Entity_Id;
1959 begin
1960 if Nkind (Id) = N_Identifier
1961 and then Chars (Id) = Name_Unchecked_Conversion
1962 then
1963 Conv := Current_Entity (Id);
1965 elsif (Nkind (Id) = N_Selected_Component
1966 or else Nkind (Id) = N_Expanded_Name)
1967 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
1968 then
1969 Conv := Current_Entity (Selector_Name (Id));
1971 else
1972 return False;
1973 end if;
1975 return Present (Conv)
1976 and then Is_Predefined_File_Name
1977 (Unit_File_Name (Get_Source_Unit (Conv)))
1978 and then Is_Intrinsic_Subprogram (Conv);
1979 end Is_Unchecked_Conversion;
1981 -- Start of processing for Has_Excluded_Declaration
1983 begin
1984 D := First (Decls);
1986 while Present (D) loop
1987 if (Nkind (D) = N_Function_Instantiation
1988 and then not Is_Unchecked_Conversion (D))
1989 or else Nkind (D) = N_Protected_Type_Declaration
1990 or else Nkind (D) = N_Package_Declaration
1991 or else Nkind (D) = N_Package_Instantiation
1992 or else Nkind (D) = N_Subprogram_Body
1993 or else Nkind (D) = N_Procedure_Instantiation
1994 or else Nkind (D) = N_Task_Type_Declaration
1995 then
1996 Cannot_Inline
1997 ("cannot inline & (non-allowed declaration)?", D, Subp);
1998 return True;
1999 end if;
2001 Next (D);
2002 end loop;
2004 return False;
2005 end Has_Excluded_Declaration;
2007 ----------------------------
2008 -- Has_Excluded_Statement --
2009 ----------------------------
2011 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
2012 S : Node_Id;
2013 E : Node_Id;
2015 begin
2016 S := First (Stats);
2018 while Present (S) loop
2019 Stat_Count := Stat_Count + 1;
2021 if Nkind (S) = N_Abort_Statement
2022 or else Nkind (S) = N_Asynchronous_Select
2023 or else Nkind (S) = N_Conditional_Entry_Call
2024 or else Nkind (S) = N_Delay_Relative_Statement
2025 or else Nkind (S) = N_Delay_Until_Statement
2026 or else Nkind (S) = N_Selective_Accept
2027 or else Nkind (S) = N_Timed_Entry_Call
2028 then
2029 Cannot_Inline
2030 ("cannot inline & (non-allowed statement)?", S, Subp);
2031 return True;
2033 elsif Nkind (S) = N_Block_Statement then
2034 if Present (Declarations (S))
2035 and then Has_Excluded_Declaration (Declarations (S))
2036 then
2037 return True;
2039 elsif Present (Handled_Statement_Sequence (S))
2040 and then
2041 (Present
2042 (Exception_Handlers (Handled_Statement_Sequence (S)))
2043 or else
2044 Has_Excluded_Statement
2045 (Statements (Handled_Statement_Sequence (S))))
2046 then
2047 return True;
2048 end if;
2050 elsif Nkind (S) = N_Case_Statement then
2051 E := First (Alternatives (S));
2052 while Present (E) loop
2053 if Has_Excluded_Statement (Statements (E)) then
2054 return True;
2055 end if;
2057 Next (E);
2058 end loop;
2060 elsif Nkind (S) = N_If_Statement then
2061 if Has_Excluded_Statement (Then_Statements (S)) then
2062 return True;
2063 end if;
2065 if Present (Elsif_Parts (S)) then
2066 E := First (Elsif_Parts (S));
2067 while Present (E) loop
2068 if Has_Excluded_Statement (Then_Statements (E)) then
2069 return True;
2070 end if;
2071 Next (E);
2072 end loop;
2073 end if;
2075 if Present (Else_Statements (S))
2076 and then Has_Excluded_Statement (Else_Statements (S))
2077 then
2078 return True;
2079 end if;
2081 elsif Nkind (S) = N_Loop_Statement
2082 and then Has_Excluded_Statement (Statements (S))
2083 then
2084 return True;
2085 end if;
2087 Next (S);
2088 end loop;
2090 return False;
2091 end Has_Excluded_Statement;
2093 -------------------------------
2094 -- Has_Pending_Instantiation --
2095 -------------------------------
2097 function Has_Pending_Instantiation return Boolean is
2098 S : Entity_Id := Current_Scope;
2100 begin
2101 while Present (S) loop
2102 if Is_Compilation_Unit (S)
2103 or else Is_Child_Unit (S)
2104 then
2105 return False;
2106 elsif Ekind (S) = E_Package
2107 and then Has_Forward_Instantiation (S)
2108 then
2109 return True;
2110 end if;
2112 S := Scope (S);
2113 end loop;
2115 return False;
2116 end Has_Pending_Instantiation;
2118 ------------------------
2119 -- Has_Single_Return --
2120 ------------------------
2122 function Has_Single_Return return Boolean is
2123 Return_Statement : Node_Id := Empty;
2125 function Check_Return (N : Node_Id) return Traverse_Result;
2127 ------------------
2128 -- Check_Return --
2129 ------------------
2131 function Check_Return (N : Node_Id) return Traverse_Result is
2132 begin
2133 if Nkind (N) = N_Return_Statement then
2134 if Present (Expression (N))
2135 and then Is_Entity_Name (Expression (N))
2136 then
2137 if No (Return_Statement) then
2138 Return_Statement := N;
2139 return OK;
2141 elsif Chars (Expression (N)) =
2142 Chars (Expression (Return_Statement))
2143 then
2144 return OK;
2146 else
2147 return Abandon;
2148 end if;
2150 else
2151 -- Expression has wrong form
2153 return Abandon;
2154 end if;
2156 else
2157 return OK;
2158 end if;
2159 end Check_Return;
2161 function Check_All_Returns is new Traverse_Func (Check_Return);
2163 -- Start of processing for Has_Single_Return
2165 begin
2166 return Check_All_Returns (N) = OK
2167 and then Present (Declarations (N))
2168 and then Chars (Expression (Return_Statement)) =
2169 Chars (Defining_Identifier (First (Declarations (N))));
2170 end Has_Single_Return;
2172 --------------------
2173 -- Remove_Pragmas --
2174 --------------------
2176 procedure Remove_Pragmas is
2177 Decl : Node_Id;
2178 Nxt : Node_Id;
2180 begin
2181 Decl := First (Declarations (Body_To_Analyze));
2182 while Present (Decl) loop
2183 Nxt := Next (Decl);
2185 if Nkind (Decl) = N_Pragma
2186 and then Chars (Decl) = Name_Unreferenced
2187 then
2188 Remove (Decl);
2189 end if;
2191 Decl := Nxt;
2192 end loop;
2193 end Remove_Pragmas;
2195 --------------------------
2196 -- Uses_Secondary_Stack --
2197 --------------------------
2199 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
2200 function Check_Call (N : Node_Id) return Traverse_Result;
2201 -- Look for function calls that return an unconstrained type
2203 ----------------
2204 -- Check_Call --
2205 ----------------
2207 function Check_Call (N : Node_Id) return Traverse_Result is
2208 begin
2209 if Nkind (N) = N_Function_Call
2210 and then Is_Entity_Name (Name (N))
2211 and then Is_Composite_Type (Etype (Entity (Name (N))))
2212 and then not Is_Constrained (Etype (Entity (Name (N))))
2213 then
2214 Cannot_Inline
2215 ("cannot inline & (call returns unconstrained type)?",
2216 N, Subp);
2217 return Abandon;
2218 else
2219 return OK;
2220 end if;
2221 end Check_Call;
2223 function Check_Calls is new Traverse_Func (Check_Call);
2225 begin
2226 return Check_Calls (Bod) = Abandon;
2227 end Uses_Secondary_Stack;
2229 -- Start of processing for Build_Body_To_Inline
2231 begin
2232 if Nkind (Decl) = N_Subprogram_Declaration
2233 and then Present (Body_To_Inline (Decl))
2234 then
2235 return; -- Done already.
2237 -- Functions that return unconstrained composite types require
2238 -- secondary stack handling, and cannot currently be inlined, unless
2239 -- all return statements return a local variable that is the first
2240 -- local declaration in the body.
2242 elsif Ekind (Subp) = E_Function
2243 and then not Is_Scalar_Type (Etype (Subp))
2244 and then not Is_Access_Type (Etype (Subp))
2245 and then not Is_Constrained (Etype (Subp))
2246 then
2247 if not Has_Single_Return then
2248 Cannot_Inline
2249 ("cannot inline & (unconstrained return type)?", N, Subp);
2250 return;
2251 end if;
2253 -- Ditto for functions that return controlled types, where controlled
2254 -- actions interfere in complex ways with inlining.
2256 elsif Ekind (Subp) = E_Function
2257 and then Controlled_Type (Etype (Subp))
2258 then
2259 Cannot_Inline
2260 ("cannot inline & (controlled return type)?", N, Subp);
2261 return;
2262 end if;
2264 if Present (Declarations (N))
2265 and then Has_Excluded_Declaration (Declarations (N))
2266 then
2267 return;
2268 end if;
2270 if Present (Handled_Statement_Sequence (N)) then
2271 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
2272 Cannot_Inline
2273 ("cannot inline& (exception handler)?",
2274 First (Exception_Handlers (Handled_Statement_Sequence (N))),
2275 Subp);
2276 return;
2277 elsif
2278 Has_Excluded_Statement
2279 (Statements (Handled_Statement_Sequence (N)))
2280 then
2281 return;
2282 end if;
2283 end if;
2285 -- We do not inline a subprogram that is too large, unless it is
2286 -- marked Inline_Always. This pragma does not suppress the other
2287 -- checks on inlining (forbidden declarations, handlers, etc).
2289 if Stat_Count > Max_Size
2290 and then not Is_Always_Inlined (Subp)
2291 then
2292 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
2293 return;
2294 end if;
2296 if Has_Pending_Instantiation then
2297 Cannot_Inline
2298 ("cannot inline& (forward instance within enclosing body)?",
2299 N, Subp);
2300 return;
2301 end if;
2303 -- Within an instance, the body to inline must be treated as a nested
2304 -- generic, so that the proper global references are preserved.
2306 if In_Instance then
2307 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
2308 Original_Body := Copy_Generic_Node (N, Empty, True);
2309 else
2310 Original_Body := Copy_Separate_Tree (N);
2311 end if;
2313 -- We need to capture references to the formals in order to substitute
2314 -- the actuals at the point of inlining, i.e. instantiation. To treat
2315 -- the formals as globals to the body to inline, we nest it within
2316 -- a dummy parameterless subprogram, declared within the real one.
2317 -- To avoid generating an internal name (which is never public, and
2318 -- which affects serial numbers of other generated names), we use
2319 -- an internal symbol that cannot conflict with user declarations.
2321 Set_Parameter_Specifications (Specification (Original_Body), No_List);
2322 Set_Defining_Unit_Name
2323 (Specification (Original_Body),
2324 Make_Defining_Identifier (Sloc (N), Name_uParent));
2325 Set_Corresponding_Spec (Original_Body, Empty);
2327 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
2329 -- Set return type of function, which is also global and does not need
2330 -- to be resolved.
2332 if Ekind (Subp) = E_Function then
2333 Set_Result_Definition (Specification (Body_To_Analyze),
2334 New_Occurrence_Of (Etype (Subp), Sloc (N)));
2335 end if;
2337 if No (Declarations (N)) then
2338 Set_Declarations (N, New_List (Body_To_Analyze));
2339 else
2340 Append (Body_To_Analyze, Declarations (N));
2341 end if;
2343 Expander_Mode_Save_And_Set (False);
2344 Remove_Pragmas;
2346 Analyze (Body_To_Analyze);
2347 New_Scope (Defining_Entity (Body_To_Analyze));
2348 Save_Global_References (Original_Body);
2349 End_Scope;
2350 Remove (Body_To_Analyze);
2352 Expander_Mode_Restore;
2354 if In_Instance then
2355 Restore_Env;
2356 end if;
2358 -- If secondary stk used there is no point in inlining. We have
2359 -- already issued the warning in this case, so nothing to do.
2361 if Uses_Secondary_Stack (Body_To_Analyze) then
2362 return;
2363 end if;
2365 Set_Body_To_Inline (Decl, Original_Body);
2366 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
2367 Set_Is_Inlined (Subp);
2368 end Build_Body_To_Inline;
2370 -------------------
2371 -- Cannot_Inline --
2372 -------------------
2374 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
2375 begin
2376 -- Do not emit warning if this is a predefined unit which is not
2377 -- the main unit. With validity checks enabled, some predefined
2378 -- subprograms may contain nested subprograms and become ineligible
2379 -- for inlining.
2381 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
2382 and then not In_Extended_Main_Source_Unit (Subp)
2383 then
2384 null;
2386 elsif Is_Always_Inlined (Subp) then
2388 -- Remove last character (question mark) to make this into an error,
2389 -- because the Inline_Always pragma cannot be obeyed.
2391 Error_Msg_NE (Msg (1 .. Msg'Length - 1), N, Subp);
2393 elsif Ineffective_Inline_Warnings then
2394 Error_Msg_NE (Msg, N, Subp);
2395 end if;
2396 end Cannot_Inline;
2398 -----------------------
2399 -- Check_Conformance --
2400 -----------------------
2402 procedure Check_Conformance
2403 (New_Id : Entity_Id;
2404 Old_Id : Entity_Id;
2405 Ctype : Conformance_Type;
2406 Errmsg : Boolean;
2407 Conforms : out Boolean;
2408 Err_Loc : Node_Id := Empty;
2409 Get_Inst : Boolean := False;
2410 Skip_Controlling_Formals : Boolean := False)
2412 Old_Type : constant Entity_Id := Etype (Old_Id);
2413 New_Type : constant Entity_Id := Etype (New_Id);
2414 Old_Formal : Entity_Id;
2415 New_Formal : Entity_Id;
2417 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
2418 -- Post error message for conformance error on given node. Two messages
2419 -- are output. The first points to the previous declaration with a
2420 -- general "no conformance" message. The second is the detailed reason,
2421 -- supplied as Msg. The parameter N provide information for a possible
2422 -- & insertion in the message, and also provides the location for
2423 -- posting the message in the absence of a specified Err_Loc location.
2425 -----------------------
2426 -- Conformance_Error --
2427 -----------------------
2429 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
2430 Enode : Node_Id;
2432 begin
2433 Conforms := False;
2435 if Errmsg then
2436 if No (Err_Loc) then
2437 Enode := N;
2438 else
2439 Enode := Err_Loc;
2440 end if;
2442 Error_Msg_Sloc := Sloc (Old_Id);
2444 case Ctype is
2445 when Type_Conformant =>
2446 Error_Msg_N
2447 ("not type conformant with declaration#!", Enode);
2449 when Mode_Conformant =>
2450 Error_Msg_N
2451 ("not mode conformant with declaration#!", Enode);
2453 when Subtype_Conformant =>
2454 Error_Msg_N
2455 ("not subtype conformant with declaration#!", Enode);
2457 when Fully_Conformant =>
2458 Error_Msg_N
2459 ("not fully conformant with declaration#!", Enode);
2460 end case;
2462 Error_Msg_NE (Msg, Enode, N);
2463 end if;
2464 end Conformance_Error;
2466 -- Start of processing for Check_Conformance
2468 begin
2469 Conforms := True;
2471 -- We need a special case for operators, since they don't appear
2472 -- explicitly.
2474 if Ctype = Type_Conformant then
2475 if Ekind (New_Id) = E_Operator
2476 and then Operator_Matches_Spec (New_Id, Old_Id)
2477 then
2478 return;
2479 end if;
2480 end if;
2482 -- If both are functions/operators, check return types conform
2484 if Old_Type /= Standard_Void_Type
2485 and then New_Type /= Standard_Void_Type
2486 then
2487 if not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
2488 Conformance_Error ("return type does not match!", New_Id);
2489 return;
2490 end if;
2492 -- Ada 2005 (AI-231): In case of anonymous access types check the
2493 -- null-exclusion and access-to-constant attributes must match.
2495 if Ada_Version >= Ada_05
2496 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
2497 and then
2498 (Can_Never_Be_Null (Old_Type)
2499 /= Can_Never_Be_Null (New_Type)
2500 or else Is_Access_Constant (Etype (Old_Type))
2501 /= Is_Access_Constant (Etype (New_Type)))
2502 then
2503 Conformance_Error ("return type does not match!", New_Id);
2504 return;
2505 end if;
2507 -- If either is a function/operator and the other isn't, error
2509 elsif Old_Type /= Standard_Void_Type
2510 or else New_Type /= Standard_Void_Type
2511 then
2512 Conformance_Error ("functions can only match functions!", New_Id);
2513 return;
2514 end if;
2516 -- In subtype conformant case, conventions must match (RM 6.3.1(16))
2517 -- If this is a renaming as body, refine error message to indicate that
2518 -- the conflict is with the original declaration. If the entity is not
2519 -- frozen, the conventions don't have to match, the one of the renamed
2520 -- entity is inherited.
2522 if Ctype >= Subtype_Conformant then
2523 if Convention (Old_Id) /= Convention (New_Id) then
2525 if not Is_Frozen (New_Id) then
2526 null;
2528 elsif Present (Err_Loc)
2529 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
2530 and then Present (Corresponding_Spec (Err_Loc))
2531 then
2532 Error_Msg_Name_1 := Chars (New_Id);
2533 Error_Msg_Name_2 :=
2534 Name_Ada + Convention_Id'Pos (Convention (New_Id));
2536 Conformance_Error ("prior declaration for% has convention %!");
2538 else
2539 Conformance_Error ("calling conventions do not match!");
2540 end if;
2542 return;
2544 elsif Is_Formal_Subprogram (Old_Id)
2545 or else Is_Formal_Subprogram (New_Id)
2546 then
2547 Conformance_Error ("formal subprograms not allowed!");
2548 return;
2549 end if;
2550 end if;
2552 -- Deal with parameters
2554 -- Note: we use the entity information, rather than going directly
2555 -- to the specification in the tree. This is not only simpler, but
2556 -- absolutely necessary for some cases of conformance tests between
2557 -- operators, where the declaration tree simply does not exist!
2559 Old_Formal := First_Formal (Old_Id);
2560 New_Formal := First_Formal (New_Id);
2562 while Present (Old_Formal) and then Present (New_Formal) loop
2563 if Is_Controlling_Formal (Old_Formal)
2564 and then Is_Controlling_Formal (New_Formal)
2565 and then Skip_Controlling_Formals
2566 then
2567 goto Skip_Controlling_Formal;
2568 end if;
2570 if Ctype = Fully_Conformant then
2572 -- Names must match. Error message is more accurate if we do
2573 -- this before checking that the types of the formals match.
2575 if Chars (Old_Formal) /= Chars (New_Formal) then
2576 Conformance_Error ("name & does not match!", New_Formal);
2578 -- Set error posted flag on new formal as well to stop
2579 -- junk cascaded messages in some cases.
2581 Set_Error_Posted (New_Formal);
2582 return;
2583 end if;
2584 end if;
2586 -- Types must always match. In the visible part of an instance,
2587 -- usual overloading rules for dispatching operations apply, and
2588 -- we check base types (not the actual subtypes).
2590 if In_Instance_Visible_Part
2591 and then Is_Dispatching_Operation (New_Id)
2592 then
2593 if not Conforming_Types
2594 (Base_Type (Etype (Old_Formal)),
2595 Base_Type (Etype (New_Formal)), Ctype, Get_Inst)
2596 then
2597 Conformance_Error ("type of & does not match!", New_Formal);
2598 return;
2599 end if;
2601 elsif not Conforming_Types
2602 (Etype (Old_Formal), Etype (New_Formal), Ctype, Get_Inst)
2603 then
2604 Conformance_Error ("type of & does not match!", New_Formal);
2605 return;
2606 end if;
2608 -- For mode conformance, mode must match
2610 if Ctype >= Mode_Conformant
2611 and then Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal)
2612 then
2613 Conformance_Error ("mode of & does not match!", New_Formal);
2614 return;
2615 end if;
2617 -- Full conformance checks
2619 if Ctype = Fully_Conformant then
2621 -- We have checked already that names match
2623 if Parameter_Mode (Old_Formal) = E_In_Parameter then
2625 -- Ada 2005 (AI-231): In case of anonymous access types check
2626 -- the null-exclusion and access-to-constant attributes must
2627 -- match.
2629 if Ada_Version >= Ada_05
2630 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
2631 and then
2632 (Can_Never_Be_Null (Old_Formal)
2633 /= Can_Never_Be_Null (New_Formal)
2634 or else Is_Access_Constant (Etype (Old_Formal))
2635 /= Is_Access_Constant (Etype (New_Formal)))
2636 then
2637 -- It is allowed to omit the null-exclusion in case of
2638 -- stream attribute subprograms
2640 declare
2641 TSS_Name : TSS_Name_Type;
2643 begin
2644 Get_Name_String (Chars (New_Id));
2645 TSS_Name :=
2646 TSS_Name_Type
2647 (Name_Buffer
2648 (Name_Len - TSS_Name'Length + 1 .. Name_Len));
2650 if TSS_Name /= TSS_Stream_Read
2651 and then TSS_Name /= TSS_Stream_Write
2652 and then TSS_Name /= TSS_Stream_Input
2653 and then TSS_Name /= TSS_Stream_Output
2654 then
2655 Conformance_Error
2656 ("type of & does not match!", New_Formal);
2657 return;
2658 end if;
2659 end;
2660 end if;
2662 -- Check default expressions for in parameters
2664 declare
2665 NewD : constant Boolean :=
2666 Present (Default_Value (New_Formal));
2667 OldD : constant Boolean :=
2668 Present (Default_Value (Old_Formal));
2669 begin
2670 if NewD or OldD then
2672 -- The old default value has been analyzed because the
2673 -- current full declaration will have frozen everything
2674 -- before. The new default values have not been
2675 -- analyzed, so analyze them now before we check for
2676 -- conformance.
2678 if NewD then
2679 New_Scope (New_Id);
2680 Analyze_Per_Use_Expression
2681 (Default_Value (New_Formal), Etype (New_Formal));
2682 End_Scope;
2683 end if;
2685 if not (NewD and OldD)
2686 or else not Fully_Conformant_Expressions
2687 (Default_Value (Old_Formal),
2688 Default_Value (New_Formal))
2689 then
2690 Conformance_Error
2691 ("default expression for & does not match!",
2692 New_Formal);
2693 return;
2694 end if;
2695 end if;
2696 end;
2697 end if;
2698 end if;
2700 -- A couple of special checks for Ada 83 mode. These checks are
2701 -- skipped if either entity is an operator in package Standard.
2702 -- or if either old or new instance is not from the source program.
2704 if Ada_Version = Ada_83
2705 and then Sloc (Old_Id) > Standard_Location
2706 and then Sloc (New_Id) > Standard_Location
2707 and then Comes_From_Source (Old_Id)
2708 and then Comes_From_Source (New_Id)
2709 then
2710 declare
2711 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
2712 New_Param : constant Node_Id := Declaration_Node (New_Formal);
2714 begin
2715 -- Explicit IN must be present or absent in both cases. This
2716 -- test is required only in the full conformance case.
2718 if In_Present (Old_Param) /= In_Present (New_Param)
2719 and then Ctype = Fully_Conformant
2720 then
2721 Conformance_Error
2722 ("(Ada 83) IN must appear in both declarations",
2723 New_Formal);
2724 return;
2725 end if;
2727 -- Grouping (use of comma in param lists) must be the same
2728 -- This is where we catch a misconformance like:
2730 -- A,B : Integer
2731 -- A : Integer; B : Integer
2733 -- which are represented identically in the tree except
2734 -- for the setting of the flags More_Ids and Prev_Ids.
2736 if More_Ids (Old_Param) /= More_Ids (New_Param)
2737 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
2738 then
2739 Conformance_Error
2740 ("grouping of & does not match!", New_Formal);
2741 return;
2742 end if;
2743 end;
2744 end if;
2746 -- This label is required when skipping controlling formals
2748 <<Skip_Controlling_Formal>>
2750 Next_Formal (Old_Formal);
2751 Next_Formal (New_Formal);
2752 end loop;
2754 if Present (Old_Formal) then
2755 Conformance_Error ("too few parameters!");
2756 return;
2758 elsif Present (New_Formal) then
2759 Conformance_Error ("too many parameters!", New_Formal);
2760 return;
2761 end if;
2762 end Check_Conformance;
2764 ------------------------------
2765 -- Check_Delayed_Subprogram --
2766 ------------------------------
2768 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
2769 F : Entity_Id;
2771 procedure Possible_Freeze (T : Entity_Id);
2772 -- T is the type of either a formal parameter or of the return type.
2773 -- If T is not yet frozen and needs a delayed freeze, then the
2774 -- subprogram itself must be delayed.
2776 ---------------------
2777 -- Possible_Freeze --
2778 ---------------------
2780 procedure Possible_Freeze (T : Entity_Id) is
2781 begin
2782 if Has_Delayed_Freeze (T)
2783 and then not Is_Frozen (T)
2784 then
2785 Set_Has_Delayed_Freeze (Designator);
2787 elsif Is_Access_Type (T)
2788 and then Has_Delayed_Freeze (Designated_Type (T))
2789 and then not Is_Frozen (Designated_Type (T))
2790 then
2791 Set_Has_Delayed_Freeze (Designator);
2792 end if;
2793 end Possible_Freeze;
2795 -- Start of processing for Check_Delayed_Subprogram
2797 begin
2798 -- Never need to freeze abstract subprogram
2800 if Is_Abstract (Designator) then
2801 null;
2802 else
2803 -- Need delayed freeze if return type itself needs a delayed
2804 -- freeze and is not yet frozen.
2806 Possible_Freeze (Etype (Designator));
2807 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
2809 -- Need delayed freeze if any of the formal types themselves need
2810 -- a delayed freeze and are not yet frozen.
2812 F := First_Formal (Designator);
2813 while Present (F) loop
2814 Possible_Freeze (Etype (F));
2815 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
2816 Next_Formal (F);
2817 end loop;
2818 end if;
2820 -- Mark functions that return by reference. Note that it cannot be
2821 -- done for delayed_freeze subprograms because the underlying
2822 -- returned type may not be known yet (for private types)
2824 if not Has_Delayed_Freeze (Designator)
2825 and then Expander_Active
2826 then
2827 declare
2828 Typ : constant Entity_Id := Etype (Designator);
2829 Utyp : constant Entity_Id := Underlying_Type (Typ);
2831 begin
2832 if Is_Return_By_Reference_Type (Typ) then
2833 Set_Returns_By_Ref (Designator);
2835 elsif Present (Utyp) and then Controlled_Type (Utyp) then
2836 Set_Returns_By_Ref (Designator);
2837 end if;
2838 end;
2839 end if;
2840 end Check_Delayed_Subprogram;
2842 ------------------------------------
2843 -- Check_Discriminant_Conformance --
2844 ------------------------------------
2846 procedure Check_Discriminant_Conformance
2847 (N : Node_Id;
2848 Prev : Entity_Id;
2849 Prev_Loc : Node_Id)
2851 Old_Discr : Entity_Id := First_Discriminant (Prev);
2852 New_Discr : Node_Id := First (Discriminant_Specifications (N));
2853 New_Discr_Id : Entity_Id;
2854 New_Discr_Type : Entity_Id;
2856 procedure Conformance_Error (Msg : String; N : Node_Id);
2857 -- Post error message for conformance error on given node. Two messages
2858 -- are output. The first points to the previous declaration with a
2859 -- general "no conformance" message. The second is the detailed reason,
2860 -- supplied as Msg. The parameter N provide information for a possible
2861 -- & insertion in the message.
2863 -----------------------
2864 -- Conformance_Error --
2865 -----------------------
2867 procedure Conformance_Error (Msg : String; N : Node_Id) is
2868 begin
2869 Error_Msg_Sloc := Sloc (Prev_Loc);
2870 Error_Msg_N ("not fully conformant with declaration#!", N);
2871 Error_Msg_NE (Msg, N, N);
2872 end Conformance_Error;
2874 -- Start of processing for Check_Discriminant_Conformance
2876 begin
2877 while Present (Old_Discr) and then Present (New_Discr) loop
2879 New_Discr_Id := Defining_Identifier (New_Discr);
2881 -- The subtype mark of the discriminant on the full type has not
2882 -- been analyzed so we do it here. For an access discriminant a new
2883 -- type is created.
2885 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
2886 New_Discr_Type :=
2887 Access_Definition (N, Discriminant_Type (New_Discr));
2889 else
2890 Analyze (Discriminant_Type (New_Discr));
2891 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
2892 end if;
2894 if not Conforming_Types
2895 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
2896 then
2897 Conformance_Error ("type of & does not match!", New_Discr_Id);
2898 return;
2899 else
2900 -- Treat the new discriminant as an occurrence of the old one,
2901 -- for navigation purposes, and fill in some semantic
2902 -- information, for completeness.
2904 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
2905 Set_Etype (New_Discr_Id, Etype (Old_Discr));
2906 Set_Scope (New_Discr_Id, Scope (Old_Discr));
2907 end if;
2909 -- Names must match
2911 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
2912 Conformance_Error ("name & does not match!", New_Discr_Id);
2913 return;
2914 end if;
2916 -- Default expressions must match
2918 declare
2919 NewD : constant Boolean :=
2920 Present (Expression (New_Discr));
2921 OldD : constant Boolean :=
2922 Present (Expression (Parent (Old_Discr)));
2924 begin
2925 if NewD or OldD then
2927 -- The old default value has been analyzed and expanded,
2928 -- because the current full declaration will have frozen
2929 -- everything before. The new default values have not been
2930 -- expanded, so expand now to check conformance.
2932 if NewD then
2933 Analyze_Per_Use_Expression
2934 (Expression (New_Discr), New_Discr_Type);
2935 end if;
2937 if not (NewD and OldD)
2938 or else not Fully_Conformant_Expressions
2939 (Expression (Parent (Old_Discr)),
2940 Expression (New_Discr))
2942 then
2943 Conformance_Error
2944 ("default expression for & does not match!",
2945 New_Discr_Id);
2946 return;
2947 end if;
2948 end if;
2949 end;
2951 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
2953 if Ada_Version = Ada_83 then
2954 declare
2955 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
2957 begin
2958 -- Grouping (use of comma in param lists) must be the same
2959 -- This is where we catch a misconformance like:
2961 -- A,B : Integer
2962 -- A : Integer; B : Integer
2964 -- which are represented identically in the tree except
2965 -- for the setting of the flags More_Ids and Prev_Ids.
2967 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
2968 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
2969 then
2970 Conformance_Error
2971 ("grouping of & does not match!", New_Discr_Id);
2972 return;
2973 end if;
2974 end;
2975 end if;
2977 Next_Discriminant (Old_Discr);
2978 Next (New_Discr);
2979 end loop;
2981 if Present (Old_Discr) then
2982 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
2983 return;
2985 elsif Present (New_Discr) then
2986 Conformance_Error
2987 ("too many discriminants!", Defining_Identifier (New_Discr));
2988 return;
2989 end if;
2990 end Check_Discriminant_Conformance;
2992 ----------------------------
2993 -- Check_Fully_Conformant --
2994 ----------------------------
2996 procedure Check_Fully_Conformant
2997 (New_Id : Entity_Id;
2998 Old_Id : Entity_Id;
2999 Err_Loc : Node_Id := Empty)
3001 Result : Boolean;
3002 begin
3003 Check_Conformance
3004 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
3005 end Check_Fully_Conformant;
3007 ---------------------------
3008 -- Check_Mode_Conformant --
3009 ---------------------------
3011 procedure Check_Mode_Conformant
3012 (New_Id : Entity_Id;
3013 Old_Id : Entity_Id;
3014 Err_Loc : Node_Id := Empty;
3015 Get_Inst : Boolean := False)
3017 Result : Boolean;
3019 begin
3020 Check_Conformance
3021 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
3022 end Check_Mode_Conformant;
3024 --------------------------------
3025 -- Check_Overriding_Indicator --
3026 --------------------------------
3028 procedure Check_Overriding_Indicator
3029 (Subp : Entity_Id;
3030 Does_Override : Boolean)
3032 Decl : Node_Id;
3033 Spec : Node_Id;
3035 begin
3036 if Ekind (Subp) = E_Enumeration_Literal then
3038 -- No overriding indicator for literals
3040 return;
3042 else
3043 Decl := Unit_Declaration_Node (Subp);
3044 end if;
3046 if Nkind (Decl) = N_Subprogram_Declaration
3047 or else Nkind (Decl) = N_Subprogram_Body
3048 or else Nkind (Decl) = N_Subprogram_Renaming_Declaration
3049 or else Nkind (Decl) = N_Subprogram_Body_Stub
3050 then
3051 Spec := Specification (Decl);
3052 else
3053 return;
3054 end if;
3056 if not Does_Override then
3057 if Must_Override (Spec) then
3058 Error_Msg_NE ("subprogram& is not overriding", Spec, Subp);
3059 end if;
3061 else
3062 if Must_Not_Override (Spec) then
3063 Error_Msg_NE
3064 ("subprogram& overrides inherited operation", Spec, Subp);
3065 end if;
3066 end if;
3067 end Check_Overriding_Indicator;
3069 -------------------
3070 -- Check_Returns --
3071 -------------------
3073 procedure Check_Returns
3074 (HSS : Node_Id;
3075 Mode : Character;
3076 Err : out Boolean;
3077 Proc : Entity_Id := Empty)
3079 Handler : Node_Id;
3081 procedure Check_Statement_Sequence (L : List_Id);
3082 -- Internal recursive procedure to check a list of statements for proper
3083 -- termination by a return statement (or a transfer of control or a
3084 -- compound statement that is itself internally properly terminated).
3086 ------------------------------
3087 -- Check_Statement_Sequence --
3088 ------------------------------
3090 procedure Check_Statement_Sequence (L : List_Id) is
3091 Last_Stm : Node_Id;
3092 Kind : Node_Kind;
3094 Raise_Exception_Call : Boolean;
3095 -- Set True if statement sequence terminated by Raise_Exception call
3096 -- or a Reraise_Occurrence call.
3098 begin
3099 Raise_Exception_Call := False;
3101 -- Get last real statement
3103 Last_Stm := Last (L);
3105 -- Don't count pragmas
3107 while Nkind (Last_Stm) = N_Pragma
3109 -- Don't count call to SS_Release (can happen after Raise_Exception)
3111 or else
3112 (Nkind (Last_Stm) = N_Procedure_Call_Statement
3113 and then
3114 Nkind (Name (Last_Stm)) = N_Identifier
3115 and then
3116 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
3118 -- Don't count exception junk
3120 or else
3121 ((Nkind (Last_Stm) = N_Goto_Statement
3122 or else Nkind (Last_Stm) = N_Label
3123 or else Nkind (Last_Stm) = N_Object_Declaration)
3124 and then Exception_Junk (Last_Stm))
3125 loop
3126 Prev (Last_Stm);
3127 end loop;
3129 -- Here we have the "real" last statement
3131 Kind := Nkind (Last_Stm);
3133 -- Transfer of control, OK. Note that in the No_Return procedure
3134 -- case, we already diagnosed any explicit return statements, so
3135 -- we can treat them as OK in this context.
3137 if Is_Transfer (Last_Stm) then
3138 return;
3140 -- Check cases of explicit non-indirect procedure calls
3142 elsif Kind = N_Procedure_Call_Statement
3143 and then Is_Entity_Name (Name (Last_Stm))
3144 then
3145 -- Check call to Raise_Exception procedure which is treated
3146 -- specially, as is a call to Reraise_Occurrence.
3148 -- We suppress the warning in these cases since it is likely that
3149 -- the programmer really does not expect to deal with the case
3150 -- of Null_Occurrence, and thus would find a warning about a
3151 -- missing return curious, and raising Program_Error does not
3152 -- seem such a bad behavior if this does occur.
3154 -- Note that in the Ada 2005 case for Raise_Exception, the actual
3155 -- behavior will be to raise Constraint_Error (see AI-329).
3157 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
3158 or else
3159 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
3160 then
3161 Raise_Exception_Call := True;
3163 -- For Raise_Exception call, test first argument, if it is
3164 -- an attribute reference for a 'Identity call, then we know
3165 -- that the call cannot possibly return.
3167 declare
3168 Arg : constant Node_Id :=
3169 Original_Node (First_Actual (Last_Stm));
3171 begin
3172 if Nkind (Arg) = N_Attribute_Reference
3173 and then Attribute_Name (Arg) = Name_Identity
3174 then
3175 return;
3176 end if;
3177 end;
3178 end if;
3180 -- If statement, need to look inside if there is an else and check
3181 -- each constituent statement sequence for proper termination.
3183 elsif Kind = N_If_Statement
3184 and then Present (Else_Statements (Last_Stm))
3185 then
3186 Check_Statement_Sequence (Then_Statements (Last_Stm));
3187 Check_Statement_Sequence (Else_Statements (Last_Stm));
3189 if Present (Elsif_Parts (Last_Stm)) then
3190 declare
3191 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
3193 begin
3194 while Present (Elsif_Part) loop
3195 Check_Statement_Sequence (Then_Statements (Elsif_Part));
3196 Next (Elsif_Part);
3197 end loop;
3198 end;
3199 end if;
3201 return;
3203 -- Case statement, check each case for proper termination
3205 elsif Kind = N_Case_Statement then
3206 declare
3207 Case_Alt : Node_Id;
3209 begin
3210 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
3211 while Present (Case_Alt) loop
3212 Check_Statement_Sequence (Statements (Case_Alt));
3213 Next_Non_Pragma (Case_Alt);
3214 end loop;
3215 end;
3217 return;
3219 -- Block statement, check its handled sequence of statements
3221 elsif Kind = N_Block_Statement then
3222 declare
3223 Err1 : Boolean;
3225 begin
3226 Check_Returns
3227 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
3229 if Err1 then
3230 Err := True;
3231 end if;
3233 return;
3234 end;
3236 -- Loop statement. If there is an iteration scheme, we can definitely
3237 -- fall out of the loop. Similarly if there is an exit statement, we
3238 -- can fall out. In either case we need a following return.
3240 elsif Kind = N_Loop_Statement then
3241 if Present (Iteration_Scheme (Last_Stm))
3242 or else Has_Exit (Entity (Identifier (Last_Stm)))
3243 then
3244 null;
3246 -- A loop with no exit statement or iteration scheme if either
3247 -- an inifite loop, or it has some other exit (raise/return).
3248 -- In either case, no warning is required.
3250 else
3251 return;
3252 end if;
3254 -- Timed entry call, check entry call and delay alternatives
3256 -- Note: in expanded code, the timed entry call has been converted
3257 -- to a set of expanded statements on which the check will work
3258 -- correctly in any case.
3260 elsif Kind = N_Timed_Entry_Call then
3261 declare
3262 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
3263 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
3265 begin
3266 -- If statement sequence of entry call alternative is missing,
3267 -- then we can definitely fall through, and we post the error
3268 -- message on the entry call alternative itself.
3270 if No (Statements (ECA)) then
3271 Last_Stm := ECA;
3273 -- If statement sequence of delay alternative is missing, then
3274 -- we can definitely fall through, and we post the error
3275 -- message on the delay alternative itself.
3277 -- Note: if both ECA and DCA are missing the return, then we
3278 -- post only one message, should be enough to fix the bugs.
3279 -- If not we will get a message next time on the DCA when the
3280 -- ECA is fixed!
3282 elsif No (Statements (DCA)) then
3283 Last_Stm := DCA;
3285 -- Else check both statement sequences
3287 else
3288 Check_Statement_Sequence (Statements (ECA));
3289 Check_Statement_Sequence (Statements (DCA));
3290 return;
3291 end if;
3292 end;
3294 -- Conditional entry call, check entry call and else part
3296 -- Note: in expanded code, the conditional entry call has been
3297 -- converted to a set of expanded statements on which the check
3298 -- will work correctly in any case.
3300 elsif Kind = N_Conditional_Entry_Call then
3301 declare
3302 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
3304 begin
3305 -- If statement sequence of entry call alternative is missing,
3306 -- then we can definitely fall through, and we post the error
3307 -- message on the entry call alternative itself.
3309 if No (Statements (ECA)) then
3310 Last_Stm := ECA;
3312 -- Else check statement sequence and else part
3314 else
3315 Check_Statement_Sequence (Statements (ECA));
3316 Check_Statement_Sequence (Else_Statements (Last_Stm));
3317 return;
3318 end if;
3319 end;
3320 end if;
3322 -- If we fall through, issue appropriate message
3324 if Mode = 'F' then
3325 if not Raise_Exception_Call then
3326 Error_Msg_N
3327 ("?RETURN statement missing following this statement",
3328 Last_Stm);
3329 Error_Msg_N
3330 ("\?Program_Error may be raised at run time",
3331 Last_Stm);
3332 end if;
3334 -- Note: we set Err even though we have not issued a warning
3335 -- because we still have a case of a missing return. This is
3336 -- an extremely marginal case, probably will never be noticed
3337 -- but we might as well get it right.
3339 Err := True;
3341 -- Otherwise we have the case of a procedure marked No_Return
3343 else
3344 Error_Msg_N
3345 ("?implied return after this statement will raise Program_Error",
3346 Last_Stm);
3347 Error_Msg_NE
3348 ("?procedure & is marked as No_Return",
3349 Last_Stm, Proc);
3351 declare
3352 RE : constant Node_Id :=
3353 Make_Raise_Program_Error (Sloc (Last_Stm),
3354 Reason => PE_Implicit_Return);
3355 begin
3356 Insert_After (Last_Stm, RE);
3357 Analyze (RE);
3358 end;
3359 end if;
3360 end Check_Statement_Sequence;
3362 -- Start of processing for Check_Returns
3364 begin
3365 Err := False;
3366 Check_Statement_Sequence (Statements (HSS));
3368 if Present (Exception_Handlers (HSS)) then
3369 Handler := First_Non_Pragma (Exception_Handlers (HSS));
3370 while Present (Handler) loop
3371 Check_Statement_Sequence (Statements (Handler));
3372 Next_Non_Pragma (Handler);
3373 end loop;
3374 end if;
3375 end Check_Returns;
3377 ----------------------------
3378 -- Check_Subprogram_Order --
3379 ----------------------------
3381 procedure Check_Subprogram_Order (N : Node_Id) is
3383 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
3384 -- This is used to check if S1 > S2 in the sense required by this
3385 -- test, for example nameab < namec, but name2 < name10.
3387 -----------------------------
3388 -- Subprogram_Name_Greater --
3389 -----------------------------
3391 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
3392 L1, L2 : Positive;
3393 N1, N2 : Natural;
3395 begin
3396 -- Remove trailing numeric parts
3398 L1 := S1'Last;
3399 while S1 (L1) in '0' .. '9' loop
3400 L1 := L1 - 1;
3401 end loop;
3403 L2 := S2'Last;
3404 while S2 (L2) in '0' .. '9' loop
3405 L2 := L2 - 1;
3406 end loop;
3408 -- If non-numeric parts non-equal, that's decisive
3410 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
3411 return False;
3413 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
3414 return True;
3416 -- If non-numeric parts equal, compare suffixed numeric parts. Note
3417 -- that a missing suffix is treated as numeric zero in this test.
3419 else
3420 N1 := 0;
3421 while L1 < S1'Last loop
3422 L1 := L1 + 1;
3423 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
3424 end loop;
3426 N2 := 0;
3427 while L2 < S2'Last loop
3428 L2 := L2 + 1;
3429 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
3430 end loop;
3432 return N1 > N2;
3433 end if;
3434 end Subprogram_Name_Greater;
3436 -- Start of processing for Check_Subprogram_Order
3438 begin
3439 -- Check body in alpha order if this is option
3441 if Style_Check
3442 and then Style_Check_Order_Subprograms
3443 and then Nkind (N) = N_Subprogram_Body
3444 and then Comes_From_Source (N)
3445 and then In_Extended_Main_Source_Unit (N)
3446 then
3447 declare
3448 LSN : String_Ptr
3449 renames Scope_Stack.Table
3450 (Scope_Stack.Last).Last_Subprogram_Name;
3452 Body_Id : constant Entity_Id :=
3453 Defining_Entity (Specification (N));
3455 begin
3456 Get_Decoded_Name_String (Chars (Body_Id));
3458 if LSN /= null then
3459 if Subprogram_Name_Greater
3460 (LSN.all, Name_Buffer (1 .. Name_Len))
3461 then
3462 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
3463 end if;
3465 Free (LSN);
3466 end if;
3468 LSN := new String'(Name_Buffer (1 .. Name_Len));
3469 end;
3470 end if;
3471 end Check_Subprogram_Order;
3473 ------------------------------
3474 -- Check_Subtype_Conformant --
3475 ------------------------------
3477 procedure Check_Subtype_Conformant
3478 (New_Id : Entity_Id;
3479 Old_Id : Entity_Id;
3480 Err_Loc : Node_Id := Empty)
3482 Result : Boolean;
3483 begin
3484 Check_Conformance
3485 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc);
3486 end Check_Subtype_Conformant;
3488 ---------------------------
3489 -- Check_Type_Conformant --
3490 ---------------------------
3492 procedure Check_Type_Conformant
3493 (New_Id : Entity_Id;
3494 Old_Id : Entity_Id;
3495 Err_Loc : Node_Id := Empty)
3497 Result : Boolean;
3498 begin
3499 Check_Conformance
3500 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
3501 end Check_Type_Conformant;
3503 ----------------------
3504 -- Conforming_Types --
3505 ----------------------
3507 function Conforming_Types
3508 (T1 : Entity_Id;
3509 T2 : Entity_Id;
3510 Ctype : Conformance_Type;
3511 Get_Inst : Boolean := False) return Boolean
3513 Type_1 : Entity_Id := T1;
3514 Type_2 : Entity_Id := T2;
3515 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
3517 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
3518 -- If neither T1 nor T2 are generic actual types, or if they are
3519 -- in different scopes (e.g. parent and child instances), then verify
3520 -- that the base types are equal. Otherwise T1 and T2 must be
3521 -- on the same subtype chain. The whole purpose of this procedure
3522 -- is to prevent spurious ambiguities in an instantiation that may
3523 -- arise if two distinct generic types are instantiated with the
3524 -- same actual.
3526 ----------------------
3527 -- Base_Types_Match --
3528 ----------------------
3530 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
3531 begin
3532 if T1 = T2 then
3533 return True;
3535 elsif Base_Type (T1) = Base_Type (T2) then
3537 -- The following is too permissive. A more precise test must
3538 -- check that the generic actual is an ancestor subtype of the
3539 -- other ???.
3541 return not Is_Generic_Actual_Type (T1)
3542 or else not Is_Generic_Actual_Type (T2)
3543 or else Scope (T1) /= Scope (T2);
3545 -- In some cases a type imported through a limited_with clause,
3546 -- and its non-limited view are both visible, for example in an
3547 -- anonymous access_to_classwide type in a formal. Both entities
3548 -- designate the same type.
3550 elsif From_With_Type (T1)
3551 and then Ekind (T1) = E_Incomplete_Type
3552 and then T2 = Non_Limited_View (T1)
3553 then
3554 return True;
3556 elsif From_With_Type (T2)
3557 and then Ekind (T2) = E_Incomplete_Type
3558 and then T1 = Non_Limited_View (T2)
3559 then
3560 return True;
3562 else
3563 return False;
3564 end if;
3565 end Base_Types_Match;
3567 -- Start of processing for Conforming_Types
3569 begin
3570 -- The context is an instance association for a formal
3571 -- access-to-subprogram type; the formal parameter types require
3572 -- mapping because they may denote other formal parameters of the
3573 -- generic unit.
3575 if Get_Inst then
3576 Type_1 := Get_Instance_Of (T1);
3577 Type_2 := Get_Instance_Of (T2);
3578 end if;
3580 -- First see if base types match
3582 if Base_Types_Match (Type_1, Type_2) then
3583 return Ctype <= Mode_Conformant
3584 or else Subtypes_Statically_Match (Type_1, Type_2);
3586 elsif Is_Incomplete_Or_Private_Type (Type_1)
3587 and then Present (Full_View (Type_1))
3588 and then Base_Types_Match (Full_View (Type_1), Type_2)
3589 then
3590 return Ctype <= Mode_Conformant
3591 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
3593 elsif Ekind (Type_2) = E_Incomplete_Type
3594 and then Present (Full_View (Type_2))
3595 and then Base_Types_Match (Type_1, Full_View (Type_2))
3596 then
3597 return Ctype <= Mode_Conformant
3598 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3600 elsif Is_Private_Type (Type_2)
3601 and then In_Instance
3602 and then Present (Full_View (Type_2))
3603 and then Base_Types_Match (Type_1, Full_View (Type_2))
3604 then
3605 return Ctype <= Mode_Conformant
3606 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3607 end if;
3609 -- Ada 2005 (AI-254): Anonymous access to subprogram types must be
3610 -- treated recursively because they carry a signature.
3612 Are_Anonymous_Access_To_Subprogram_Types :=
3614 -- Case 1: Anonymous access to subprogram types
3616 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
3617 and then Ekind (Type_2) = E_Anonymous_Access_Subprogram_Type)
3619 -- Case 2: Anonymous access to PROTECTED subprogram types. In this
3620 -- case the anonymous type_declaration has been replaced by an
3621 -- occurrence of an internal access to subprogram type declaration
3622 -- available through the Original_Access_Type attribute
3624 or else
3625 (Ekind (Type_1) = E_Access_Protected_Subprogram_Type
3626 and then Ekind (Type_2) = E_Access_Protected_Subprogram_Type
3627 and then not Comes_From_Source (Type_1)
3628 and then not Comes_From_Source (Type_2)
3629 and then Present (Original_Access_Type (Type_1))
3630 and then Present (Original_Access_Type (Type_2))
3631 and then Ekind (Original_Access_Type (Type_1)) =
3632 E_Anonymous_Access_Protected_Subprogram_Type
3633 and then Ekind (Original_Access_Type (Type_2)) =
3634 E_Anonymous_Access_Protected_Subprogram_Type);
3636 -- Test anonymous access type case. For this case, static subtype
3637 -- matching is required for mode conformance (RM 6.3.1(15))
3639 if (Ekind (Type_1) = E_Anonymous_Access_Type
3640 and then Ekind (Type_2) = E_Anonymous_Access_Type)
3641 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
3642 then
3643 declare
3644 Desig_1 : Entity_Id;
3645 Desig_2 : Entity_Id;
3647 begin
3648 Desig_1 := Directly_Designated_Type (Type_1);
3650 -- An access parameter can designate an incomplete type
3651 -- If the incomplete type is the limited view of a type
3652 -- from a limited_with_clause, check whether the non-limited
3653 -- view is available.
3655 if Ekind (Desig_1) = E_Incomplete_Type then
3656 if Present (Full_View (Desig_1)) then
3657 Desig_1 := Full_View (Desig_1);
3659 elsif Present (Non_Limited_View (Desig_1)) then
3660 Desig_1 := Non_Limited_View (Desig_1);
3661 end if;
3662 end if;
3664 Desig_2 := Directly_Designated_Type (Type_2);
3666 if Ekind (Desig_2) = E_Incomplete_Type then
3667 if Present (Full_View (Desig_2)) then
3668 Desig_2 := Full_View (Desig_2);
3669 elsif Present (Non_Limited_View (Desig_2)) then
3670 Desig_2 := Non_Limited_View (Desig_2);
3671 end if;
3672 end if;
3674 -- The context is an instance association for a formal
3675 -- access-to-subprogram type; formal access parameter designated
3676 -- types require mapping because they may denote other formal
3677 -- parameters of the generic unit.
3679 if Get_Inst then
3680 Desig_1 := Get_Instance_Of (Desig_1);
3681 Desig_2 := Get_Instance_Of (Desig_2);
3682 end if;
3684 -- It is possible for a Class_Wide_Type to be introduced for an
3685 -- incomplete type, in which case there is a separate class_ wide
3686 -- type for the full view. The types conform if their Etypes
3687 -- conform, i.e. one may be the full view of the other. This can
3688 -- only happen in the context of an access parameter, other uses
3689 -- of an incomplete Class_Wide_Type are illegal.
3691 if Is_Class_Wide_Type (Desig_1)
3692 and then Is_Class_Wide_Type (Desig_2)
3693 then
3694 return
3695 Conforming_Types
3696 (Etype (Base_Type (Desig_1)),
3697 Etype (Base_Type (Desig_2)), Ctype);
3699 elsif Are_Anonymous_Access_To_Subprogram_Types then
3700 if Ada_Version < Ada_05 then
3701 return Ctype = Type_Conformant
3702 or else
3703 Subtypes_Statically_Match (Desig_1, Desig_2);
3705 -- We must check the conformance of the signatures themselves
3707 else
3708 declare
3709 Conformant : Boolean;
3710 begin
3711 Check_Conformance
3712 (Desig_1, Desig_2, Ctype, False, Conformant);
3713 return Conformant;
3714 end;
3715 end if;
3717 else
3718 return Base_Type (Desig_1) = Base_Type (Desig_2)
3719 and then (Ctype = Type_Conformant
3720 or else
3721 Subtypes_Statically_Match (Desig_1, Desig_2));
3722 end if;
3723 end;
3725 -- Otherwise definitely no match
3727 else
3728 if ((Ekind (Type_1) = E_Anonymous_Access_Type
3729 and then Is_Access_Type (Type_2))
3730 or else (Ekind (Type_2) = E_Anonymous_Access_Type
3731 and then Is_Access_Type (Type_1)))
3732 and then
3733 Conforming_Types
3734 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
3735 then
3736 May_Hide_Profile := True;
3737 end if;
3739 return False;
3740 end if;
3741 end Conforming_Types;
3743 --------------------------
3744 -- Create_Extra_Formals --
3745 --------------------------
3747 procedure Create_Extra_Formals (E : Entity_Id) is
3748 Formal : Entity_Id;
3749 Last_Extra : Entity_Id;
3750 Formal_Type : Entity_Id;
3751 P_Formal : Entity_Id := Empty;
3753 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id;
3754 -- Add an extra formal, associated with the current Formal. The extra
3755 -- formal is added to the list of extra formals, and also returned as
3756 -- the result. These formals are always of mode IN.
3758 ----------------------
3759 -- Add_Extra_Formal --
3760 ----------------------
3762 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id is
3763 EF : constant Entity_Id :=
3764 Make_Defining_Identifier (Sloc (Formal),
3765 Chars => New_External_Name (Chars (Formal), 'F'));
3767 begin
3768 -- We never generate extra formals if expansion is not active
3769 -- because we don't need them unless we are generating code.
3771 if not Expander_Active then
3772 return Empty;
3773 end if;
3775 -- A little optimization. Never generate an extra formal for the
3776 -- _init operand of an initialization procedure, since it could
3777 -- never be used.
3779 if Chars (Formal) = Name_uInit then
3780 return Empty;
3781 end if;
3783 Set_Ekind (EF, E_In_Parameter);
3784 Set_Actual_Subtype (EF, Typ);
3785 Set_Etype (EF, Typ);
3786 Set_Scope (EF, Scope (Formal));
3787 Set_Mechanism (EF, Default_Mechanism);
3788 Set_Formal_Validity (EF);
3790 Set_Extra_Formal (Last_Extra, EF);
3791 Last_Extra := EF;
3792 return EF;
3793 end Add_Extra_Formal;
3795 -- Start of processing for Create_Extra_Formals
3797 begin
3798 -- If this is a derived subprogram then the subtypes of the parent
3799 -- subprogram's formal parameters will be used to to determine the need
3800 -- for extra formals.
3802 if Is_Overloadable (E) and then Present (Alias (E)) then
3803 P_Formal := First_Formal (Alias (E));
3804 end if;
3806 Last_Extra := Empty;
3807 Formal := First_Formal (E);
3808 while Present (Formal) loop
3809 Last_Extra := Formal;
3810 Next_Formal (Formal);
3811 end loop;
3813 -- If Extra_formals where already created, don't do it again. This
3814 -- situation may arise for subprogram types created as part of
3815 -- dispatching calls (see Expand_Dispatching_Call)
3817 if Present (Last_Extra) and then
3818 Present (Extra_Formal (Last_Extra))
3819 then
3820 return;
3821 end if;
3823 Formal := First_Formal (E);
3825 while Present (Formal) loop
3827 -- Create extra formal for supporting the attribute 'Constrained.
3828 -- The case of a private type view without discriminants also
3829 -- requires the extra formal if the underlying type has defaulted
3830 -- discriminants.
3832 if Ekind (Formal) /= E_In_Parameter then
3833 if Present (P_Formal) then
3834 Formal_Type := Etype (P_Formal);
3835 else
3836 Formal_Type := Etype (Formal);
3837 end if;
3839 -- Do not produce extra formals for Unchecked_Union parameters.
3840 -- Jump directly to the end of the loop.
3842 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
3843 goto Skip_Extra_Formal_Generation;
3844 end if;
3846 if not Has_Discriminants (Formal_Type)
3847 and then Ekind (Formal_Type) in Private_Kind
3848 and then Present (Underlying_Type (Formal_Type))
3849 then
3850 Formal_Type := Underlying_Type (Formal_Type);
3851 end if;
3853 if Has_Discriminants (Formal_Type)
3854 and then
3855 ((not Is_Constrained (Formal_Type)
3856 and then not Is_Indefinite_Subtype (Formal_Type))
3857 or else Present (Extra_Formal (Formal)))
3858 then
3859 Set_Extra_Constrained
3860 (Formal, Add_Extra_Formal (Standard_Boolean));
3861 end if;
3862 end if;
3864 -- Create extra formal for supporting accessibility checking
3866 -- This is suppressed if we specifically suppress accessibility
3867 -- checks at the pacage level for either the subprogram, or the
3868 -- package in which it resides. However, we do not suppress it
3869 -- simply if the scope has accessibility checks suppressed, since
3870 -- this could cause trouble when clients are compiled with a
3871 -- different suppression setting. The explicit checks at the
3872 -- package level are safe from this point of view.
3874 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
3875 and then not
3876 (Explicit_Suppress (E, Accessibility_Check)
3877 or else
3878 Explicit_Suppress (Scope (E), Accessibility_Check))
3879 and then
3880 (No (P_Formal)
3881 or else Present (Extra_Accessibility (P_Formal)))
3882 then
3883 -- Temporary kludge: for now we avoid creating the extra formal
3884 -- for access parameters of protected operations because of
3885 -- problem with the case of internal protected calls. ???
3887 if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
3888 and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
3889 then
3890 Set_Extra_Accessibility
3891 (Formal, Add_Extra_Formal (Standard_Natural));
3892 end if;
3893 end if;
3895 if Present (P_Formal) then
3896 Next_Formal (P_Formal);
3897 end if;
3899 -- This label is required when skipping extra formal generation for
3900 -- Unchecked_Union parameters.
3902 <<Skip_Extra_Formal_Generation>>
3904 Next_Formal (Formal);
3905 end loop;
3906 end Create_Extra_Formals;
3908 -----------------------------
3909 -- Enter_Overloaded_Entity --
3910 -----------------------------
3912 procedure Enter_Overloaded_Entity (S : Entity_Id) is
3913 E : Entity_Id := Current_Entity_In_Scope (S);
3914 C_E : Entity_Id := Current_Entity (S);
3916 begin
3917 if Present (E) then
3918 Set_Has_Homonym (E);
3919 Set_Has_Homonym (S);
3920 end if;
3922 Set_Is_Immediately_Visible (S);
3923 Set_Scope (S, Current_Scope);
3925 -- Chain new entity if front of homonym in current scope, so that
3926 -- homonyms are contiguous.
3928 if Present (E)
3929 and then E /= C_E
3930 then
3931 while Homonym (C_E) /= E loop
3932 C_E := Homonym (C_E);
3933 end loop;
3935 Set_Homonym (C_E, S);
3937 else
3938 E := C_E;
3939 Set_Current_Entity (S);
3940 end if;
3942 Set_Homonym (S, E);
3944 Append_Entity (S, Current_Scope);
3945 Set_Public_Status (S);
3947 if Debug_Flag_E then
3948 Write_Str ("New overloaded entity chain: ");
3949 Write_Name (Chars (S));
3951 E := S;
3952 while Present (E) loop
3953 Write_Str (" "); Write_Int (Int (E));
3954 E := Homonym (E);
3955 end loop;
3957 Write_Eol;
3958 end if;
3960 -- Generate warning for hiding
3962 if Warn_On_Hiding
3963 and then Comes_From_Source (S)
3964 and then In_Extended_Main_Source_Unit (S)
3965 then
3966 E := S;
3967 loop
3968 E := Homonym (E);
3969 exit when No (E);
3971 -- Warn unless genuine overloading
3973 if (not Is_Overloadable (E))
3974 or else Subtype_Conformant (E, S)
3975 then
3976 Error_Msg_Sloc := Sloc (E);
3977 Error_Msg_N ("declaration of & hides one#?", S);
3978 end if;
3979 end loop;
3980 end if;
3981 end Enter_Overloaded_Entity;
3983 -----------------------------
3984 -- Find_Corresponding_Spec --
3985 -----------------------------
3987 function Find_Corresponding_Spec (N : Node_Id) return Entity_Id is
3988 Spec : constant Node_Id := Specification (N);
3989 Designator : constant Entity_Id := Defining_Entity (Spec);
3991 E : Entity_Id;
3993 begin
3994 E := Current_Entity (Designator);
3996 while Present (E) loop
3998 -- We are looking for a matching spec. It must have the same scope,
3999 -- and the same name, and either be type conformant, or be the case
4000 -- of a library procedure spec and its body (which belong to one
4001 -- another regardless of whether they are type conformant or not).
4003 if Scope (E) = Current_Scope then
4004 if Current_Scope = Standard_Standard
4005 or else (Ekind (E) = Ekind (Designator)
4006 and then Type_Conformant (E, Designator))
4007 then
4008 -- Within an instantiation, we know that spec and body are
4009 -- subtype conformant, because they were subtype conformant
4010 -- in the generic. We choose the subtype-conformant entity
4011 -- here as well, to resolve spurious ambiguities in the
4012 -- instance that were not present in the generic (i.e. when
4013 -- two different types are given the same actual). If we are
4014 -- looking for a spec to match a body, full conformance is
4015 -- expected.
4017 if In_Instance then
4018 Set_Convention (Designator, Convention (E));
4020 if Nkind (N) = N_Subprogram_Body
4021 and then Present (Homonym (E))
4022 and then not Fully_Conformant (E, Designator)
4023 then
4024 goto Next_Entity;
4026 elsif not Subtype_Conformant (E, Designator) then
4027 goto Next_Entity;
4028 end if;
4029 end if;
4031 if not Has_Completion (E) then
4033 if Nkind (N) /= N_Subprogram_Body_Stub then
4034 Set_Corresponding_Spec (N, E);
4035 end if;
4037 Set_Has_Completion (E);
4038 return E;
4040 elsif Nkind (Parent (N)) = N_Subunit then
4042 -- If this is the proper body of a subunit, the completion
4043 -- flag is set when analyzing the stub.
4045 return E;
4047 -- If body already exists, this is an error unless the
4048 -- previous declaration is the implicit declaration of
4049 -- a derived subprogram, or this is a spurious overloading
4050 -- in an instance.
4052 elsif No (Alias (E))
4053 and then not Is_Intrinsic_Subprogram (E)
4054 and then not In_Instance
4055 then
4056 Error_Msg_Sloc := Sloc (E);
4057 if Is_Imported (E) then
4058 Error_Msg_NE
4059 ("body not allowed for imported subprogram & declared#",
4060 N, E);
4061 else
4062 Error_Msg_NE ("duplicate body for & declared#", N, E);
4063 end if;
4064 end if;
4066 elsif Is_Child_Unit (E)
4067 and then
4068 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
4069 and then
4070 Nkind (Parent (Unit_Declaration_Node (Designator)))
4071 = N_Compilation_Unit
4072 then
4074 -- Child units cannot be overloaded, so a conformance mismatch
4075 -- between body and a previous spec is an error.
4077 Error_Msg_N
4078 ("body of child unit does not match previous declaration", N);
4079 end if;
4080 end if;
4082 <<Next_Entity>>
4083 E := Homonym (E);
4084 end loop;
4086 -- On exit, we know that no previous declaration of subprogram exists
4088 return Empty;
4089 end Find_Corresponding_Spec;
4091 ----------------------
4092 -- Fully_Conformant --
4093 ----------------------
4095 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
4096 Result : Boolean;
4097 begin
4098 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
4099 return Result;
4100 end Fully_Conformant;
4102 ----------------------------------
4103 -- Fully_Conformant_Expressions --
4104 ----------------------------------
4106 function Fully_Conformant_Expressions
4107 (Given_E1 : Node_Id;
4108 Given_E2 : Node_Id) return Boolean
4110 E1 : constant Node_Id := Original_Node (Given_E1);
4111 E2 : constant Node_Id := Original_Node (Given_E2);
4112 -- We always test conformance on original nodes, since it is possible
4113 -- for analysis and/or expansion to make things look as though they
4114 -- conform when they do not, e.g. by converting 1+2 into 3.
4116 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
4117 renames Fully_Conformant_Expressions;
4119 function FCL (L1, L2 : List_Id) return Boolean;
4120 -- Compare elements of two lists for conformance. Elements have to
4121 -- be conformant, and actuals inserted as default parameters do not
4122 -- match explicit actuals with the same value.
4124 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
4125 -- Compare an operator node with a function call
4127 ---------
4128 -- FCL --
4129 ---------
4131 function FCL (L1, L2 : List_Id) return Boolean is
4132 N1, N2 : Node_Id;
4134 begin
4135 if L1 = No_List then
4136 N1 := Empty;
4137 else
4138 N1 := First (L1);
4139 end if;
4141 if L2 = No_List then
4142 N2 := Empty;
4143 else
4144 N2 := First (L2);
4145 end if;
4147 -- Compare two lists, skipping rewrite insertions (we want to
4148 -- compare the original trees, not the expanded versions!)
4150 loop
4151 if Is_Rewrite_Insertion (N1) then
4152 Next (N1);
4153 elsif Is_Rewrite_Insertion (N2) then
4154 Next (N2);
4155 elsif No (N1) then
4156 return No (N2);
4157 elsif No (N2) then
4158 return False;
4159 elsif not FCE (N1, N2) then
4160 return False;
4161 else
4162 Next (N1);
4163 Next (N2);
4164 end if;
4165 end loop;
4166 end FCL;
4168 ---------
4169 -- FCO --
4170 ---------
4172 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
4173 Actuals : constant List_Id := Parameter_Associations (Call_Node);
4174 Act : Node_Id;
4176 begin
4177 if No (Actuals)
4178 or else Entity (Op_Node) /= Entity (Name (Call_Node))
4179 then
4180 return False;
4182 else
4183 Act := First (Actuals);
4185 if Nkind (Op_Node) in N_Binary_Op then
4187 if not FCE (Left_Opnd (Op_Node), Act) then
4188 return False;
4189 end if;
4191 Next (Act);
4192 end if;
4194 return Present (Act)
4195 and then FCE (Right_Opnd (Op_Node), Act)
4196 and then No (Next (Act));
4197 end if;
4198 end FCO;
4200 -- Start of processing for Fully_Conformant_Expressions
4202 begin
4203 -- Non-conformant if paren count does not match. Note: if some idiot
4204 -- complains that we don't do this right for more than 3 levels of
4205 -- parentheses, they will be treated with the respect they deserve :-)
4207 if Paren_Count (E1) /= Paren_Count (E2) then
4208 return False;
4210 -- If same entities are referenced, then they are conformant even if
4211 -- they have different forms (RM 8.3.1(19-20)).
4213 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
4214 if Present (Entity (E1)) then
4215 return Entity (E1) = Entity (E2)
4216 or else (Chars (Entity (E1)) = Chars (Entity (E2))
4217 and then Ekind (Entity (E1)) = E_Discriminant
4218 and then Ekind (Entity (E2)) = E_In_Parameter);
4220 elsif Nkind (E1) = N_Expanded_Name
4221 and then Nkind (E2) = N_Expanded_Name
4222 and then Nkind (Selector_Name (E1)) = N_Character_Literal
4223 and then Nkind (Selector_Name (E2)) = N_Character_Literal
4224 then
4225 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
4227 else
4228 -- Identifiers in component associations don't always have
4229 -- entities, but their names must conform.
4231 return Nkind (E1) = N_Identifier
4232 and then Nkind (E2) = N_Identifier
4233 and then Chars (E1) = Chars (E2);
4234 end if;
4236 elsif Nkind (E1) = N_Character_Literal
4237 and then Nkind (E2) = N_Expanded_Name
4238 then
4239 return Nkind (Selector_Name (E2)) = N_Character_Literal
4240 and then Chars (E1) = Chars (Selector_Name (E2));
4242 elsif Nkind (E2) = N_Character_Literal
4243 and then Nkind (E1) = N_Expanded_Name
4244 then
4245 return Nkind (Selector_Name (E1)) = N_Character_Literal
4246 and then Chars (E2) = Chars (Selector_Name (E1));
4248 elsif Nkind (E1) in N_Op
4249 and then Nkind (E2) = N_Function_Call
4250 then
4251 return FCO (E1, E2);
4253 elsif Nkind (E2) in N_Op
4254 and then Nkind (E1) = N_Function_Call
4255 then
4256 return FCO (E2, E1);
4258 -- Otherwise we must have the same syntactic entity
4260 elsif Nkind (E1) /= Nkind (E2) then
4261 return False;
4263 -- At this point, we specialize by node type
4265 else
4266 case Nkind (E1) is
4268 when N_Aggregate =>
4269 return
4270 FCL (Expressions (E1), Expressions (E2))
4271 and then FCL (Component_Associations (E1),
4272 Component_Associations (E2));
4274 when N_Allocator =>
4275 if Nkind (Expression (E1)) = N_Qualified_Expression
4276 or else
4277 Nkind (Expression (E2)) = N_Qualified_Expression
4278 then
4279 return FCE (Expression (E1), Expression (E2));
4281 -- Check that the subtype marks and any constraints
4282 -- are conformant
4284 else
4285 declare
4286 Indic1 : constant Node_Id := Expression (E1);
4287 Indic2 : constant Node_Id := Expression (E2);
4288 Elt1 : Node_Id;
4289 Elt2 : Node_Id;
4291 begin
4292 if Nkind (Indic1) /= N_Subtype_Indication then
4293 return
4294 Nkind (Indic2) /= N_Subtype_Indication
4295 and then Entity (Indic1) = Entity (Indic2);
4297 elsif Nkind (Indic2) /= N_Subtype_Indication then
4298 return
4299 Nkind (Indic1) /= N_Subtype_Indication
4300 and then Entity (Indic1) = Entity (Indic2);
4302 else
4303 if Entity (Subtype_Mark (Indic1)) /=
4304 Entity (Subtype_Mark (Indic2))
4305 then
4306 return False;
4307 end if;
4309 Elt1 := First (Constraints (Constraint (Indic1)));
4310 Elt2 := First (Constraints (Constraint (Indic2)));
4312 while Present (Elt1) and then Present (Elt2) loop
4313 if not FCE (Elt1, Elt2) then
4314 return False;
4315 end if;
4317 Next (Elt1);
4318 Next (Elt2);
4319 end loop;
4321 return True;
4322 end if;
4323 end;
4324 end if;
4326 when N_Attribute_Reference =>
4327 return
4328 Attribute_Name (E1) = Attribute_Name (E2)
4329 and then FCL (Expressions (E1), Expressions (E2));
4331 when N_Binary_Op =>
4332 return
4333 Entity (E1) = Entity (E2)
4334 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
4335 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
4337 when N_And_Then | N_Or_Else | N_In | N_Not_In =>
4338 return
4339 FCE (Left_Opnd (E1), Left_Opnd (E2))
4340 and then
4341 FCE (Right_Opnd (E1), Right_Opnd (E2));
4343 when N_Character_Literal =>
4344 return
4345 Char_Literal_Value (E1) = Char_Literal_Value (E2);
4347 when N_Component_Association =>
4348 return
4349 FCL (Choices (E1), Choices (E2))
4350 and then FCE (Expression (E1), Expression (E2));
4352 when N_Conditional_Expression =>
4353 return
4354 FCL (Expressions (E1), Expressions (E2));
4356 when N_Explicit_Dereference =>
4357 return
4358 FCE (Prefix (E1), Prefix (E2));
4360 when N_Extension_Aggregate =>
4361 return
4362 FCL (Expressions (E1), Expressions (E2))
4363 and then Null_Record_Present (E1) =
4364 Null_Record_Present (E2)
4365 and then FCL (Component_Associations (E1),
4366 Component_Associations (E2));
4368 when N_Function_Call =>
4369 return
4370 FCE (Name (E1), Name (E2))
4371 and then FCL (Parameter_Associations (E1),
4372 Parameter_Associations (E2));
4374 when N_Indexed_Component =>
4375 return
4376 FCE (Prefix (E1), Prefix (E2))
4377 and then FCL (Expressions (E1), Expressions (E2));
4379 when N_Integer_Literal =>
4380 return (Intval (E1) = Intval (E2));
4382 when N_Null =>
4383 return True;
4385 when N_Operator_Symbol =>
4386 return
4387 Chars (E1) = Chars (E2);
4389 when N_Others_Choice =>
4390 return True;
4392 when N_Parameter_Association =>
4393 return
4394 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
4395 and then FCE (Explicit_Actual_Parameter (E1),
4396 Explicit_Actual_Parameter (E2));
4398 when N_Qualified_Expression =>
4399 return
4400 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4401 and then FCE (Expression (E1), Expression (E2));
4403 when N_Range =>
4404 return
4405 FCE (Low_Bound (E1), Low_Bound (E2))
4406 and then FCE (High_Bound (E1), High_Bound (E2));
4408 when N_Real_Literal =>
4409 return (Realval (E1) = Realval (E2));
4411 when N_Selected_Component =>
4412 return
4413 FCE (Prefix (E1), Prefix (E2))
4414 and then FCE (Selector_Name (E1), Selector_Name (E2));
4416 when N_Slice =>
4417 return
4418 FCE (Prefix (E1), Prefix (E2))
4419 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
4421 when N_String_Literal =>
4422 declare
4423 S1 : constant String_Id := Strval (E1);
4424 S2 : constant String_Id := Strval (E2);
4425 L1 : constant Nat := String_Length (S1);
4426 L2 : constant Nat := String_Length (S2);
4428 begin
4429 if L1 /= L2 then
4430 return False;
4432 else
4433 for J in 1 .. L1 loop
4434 if Get_String_Char (S1, J) /=
4435 Get_String_Char (S2, J)
4436 then
4437 return False;
4438 end if;
4439 end loop;
4441 return True;
4442 end if;
4443 end;
4445 when N_Type_Conversion =>
4446 return
4447 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4448 and then FCE (Expression (E1), Expression (E2));
4450 when N_Unary_Op =>
4451 return
4452 Entity (E1) = Entity (E2)
4453 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
4455 when N_Unchecked_Type_Conversion =>
4456 return
4457 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4458 and then FCE (Expression (E1), Expression (E2));
4460 -- All other node types cannot appear in this context. Strictly
4461 -- we should raise a fatal internal error. Instead we just ignore
4462 -- the nodes. This means that if anyone makes a mistake in the
4463 -- expander and mucks an expression tree irretrievably, the
4464 -- result will be a failure to detect a (probably very obscure)
4465 -- case of non-conformance, which is better than bombing on some
4466 -- case where two expressions do in fact conform.
4468 when others =>
4469 return True;
4471 end case;
4472 end if;
4473 end Fully_Conformant_Expressions;
4475 ----------------------------------------
4476 -- Fully_Conformant_Discrete_Subtypes --
4477 ----------------------------------------
4479 function Fully_Conformant_Discrete_Subtypes
4480 (Given_S1 : Node_Id;
4481 Given_S2 : Node_Id) return Boolean
4483 S1 : constant Node_Id := Original_Node (Given_S1);
4484 S2 : constant Node_Id := Original_Node (Given_S2);
4486 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
4487 -- Special-case for a bound given by a discriminant, which in the body
4488 -- is replaced with the discriminal of the enclosing type.
4490 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
4491 -- Check both bounds
4493 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
4494 begin
4495 if Is_Entity_Name (B1)
4496 and then Is_Entity_Name (B2)
4497 and then Ekind (Entity (B1)) = E_Discriminant
4498 then
4499 return Chars (B1) = Chars (B2);
4501 else
4502 return Fully_Conformant_Expressions (B1, B2);
4503 end if;
4504 end Conforming_Bounds;
4506 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
4507 begin
4508 return
4509 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
4510 and then
4511 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
4512 end Conforming_Ranges;
4514 -- Start of processing for Fully_Conformant_Discrete_Subtypes
4516 begin
4517 if Nkind (S1) /= Nkind (S2) then
4518 return False;
4520 elsif Is_Entity_Name (S1) then
4521 return Entity (S1) = Entity (S2);
4523 elsif Nkind (S1) = N_Range then
4524 return Conforming_Ranges (S1, S2);
4526 elsif Nkind (S1) = N_Subtype_Indication then
4527 return
4528 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
4529 and then
4530 Conforming_Ranges
4531 (Range_Expression (Constraint (S1)),
4532 Range_Expression (Constraint (S2)));
4533 else
4534 return True;
4535 end if;
4536 end Fully_Conformant_Discrete_Subtypes;
4538 --------------------
4539 -- Install_Entity --
4540 --------------------
4542 procedure Install_Entity (E : Entity_Id) is
4543 Prev : constant Entity_Id := Current_Entity (E);
4544 begin
4545 Set_Is_Immediately_Visible (E);
4546 Set_Current_Entity (E);
4547 Set_Homonym (E, Prev);
4548 end Install_Entity;
4550 ---------------------
4551 -- Install_Formals --
4552 ---------------------
4554 procedure Install_Formals (Id : Entity_Id) is
4555 F : Entity_Id;
4556 begin
4557 F := First_Formal (Id);
4558 while Present (F) loop
4559 Install_Entity (F);
4560 Next_Formal (F);
4561 end loop;
4562 end Install_Formals;
4564 ---------------------------------
4565 -- Is_Non_Overriding_Operation --
4566 ---------------------------------
4568 function Is_Non_Overriding_Operation
4569 (Prev_E : Entity_Id;
4570 New_E : Entity_Id) return Boolean
4572 Formal : Entity_Id;
4573 F_Typ : Entity_Id;
4574 G_Typ : Entity_Id := Empty;
4576 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
4577 -- If F_Type is a derived type associated with a generic actual
4578 -- subtype, then return its Generic_Parent_Type attribute, else return
4579 -- Empty.
4581 function Types_Correspond
4582 (P_Type : Entity_Id;
4583 N_Type : Entity_Id) return Boolean;
4584 -- Returns true if and only if the types (or designated types in the
4585 -- case of anonymous access types) are the same or N_Type is derived
4586 -- directly or indirectly from P_Type.
4588 -----------------------------
4589 -- Get_Generic_Parent_Type --
4590 -----------------------------
4592 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
4593 G_Typ : Entity_Id;
4594 Indic : Node_Id;
4596 begin
4597 if Is_Derived_Type (F_Typ)
4598 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
4599 then
4600 -- The tree must be traversed to determine the parent subtype in
4601 -- the generic unit, which unfortunately isn't always available
4602 -- via semantic attributes. ??? (Note: The use of Original_Node
4603 -- is needed for cases where a full derived type has been
4604 -- rewritten.)
4606 Indic := Subtype_Indication
4607 (Type_Definition (Original_Node (Parent (F_Typ))));
4609 if Nkind (Indic) = N_Subtype_Indication then
4610 G_Typ := Entity (Subtype_Mark (Indic));
4611 else
4612 G_Typ := Entity (Indic);
4613 end if;
4615 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
4616 and then Present (Generic_Parent_Type (Parent (G_Typ)))
4617 then
4618 return Generic_Parent_Type (Parent (G_Typ));
4619 end if;
4620 end if;
4622 return Empty;
4623 end Get_Generic_Parent_Type;
4625 ----------------------
4626 -- Types_Correspond --
4627 ----------------------
4629 function Types_Correspond
4630 (P_Type : Entity_Id;
4631 N_Type : Entity_Id) return Boolean
4633 Prev_Type : Entity_Id := Base_Type (P_Type);
4634 New_Type : Entity_Id := Base_Type (N_Type);
4636 begin
4637 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
4638 Prev_Type := Designated_Type (Prev_Type);
4639 end if;
4641 if Ekind (New_Type) = E_Anonymous_Access_Type then
4642 New_Type := Designated_Type (New_Type);
4643 end if;
4645 if Prev_Type = New_Type then
4646 return True;
4648 elsif not Is_Class_Wide_Type (New_Type) then
4649 while Etype (New_Type) /= New_Type loop
4650 New_Type := Etype (New_Type);
4651 if New_Type = Prev_Type then
4652 return True;
4653 end if;
4654 end loop;
4655 end if;
4656 return False;
4657 end Types_Correspond;
4659 -- Start of processing for Is_Non_Overriding_Operation
4661 begin
4662 -- In the case where both operations are implicit derived subprograms
4663 -- then neither overrides the other. This can only occur in certain
4664 -- obscure cases (e.g., derivation from homographs created in a generic
4665 -- instantiation).
4667 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
4668 return True;
4670 elsif Ekind (Current_Scope) = E_Package
4671 and then Is_Generic_Instance (Current_Scope)
4672 and then In_Private_Part (Current_Scope)
4673 and then Comes_From_Source (New_E)
4674 then
4675 -- We examine the formals and result subtype of the inherited
4676 -- operation, to determine whether their type is derived from (the
4677 -- instance of) a generic type.
4679 Formal := First_Formal (Prev_E);
4681 while Present (Formal) loop
4682 F_Typ := Base_Type (Etype (Formal));
4684 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4685 F_Typ := Designated_Type (F_Typ);
4686 end if;
4688 G_Typ := Get_Generic_Parent_Type (F_Typ);
4690 Next_Formal (Formal);
4691 end loop;
4693 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
4694 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
4695 end if;
4697 if No (G_Typ) then
4698 return False;
4699 end if;
4701 -- If the generic type is a private type, then the original
4702 -- operation was not overriding in the generic, because there was
4703 -- no primitive operation to override.
4705 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
4706 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
4707 N_Formal_Private_Type_Definition
4708 then
4709 return True;
4711 -- The generic parent type is the ancestor of a formal derived
4712 -- type declaration. We need to check whether it has a primitive
4713 -- operation that should be overridden by New_E in the generic.
4715 else
4716 declare
4717 P_Formal : Entity_Id;
4718 N_Formal : Entity_Id;
4719 P_Typ : Entity_Id;
4720 N_Typ : Entity_Id;
4721 P_Prim : Entity_Id;
4722 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
4724 begin
4725 while Present (Prim_Elt) loop
4726 P_Prim := Node (Prim_Elt);
4728 if Chars (P_Prim) = Chars (New_E)
4729 and then Ekind (P_Prim) = Ekind (New_E)
4730 then
4731 P_Formal := First_Formal (P_Prim);
4732 N_Formal := First_Formal (New_E);
4733 while Present (P_Formal) and then Present (N_Formal) loop
4734 P_Typ := Etype (P_Formal);
4735 N_Typ := Etype (N_Formal);
4737 if not Types_Correspond (P_Typ, N_Typ) then
4738 exit;
4739 end if;
4741 Next_Entity (P_Formal);
4742 Next_Entity (N_Formal);
4743 end loop;
4745 -- Found a matching primitive operation belonging to the
4746 -- formal ancestor type, so the new subprogram is
4747 -- overriding.
4749 if No (P_Formal)
4750 and then No (N_Formal)
4751 and then (Ekind (New_E) /= E_Function
4752 or else
4753 Types_Correspond
4754 (Etype (P_Prim), Etype (New_E)))
4755 then
4756 return False;
4757 end if;
4758 end if;
4760 Next_Elmt (Prim_Elt);
4761 end loop;
4763 -- If no match found, then the new subprogram does not
4764 -- override in the generic (nor in the instance).
4766 return True;
4767 end;
4768 end if;
4769 else
4770 return False;
4771 end if;
4772 end Is_Non_Overriding_Operation;
4774 ------------------------------
4775 -- Make_Inequality_Operator --
4776 ------------------------------
4778 -- S is the defining identifier of an equality operator. We build a
4779 -- subprogram declaration with the right signature. This operation is
4780 -- intrinsic, because it is always expanded as the negation of the
4781 -- call to the equality function.
4783 procedure Make_Inequality_Operator (S : Entity_Id) is
4784 Loc : constant Source_Ptr := Sloc (S);
4785 Decl : Node_Id;
4786 Formals : List_Id;
4787 Op_Name : Entity_Id;
4789 FF : constant Entity_Id := First_Formal (S);
4790 NF : constant Entity_Id := Next_Formal (FF);
4792 begin
4793 -- Check that equality was properly defined, ignore call if not
4795 if No (NF) then
4796 return;
4797 end if;
4799 declare
4800 A : constant Entity_Id :=
4801 Make_Defining_Identifier (Sloc (FF),
4802 Chars => Chars (FF));
4804 B : constant Entity_Id :=
4805 Make_Defining_Identifier (Sloc (NF),
4806 Chars => Chars (NF));
4808 begin
4809 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
4811 Formals := New_List (
4812 Make_Parameter_Specification (Loc,
4813 Defining_Identifier => A,
4814 Parameter_Type =>
4815 New_Reference_To (Etype (First_Formal (S)),
4816 Sloc (Etype (First_Formal (S))))),
4818 Make_Parameter_Specification (Loc,
4819 Defining_Identifier => B,
4820 Parameter_Type =>
4821 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
4822 Sloc (Etype (Next_Formal (First_Formal (S)))))));
4824 Decl :=
4825 Make_Subprogram_Declaration (Loc,
4826 Specification =>
4827 Make_Function_Specification (Loc,
4828 Defining_Unit_Name => Op_Name,
4829 Parameter_Specifications => Formals,
4830 Result_Definition =>
4831 New_Reference_To (Standard_Boolean, Loc)));
4833 -- Insert inequality right after equality if it is explicit or after
4834 -- the derived type when implicit. These entities are created only
4835 -- for visibility purposes, and eventually replaced in the course of
4836 -- expansion, so they do not need to be attached to the tree and seen
4837 -- by the back-end. Keeping them internal also avoids spurious
4838 -- freezing problems. The declaration is inserted in the tree for
4839 -- analysis, and removed afterwards. If the equality operator comes
4840 -- from an explicit declaration, attach the inequality immediately
4841 -- after. Else the equality is inherited from a derived type
4842 -- declaration, so insert inequality after that declaration.
4844 if No (Alias (S)) then
4845 Insert_After (Unit_Declaration_Node (S), Decl);
4846 elsif Is_List_Member (Parent (S)) then
4847 Insert_After (Parent (S), Decl);
4848 else
4849 Insert_After (Parent (Etype (First_Formal (S))), Decl);
4850 end if;
4852 Mark_Rewrite_Insertion (Decl);
4853 Set_Is_Intrinsic_Subprogram (Op_Name);
4854 Analyze (Decl);
4855 Remove (Decl);
4856 Set_Has_Completion (Op_Name);
4857 Set_Corresponding_Equality (Op_Name, S);
4858 Set_Is_Abstract (Op_Name, Is_Abstract (S));
4859 end;
4860 end Make_Inequality_Operator;
4862 ----------------------
4863 -- May_Need_Actuals --
4864 ----------------------
4866 procedure May_Need_Actuals (Fun : Entity_Id) is
4867 F : Entity_Id;
4868 B : Boolean;
4870 begin
4871 F := First_Formal (Fun);
4872 B := True;
4874 while Present (F) loop
4875 if No (Default_Value (F)) then
4876 B := False;
4877 exit;
4878 end if;
4880 Next_Formal (F);
4881 end loop;
4883 Set_Needs_No_Actuals (Fun, B);
4884 end May_Need_Actuals;
4886 ---------------------
4887 -- Mode_Conformant --
4888 ---------------------
4890 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
4891 Result : Boolean;
4892 begin
4893 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
4894 return Result;
4895 end Mode_Conformant;
4897 ---------------------------
4898 -- New_Overloaded_Entity --
4899 ---------------------------
4901 procedure New_Overloaded_Entity
4902 (S : Entity_Id;
4903 Derived_Type : Entity_Id := Empty)
4905 Does_Override : Boolean := False;
4906 -- Set if the current scope has an operation that is type-conformant
4907 -- with S, and becomes hidden by S.
4909 E : Entity_Id;
4910 -- Entity that S overrides
4912 Prev_Vis : Entity_Id := Empty;
4913 -- Needs comment ???
4915 Is_Alias_Interface : Boolean := False;
4917 function Is_Private_Declaration (E : Entity_Id) return Boolean;
4918 -- Check that E is declared in the private part of the current package,
4919 -- or in the package body, where it may hide a previous declaration.
4920 -- We can't use In_Private_Part by itself because this flag is also
4921 -- set when freezing entities, so we must examine the place of the
4922 -- declaration in the tree, and recognize wrapper packages as well.
4924 procedure Maybe_Primitive_Operation (Is_Overriding : Boolean := False);
4925 -- If the subprogram being analyzed is a primitive operation of
4926 -- the type of one of its formals, set the corresponding flag.
4928 ----------------------------
4929 -- Is_Private_Declaration --
4930 ----------------------------
4932 function Is_Private_Declaration (E : Entity_Id) return Boolean is
4933 Priv_Decls : List_Id;
4934 Decl : constant Node_Id := Unit_Declaration_Node (E);
4936 begin
4937 if Is_Package_Or_Generic_Package (Current_Scope)
4938 and then In_Private_Part (Current_Scope)
4939 then
4940 Priv_Decls :=
4941 Private_Declarations (
4942 Specification (Unit_Declaration_Node (Current_Scope)));
4944 return In_Package_Body (Current_Scope)
4945 or else
4946 (Is_List_Member (Decl)
4947 and then List_Containing (Decl) = Priv_Decls)
4948 or else (Nkind (Parent (Decl)) = N_Package_Specification
4949 and then not Is_Compilation_Unit (
4950 Defining_Entity (Parent (Decl)))
4951 and then List_Containing (Parent (Parent (Decl)))
4952 = Priv_Decls);
4953 else
4954 return False;
4955 end if;
4956 end Is_Private_Declaration;
4958 -------------------------------
4959 -- Maybe_Primitive_Operation --
4960 -------------------------------
4962 procedure Maybe_Primitive_Operation (Is_Overriding : Boolean := False) is
4963 Formal : Entity_Id;
4964 F_Typ : Entity_Id;
4965 B_Typ : Entity_Id;
4967 function Visible_Part_Type (T : Entity_Id) return Boolean;
4968 -- Returns true if T is declared in the visible part of
4969 -- the current package scope; otherwise returns false.
4970 -- Assumes that T is declared in a package.
4972 procedure Check_Private_Overriding (T : Entity_Id);
4973 -- Checks that if a primitive abstract subprogram of a visible
4974 -- abstract type is declared in a private part, then it must
4975 -- override an abstract subprogram declared in the visible part.
4976 -- Also checks that if a primitive function with a controlling
4977 -- result is declared in a private part, then it must override
4978 -- a function declared in the visible part.
4980 ------------------------------
4981 -- Check_Private_Overriding --
4982 ------------------------------
4984 procedure Check_Private_Overriding (T : Entity_Id) is
4985 begin
4986 if Ekind (Current_Scope) = E_Package
4987 and then In_Private_Part (Current_Scope)
4988 and then Visible_Part_Type (T)
4989 and then not In_Instance
4990 then
4991 if Is_Abstract (T)
4992 and then Is_Abstract (S)
4993 and then (not Is_Overriding or else not Is_Abstract (E))
4994 then
4995 if not Is_Interface (T) then
4996 Error_Msg_N ("abstract subprograms must be visible "
4997 & "('R'M 3.9.3(10))!", S);
4999 -- Ada 2005 (AI-251)
5001 else
5002 Error_Msg_N ("primitive subprograms of interface types "
5003 & "declared in a visible part, must be declared in "
5004 & "the visible part ('R'M 3.9.4)!", S);
5005 end if;
5007 elsif Ekind (S) = E_Function
5008 and then Is_Tagged_Type (T)
5009 and then T = Base_Type (Etype (S))
5010 and then not Is_Overriding
5011 then
5012 Error_Msg_N
5013 ("private function with tagged result must"
5014 & " override visible-part function", S);
5015 Error_Msg_N
5016 ("\move subprogram to the visible part"
5017 & " ('R'M 3.9.3(10))", S);
5018 end if;
5019 end if;
5020 end Check_Private_Overriding;
5022 -----------------------
5023 -- Visible_Part_Type --
5024 -----------------------
5026 function Visible_Part_Type (T : Entity_Id) return Boolean is
5027 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
5028 N : Node_Id;
5030 begin
5031 -- If the entity is a private type, then it must be
5032 -- declared in a visible part.
5034 if Ekind (T) in Private_Kind then
5035 return True;
5036 end if;
5038 -- Otherwise, we traverse the visible part looking for its
5039 -- corresponding declaration. We cannot use the declaration
5040 -- node directly because in the private part the entity of a
5041 -- private type is the one in the full view, which does not
5042 -- indicate that it is the completion of something visible.
5044 N := First (Visible_Declarations (Specification (P)));
5045 while Present (N) loop
5046 if Nkind (N) = N_Full_Type_Declaration
5047 and then Present (Defining_Identifier (N))
5048 and then T = Defining_Identifier (N)
5049 then
5050 return True;
5052 elsif (Nkind (N) = N_Private_Type_Declaration
5053 or else
5054 Nkind (N) = N_Private_Extension_Declaration)
5055 and then Present (Defining_Identifier (N))
5056 and then T = Full_View (Defining_Identifier (N))
5057 then
5058 return True;
5059 end if;
5061 Next (N);
5062 end loop;
5064 return False;
5065 end Visible_Part_Type;
5067 -- Start of processing for Maybe_Primitive_Operation
5069 begin
5070 if not Comes_From_Source (S) then
5071 null;
5073 -- If the subprogram is at library level, it is not primitive
5074 -- operation.
5076 elsif Current_Scope = Standard_Standard then
5077 null;
5079 elsif (Ekind (Current_Scope) = E_Package
5080 and then not In_Package_Body (Current_Scope))
5081 or else Is_Overriding
5082 then
5083 -- For function, check return type
5085 if Ekind (S) = E_Function then
5086 B_Typ := Base_Type (Etype (S));
5088 if Scope (B_Typ) = Current_Scope then
5089 Set_Has_Primitive_Operations (B_Typ);
5090 Check_Private_Overriding (B_Typ);
5091 end if;
5092 end if;
5094 -- For all subprograms, check formals
5096 Formal := First_Formal (S);
5097 while Present (Formal) loop
5098 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
5099 F_Typ := Designated_Type (Etype (Formal));
5100 else
5101 F_Typ := Etype (Formal);
5102 end if;
5104 B_Typ := Base_Type (F_Typ);
5106 if Scope (B_Typ) = Current_Scope then
5107 Set_Has_Primitive_Operations (B_Typ);
5108 Check_Private_Overriding (B_Typ);
5109 end if;
5111 Next_Formal (Formal);
5112 end loop;
5113 end if;
5114 end Maybe_Primitive_Operation;
5116 -- Start of processing for New_Overloaded_Entity
5118 begin
5119 -- We need to look for an entity that S may override. This must be a
5120 -- homonym in the current scope, so we look for the first homonym of
5121 -- S in the current scope as the starting point for the search.
5123 E := Current_Entity_In_Scope (S);
5125 -- If there is no homonym then this is definitely not overriding
5127 if No (E) then
5128 Enter_Overloaded_Entity (S);
5129 Check_Dispatching_Operation (S, Empty);
5130 Maybe_Primitive_Operation;
5132 -- Ada 2005 (AI-397): Subprograms in the context of protected
5133 -- types have their overriding indicators checked in Sem_Ch9.
5135 if Ekind (S) not in Subprogram_Kind
5136 or else Ekind (Scope (S)) /= E_Protected_Type
5137 then
5138 Check_Overriding_Indicator (S, False);
5139 end if;
5141 -- If there is a homonym that is not overloadable, then we have an
5142 -- error, except for the special cases checked explicitly below.
5144 elsif not Is_Overloadable (E) then
5146 -- Check for spurious conflict produced by a subprogram that has the
5147 -- same name as that of the enclosing generic package. The conflict
5148 -- occurs within an instance, between the subprogram and the renaming
5149 -- declaration for the package. After the subprogram, the package
5150 -- renaming declaration becomes hidden.
5152 if Ekind (E) = E_Package
5153 and then Present (Renamed_Object (E))
5154 and then Renamed_Object (E) = Current_Scope
5155 and then Nkind (Parent (Renamed_Object (E))) =
5156 N_Package_Specification
5157 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
5158 then
5159 Set_Is_Hidden (E);
5160 Set_Is_Immediately_Visible (E, False);
5161 Enter_Overloaded_Entity (S);
5162 Set_Homonym (S, Homonym (E));
5163 Check_Dispatching_Operation (S, Empty);
5164 Check_Overriding_Indicator (S, False);
5166 -- If the subprogram is implicit it is hidden by the previous
5167 -- declaration. However if it is dispatching, it must appear in the
5168 -- dispatch table anyway, because it can be dispatched to even if it
5169 -- cannot be called directly.
5171 elsif Present (Alias (S))
5172 and then not Comes_From_Source (S)
5173 then
5174 Set_Scope (S, Current_Scope);
5176 if Is_Dispatching_Operation (Alias (S)) then
5177 Check_Dispatching_Operation (S, Empty);
5178 end if;
5180 return;
5182 else
5183 Error_Msg_Sloc := Sloc (E);
5184 Error_Msg_N ("& conflicts with declaration#", S);
5186 -- Useful additional warning
5188 if Is_Generic_Unit (E) then
5189 Error_Msg_N ("\previous generic unit cannot be overloaded", S);
5190 end if;
5192 return;
5193 end if;
5195 -- E exists and is overloadable
5197 else
5198 Is_Alias_Interface :=
5199 Present (Alias (S))
5200 and then Is_Dispatching_Operation (Alias (S))
5201 and then Present (DTC_Entity (Alias (S)))
5202 and then Is_Interface (Scope (DTC_Entity (Alias (S))));
5204 -- Loop through E and its homonyms to determine if any of them is
5205 -- the candidate for overriding by S.
5207 while Present (E) loop
5209 -- Definitely not interesting if not in the current scope
5211 if Scope (E) /= Current_Scope then
5212 null;
5214 -- Check if we have type conformance
5216 -- Ada 2005 (AI-251): In case of overriding an interface
5217 -- subprogram it is not an error that the old and new entities
5218 -- have the same profile, and hence we skip this code.
5220 elsif not Is_Alias_Interface
5221 and then Type_Conformant (E, S)
5223 -- Ada 2005 (AI-251): Do not consider here entities that cover
5224 -- abstract interface primitives. They will be handled after
5225 -- the overriden entity is found (see comments bellow inside
5226 -- this subprogram).
5228 and then not (Is_Subprogram (E)
5229 and then Present (Abstract_Interface_Alias (E)))
5230 then
5231 -- If the old and new entities have the same profile and one
5232 -- is not the body of the other, then this is an error, unless
5233 -- one of them is implicitly declared.
5235 -- There are some cases when both can be implicit, for example
5236 -- when both a literal and a function that overrides it are
5237 -- inherited in a derivation, or when an inhertited operation
5238 -- of a tagged full type overrides the ineherited operation of
5239 -- a private extension. Ada 83 had a special rule for the the
5240 -- literal case. In Ada95, the later implicit operation hides
5241 -- the former, and the literal is always the former. In the
5242 -- odd case where both are derived operations declared at the
5243 -- same point, both operations should be declared, and in that
5244 -- case we bypass the following test and proceed to the next
5245 -- part (this can only occur for certain obscure cases
5246 -- involving homographs in instances and can't occur for
5247 -- dispatching operations ???). Note that the following
5248 -- condition is less than clear. For example, it's not at all
5249 -- clear why there's a test for E_Entry here. ???
5251 if Present (Alias (S))
5252 and then (No (Alias (E))
5253 or else Comes_From_Source (E)
5254 or else Is_Dispatching_Operation (E))
5255 and then
5256 (Ekind (E) = E_Entry
5257 or else Ekind (E) /= E_Enumeration_Literal)
5258 then
5259 -- When an derived operation is overloaded it may be due to
5260 -- the fact that the full view of a private extension
5261 -- re-inherits. It has to be dealt with.
5263 if Is_Package_Or_Generic_Package (Current_Scope)
5264 and then In_Private_Part (Current_Scope)
5265 then
5266 Check_Operation_From_Private_View (S, E);
5267 end if;
5269 -- In any case the implicit operation remains hidden by
5270 -- the existing declaration, which is overriding.
5272 Set_Is_Overriding_Operation (E);
5274 if Comes_From_Source (E) then
5275 Check_Overriding_Indicator (E, True);
5277 -- Indicate that E overrides the operation from which
5278 -- S is inherited.
5280 if Present (Alias (S)) then
5281 Set_Overridden_Operation (E, Alias (S));
5282 else
5283 Set_Overridden_Operation (E, S);
5284 end if;
5285 end if;
5287 return;
5289 -- Within an instance, the renaming declarations for
5290 -- actual subprograms may become ambiguous, but they do
5291 -- not hide each other.
5293 elsif Ekind (E) /= E_Entry
5294 and then not Comes_From_Source (E)
5295 and then not Is_Generic_Instance (E)
5296 and then (Present (Alias (E))
5297 or else Is_Intrinsic_Subprogram (E))
5298 and then (not In_Instance
5299 or else No (Parent (E))
5300 or else Nkind (Unit_Declaration_Node (E)) /=
5301 N_Subprogram_Renaming_Declaration)
5302 then
5303 -- A subprogram child unit is not allowed to override
5304 -- an inherited subprogram (10.1.1(20)).
5306 if Is_Child_Unit (S) then
5307 Error_Msg_N
5308 ("child unit overrides inherited subprogram in parent",
5310 return;
5311 end if;
5313 if Is_Non_Overriding_Operation (E, S) then
5314 Enter_Overloaded_Entity (S);
5315 if No (Derived_Type)
5316 or else Is_Tagged_Type (Derived_Type)
5317 then
5318 Check_Dispatching_Operation (S, Empty);
5319 end if;
5321 return;
5322 end if;
5324 -- E is a derived operation or an internal operator which
5325 -- is being overridden. Remove E from further visibility.
5326 -- Furthermore, if E is a dispatching operation, it must be
5327 -- replaced in the list of primitive operations of its type
5328 -- (see Override_Dispatching_Operation).
5330 Does_Override := True;
5332 declare
5333 Prev : Entity_Id;
5335 begin
5336 Prev := First_Entity (Current_Scope);
5338 while Present (Prev)
5339 and then Next_Entity (Prev) /= E
5340 loop
5341 Next_Entity (Prev);
5342 end loop;
5344 -- It is possible for E to be in the current scope and
5345 -- yet not in the entity chain. This can only occur in a
5346 -- generic context where E is an implicit concatenation
5347 -- in the formal part, because in a generic body the
5348 -- entity chain starts with the formals.
5350 pragma Assert
5351 (Present (Prev) or else Chars (E) = Name_Op_Concat);
5353 -- E must be removed both from the entity_list of the
5354 -- current scope, and from the visibility chain
5356 if Debug_Flag_E then
5357 Write_Str ("Override implicit operation ");
5358 Write_Int (Int (E));
5359 Write_Eol;
5360 end if;
5362 -- If E is a predefined concatenation, it stands for four
5363 -- different operations. As a result, a single explicit
5364 -- declaration does not hide it. In a possible ambiguous
5365 -- situation, Disambiguate chooses the user-defined op,
5366 -- so it is correct to retain the previous internal one.
5368 if Chars (E) /= Name_Op_Concat
5369 or else Ekind (E) /= E_Operator
5370 then
5371 -- For nondispatching derived operations that are
5372 -- overridden by a subprogram declared in the private
5373 -- part of a package, we retain the derived
5374 -- subprogram but mark it as not immediately visible.
5375 -- If the derived operation was declared in the
5376 -- visible part then this ensures that it will still
5377 -- be visible outside the package with the proper
5378 -- signature (calls from outside must also be
5379 -- directed to this version rather than the
5380 -- overriding one, unlike the dispatching case).
5381 -- Calls from inside the package will still resolve
5382 -- to the overriding subprogram since the derived one
5383 -- is marked as not visible within the package.
5385 -- If the private operation is dispatching, we achieve
5386 -- the overriding by keeping the implicit operation
5387 -- but setting its alias to be the overriding one. In
5388 -- this fashion the proper body is executed in all
5389 -- cases, but the original signature is used outside
5390 -- of the package.
5392 -- If the overriding is not in the private part, we
5393 -- remove the implicit operation altogether.
5395 if Is_Private_Declaration (S) then
5397 if not Is_Dispatching_Operation (E) then
5398 Set_Is_Immediately_Visible (E, False);
5399 else
5400 -- Work done in Override_Dispatching_Operation,
5401 -- so nothing else need to be done here.
5403 null;
5404 end if;
5406 else
5407 -- Find predecessor of E in Homonym chain
5409 if E = Current_Entity (E) then
5410 Prev_Vis := Empty;
5411 else
5412 Prev_Vis := Current_Entity (E);
5413 while Homonym (Prev_Vis) /= E loop
5414 Prev_Vis := Homonym (Prev_Vis);
5415 end loop;
5416 end if;
5418 if Prev_Vis /= Empty then
5420 -- Skip E in the visibility chain
5422 Set_Homonym (Prev_Vis, Homonym (E));
5424 else
5425 Set_Name_Entity_Id (Chars (E), Homonym (E));
5426 end if;
5428 Set_Next_Entity (Prev, Next_Entity (E));
5430 if No (Next_Entity (Prev)) then
5431 Set_Last_Entity (Current_Scope, Prev);
5432 end if;
5434 end if;
5435 end if;
5437 Enter_Overloaded_Entity (S);
5438 Set_Is_Overriding_Operation (S);
5439 Check_Overriding_Indicator (S, True);
5441 -- Indicate that S overrides the operation from which
5442 -- E is inherited.
5444 if Comes_From_Source (S) then
5445 if Present (Alias (E)) then
5446 Set_Overridden_Operation (S, Alias (E));
5447 else
5448 Set_Overridden_Operation (S, E);
5449 end if;
5450 end if;
5452 if Is_Dispatching_Operation (E) then
5454 -- An overriding dispatching subprogram inherits the
5455 -- convention of the overridden subprogram (by
5456 -- AI-117).
5458 Set_Convention (S, Convention (E));
5460 -- AI-251: For an entity overriding an interface
5461 -- primitive check if the entity also covers other
5462 -- abstract subprograms in the same scope. This is
5463 -- required to handle the general case, that is,
5464 -- 1) overriding other interface primitives, and
5465 -- 2) overriding abstract subprograms inherited from
5466 -- some abstract ancestor type.
5468 if Has_Homonym (E)
5469 and then Present (Alias (E))
5470 and then Ekind (Alias (E)) /= E_Operator
5471 and then Present (DTC_Entity (Alias (E)))
5472 and then Is_Interface (Scope (DTC_Entity
5473 (Alias (E))))
5474 then
5475 declare
5476 E1 : Entity_Id;
5478 begin
5479 E1 := Homonym (E);
5480 while Present (E1) loop
5481 if (Is_Overloadable (E1)
5482 or else Ekind (E1) = E_Subprogram_Type)
5483 and then Present (Alias (E1))
5484 and then Ekind (Alias (E1)) /= E_Operator
5485 and then Present (DTC_Entity (Alias (E1)))
5486 and then Is_Abstract
5487 (Scope (DTC_Entity (Alias (E1))))
5488 and then Type_Conformant (E1, S)
5489 then
5490 Check_Dispatching_Operation (S, E1);
5491 end if;
5493 E1 := Homonym (E1);
5494 end loop;
5495 end;
5496 end if;
5498 Check_Dispatching_Operation (S, E);
5500 -- AI-251: Handle the case in which the entity
5501 -- overrides a primitive operation that covered
5502 -- several abstract interface primitives.
5504 declare
5505 E1 : Entity_Id;
5506 begin
5507 E1 := Current_Entity_In_Scope (S);
5508 while Present (E1) loop
5509 if Is_Subprogram (E1)
5510 and then Present
5511 (Abstract_Interface_Alias (E1))
5512 and then Alias (E1) = E
5513 then
5514 Set_Alias (E1, S);
5515 end if;
5517 E1 := Homonym (E1);
5518 end loop;
5519 end;
5521 else
5522 Check_Dispatching_Operation (S, Empty);
5523 end if;
5525 Maybe_Primitive_Operation (Is_Overriding => True);
5526 goto Check_Inequality;
5527 end;
5529 -- Apparent redeclarations in instances can occur when two
5530 -- formal types get the same actual type. The subprograms in
5531 -- in the instance are legal, even if not callable from the
5532 -- outside. Calls from within are disambiguated elsewhere.
5533 -- For dispatching operations in the visible part, the usual
5534 -- rules apply, and operations with the same profile are not
5535 -- legal (B830001).
5537 elsif (In_Instance_Visible_Part
5538 and then not Is_Dispatching_Operation (E))
5539 or else In_Instance_Not_Visible
5540 then
5541 null;
5543 -- Here we have a real error (identical profile)
5545 else
5546 Error_Msg_Sloc := Sloc (E);
5548 -- Avoid cascaded errors if the entity appears in
5549 -- subsequent calls.
5551 Set_Scope (S, Current_Scope);
5553 Error_Msg_N ("& conflicts with declaration#", S);
5555 if Is_Generic_Instance (S)
5556 and then not Has_Completion (E)
5557 then
5558 Error_Msg_N
5559 ("\instantiation cannot provide body for it", S);
5560 end if;
5562 return;
5563 end if;
5565 else
5566 -- If one subprogram has an access parameter and the other
5567 -- a parameter of an access type, calls to either might be
5568 -- ambiguous. Verify that parameters match except for the
5569 -- access parameter.
5571 if May_Hide_Profile then
5572 declare
5573 F1 : Entity_Id;
5574 F2 : Entity_Id;
5575 begin
5576 F1 := First_Formal (S);
5577 F2 := First_Formal (E);
5578 while Present (F1) and then Present (F2) loop
5579 if Is_Access_Type (Etype (F1)) then
5580 if not Is_Access_Type (Etype (F2))
5581 or else not Conforming_Types
5582 (Designated_Type (Etype (F1)),
5583 Designated_Type (Etype (F2)),
5584 Type_Conformant)
5585 then
5586 May_Hide_Profile := False;
5587 end if;
5589 elsif
5590 not Conforming_Types
5591 (Etype (F1), Etype (F2), Type_Conformant)
5592 then
5593 May_Hide_Profile := False;
5594 end if;
5596 Next_Formal (F1);
5597 Next_Formal (F2);
5598 end loop;
5600 if May_Hide_Profile
5601 and then No (F1)
5602 and then No (F2)
5603 then
5604 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
5605 end if;
5606 end;
5607 end if;
5608 end if;
5610 Prev_Vis := E;
5611 E := Homonym (E);
5612 end loop;
5614 -- On exit, we know that S is a new entity
5616 Enter_Overloaded_Entity (S);
5617 Maybe_Primitive_Operation;
5618 Check_Overriding_Indicator (S, Does_Override);
5620 -- If S is a derived operation for an untagged type then by
5621 -- definition it's not a dispatching operation (even if the parent
5622 -- operation was dispatching), so we don't call
5623 -- Check_Dispatching_Operation in that case.
5625 if No (Derived_Type)
5626 or else Is_Tagged_Type (Derived_Type)
5627 then
5628 Check_Dispatching_Operation (S, Empty);
5629 end if;
5630 end if;
5632 -- If this is a user-defined equality operator that is not a derived
5633 -- subprogram, create the corresponding inequality. If the operation is
5634 -- dispatching, the expansion is done elsewhere, and we do not create
5635 -- an explicit inequality operation.
5637 <<Check_Inequality>>
5638 if Chars (S) = Name_Op_Eq
5639 and then Etype (S) = Standard_Boolean
5640 and then Present (Parent (S))
5641 and then not Is_Dispatching_Operation (S)
5642 then
5643 Make_Inequality_Operator (S);
5644 end if;
5645 end New_Overloaded_Entity;
5647 ---------------------
5648 -- Process_Formals --
5649 ---------------------
5651 procedure Process_Formals
5652 (T : List_Id;
5653 Related_Nod : Node_Id)
5655 Param_Spec : Node_Id;
5656 Formal : Entity_Id;
5657 Formal_Type : Entity_Id;
5658 Default : Node_Id;
5659 Ptype : Entity_Id;
5661 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
5662 -- Check whether the default has a class-wide type. After analysis the
5663 -- default has the type of the formal, so we must also check explicitly
5664 -- for an access attribute.
5666 ---------------------------
5667 -- Is_Class_Wide_Default --
5668 ---------------------------
5670 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
5671 begin
5672 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
5673 or else (Nkind (D) = N_Attribute_Reference
5674 and then Attribute_Name (D) = Name_Access
5675 and then Is_Class_Wide_Type (Etype (Prefix (D))));
5676 end Is_Class_Wide_Default;
5678 -- Start of processing for Process_Formals
5680 begin
5681 -- In order to prevent premature use of the formals in the same formal
5682 -- part, the Ekind is left undefined until all default expressions are
5683 -- analyzed. The Ekind is established in a separate loop at the end.
5685 Param_Spec := First (T);
5687 while Present (Param_Spec) loop
5689 Formal := Defining_Identifier (Param_Spec);
5690 Enter_Name (Formal);
5692 -- Case of ordinary parameters
5694 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
5695 Find_Type (Parameter_Type (Param_Spec));
5696 Ptype := Parameter_Type (Param_Spec);
5698 if Ptype = Error then
5699 goto Continue;
5700 end if;
5702 Formal_Type := Entity (Ptype);
5704 if Ekind (Formal_Type) = E_Incomplete_Type
5705 or else (Is_Class_Wide_Type (Formal_Type)
5706 and then Ekind (Root_Type (Formal_Type)) =
5707 E_Incomplete_Type)
5708 then
5709 -- Ada 2005 (AI-326): Tagged incomplete types allowed
5711 if Is_Tagged_Type (Formal_Type) then
5712 null;
5714 elsif Nkind (Parent (T)) /= N_Access_Function_Definition
5715 and then Nkind (Parent (T)) /= N_Access_Procedure_Definition
5716 then
5717 Error_Msg_N ("invalid use of incomplete type", Param_Spec);
5718 end if;
5720 elsif Ekind (Formal_Type) = E_Void then
5721 Error_Msg_NE ("premature use of&",
5722 Parameter_Type (Param_Spec), Formal_Type);
5723 end if;
5725 -- Ada 2005 (AI-231): Create and decorate an internal subtype
5726 -- declaration corresponding to the null-excluding type of the
5727 -- formal in the enclosing scope. Finally, replace the parameter
5728 -- type of the formal with the internal subtype.
5730 if Ada_Version >= Ada_05
5731 and then Is_Access_Type (Formal_Type)
5732 and then Null_Exclusion_Present (Param_Spec)
5733 then
5734 if Can_Never_Be_Null (Formal_Type)
5735 and then Comes_From_Source (Related_Nod)
5736 then
5737 Error_Msg_N
5738 ("null exclusion must apply to a type that does not "
5739 & "exclude null ('R'M 3.10 (14)", Related_Nod);
5740 end if;
5742 Formal_Type :=
5743 Create_Null_Excluding_Itype
5744 (T => Formal_Type,
5745 Related_Nod => Related_Nod,
5746 Scope_Id => Scope (Current_Scope));
5747 end if;
5749 -- An access formal type
5751 else
5752 Formal_Type :=
5753 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
5755 -- Ada 2005 (AI-254)
5757 declare
5758 AD : constant Node_Id :=
5759 Access_To_Subprogram_Definition
5760 (Parameter_Type (Param_Spec));
5761 begin
5762 if Present (AD) and then Protected_Present (AD) then
5763 Formal_Type :=
5764 Replace_Anonymous_Access_To_Protected_Subprogram
5765 (Param_Spec, Formal_Type);
5766 end if;
5767 end;
5768 end if;
5770 Set_Etype (Formal, Formal_Type);
5771 Default := Expression (Param_Spec);
5773 if Present (Default) then
5774 if Out_Present (Param_Spec) then
5775 Error_Msg_N
5776 ("default initialization only allowed for IN parameters",
5777 Param_Spec);
5778 end if;
5780 -- Do the special preanalysis of the expression (see section on
5781 -- "Handling of Default Expressions" in the spec of package Sem).
5783 Analyze_Per_Use_Expression (Default, Formal_Type);
5785 -- Check that the designated type of an access parameter's default
5786 -- is not a class-wide type unless the parameter's designated type
5787 -- is also class-wide.
5789 if Ekind (Formal_Type) = E_Anonymous_Access_Type
5790 and then not From_With_Type (Formal_Type)
5791 and then Is_Class_Wide_Default (Default)
5792 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
5793 then
5794 Error_Msg_N
5795 ("access to class-wide expression not allowed here", Default);
5796 end if;
5797 end if;
5799 -- Ada 2005 (AI-231): Static checks
5801 if Ada_Version >= Ada_05
5802 and then Is_Access_Type (Etype (Formal))
5803 and then Can_Never_Be_Null (Etype (Formal))
5804 then
5805 Null_Exclusion_Static_Checks (Param_Spec);
5806 end if;
5808 <<Continue>>
5809 Next (Param_Spec);
5810 end loop;
5812 -- If this is the formal part of a function specification, analyze the
5813 -- subtype mark in the context where the formals are visible but not
5814 -- yet usable, and may hide outer homographs.
5816 if Nkind (Related_Nod) = N_Function_Specification then
5817 Analyze_Return_Type (Related_Nod);
5818 end if;
5820 -- Now set the kind (mode) of each formal
5822 Param_Spec := First (T);
5824 while Present (Param_Spec) loop
5825 Formal := Defining_Identifier (Param_Spec);
5826 Set_Formal_Mode (Formal);
5828 if Ekind (Formal) = E_In_Parameter then
5829 Set_Default_Value (Formal, Expression (Param_Spec));
5831 if Present (Expression (Param_Spec)) then
5832 Default := Expression (Param_Spec);
5834 if Is_Scalar_Type (Etype (Default)) then
5835 if Nkind
5836 (Parameter_Type (Param_Spec)) /= N_Access_Definition
5837 then
5838 Formal_Type := Entity (Parameter_Type (Param_Spec));
5840 else
5841 Formal_Type := Access_Definition
5842 (Related_Nod, Parameter_Type (Param_Spec));
5843 end if;
5845 Apply_Scalar_Range_Check (Default, Formal_Type);
5846 end if;
5847 end if;
5848 end if;
5850 Next (Param_Spec);
5851 end loop;
5853 end Process_Formals;
5855 ----------------------------
5856 -- Reference_Body_Formals --
5857 ----------------------------
5859 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
5860 Fs : Entity_Id;
5861 Fb : Entity_Id;
5863 begin
5864 if Error_Posted (Spec) then
5865 return;
5866 end if;
5868 Fs := First_Formal (Spec);
5869 Fb := First_Formal (Bod);
5871 while Present (Fs) loop
5872 Generate_Reference (Fs, Fb, 'b');
5874 if Style_Check then
5875 Style.Check_Identifier (Fb, Fs);
5876 end if;
5878 Set_Spec_Entity (Fb, Fs);
5879 Set_Referenced (Fs, False);
5880 Next_Formal (Fs);
5881 Next_Formal (Fb);
5882 end loop;
5883 end Reference_Body_Formals;
5885 -------------------------
5886 -- Set_Actual_Subtypes --
5887 -------------------------
5889 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
5890 Loc : constant Source_Ptr := Sloc (N);
5891 Decl : Node_Id;
5892 Formal : Entity_Id;
5893 T : Entity_Id;
5894 First_Stmt : Node_Id := Empty;
5895 AS_Needed : Boolean;
5897 begin
5898 -- If this is an emtpy initialization procedure, no need to create
5899 -- actual subtypes (small optimization).
5901 if Ekind (Subp) = E_Procedure
5902 and then Is_Null_Init_Proc (Subp)
5903 then
5904 return;
5905 end if;
5907 Formal := First_Formal (Subp);
5908 while Present (Formal) loop
5909 T := Etype (Formal);
5911 -- We never need an actual subtype for a constrained formal
5913 if Is_Constrained (T) then
5914 AS_Needed := False;
5916 -- If we have unknown discriminants, then we do not need an actual
5917 -- subtype, or more accurately we cannot figure it out! Note that
5918 -- all class-wide types have unknown discriminants.
5920 elsif Has_Unknown_Discriminants (T) then
5921 AS_Needed := False;
5923 -- At this stage we have an unconstrained type that may need an
5924 -- actual subtype. For sure the actual subtype is needed if we have
5925 -- an unconstrained array type.
5927 elsif Is_Array_Type (T) then
5928 AS_Needed := True;
5930 -- The only other case needing an actual subtype is an unconstrained
5931 -- record type which is an IN parameter (we cannot generate actual
5932 -- subtypes for the OUT or IN OUT case, since an assignment can
5933 -- change the discriminant values. However we exclude the case of
5934 -- initialization procedures, since discriminants are handled very
5935 -- specially in this context, see the section entitled "Handling of
5936 -- Discriminants" in Einfo.
5938 -- We also exclude the case of Discrim_SO_Functions (functions used
5939 -- in front end layout mode for size/offset values), since in such
5940 -- functions only discriminants are referenced, and not only are such
5941 -- subtypes not needed, but they cannot always be generated, because
5942 -- of order of elaboration issues.
5944 elsif Is_Record_Type (T)
5945 and then Ekind (Formal) = E_In_Parameter
5946 and then Chars (Formal) /= Name_uInit
5947 and then not Is_Unchecked_Union (T)
5948 and then not Is_Discrim_SO_Function (Subp)
5949 then
5950 AS_Needed := True;
5952 -- All other cases do not need an actual subtype
5954 else
5955 AS_Needed := False;
5956 end if;
5958 -- Generate actual subtypes for unconstrained arrays and
5959 -- unconstrained discriminated records.
5961 if AS_Needed then
5962 if Nkind (N) = N_Accept_Statement then
5964 -- If expansion is active, The formal is replaced by a local
5965 -- variable that renames the corresponding entry of the
5966 -- parameter block, and it is this local variable that may
5967 -- require an actual subtype.
5969 if Expander_Active then
5970 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
5971 else
5972 Decl := Build_Actual_Subtype (T, Formal);
5973 end if;
5975 if Present (Handled_Statement_Sequence (N)) then
5976 First_Stmt :=
5977 First (Statements (Handled_Statement_Sequence (N)));
5978 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
5979 Mark_Rewrite_Insertion (Decl);
5980 else
5981 -- If the accept statement has no body, there will be no
5982 -- reference to the actuals, so no need to compute actual
5983 -- subtypes.
5985 return;
5986 end if;
5988 else
5989 Decl := Build_Actual_Subtype (T, Formal);
5990 Prepend (Decl, Declarations (N));
5991 Mark_Rewrite_Insertion (Decl);
5992 end if;
5994 -- The declaration uses the bounds of an existing object, and
5995 -- therefore needs no constraint checks.
5997 Analyze (Decl, Suppress => All_Checks);
5999 -- We need to freeze manually the generated type when it is
6000 -- inserted anywhere else than in a declarative part.
6002 if Present (First_Stmt) then
6003 Insert_List_Before_And_Analyze (First_Stmt,
6004 Freeze_Entity (Defining_Identifier (Decl), Loc));
6005 end if;
6007 if Nkind (N) = N_Accept_Statement
6008 and then Expander_Active
6009 then
6010 Set_Actual_Subtype (Renamed_Object (Formal),
6011 Defining_Identifier (Decl));
6012 else
6013 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
6014 end if;
6015 end if;
6017 Next_Formal (Formal);
6018 end loop;
6019 end Set_Actual_Subtypes;
6021 ---------------------
6022 -- Set_Formal_Mode --
6023 ---------------------
6025 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
6026 Spec : constant Node_Id := Parent (Formal_Id);
6028 begin
6029 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
6030 -- since we ensure that corresponding actuals are always valid at the
6031 -- point of the call.
6033 if Out_Present (Spec) then
6034 if Ekind (Scope (Formal_Id)) = E_Function
6035 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
6036 then
6037 Error_Msg_N ("functions can only have IN parameters", Spec);
6038 Set_Ekind (Formal_Id, E_In_Parameter);
6040 elsif In_Present (Spec) then
6041 Set_Ekind (Formal_Id, E_In_Out_Parameter);
6043 else
6044 Set_Ekind (Formal_Id, E_Out_Parameter);
6045 Set_Never_Set_In_Source (Formal_Id, True);
6046 Set_Is_True_Constant (Formal_Id, False);
6047 Set_Current_Value (Formal_Id, Empty);
6048 end if;
6050 else
6051 Set_Ekind (Formal_Id, E_In_Parameter);
6052 end if;
6054 -- Set Is_Known_Non_Null for access parameters since the language
6055 -- guarantees that access parameters are always non-null. We also set
6056 -- Can_Never_Be_Null, since there is no way to change the value.
6058 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
6060 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
6061 -- null; In Ada 2005, only if then null_exclusion is explicit.
6063 if Ada_Version < Ada_05
6064 or else Can_Never_Be_Null (Etype (Formal_Id))
6065 then
6066 Set_Is_Known_Non_Null (Formal_Id);
6067 Set_Can_Never_Be_Null (Formal_Id);
6068 end if;
6070 -- Ada 2005 (AI-231): Null-exclusion access subtype
6072 elsif Is_Access_Type (Etype (Formal_Id))
6073 and then Can_Never_Be_Null (Etype (Formal_Id))
6074 then
6075 Set_Is_Known_Non_Null (Formal_Id);
6076 end if;
6078 Set_Mechanism (Formal_Id, Default_Mechanism);
6079 Set_Formal_Validity (Formal_Id);
6080 end Set_Formal_Mode;
6082 -------------------------
6083 -- Set_Formal_Validity --
6084 -------------------------
6086 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
6087 begin
6088 -- If no validity checking, then we cannot assume anything about the
6089 -- validity of parameters, since we do not know there is any checking
6090 -- of the validity on the call side.
6092 if not Validity_Checks_On then
6093 return;
6095 -- If validity checking for parameters is enabled, this means we are
6096 -- not supposed to make any assumptions about argument values.
6098 elsif Validity_Check_Parameters then
6099 return;
6101 -- If we are checking in parameters, we will assume that the caller is
6102 -- also checking parameters, so we can assume the parameter is valid.
6104 elsif Ekind (Formal_Id) = E_In_Parameter
6105 and then Validity_Check_In_Params
6106 then
6107 Set_Is_Known_Valid (Formal_Id, True);
6109 -- Similar treatment for IN OUT parameters
6111 elsif Ekind (Formal_Id) = E_In_Out_Parameter
6112 and then Validity_Check_In_Out_Params
6113 then
6114 Set_Is_Known_Valid (Formal_Id, True);
6115 end if;
6116 end Set_Formal_Validity;
6118 ------------------------
6119 -- Subtype_Conformant --
6120 ------------------------
6122 function Subtype_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6123 Result : Boolean;
6124 begin
6125 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result);
6126 return Result;
6127 end Subtype_Conformant;
6129 ---------------------
6130 -- Type_Conformant --
6131 ---------------------
6133 function Type_Conformant
6134 (New_Id : Entity_Id;
6135 Old_Id : Entity_Id;
6136 Skip_Controlling_Formals : Boolean := False) return Boolean
6138 Result : Boolean;
6139 begin
6140 May_Hide_Profile := False;
6142 Check_Conformance
6143 (New_Id, Old_Id, Type_Conformant, False, Result,
6144 Skip_Controlling_Formals => Skip_Controlling_Formals);
6145 return Result;
6146 end Type_Conformant;
6148 -------------------------------
6149 -- Valid_Operator_Definition --
6150 -------------------------------
6152 procedure Valid_Operator_Definition (Designator : Entity_Id) is
6153 N : Integer := 0;
6154 F : Entity_Id;
6155 Id : constant Name_Id := Chars (Designator);
6156 N_OK : Boolean;
6158 begin
6159 F := First_Formal (Designator);
6160 while Present (F) loop
6161 N := N + 1;
6163 if Present (Default_Value (F)) then
6164 Error_Msg_N
6165 ("default values not allowed for operator parameters",
6166 Parent (F));
6167 end if;
6169 Next_Formal (F);
6170 end loop;
6172 -- Verify that user-defined operators have proper number of arguments
6173 -- First case of operators which can only be unary
6175 if Id = Name_Op_Not
6176 or else Id = Name_Op_Abs
6177 then
6178 N_OK := (N = 1);
6180 -- Case of operators which can be unary or binary
6182 elsif Id = Name_Op_Add
6183 or Id = Name_Op_Subtract
6184 then
6185 N_OK := (N in 1 .. 2);
6187 -- All other operators can only be binary
6189 else
6190 N_OK := (N = 2);
6191 end if;
6193 if not N_OK then
6194 Error_Msg_N
6195 ("incorrect number of arguments for operator", Designator);
6196 end if;
6198 if Id = Name_Op_Ne
6199 and then Base_Type (Etype (Designator)) = Standard_Boolean
6200 and then not Is_Intrinsic_Subprogram (Designator)
6201 then
6202 Error_Msg_N
6203 ("explicit definition of inequality not allowed", Designator);
6204 end if;
6205 end Valid_Operator_Definition;
6207 end Sem_Ch6;