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