errout.adb, errout.ads (Check_Formal_Restriction): new procedure which issues an...
[gcc.git] / gcc / ada / sem_ch6.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
42 with Lib.Xref; use Lib.Xref;
43 with Layout; use Layout;
44 with Namet; use Namet;
45 with Lib; use Lib;
46 with Nlists; use Nlists;
47 with Nmake; use Nmake;
48 with Opt; use Opt;
49 with Output; use Output;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch4; use Sem_Ch4;
58 with Sem_Ch5; use Sem_Ch5;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch10; use Sem_Ch10;
61 with Sem_Ch12; use Sem_Ch12;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Prag; use Sem_Prag;
69 with Sem_Res; use Sem_Res;
70 with Sem_Util; use Sem_Util;
71 with Sem_Type; use Sem_Type;
72 with Sem_Warn; use Sem_Warn;
73 with Sinput; use Sinput;
74 with Stand; use Stand;
75 with Sinfo; use Sinfo;
76 with Sinfo.CN; use Sinfo.CN;
77 with Snames; use Snames;
78 with Stringt; use Stringt;
79 with Style;
80 with Stylesw; use Stylesw;
81 with Tbuild; use Tbuild;
82 with Uintp; use Uintp;
83 with Urealp; use Urealp;
84 with Validsw; use Validsw;
85
86 package body Sem_Ch6 is
87
88 May_Hide_Profile : Boolean := False;
89 -- This flag is used to indicate that two formals in two subprograms being
90 -- checked for conformance differ only in that one is an access parameter
91 -- while the other is of a general access type with the same designated
92 -- type. In this case, if the rest of the signatures match, a call to
93 -- either subprogram may be ambiguous, which is worth a warning. The flag
94 -- is set in Compatible_Types, and the warning emitted in
95 -- New_Overloaded_Entity.
96
97 -----------------------
98 -- Local Subprograms --
99 -----------------------
100
101 procedure Analyze_Return_Statement (N : Node_Id);
102 -- Common processing for simple and extended return statements
103
104 procedure Analyze_Function_Return (N : Node_Id);
105 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
106 -- applies to a [generic] function.
107
108 procedure Analyze_Return_Type (N : Node_Id);
109 -- Subsidiary to Process_Formals: analyze subtype mark in function
110 -- specification in a context where the formals are visible and hide
111 -- outer homographs.
112
113 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
114 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
115 -- that we can use RETURN but not skip the debug output at the end.
116
117 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
118 -- Analyze a generic subprogram body. N is the body to be analyzed, and
119 -- Gen_Id is the defining entity Id for the corresponding spec.
120
121 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
122 -- If a subprogram has pragma Inline and inlining is active, use generic
123 -- machinery to build an unexpanded body for the subprogram. This body is
124 -- subsequently used for inline expansions at call sites. If subprogram can
125 -- be inlined (depending on size and nature of local declarations) this
126 -- function returns true. Otherwise subprogram body is treated normally.
127 -- If proper warnings are enabled and the subprogram contains a construct
128 -- that cannot be inlined, the offending construct is flagged accordingly.
129
130 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
131 -- Returns true if Subp can override a predefined operator.
132
133 procedure Check_Conformance
134 (New_Id : Entity_Id;
135 Old_Id : Entity_Id;
136 Ctype : Conformance_Type;
137 Errmsg : Boolean;
138 Conforms : out Boolean;
139 Err_Loc : Node_Id := Empty;
140 Get_Inst : Boolean := False;
141 Skip_Controlling_Formals : Boolean := False);
142 -- Given two entities, this procedure checks that the profiles associated
143 -- with these entities meet the conformance criterion given by the third
144 -- parameter. If they conform, Conforms is set True and control returns
145 -- to the caller. If they do not conform, Conforms is set to False, and
146 -- in addition, if Errmsg is True on the call, proper messages are output
147 -- to complain about the conformance failure. If Err_Loc is non_Empty
148 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
149 -- error messages are placed on the appropriate part of the construct
150 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
151 -- against a formal access-to-subprogram type so Get_Instance_Of must
152 -- be called.
153
154 procedure Check_Subprogram_Order (N : Node_Id);
155 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
156 -- the alpha ordering rule for N if this ordering requirement applicable.
157
158 procedure Check_Returns
159 (HSS : Node_Id;
160 Mode : Character;
161 Err : out Boolean;
162 Proc : Entity_Id := Empty);
163 -- Called to check for missing return statements in a function body, or for
164 -- returns present in a procedure body which has No_Return set. HSS is the
165 -- handled statement sequence for the subprogram body. This procedure
166 -- checks all flow paths to make sure they either have return (Mode = 'F',
167 -- used for functions) or do not have a return (Mode = 'P', used for
168 -- No_Return procedures). The flag Err is set if there are any control
169 -- paths not explicitly terminated by a return in the function case, and is
170 -- True otherwise. Proc is the entity for the procedure case and is used
171 -- in posting the warning message.
172
173 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
174 -- In Ada 2012, a primitive equality operator on an untagged record type
175 -- must appear before the type is frozen, and have the same visibility as
176 -- that of the type. This procedure checks that this rule is met, and
177 -- otherwise emits an error on the subprogram declaration and a warning
178 -- on the earlier freeze point if it is easy to locate.
179
180 procedure Enter_Overloaded_Entity (S : Entity_Id);
181 -- This procedure makes S, a new overloaded entity, into the first visible
182 -- entity with that name.
183
184 procedure Install_Entity (E : Entity_Id);
185 -- Make single entity visible (used for generic formals as well)
186
187 function Is_Non_Overriding_Operation
188 (Prev_E : Entity_Id;
189 New_E : Entity_Id) return Boolean;
190 -- Enforce the rule given in 12.3(18): a private operation in an instance
191 -- overrides an inherited operation only if the corresponding operation
192 -- was overriding in the generic. This can happen for primitive operations
193 -- of types derived (in the generic unit) from formal private or formal
194 -- derived types.
195
196 procedure Make_Inequality_Operator (S : Entity_Id);
197 -- Create the declaration for an inequality operator that is implicitly
198 -- created by a user-defined equality operator that yields a boolean.
199
200 procedure May_Need_Actuals (Fun : Entity_Id);
201 -- Flag functions that can be called without parameters, i.e. those that
202 -- have no parameters, or those for which defaults exist for all parameters
203
204 procedure Process_PPCs
205 (N : Node_Id;
206 Spec_Id : Entity_Id;
207 Body_Id : Entity_Id);
208 -- Called from Analyze[_Generic]_Subprogram_Body to deal with scanning post
209 -- conditions for the body and assembling and inserting the _postconditions
210 -- procedure. N is the node for the subprogram body and Body_Id/Spec_Id are
211 -- the entities for the body and separate spec (if there is no separate
212 -- spec, Spec_Id is Empty). Note that invariants and predicates may also
213 -- provide postconditions, and are also handled in this procedure.
214
215 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
216 -- Formal_Id is an formal parameter entity. This procedure deals with
217 -- setting the proper validity status for this entity, which depends on
218 -- the kind of parameter and the validity checking mode.
219
220 ---------------------------------------------
221 -- Analyze_Abstract_Subprogram_Declaration --
222 ---------------------------------------------
223
224 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
225 Designator : constant Entity_Id :=
226 Analyze_Subprogram_Specification (Specification (N));
227 Scop : constant Entity_Id := Current_Scope;
228
229 begin
230 Check_Formal_Restriction ("abstract subprogram is not allowed", N);
231
232 Generate_Definition (Designator);
233 Set_Is_Abstract_Subprogram (Designator);
234 New_Overloaded_Entity (Designator);
235 Check_Delayed_Subprogram (Designator);
236
237 Set_Categorization_From_Scope (Designator, Scop);
238
239 if Ekind (Scope (Designator)) = E_Protected_Type then
240 Error_Msg_N
241 ("abstract subprogram not allowed in protected type", N);
242
243 -- Issue a warning if the abstract subprogram is neither a dispatching
244 -- operation nor an operation that overrides an inherited subprogram or
245 -- predefined operator, since this most likely indicates a mistake.
246
247 elsif Warn_On_Redundant_Constructs
248 and then not Is_Dispatching_Operation (Designator)
249 and then not Present (Overridden_Operation (Designator))
250 and then (not Is_Operator_Symbol_Name (Chars (Designator))
251 or else Scop /= Scope (Etype (First_Formal (Designator))))
252 then
253 Error_Msg_N
254 ("?abstract subprogram is not dispatching or overriding", N);
255 end if;
256
257 Generate_Reference_To_Formals (Designator);
258 Check_Eliminated (Designator);
259
260 if Has_Aspects (N) then
261 Analyze_Aspect_Specifications (N, Designator);
262 end if;
263 end Analyze_Abstract_Subprogram_Declaration;
264
265 ---------------------------------
266 -- Analyze_Expression_Function --
267 ---------------------------------
268
269 procedure Analyze_Expression_Function (N : Node_Id) is
270 Loc : constant Source_Ptr := Sloc (N);
271 LocX : constant Source_Ptr := Sloc (Expression (N));
272 Def_Id : constant Entity_Id := Defining_Entity (Specification (N));
273 New_Body : Node_Id;
274
275 Prev : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
276 -- If the expression is a completion, Prev is the entity whose
277 -- declaration is completed.
278
279 begin
280 -- This is one of the occasions on which we transform the tree during
281 -- semantic analysis. Transform the expression function into an
282 -- equivalent subprogram body, and then analyze that.
283
284 New_Body :=
285 Make_Subprogram_Body (Loc,
286 Specification => Specification (N),
287 Declarations => Empty_List,
288 Handled_Statement_Sequence =>
289 Make_Handled_Sequence_Of_Statements (LocX,
290 Statements => New_List (
291 Make_Simple_Return_Statement (LocX,
292 Expression => Expression (N)))));
293
294 if Present (Prev)
295 and then Ekind (Prev) = E_Generic_Function
296 then
297 -- If the expression completes a generic subprogram, we must create a
298 -- separate node for the body, because at instantiation the original
299 -- node of the generic copy must be a generic subprogram body, and
300 -- cannot be a expression function. Otherwise we just rewrite the
301 -- expression with the non-generic body.
302
303 Insert_After (N, New_Body);
304 Rewrite (N, Make_Null_Statement (Loc));
305 Analyze (N);
306 Analyze (New_Body);
307
308 else
309 Rewrite (N, New_Body);
310 Analyze (N);
311 end if;
312 end Analyze_Expression_Function;
313
314 ----------------------------------------
315 -- Analyze_Extended_Return_Statement --
316 ----------------------------------------
317
318 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
319 begin
320 Analyze_Return_Statement (N);
321 end Analyze_Extended_Return_Statement;
322
323 ----------------------------
324 -- Analyze_Function_Call --
325 ----------------------------
326
327 procedure Analyze_Function_Call (N : Node_Id) is
328 P : constant Node_Id := Name (N);
329 L : constant List_Id := Parameter_Associations (N);
330 Actual : Node_Id;
331
332 begin
333 Analyze (P);
334
335 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
336 -- as B (A, X). If the rewriting is successful, the call has been
337 -- analyzed and we just return.
338
339 if Nkind (P) = N_Selected_Component
340 and then Name (N) /= P
341 and then Is_Rewrite_Substitution (N)
342 and then Present (Etype (N))
343 then
344 return;
345 end if;
346
347 -- If error analyzing name, then set Any_Type as result type and return
348
349 if Etype (P) = Any_Type then
350 Set_Etype (N, Any_Type);
351 return;
352 end if;
353
354 -- Otherwise analyze the parameters
355
356 if Present (L) then
357 Actual := First (L);
358 while Present (Actual) loop
359 Analyze (Actual);
360 Check_Parameterless_Call (Actual);
361 Next (Actual);
362 end loop;
363 end if;
364
365 Analyze_Call (N);
366 end Analyze_Function_Call;
367
368 -----------------------------
369 -- Analyze_Function_Return --
370 -----------------------------
371
372 procedure Analyze_Function_Return (N : Node_Id) is
373 Loc : constant Source_Ptr := Sloc (N);
374 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
375 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
376
377 R_Type : constant Entity_Id := Etype (Scope_Id);
378 -- Function result subtype
379
380 procedure Check_Limited_Return (Expr : Node_Id);
381 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
382 -- limited types. Used only for simple return statements.
383 -- Expr is the expression returned.
384
385 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
386 -- Check that the return_subtype_indication properly matches the result
387 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
388
389 --------------------------
390 -- Check_Limited_Return --
391 --------------------------
392
393 procedure Check_Limited_Return (Expr : Node_Id) is
394 begin
395 -- Ada 2005 (AI-318-02): Return-by-reference types have been
396 -- removed and replaced by anonymous access results. This is an
397 -- incompatibility with Ada 95. Not clear whether this should be
398 -- enforced yet or perhaps controllable with special switch. ???
399
400 if Is_Limited_Type (R_Type)
401 and then Comes_From_Source (N)
402 and then not In_Instance_Body
403 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
404 then
405 -- Error in Ada 2005
406
407 if Ada_Version >= Ada_2005
408 and then not Debug_Flag_Dot_L
409 and then not GNAT_Mode
410 then
411 Error_Msg_N
412 ("(Ada 2005) cannot copy object of a limited type " &
413 "(RM-2005 6.5(5.5/2))", Expr);
414
415 if Is_Immutably_Limited_Type (R_Type) then
416 Error_Msg_N
417 ("\return by reference not permitted in Ada 2005", Expr);
418 end if;
419
420 -- Warn in Ada 95 mode, to give folks a heads up about this
421 -- incompatibility.
422
423 -- In GNAT mode, this is just a warning, to allow it to be
424 -- evilly turned off. Otherwise it is a real error.
425
426 -- In a generic context, simplify the warning because it makes
427 -- no sense to discuss pass-by-reference or copy.
428
429 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
430 if Inside_A_Generic then
431 Error_Msg_N
432 ("return of limited object not permitted in Ada2005 "
433 & "(RM-2005 6.5(5.5/2))?", Expr);
434
435 elsif Is_Immutably_Limited_Type (R_Type) then
436 Error_Msg_N
437 ("return by reference not permitted in Ada 2005 "
438 & "(RM-2005 6.5(5.5/2))?", Expr);
439 else
440 Error_Msg_N
441 ("cannot copy object of a limited type in Ada 2005 "
442 & "(RM-2005 6.5(5.5/2))?", Expr);
443 end if;
444
445 -- Ada 95 mode, compatibility warnings disabled
446
447 else
448 return; -- skip continuation messages below
449 end if;
450
451 if not Inside_A_Generic then
452 Error_Msg_N
453 ("\consider switching to return of access type", Expr);
454 Explain_Limited_Type (R_Type, Expr);
455 end if;
456 end if;
457 end Check_Limited_Return;
458
459 -------------------------------------
460 -- Check_Return_Subtype_Indication --
461 -------------------------------------
462
463 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
464 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
465
466 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
467 -- Subtype given in the extended return statement (must match R_Type)
468
469 Subtype_Ind : constant Node_Id :=
470 Object_Definition (Original_Node (Obj_Decl));
471
472 R_Type_Is_Anon_Access :
473 constant Boolean :=
474 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
475 or else
476 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
477 or else
478 Ekind (R_Type) = E_Anonymous_Access_Type;
479 -- True if return type of the function is an anonymous access type
480 -- Can't we make Is_Anonymous_Access_Type in einfo ???
481
482 R_Stm_Type_Is_Anon_Access :
483 constant Boolean :=
484 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
485 or else
486 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
487 or else
488 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
489 -- True if type of the return object is an anonymous access type
490
491 begin
492 -- First, avoid cascaded errors
493
494 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
495 return;
496 end if;
497
498 -- "return access T" case; check that the return statement also has
499 -- "access T", and that the subtypes statically match:
500 -- if this is an access to subprogram the signatures must match.
501
502 if R_Type_Is_Anon_Access then
503 if R_Stm_Type_Is_Anon_Access then
504 if
505 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
506 then
507 if Base_Type (Designated_Type (R_Stm_Type)) /=
508 Base_Type (Designated_Type (R_Type))
509 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
510 then
511 Error_Msg_N
512 ("subtype must statically match function result subtype",
513 Subtype_Mark (Subtype_Ind));
514 end if;
515
516 else
517 -- For two anonymous access to subprogram types, the
518 -- types themselves must be type conformant.
519
520 if not Conforming_Types
521 (R_Stm_Type, R_Type, Fully_Conformant)
522 then
523 Error_Msg_N
524 ("subtype must statically match function result subtype",
525 Subtype_Ind);
526 end if;
527 end if;
528
529 else
530 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
531 end if;
532
533 -- Subtype indication case: check that the return object's type is
534 -- covered by the result type, and that the subtypes statically match
535 -- when the result subtype is constrained. Also handle record types
536 -- with unknown discriminants for which we have built the underlying
537 -- record view. Coverage is needed to allow specific-type return
538 -- objects when the result type is class-wide (see AI05-32).
539
540 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
541 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
542 and then
543 Covers
544 (Base_Type (R_Type),
545 Underlying_Record_View (Base_Type (R_Stm_Type))))
546 then
547 -- A null exclusion may be present on the return type, on the
548 -- function specification, on the object declaration or on the
549 -- subtype itself.
550
551 if Is_Access_Type (R_Type)
552 and then
553 (Can_Never_Be_Null (R_Type)
554 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
555 Can_Never_Be_Null (R_Stm_Type)
556 then
557 Error_Msg_N
558 ("subtype must statically match function result subtype",
559 Subtype_Ind);
560 end if;
561
562 -- AI05-103: for elementary types, subtypes must statically match
563
564 if Is_Constrained (R_Type)
565 or else Is_Access_Type (R_Type)
566 then
567 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
568 Error_Msg_N
569 ("subtype must statically match function result subtype",
570 Subtype_Ind);
571 end if;
572 end if;
573
574 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
575 and then Is_Null_Extension (Base_Type (R_Type))
576 then
577 null;
578
579 else
580 Error_Msg_N
581 ("wrong type for return_subtype_indication", Subtype_Ind);
582 end if;
583 end Check_Return_Subtype_Indication;
584
585 ---------------------
586 -- Local Variables --
587 ---------------------
588
589 Expr : Node_Id;
590
591 -- Start of processing for Analyze_Function_Return
592
593 begin
594 Set_Return_Present (Scope_Id);
595
596 if Nkind (N) = N_Simple_Return_Statement then
597 Expr := Expression (N);
598 Analyze_And_Resolve (Expr, R_Type);
599 Check_Limited_Return (Expr);
600
601 -- The only RETURN allowed in SPARK or ALFA is as the last statement
602 -- of the function.
603
604 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
605 and then
606 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
607 or else Present (Next (N)))
608 then
609 Check_Formal_Restriction
610 ("RETURN should be the last statement in function", N);
611 end if;
612
613 else
614 Check_Formal_Restriction ("extended RETURN is not allowed", N);
615
616 -- Analyze parts specific to extended_return_statement:
617
618 declare
619 Obj_Decl : constant Node_Id :=
620 Last (Return_Object_Declarations (N));
621
622 HSS : constant Node_Id := Handled_Statement_Sequence (N);
623
624 begin
625 Expr := Expression (Obj_Decl);
626
627 -- Note: The check for OK_For_Limited_Init will happen in
628 -- Analyze_Object_Declaration; we treat it as a normal
629 -- object declaration.
630
631 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
632 Analyze (Obj_Decl);
633
634 Check_Return_Subtype_Indication (Obj_Decl);
635
636 if Present (HSS) then
637 Analyze (HSS);
638
639 if Present (Exception_Handlers (HSS)) then
640
641 -- ???Has_Nested_Block_With_Handler needs to be set.
642 -- Probably by creating an actual N_Block_Statement.
643 -- Probably in Expand.
644
645 null;
646 end if;
647 end if;
648
649 -- Mark the return object as referenced, since the return is an
650 -- implicit reference of the object.
651
652 Set_Referenced (Defining_Identifier (Obj_Decl));
653
654 Check_References (Stm_Entity);
655 end;
656 end if;
657
658 -- Case of Expr present
659
660 if Present (Expr)
661
662 -- Defend against previous errors
663
664 and then Nkind (Expr) /= N_Empty
665 and then Present (Etype (Expr))
666 then
667 -- Apply constraint check. Note that this is done before the implicit
668 -- conversion of the expression done for anonymous access types to
669 -- ensure correct generation of the null-excluding check associated
670 -- with null-excluding expressions found in return statements.
671
672 Apply_Constraint_Check (Expr, R_Type);
673
674 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
675 -- type, apply an implicit conversion of the expression to that type
676 -- to force appropriate static and run-time accessibility checks.
677
678 if Ada_Version >= Ada_2005
679 and then Ekind (R_Type) = E_Anonymous_Access_Type
680 then
681 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
682 Analyze_And_Resolve (Expr, R_Type);
683 end if;
684
685 -- If the result type is class-wide, then check that the return
686 -- expression's type is not declared at a deeper level than the
687 -- function (RM05-6.5(5.6/2)).
688
689 if Ada_Version >= Ada_2005
690 and then Is_Class_Wide_Type (R_Type)
691 then
692 if Type_Access_Level (Etype (Expr)) >
693 Subprogram_Access_Level (Scope_Id)
694 then
695 Error_Msg_N
696 ("level of return expression type is deeper than " &
697 "class-wide function!", Expr);
698 end if;
699 end if;
700
701 -- Check incorrect use of dynamically tagged expression
702
703 if Is_Tagged_Type (R_Type) then
704 Check_Dynamically_Tagged_Expression
705 (Expr => Expr,
706 Typ => R_Type,
707 Related_Nod => N);
708 end if;
709
710 -- ??? A real run-time accessibility check is needed in cases
711 -- involving dereferences of access parameters. For now we just
712 -- check the static cases.
713
714 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
715 and then Is_Immutably_Limited_Type (Etype (Scope_Id))
716 and then Object_Access_Level (Expr) >
717 Subprogram_Access_Level (Scope_Id)
718 then
719
720 -- Suppress the message in a generic, where the rewriting
721 -- is irrelevant.
722
723 if Inside_A_Generic then
724 null;
725
726 else
727 Rewrite (N,
728 Make_Raise_Program_Error (Loc,
729 Reason => PE_Accessibility_Check_Failed));
730 Analyze (N);
731
732 Error_Msg_N
733 ("cannot return a local value by reference?", N);
734 Error_Msg_NE
735 ("\& will be raised at run time?",
736 N, Standard_Program_Error);
737 end if;
738 end if;
739
740 if Known_Null (Expr)
741 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
742 and then Null_Exclusion_Present (Parent (Scope_Id))
743 then
744 Apply_Compile_Time_Constraint_Error
745 (N => Expr,
746 Msg => "(Ada 2005) null not allowed for "
747 & "null-excluding return?",
748 Reason => CE_Null_Not_Allowed);
749 end if;
750
751 -- Apply checks suggested by AI05-0144 (dangerous order dependence)
752
753 Check_Order_Dependence;
754 end if;
755 end Analyze_Function_Return;
756
757 -------------------------------------
758 -- Analyze_Generic_Subprogram_Body --
759 -------------------------------------
760
761 procedure Analyze_Generic_Subprogram_Body
762 (N : Node_Id;
763 Gen_Id : Entity_Id)
764 is
765 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
766 Kind : constant Entity_Kind := Ekind (Gen_Id);
767 Body_Id : Entity_Id;
768 New_N : Node_Id;
769 Spec : Node_Id;
770
771 begin
772 -- Copy body and disable expansion while analyzing the generic For a
773 -- stub, do not copy the stub (which would load the proper body), this
774 -- will be done when the proper body is analyzed.
775
776 if Nkind (N) /= N_Subprogram_Body_Stub then
777 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
778 Rewrite (N, New_N);
779 Start_Generic;
780 end if;
781
782 Spec := Specification (N);
783
784 -- Within the body of the generic, the subprogram is callable, and
785 -- behaves like the corresponding non-generic unit.
786
787 Body_Id := Defining_Entity (Spec);
788
789 if Kind = E_Generic_Procedure
790 and then Nkind (Spec) /= N_Procedure_Specification
791 then
792 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
793 return;
794
795 elsif Kind = E_Generic_Function
796 and then Nkind (Spec) /= N_Function_Specification
797 then
798 Error_Msg_N ("invalid body for generic function ", Body_Id);
799 return;
800 end if;
801
802 Set_Corresponding_Body (Gen_Decl, Body_Id);
803
804 if Has_Completion (Gen_Id)
805 and then Nkind (Parent (N)) /= N_Subunit
806 then
807 Error_Msg_N ("duplicate generic body", N);
808 return;
809 else
810 Set_Has_Completion (Gen_Id);
811 end if;
812
813 if Nkind (N) = N_Subprogram_Body_Stub then
814 Set_Ekind (Defining_Entity (Specification (N)), Kind);
815 else
816 Set_Corresponding_Spec (N, Gen_Id);
817 end if;
818
819 if Nkind (Parent (N)) = N_Compilation_Unit then
820 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
821 end if;
822
823 -- Make generic parameters immediately visible in the body. They are
824 -- needed to process the formals declarations. Then make the formals
825 -- visible in a separate step.
826
827 Push_Scope (Gen_Id);
828
829 declare
830 E : Entity_Id;
831 First_Ent : Entity_Id;
832
833 begin
834 First_Ent := First_Entity (Gen_Id);
835
836 E := First_Ent;
837 while Present (E) and then not Is_Formal (E) loop
838 Install_Entity (E);
839 Next_Entity (E);
840 end loop;
841
842 Set_Use (Generic_Formal_Declarations (Gen_Decl));
843
844 -- Now generic formals are visible, and the specification can be
845 -- analyzed, for subsequent conformance check.
846
847 Body_Id := Analyze_Subprogram_Specification (Spec);
848
849 -- Make formal parameters visible
850
851 if Present (E) then
852
853 -- E is the first formal parameter, we loop through the formals
854 -- installing them so that they will be visible.
855
856 Set_First_Entity (Gen_Id, E);
857 while Present (E) loop
858 Install_Entity (E);
859 Next_Formal (E);
860 end loop;
861 end if;
862
863 -- Visible generic entity is callable within its own body
864
865 Set_Ekind (Gen_Id, Ekind (Body_Id));
866 Set_Ekind (Body_Id, E_Subprogram_Body);
867 Set_Convention (Body_Id, Convention (Gen_Id));
868 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
869 Set_Scope (Body_Id, Scope (Gen_Id));
870 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
871
872 if Nkind (N) = N_Subprogram_Body_Stub then
873
874 -- No body to analyze, so restore state of generic unit
875
876 Set_Ekind (Gen_Id, Kind);
877 Set_Ekind (Body_Id, Kind);
878
879 if Present (First_Ent) then
880 Set_First_Entity (Gen_Id, First_Ent);
881 end if;
882
883 End_Scope;
884 return;
885 end if;
886
887 -- If this is a compilation unit, it must be made visible explicitly,
888 -- because the compilation of the declaration, unlike other library
889 -- unit declarations, does not. If it is not a unit, the following
890 -- is redundant but harmless.
891
892 Set_Is_Immediately_Visible (Gen_Id);
893 Reference_Body_Formals (Gen_Id, Body_Id);
894
895 if Is_Child_Unit (Gen_Id) then
896 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
897 end if;
898
899 Set_Actual_Subtypes (N, Current_Scope);
900 Process_PPCs (N, Gen_Id, Body_Id);
901
902 -- If the generic unit carries pre- or post-conditions, copy them
903 -- to the original generic tree, so that they are properly added
904 -- to any instantiation.
905
906 declare
907 Orig : constant Node_Id := Original_Node (N);
908 Cond : Node_Id;
909
910 begin
911 Cond := First (Declarations (N));
912 while Present (Cond) loop
913 if Nkind (Cond) = N_Pragma
914 and then Pragma_Name (Cond) = Name_Check
915 then
916 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
917
918 elsif Nkind (Cond) = N_Pragma
919 and then Pragma_Name (Cond) = Name_Postcondition
920 then
921 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
922 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
923 else
924 exit;
925 end if;
926
927 Next (Cond);
928 end loop;
929 end;
930
931 Analyze_Declarations (Declarations (N));
932 Check_Completion;
933 Analyze (Handled_Statement_Sequence (N));
934
935 Save_Global_References (Original_Node (N));
936
937 -- Prior to exiting the scope, include generic formals again (if any
938 -- are present) in the set of local entities.
939
940 if Present (First_Ent) then
941 Set_First_Entity (Gen_Id, First_Ent);
942 end if;
943
944 Check_References (Gen_Id);
945 end;
946
947 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
948 End_Scope;
949 Check_Subprogram_Order (N);
950
951 -- Outside of its body, unit is generic again
952
953 Set_Ekind (Gen_Id, Kind);
954 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
955
956 if Style_Check then
957 Style.Check_Identifier (Body_Id, Gen_Id);
958 end if;
959
960 End_Generic;
961 end Analyze_Generic_Subprogram_Body;
962
963 -----------------------------
964 -- Analyze_Operator_Symbol --
965 -----------------------------
966
967 -- An operator symbol such as "+" or "and" may appear in context where the
968 -- literal denotes an entity name, such as "+"(x, y) or in context when it
969 -- is just a string, as in (conjunction = "or"). In these cases the parser
970 -- generates this node, and the semantics does the disambiguation. Other
971 -- such case are actuals in an instantiation, the generic unit in an
972 -- instantiation, and pragma arguments.
973
974 procedure Analyze_Operator_Symbol (N : Node_Id) is
975 Par : constant Node_Id := Parent (N);
976
977 begin
978 if (Nkind (Par) = N_Function_Call
979 and then N = Name (Par))
980 or else Nkind (Par) = N_Function_Instantiation
981 or else (Nkind (Par) = N_Indexed_Component
982 and then N = Prefix (Par))
983 or else (Nkind (Par) = N_Pragma_Argument_Association
984 and then not Is_Pragma_String_Literal (Par))
985 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
986 or else (Nkind (Par) = N_Attribute_Reference
987 and then Attribute_Name (Par) /= Name_Value)
988 then
989 Find_Direct_Name (N);
990
991 else
992 Change_Operator_Symbol_To_String_Literal (N);
993 Analyze (N);
994 end if;
995 end Analyze_Operator_Symbol;
996
997 -----------------------------------
998 -- Analyze_Parameter_Association --
999 -----------------------------------
1000
1001 procedure Analyze_Parameter_Association (N : Node_Id) is
1002 begin
1003 Analyze (Explicit_Actual_Parameter (N));
1004 end Analyze_Parameter_Association;
1005
1006 ----------------------------
1007 -- Analyze_Procedure_Call --
1008 ----------------------------
1009
1010 procedure Analyze_Procedure_Call (N : Node_Id) is
1011 Loc : constant Source_Ptr := Sloc (N);
1012 P : constant Node_Id := Name (N);
1013 Actuals : constant List_Id := Parameter_Associations (N);
1014 Actual : Node_Id;
1015 New_N : Node_Id;
1016
1017 procedure Analyze_Call_And_Resolve;
1018 -- Do Analyze and Resolve calls for procedure call
1019 -- At end, check illegal order dependence.
1020
1021 ------------------------------
1022 -- Analyze_Call_And_Resolve --
1023 ------------------------------
1024
1025 procedure Analyze_Call_And_Resolve is
1026 begin
1027 if Nkind (N) = N_Procedure_Call_Statement then
1028 Analyze_Call (N);
1029 Resolve (N, Standard_Void_Type);
1030
1031 -- Apply checks suggested by AI05-0144
1032
1033 Check_Order_Dependence;
1034
1035 else
1036 Analyze (N);
1037 end if;
1038 end Analyze_Call_And_Resolve;
1039
1040 -- Start of processing for Analyze_Procedure_Call
1041
1042 begin
1043 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1044 -- a procedure call or an entry call. The prefix may denote an access
1045 -- to subprogram type, in which case an implicit dereference applies.
1046 -- If the prefix is an indexed component (without implicit dereference)
1047 -- then the construct denotes a call to a member of an entire family.
1048 -- If the prefix is a simple name, it may still denote a call to a
1049 -- parameterless member of an entry family. Resolution of these various
1050 -- interpretations is delicate.
1051
1052 Analyze (P);
1053
1054 -- If this is a call of the form Obj.Op, the call may have been
1055 -- analyzed and possibly rewritten into a block, in which case
1056 -- we are done.
1057
1058 if Analyzed (N) then
1059 return;
1060 end if;
1061
1062 -- If there is an error analyzing the name (which may have been
1063 -- rewritten if the original call was in prefix notation) then error
1064 -- has been emitted already, mark node and return.
1065
1066 if Error_Posted (N)
1067 or else Etype (Name (N)) = Any_Type
1068 then
1069 Set_Etype (N, Any_Type);
1070 return;
1071 end if;
1072
1073 -- Otherwise analyze the parameters
1074
1075 if Present (Actuals) then
1076 Actual := First (Actuals);
1077
1078 while Present (Actual) loop
1079 Analyze (Actual);
1080 Check_Parameterless_Call (Actual);
1081 Next (Actual);
1082 end loop;
1083 end if;
1084
1085 -- Special processing for Elab_Spec and Elab_Body calls
1086
1087 if Nkind (P) = N_Attribute_Reference
1088 and then (Attribute_Name (P) = Name_Elab_Spec
1089 or else Attribute_Name (P) = Name_Elab_Body)
1090 then
1091 if Present (Actuals) then
1092 Error_Msg_N
1093 ("no parameters allowed for this call", First (Actuals));
1094 return;
1095 end if;
1096
1097 Set_Etype (N, Standard_Void_Type);
1098 Set_Analyzed (N);
1099
1100 elsif Is_Entity_Name (P)
1101 and then Is_Record_Type (Etype (Entity (P)))
1102 and then Remote_AST_I_Dereference (P)
1103 then
1104 return;
1105
1106 elsif Is_Entity_Name (P)
1107 and then Ekind (Entity (P)) /= E_Entry_Family
1108 then
1109 if Is_Access_Type (Etype (P))
1110 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1111 and then No (Actuals)
1112 and then Comes_From_Source (N)
1113 then
1114 Error_Msg_N ("missing explicit dereference in call", N);
1115 end if;
1116
1117 Analyze_Call_And_Resolve;
1118
1119 -- If the prefix is the simple name of an entry family, this is
1120 -- a parameterless call from within the task body itself.
1121
1122 elsif Is_Entity_Name (P)
1123 and then Nkind (P) = N_Identifier
1124 and then Ekind (Entity (P)) = E_Entry_Family
1125 and then Present (Actuals)
1126 and then No (Next (First (Actuals)))
1127 then
1128 -- Can be call to parameterless entry family. What appears to be the
1129 -- sole argument is in fact the entry index. Rewrite prefix of node
1130 -- accordingly. Source representation is unchanged by this
1131 -- transformation.
1132
1133 New_N :=
1134 Make_Indexed_Component (Loc,
1135 Prefix =>
1136 Make_Selected_Component (Loc,
1137 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1138 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1139 Expressions => Actuals);
1140 Set_Name (N, New_N);
1141 Set_Etype (New_N, Standard_Void_Type);
1142 Set_Parameter_Associations (N, No_List);
1143 Analyze_Call_And_Resolve;
1144
1145 elsif Nkind (P) = N_Explicit_Dereference then
1146 if Ekind (Etype (P)) = E_Subprogram_Type then
1147 Analyze_Call_And_Resolve;
1148 else
1149 Error_Msg_N ("expect access to procedure in call", P);
1150 end if;
1151
1152 -- The name can be a selected component or an indexed component that
1153 -- yields an access to subprogram. Such a prefix is legal if the call
1154 -- has parameter associations.
1155
1156 elsif Is_Access_Type (Etype (P))
1157 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1158 then
1159 if Present (Actuals) then
1160 Analyze_Call_And_Resolve;
1161 else
1162 Error_Msg_N ("missing explicit dereference in call ", N);
1163 end if;
1164
1165 -- If not an access to subprogram, then the prefix must resolve to the
1166 -- name of an entry, entry family, or protected operation.
1167
1168 -- For the case of a simple entry call, P is a selected component where
1169 -- the prefix is the task and the selector name is the entry. A call to
1170 -- a protected procedure will have the same syntax. If the protected
1171 -- object contains overloaded operations, the entity may appear as a
1172 -- function, the context will select the operation whose type is Void.
1173
1174 elsif Nkind (P) = N_Selected_Component
1175 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1176 or else
1177 Ekind (Entity (Selector_Name (P))) = E_Procedure
1178 or else
1179 Ekind (Entity (Selector_Name (P))) = E_Function)
1180 then
1181 Analyze_Call_And_Resolve;
1182
1183 elsif Nkind (P) = N_Selected_Component
1184 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1185 and then Present (Actuals)
1186 and then No (Next (First (Actuals)))
1187 then
1188 -- Can be call to parameterless entry family. What appears to be the
1189 -- sole argument is in fact the entry index. Rewrite prefix of node
1190 -- accordingly. Source representation is unchanged by this
1191 -- transformation.
1192
1193 New_N :=
1194 Make_Indexed_Component (Loc,
1195 Prefix => New_Copy (P),
1196 Expressions => Actuals);
1197 Set_Name (N, New_N);
1198 Set_Etype (New_N, Standard_Void_Type);
1199 Set_Parameter_Associations (N, No_List);
1200 Analyze_Call_And_Resolve;
1201
1202 -- For the case of a reference to an element of an entry family, P is
1203 -- an indexed component whose prefix is a selected component (task and
1204 -- entry family), and whose index is the entry family index.
1205
1206 elsif Nkind (P) = N_Indexed_Component
1207 and then Nkind (Prefix (P)) = N_Selected_Component
1208 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1209 then
1210 Analyze_Call_And_Resolve;
1211
1212 -- If the prefix is the name of an entry family, it is a call from
1213 -- within the task body itself.
1214
1215 elsif Nkind (P) = N_Indexed_Component
1216 and then Nkind (Prefix (P)) = N_Identifier
1217 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1218 then
1219 New_N :=
1220 Make_Selected_Component (Loc,
1221 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1222 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1223 Rewrite (Prefix (P), New_N);
1224 Analyze (P);
1225 Analyze_Call_And_Resolve;
1226
1227 -- Anything else is an error
1228
1229 else
1230 Error_Msg_N ("invalid procedure or entry call", N);
1231 end if;
1232 end Analyze_Procedure_Call;
1233
1234 ------------------------------
1235 -- Analyze_Return_Statement --
1236 ------------------------------
1237
1238 procedure Analyze_Return_Statement (N : Node_Id) is
1239
1240 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1241 N_Extended_Return_Statement));
1242
1243 Returns_Object : constant Boolean :=
1244 Nkind (N) = N_Extended_Return_Statement
1245 or else
1246 (Nkind (N) = N_Simple_Return_Statement
1247 and then Present (Expression (N)));
1248 -- True if we're returning something; that is, "return <expression>;"
1249 -- or "return Result : T [:= ...]". False for "return;". Used for error
1250 -- checking: If Returns_Object is True, N should apply to a function
1251 -- body; otherwise N should apply to a procedure body, entry body,
1252 -- accept statement, or extended return statement.
1253
1254 function Find_What_It_Applies_To return Entity_Id;
1255 -- Find the entity representing the innermost enclosing body, accept
1256 -- statement, or extended return statement. If the result is a callable
1257 -- construct or extended return statement, then this will be the value
1258 -- of the Return_Applies_To attribute. Otherwise, the program is
1259 -- illegal. See RM-6.5(4/2).
1260
1261 -----------------------------
1262 -- Find_What_It_Applies_To --
1263 -----------------------------
1264
1265 function Find_What_It_Applies_To return Entity_Id is
1266 Result : Entity_Id := Empty;
1267
1268 begin
1269 -- Loop outward through the Scope_Stack, skipping blocks and loops
1270
1271 for J in reverse 0 .. Scope_Stack.Last loop
1272 Result := Scope_Stack.Table (J).Entity;
1273 exit when Ekind (Result) /= E_Block and then
1274 Ekind (Result) /= E_Loop;
1275 end loop;
1276
1277 pragma Assert (Present (Result));
1278 return Result;
1279 end Find_What_It_Applies_To;
1280
1281 -- Local declarations
1282
1283 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1284 Kind : constant Entity_Kind := Ekind (Scope_Id);
1285 Loc : constant Source_Ptr := Sloc (N);
1286 Stm_Entity : constant Entity_Id :=
1287 New_Internal_Entity
1288 (E_Return_Statement, Current_Scope, Loc, 'R');
1289
1290 -- Start of processing for Analyze_Return_Statement
1291
1292 begin
1293 Set_Return_Statement_Entity (N, Stm_Entity);
1294
1295 Set_Etype (Stm_Entity, Standard_Void_Type);
1296 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1297
1298 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1299 -- (4/2): an inner return statement will apply to this extended return.
1300
1301 if Nkind (N) = N_Extended_Return_Statement then
1302 Push_Scope (Stm_Entity);
1303 end if;
1304
1305 -- Check that pragma No_Return is obeyed. Don't complain about the
1306 -- implicitly-generated return that is placed at the end.
1307
1308 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1309 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1310 end if;
1311
1312 -- Warn on any unassigned OUT parameters if in procedure
1313
1314 if Ekind (Scope_Id) = E_Procedure then
1315 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1316 end if;
1317
1318 -- Check that functions return objects, and other things do not
1319
1320 if Kind = E_Function or else Kind = E_Generic_Function then
1321 if not Returns_Object then
1322 Error_Msg_N ("missing expression in return from function", N);
1323 end if;
1324
1325 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1326 if Returns_Object then
1327 Error_Msg_N ("procedure cannot return value (use function)", N);
1328 end if;
1329
1330 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1331 if Returns_Object then
1332 if Is_Protected_Type (Scope (Scope_Id)) then
1333 Error_Msg_N ("entry body cannot return value", N);
1334 else
1335 Error_Msg_N ("accept statement cannot return value", N);
1336 end if;
1337 end if;
1338
1339 elsif Kind = E_Return_Statement then
1340
1341 -- We are nested within another return statement, which must be an
1342 -- extended_return_statement.
1343
1344 if Returns_Object then
1345 Error_Msg_N
1346 ("extended_return_statement cannot return value; " &
1347 "use `""RETURN;""`", N);
1348 end if;
1349
1350 else
1351 Error_Msg_N ("illegal context for return statement", N);
1352 end if;
1353
1354 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1355 Analyze_Function_Return (N);
1356
1357 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1358 Set_Return_Present (Scope_Id);
1359 end if;
1360
1361 if Nkind (N) = N_Extended_Return_Statement then
1362 End_Scope;
1363 end if;
1364
1365 Kill_Current_Values (Last_Assignment_Only => True);
1366 Check_Unreachable_Code (N);
1367 end Analyze_Return_Statement;
1368
1369 -------------------------------------
1370 -- Analyze_Simple_Return_Statement --
1371 -------------------------------------
1372
1373 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1374 begin
1375 if Present (Expression (N)) then
1376 Mark_Coextensions (N, Expression (N));
1377 end if;
1378
1379 Analyze_Return_Statement (N);
1380 end Analyze_Simple_Return_Statement;
1381
1382 -------------------------
1383 -- Analyze_Return_Type --
1384 -------------------------
1385
1386 procedure Analyze_Return_Type (N : Node_Id) is
1387 Designator : constant Entity_Id := Defining_Entity (N);
1388 Typ : Entity_Id := Empty;
1389
1390 begin
1391 -- Normal case where result definition does not indicate an error
1392
1393 if Result_Definition (N) /= Error then
1394 if Nkind (Result_Definition (N)) = N_Access_Definition then
1395
1396 Check_Formal_Restriction
1397 ("access result is not allowed", Result_Definition (N));
1398
1399 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1400
1401 declare
1402 AD : constant Node_Id :=
1403 Access_To_Subprogram_Definition (Result_Definition (N));
1404 begin
1405 if Present (AD) and then Protected_Present (AD) then
1406 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1407 else
1408 Typ := Access_Definition (N, Result_Definition (N));
1409 end if;
1410 end;
1411
1412 Set_Parent (Typ, Result_Definition (N));
1413 Set_Is_Local_Anonymous_Access (Typ);
1414 Set_Etype (Designator, Typ);
1415
1416 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1417
1418 Null_Exclusion_Static_Checks (N);
1419
1420 -- Subtype_Mark case
1421
1422 else
1423 Find_Type (Result_Definition (N));
1424 Typ := Entity (Result_Definition (N));
1425 Set_Etype (Designator, Typ);
1426
1427 -- Unconstrained array as result is not allowed in SPARK or ALFA
1428
1429 if Is_Array_Type (Typ)
1430 and then not Is_Constrained (Typ)
1431 then
1432 Check_Formal_Restriction
1433 ("returning an unconstrained array is not allowed",
1434 Result_Definition (N));
1435 end if;
1436
1437 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1438
1439 Null_Exclusion_Static_Checks (N);
1440
1441 -- If a null exclusion is imposed on the result type, then create
1442 -- a null-excluding itype (an access subtype) and use it as the
1443 -- function's Etype. Note that the null exclusion checks are done
1444 -- right before this, because they don't get applied to types that
1445 -- do not come from source.
1446
1447 if Is_Access_Type (Typ)
1448 and then Null_Exclusion_Present (N)
1449 then
1450 Set_Etype (Designator,
1451 Create_Null_Excluding_Itype
1452 (T => Typ,
1453 Related_Nod => N,
1454 Scope_Id => Scope (Current_Scope)));
1455
1456 -- The new subtype must be elaborated before use because
1457 -- it is visible outside of the function. However its base
1458 -- type may not be frozen yet, so the reference that will
1459 -- force elaboration must be attached to the freezing of
1460 -- the base type.
1461
1462 -- If the return specification appears on a proper body,
1463 -- the subtype will have been created already on the spec.
1464
1465 if Is_Frozen (Typ) then
1466 if Nkind (Parent (N)) = N_Subprogram_Body
1467 and then Nkind (Parent (Parent (N))) = N_Subunit
1468 then
1469 null;
1470 else
1471 Build_Itype_Reference (Etype (Designator), Parent (N));
1472 end if;
1473
1474 else
1475 Ensure_Freeze_Node (Typ);
1476
1477 declare
1478 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1479 begin
1480 Set_Itype (IR, Etype (Designator));
1481 Append_Freeze_Actions (Typ, New_List (IR));
1482 end;
1483 end if;
1484
1485 else
1486 Set_Etype (Designator, Typ);
1487 end if;
1488
1489 if Ekind (Typ) = E_Incomplete_Type
1490 and then Is_Value_Type (Typ)
1491 then
1492 null;
1493
1494 elsif Ekind (Typ) = E_Incomplete_Type
1495 or else (Is_Class_Wide_Type (Typ)
1496 and then
1497 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1498 then
1499 -- AI05-0151: Tagged incomplete types are allowed in all formal
1500 -- parts. Untagged incomplete types are not allowed in bodies.
1501
1502 if Ada_Version >= Ada_2012 then
1503 if Is_Tagged_Type (Typ) then
1504 null;
1505
1506 elsif Nkind_In (Parent (Parent (N)),
1507 N_Accept_Statement,
1508 N_Entry_Body,
1509 N_Subprogram_Body)
1510 then
1511 Error_Msg_NE
1512 ("invalid use of untagged incomplete type&",
1513 Designator, Typ);
1514 end if;
1515
1516 else
1517 Error_Msg_NE
1518 ("invalid use of incomplete type&", Designator, Typ);
1519 end if;
1520 end if;
1521 end if;
1522
1523 -- Case where result definition does indicate an error
1524
1525 else
1526 Set_Etype (Designator, Any_Type);
1527 end if;
1528 end Analyze_Return_Type;
1529
1530 -----------------------------
1531 -- Analyze_Subprogram_Body --
1532 -----------------------------
1533
1534 procedure Analyze_Subprogram_Body (N : Node_Id) is
1535 Loc : constant Source_Ptr := Sloc (N);
1536 Body_Spec : constant Node_Id := Specification (N);
1537 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1538
1539 begin
1540 if Debug_Flag_C then
1541 Write_Str ("==> subprogram body ");
1542 Write_Name (Chars (Body_Id));
1543 Write_Str (" from ");
1544 Write_Location (Loc);
1545 Write_Eol;
1546 Indent;
1547 end if;
1548
1549 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1550
1551 -- The real work is split out into the helper, so it can do "return;"
1552 -- without skipping the debug output:
1553
1554 Analyze_Subprogram_Body_Helper (N);
1555
1556 if Debug_Flag_C then
1557 Outdent;
1558 Write_Str ("<== subprogram body ");
1559 Write_Name (Chars (Body_Id));
1560 Write_Str (" from ");
1561 Write_Location (Loc);
1562 Write_Eol;
1563 end if;
1564 end Analyze_Subprogram_Body;
1565
1566 ------------------------------------
1567 -- Analyze_Subprogram_Body_Helper --
1568 ------------------------------------
1569
1570 -- This procedure is called for regular subprogram bodies, generic bodies,
1571 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1572 -- specification matters, and is used to create a proper declaration for
1573 -- the subprogram, or to perform conformance checks.
1574
1575 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
1576 Loc : constant Source_Ptr := Sloc (N);
1577 Body_Deleted : constant Boolean := False;
1578 Body_Spec : constant Node_Id := Specification (N);
1579 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1580 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1581 Conformant : Boolean;
1582 HSS : Node_Id;
1583 P_Ent : Entity_Id;
1584 Prot_Typ : Entity_Id := Empty;
1585 Spec_Id : Entity_Id;
1586 Spec_Decl : Node_Id := Empty;
1587
1588 Last_Real_Spec_Entity : Entity_Id := Empty;
1589 -- When we analyze a separate spec, the entity chain ends up containing
1590 -- the formals, as well as any itypes generated during analysis of the
1591 -- default expressions for parameters, or the arguments of associated
1592 -- precondition/postcondition pragmas (which are analyzed in the context
1593 -- of the spec since they have visibility on formals).
1594 --
1595 -- These entities belong with the spec and not the body. However we do
1596 -- the analysis of the body in the context of the spec (again to obtain
1597 -- visibility to the formals), and all the entities generated during
1598 -- this analysis end up also chained to the entity chain of the spec.
1599 -- But they really belong to the body, and there is circuitry to move
1600 -- them from the spec to the body.
1601 --
1602 -- However, when we do this move, we don't want to move the real spec
1603 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1604 -- variable points to the last real spec entity, so we only move those
1605 -- chained beyond that point. It is initialized to Empty to deal with
1606 -- the case where there is no separate spec.
1607
1608 procedure Check_Anonymous_Return;
1609 -- Ada 2005: if a function returns an access type that denotes a task,
1610 -- or a type that contains tasks, we must create a master entity for
1611 -- the anonymous type, which typically will be used in an allocator
1612 -- in the body of the function.
1613
1614 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1615 -- Look ahead to recognize a pragma that may appear after the body.
1616 -- If there is a previous spec, check that it appears in the same
1617 -- declarative part. If the pragma is Inline_Always, perform inlining
1618 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1619 -- If the body acts as a spec, and inlining is required, we create a
1620 -- subprogram declaration for it, in order to attach the body to inline.
1621 -- If pragma does not appear after the body, check whether there is
1622 -- an inline pragma before any local declarations.
1623
1624 procedure Check_Missing_Return;
1625 -- Checks for a function with a no return statements, and also performs
1626 -- the warning checks implemented by Check_Returns. In formal mode, also
1627 -- verify that a function ends with a RETURN and that a procedure does
1628 -- not contain any RETURN.
1629
1630 function Disambiguate_Spec return Entity_Id;
1631 -- When a primitive is declared between the private view and the full
1632 -- view of a concurrent type which implements an interface, a special
1633 -- mechanism is used to find the corresponding spec of the primitive
1634 -- body.
1635
1636 function Is_Private_Concurrent_Primitive
1637 (Subp_Id : Entity_Id) return Boolean;
1638 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
1639 -- type that implements an interface and has a private view.
1640
1641 procedure Set_Trivial_Subprogram (N : Node_Id);
1642 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1643 -- subprogram whose body is being analyzed. N is the statement node
1644 -- causing the flag to be set, if the following statement is a return
1645 -- of an entity, we mark the entity as set in source to suppress any
1646 -- warning on the stylized use of function stubs with a dummy return.
1647
1648 procedure Verify_Overriding_Indicator;
1649 -- If there was a previous spec, the entity has been entered in the
1650 -- current scope previously. If the body itself carries an overriding
1651 -- indicator, check that it is consistent with the known status of the
1652 -- entity.
1653
1654 ----------------------------
1655 -- Check_Anonymous_Return --
1656 ----------------------------
1657
1658 procedure Check_Anonymous_Return is
1659 Decl : Node_Id;
1660 Par : Node_Id;
1661 Scop : Entity_Id;
1662
1663 begin
1664 if Present (Spec_Id) then
1665 Scop := Spec_Id;
1666 else
1667 Scop := Body_Id;
1668 end if;
1669
1670 if Ekind (Scop) = E_Function
1671 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1672 and then not Is_Thunk (Scop)
1673 and then (Has_Task (Designated_Type (Etype (Scop)))
1674 or else
1675 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
1676 and then
1677 Is_Limited_Record (Designated_Type (Etype (Scop)))))
1678 and then Expander_Active
1679
1680 -- Avoid cases with no tasking support
1681
1682 and then RTE_Available (RE_Current_Master)
1683 and then not Restriction_Active (No_Task_Hierarchy)
1684 then
1685 Decl :=
1686 Make_Object_Declaration (Loc,
1687 Defining_Identifier =>
1688 Make_Defining_Identifier (Loc, Name_uMaster),
1689 Constant_Present => True,
1690 Object_Definition =>
1691 New_Reference_To (RTE (RE_Master_Id), Loc),
1692 Expression =>
1693 Make_Explicit_Dereference (Loc,
1694 New_Reference_To (RTE (RE_Current_Master), Loc)));
1695
1696 if Present (Declarations (N)) then
1697 Prepend (Decl, Declarations (N));
1698 else
1699 Set_Declarations (N, New_List (Decl));
1700 end if;
1701
1702 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1703 Set_Has_Master_Entity (Scop);
1704
1705 -- Now mark the containing scope as a task master
1706
1707 Par := N;
1708 while Nkind (Par) /= N_Compilation_Unit loop
1709 Par := Parent (Par);
1710 pragma Assert (Present (Par));
1711
1712 -- If we fall off the top, we are at the outer level, and
1713 -- the environment task is our effective master, so nothing
1714 -- to mark.
1715
1716 if Nkind_In
1717 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
1718 then
1719 Set_Is_Task_Master (Par, True);
1720 exit;
1721 end if;
1722 end loop;
1723 end if;
1724 end Check_Anonymous_Return;
1725
1726 -------------------------
1727 -- Check_Inline_Pragma --
1728 -------------------------
1729
1730 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1731 Prag : Node_Id;
1732 Plist : List_Id;
1733
1734 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1735 -- True when N is a pragma Inline or Inline_Always that applies
1736 -- to this subprogram.
1737
1738 -----------------------
1739 -- Is_Inline_Pragma --
1740 -----------------------
1741
1742 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1743 begin
1744 return
1745 Nkind (N) = N_Pragma
1746 and then
1747 (Pragma_Name (N) = Name_Inline_Always
1748 or else
1749 (Front_End_Inlining
1750 and then Pragma_Name (N) = Name_Inline))
1751 and then
1752 Chars
1753 (Expression (First (Pragma_Argument_Associations (N))))
1754 = Chars (Body_Id);
1755 end Is_Inline_Pragma;
1756
1757 -- Start of processing for Check_Inline_Pragma
1758
1759 begin
1760 if not Expander_Active then
1761 return;
1762 end if;
1763
1764 if Is_List_Member (N)
1765 and then Present (Next (N))
1766 and then Is_Inline_Pragma (Next (N))
1767 then
1768 Prag := Next (N);
1769
1770 elsif Nkind (N) /= N_Subprogram_Body_Stub
1771 and then Present (Declarations (N))
1772 and then Is_Inline_Pragma (First (Declarations (N)))
1773 then
1774 Prag := First (Declarations (N));
1775
1776 else
1777 Prag := Empty;
1778 end if;
1779
1780 if Present (Prag) then
1781 if Present (Spec_Id) then
1782 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
1783 Analyze (Prag);
1784 end if;
1785
1786 else
1787 -- Create a subprogram declaration, to make treatment uniform
1788
1789 declare
1790 Subp : constant Entity_Id :=
1791 Make_Defining_Identifier (Loc, Chars (Body_Id));
1792 Decl : constant Node_Id :=
1793 Make_Subprogram_Declaration (Loc,
1794 Specification =>
1795 New_Copy_Tree (Specification (N)));
1796
1797 begin
1798 Set_Defining_Unit_Name (Specification (Decl), Subp);
1799
1800 if Present (First_Formal (Body_Id)) then
1801 Plist := Copy_Parameter_List (Body_Id);
1802 Set_Parameter_Specifications
1803 (Specification (Decl), Plist);
1804 end if;
1805
1806 Insert_Before (N, Decl);
1807 Analyze (Decl);
1808 Analyze (Prag);
1809 Set_Has_Pragma_Inline (Subp);
1810
1811 if Pragma_Name (Prag) = Name_Inline_Always then
1812 Set_Is_Inlined (Subp);
1813 Set_Has_Pragma_Inline_Always (Subp);
1814 end if;
1815
1816 Spec := Subp;
1817 end;
1818 end if;
1819 end if;
1820 end Check_Inline_Pragma;
1821
1822 --------------------------
1823 -- Check_Missing_Return --
1824 --------------------------
1825
1826 procedure Check_Missing_Return is
1827 Id : Entity_Id;
1828 Missing_Ret : Boolean;
1829
1830 begin
1831 if Nkind (Body_Spec) = N_Function_Specification then
1832 if Present (Spec_Id) then
1833 Id := Spec_Id;
1834 else
1835 Id := Body_Id;
1836 end if;
1837
1838 if Return_Present (Id) then
1839 Check_Returns (HSS, 'F', Missing_Ret);
1840
1841 if Missing_Ret then
1842 Set_Has_Missing_Return (Id);
1843 end if;
1844
1845 elsif (Is_Generic_Subprogram (Id)
1846 or else not Is_Machine_Code_Subprogram (Id))
1847 and then not Body_Deleted
1848 then
1849 Error_Msg_N ("missing RETURN statement in function body", N);
1850 end if;
1851
1852 -- If procedure with No_Return, check returns
1853
1854 elsif Nkind (Body_Spec) = N_Procedure_Specification
1855 and then Present (Spec_Id)
1856 and then No_Return (Spec_Id)
1857 then
1858 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
1859 end if;
1860
1861 -- Special checks in formal mode
1862
1863 if Nkind (Body_Spec) = N_Function_Specification then
1864 -- In formal mode, the last statement of a function should be a
1865 -- return statement.
1866
1867 declare
1868 Stat : constant Node_Id := Last_Source_Statement (HSS);
1869 begin
1870 if Present (Stat)
1871 and then not Nkind_In (Stat,
1872 N_Simple_Return_Statement,
1873 N_Extended_Return_Statement)
1874 then
1875 Check_Formal_Restriction
1876 ("last statement in function should be RETURN", Stat);
1877 end if;
1878 end;
1879
1880 -- In formal mode, verify that a procedure has no return
1881
1882 elsif Nkind (Body_Spec) = N_Procedure_Specification then
1883 if Present (Spec_Id) then
1884 Id := Spec_Id;
1885 else
1886 Id := Body_Id;
1887 end if;
1888
1889 -- Would be nice to point to return statement here, can we
1890 -- borrow the Check_Returns procedure here ???
1891
1892 if Return_Present (Id) then
1893 Check_Formal_Restriction
1894 ("procedure should not have RETURN", N);
1895 end if;
1896 end if;
1897 end Check_Missing_Return;
1898
1899 -----------------------
1900 -- Disambiguate_Spec --
1901 -----------------------
1902
1903 function Disambiguate_Spec return Entity_Id is
1904 Priv_Spec : Entity_Id;
1905 Spec_N : Entity_Id;
1906
1907 procedure Replace_Types (To_Corresponding : Boolean);
1908 -- Depending on the flag, replace the type of formal parameters of
1909 -- Body_Id if it is a concurrent type implementing interfaces with
1910 -- the corresponding record type or the other way around.
1911
1912 procedure Replace_Types (To_Corresponding : Boolean) is
1913 Formal : Entity_Id;
1914 Formal_Typ : Entity_Id;
1915
1916 begin
1917 Formal := First_Formal (Body_Id);
1918 while Present (Formal) loop
1919 Formal_Typ := Etype (Formal);
1920
1921 -- From concurrent type to corresponding record
1922
1923 if To_Corresponding then
1924 if Is_Concurrent_Type (Formal_Typ)
1925 and then Present (Corresponding_Record_Type (Formal_Typ))
1926 and then Present (Interfaces (
1927 Corresponding_Record_Type (Formal_Typ)))
1928 then
1929 Set_Etype (Formal,
1930 Corresponding_Record_Type (Formal_Typ));
1931 end if;
1932
1933 -- From corresponding record to concurrent type
1934
1935 else
1936 if Is_Concurrent_Record_Type (Formal_Typ)
1937 and then Present (Interfaces (Formal_Typ))
1938 then
1939 Set_Etype (Formal,
1940 Corresponding_Concurrent_Type (Formal_Typ));
1941 end if;
1942 end if;
1943
1944 Next_Formal (Formal);
1945 end loop;
1946 end Replace_Types;
1947
1948 -- Start of processing for Disambiguate_Spec
1949
1950 begin
1951 -- Try to retrieve the specification of the body as is. All error
1952 -- messages are suppressed because the body may not have a spec in
1953 -- its current state.
1954
1955 Spec_N := Find_Corresponding_Spec (N, False);
1956
1957 -- It is possible that this is the body of a primitive declared
1958 -- between a private and a full view of a concurrent type. The
1959 -- controlling parameter of the spec carries the concurrent type,
1960 -- not the corresponding record type as transformed by Analyze_
1961 -- Subprogram_Specification. In such cases, we undo the change
1962 -- made by the analysis of the specification and try to find the
1963 -- spec again.
1964
1965 -- Note that wrappers already have their corresponding specs and
1966 -- bodies set during their creation, so if the candidate spec is
1967 -- a wrapper, then we definitely need to swap all types to their
1968 -- original concurrent status.
1969
1970 if No (Spec_N)
1971 or else Is_Primitive_Wrapper (Spec_N)
1972 then
1973 -- Restore all references of corresponding record types to the
1974 -- original concurrent types.
1975
1976 Replace_Types (To_Corresponding => False);
1977 Priv_Spec := Find_Corresponding_Spec (N, False);
1978
1979 -- The current body truly belongs to a primitive declared between
1980 -- a private and a full view. We leave the modified body as is,
1981 -- and return the true spec.
1982
1983 if Present (Priv_Spec)
1984 and then Is_Private_Primitive (Priv_Spec)
1985 then
1986 return Priv_Spec;
1987 end if;
1988
1989 -- In case that this is some sort of error, restore the original
1990 -- state of the body.
1991
1992 Replace_Types (To_Corresponding => True);
1993 end if;
1994
1995 return Spec_N;
1996 end Disambiguate_Spec;
1997
1998 -------------------------------------
1999 -- Is_Private_Concurrent_Primitive --
2000 -------------------------------------
2001
2002 function Is_Private_Concurrent_Primitive
2003 (Subp_Id : Entity_Id) return Boolean
2004 is
2005 Formal_Typ : Entity_Id;
2006
2007 begin
2008 if Present (First_Formal (Subp_Id)) then
2009 Formal_Typ := Etype (First_Formal (Subp_Id));
2010
2011 if Is_Concurrent_Record_Type (Formal_Typ) then
2012 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2013 end if;
2014
2015 -- The type of the first formal is a concurrent tagged type with
2016 -- a private view.
2017
2018 return
2019 Is_Concurrent_Type (Formal_Typ)
2020 and then Is_Tagged_Type (Formal_Typ)
2021 and then Has_Private_Declaration (Formal_Typ);
2022 end if;
2023
2024 return False;
2025 end Is_Private_Concurrent_Primitive;
2026
2027 ----------------------------
2028 -- Set_Trivial_Subprogram --
2029 ----------------------------
2030
2031 procedure Set_Trivial_Subprogram (N : Node_Id) is
2032 Nxt : constant Node_Id := Next (N);
2033
2034 begin
2035 Set_Is_Trivial_Subprogram (Body_Id);
2036
2037 if Present (Spec_Id) then
2038 Set_Is_Trivial_Subprogram (Spec_Id);
2039 end if;
2040
2041 if Present (Nxt)
2042 and then Nkind (Nxt) = N_Simple_Return_Statement
2043 and then No (Next (Nxt))
2044 and then Present (Expression (Nxt))
2045 and then Is_Entity_Name (Expression (Nxt))
2046 then
2047 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2048 end if;
2049 end Set_Trivial_Subprogram;
2050
2051 ---------------------------------
2052 -- Verify_Overriding_Indicator --
2053 ---------------------------------
2054
2055 procedure Verify_Overriding_Indicator is
2056 begin
2057 if Must_Override (Body_Spec) then
2058 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2059 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2060 then
2061 null;
2062
2063 elsif not Present (Overridden_Operation (Spec_Id)) then
2064 Error_Msg_NE
2065 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2066 end if;
2067
2068 elsif Must_Not_Override (Body_Spec) then
2069 if Present (Overridden_Operation (Spec_Id)) then
2070 Error_Msg_NE
2071 ("subprogram& overrides inherited operation",
2072 Body_Spec, Spec_Id);
2073
2074 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2075 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2076 then
2077 Error_Msg_NE
2078 ("subprogram & overrides predefined operator ",
2079 Body_Spec, Spec_Id);
2080
2081 -- If this is not a primitive operation or protected subprogram,
2082 -- then the overriding indicator is altogether illegal.
2083
2084 elsif not Is_Primitive (Spec_Id)
2085 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2086 then
2087 Error_Msg_N
2088 ("overriding indicator only allowed " &
2089 "if subprogram is primitive",
2090 Body_Spec);
2091 end if;
2092
2093 elsif Style_Check
2094 and then Present (Overridden_Operation (Spec_Id))
2095 then
2096 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2097 Style.Missing_Overriding (N, Body_Id);
2098
2099 elsif Style_Check
2100 and then Can_Override_Operator (Spec_Id)
2101 and then not Is_Predefined_File_Name
2102 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2103 then
2104 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2105 Style.Missing_Overriding (N, Body_Id);
2106 end if;
2107 end Verify_Overriding_Indicator;
2108
2109 -- Start of processing for Analyze_Subprogram_Body_Helper
2110
2111 begin
2112 -- Generic subprograms are handled separately. They always have a
2113 -- generic specification. Determine whether current scope has a
2114 -- previous declaration.
2115
2116 -- If the subprogram body is defined within an instance of the same
2117 -- name, the instance appears as a package renaming, and will be hidden
2118 -- within the subprogram.
2119
2120 if Present (Prev_Id)
2121 and then not Is_Overloadable (Prev_Id)
2122 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2123 or else Comes_From_Source (Prev_Id))
2124 then
2125 if Is_Generic_Subprogram (Prev_Id) then
2126 Spec_Id := Prev_Id;
2127 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2128 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2129
2130 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2131
2132 if Nkind (N) = N_Subprogram_Body then
2133 HSS := Handled_Statement_Sequence (N);
2134 Check_Missing_Return;
2135 end if;
2136
2137 return;
2138
2139 else
2140 -- Previous entity conflicts with subprogram name. Attempting to
2141 -- enter name will post error.
2142
2143 Enter_Name (Body_Id);
2144 return;
2145 end if;
2146
2147 -- Non-generic case, find the subprogram declaration, if one was seen,
2148 -- or enter new overloaded entity in the current scope. If the
2149 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2150 -- part of the context of one of its subunits. No need to redo the
2151 -- analysis.
2152
2153 elsif Prev_Id = Body_Id
2154 and then Has_Completion (Body_Id)
2155 then
2156 return;
2157
2158 else
2159 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2160
2161 if Nkind (N) = N_Subprogram_Body_Stub
2162 or else No (Corresponding_Spec (N))
2163 then
2164 if Is_Private_Concurrent_Primitive (Body_Id) then
2165 Spec_Id := Disambiguate_Spec;
2166 else
2167 Spec_Id := Find_Corresponding_Spec (N);
2168 end if;
2169
2170 -- If this is a duplicate body, no point in analyzing it
2171
2172 if Error_Posted (N) then
2173 return;
2174 end if;
2175
2176 -- A subprogram body should cause freezing of its own declaration,
2177 -- but if there was no previous explicit declaration, then the
2178 -- subprogram will get frozen too late (there may be code within
2179 -- the body that depends on the subprogram having been frozen,
2180 -- such as uses of extra formals), so we force it to be frozen
2181 -- here. Same holds if the body and spec are compilation units.
2182 -- Finally, if the return type is an anonymous access to protected
2183 -- subprogram, it must be frozen before the body because its
2184 -- expansion has generated an equivalent type that is used when
2185 -- elaborating the body.
2186
2187 if No (Spec_Id) then
2188 Freeze_Before (N, Body_Id);
2189
2190 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2191 Freeze_Before (N, Spec_Id);
2192
2193 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2194 Freeze_Before (N, Etype (Body_Id));
2195 end if;
2196
2197 else
2198 Spec_Id := Corresponding_Spec (N);
2199 end if;
2200 end if;
2201
2202 -- Do not inline any subprogram that contains nested subprograms, since
2203 -- the backend inlining circuit seems to generate uninitialized
2204 -- references in this case. We know this happens in the case of front
2205 -- end ZCX support, but it also appears it can happen in other cases as
2206 -- well. The backend often rejects attempts to inline in the case of
2207 -- nested procedures anyway, so little if anything is lost by this.
2208 -- Note that this is test is for the benefit of the back-end. There is
2209 -- a separate test for front-end inlining that also rejects nested
2210 -- subprograms.
2211
2212 -- Do not do this test if errors have been detected, because in some
2213 -- error cases, this code blows up, and we don't need it anyway if
2214 -- there have been errors, since we won't get to the linker anyway.
2215
2216 if Comes_From_Source (Body_Id)
2217 and then Serious_Errors_Detected = 0
2218 then
2219 P_Ent := Body_Id;
2220 loop
2221 P_Ent := Scope (P_Ent);
2222 exit when No (P_Ent) or else P_Ent = Standard_Standard;
2223
2224 if Is_Subprogram (P_Ent) then
2225 Set_Is_Inlined (P_Ent, False);
2226
2227 if Comes_From_Source (P_Ent)
2228 and then Has_Pragma_Inline (P_Ent)
2229 then
2230 Cannot_Inline
2231 ("cannot inline& (nested subprogram)?",
2232 N, P_Ent);
2233 end if;
2234 end if;
2235 end loop;
2236 end if;
2237
2238 Check_Inline_Pragma (Spec_Id);
2239
2240 -- Deal with special case of a fully private operation in the body of
2241 -- the protected type. We must create a declaration for the subprogram,
2242 -- in order to attach the protected subprogram that will be used in
2243 -- internal calls. We exclude compiler generated bodies from the
2244 -- expander since the issue does not arise for those cases.
2245
2246 if No (Spec_Id)
2247 and then Comes_From_Source (N)
2248 and then Is_Protected_Type (Current_Scope)
2249 then
2250 Spec_Id := Build_Private_Protected_Declaration (N);
2251 end if;
2252
2253 -- If a separate spec is present, then deal with freezing issues
2254
2255 if Present (Spec_Id) then
2256 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2257 Verify_Overriding_Indicator;
2258
2259 -- In general, the spec will be frozen when we start analyzing the
2260 -- body. However, for internally generated operations, such as
2261 -- wrapper functions for inherited operations with controlling
2262 -- results, the spec may not have been frozen by the time we
2263 -- expand the freeze actions that include the bodies. In particular,
2264 -- extra formals for accessibility or for return-in-place may need
2265 -- to be generated. Freeze nodes, if any, are inserted before the
2266 -- current body.
2267
2268 if not Is_Frozen (Spec_Id)
2269 and then Expander_Active
2270 then
2271 -- Force the generation of its freezing node to ensure proper
2272 -- management of access types in the backend.
2273
2274 -- This is definitely needed for some cases, but it is not clear
2275 -- why, to be investigated further???
2276
2277 Set_Has_Delayed_Freeze (Spec_Id);
2278 Freeze_Before (N, Spec_Id);
2279 end if;
2280 end if;
2281
2282 -- Mark presence of postcondition procedure in current scope and mark
2283 -- the procedure itself as needing debug info. The latter is important
2284 -- when analyzing decision coverage (for example, for MC/DC coverage).
2285
2286 if Chars (Body_Id) = Name_uPostconditions then
2287 Set_Has_Postconditions (Current_Scope);
2288 Set_Debug_Info_Needed (Body_Id);
2289 end if;
2290
2291 -- Place subprogram on scope stack, and make formals visible. If there
2292 -- is a spec, the visible entity remains that of the spec.
2293
2294 if Present (Spec_Id) then
2295 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2296
2297 if Is_Child_Unit (Spec_Id) then
2298 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2299 end if;
2300
2301 if Style_Check then
2302 Style.Check_Identifier (Body_Id, Spec_Id);
2303 end if;
2304
2305 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2306 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2307
2308 if Is_Abstract_Subprogram (Spec_Id) then
2309 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2310 return;
2311
2312 else
2313 Set_Convention (Body_Id, Convention (Spec_Id));
2314 Set_Has_Completion (Spec_Id);
2315
2316 if Is_Protected_Type (Scope (Spec_Id)) then
2317 Prot_Typ := Scope (Spec_Id);
2318 end if;
2319
2320 -- If this is a body generated for a renaming, do not check for
2321 -- full conformance. The check is redundant, because the spec of
2322 -- the body is a copy of the spec in the renaming declaration,
2323 -- and the test can lead to spurious errors on nested defaults.
2324
2325 if Present (Spec_Decl)
2326 and then not Comes_From_Source (N)
2327 and then
2328 (Nkind (Original_Node (Spec_Decl)) =
2329 N_Subprogram_Renaming_Declaration
2330 or else (Present (Corresponding_Body (Spec_Decl))
2331 and then
2332 Nkind (Unit_Declaration_Node
2333 (Corresponding_Body (Spec_Decl))) =
2334 N_Subprogram_Renaming_Declaration))
2335 then
2336 Conformant := True;
2337
2338 -- Conversely, the spec may have been generated for specless body
2339 -- with an inline pragma.
2340
2341 elsif Comes_From_Source (N)
2342 and then not Comes_From_Source (Spec_Id)
2343 and then Has_Pragma_Inline (Spec_Id)
2344 then
2345 Conformant := True;
2346
2347 else
2348 Check_Conformance
2349 (Body_Id, Spec_Id,
2350 Fully_Conformant, True, Conformant, Body_Id);
2351 end if;
2352
2353 -- If the body is not fully conformant, we have to decide if we
2354 -- should analyze it or not. If it has a really messed up profile
2355 -- then we probably should not analyze it, since we will get too
2356 -- many bogus messages.
2357
2358 -- Our decision is to go ahead in the non-fully conformant case
2359 -- only if it is at least mode conformant with the spec. Note
2360 -- that the call to Check_Fully_Conformant has issued the proper
2361 -- error messages to complain about the lack of conformance.
2362
2363 if not Conformant
2364 and then not Mode_Conformant (Body_Id, Spec_Id)
2365 then
2366 return;
2367 end if;
2368 end if;
2369
2370 if Spec_Id /= Body_Id then
2371 Reference_Body_Formals (Spec_Id, Body_Id);
2372 end if;
2373
2374 if Nkind (N) /= N_Subprogram_Body_Stub then
2375 Set_Corresponding_Spec (N, Spec_Id);
2376
2377 -- Ada 2005 (AI-345): If the operation is a primitive operation
2378 -- of a concurrent type, the type of the first parameter has been
2379 -- replaced with the corresponding record, which is the proper
2380 -- run-time structure to use. However, within the body there may
2381 -- be uses of the formals that depend on primitive operations
2382 -- of the type (in particular calls in prefixed form) for which
2383 -- we need the original concurrent type. The operation may have
2384 -- several controlling formals, so the replacement must be done
2385 -- for all of them.
2386
2387 if Comes_From_Source (Spec_Id)
2388 and then Present (First_Entity (Spec_Id))
2389 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2390 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2391 and then
2392 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2393 and then
2394 Present
2395 (Corresponding_Concurrent_Type
2396 (Etype (First_Entity (Spec_Id))))
2397 then
2398 declare
2399 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2400 Form : Entity_Id;
2401
2402 begin
2403 Form := First_Formal (Spec_Id);
2404 while Present (Form) loop
2405 if Etype (Form) = Typ then
2406 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2407 end if;
2408
2409 Next_Formal (Form);
2410 end loop;
2411 end;
2412 end if;
2413
2414 -- Make the formals visible, and place subprogram on scope stack.
2415 -- This is also the point at which we set Last_Real_Spec_Entity
2416 -- to mark the entities which will not be moved to the body.
2417
2418 Install_Formals (Spec_Id);
2419 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2420 Push_Scope (Spec_Id);
2421
2422 -- Make sure that the subprogram is immediately visible. For
2423 -- child units that have no separate spec this is indispensable.
2424 -- Otherwise it is safe albeit redundant.
2425
2426 Set_Is_Immediately_Visible (Spec_Id);
2427 end if;
2428
2429 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2430 Set_Ekind (Body_Id, E_Subprogram_Body);
2431 Set_Scope (Body_Id, Scope (Spec_Id));
2432 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2433
2434 -- Case of subprogram body with no previous spec
2435
2436 else
2437 -- Check for style warning required
2438
2439 if Style_Check
2440
2441 -- Only apply check for source level subprograms for which checks
2442 -- have not been suppressed.
2443
2444 and then Comes_From_Source (Body_Id)
2445 and then not Suppress_Style_Checks (Body_Id)
2446
2447 -- No warnings within an instance
2448
2449 and then not In_Instance
2450
2451 -- No warnings for expression functions
2452
2453 and then Nkind (Original_Node (N)) /= N_Expression_Function
2454 then
2455 Style.Body_With_No_Spec (N);
2456 end if;
2457
2458 New_Overloaded_Entity (Body_Id);
2459
2460 if Nkind (N) /= N_Subprogram_Body_Stub then
2461 Set_Acts_As_Spec (N);
2462 Generate_Definition (Body_Id);
2463 Generate_Reference
2464 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2465 Generate_Reference_To_Formals (Body_Id);
2466 Install_Formals (Body_Id);
2467 Push_Scope (Body_Id);
2468 end if;
2469 end if;
2470
2471 -- If the return type is an anonymous access type whose designated type
2472 -- is the limited view of a class-wide type and the non-limited view is
2473 -- available, update the return type accordingly.
2474
2475 if Ada_Version >= Ada_2005
2476 and then Comes_From_Source (N)
2477 then
2478 declare
2479 Etyp : Entity_Id;
2480 Rtyp : Entity_Id;
2481
2482 begin
2483 Rtyp := Etype (Current_Scope);
2484
2485 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2486 Etyp := Directly_Designated_Type (Rtyp);
2487
2488 if Is_Class_Wide_Type (Etyp)
2489 and then From_With_Type (Etyp)
2490 then
2491 Set_Directly_Designated_Type
2492 (Etype (Current_Scope), Available_View (Etyp));
2493 end if;
2494 end if;
2495 end;
2496 end if;
2497
2498 -- If this is the proper body of a stub, we must verify that the stub
2499 -- conforms to the body, and to the previous spec if one was present.
2500 -- we know already that the body conforms to that spec. This test is
2501 -- only required for subprograms that come from source.
2502
2503 if Nkind (Parent (N)) = N_Subunit
2504 and then Comes_From_Source (N)
2505 and then not Error_Posted (Body_Id)
2506 and then Nkind (Corresponding_Stub (Parent (N))) =
2507 N_Subprogram_Body_Stub
2508 then
2509 declare
2510 Old_Id : constant Entity_Id :=
2511 Defining_Entity
2512 (Specification (Corresponding_Stub (Parent (N))));
2513
2514 Conformant : Boolean := False;
2515
2516 begin
2517 if No (Spec_Id) then
2518 Check_Fully_Conformant (Body_Id, Old_Id);
2519
2520 else
2521 Check_Conformance
2522 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
2523
2524 if not Conformant then
2525
2526 -- The stub was taken to be a new declaration. Indicate
2527 -- that it lacks a body.
2528
2529 Set_Has_Completion (Old_Id, False);
2530 end if;
2531 end if;
2532 end;
2533 end if;
2534
2535 Set_Has_Completion (Body_Id);
2536 Check_Eliminated (Body_Id);
2537
2538 if Nkind (N) = N_Subprogram_Body_Stub then
2539 return;
2540
2541 elsif Present (Spec_Id)
2542 and then Expander_Active
2543 and then
2544 (Has_Pragma_Inline_Always (Spec_Id)
2545 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
2546 then
2547 Build_Body_To_Inline (N, Spec_Id);
2548 end if;
2549
2550 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
2551 -- if its specification we have to install the private withed units.
2552 -- This holds for child units as well.
2553
2554 if Is_Compilation_Unit (Body_Id)
2555 or else Nkind (Parent (N)) = N_Compilation_Unit
2556 then
2557 Install_Private_With_Clauses (Body_Id);
2558 end if;
2559
2560 Check_Anonymous_Return;
2561
2562 -- Set the Protected_Formal field of each extra formal of the protected
2563 -- subprogram to reference the corresponding extra formal of the
2564 -- subprogram that implements it. For regular formals this occurs when
2565 -- the protected subprogram's declaration is expanded, but the extra
2566 -- formals don't get created until the subprogram is frozen. We need to
2567 -- do this before analyzing the protected subprogram's body so that any
2568 -- references to the original subprogram's extra formals will be changed
2569 -- refer to the implementing subprogram's formals (see Expand_Formal).
2570
2571 if Present (Spec_Id)
2572 and then Is_Protected_Type (Scope (Spec_Id))
2573 and then Present (Protected_Body_Subprogram (Spec_Id))
2574 then
2575 declare
2576 Impl_Subp : constant Entity_Id :=
2577 Protected_Body_Subprogram (Spec_Id);
2578 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2579 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2580 begin
2581 while Present (Prot_Ext_Formal) loop
2582 pragma Assert (Present (Impl_Ext_Formal));
2583 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2584 Next_Formal_With_Extras (Prot_Ext_Formal);
2585 Next_Formal_With_Extras (Impl_Ext_Formal);
2586 end loop;
2587 end;
2588 end if;
2589
2590 -- Now we can go on to analyze the body
2591
2592 HSS := Handled_Statement_Sequence (N);
2593 Set_Actual_Subtypes (N, Current_Scope);
2594
2595 -- Deal with preconditions and postconditions
2596
2597 Process_PPCs (N, Spec_Id, Body_Id);
2598
2599 -- Add a declaration for the Protection object, renaming declarations
2600 -- for discriminals and privals and finally a declaration for the entry
2601 -- family index (if applicable). This form of early expansion is done
2602 -- when the Expander is active because Install_Private_Data_Declarations
2603 -- references entities which were created during regular expansion.
2604
2605 if Expander_Active
2606 and then Comes_From_Source (N)
2607 and then Present (Prot_Typ)
2608 and then Present (Spec_Id)
2609 and then not Is_Eliminated (Spec_Id)
2610 then
2611 Install_Private_Data_Declarations
2612 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2613 end if;
2614
2615 -- Analyze the declarations (this call will analyze the precondition
2616 -- Check pragmas we prepended to the list, as well as the declaration
2617 -- of the _Postconditions procedure).
2618
2619 Analyze_Declarations (Declarations (N));
2620
2621 -- Check completion, and analyze the statements
2622
2623 Check_Completion;
2624 Inspect_Deferred_Constant_Completion (Declarations (N));
2625 Analyze (HSS);
2626
2627 -- Deal with end of scope processing for the body
2628
2629 Process_End_Label (HSS, 't', Current_Scope);
2630 End_Scope;
2631 Check_Subprogram_Order (N);
2632 Set_Analyzed (Body_Id);
2633
2634 -- If we have a separate spec, then the analysis of the declarations
2635 -- caused the entities in the body to be chained to the spec id, but
2636 -- we want them chained to the body id. Only the formal parameters
2637 -- end up chained to the spec id in this case.
2638
2639 if Present (Spec_Id) then
2640
2641 -- We must conform to the categorization of our spec
2642
2643 Validate_Categorization_Dependency (N, Spec_Id);
2644
2645 -- And if this is a child unit, the parent units must conform
2646
2647 if Is_Child_Unit (Spec_Id) then
2648 Validate_Categorization_Dependency
2649 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2650 end if;
2651
2652 -- Here is where we move entities from the spec to the body
2653
2654 -- Case where there are entities that stay with the spec
2655
2656 if Present (Last_Real_Spec_Entity) then
2657
2658 -- No body entities (happens when the only real spec entities
2659 -- come from precondition and postcondition pragmas)
2660
2661 if No (Last_Entity (Body_Id)) then
2662 Set_First_Entity
2663 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2664
2665 -- Body entities present (formals), so chain stuff past them
2666
2667 else
2668 Set_Next_Entity
2669 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2670 end if;
2671
2672 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2673 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2674 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2675
2676 -- Case where there are no spec entities, in this case there can
2677 -- be no body entities either, so just move everything.
2678
2679 else
2680 pragma Assert (No (Last_Entity (Body_Id)));
2681 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2682 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2683 Set_First_Entity (Spec_Id, Empty);
2684 Set_Last_Entity (Spec_Id, Empty);
2685 end if;
2686 end if;
2687
2688 Check_Missing_Return;
2689
2690 -- Now we are going to check for variables that are never modified in
2691 -- the body of the procedure. But first we deal with a special case
2692 -- where we want to modify this check. If the body of the subprogram
2693 -- starts with a raise statement or its equivalent, or if the body
2694 -- consists entirely of a null statement, then it is pretty obvious
2695 -- that it is OK to not reference the parameters. For example, this
2696 -- might be the following common idiom for a stubbed function:
2697 -- statement of the procedure raises an exception. In particular this
2698 -- deals with the common idiom of a stubbed function, which might
2699 -- appear as something like
2700
2701 -- function F (A : Integer) return Some_Type;
2702 -- X : Some_Type;
2703 -- begin
2704 -- raise Program_Error;
2705 -- return X;
2706 -- end F;
2707
2708 -- Here the purpose of X is simply to satisfy the annoying requirement
2709 -- in Ada that there be at least one return, and we certainly do not
2710 -- want to go posting warnings on X that it is not initialized! On
2711 -- the other hand, if X is entirely unreferenced that should still
2712 -- get a warning.
2713
2714 -- What we do is to detect these cases, and if we find them, flag the
2715 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2716 -- suppress unwanted warnings. For the case of the function stub above
2717 -- we have a special test to set X as apparently assigned to suppress
2718 -- the warning.
2719
2720 declare
2721 Stm : Node_Id;
2722
2723 begin
2724 -- Skip initial labels (for one thing this occurs when we are in
2725 -- front end ZCX mode, but in any case it is irrelevant), and also
2726 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2727
2728 Stm := First (Statements (HSS));
2729 while Nkind (Stm) = N_Label
2730 or else Nkind (Stm) in N_Push_xxx_Label
2731 loop
2732 Next (Stm);
2733 end loop;
2734
2735 -- Do the test on the original statement before expansion
2736
2737 declare
2738 Ostm : constant Node_Id := Original_Node (Stm);
2739
2740 begin
2741 -- If explicit raise statement, turn on flag
2742
2743 if Nkind (Ostm) = N_Raise_Statement then
2744 Set_Trivial_Subprogram (Stm);
2745
2746 -- If null statement, and no following statements, turn on flag
2747
2748 elsif Nkind (Stm) = N_Null_Statement
2749 and then Comes_From_Source (Stm)
2750 and then No (Next (Stm))
2751 then
2752 Set_Trivial_Subprogram (Stm);
2753
2754 -- Check for explicit call cases which likely raise an exception
2755
2756 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
2757 if Is_Entity_Name (Name (Ostm)) then
2758 declare
2759 Ent : constant Entity_Id := Entity (Name (Ostm));
2760
2761 begin
2762 -- If the procedure is marked No_Return, then likely it
2763 -- raises an exception, but in any case it is not coming
2764 -- back here, so turn on the flag.
2765
2766 if Ekind (Ent) = E_Procedure
2767 and then No_Return (Ent)
2768 then
2769 Set_Trivial_Subprogram (Stm);
2770 end if;
2771 end;
2772 end if;
2773 end if;
2774 end;
2775 end;
2776
2777 -- Check for variables that are never modified
2778
2779 declare
2780 E1, E2 : Entity_Id;
2781
2782 begin
2783 -- If there is a separate spec, then transfer Never_Set_In_Source
2784 -- flags from out parameters to the corresponding entities in the
2785 -- body. The reason we do that is we want to post error flags on
2786 -- the body entities, not the spec entities.
2787
2788 if Present (Spec_Id) then
2789 E1 := First_Entity (Spec_Id);
2790 while Present (E1) loop
2791 if Ekind (E1) = E_Out_Parameter then
2792 E2 := First_Entity (Body_Id);
2793 while Present (E2) loop
2794 exit when Chars (E1) = Chars (E2);
2795 Next_Entity (E2);
2796 end loop;
2797
2798 if Present (E2) then
2799 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
2800 end if;
2801 end if;
2802
2803 Next_Entity (E1);
2804 end loop;
2805 end if;
2806
2807 -- Check references in body unless it was deleted. Note that the
2808 -- check of Body_Deleted here is not just for efficiency, it is
2809 -- necessary to avoid junk warnings on formal parameters.
2810
2811 if not Body_Deleted then
2812 Check_References (Body_Id);
2813 end if;
2814 end;
2815 end Analyze_Subprogram_Body_Helper;
2816
2817 ------------------------------------
2818 -- Analyze_Subprogram_Declaration --
2819 ------------------------------------
2820
2821 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
2822 Loc : constant Source_Ptr := Sloc (N);
2823 Scop : constant Entity_Id := Current_Scope;
2824 Designator : Entity_Id;
2825 Form : Node_Id;
2826 Null_Body : Node_Id := Empty;
2827
2828 -- Start of processing for Analyze_Subprogram_Declaration
2829
2830 begin
2831 -- Null procedures are not allowed in SPARK or ALFA
2832
2833 if Nkind (Specification (N)) = N_Procedure_Specification
2834 and then Null_Present (Specification (N))
2835 then
2836 Check_Formal_Restriction ("null procedure is not allowed", N);
2837 end if;
2838
2839 -- For a null procedure, capture the profile before analysis, for
2840 -- expansion at the freeze point and at each point of call. The body
2841 -- will only be used if the procedure has preconditions. In that case
2842 -- the body is analyzed at the freeze point.
2843
2844 if Nkind (Specification (N)) = N_Procedure_Specification
2845 and then Null_Present (Specification (N))
2846 and then Expander_Active
2847 then
2848 Null_Body :=
2849 Make_Subprogram_Body (Loc,
2850 Specification =>
2851 New_Copy_Tree (Specification (N)),
2852 Declarations =>
2853 New_List,
2854 Handled_Statement_Sequence =>
2855 Make_Handled_Sequence_Of_Statements (Loc,
2856 Statements => New_List (Make_Null_Statement (Loc))));
2857
2858 -- Create new entities for body and formals
2859
2860 Set_Defining_Unit_Name (Specification (Null_Body),
2861 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
2862 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2863
2864 Form := First (Parameter_Specifications (Specification (Null_Body)));
2865 while Present (Form) loop
2866 Set_Defining_Identifier (Form,
2867 Make_Defining_Identifier (Loc,
2868 Chars (Defining_Identifier (Form))));
2869
2870 -- Resolve the types of the formals now, because the freeze point
2871 -- may appear in a different context, e.g. an instantiation.
2872
2873 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
2874 Find_Type (Parameter_Type (Form));
2875
2876 elsif
2877 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
2878 then
2879 Find_Type (Subtype_Mark (Parameter_Type (Form)));
2880
2881 else
2882
2883 -- the case of a null procedure with a formal that is an
2884 -- access_to_subprogram type, and that is used as an actual
2885 -- in an instantiation is left to the enthusiastic reader.
2886
2887 null;
2888 end if;
2889
2890 Next (Form);
2891 end loop;
2892
2893 if Is_Protected_Type (Current_Scope) then
2894 Error_Msg_N ("protected operation cannot be a null procedure", N);
2895 end if;
2896 end if;
2897
2898 Designator := Analyze_Subprogram_Specification (Specification (N));
2899 Generate_Definition (Designator);
2900
2901 if Debug_Flag_C then
2902 Write_Str ("==> subprogram spec ");
2903 Write_Name (Chars (Designator));
2904 Write_Str (" from ");
2905 Write_Location (Sloc (N));
2906 Write_Eol;
2907 Indent;
2908 end if;
2909
2910 if Nkind (Specification (N)) = N_Procedure_Specification
2911 and then Null_Present (Specification (N))
2912 then
2913 Set_Has_Completion (Designator);
2914
2915 if Present (Null_Body) then
2916 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2917 Set_Body_To_Inline (N, Null_Body);
2918 Set_Is_Inlined (Designator);
2919 end if;
2920 end if;
2921
2922 Validate_RCI_Subprogram_Declaration (N);
2923 New_Overloaded_Entity (Designator);
2924 Check_Delayed_Subprogram (Designator);
2925
2926 -- If the type of the first formal of the current subprogram is a
2927 -- nongeneric tagged private type, mark the subprogram as being a
2928 -- private primitive. Ditto if this is a function with controlling
2929 -- result, and the return type is currently private. In both cases,
2930 -- the type of the controlling argument or result must be in the
2931 -- current scope for the operation to be primitive.
2932
2933 if Has_Controlling_Result (Designator)
2934 and then Is_Private_Type (Etype (Designator))
2935 and then Scope (Etype (Designator)) = Current_Scope
2936 and then not Is_Generic_Actual_Type (Etype (Designator))
2937 then
2938 Set_Is_Private_Primitive (Designator);
2939
2940 elsif Present (First_Formal (Designator)) then
2941 declare
2942 Formal_Typ : constant Entity_Id :=
2943 Etype (First_Formal (Designator));
2944 begin
2945 Set_Is_Private_Primitive (Designator,
2946 Is_Tagged_Type (Formal_Typ)
2947 and then Scope (Formal_Typ) = Current_Scope
2948 and then Is_Private_Type (Formal_Typ)
2949 and then not Is_Generic_Actual_Type (Formal_Typ));
2950 end;
2951 end if;
2952
2953 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
2954 -- or null.
2955
2956 if Ada_Version >= Ada_2005
2957 and then Comes_From_Source (N)
2958 and then Is_Dispatching_Operation (Designator)
2959 then
2960 declare
2961 E : Entity_Id;
2962 Etyp : Entity_Id;
2963
2964 begin
2965 if Has_Controlling_Result (Designator) then
2966 Etyp := Etype (Designator);
2967
2968 else
2969 E := First_Entity (Designator);
2970 while Present (E)
2971 and then Is_Formal (E)
2972 and then not Is_Controlling_Formal (E)
2973 loop
2974 Next_Entity (E);
2975 end loop;
2976
2977 Etyp := Etype (E);
2978 end if;
2979
2980 if Is_Access_Type (Etyp) then
2981 Etyp := Directly_Designated_Type (Etyp);
2982 end if;
2983
2984 if Is_Interface (Etyp)
2985 and then not Is_Abstract_Subprogram (Designator)
2986 and then not (Ekind (Designator) = E_Procedure
2987 and then Null_Present (Specification (N)))
2988 then
2989 Error_Msg_Name_1 := Chars (Defining_Entity (N));
2990 Error_Msg_N
2991 ("(Ada 2005) interface subprogram % must be abstract or null",
2992 N);
2993 end if;
2994 end;
2995 end if;
2996
2997 -- What is the following code for, it used to be
2998
2999 -- ??? Set_Suppress_Elaboration_Checks
3000 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3001
3002 -- The following seems equivalent, but a bit dubious
3003
3004 if Elaboration_Checks_Suppressed (Designator) then
3005 Set_Kill_Elaboration_Checks (Designator);
3006 end if;
3007
3008 if Scop /= Standard_Standard
3009 and then not Is_Child_Unit (Designator)
3010 then
3011 Set_Categorization_From_Scope (Designator, Scop);
3012 else
3013 -- For a compilation unit, check for library-unit pragmas
3014
3015 Push_Scope (Designator);
3016 Set_Categorization_From_Pragmas (N);
3017 Validate_Categorization_Dependency (N, Designator);
3018 Pop_Scope;
3019 end if;
3020
3021 -- For a compilation unit, set body required. This flag will only be
3022 -- reset if a valid Import or Interface pragma is processed later on.
3023
3024 if Nkind (Parent (N)) = N_Compilation_Unit then
3025 Set_Body_Required (Parent (N), True);
3026
3027 if Ada_Version >= Ada_2005
3028 and then Nkind (Specification (N)) = N_Procedure_Specification
3029 and then Null_Present (Specification (N))
3030 then
3031 Error_Msg_N
3032 ("null procedure cannot be declared at library level", N);
3033 end if;
3034 end if;
3035
3036 Generate_Reference_To_Formals (Designator);
3037 Check_Eliminated (Designator);
3038
3039 if Debug_Flag_C then
3040 Outdent;
3041 Write_Str ("<== subprogram spec ");
3042 Write_Name (Chars (Designator));
3043 Write_Str (" from ");
3044 Write_Location (Sloc (N));
3045 Write_Eol;
3046 end if;
3047
3048 if Is_Protected_Type (Current_Scope) then
3049
3050 -- Indicate that this is a protected operation, because it may be
3051 -- used in subsequent declarations within the protected type.
3052
3053 Set_Convention (Designator, Convention_Protected);
3054 end if;
3055
3056 List_Inherited_Pre_Post_Aspects (Designator);
3057
3058 if Has_Aspects (N) then
3059 Analyze_Aspect_Specifications (N, Designator);
3060 end if;
3061 end Analyze_Subprogram_Declaration;
3062
3063 --------------------------------------
3064 -- Analyze_Subprogram_Specification --
3065 --------------------------------------
3066
3067 -- Reminder: N here really is a subprogram specification (not a subprogram
3068 -- declaration). This procedure is called to analyze the specification in
3069 -- both subprogram bodies and subprogram declarations (specs).
3070
3071 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
3072 Designator : constant Entity_Id := Defining_Entity (N);
3073 Formals : constant List_Id := Parameter_Specifications (N);
3074
3075 -- Start of processing for Analyze_Subprogram_Specification
3076
3077 begin
3078 -- User-defined operator is not allowed in SPARK or ALFA
3079
3080 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol then
3081 Check_Formal_Restriction ("user-defined operator is not allowed", N);
3082 end if;
3083
3084 -- Proceed with analysis
3085
3086 Generate_Definition (Designator);
3087
3088 if Nkind (N) = N_Function_Specification then
3089 Set_Ekind (Designator, E_Function);
3090 Set_Mechanism (Designator, Default_Mechanism);
3091 else
3092 Set_Ekind (Designator, E_Procedure);
3093 Set_Etype (Designator, Standard_Void_Type);
3094 end if;
3095
3096 -- Introduce new scope for analysis of the formals and the return type
3097
3098 Set_Scope (Designator, Current_Scope);
3099
3100 if Present (Formals) then
3101 Push_Scope (Designator);
3102 Process_Formals (Formals, N);
3103
3104 -- Ada 2005 (AI-345): If this is an overriding operation of an
3105 -- inherited interface operation, and the controlling type is
3106 -- a synchronized type, replace the type with its corresponding
3107 -- record, to match the proper signature of an overriding operation.
3108 -- Same processing for an access parameter whose designated type is
3109 -- derived from a synchronized interface.
3110
3111 if Ada_Version >= Ada_2005 then
3112 declare
3113 Formal : Entity_Id;
3114 Formal_Typ : Entity_Id;
3115 Rec_Typ : Entity_Id;
3116 Desig_Typ : Entity_Id;
3117
3118 begin
3119 Formal := First_Formal (Designator);
3120 while Present (Formal) loop
3121 Formal_Typ := Etype (Formal);
3122
3123 if Is_Concurrent_Type (Formal_Typ)
3124 and then Present (Corresponding_Record_Type (Formal_Typ))
3125 then
3126 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
3127
3128 if Present (Interfaces (Rec_Typ)) then
3129 Set_Etype (Formal, Rec_Typ);
3130 end if;
3131
3132 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
3133 Desig_Typ := Designated_Type (Formal_Typ);
3134
3135 if Is_Concurrent_Type (Desig_Typ)
3136 and then Present (Corresponding_Record_Type (Desig_Typ))
3137 then
3138 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
3139
3140 if Present (Interfaces (Rec_Typ)) then
3141 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
3142 end if;
3143 end if;
3144 end if;
3145
3146 Next_Formal (Formal);
3147 end loop;
3148 end;
3149 end if;
3150
3151 End_Scope;
3152
3153 -- The subprogram scope is pushed and popped around the processing of
3154 -- the return type for consistency with call above to Process_Formals
3155 -- (which itself can call Analyze_Return_Type), and to ensure that any
3156 -- itype created for the return type will be associated with the proper
3157 -- scope.
3158
3159 elsif Nkind (N) = N_Function_Specification then
3160 Push_Scope (Designator);
3161 Analyze_Return_Type (N);
3162 End_Scope;
3163 end if;
3164
3165 -- Function case
3166
3167 if Nkind (N) = N_Function_Specification then
3168
3169 -- Deal with operator symbol case
3170
3171 if Nkind (Designator) = N_Defining_Operator_Symbol then
3172 Valid_Operator_Definition (Designator);
3173 end if;
3174
3175 May_Need_Actuals (Designator);
3176
3177 -- Ada 2005 (AI-251): If the return type is abstract, verify that
3178 -- the subprogram is abstract also. This does not apply to renaming
3179 -- declarations, where abstractness is inherited.
3180
3181 -- In case of primitives associated with abstract interface types
3182 -- the check is applied later (see Analyze_Subprogram_Declaration).
3183
3184 if not Nkind_In (Parent (N), N_Subprogram_Renaming_Declaration,
3185 N_Abstract_Subprogram_Declaration,
3186 N_Formal_Abstract_Subprogram_Declaration)
3187 then
3188 if Is_Abstract_Type (Etype (Designator))
3189 and then not Is_Interface (Etype (Designator))
3190 then
3191 Error_Msg_N
3192 ("function that returns abstract type must be abstract", N);
3193
3194 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
3195 -- access result whose designated type is abstract.
3196
3197 elsif Nkind (Result_Definition (N)) = N_Access_Definition
3198 and then
3199 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
3200 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
3201 and then Ada_Version >= Ada_2012
3202 then
3203 Error_Msg_N ("function whose access result designates "
3204 & "abstract type must be abstract", N);
3205 end if;
3206 end if;
3207 end if;
3208
3209 return Designator;
3210 end Analyze_Subprogram_Specification;
3211
3212 --------------------------
3213 -- Build_Body_To_Inline --
3214 --------------------------
3215
3216 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
3217 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3218 Original_Body : Node_Id;
3219 Body_To_Analyze : Node_Id;
3220 Max_Size : constant := 10;
3221 Stat_Count : Integer := 0;
3222
3223 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
3224 -- Check for declarations that make inlining not worthwhile
3225
3226 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
3227 -- Check for statements that make inlining not worthwhile: any tasking
3228 -- statement, nested at any level. Keep track of total number of
3229 -- elementary statements, as a measure of acceptable size.
3230
3231 function Has_Pending_Instantiation return Boolean;
3232 -- If some enclosing body contains instantiations that appear before the
3233 -- corresponding generic body, the enclosing body has a freeze node so
3234 -- that it can be elaborated after the generic itself. This might
3235 -- conflict with subsequent inlinings, so that it is unsafe to try to
3236 -- inline in such a case.
3237
3238 function Has_Single_Return return Boolean;
3239 -- In general we cannot inline functions that return unconstrained type.
3240 -- However, we can handle such functions if all return statements return
3241 -- a local variable that is the only declaration in the body of the
3242 -- function. In that case the call can be replaced by that local
3243 -- variable as is done for other inlined calls.
3244
3245 procedure Remove_Pragmas;
3246 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
3247 -- parameter has no meaning when the body is inlined and the formals
3248 -- are rewritten. Remove it from body to inline. The analysis of the
3249 -- non-inlined body will handle the pragma properly.
3250
3251 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
3252 -- If the body of the subprogram includes a call that returns an
3253 -- unconstrained type, the secondary stack is involved, and it
3254 -- is not worth inlining.
3255
3256 ------------------------------
3257 -- Has_Excluded_Declaration --
3258 ------------------------------
3259
3260 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
3261 D : Node_Id;
3262
3263 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3264 -- Nested subprograms make a given body ineligible for inlining, but
3265 -- we make an exception for instantiations of unchecked conversion.
3266 -- The body has not been analyzed yet, so check the name, and verify
3267 -- that the visible entity with that name is the predefined unit.
3268
3269 -----------------------------
3270 -- Is_Unchecked_Conversion --
3271 -----------------------------
3272
3273 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3274 Id : constant Node_Id := Name (D);
3275 Conv : Entity_Id;
3276
3277 begin
3278 if Nkind (Id) = N_Identifier
3279 and then Chars (Id) = Name_Unchecked_Conversion
3280 then
3281 Conv := Current_Entity (Id);
3282
3283 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3284 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3285 then
3286 Conv := Current_Entity (Selector_Name (Id));
3287 else
3288 return False;
3289 end if;
3290
3291 return Present (Conv)
3292 and then Is_Predefined_File_Name
3293 (Unit_File_Name (Get_Source_Unit (Conv)))
3294 and then Is_Intrinsic_Subprogram (Conv);
3295 end Is_Unchecked_Conversion;
3296
3297 -- Start of processing for Has_Excluded_Declaration
3298
3299 begin
3300 D := First (Decls);
3301 while Present (D) loop
3302 if (Nkind (D) = N_Function_Instantiation
3303 and then not Is_Unchecked_Conversion (D))
3304 or else Nkind_In (D, N_Protected_Type_Declaration,
3305 N_Package_Declaration,
3306 N_Package_Instantiation,
3307 N_Subprogram_Body,
3308 N_Procedure_Instantiation,
3309 N_Task_Type_Declaration)
3310 then
3311 Cannot_Inline
3312 ("cannot inline & (non-allowed declaration)?", D, Subp);
3313 return True;
3314 end if;
3315
3316 Next (D);
3317 end loop;
3318
3319 return False;
3320 end Has_Excluded_Declaration;
3321
3322 ----------------------------
3323 -- Has_Excluded_Statement --
3324 ----------------------------
3325
3326 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
3327 S : Node_Id;
3328 E : Node_Id;
3329
3330 begin
3331 S := First (Stats);
3332 while Present (S) loop
3333 Stat_Count := Stat_Count + 1;
3334
3335 if Nkind_In (S, N_Abort_Statement,
3336 N_Asynchronous_Select,
3337 N_Conditional_Entry_Call,
3338 N_Delay_Relative_Statement,
3339 N_Delay_Until_Statement,
3340 N_Selective_Accept,
3341 N_Timed_Entry_Call)
3342 then
3343 Cannot_Inline
3344 ("cannot inline & (non-allowed statement)?", S, Subp);
3345 return True;
3346
3347 elsif Nkind (S) = N_Block_Statement then
3348 if Present (Declarations (S))
3349 and then Has_Excluded_Declaration (Declarations (S))
3350 then
3351 return True;
3352
3353 elsif Present (Handled_Statement_Sequence (S))
3354 and then
3355 (Present
3356 (Exception_Handlers (Handled_Statement_Sequence (S)))
3357 or else
3358 Has_Excluded_Statement
3359 (Statements (Handled_Statement_Sequence (S))))
3360 then
3361 return True;
3362 end if;
3363
3364 elsif Nkind (S) = N_Case_Statement then
3365 E := First (Alternatives (S));
3366 while Present (E) loop
3367 if Has_Excluded_Statement (Statements (E)) then
3368 return True;
3369 end if;
3370
3371 Next (E);
3372 end loop;
3373
3374 elsif Nkind (S) = N_If_Statement then
3375 if Has_Excluded_Statement (Then_Statements (S)) then
3376 return True;
3377 end if;
3378
3379 if Present (Elsif_Parts (S)) then
3380 E := First (Elsif_Parts (S));
3381 while Present (E) loop
3382 if Has_Excluded_Statement (Then_Statements (E)) then
3383 return True;
3384 end if;
3385 Next (E);
3386 end loop;
3387 end if;
3388
3389 if Present (Else_Statements (S))
3390 and then Has_Excluded_Statement (Else_Statements (S))
3391 then
3392 return True;
3393 end if;
3394
3395 elsif Nkind (S) = N_Loop_Statement
3396 and then Has_Excluded_Statement (Statements (S))
3397 then
3398 return True;
3399
3400 elsif Nkind (S) = N_Extended_Return_Statement then
3401 if Has_Excluded_Statement
3402 (Statements (Handled_Statement_Sequence (S)))
3403 or else Present
3404 (Exception_Handlers (Handled_Statement_Sequence (S)))
3405 then
3406 return True;
3407 end if;
3408 end if;
3409
3410 Next (S);
3411 end loop;
3412
3413 return False;
3414 end Has_Excluded_Statement;
3415
3416 -------------------------------
3417 -- Has_Pending_Instantiation --
3418 -------------------------------
3419
3420 function Has_Pending_Instantiation return Boolean is
3421 S : Entity_Id;
3422
3423 begin
3424 S := Current_Scope;
3425 while Present (S) loop
3426 if Is_Compilation_Unit (S)
3427 or else Is_Child_Unit (S)
3428 then
3429 return False;
3430
3431 elsif Ekind (S) = E_Package
3432 and then Has_Forward_Instantiation (S)
3433 then
3434 return True;
3435 end if;
3436
3437 S := Scope (S);
3438 end loop;
3439
3440 return False;
3441 end Has_Pending_Instantiation;
3442
3443 ------------------------
3444 -- Has_Single_Return --
3445 ------------------------
3446
3447 function Has_Single_Return return Boolean is
3448 Return_Statement : Node_Id := Empty;
3449
3450 function Check_Return (N : Node_Id) return Traverse_Result;
3451
3452 ------------------
3453 -- Check_Return --
3454 ------------------
3455
3456 function Check_Return (N : Node_Id) return Traverse_Result is
3457 begin
3458 if Nkind (N) = N_Simple_Return_Statement then
3459 if Present (Expression (N))
3460 and then Is_Entity_Name (Expression (N))
3461 then
3462 if No (Return_Statement) then
3463 Return_Statement := N;
3464 return OK;
3465
3466 elsif Chars (Expression (N)) =
3467 Chars (Expression (Return_Statement))
3468 then
3469 return OK;
3470
3471 else
3472 return Abandon;
3473 end if;
3474
3475 -- A return statement within an extended return is a noop
3476 -- after inlining.
3477
3478 elsif No (Expression (N))
3479 and then Nkind (Parent (Parent (N))) =
3480 N_Extended_Return_Statement
3481 then
3482 return OK;
3483
3484 else
3485 -- Expression has wrong form
3486
3487 return Abandon;
3488 end if;
3489
3490 -- We can only inline a build-in-place function if
3491 -- it has a single extended return.
3492
3493 elsif Nkind (N) = N_Extended_Return_Statement then
3494 if No (Return_Statement) then
3495 Return_Statement := N;
3496 return OK;
3497
3498 else
3499 return Abandon;
3500 end if;
3501
3502 else
3503 return OK;
3504 end if;
3505 end Check_Return;
3506
3507 function Check_All_Returns is new Traverse_Func (Check_Return);
3508
3509 -- Start of processing for Has_Single_Return
3510
3511 begin
3512 if Check_All_Returns (N) /= OK then
3513 return False;
3514
3515 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
3516 return True;
3517
3518 else
3519 return Present (Declarations (N))
3520 and then Present (First (Declarations (N)))
3521 and then Chars (Expression (Return_Statement)) =
3522 Chars (Defining_Identifier (First (Declarations (N))));
3523 end if;
3524 end Has_Single_Return;
3525
3526 --------------------
3527 -- Remove_Pragmas --
3528 --------------------
3529
3530 procedure Remove_Pragmas is
3531 Decl : Node_Id;
3532 Nxt : Node_Id;
3533
3534 begin
3535 Decl := First (Declarations (Body_To_Analyze));
3536 while Present (Decl) loop
3537 Nxt := Next (Decl);
3538
3539 if Nkind (Decl) = N_Pragma
3540 and then (Pragma_Name (Decl) = Name_Unreferenced
3541 or else
3542 Pragma_Name (Decl) = Name_Unmodified)
3543 then
3544 Remove (Decl);
3545 end if;
3546
3547 Decl := Nxt;
3548 end loop;
3549 end Remove_Pragmas;
3550
3551 --------------------------
3552 -- Uses_Secondary_Stack --
3553 --------------------------
3554
3555 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
3556 function Check_Call (N : Node_Id) return Traverse_Result;
3557 -- Look for function calls that return an unconstrained type
3558
3559 ----------------
3560 -- Check_Call --
3561 ----------------
3562
3563 function Check_Call (N : Node_Id) return Traverse_Result is
3564 begin
3565 if Nkind (N) = N_Function_Call
3566 and then Is_Entity_Name (Name (N))
3567 and then Is_Composite_Type (Etype (Entity (Name (N))))
3568 and then not Is_Constrained (Etype (Entity (Name (N))))
3569 then
3570 Cannot_Inline
3571 ("cannot inline & (call returns unconstrained type)?",
3572 N, Subp);
3573 return Abandon;
3574 else
3575 return OK;
3576 end if;
3577 end Check_Call;
3578
3579 function Check_Calls is new Traverse_Func (Check_Call);
3580
3581 begin
3582 return Check_Calls (Bod) = Abandon;
3583 end Uses_Secondary_Stack;
3584
3585 -- Start of processing for Build_Body_To_Inline
3586
3587 begin
3588 -- Return immediately if done already
3589
3590 if Nkind (Decl) = N_Subprogram_Declaration
3591 and then Present (Body_To_Inline (Decl))
3592 then
3593 return;
3594
3595 -- Functions that return unconstrained composite types require
3596 -- secondary stack handling, and cannot currently be inlined, unless
3597 -- all return statements return a local variable that is the first
3598 -- local declaration in the body.
3599
3600 elsif Ekind (Subp) = E_Function
3601 and then not Is_Scalar_Type (Etype (Subp))
3602 and then not Is_Access_Type (Etype (Subp))
3603 and then not Is_Constrained (Etype (Subp))
3604 then
3605 if not Has_Single_Return then
3606 Cannot_Inline
3607 ("cannot inline & (unconstrained return type)?", N, Subp);
3608 return;
3609 end if;
3610
3611 -- Ditto for functions that return controlled types, where controlled
3612 -- actions interfere in complex ways with inlining.
3613
3614 elsif Ekind (Subp) = E_Function
3615 and then Needs_Finalization (Etype (Subp))
3616 then
3617 Cannot_Inline
3618 ("cannot inline & (controlled return type)?", N, Subp);
3619 return;
3620 end if;
3621
3622 if Present (Declarations (N))
3623 and then Has_Excluded_Declaration (Declarations (N))
3624 then
3625 return;
3626 end if;
3627
3628 if Present (Handled_Statement_Sequence (N)) then
3629 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
3630 Cannot_Inline
3631 ("cannot inline& (exception handler)?",
3632 First (Exception_Handlers (Handled_Statement_Sequence (N))),
3633 Subp);
3634 return;
3635 elsif
3636 Has_Excluded_Statement
3637 (Statements (Handled_Statement_Sequence (N)))
3638 then
3639 return;
3640 end if;
3641 end if;
3642
3643 -- We do not inline a subprogram that is too large, unless it is
3644 -- marked Inline_Always. This pragma does not suppress the other
3645 -- checks on inlining (forbidden declarations, handlers, etc).
3646
3647 if Stat_Count > Max_Size
3648 and then not Has_Pragma_Inline_Always (Subp)
3649 then
3650 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
3651 return;
3652 end if;
3653
3654 if Has_Pending_Instantiation then
3655 Cannot_Inline
3656 ("cannot inline& (forward instance within enclosing body)?",
3657 N, Subp);
3658 return;
3659 end if;
3660
3661 -- Within an instance, the body to inline must be treated as a nested
3662 -- generic, so that the proper global references are preserved.
3663
3664 -- Note that we do not do this at the library level, because it is not
3665 -- needed, and furthermore this causes trouble if front end inlining
3666 -- is activated (-gnatN).
3667
3668 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3669 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
3670 Original_Body := Copy_Generic_Node (N, Empty, True);
3671 else
3672 Original_Body := Copy_Separate_Tree (N);
3673 end if;
3674
3675 -- We need to capture references to the formals in order to substitute
3676 -- the actuals at the point of inlining, i.e. instantiation. To treat
3677 -- the formals as globals to the body to inline, we nest it within
3678 -- a dummy parameterless subprogram, declared within the real one.
3679 -- To avoid generating an internal name (which is never public, and
3680 -- which affects serial numbers of other generated names), we use
3681 -- an internal symbol that cannot conflict with user declarations.
3682
3683 Set_Parameter_Specifications (Specification (Original_Body), No_List);
3684 Set_Defining_Unit_Name
3685 (Specification (Original_Body),
3686 Make_Defining_Identifier (Sloc (N), Name_uParent));
3687 Set_Corresponding_Spec (Original_Body, Empty);
3688
3689 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
3690
3691 -- Set return type of function, which is also global and does not need
3692 -- to be resolved.
3693
3694 if Ekind (Subp) = E_Function then
3695 Set_Result_Definition (Specification (Body_To_Analyze),
3696 New_Occurrence_Of (Etype (Subp), Sloc (N)));
3697 end if;
3698
3699 if No (Declarations (N)) then
3700 Set_Declarations (N, New_List (Body_To_Analyze));
3701 else
3702 Append (Body_To_Analyze, Declarations (N));
3703 end if;
3704
3705 Expander_Mode_Save_And_Set (False);
3706 Remove_Pragmas;
3707
3708 Analyze (Body_To_Analyze);
3709 Push_Scope (Defining_Entity (Body_To_Analyze));
3710 Save_Global_References (Original_Body);
3711 End_Scope;
3712 Remove (Body_To_Analyze);
3713
3714 Expander_Mode_Restore;
3715
3716 -- Restore environment if previously saved
3717
3718 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3719 Restore_Env;
3720 end if;
3721
3722 -- If secondary stk used there is no point in inlining. We have
3723 -- already issued the warning in this case, so nothing to do.
3724
3725 if Uses_Secondary_Stack (Body_To_Analyze) then
3726 return;
3727 end if;
3728
3729 Set_Body_To_Inline (Decl, Original_Body);
3730 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
3731 Set_Is_Inlined (Subp);
3732 end Build_Body_To_Inline;
3733
3734 -------------------
3735 -- Cannot_Inline --
3736 -------------------
3737
3738 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
3739 begin
3740 -- Do not emit warning if this is a predefined unit which is not the
3741 -- main unit. With validity checks enabled, some predefined subprograms
3742 -- may contain nested subprograms and become ineligible for inlining.
3743
3744 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
3745 and then not In_Extended_Main_Source_Unit (Subp)
3746 then
3747 null;
3748
3749 elsif Has_Pragma_Inline_Always (Subp) then
3750
3751 -- Remove last character (question mark) to make this into an error,
3752 -- because the Inline_Always pragma cannot be obeyed.
3753
3754 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
3755
3756 elsif Ineffective_Inline_Warnings then
3757 Error_Msg_NE (Msg, N, Subp);
3758 end if;
3759 end Cannot_Inline;
3760
3761 -----------------------
3762 -- Check_Conformance --
3763 -----------------------
3764
3765 procedure Check_Conformance
3766 (New_Id : Entity_Id;
3767 Old_Id : Entity_Id;
3768 Ctype : Conformance_Type;
3769 Errmsg : Boolean;
3770 Conforms : out Boolean;
3771 Err_Loc : Node_Id := Empty;
3772 Get_Inst : Boolean := False;
3773 Skip_Controlling_Formals : Boolean := False)
3774 is
3775 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
3776 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
3777 -- If Errmsg is True, then processing continues to post an error message
3778 -- for conformance error on given node. Two messages are output. The
3779 -- first message points to the previous declaration with a general "no
3780 -- conformance" message. The second is the detailed reason, supplied as
3781 -- Msg. The parameter N provide information for a possible & insertion
3782 -- in the message, and also provides the location for posting the
3783 -- message in the absence of a specified Err_Loc location.
3784
3785 -----------------------
3786 -- Conformance_Error --
3787 -----------------------
3788
3789 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
3790 Enode : Node_Id;
3791
3792 begin
3793 Conforms := False;
3794
3795 if Errmsg then
3796 if No (Err_Loc) then
3797 Enode := N;
3798 else
3799 Enode := Err_Loc;
3800 end if;
3801
3802 Error_Msg_Sloc := Sloc (Old_Id);
3803
3804 case Ctype is
3805 when Type_Conformant =>
3806 Error_Msg_N -- CODEFIX
3807 ("not type conformant with declaration#!", Enode);
3808
3809 when Mode_Conformant =>
3810 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3811 Error_Msg_N
3812 ("not mode conformant with operation inherited#!",
3813 Enode);
3814 else
3815 Error_Msg_N
3816 ("not mode conformant with declaration#!", Enode);
3817 end if;
3818
3819 when Subtype_Conformant =>
3820 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3821 Error_Msg_N
3822 ("not subtype conformant with operation inherited#!",
3823 Enode);
3824 else
3825 Error_Msg_N
3826 ("not subtype conformant with declaration#!", Enode);
3827 end if;
3828
3829 when Fully_Conformant =>
3830 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3831 Error_Msg_N -- CODEFIX
3832 ("not fully conformant with operation inherited#!",
3833 Enode);
3834 else
3835 Error_Msg_N -- CODEFIX
3836 ("not fully conformant with declaration#!", Enode);
3837 end if;
3838 end case;
3839
3840 Error_Msg_NE (Msg, Enode, N);
3841 end if;
3842 end Conformance_Error;
3843
3844 -- Local Variables
3845
3846 Old_Type : constant Entity_Id := Etype (Old_Id);
3847 New_Type : constant Entity_Id := Etype (New_Id);
3848 Old_Formal : Entity_Id;
3849 New_Formal : Entity_Id;
3850 Access_Types_Match : Boolean;
3851 Old_Formal_Base : Entity_Id;
3852 New_Formal_Base : Entity_Id;
3853
3854 -- Start of processing for Check_Conformance
3855
3856 begin
3857 Conforms := True;
3858
3859 -- We need a special case for operators, since they don't appear
3860 -- explicitly.
3861
3862 if Ctype = Type_Conformant then
3863 if Ekind (New_Id) = E_Operator
3864 and then Operator_Matches_Spec (New_Id, Old_Id)
3865 then
3866 return;
3867 end if;
3868 end if;
3869
3870 -- If both are functions/operators, check return types conform
3871
3872 if Old_Type /= Standard_Void_Type
3873 and then New_Type /= Standard_Void_Type
3874 then
3875
3876 -- If we are checking interface conformance we omit controlling
3877 -- arguments and result, because we are only checking the conformance
3878 -- of the remaining parameters.
3879
3880 if Has_Controlling_Result (Old_Id)
3881 and then Has_Controlling_Result (New_Id)
3882 and then Skip_Controlling_Formals
3883 then
3884 null;
3885
3886 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
3887 Conformance_Error ("\return type does not match!", New_Id);
3888 return;
3889 end if;
3890
3891 -- Ada 2005 (AI-231): In case of anonymous access types check the
3892 -- null-exclusion and access-to-constant attributes match.
3893
3894 if Ada_Version >= Ada_2005
3895 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
3896 and then
3897 (Can_Never_Be_Null (Old_Type)
3898 /= Can_Never_Be_Null (New_Type)
3899 or else Is_Access_Constant (Etype (Old_Type))
3900 /= Is_Access_Constant (Etype (New_Type)))
3901 then
3902 Conformance_Error ("\return type does not match!", New_Id);
3903 return;
3904 end if;
3905
3906 -- If either is a function/operator and the other isn't, error
3907
3908 elsif Old_Type /= Standard_Void_Type
3909 or else New_Type /= Standard_Void_Type
3910 then
3911 Conformance_Error ("\functions can only match functions!", New_Id);
3912 return;
3913 end if;
3914
3915 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
3916 -- If this is a renaming as body, refine error message to indicate that
3917 -- the conflict is with the original declaration. If the entity is not
3918 -- frozen, the conventions don't have to match, the one of the renamed
3919 -- entity is inherited.
3920
3921 if Ctype >= Subtype_Conformant then
3922 if Convention (Old_Id) /= Convention (New_Id) then
3923
3924 if not Is_Frozen (New_Id) then
3925 null;
3926
3927 elsif Present (Err_Loc)
3928 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
3929 and then Present (Corresponding_Spec (Err_Loc))
3930 then
3931 Error_Msg_Name_1 := Chars (New_Id);
3932 Error_Msg_Name_2 :=
3933 Name_Ada + Convention_Id'Pos (Convention (New_Id));
3934 Conformance_Error ("\prior declaration for% has convention %!");
3935
3936 else
3937 Conformance_Error ("\calling conventions do not match!");
3938 end if;
3939
3940 return;
3941
3942 elsif Is_Formal_Subprogram (Old_Id)
3943 or else Is_Formal_Subprogram (New_Id)
3944 then
3945 Conformance_Error ("\formal subprograms not allowed!");
3946 return;
3947 end if;
3948 end if;
3949
3950 -- Deal with parameters
3951
3952 -- Note: we use the entity information, rather than going directly
3953 -- to the specification in the tree. This is not only simpler, but
3954 -- absolutely necessary for some cases of conformance tests between
3955 -- operators, where the declaration tree simply does not exist!
3956
3957 Old_Formal := First_Formal (Old_Id);
3958 New_Formal := First_Formal (New_Id);
3959 while Present (Old_Formal) and then Present (New_Formal) loop
3960 if Is_Controlling_Formal (Old_Formal)
3961 and then Is_Controlling_Formal (New_Formal)
3962 and then Skip_Controlling_Formals
3963 then
3964 -- The controlling formals will have different types when
3965 -- comparing an interface operation with its match, but both
3966 -- or neither must be access parameters.
3967
3968 if Is_Access_Type (Etype (Old_Formal))
3969 =
3970 Is_Access_Type (Etype (New_Formal))
3971 then
3972 goto Skip_Controlling_Formal;
3973 else
3974 Conformance_Error
3975 ("\access parameter does not match!", New_Formal);
3976 end if;
3977 end if;
3978
3979 if Ctype = Fully_Conformant then
3980
3981 -- Names must match. Error message is more accurate if we do
3982 -- this before checking that the types of the formals match.
3983
3984 if Chars (Old_Formal) /= Chars (New_Formal) then
3985 Conformance_Error ("\name & does not match!", New_Formal);
3986
3987 -- Set error posted flag on new formal as well to stop
3988 -- junk cascaded messages in some cases.
3989
3990 Set_Error_Posted (New_Formal);
3991 return;
3992 end if;
3993
3994 -- Null exclusion must match
3995
3996 if Null_Exclusion_Present (Parent (Old_Formal))
3997 /=
3998 Null_Exclusion_Present (Parent (New_Formal))
3999 then
4000 -- Only give error if both come from source. This should be
4001 -- investigated some time, since it should not be needed ???
4002
4003 if Comes_From_Source (Old_Formal)
4004 and then
4005 Comes_From_Source (New_Formal)
4006 then
4007 Conformance_Error
4008 ("\null exclusion for & does not match", New_Formal);
4009
4010 -- Mark error posted on the new formal to avoid duplicated
4011 -- complaint about types not matching.
4012
4013 Set_Error_Posted (New_Formal);
4014 end if;
4015 end if;
4016 end if;
4017
4018 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
4019 -- case occurs whenever a subprogram is being renamed and one of its
4020 -- parameters imposes a null exclusion. For example:
4021
4022 -- type T is null record;
4023 -- type Acc_T is access T;
4024 -- subtype Acc_T_Sub is Acc_T;
4025
4026 -- procedure P (Obj : not null Acc_T_Sub); -- itype
4027 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
4028 -- renames P;
4029
4030 Old_Formal_Base := Etype (Old_Formal);
4031 New_Formal_Base := Etype (New_Formal);
4032
4033 if Get_Inst then
4034 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
4035 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
4036 end if;
4037
4038 Access_Types_Match := Ada_Version >= Ada_2005
4039
4040 -- Ensure that this rule is only applied when New_Id is a
4041 -- renaming of Old_Id.
4042
4043 and then Nkind (Parent (Parent (New_Id))) =
4044 N_Subprogram_Renaming_Declaration
4045 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
4046 and then Present (Entity (Name (Parent (Parent (New_Id)))))
4047 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
4048
4049 -- Now handle the allowed access-type case
4050
4051 and then Is_Access_Type (Old_Formal_Base)
4052 and then Is_Access_Type (New_Formal_Base)
4053
4054 -- The type kinds must match. The only exception occurs with
4055 -- multiple generics of the form:
4056
4057 -- generic generic
4058 -- type F is private; type A is private;
4059 -- type F_Ptr is access F; type A_Ptr is access A;
4060 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
4061 -- package F_Pack is ... package A_Pack is
4062 -- package F_Inst is
4063 -- new F_Pack (A, A_Ptr, A_P);
4064
4065 -- When checking for conformance between the parameters of A_P
4066 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
4067 -- because the compiler has transformed A_Ptr into a subtype of
4068 -- F_Ptr. We catch this case in the code below.
4069
4070 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
4071 or else
4072 (Is_Generic_Type (Old_Formal_Base)
4073 and then Is_Generic_Type (New_Formal_Base)
4074 and then Is_Internal (New_Formal_Base)
4075 and then Etype (Etype (New_Formal_Base)) =
4076 Old_Formal_Base))
4077 and then Directly_Designated_Type (Old_Formal_Base) =
4078 Directly_Designated_Type (New_Formal_Base)
4079 and then ((Is_Itype (Old_Formal_Base)
4080 and then Can_Never_Be_Null (Old_Formal_Base))
4081 or else
4082 (Is_Itype (New_Formal_Base)
4083 and then Can_Never_Be_Null (New_Formal_Base)));
4084
4085 -- Types must always match. In the visible part of an instance,
4086 -- usual overloading rules for dispatching operations apply, and
4087 -- we check base types (not the actual subtypes).
4088
4089 if In_Instance_Visible_Part
4090 and then Is_Dispatching_Operation (New_Id)
4091 then
4092 if not Conforming_Types
4093 (T1 => Base_Type (Etype (Old_Formal)),
4094 T2 => Base_Type (Etype (New_Formal)),
4095 Ctype => Ctype,
4096 Get_Inst => Get_Inst)
4097 and then not Access_Types_Match
4098 then
4099 Conformance_Error ("\type of & does not match!", New_Formal);
4100 return;
4101 end if;
4102
4103 elsif not Conforming_Types
4104 (T1 => Old_Formal_Base,
4105 T2 => New_Formal_Base,
4106 Ctype => Ctype,
4107 Get_Inst => Get_Inst)
4108 and then not Access_Types_Match
4109 then
4110 -- Don't give error message if old type is Any_Type. This test
4111 -- avoids some cascaded errors, e.g. in case of a bad spec.
4112
4113 if Errmsg and then Old_Formal_Base = Any_Type then
4114 Conforms := False;
4115 else
4116 Conformance_Error ("\type of & does not match!", New_Formal);
4117 end if;
4118
4119 return;
4120 end if;
4121
4122 -- For mode conformance, mode must match
4123
4124 if Ctype >= Mode_Conformant then
4125 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
4126 Conformance_Error ("\mode of & does not match!", New_Formal);
4127 return;
4128
4129 -- Part of mode conformance for access types is having the same
4130 -- constant modifier.
4131
4132 elsif Access_Types_Match
4133 and then Is_Access_Constant (Old_Formal_Base) /=
4134 Is_Access_Constant (New_Formal_Base)
4135 then
4136 Conformance_Error
4137 ("\constant modifier does not match!", New_Formal);
4138 return;
4139 end if;
4140 end if;
4141
4142 if Ctype >= Subtype_Conformant then
4143
4144 -- Ada 2005 (AI-231): In case of anonymous access types check
4145 -- the null-exclusion and access-to-constant attributes must
4146 -- match.
4147
4148 if Ada_Version >= Ada_2005
4149 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
4150 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
4151 and then
4152 (Can_Never_Be_Null (Old_Formal) /=
4153 Can_Never_Be_Null (New_Formal)
4154 or else
4155 Is_Access_Constant (Etype (Old_Formal)) /=
4156 Is_Access_Constant (Etype (New_Formal)))
4157
4158 -- Do not complain if error already posted on New_Formal. This
4159 -- avoids some redundant error messages.
4160
4161 and then not Error_Posted (New_Formal)
4162 then
4163 -- It is allowed to omit the null-exclusion in case of stream
4164 -- attribute subprograms. We recognize stream subprograms
4165 -- through their TSS-generated suffix.
4166
4167 declare
4168 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
4169 begin
4170 if TSS_Name /= TSS_Stream_Read
4171 and then TSS_Name /= TSS_Stream_Write
4172 and then TSS_Name /= TSS_Stream_Input
4173 and then TSS_Name /= TSS_Stream_Output
4174 then
4175 Conformance_Error
4176 ("\type of & does not match!", New_Formal);
4177 return;
4178 end if;
4179 end;
4180 end if;
4181 end if;
4182
4183 -- Full conformance checks
4184
4185 if Ctype = Fully_Conformant then
4186
4187 -- We have checked already that names match
4188
4189 if Parameter_Mode (Old_Formal) = E_In_Parameter then
4190
4191 -- Check default expressions for in parameters
4192
4193 declare
4194 NewD : constant Boolean :=
4195 Present (Default_Value (New_Formal));
4196 OldD : constant Boolean :=
4197 Present (Default_Value (Old_Formal));
4198 begin
4199 if NewD or OldD then
4200
4201 -- The old default value has been analyzed because the
4202 -- current full declaration will have frozen everything
4203 -- before. The new default value has not been analyzed,
4204 -- so analyze it now before we check for conformance.
4205
4206 if NewD then
4207 Push_Scope (New_Id);
4208 Preanalyze_Spec_Expression
4209 (Default_Value (New_Formal), Etype (New_Formal));
4210 End_Scope;
4211 end if;
4212
4213 if not (NewD and OldD)
4214 or else not Fully_Conformant_Expressions
4215 (Default_Value (Old_Formal),
4216 Default_Value (New_Formal))
4217 then
4218 Conformance_Error
4219 ("\default expression for & does not match!",
4220 New_Formal);
4221 return;
4222 end if;
4223 end if;
4224 end;
4225 end if;
4226 end if;
4227
4228 -- A couple of special checks for Ada 83 mode. These checks are
4229 -- skipped if either entity is an operator in package Standard,
4230 -- or if either old or new instance is not from the source program.
4231
4232 if Ada_Version = Ada_83
4233 and then Sloc (Old_Id) > Standard_Location
4234 and then Sloc (New_Id) > Standard_Location
4235 and then Comes_From_Source (Old_Id)
4236 and then Comes_From_Source (New_Id)
4237 then
4238 declare
4239 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
4240 New_Param : constant Node_Id := Declaration_Node (New_Formal);
4241
4242 begin
4243 -- Explicit IN must be present or absent in both cases. This
4244 -- test is required only in the full conformance case.
4245
4246 if In_Present (Old_Param) /= In_Present (New_Param)
4247 and then Ctype = Fully_Conformant
4248 then
4249 Conformance_Error
4250 ("\(Ada 83) IN must appear in both declarations",
4251 New_Formal);
4252 return;
4253 end if;
4254
4255 -- Grouping (use of comma in param lists) must be the same
4256 -- This is where we catch a misconformance like:
4257
4258 -- A, B : Integer
4259 -- A : Integer; B : Integer
4260
4261 -- which are represented identically in the tree except
4262 -- for the setting of the flags More_Ids and Prev_Ids.
4263
4264 if More_Ids (Old_Param) /= More_Ids (New_Param)
4265 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
4266 then
4267 Conformance_Error
4268 ("\grouping of & does not match!", New_Formal);
4269 return;
4270 end if;
4271 end;
4272 end if;
4273
4274 -- This label is required when skipping controlling formals
4275
4276 <<Skip_Controlling_Formal>>
4277
4278 Next_Formal (Old_Formal);
4279 Next_Formal (New_Formal);
4280 end loop;
4281
4282 if Present (Old_Formal) then
4283 Conformance_Error ("\too few parameters!");
4284 return;
4285
4286 elsif Present (New_Formal) then
4287 Conformance_Error ("\too many parameters!", New_Formal);
4288 return;
4289 end if;
4290 end Check_Conformance;
4291
4292 -----------------------
4293 -- Check_Conventions --
4294 -----------------------
4295
4296 procedure Check_Conventions (Typ : Entity_Id) is
4297 Ifaces_List : Elist_Id;
4298
4299 procedure Check_Convention (Op : Entity_Id);
4300 -- Verify that the convention of inherited dispatching operation Op is
4301 -- consistent among all subprograms it overrides. In order to minimize
4302 -- the search, Search_From is utilized to designate a specific point in
4303 -- the list rather than iterating over the whole list once more.
4304
4305 ----------------------
4306 -- Check_Convention --
4307 ----------------------
4308
4309 procedure Check_Convention (Op : Entity_Id) is
4310 Iface_Elmt : Elmt_Id;
4311 Iface_Prim_Elmt : Elmt_Id;
4312 Iface_Prim : Entity_Id;
4313
4314 begin
4315 Iface_Elmt := First_Elmt (Ifaces_List);
4316 while Present (Iface_Elmt) loop
4317 Iface_Prim_Elmt :=
4318 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
4319 while Present (Iface_Prim_Elmt) loop
4320 Iface_Prim := Node (Iface_Prim_Elmt);
4321
4322 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
4323 and then Convention (Iface_Prim) /= Convention (Op)
4324 then
4325 Error_Msg_N
4326 ("inconsistent conventions in primitive operations", Typ);
4327
4328 Error_Msg_Name_1 := Chars (Op);
4329 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
4330 Error_Msg_Sloc := Sloc (Op);
4331
4332 if Comes_From_Source (Op) or else No (Alias (Op)) then
4333 if not Present (Overridden_Operation (Op)) then
4334 Error_Msg_N ("\\primitive % defined #", Typ);
4335 else
4336 Error_Msg_N
4337 ("\\overriding operation % with " &
4338 "convention % defined #", Typ);
4339 end if;
4340
4341 else pragma Assert (Present (Alias (Op)));
4342 Error_Msg_Sloc := Sloc (Alias (Op));
4343 Error_Msg_N
4344 ("\\inherited operation % with " &
4345 "convention % defined #", Typ);
4346 end if;
4347
4348 Error_Msg_Name_1 := Chars (Op);
4349 Error_Msg_Name_2 :=
4350 Get_Convention_Name (Convention (Iface_Prim));
4351 Error_Msg_Sloc := Sloc (Iface_Prim);
4352 Error_Msg_N
4353 ("\\overridden operation % with " &
4354 "convention % defined #", Typ);
4355
4356 -- Avoid cascading errors
4357
4358 return;
4359 end if;
4360
4361 Next_Elmt (Iface_Prim_Elmt);
4362 end loop;
4363
4364 Next_Elmt (Iface_Elmt);
4365 end loop;
4366 end Check_Convention;
4367
4368 -- Local variables
4369
4370 Prim_Op : Entity_Id;
4371 Prim_Op_Elmt : Elmt_Id;
4372
4373 -- Start of processing for Check_Conventions
4374
4375 begin
4376 if not Has_Interfaces (Typ) then
4377 return;
4378 end if;
4379
4380 Collect_Interfaces (Typ, Ifaces_List);
4381
4382 -- The algorithm checks every overriding dispatching operation against
4383 -- all the corresponding overridden dispatching operations, detecting
4384 -- differences in conventions.
4385
4386 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
4387 while Present (Prim_Op_Elmt) loop
4388 Prim_Op := Node (Prim_Op_Elmt);
4389
4390 -- A small optimization: skip the predefined dispatching operations
4391 -- since they always have the same convention.
4392
4393 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
4394 Check_Convention (Prim_Op);
4395 end if;
4396
4397 Next_Elmt (Prim_Op_Elmt);
4398 end loop;
4399 end Check_Conventions;
4400
4401 ------------------------------
4402 -- Check_Delayed_Subprogram --
4403 ------------------------------
4404
4405 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
4406 F : Entity_Id;
4407
4408 procedure Possible_Freeze (T : Entity_Id);
4409 -- T is the type of either a formal parameter or of the return type.
4410 -- If T is not yet frozen and needs a delayed freeze, then the
4411 -- subprogram itself must be delayed. If T is the limited view of an
4412 -- incomplete type the subprogram must be frozen as well, because
4413 -- T may depend on local types that have not been frozen yet.
4414
4415 ---------------------
4416 -- Possible_Freeze --
4417 ---------------------
4418
4419 procedure Possible_Freeze (T : Entity_Id) is
4420 begin
4421 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
4422 Set_Has_Delayed_Freeze (Designator);
4423
4424 elsif Is_Access_Type (T)
4425 and then Has_Delayed_Freeze (Designated_Type (T))
4426 and then not Is_Frozen (Designated_Type (T))
4427 then
4428 Set_Has_Delayed_Freeze (Designator);
4429
4430 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
4431 Set_Has_Delayed_Freeze (Designator);
4432 end if;
4433
4434 end Possible_Freeze;
4435
4436 -- Start of processing for Check_Delayed_Subprogram
4437
4438 begin
4439 -- All subprograms, including abstract subprograms, may need a freeze
4440 -- node if some formal type or the return type needs one.
4441
4442 Possible_Freeze (Etype (Designator));
4443 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
4444
4445 -- Need delayed freeze if any of the formal types themselves need
4446 -- a delayed freeze and are not yet frozen.
4447
4448 F := First_Formal (Designator);
4449 while Present (F) loop
4450 Possible_Freeze (Etype (F));
4451 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
4452 Next_Formal (F);
4453 end loop;
4454
4455 -- Mark functions that return by reference. Note that it cannot be
4456 -- done for delayed_freeze subprograms because the underlying
4457 -- returned type may not be known yet (for private types)
4458
4459 if not Has_Delayed_Freeze (Designator)
4460 and then Expander_Active
4461 then
4462 declare
4463 Typ : constant Entity_Id := Etype (Designator);
4464 Utyp : constant Entity_Id := Underlying_Type (Typ);
4465
4466 begin
4467 if Is_Immutably_Limited_Type (Typ) then
4468 Set_Returns_By_Ref (Designator);
4469
4470 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
4471 Set_Returns_By_Ref (Designator);
4472 end if;
4473 end;
4474 end if;
4475 end Check_Delayed_Subprogram;
4476
4477 ------------------------------------
4478 -- Check_Discriminant_Conformance --
4479 ------------------------------------
4480
4481 procedure Check_Discriminant_Conformance
4482 (N : Node_Id;
4483 Prev : Entity_Id;
4484 Prev_Loc : Node_Id)
4485 is
4486 Old_Discr : Entity_Id := First_Discriminant (Prev);
4487 New_Discr : Node_Id := First (Discriminant_Specifications (N));
4488 New_Discr_Id : Entity_Id;
4489 New_Discr_Type : Entity_Id;
4490
4491 procedure Conformance_Error (Msg : String; N : Node_Id);
4492 -- Post error message for conformance error on given node. Two messages
4493 -- are output. The first points to the previous declaration with a
4494 -- general "no conformance" message. The second is the detailed reason,
4495 -- supplied as Msg. The parameter N provide information for a possible
4496 -- & insertion in the message.
4497
4498 -----------------------
4499 -- Conformance_Error --
4500 -----------------------
4501
4502 procedure Conformance_Error (Msg : String; N : Node_Id) is
4503 begin
4504 Error_Msg_Sloc := Sloc (Prev_Loc);
4505 Error_Msg_N -- CODEFIX
4506 ("not fully conformant with declaration#!", N);
4507 Error_Msg_NE (Msg, N, N);
4508 end Conformance_Error;
4509
4510 -- Start of processing for Check_Discriminant_Conformance
4511
4512 begin
4513 while Present (Old_Discr) and then Present (New_Discr) loop
4514
4515 New_Discr_Id := Defining_Identifier (New_Discr);
4516
4517 -- The subtype mark of the discriminant on the full type has not
4518 -- been analyzed so we do it here. For an access discriminant a new
4519 -- type is created.
4520
4521 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
4522 New_Discr_Type :=
4523 Access_Definition (N, Discriminant_Type (New_Discr));
4524
4525 else
4526 Analyze (Discriminant_Type (New_Discr));
4527 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
4528
4529 -- Ada 2005: if the discriminant definition carries a null
4530 -- exclusion, create an itype to check properly for consistency
4531 -- with partial declaration.
4532
4533 if Is_Access_Type (New_Discr_Type)
4534 and then Null_Exclusion_Present (New_Discr)
4535 then
4536 New_Discr_Type :=
4537 Create_Null_Excluding_Itype
4538 (T => New_Discr_Type,
4539 Related_Nod => New_Discr,
4540 Scope_Id => Current_Scope);
4541 end if;
4542 end if;
4543
4544 if not Conforming_Types
4545 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
4546 then
4547 Conformance_Error ("type of & does not match!", New_Discr_Id);
4548 return;
4549 else
4550 -- Treat the new discriminant as an occurrence of the old one,
4551 -- for navigation purposes, and fill in some semantic
4552 -- information, for completeness.
4553
4554 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
4555 Set_Etype (New_Discr_Id, Etype (Old_Discr));
4556 Set_Scope (New_Discr_Id, Scope (Old_Discr));
4557 end if;
4558
4559 -- Names must match
4560
4561 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
4562 Conformance_Error ("name & does not match!", New_Discr_Id);
4563 return;
4564 end if;
4565
4566 -- Default expressions must match
4567
4568 declare
4569 NewD : constant Boolean :=
4570 Present (Expression (New_Discr));
4571 OldD : constant Boolean :=
4572 Present (Expression (Parent (Old_Discr)));
4573
4574 begin
4575 if NewD or OldD then
4576
4577 -- The old default value has been analyzed and expanded,
4578 -- because the current full declaration will have frozen
4579 -- everything before. The new default values have not been
4580 -- expanded, so expand now to check conformance.
4581
4582 if NewD then
4583 Preanalyze_Spec_Expression
4584 (Expression (New_Discr), New_Discr_Type);
4585 end if;
4586
4587 if not (NewD and OldD)
4588 or else not Fully_Conformant_Expressions
4589 (Expression (Parent (Old_Discr)),
4590 Expression (New_Discr))
4591
4592 then
4593 Conformance_Error
4594 ("default expression for & does not match!",
4595 New_Discr_Id);
4596 return;
4597 end if;
4598 end if;
4599 end;
4600
4601 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
4602
4603 if Ada_Version = Ada_83 then
4604 declare
4605 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
4606
4607 begin
4608 -- Grouping (use of comma in param lists) must be the same
4609 -- This is where we catch a misconformance like:
4610
4611 -- A,B : Integer
4612 -- A : Integer; B : Integer
4613
4614 -- which are represented identically in the tree except
4615 -- for the setting of the flags More_Ids and Prev_Ids.
4616
4617 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
4618 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
4619 then
4620 Conformance_Error
4621 ("grouping of & does not match!", New_Discr_Id);
4622 return;
4623 end if;
4624 end;
4625 end if;
4626
4627 Next_Discriminant (Old_Discr);
4628 Next (New_Discr);
4629 end loop;
4630
4631 if Present (Old_Discr) then
4632 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
4633 return;
4634
4635 elsif Present (New_Discr) then
4636 Conformance_Error
4637 ("too many discriminants!", Defining_Identifier (New_Discr));
4638 return;
4639 end if;
4640 end Check_Discriminant_Conformance;
4641
4642 ----------------------------
4643 -- Check_Fully_Conformant --
4644 ----------------------------
4645
4646 procedure Check_Fully_Conformant
4647 (New_Id : Entity_Id;
4648 Old_Id : Entity_Id;
4649 Err_Loc : Node_Id := Empty)
4650 is
4651 Result : Boolean;
4652 pragma Warnings (Off, Result);
4653 begin
4654 Check_Conformance
4655 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
4656 end Check_Fully_Conformant;
4657
4658 ---------------------------
4659 -- Check_Mode_Conformant --
4660 ---------------------------
4661
4662 procedure Check_Mode_Conformant
4663 (New_Id : Entity_Id;
4664 Old_Id : Entity_Id;
4665 Err_Loc : Node_Id := Empty;
4666 Get_Inst : Boolean := False)
4667 is
4668 Result : Boolean;
4669 pragma Warnings (Off, Result);
4670 begin
4671 Check_Conformance
4672 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
4673 end Check_Mode_Conformant;
4674
4675 --------------------------------
4676 -- Check_Overriding_Indicator --
4677 --------------------------------
4678
4679 procedure Check_Overriding_Indicator
4680 (Subp : Entity_Id;
4681 Overridden_Subp : Entity_Id;
4682 Is_Primitive : Boolean)
4683 is
4684 Decl : Node_Id;
4685 Spec : Node_Id;
4686
4687 begin
4688 -- No overriding indicator for literals
4689
4690 if Ekind (Subp) = E_Enumeration_Literal then
4691 return;
4692
4693 elsif Ekind (Subp) = E_Entry then
4694 Decl := Parent (Subp);
4695
4696 -- No point in analyzing a malformed operator
4697
4698 elsif Nkind (Subp) = N_Defining_Operator_Symbol
4699 and then Error_Posted (Subp)
4700 then
4701 return;
4702
4703 else
4704 Decl := Unit_Declaration_Node (Subp);
4705 end if;
4706
4707 if Nkind_In (Decl, N_Subprogram_Body,
4708 N_Subprogram_Body_Stub,
4709 N_Subprogram_Declaration,
4710 N_Abstract_Subprogram_Declaration,
4711 N_Subprogram_Renaming_Declaration)
4712 then
4713 Spec := Specification (Decl);
4714
4715 elsif Nkind (Decl) = N_Entry_Declaration then
4716 Spec := Decl;
4717
4718 else
4719 return;
4720 end if;
4721
4722 -- The overriding operation is type conformant with the overridden one,
4723 -- but the names of the formals are not required to match. If the names
4724 -- appear permuted in the overriding operation, this is a possible
4725 -- source of confusion that is worth diagnosing. Controlling formals
4726 -- often carry names that reflect the type, and it is not worthwhile
4727 -- requiring that their names match.
4728
4729 if Present (Overridden_Subp)
4730 and then Nkind (Subp) /= N_Defining_Operator_Symbol
4731 then
4732 declare
4733 Form1 : Entity_Id;
4734 Form2 : Entity_Id;
4735
4736 begin
4737 Form1 := First_Formal (Subp);
4738 Form2 := First_Formal (Overridden_Subp);
4739
4740 -- If the overriding operation is a synchronized operation, skip
4741 -- the first parameter of the overridden operation, which is
4742 -- implicit in the new one. If the operation is declared in the
4743 -- body it is not primitive and all formals must match.
4744
4745 if Is_Concurrent_Type (Scope (Subp))
4746 and then Is_Tagged_Type (Scope (Subp))
4747 and then not Has_Completion (Scope (Subp))
4748 then
4749 Form2 := Next_Formal (Form2);
4750 end if;
4751
4752 if Present (Form1) then
4753 Form1 := Next_Formal (Form1);
4754 Form2 := Next_Formal (Form2);
4755 end if;
4756
4757 while Present (Form1) loop
4758 if not Is_Controlling_Formal (Form1)
4759 and then Present (Next_Formal (Form2))
4760 and then Chars (Form1) = Chars (Next_Formal (Form2))
4761 then
4762 Error_Msg_Node_2 := Alias (Overridden_Subp);
4763 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
4764 Error_Msg_NE
4765 ("& does not match corresponding formal of&#",
4766 Form1, Form1);
4767 exit;
4768 end if;
4769
4770 Next_Formal (Form1);
4771 Next_Formal (Form2);
4772 end loop;
4773 end;
4774 end if;
4775
4776 -- If there is an overridden subprogram, then check that there is no
4777 -- "not overriding" indicator, and mark the subprogram as overriding.
4778 -- This is not done if the overridden subprogram is marked as hidden,
4779 -- which can occur for the case of inherited controlled operations
4780 -- (see Derive_Subprogram), unless the inherited subprogram's parent
4781 -- subprogram is not itself hidden. (Note: This condition could probably
4782 -- be simplified, leaving out the testing for the specific controlled
4783 -- cases, but it seems safer and clearer this way, and echoes similar
4784 -- special-case tests of this kind in other places.)
4785
4786 if Present (Overridden_Subp)
4787 and then (not Is_Hidden (Overridden_Subp)
4788 or else
4789 ((Chars (Overridden_Subp) = Name_Initialize
4790 or else
4791 Chars (Overridden_Subp) = Name_Adjust
4792 or else
4793 Chars (Overridden_Subp) = Name_Finalize)
4794 and then Present (Alias (Overridden_Subp))
4795 and then not Is_Hidden (Alias (Overridden_Subp))))
4796 then
4797 if Must_Not_Override (Spec) then
4798 Error_Msg_Sloc := Sloc (Overridden_Subp);
4799
4800 if Ekind (Subp) = E_Entry then
4801 Error_Msg_NE
4802 ("entry & overrides inherited operation #", Spec, Subp);
4803 else
4804 Error_Msg_NE
4805 ("subprogram & overrides inherited operation #", Spec, Subp);
4806 end if;
4807
4808 elsif Is_Subprogram (Subp) then
4809 if No (Overridden_Operation (Subp)) then
4810
4811 -- For entities generated by Derive_Subprograms the overridden
4812 -- operation is the inherited primitive (which is available
4813 -- through the attribute alias)
4814
4815 if (Is_Dispatching_Operation (Subp)
4816 or else Is_Dispatching_Operation (Overridden_Subp))
4817 and then not Comes_From_Source (Overridden_Subp)
4818 and then Find_Dispatching_Type (Overridden_Subp) =
4819 Find_Dispatching_Type (Subp)
4820 and then Present (Alias (Overridden_Subp))
4821 and then Comes_From_Source (Alias (Overridden_Subp))
4822 then
4823 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
4824 else
4825 Set_Overridden_Operation (Subp, Overridden_Subp);
4826 end if;
4827 end if;
4828 end if;
4829
4830 -- If primitive flag is set or this is a protected operation, then
4831 -- the operation is overriding at the point of its declaration, so
4832 -- warn if necessary. Otherwise it may have been declared before the
4833 -- operation it overrides and no check is required.
4834
4835 if Style_Check
4836 and then not Must_Override (Spec)
4837 and then (Is_Primitive
4838 or else Ekind (Scope (Subp)) = E_Protected_Type)
4839 then
4840 Style.Missing_Overriding (Decl, Subp);
4841 end if;
4842
4843 -- If Subp is an operator, it may override a predefined operation, if
4844 -- it is defined in the same scope as the type to which it applies.
4845 -- In that case Overridden_Subp is empty because of our implicit
4846 -- representation for predefined operators. We have to check whether the
4847 -- signature of Subp matches that of a predefined operator. Note that
4848 -- first argument provides the name of the operator, and the second
4849 -- argument the signature that may match that of a standard operation.
4850 -- If the indicator is overriding, then the operator must match a
4851 -- predefined signature, because we know already that there is no
4852 -- explicit overridden operation.
4853
4854 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
4855 if Must_Not_Override (Spec) then
4856
4857 -- If this is not a primitive or a protected subprogram, then
4858 -- "not overriding" is illegal.
4859
4860 if not Is_Primitive
4861 and then Ekind (Scope (Subp)) /= E_Protected_Type
4862 then
4863 Error_Msg_N
4864 ("overriding indicator only allowed "
4865 & "if subprogram is primitive", Subp);
4866
4867 elsif Can_Override_Operator (Subp) then
4868 Error_Msg_NE
4869 ("subprogram& overrides predefined operator ", Spec, Subp);
4870 end if;
4871
4872 elsif Must_Override (Spec) then
4873 if No (Overridden_Operation (Subp))
4874 and then not Can_Override_Operator (Subp)
4875 then
4876 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4877 end if;
4878
4879 elsif not Error_Posted (Subp)
4880 and then Style_Check
4881 and then Can_Override_Operator (Subp)
4882 and then
4883 not Is_Predefined_File_Name
4884 (Unit_File_Name (Get_Source_Unit (Subp)))
4885 then
4886 -- If style checks are enabled, indicate that the indicator is
4887 -- missing. However, at the point of declaration, the type of
4888 -- which this is a primitive operation may be private, in which
4889 -- case the indicator would be premature.
4890
4891 if Has_Private_Declaration (Etype (Subp))
4892 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
4893 then
4894 null;
4895 else
4896 Style.Missing_Overriding (Decl, Subp);
4897 end if;
4898 end if;
4899
4900 elsif Must_Override (Spec) then
4901 if Ekind (Subp) = E_Entry then
4902 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
4903 else
4904 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4905 end if;
4906
4907 -- If the operation is marked "not overriding" and it's not primitive
4908 -- then an error is issued, unless this is an operation of a task or
4909 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
4910 -- has been specified have already been checked above.
4911
4912 elsif Must_Not_Override (Spec)
4913 and then not Is_Primitive
4914 and then Ekind (Subp) /= E_Entry
4915 and then Ekind (Scope (Subp)) /= E_Protected_Type
4916 then
4917 Error_Msg_N
4918 ("overriding indicator only allowed if subprogram is primitive",
4919 Subp);
4920 return;
4921 end if;
4922 end Check_Overriding_Indicator;
4923
4924 -------------------
4925 -- Check_Returns --
4926 -------------------
4927
4928 -- Note: this procedure needs to know far too much about how the expander
4929 -- messes with exceptions. The use of the flag Exception_Junk and the
4930 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
4931 -- works, but is not very clean. It would be better if the expansion
4932 -- routines would leave Original_Node working nicely, and we could use
4933 -- Original_Node here to ignore all the peculiar expander messing ???
4934
4935 procedure Check_Returns
4936 (HSS : Node_Id;
4937 Mode : Character;
4938 Err : out Boolean;
4939 Proc : Entity_Id := Empty)
4940 is
4941 Handler : Node_Id;
4942
4943 procedure Check_Statement_Sequence (L : List_Id);
4944 -- Internal recursive procedure to check a list of statements for proper
4945 -- termination by a return statement (or a transfer of control or a
4946 -- compound statement that is itself internally properly terminated).
4947
4948 ------------------------------
4949 -- Check_Statement_Sequence --
4950 ------------------------------
4951
4952 procedure Check_Statement_Sequence (L : List_Id) is
4953 Last_Stm : Node_Id;
4954 Stm : Node_Id;
4955 Kind : Node_Kind;
4956
4957 Raise_Exception_Call : Boolean;
4958 -- Set True if statement sequence terminated by Raise_Exception call
4959 -- or a Reraise_Occurrence call.
4960
4961 begin
4962 Raise_Exception_Call := False;
4963
4964 -- Get last real statement
4965
4966 Last_Stm := Last (L);
4967
4968 -- Deal with digging out exception handler statement sequences that
4969 -- have been transformed by the local raise to goto optimization.
4970 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
4971 -- optimization has occurred, we are looking at something like:
4972
4973 -- begin
4974 -- original stmts in block
4975
4976 -- exception \
4977 -- when excep1 => |
4978 -- goto L1; | omitted if No_Exception_Propagation
4979 -- when excep2 => |
4980 -- goto L2; /
4981 -- end;
4982
4983 -- goto L3; -- skip handler when exception not raised
4984
4985 -- <<L1>> -- target label for local exception
4986 -- begin
4987 -- estmts1
4988 -- end;
4989
4990 -- goto L3;
4991
4992 -- <<L2>>
4993 -- begin
4994 -- estmts2
4995 -- end;
4996
4997 -- <<L3>>
4998
4999 -- and what we have to do is to dig out the estmts1 and estmts2
5000 -- sequences (which were the original sequences of statements in
5001 -- the exception handlers) and check them.
5002
5003 if Nkind (Last_Stm) = N_Label
5004 and then Exception_Junk (Last_Stm)
5005 then
5006 Stm := Last_Stm;
5007 loop
5008 Prev (Stm);
5009 exit when No (Stm);
5010 exit when Nkind (Stm) /= N_Block_Statement;
5011 exit when not Exception_Junk (Stm);
5012 Prev (Stm);
5013 exit when No (Stm);
5014 exit when Nkind (Stm) /= N_Label;
5015 exit when not Exception_Junk (Stm);
5016 Check_Statement_Sequence
5017 (Statements (Handled_Statement_Sequence (Next (Stm))));
5018
5019 Prev (Stm);
5020 Last_Stm := Stm;
5021 exit when No (Stm);
5022 exit when Nkind (Stm) /= N_Goto_Statement;
5023 exit when not Exception_Junk (Stm);
5024 end loop;
5025 end if;
5026
5027 -- Don't count pragmas
5028
5029 while Nkind (Last_Stm) = N_Pragma
5030
5031 -- Don't count call to SS_Release (can happen after Raise_Exception)
5032
5033 or else
5034 (Nkind (Last_Stm) = N_Procedure_Call_Statement
5035 and then
5036 Nkind (Name (Last_Stm)) = N_Identifier
5037 and then
5038 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
5039
5040 -- Don't count exception junk
5041
5042 or else
5043 (Nkind_In (Last_Stm, N_Goto_Statement,
5044 N_Label,
5045 N_Object_Declaration)
5046 and then Exception_Junk (Last_Stm))
5047 or else Nkind (Last_Stm) in N_Push_xxx_Label
5048 or else Nkind (Last_Stm) in N_Pop_xxx_Label
5049 loop
5050 Prev (Last_Stm);
5051 end loop;
5052
5053 -- Here we have the "real" last statement
5054
5055 Kind := Nkind (Last_Stm);
5056
5057 -- Transfer of control, OK. Note that in the No_Return procedure
5058 -- case, we already diagnosed any explicit return statements, so
5059 -- we can treat them as OK in this context.
5060
5061 if Is_Transfer (Last_Stm) then
5062 return;
5063
5064 -- Check cases of explicit non-indirect procedure calls
5065
5066 elsif Kind = N_Procedure_Call_Statement
5067 and then Is_Entity_Name (Name (Last_Stm))
5068 then
5069 -- Check call to Raise_Exception procedure which is treated
5070 -- specially, as is a call to Reraise_Occurrence.
5071
5072 -- We suppress the warning in these cases since it is likely that
5073 -- the programmer really does not expect to deal with the case
5074 -- of Null_Occurrence, and thus would find a warning about a
5075 -- missing return curious, and raising Program_Error does not
5076 -- seem such a bad behavior if this does occur.
5077
5078 -- Note that in the Ada 2005 case for Raise_Exception, the actual
5079 -- behavior will be to raise Constraint_Error (see AI-329).
5080
5081 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
5082 or else
5083 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
5084 then
5085 Raise_Exception_Call := True;
5086
5087 -- For Raise_Exception call, test first argument, if it is
5088 -- an attribute reference for a 'Identity call, then we know
5089 -- that the call cannot possibly return.
5090
5091 declare
5092 Arg : constant Node_Id :=
5093 Original_Node (First_Actual (Last_Stm));
5094 begin
5095 if Nkind (Arg) = N_Attribute_Reference
5096 and then Attribute_Name (Arg) = Name_Identity
5097 then
5098 return;
5099 end if;
5100 end;
5101 end if;
5102
5103 -- If statement, need to look inside if there is an else and check
5104 -- each constituent statement sequence for proper termination.
5105
5106 elsif Kind = N_If_Statement
5107 and then Present (Else_Statements (Last_Stm))
5108 then
5109 Check_Statement_Sequence (Then_Statements (Last_Stm));
5110 Check_Statement_Sequence (Else_Statements (Last_Stm));
5111
5112 if Present (Elsif_Parts (Last_Stm)) then
5113 declare
5114 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
5115
5116 begin
5117 while Present (Elsif_Part) loop
5118 Check_Statement_Sequence (Then_Statements (Elsif_Part));
5119 Next (Elsif_Part);
5120 end loop;
5121 end;
5122 end if;
5123
5124 return;
5125
5126 -- Case statement, check each case for proper termination
5127
5128 elsif Kind = N_Case_Statement then
5129 declare
5130 Case_Alt : Node_Id;
5131 begin
5132 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
5133 while Present (Case_Alt) loop
5134 Check_Statement_Sequence (Statements (Case_Alt));
5135 Next_Non_Pragma (Case_Alt);
5136 end loop;
5137 end;
5138
5139 return;
5140
5141 -- Block statement, check its handled sequence of statements
5142
5143 elsif Kind = N_Block_Statement then
5144 declare
5145 Err1 : Boolean;
5146
5147 begin
5148 Check_Returns
5149 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
5150
5151 if Err1 then
5152 Err := True;
5153 end if;
5154
5155 return;
5156 end;
5157
5158 -- Loop statement. If there is an iteration scheme, we can definitely
5159 -- fall out of the loop. Similarly if there is an exit statement, we
5160 -- can fall out. In either case we need a following return.
5161
5162 elsif Kind = N_Loop_Statement then
5163 if Present (Iteration_Scheme (Last_Stm))
5164 or else Has_Exit (Entity (Identifier (Last_Stm)))
5165 then
5166 null;
5167
5168 -- A loop with no exit statement or iteration scheme is either
5169 -- an infinite loop, or it has some other exit (raise/return).
5170 -- In either case, no warning is required.
5171
5172 else
5173 return;
5174 end if;
5175
5176 -- Timed entry call, check entry call and delay alternatives
5177
5178 -- Note: in expanded code, the timed entry call has been converted
5179 -- to a set of expanded statements on which the check will work
5180 -- correctly in any case.
5181
5182 elsif Kind = N_Timed_Entry_Call then
5183 declare
5184 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5185 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
5186
5187 begin
5188 -- If statement sequence of entry call alternative is missing,
5189 -- then we can definitely fall through, and we post the error
5190 -- message on the entry call alternative itself.
5191
5192 if No (Statements (ECA)) then
5193 Last_Stm := ECA;
5194
5195 -- If statement sequence of delay alternative is missing, then
5196 -- we can definitely fall through, and we post the error
5197 -- message on the delay alternative itself.
5198
5199 -- Note: if both ECA and DCA are missing the return, then we
5200 -- post only one message, should be enough to fix the bugs.
5201 -- If not we will get a message next time on the DCA when the
5202 -- ECA is fixed!
5203
5204 elsif No (Statements (DCA)) then
5205 Last_Stm := DCA;
5206
5207 -- Else check both statement sequences
5208
5209 else
5210 Check_Statement_Sequence (Statements (ECA));
5211 Check_Statement_Sequence (Statements (DCA));
5212 return;
5213 end if;
5214 end;
5215
5216 -- Conditional entry call, check entry call and else part
5217
5218 -- Note: in expanded code, the conditional entry call has been
5219 -- converted to a set of expanded statements on which the check
5220 -- will work correctly in any case.
5221
5222 elsif Kind = N_Conditional_Entry_Call then
5223 declare
5224 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5225
5226 begin
5227 -- If statement sequence of entry call alternative is missing,
5228 -- then we can definitely fall through, and we post the error
5229 -- message on the entry call alternative itself.
5230
5231 if No (Statements (ECA)) then
5232 Last_Stm := ECA;
5233
5234 -- Else check statement sequence and else part
5235
5236 else
5237 Check_Statement_Sequence (Statements (ECA));
5238 Check_Statement_Sequence (Else_Statements (Last_Stm));
5239 return;
5240 end if;
5241 end;
5242 end if;
5243
5244 -- If we fall through, issue appropriate message
5245
5246 if Mode = 'F' then
5247 if not Raise_Exception_Call then
5248 Error_Msg_N
5249 ("?RETURN statement missing following this statement!",
5250 Last_Stm);
5251 Error_Msg_N
5252 ("\?Program_Error may be raised at run time!",
5253 Last_Stm);
5254 end if;
5255
5256 -- Note: we set Err even though we have not issued a warning
5257 -- because we still have a case of a missing return. This is
5258 -- an extremely marginal case, probably will never be noticed
5259 -- but we might as well get it right.
5260
5261 Err := True;
5262
5263 -- Otherwise we have the case of a procedure marked No_Return
5264
5265 else
5266 if not Raise_Exception_Call then
5267 Error_Msg_N
5268 ("?implied return after this statement " &
5269 "will raise Program_Error",
5270 Last_Stm);
5271 Error_Msg_NE
5272 ("\?procedure & is marked as No_Return!",
5273 Last_Stm, Proc);
5274 end if;
5275
5276 declare
5277 RE : constant Node_Id :=
5278 Make_Raise_Program_Error (Sloc (Last_Stm),
5279 Reason => PE_Implicit_Return);
5280 begin
5281 Insert_After (Last_Stm, RE);
5282 Analyze (RE);
5283 end;
5284 end if;
5285 end Check_Statement_Sequence;
5286
5287 -- Start of processing for Check_Returns
5288
5289 begin
5290 Err := False;
5291 Check_Statement_Sequence (Statements (HSS));
5292
5293 if Present (Exception_Handlers (HSS)) then
5294 Handler := First_Non_Pragma (Exception_Handlers (HSS));
5295 while Present (Handler) loop
5296 Check_Statement_Sequence (Statements (Handler));
5297 Next_Non_Pragma (Handler);
5298 end loop;
5299 end if;
5300 end Check_Returns;
5301
5302 ----------------------------
5303 -- Check_Subprogram_Order --
5304 ----------------------------
5305
5306 procedure Check_Subprogram_Order (N : Node_Id) is
5307
5308 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
5309 -- This is used to check if S1 > S2 in the sense required by this
5310 -- test, for example nameab < namec, but name2 < name10.
5311
5312 -----------------------------
5313 -- Subprogram_Name_Greater --
5314 -----------------------------
5315
5316 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
5317 L1, L2 : Positive;
5318 N1, N2 : Natural;
5319
5320 begin
5321 -- Remove trailing numeric parts
5322
5323 L1 := S1'Last;
5324 while S1 (L1) in '0' .. '9' loop
5325 L1 := L1 - 1;
5326 end loop;
5327
5328 L2 := S2'Last;
5329 while S2 (L2) in '0' .. '9' loop
5330 L2 := L2 - 1;
5331 end loop;
5332
5333 -- If non-numeric parts non-equal, that's decisive
5334
5335 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
5336 return False;
5337
5338 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
5339 return True;
5340
5341 -- If non-numeric parts equal, compare suffixed numeric parts. Note
5342 -- that a missing suffix is treated as numeric zero in this test.
5343
5344 else
5345 N1 := 0;
5346 while L1 < S1'Last loop
5347 L1 := L1 + 1;
5348 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
5349 end loop;
5350
5351 N2 := 0;
5352 while L2 < S2'Last loop
5353 L2 := L2 + 1;
5354 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
5355 end loop;
5356
5357 return N1 > N2;
5358 end if;
5359 end Subprogram_Name_Greater;
5360
5361 -- Start of processing for Check_Subprogram_Order
5362
5363 begin
5364 -- Check body in alpha order if this is option
5365
5366 if Style_Check
5367 and then Style_Check_Order_Subprograms
5368 and then Nkind (N) = N_Subprogram_Body
5369 and then Comes_From_Source (N)
5370 and then In_Extended_Main_Source_Unit (N)
5371 then
5372 declare
5373 LSN : String_Ptr
5374 renames Scope_Stack.Table
5375 (Scope_Stack.Last).Last_Subprogram_Name;
5376
5377 Body_Id : constant Entity_Id :=
5378 Defining_Entity (Specification (N));
5379
5380 begin
5381 Get_Decoded_Name_String (Chars (Body_Id));
5382
5383 if LSN /= null then
5384 if Subprogram_Name_Greater
5385 (LSN.all, Name_Buffer (1 .. Name_Len))
5386 then
5387 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
5388 end if;
5389
5390 Free (LSN);
5391 end if;
5392
5393 LSN := new String'(Name_Buffer (1 .. Name_Len));
5394 end;
5395 end if;
5396 end Check_Subprogram_Order;
5397
5398 ------------------------------
5399 -- Check_Subtype_Conformant --
5400 ------------------------------
5401
5402 procedure Check_Subtype_Conformant
5403 (New_Id : Entity_Id;
5404 Old_Id : Entity_Id;
5405 Err_Loc : Node_Id := Empty;
5406 Skip_Controlling_Formals : Boolean := False)
5407 is
5408 Result : Boolean;
5409 pragma Warnings (Off, Result);
5410 begin
5411 Check_Conformance
5412 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
5413 Skip_Controlling_Formals => Skip_Controlling_Formals);
5414 end Check_Subtype_Conformant;
5415
5416 ---------------------------
5417 -- Check_Type_Conformant --
5418 ---------------------------
5419
5420 procedure Check_Type_Conformant
5421 (New_Id : Entity_Id;
5422 Old_Id : Entity_Id;
5423 Err_Loc : Node_Id := Empty)
5424 is
5425 Result : Boolean;
5426 pragma Warnings (Off, Result);
5427 begin
5428 Check_Conformance
5429 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
5430 end Check_Type_Conformant;
5431
5432 ---------------------------
5433 -- Can_Override_Operator --
5434 ---------------------------
5435
5436 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
5437 Typ : Entity_Id;
5438 begin
5439 if Nkind (Subp) /= N_Defining_Operator_Symbol then
5440 return False;
5441
5442 else
5443 Typ := Base_Type (Etype (First_Formal (Subp)));
5444
5445 return Operator_Matches_Spec (Subp, Subp)
5446 and then Scope (Subp) = Scope (Typ)
5447 and then not Is_Class_Wide_Type (Typ);
5448 end if;
5449 end Can_Override_Operator;
5450
5451 ----------------------
5452 -- Conforming_Types --
5453 ----------------------
5454
5455 function Conforming_Types
5456 (T1 : Entity_Id;
5457 T2 : Entity_Id;
5458 Ctype : Conformance_Type;
5459 Get_Inst : Boolean := False) return Boolean
5460 is
5461 Type_1 : Entity_Id := T1;
5462 Type_2 : Entity_Id := T2;
5463 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
5464
5465 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
5466 -- If neither T1 nor T2 are generic actual types, or if they are in
5467 -- different scopes (e.g. parent and child instances), then verify that
5468 -- the base types are equal. Otherwise T1 and T2 must be on the same
5469 -- subtype chain. The whole purpose of this procedure is to prevent
5470 -- spurious ambiguities in an instantiation that may arise if two
5471 -- distinct generic types are instantiated with the same actual.
5472
5473 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
5474 -- An access parameter can designate an incomplete type. If the
5475 -- incomplete type is the limited view of a type from a limited_
5476 -- with_clause, check whether the non-limited view is available. If
5477 -- it is a (non-limited) incomplete type, get the full view.
5478
5479 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
5480 -- Returns True if and only if either T1 denotes a limited view of T2
5481 -- or T2 denotes a limited view of T1. This can arise when the limited
5482 -- with view of a type is used in a subprogram declaration and the
5483 -- subprogram body is in the scope of a regular with clause for the
5484 -- same unit. In such a case, the two type entities can be considered
5485 -- identical for purposes of conformance checking.
5486
5487 ----------------------
5488 -- Base_Types_Match --
5489 ----------------------
5490
5491 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
5492 begin
5493 if T1 = T2 then
5494 return True;
5495
5496 elsif Base_Type (T1) = Base_Type (T2) then
5497
5498 -- The following is too permissive. A more precise test should
5499 -- check that the generic actual is an ancestor subtype of the
5500 -- other ???.
5501
5502 return not Is_Generic_Actual_Type (T1)
5503 or else not Is_Generic_Actual_Type (T2)
5504 or else Scope (T1) /= Scope (T2);
5505
5506 else
5507 return False;
5508 end if;
5509 end Base_Types_Match;
5510
5511 --------------------------
5512 -- Find_Designated_Type --
5513 --------------------------
5514
5515 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
5516 Desig : Entity_Id;
5517
5518 begin
5519 Desig := Directly_Designated_Type (T);
5520
5521 if Ekind (Desig) = E_Incomplete_Type then
5522
5523 -- If regular incomplete type, get full view if available
5524
5525 if Present (Full_View (Desig)) then
5526 Desig := Full_View (Desig);
5527
5528 -- If limited view of a type, get non-limited view if available,
5529 -- and check again for a regular incomplete type.
5530
5531 elsif Present (Non_Limited_View (Desig)) then
5532 Desig := Get_Full_View (Non_Limited_View (Desig));
5533 end if;
5534 end if;
5535
5536 return Desig;
5537 end Find_Designated_Type;
5538
5539 -------------------------------
5540 -- Matches_Limited_With_View --
5541 -------------------------------
5542
5543 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
5544 begin
5545 -- In some cases a type imported through a limited_with clause, and
5546 -- its nonlimited view are both visible, for example in an anonymous
5547 -- access-to-class-wide type in a formal. Both entities designate the
5548 -- same type.
5549
5550 if From_With_Type (T1)
5551 and then T2 = Available_View (T1)
5552 then
5553 return True;
5554
5555 elsif From_With_Type (T2)
5556 and then T1 = Available_View (T2)
5557 then
5558 return True;
5559
5560 else
5561 return False;
5562 end if;
5563 end Matches_Limited_With_View;
5564
5565 -- Start of processing for Conforming_Types
5566
5567 begin
5568 -- The context is an instance association for a formal
5569 -- access-to-subprogram type; the formal parameter types require
5570 -- mapping because they may denote other formal parameters of the
5571 -- generic unit.
5572
5573 if Get_Inst then
5574 Type_1 := Get_Instance_Of (T1);
5575 Type_2 := Get_Instance_Of (T2);
5576 end if;
5577
5578 -- If one of the types is a view of the other introduced by a limited
5579 -- with clause, treat these as conforming for all purposes.
5580
5581 if Matches_Limited_With_View (T1, T2) then
5582 return True;
5583
5584 elsif Base_Types_Match (Type_1, Type_2) then
5585 return Ctype <= Mode_Conformant
5586 or else Subtypes_Statically_Match (Type_1, Type_2);
5587
5588 elsif Is_Incomplete_Or_Private_Type (Type_1)
5589 and then Present (Full_View (Type_1))
5590 and then Base_Types_Match (Full_View (Type_1), Type_2)
5591 then
5592 return Ctype <= Mode_Conformant
5593 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
5594
5595 elsif Ekind (Type_2) = E_Incomplete_Type
5596 and then Present (Full_View (Type_2))
5597 and then Base_Types_Match (Type_1, Full_View (Type_2))
5598 then
5599 return Ctype <= Mode_Conformant
5600 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5601
5602 elsif Is_Private_Type (Type_2)
5603 and then In_Instance
5604 and then Present (Full_View (Type_2))
5605 and then Base_Types_Match (Type_1, Full_View (Type_2))
5606 then
5607 return Ctype <= Mode_Conformant
5608 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5609 end if;
5610
5611 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
5612 -- treated recursively because they carry a signature.
5613
5614 Are_Anonymous_Access_To_Subprogram_Types :=
5615 Ekind (Type_1) = Ekind (Type_2)
5616 and then
5617 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
5618 or else
5619 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
5620
5621 -- Test anonymous access type case. For this case, static subtype
5622 -- matching is required for mode conformance (RM 6.3.1(15)). We check
5623 -- the base types because we may have built internal subtype entities
5624 -- to handle null-excluding types (see Process_Formals).
5625
5626 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
5627 and then
5628 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
5629 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
5630 then
5631 declare
5632 Desig_1 : Entity_Id;
5633 Desig_2 : Entity_Id;
5634
5635 begin
5636 -- In Ada2005, access constant indicators must match for
5637 -- subtype conformance.
5638
5639 if Ada_Version >= Ada_2005
5640 and then Ctype >= Subtype_Conformant
5641 and then
5642 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
5643 then
5644 return False;
5645 end if;
5646
5647 Desig_1 := Find_Designated_Type (Type_1);
5648 Desig_2 := Find_Designated_Type (Type_2);
5649
5650 -- If the context is an instance association for a formal
5651 -- access-to-subprogram type; formal access parameter designated
5652 -- types require mapping because they may denote other formal
5653 -- parameters of the generic unit.
5654
5655 if Get_Inst then
5656 Desig_1 := Get_Instance_Of (Desig_1);
5657 Desig_2 := Get_Instance_Of (Desig_2);
5658 end if;
5659
5660 -- It is possible for a Class_Wide_Type to be introduced for an
5661 -- incomplete type, in which case there is a separate class_ wide
5662 -- type for the full view. The types conform if their Etypes
5663 -- conform, i.e. one may be the full view of the other. This can
5664 -- only happen in the context of an access parameter, other uses
5665 -- of an incomplete Class_Wide_Type are illegal.
5666
5667 if Is_Class_Wide_Type (Desig_1)
5668 and then
5669 Is_Class_Wide_Type (Desig_2)
5670 then
5671 return
5672 Conforming_Types
5673 (Etype (Base_Type (Desig_1)),
5674 Etype (Base_Type (Desig_2)), Ctype);
5675
5676 elsif Are_Anonymous_Access_To_Subprogram_Types then
5677 if Ada_Version < Ada_2005 then
5678 return Ctype = Type_Conformant
5679 or else
5680 Subtypes_Statically_Match (Desig_1, Desig_2);
5681
5682 -- We must check the conformance of the signatures themselves
5683
5684 else
5685 declare
5686 Conformant : Boolean;
5687 begin
5688 Check_Conformance
5689 (Desig_1, Desig_2, Ctype, False, Conformant);
5690 return Conformant;
5691 end;
5692 end if;
5693
5694 else
5695 return Base_Type (Desig_1) = Base_Type (Desig_2)
5696 and then (Ctype = Type_Conformant
5697 or else
5698 Subtypes_Statically_Match (Desig_1, Desig_2));
5699 end if;
5700 end;
5701
5702 -- Otherwise definitely no match
5703
5704 else
5705 if ((Ekind (Type_1) = E_Anonymous_Access_Type
5706 and then Is_Access_Type (Type_2))
5707 or else (Ekind (Type_2) = E_Anonymous_Access_Type
5708 and then Is_Access_Type (Type_1)))
5709 and then
5710 Conforming_Types
5711 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
5712 then
5713 May_Hide_Profile := True;
5714 end if;
5715
5716 return False;
5717 end if;
5718 end Conforming_Types;
5719
5720 --------------------------
5721 -- Create_Extra_Formals --
5722 --------------------------
5723
5724 procedure Create_Extra_Formals (E : Entity_Id) is
5725 Formal : Entity_Id;
5726 First_Extra : Entity_Id := Empty;
5727 Last_Extra : Entity_Id;
5728 Formal_Type : Entity_Id;
5729 P_Formal : Entity_Id := Empty;
5730
5731 function Add_Extra_Formal
5732 (Assoc_Entity : Entity_Id;
5733 Typ : Entity_Id;
5734 Scope : Entity_Id;
5735 Suffix : String) return Entity_Id;
5736 -- Add an extra formal to the current list of formals and extra formals.
5737 -- The extra formal is added to the end of the list of extra formals,
5738 -- and also returned as the result. These formals are always of mode IN.
5739 -- The new formal has the type Typ, is declared in Scope, and its name
5740 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
5741 -- The following suffixes are currently used. They should not be changed
5742 -- without coordinating with CodePeer, which makes use of these to
5743 -- provide better messages.
5744
5745 -- O denotes the Constrained bit.
5746 -- L denotes the accessibility level.
5747 -- BIP_xxx denotes an extra formal for a build-in-place function. See
5748 -- the full list in exp_ch6.BIP_Formal_Kind.
5749
5750 ----------------------
5751 -- Add_Extra_Formal --
5752 ----------------------
5753
5754 function Add_Extra_Formal
5755 (Assoc_Entity : Entity_Id;
5756 Typ : Entity_Id;
5757 Scope : Entity_Id;
5758 Suffix : String) return Entity_Id
5759 is
5760 EF : constant Entity_Id :=
5761 Make_Defining_Identifier (Sloc (Assoc_Entity),
5762 Chars => New_External_Name (Chars (Assoc_Entity),
5763 Suffix => Suffix));
5764
5765 begin
5766 -- A little optimization. Never generate an extra formal for the
5767 -- _init operand of an initialization procedure, since it could
5768 -- never be used.
5769
5770 if Chars (Formal) = Name_uInit then
5771 return Empty;
5772 end if;
5773
5774 Set_Ekind (EF, E_In_Parameter);
5775 Set_Actual_Subtype (EF, Typ);
5776 Set_Etype (EF, Typ);
5777 Set_Scope (EF, Scope);
5778 Set_Mechanism (EF, Default_Mechanism);
5779 Set_Formal_Validity (EF);
5780
5781 if No (First_Extra) then
5782 First_Extra := EF;
5783 Set_Extra_Formals (Scope, First_Extra);
5784 end if;
5785
5786 if Present (Last_Extra) then
5787 Set_Extra_Formal (Last_Extra, EF);
5788 end if;
5789
5790 Last_Extra := EF;
5791
5792 return EF;
5793 end Add_Extra_Formal;
5794
5795 -- Start of processing for Create_Extra_Formals
5796
5797 begin
5798 -- We never generate extra formals if expansion is not active
5799 -- because we don't need them unless we are generating code.
5800
5801 if not Expander_Active then
5802 return;
5803 end if;
5804
5805 -- If this is a derived subprogram then the subtypes of the parent
5806 -- subprogram's formal parameters will be used to determine the need
5807 -- for extra formals.
5808
5809 if Is_Overloadable (E) and then Present (Alias (E)) then
5810 P_Formal := First_Formal (Alias (E));
5811 end if;
5812
5813 Last_Extra := Empty;
5814 Formal := First_Formal (E);
5815 while Present (Formal) loop
5816 Last_Extra := Formal;
5817 Next_Formal (Formal);
5818 end loop;
5819
5820 -- If Extra_formals were already created, don't do it again. This
5821 -- situation may arise for subprogram types created as part of
5822 -- dispatching calls (see Expand_Dispatching_Call)
5823
5824 if Present (Last_Extra) and then
5825 Present (Extra_Formal (Last_Extra))
5826 then
5827 return;
5828 end if;
5829
5830 -- If the subprogram is a predefined dispatching subprogram then don't
5831 -- generate any extra constrained or accessibility level formals. In
5832 -- general we suppress these for internal subprograms (by not calling
5833 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
5834 -- generated stream attributes do get passed through because extra
5835 -- build-in-place formals are needed in some cases (limited 'Input).
5836
5837 if Is_Predefined_Internal_Operation (E) then
5838 goto Test_For_BIP_Extras;
5839 end if;
5840
5841 Formal := First_Formal (E);
5842 while Present (Formal) loop
5843
5844 -- Create extra formal for supporting the attribute 'Constrained.
5845 -- The case of a private type view without discriminants also
5846 -- requires the extra formal if the underlying type has defaulted
5847 -- discriminants.
5848
5849 if Ekind (Formal) /= E_In_Parameter then
5850 if Present (P_Formal) then
5851 Formal_Type := Etype (P_Formal);
5852 else
5853 Formal_Type := Etype (Formal);
5854 end if;
5855
5856 -- Do not produce extra formals for Unchecked_Union parameters.
5857 -- Jump directly to the end of the loop.
5858
5859 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
5860 goto Skip_Extra_Formal_Generation;
5861 end if;
5862
5863 if not Has_Discriminants (Formal_Type)
5864 and then Ekind (Formal_Type) in Private_Kind
5865 and then Present (Underlying_Type (Formal_Type))
5866 then
5867 Formal_Type := Underlying_Type (Formal_Type);
5868 end if;
5869
5870 -- Suppress the extra formal if formal's subtype is constrained or
5871 -- indefinite, or we're compiling for Ada 2012 and the underlying
5872 -- type is tagged and limited. In Ada 2012, a limited tagged type
5873 -- can have defaulted discriminants, but 'Constrained is required
5874 -- to return True, so the formal is never needed (see AI05-0214).
5875 -- Note that this ensures consistency of calling sequences for
5876 -- dispatching operations when some types in a class have defaults
5877 -- on discriminants and others do not (and requiring the extra
5878 -- formal would introduce distributed overhead).
5879
5880 if Has_Discriminants (Formal_Type)
5881 and then not Is_Constrained (Formal_Type)
5882 and then not Is_Indefinite_Subtype (Formal_Type)
5883 and then (Ada_Version < Ada_2012
5884 or else
5885 not (Is_Tagged_Type (Underlying_Type (Formal_Type))
5886 and then Is_Limited_Type (Formal_Type)))
5887 then
5888 Set_Extra_Constrained
5889 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
5890 end if;
5891 end if;
5892
5893 -- Create extra formal for supporting accessibility checking. This
5894 -- is done for both anonymous access formals and formals of named
5895 -- access types that are marked as controlling formals. The latter
5896 -- case can occur when Expand_Dispatching_Call creates a subprogram
5897 -- type and substitutes the types of access-to-class-wide actuals
5898 -- for the anonymous access-to-specific-type of controlling formals.
5899 -- Base_Type is applied because in cases where there is a null
5900 -- exclusion the formal may have an access subtype.
5901
5902 -- This is suppressed if we specifically suppress accessibility
5903 -- checks at the package level for either the subprogram, or the
5904 -- package in which it resides. However, we do not suppress it
5905 -- simply if the scope has accessibility checks suppressed, since
5906 -- this could cause trouble when clients are compiled with a
5907 -- different suppression setting. The explicit checks at the
5908 -- package level are safe from this point of view.
5909
5910 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
5911 or else (Is_Controlling_Formal (Formal)
5912 and then Is_Access_Type (Base_Type (Etype (Formal)))))
5913 and then not
5914 (Explicit_Suppress (E, Accessibility_Check)
5915 or else
5916 Explicit_Suppress (Scope (E), Accessibility_Check))
5917 and then
5918 (No (P_Formal)
5919 or else Present (Extra_Accessibility (P_Formal)))
5920 then
5921 Set_Extra_Accessibility
5922 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
5923 end if;
5924
5925 -- This label is required when skipping extra formal generation for
5926 -- Unchecked_Union parameters.
5927
5928 <<Skip_Extra_Formal_Generation>>
5929
5930 if Present (P_Formal) then
5931 Next_Formal (P_Formal);
5932 end if;
5933
5934 Next_Formal (Formal);
5935 end loop;
5936
5937 <<Test_For_BIP_Extras>>
5938
5939 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
5940 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
5941
5942 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
5943 declare
5944 Result_Subt : constant Entity_Id := Etype (E);
5945
5946 Discard : Entity_Id;
5947 pragma Warnings (Off, Discard);
5948
5949 begin
5950 -- In the case of functions with unconstrained result subtypes,
5951 -- add a 4-state formal indicating whether the return object is
5952 -- allocated by the caller (1), or should be allocated by the
5953 -- callee on the secondary stack (2), in the global heap (3), or
5954 -- in a user-defined storage pool (4). For the moment we just use
5955 -- Natural for the type of this formal. Note that this formal
5956 -- isn't usually needed in the case where the result subtype is
5957 -- constrained, but it is needed when the function has a tagged
5958 -- result, because generally such functions can be called in a
5959 -- dispatching context and such calls must be handled like calls
5960 -- to a class-wide function.
5961
5962 if not Is_Constrained (Underlying_Type (Result_Subt))
5963 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5964 then
5965 Discard :=
5966 Add_Extra_Formal
5967 (E, Standard_Natural,
5968 E, BIP_Formal_Suffix (BIP_Alloc_Form));
5969 end if;
5970
5971 -- For functions whose result type has controlled parts, we have
5972 -- an extra formal of type System.Finalization_Implementation.
5973 -- Finalizable_Ptr_Ptr. That is, we are passing a pointer to a
5974 -- finalization list (which is itself a pointer). This extra
5975 -- formal is then passed along to Move_Final_List in case of
5976 -- successful completion of a return statement. We cannot pass an
5977 -- 'in out' parameter, because we need to update the finalization
5978 -- list during an abort-deferred region, rather than using
5979 -- copy-back after the function returns. This is true even if we
5980 -- are able to get away with having 'in out' parameters, which are
5981 -- normally illegal for functions. This formal is also needed when
5982 -- the function has a tagged result.
5983
5984 if Needs_BIP_Final_List (E) then
5985 Discard :=
5986 Add_Extra_Formal
5987 (E, RTE (RE_Finalizable_Ptr_Ptr),
5988 E, BIP_Formal_Suffix (BIP_Final_List));
5989 end if;
5990
5991 -- If the result type contains tasks, we have two extra formals:
5992 -- the master of the tasks to be created, and the caller's
5993 -- activation chain.
5994
5995 if Has_Task (Result_Subt) then
5996 Discard :=
5997 Add_Extra_Formal
5998 (E, RTE (RE_Master_Id),
5999 E, BIP_Formal_Suffix (BIP_Master));
6000 Discard :=
6001 Add_Extra_Formal
6002 (E, RTE (RE_Activation_Chain_Access),
6003 E, BIP_Formal_Suffix (BIP_Activation_Chain));
6004 end if;
6005
6006 -- All build-in-place functions get an extra formal that will be
6007 -- passed the address of the return object within the caller.
6008
6009 declare
6010 Formal_Type : constant Entity_Id :=
6011 Create_Itype
6012 (E_Anonymous_Access_Type, E,
6013 Scope_Id => Scope (E));
6014 begin
6015 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
6016 Set_Etype (Formal_Type, Formal_Type);
6017 Set_Depends_On_Private
6018 (Formal_Type, Has_Private_Component (Formal_Type));
6019 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
6020 Set_Is_Access_Constant (Formal_Type, False);
6021
6022 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
6023 -- the designated type comes from the limited view (for
6024 -- back-end purposes).
6025
6026 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
6027
6028 Layout_Type (Formal_Type);
6029
6030 Discard :=
6031 Add_Extra_Formal
6032 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
6033 end;
6034 end;
6035 end if;
6036 end Create_Extra_Formals;
6037
6038 -----------------------------
6039 -- Enter_Overloaded_Entity --
6040 -----------------------------
6041
6042 procedure Enter_Overloaded_Entity (S : Entity_Id) is
6043 E : Entity_Id := Current_Entity_In_Scope (S);
6044 C_E : Entity_Id := Current_Entity (S);
6045
6046 begin
6047 if Present (E) then
6048 Set_Has_Homonym (E);
6049 Set_Has_Homonym (S);
6050 end if;
6051
6052 Set_Is_Immediately_Visible (S);
6053 Set_Scope (S, Current_Scope);
6054
6055 -- Chain new entity if front of homonym in current scope, so that
6056 -- homonyms are contiguous.
6057
6058 if Present (E)
6059 and then E /= C_E
6060 then
6061 while Homonym (C_E) /= E loop
6062 C_E := Homonym (C_E);
6063 end loop;
6064
6065 Set_Homonym (C_E, S);
6066
6067 else
6068 E := C_E;
6069 Set_Current_Entity (S);
6070 end if;
6071
6072 Set_Homonym (S, E);
6073
6074 Append_Entity (S, Current_Scope);
6075 Set_Public_Status (S);
6076
6077 if Debug_Flag_E then
6078 Write_Str ("New overloaded entity chain: ");
6079 Write_Name (Chars (S));
6080
6081 E := S;
6082 while Present (E) loop
6083 Write_Str (" "); Write_Int (Int (E));
6084 E := Homonym (E);
6085 end loop;
6086
6087 Write_Eol;
6088 end if;
6089
6090 -- Generate warning for hiding
6091
6092 if Warn_On_Hiding
6093 and then Comes_From_Source (S)
6094 and then In_Extended_Main_Source_Unit (S)
6095 then
6096 E := S;
6097 loop
6098 E := Homonym (E);
6099 exit when No (E);
6100
6101 -- Warn unless genuine overloading. Do not emit warning on
6102 -- hiding predefined operators in Standard (these are either an
6103 -- (artifact of our implicit declarations, or simple noise) but
6104 -- keep warning on a operator defined on a local subtype, because
6105 -- of the real danger that different operators may be applied in
6106 -- various parts of the program.
6107
6108 -- Note that if E and S have the same scope, there is never any
6109 -- hiding. Either the two conflict, and the program is illegal,
6110 -- or S is overriding an implicit inherited subprogram.
6111
6112 if Scope (E) /= Scope (S)
6113 and then (not Is_Overloadable (E)
6114 or else Subtype_Conformant (E, S))
6115 and then (Is_Immediately_Visible (E)
6116 or else
6117 Is_Potentially_Use_Visible (S))
6118 then
6119 if Scope (E) /= Standard_Standard then
6120 Error_Msg_Sloc := Sloc (E);
6121 Error_Msg_N ("declaration of & hides one#?", S);
6122
6123 elsif Nkind (S) = N_Defining_Operator_Symbol
6124 and then
6125 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
6126 then
6127 Error_Msg_N
6128 ("declaration of & hides predefined operator?", S);
6129 end if;
6130 end if;
6131 end loop;
6132 end if;
6133 end Enter_Overloaded_Entity;
6134
6135 -----------------------------
6136 -- Check_Untagged_Equality --
6137 -----------------------------
6138
6139 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
6140 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
6141 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
6142 Obj_Decl : Node_Id;
6143
6144 begin
6145 if Nkind (Decl) = N_Subprogram_Declaration
6146 and then Is_Record_Type (Typ)
6147 and then not Is_Tagged_Type (Typ)
6148 then
6149 -- If the type is not declared in a package, or if we are in the
6150 -- body of the package or in some other scope, the new operation is
6151 -- not primitive, and therefore legal, though suspicious. If the
6152 -- type is a generic actual (sub)type, the operation is not primitive
6153 -- either because the base type is declared elsewhere.
6154
6155 if Is_Frozen (Typ) then
6156 if Ekind (Scope (Typ)) /= E_Package
6157 or else Scope (Typ) /= Current_Scope
6158 then
6159 null;
6160
6161 elsif Is_Generic_Actual_Type (Typ) then
6162 null;
6163
6164 elsif In_Package_Body (Scope (Typ)) then
6165 Error_Msg_NE
6166 ("equality operator must be declared "
6167 & "before type& is frozen", Eq_Op, Typ);
6168 Error_Msg_N
6169 ("\move declaration to package spec", Eq_Op);
6170
6171 else
6172 Error_Msg_NE
6173 ("equality operator must be declared "
6174 & "before type& is frozen", Eq_Op, Typ);
6175
6176 Obj_Decl := Next (Parent (Typ));
6177 while Present (Obj_Decl)
6178 and then Obj_Decl /= Decl
6179 loop
6180 if Nkind (Obj_Decl) = N_Object_Declaration
6181 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
6182 then
6183 Error_Msg_NE ("type& is frozen by declaration?",
6184 Obj_Decl, Typ);
6185 Error_Msg_N
6186 ("\an equality operator cannot be declared after this "
6187 & "point ('R'M 4.5.2 (9.8)) (Ada 2012))?", Obj_Decl);
6188 exit;
6189 end if;
6190
6191 Next (Obj_Decl);
6192 end loop;
6193 end if;
6194
6195 elsif not In_Same_List (Parent (Typ), Decl)
6196 and then not Is_Limited_Type (Typ)
6197 then
6198
6199 -- This makes it illegal to have a primitive equality declared in
6200 -- the private part if the type is visible.
6201
6202 Error_Msg_N ("equality operator appears too late", Eq_Op);
6203 end if;
6204 end if;
6205 end Check_Untagged_Equality;
6206
6207 -----------------------------
6208 -- Find_Corresponding_Spec --
6209 -----------------------------
6210
6211 function Find_Corresponding_Spec
6212 (N : Node_Id;
6213 Post_Error : Boolean := True) return Entity_Id
6214 is
6215 Spec : constant Node_Id := Specification (N);
6216 Designator : constant Entity_Id := Defining_Entity (Spec);
6217
6218 E : Entity_Id;
6219
6220 begin
6221 E := Current_Entity (Designator);
6222 while Present (E) loop
6223
6224 -- We are looking for a matching spec. It must have the same scope,
6225 -- and the same name, and either be type conformant, or be the case
6226 -- of a library procedure spec and its body (which belong to one
6227 -- another regardless of whether they are type conformant or not).
6228
6229 if Scope (E) = Current_Scope then
6230 if Current_Scope = Standard_Standard
6231 or else (Ekind (E) = Ekind (Designator)
6232 and then Type_Conformant (E, Designator))
6233 then
6234 -- Within an instantiation, we know that spec and body are
6235 -- subtype conformant, because they were subtype conformant
6236 -- in the generic. We choose the subtype-conformant entity
6237 -- here as well, to resolve spurious ambiguities in the
6238 -- instance that were not present in the generic (i.e. when
6239 -- two different types are given the same actual). If we are
6240 -- looking for a spec to match a body, full conformance is
6241 -- expected.
6242
6243 if In_Instance then
6244 Set_Convention (Designator, Convention (E));
6245
6246 if Nkind (N) = N_Subprogram_Body
6247 and then Present (Homonym (E))
6248 and then not Fully_Conformant (E, Designator)
6249 then
6250 goto Next_Entity;
6251
6252 elsif not Subtype_Conformant (E, Designator) then
6253 goto Next_Entity;
6254 end if;
6255 end if;
6256
6257 if not Has_Completion (E) then
6258 if Nkind (N) /= N_Subprogram_Body_Stub then
6259 Set_Corresponding_Spec (N, E);
6260 end if;
6261
6262 Set_Has_Completion (E);
6263 return E;
6264
6265 elsif Nkind (Parent (N)) = N_Subunit then
6266
6267 -- If this is the proper body of a subunit, the completion
6268 -- flag is set when analyzing the stub.
6269
6270 return E;
6271
6272 -- If E is an internal function with a controlling result
6273 -- that was created for an operation inherited by a null
6274 -- extension, it may be overridden by a body without a previous
6275 -- spec (one more reason why these should be shunned). In that
6276 -- case remove the generated body if present, because the
6277 -- current one is the explicit overriding.
6278
6279 elsif Ekind (E) = E_Function
6280 and then Ada_Version >= Ada_2005
6281 and then not Comes_From_Source (E)
6282 and then Has_Controlling_Result (E)
6283 and then Is_Null_Extension (Etype (E))
6284 and then Comes_From_Source (Spec)
6285 then
6286 Set_Has_Completion (E, False);
6287
6288 if Expander_Active
6289 and then Nkind (Parent (E)) = N_Function_Specification
6290 then
6291 Remove
6292 (Unit_Declaration_Node
6293 (Corresponding_Body (Unit_Declaration_Node (E))));
6294
6295 return E;
6296
6297 -- If expansion is disabled, or if the wrapper function has
6298 -- not been generated yet, this a late body overriding an
6299 -- inherited operation, or it is an overriding by some other
6300 -- declaration before the controlling result is frozen. In
6301 -- either case this is a declaration of a new entity.
6302
6303 else
6304 return Empty;
6305 end if;
6306
6307 -- If the body already exists, then this is an error unless
6308 -- the previous declaration is the implicit declaration of a
6309 -- derived subprogram, or this is a spurious overloading in an
6310 -- instance.
6311
6312 elsif No (Alias (E))
6313 and then not Is_Intrinsic_Subprogram (E)
6314 and then not In_Instance
6315 and then Post_Error
6316 then
6317 Error_Msg_Sloc := Sloc (E);
6318
6319 if Is_Imported (E) then
6320 Error_Msg_NE
6321 ("body not allowed for imported subprogram & declared#",
6322 N, E);
6323 else
6324 Error_Msg_NE ("duplicate body for & declared#", N, E);
6325 end if;
6326 end if;
6327
6328 -- Child units cannot be overloaded, so a conformance mismatch
6329 -- between body and a previous spec is an error.
6330
6331 elsif Is_Child_Unit (E)
6332 and then
6333 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
6334 and then
6335 Nkind (Parent (Unit_Declaration_Node (Designator))) =
6336 N_Compilation_Unit
6337 and then Post_Error
6338 then
6339 Error_Msg_N
6340 ("body of child unit does not match previous declaration", N);
6341 end if;
6342 end if;
6343
6344 <<Next_Entity>>
6345 E := Homonym (E);
6346 end loop;
6347
6348 -- On exit, we know that no previous declaration of subprogram exists
6349
6350 return Empty;
6351 end Find_Corresponding_Spec;
6352
6353 ----------------------
6354 -- Fully_Conformant --
6355 ----------------------
6356
6357 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6358 Result : Boolean;
6359 begin
6360 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
6361 return Result;
6362 end Fully_Conformant;
6363
6364 ----------------------------------
6365 -- Fully_Conformant_Expressions --
6366 ----------------------------------
6367
6368 function Fully_Conformant_Expressions
6369 (Given_E1 : Node_Id;
6370 Given_E2 : Node_Id) return Boolean
6371 is
6372 E1 : constant Node_Id := Original_Node (Given_E1);
6373 E2 : constant Node_Id := Original_Node (Given_E2);
6374 -- We always test conformance on original nodes, since it is possible
6375 -- for analysis and/or expansion to make things look as though they
6376 -- conform when they do not, e.g. by converting 1+2 into 3.
6377
6378 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
6379 renames Fully_Conformant_Expressions;
6380
6381 function FCL (L1, L2 : List_Id) return Boolean;
6382 -- Compare elements of two lists for conformance. Elements have to
6383 -- be conformant, and actuals inserted as default parameters do not
6384 -- match explicit actuals with the same value.
6385
6386 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
6387 -- Compare an operator node with a function call
6388
6389 ---------
6390 -- FCL --
6391 ---------
6392
6393 function FCL (L1, L2 : List_Id) return Boolean is
6394 N1, N2 : Node_Id;
6395
6396 begin
6397 if L1 = No_List then
6398 N1 := Empty;
6399 else
6400 N1 := First (L1);
6401 end if;
6402
6403 if L2 = No_List then
6404 N2 := Empty;
6405 else
6406 N2 := First (L2);
6407 end if;
6408
6409 -- Compare two lists, skipping rewrite insertions (we want to
6410 -- compare the original trees, not the expanded versions!)
6411
6412 loop
6413 if Is_Rewrite_Insertion (N1) then
6414 Next (N1);
6415 elsif Is_Rewrite_Insertion (N2) then
6416 Next (N2);
6417 elsif No (N1) then
6418 return No (N2);
6419 elsif No (N2) then
6420 return False;
6421 elsif not FCE (N1, N2) then
6422 return False;
6423 else
6424 Next (N1);
6425 Next (N2);
6426 end if;
6427 end loop;
6428 end FCL;
6429
6430 ---------
6431 -- FCO --
6432 ---------
6433
6434 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
6435 Actuals : constant List_Id := Parameter_Associations (Call_Node);
6436 Act : Node_Id;
6437
6438 begin
6439 if No (Actuals)
6440 or else Entity (Op_Node) /= Entity (Name (Call_Node))
6441 then
6442 return False;
6443
6444 else
6445 Act := First (Actuals);
6446
6447 if Nkind (Op_Node) in N_Binary_Op then
6448 if not FCE (Left_Opnd (Op_Node), Act) then
6449 return False;
6450 end if;
6451
6452 Next (Act);
6453 end if;
6454
6455 return Present (Act)
6456 and then FCE (Right_Opnd (Op_Node), Act)
6457 and then No (Next (Act));
6458 end if;
6459 end FCO;
6460
6461 -- Start of processing for Fully_Conformant_Expressions
6462
6463 begin
6464 -- Non-conformant if paren count does not match. Note: if some idiot
6465 -- complains that we don't do this right for more than 3 levels of
6466 -- parentheses, they will be treated with the respect they deserve!
6467
6468 if Paren_Count (E1) /= Paren_Count (E2) then
6469 return False;
6470
6471 -- If same entities are referenced, then they are conformant even if
6472 -- they have different forms (RM 8.3.1(19-20)).
6473
6474 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
6475 if Present (Entity (E1)) then
6476 return Entity (E1) = Entity (E2)
6477 or else (Chars (Entity (E1)) = Chars (Entity (E2))
6478 and then Ekind (Entity (E1)) = E_Discriminant
6479 and then Ekind (Entity (E2)) = E_In_Parameter);
6480
6481 elsif Nkind (E1) = N_Expanded_Name
6482 and then Nkind (E2) = N_Expanded_Name
6483 and then Nkind (Selector_Name (E1)) = N_Character_Literal
6484 and then Nkind (Selector_Name (E2)) = N_Character_Literal
6485 then
6486 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
6487
6488 else
6489 -- Identifiers in component associations don't always have
6490 -- entities, but their names must conform.
6491
6492 return Nkind (E1) = N_Identifier
6493 and then Nkind (E2) = N_Identifier
6494 and then Chars (E1) = Chars (E2);
6495 end if;
6496
6497 elsif Nkind (E1) = N_Character_Literal
6498 and then Nkind (E2) = N_Expanded_Name
6499 then
6500 return Nkind (Selector_Name (E2)) = N_Character_Literal
6501 and then Chars (E1) = Chars (Selector_Name (E2));
6502
6503 elsif Nkind (E2) = N_Character_Literal
6504 and then Nkind (E1) = N_Expanded_Name
6505 then
6506 return Nkind (Selector_Name (E1)) = N_Character_Literal
6507 and then Chars (E2) = Chars (Selector_Name (E1));
6508
6509 elsif Nkind (E1) in N_Op
6510 and then Nkind (E2) = N_Function_Call
6511 then
6512 return FCO (E1, E2);
6513
6514 elsif Nkind (E2) in N_Op
6515 and then Nkind (E1) = N_Function_Call
6516 then
6517 return FCO (E2, E1);
6518
6519 -- Otherwise we must have the same syntactic entity
6520
6521 elsif Nkind (E1) /= Nkind (E2) then
6522 return False;
6523
6524 -- At this point, we specialize by node type
6525
6526 else
6527 case Nkind (E1) is
6528
6529 when N_Aggregate =>
6530 return
6531 FCL (Expressions (E1), Expressions (E2))
6532 and then
6533 FCL (Component_Associations (E1),
6534 Component_Associations (E2));
6535
6536 when N_Allocator =>
6537 if Nkind (Expression (E1)) = N_Qualified_Expression
6538 or else
6539 Nkind (Expression (E2)) = N_Qualified_Expression
6540 then
6541 return FCE (Expression (E1), Expression (E2));
6542
6543 -- Check that the subtype marks and any constraints
6544 -- are conformant
6545
6546 else
6547 declare
6548 Indic1 : constant Node_Id := Expression (E1);
6549 Indic2 : constant Node_Id := Expression (E2);
6550 Elt1 : Node_Id;
6551 Elt2 : Node_Id;
6552
6553 begin
6554 if Nkind (Indic1) /= N_Subtype_Indication then
6555 return
6556 Nkind (Indic2) /= N_Subtype_Indication
6557 and then Entity (Indic1) = Entity (Indic2);
6558
6559 elsif Nkind (Indic2) /= N_Subtype_Indication then
6560 return
6561 Nkind (Indic1) /= N_Subtype_Indication
6562 and then Entity (Indic1) = Entity (Indic2);
6563
6564 else
6565 if Entity (Subtype_Mark (Indic1)) /=
6566 Entity (Subtype_Mark (Indic2))
6567 then
6568 return False;
6569 end if;
6570
6571 Elt1 := First (Constraints (Constraint (Indic1)));
6572 Elt2 := First (Constraints (Constraint (Indic2)));
6573 while Present (Elt1) and then Present (Elt2) loop
6574 if not FCE (Elt1, Elt2) then
6575 return False;
6576 end if;
6577
6578 Next (Elt1);
6579 Next (Elt2);
6580 end loop;
6581
6582 return True;
6583 end if;
6584 end;
6585 end if;
6586
6587 when N_Attribute_Reference =>
6588 return
6589 Attribute_Name (E1) = Attribute_Name (E2)
6590 and then FCL (Expressions (E1), Expressions (E2));
6591
6592 when N_Binary_Op =>
6593 return
6594 Entity (E1) = Entity (E2)
6595 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
6596 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
6597
6598 when N_Short_Circuit | N_Membership_Test =>
6599 return
6600 FCE (Left_Opnd (E1), Left_Opnd (E2))
6601 and then
6602 FCE (Right_Opnd (E1), Right_Opnd (E2));
6603
6604 when N_Case_Expression =>
6605 declare
6606 Alt1 : Node_Id;
6607 Alt2 : Node_Id;
6608
6609 begin
6610 if not FCE (Expression (E1), Expression (E2)) then
6611 return False;
6612
6613 else
6614 Alt1 := First (Alternatives (E1));
6615 Alt2 := First (Alternatives (E2));
6616 loop
6617 if Present (Alt1) /= Present (Alt2) then
6618 return False;
6619 elsif No (Alt1) then
6620 return True;
6621 end if;
6622
6623 if not FCE (Expression (Alt1), Expression (Alt2))
6624 or else not FCL (Discrete_Choices (Alt1),
6625 Discrete_Choices (Alt2))
6626 then
6627 return False;
6628 end if;
6629
6630 Next (Alt1);
6631 Next (Alt2);
6632 end loop;
6633 end if;
6634 end;
6635
6636 when N_Character_Literal =>
6637 return
6638 Char_Literal_Value (E1) = Char_Literal_Value (E2);
6639
6640 when N_Component_Association =>
6641 return
6642 FCL (Choices (E1), Choices (E2))
6643 and then
6644 FCE (Expression (E1), Expression (E2));
6645
6646 when N_Conditional_Expression =>
6647 return
6648 FCL (Expressions (E1), Expressions (E2));
6649
6650 when N_Explicit_Dereference =>
6651 return
6652 FCE (Prefix (E1), Prefix (E2));
6653
6654 when N_Extension_Aggregate =>
6655 return
6656 FCL (Expressions (E1), Expressions (E2))
6657 and then Null_Record_Present (E1) =
6658 Null_Record_Present (E2)
6659 and then FCL (Component_Associations (E1),
6660 Component_Associations (E2));
6661
6662 when N_Function_Call =>
6663 return
6664 FCE (Name (E1), Name (E2))
6665 and then
6666 FCL (Parameter_Associations (E1),
6667 Parameter_Associations (E2));
6668
6669 when N_Indexed_Component =>
6670 return
6671 FCE (Prefix (E1), Prefix (E2))
6672 and then
6673 FCL (Expressions (E1), Expressions (E2));
6674
6675 when N_Integer_Literal =>
6676 return (Intval (E1) = Intval (E2));
6677
6678 when N_Null =>
6679 return True;
6680
6681 when N_Operator_Symbol =>
6682 return
6683 Chars (E1) = Chars (E2);
6684
6685 when N_Others_Choice =>
6686 return True;
6687
6688 when N_Parameter_Association =>
6689 return
6690 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
6691 and then FCE (Explicit_Actual_Parameter (E1),
6692 Explicit_Actual_Parameter (E2));
6693
6694 when N_Qualified_Expression =>
6695 return
6696 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6697 and then
6698 FCE (Expression (E1), Expression (E2));
6699
6700 when N_Quantified_Expression =>
6701 if not FCE (Condition (E1), Condition (E2)) then
6702 return False;
6703 end if;
6704
6705 if Present (Loop_Parameter_Specification (E1))
6706 and then Present (Loop_Parameter_Specification (E2))
6707 then
6708 declare
6709 L1 : constant Node_Id :=
6710 Loop_Parameter_Specification (E1);
6711 L2 : constant Node_Id :=
6712 Loop_Parameter_Specification (E2);
6713
6714 begin
6715 return
6716 Reverse_Present (L1) = Reverse_Present (L2)
6717 and then
6718 FCE (Defining_Identifier (L1),
6719 Defining_Identifier (L2))
6720 and then
6721 FCE (Discrete_Subtype_Definition (L1),
6722 Discrete_Subtype_Definition (L2));
6723 end;
6724
6725 else -- quantified expression with an iterator
6726 declare
6727 I1 : constant Node_Id := Iterator_Specification (E1);
6728 I2 : constant Node_Id := Iterator_Specification (E2);
6729
6730 begin
6731 return
6732 FCE (Defining_Identifier (I1),
6733 Defining_Identifier (I2))
6734 and then
6735 Of_Present (I1) = Of_Present (I2)
6736 and then
6737 Reverse_Present (I1) = Reverse_Present (I2)
6738 and then FCE (Name (I1), Name (I2))
6739 and then FCE (Subtype_Indication (I1),
6740 Subtype_Indication (I2));
6741 end;
6742 end if;
6743
6744 when N_Range =>
6745 return
6746 FCE (Low_Bound (E1), Low_Bound (E2))
6747 and then
6748 FCE (High_Bound (E1), High_Bound (E2));
6749
6750 when N_Real_Literal =>
6751 return (Realval (E1) = Realval (E2));
6752
6753 when N_Selected_Component =>
6754 return
6755 FCE (Prefix (E1), Prefix (E2))
6756 and then
6757 FCE (Selector_Name (E1), Selector_Name (E2));
6758
6759 when N_Slice =>
6760 return
6761 FCE (Prefix (E1), Prefix (E2))
6762 and then
6763 FCE (Discrete_Range (E1), Discrete_Range (E2));
6764
6765 when N_String_Literal =>
6766 declare
6767 S1 : constant String_Id := Strval (E1);
6768 S2 : constant String_Id := Strval (E2);
6769 L1 : constant Nat := String_Length (S1);
6770 L2 : constant Nat := String_Length (S2);
6771
6772 begin
6773 if L1 /= L2 then
6774 return False;
6775
6776 else
6777 for J in 1 .. L1 loop
6778 if Get_String_Char (S1, J) /=
6779 Get_String_Char (S2, J)
6780 then
6781 return False;
6782 end if;
6783 end loop;
6784
6785 return True;
6786 end if;
6787 end;
6788
6789 when N_Type_Conversion =>
6790 return
6791 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6792 and then
6793 FCE (Expression (E1), Expression (E2));
6794
6795 when N_Unary_Op =>
6796 return
6797 Entity (E1) = Entity (E2)
6798 and then
6799 FCE (Right_Opnd (E1), Right_Opnd (E2));
6800
6801 when N_Unchecked_Type_Conversion =>
6802 return
6803 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6804 and then
6805 FCE (Expression (E1), Expression (E2));
6806
6807 -- All other node types cannot appear in this context. Strictly
6808 -- we should raise a fatal internal error. Instead we just ignore
6809 -- the nodes. This means that if anyone makes a mistake in the
6810 -- expander and mucks an expression tree irretrievably, the
6811 -- result will be a failure to detect a (probably very obscure)
6812 -- case of non-conformance, which is better than bombing on some
6813 -- case where two expressions do in fact conform.
6814
6815 when others =>
6816 return True;
6817
6818 end case;
6819 end if;
6820 end Fully_Conformant_Expressions;
6821
6822 ----------------------------------------
6823 -- Fully_Conformant_Discrete_Subtypes --
6824 ----------------------------------------
6825
6826 function Fully_Conformant_Discrete_Subtypes
6827 (Given_S1 : Node_Id;
6828 Given_S2 : Node_Id) return Boolean
6829 is
6830 S1 : constant Node_Id := Original_Node (Given_S1);
6831 S2 : constant Node_Id := Original_Node (Given_S2);
6832
6833 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
6834 -- Special-case for a bound given by a discriminant, which in the body
6835 -- is replaced with the discriminal of the enclosing type.
6836
6837 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
6838 -- Check both bounds
6839
6840 -----------------------
6841 -- Conforming_Bounds --
6842 -----------------------
6843
6844 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
6845 begin
6846 if Is_Entity_Name (B1)
6847 and then Is_Entity_Name (B2)
6848 and then Ekind (Entity (B1)) = E_Discriminant
6849 then
6850 return Chars (B1) = Chars (B2);
6851
6852 else
6853 return Fully_Conformant_Expressions (B1, B2);
6854 end if;
6855 end Conforming_Bounds;
6856
6857 -----------------------
6858 -- Conforming_Ranges --
6859 -----------------------
6860
6861 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
6862 begin
6863 return
6864 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
6865 and then
6866 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
6867 end Conforming_Ranges;
6868
6869 -- Start of processing for Fully_Conformant_Discrete_Subtypes
6870
6871 begin
6872 if Nkind (S1) /= Nkind (S2) then
6873 return False;
6874
6875 elsif Is_Entity_Name (S1) then
6876 return Entity (S1) = Entity (S2);
6877
6878 elsif Nkind (S1) = N_Range then
6879 return Conforming_Ranges (S1, S2);
6880
6881 elsif Nkind (S1) = N_Subtype_Indication then
6882 return
6883 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
6884 and then
6885 Conforming_Ranges
6886 (Range_Expression (Constraint (S1)),
6887 Range_Expression (Constraint (S2)));
6888 else
6889 return True;
6890 end if;
6891 end Fully_Conformant_Discrete_Subtypes;
6892
6893 --------------------
6894 -- Install_Entity --
6895 --------------------
6896
6897 procedure Install_Entity (E : Entity_Id) is
6898 Prev : constant Entity_Id := Current_Entity (E);
6899 begin
6900 Set_Is_Immediately_Visible (E);
6901 Set_Current_Entity (E);
6902 Set_Homonym (E, Prev);
6903 end Install_Entity;
6904
6905 ---------------------
6906 -- Install_Formals --
6907 ---------------------
6908
6909 procedure Install_Formals (Id : Entity_Id) is
6910 F : Entity_Id;
6911 begin
6912 F := First_Formal (Id);
6913 while Present (F) loop
6914 Install_Entity (F);
6915 Next_Formal (F);
6916 end loop;
6917 end Install_Formals;
6918
6919 -----------------------------
6920 -- Is_Interface_Conformant --
6921 -----------------------------
6922
6923 function Is_Interface_Conformant
6924 (Tagged_Type : Entity_Id;
6925 Iface_Prim : Entity_Id;
6926 Prim : Entity_Id) return Boolean
6927 is
6928 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
6929 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
6930
6931 begin
6932 pragma Assert (Is_Subprogram (Iface_Prim)
6933 and then Is_Subprogram (Prim)
6934 and then Is_Dispatching_Operation (Iface_Prim)
6935 and then Is_Dispatching_Operation (Prim));
6936
6937 pragma Assert (Is_Interface (Iface)
6938 or else (Present (Alias (Iface_Prim))
6939 and then
6940 Is_Interface
6941 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
6942
6943 if Prim = Iface_Prim
6944 or else not Is_Subprogram (Prim)
6945 or else Ekind (Prim) /= Ekind (Iface_Prim)
6946 or else not Is_Dispatching_Operation (Prim)
6947 or else Scope (Prim) /= Scope (Tagged_Type)
6948 or else No (Typ)
6949 or else Base_Type (Typ) /= Tagged_Type
6950 or else not Primitive_Names_Match (Iface_Prim, Prim)
6951 then
6952 return False;
6953
6954 -- Case of a procedure, or a function that does not have a controlling
6955 -- result (I or access I).
6956
6957 elsif Ekind (Iface_Prim) = E_Procedure
6958 or else Etype (Prim) = Etype (Iface_Prim)
6959 or else not Has_Controlling_Result (Prim)
6960 then
6961 return Type_Conformant
6962 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
6963
6964 -- Case of a function returning an interface, or an access to one.
6965 -- Check that the return types correspond.
6966
6967 elsif Implements_Interface (Typ, Iface) then
6968 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
6969 /=
6970 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
6971 then
6972 return False;
6973 else
6974 return
6975 Type_Conformant (Prim, Iface_Prim,
6976 Skip_Controlling_Formals => True);
6977 end if;
6978
6979 else
6980 return False;
6981 end if;
6982 end Is_Interface_Conformant;
6983
6984 ---------------------------------
6985 -- Is_Non_Overriding_Operation --
6986 ---------------------------------
6987
6988 function Is_Non_Overriding_Operation
6989 (Prev_E : Entity_Id;
6990 New_E : Entity_Id) return Boolean
6991 is
6992 Formal : Entity_Id;
6993 F_Typ : Entity_Id;
6994 G_Typ : Entity_Id := Empty;
6995
6996 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
6997 -- If F_Type is a derived type associated with a generic actual subtype,
6998 -- then return its Generic_Parent_Type attribute, else return Empty.
6999
7000 function Types_Correspond
7001 (P_Type : Entity_Id;
7002 N_Type : Entity_Id) return Boolean;
7003 -- Returns true if and only if the types (or designated types in the
7004 -- case of anonymous access types) are the same or N_Type is derived
7005 -- directly or indirectly from P_Type.
7006
7007 -----------------------------
7008 -- Get_Generic_Parent_Type --
7009 -----------------------------
7010
7011 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
7012 G_Typ : Entity_Id;
7013 Indic : Node_Id;
7014
7015 begin
7016 if Is_Derived_Type (F_Typ)
7017 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
7018 then
7019 -- The tree must be traversed to determine the parent subtype in
7020 -- the generic unit, which unfortunately isn't always available
7021 -- via semantic attributes. ??? (Note: The use of Original_Node
7022 -- is needed for cases where a full derived type has been
7023 -- rewritten.)
7024
7025 Indic := Subtype_Indication
7026 (Type_Definition (Original_Node (Parent (F_Typ))));
7027
7028 if Nkind (Indic) = N_Subtype_Indication then
7029 G_Typ := Entity (Subtype_Mark (Indic));
7030 else
7031 G_Typ := Entity (Indic);
7032 end if;
7033
7034 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
7035 and then Present (Generic_Parent_Type (Parent (G_Typ)))
7036 then
7037 return Generic_Parent_Type (Parent (G_Typ));
7038 end if;
7039 end if;
7040
7041 return Empty;
7042 end Get_Generic_Parent_Type;
7043
7044 ----------------------
7045 -- Types_Correspond --
7046 ----------------------
7047
7048 function Types_Correspond
7049 (P_Type : Entity_Id;
7050 N_Type : Entity_Id) return Boolean
7051 is
7052 Prev_Type : Entity_Id := Base_Type (P_Type);
7053 New_Type : Entity_Id := Base_Type (N_Type);
7054
7055 begin
7056 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
7057 Prev_Type := Designated_Type (Prev_Type);
7058 end if;
7059
7060 if Ekind (New_Type) = E_Anonymous_Access_Type then
7061 New_Type := Designated_Type (New_Type);
7062 end if;
7063
7064 if Prev_Type = New_Type then
7065 return True;
7066
7067 elsif not Is_Class_Wide_Type (New_Type) then
7068 while Etype (New_Type) /= New_Type loop
7069 New_Type := Etype (New_Type);
7070 if New_Type = Prev_Type then
7071 return True;
7072 end if;
7073 end loop;
7074 end if;
7075 return False;
7076 end Types_Correspond;
7077
7078 -- Start of processing for Is_Non_Overriding_Operation
7079
7080 begin
7081 -- In the case where both operations are implicit derived subprograms
7082 -- then neither overrides the other. This can only occur in certain
7083 -- obscure cases (e.g., derivation from homographs created in a generic
7084 -- instantiation).
7085
7086 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
7087 return True;
7088
7089 elsif Ekind (Current_Scope) = E_Package
7090 and then Is_Generic_Instance (Current_Scope)
7091 and then In_Private_Part (Current_Scope)
7092 and then Comes_From_Source (New_E)
7093 then
7094 -- We examine the formals and result subtype of the inherited
7095 -- operation, to determine whether their type is derived from (the
7096 -- instance of) a generic type.
7097
7098 Formal := First_Formal (Prev_E);
7099 while Present (Formal) loop
7100 F_Typ := Base_Type (Etype (Formal));
7101
7102 if Ekind (F_Typ) = E_Anonymous_Access_Type then
7103 F_Typ := Designated_Type (F_Typ);
7104 end if;
7105
7106 G_Typ := Get_Generic_Parent_Type (F_Typ);
7107
7108 Next_Formal (Formal);
7109 end loop;
7110
7111 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
7112 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
7113 end if;
7114
7115 if No (G_Typ) then
7116 return False;
7117 end if;
7118
7119 -- If the generic type is a private type, then the original operation
7120 -- was not overriding in the generic, because there was no primitive
7121 -- operation to override.
7122
7123 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
7124 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
7125 N_Formal_Private_Type_Definition
7126 then
7127 return True;
7128
7129 -- The generic parent type is the ancestor of a formal derived
7130 -- type declaration. We need to check whether it has a primitive
7131 -- operation that should be overridden by New_E in the generic.
7132
7133 else
7134 declare
7135 P_Formal : Entity_Id;
7136 N_Formal : Entity_Id;
7137 P_Typ : Entity_Id;
7138 N_Typ : Entity_Id;
7139 P_Prim : Entity_Id;
7140 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
7141
7142 begin
7143 while Present (Prim_Elt) loop
7144 P_Prim := Node (Prim_Elt);
7145
7146 if Chars (P_Prim) = Chars (New_E)
7147 and then Ekind (P_Prim) = Ekind (New_E)
7148 then
7149 P_Formal := First_Formal (P_Prim);
7150 N_Formal := First_Formal (New_E);
7151 while Present (P_Formal) and then Present (N_Formal) loop
7152 P_Typ := Etype (P_Formal);
7153 N_Typ := Etype (N_Formal);
7154
7155 if not Types_Correspond (P_Typ, N_Typ) then
7156 exit;
7157 end if;
7158
7159 Next_Entity (P_Formal);
7160 Next_Entity (N_Formal);
7161 end loop;
7162
7163 -- Found a matching primitive operation belonging to the
7164 -- formal ancestor type, so the new subprogram is
7165 -- overriding.
7166
7167 if No (P_Formal)
7168 and then No (N_Formal)
7169 and then (Ekind (New_E) /= E_Function
7170 or else
7171 Types_Correspond
7172 (Etype (P_Prim), Etype (New_E)))
7173 then
7174 return False;
7175 end if;
7176 end if;
7177
7178 Next_Elmt (Prim_Elt);
7179 end loop;
7180
7181 -- If no match found, then the new subprogram does not
7182 -- override in the generic (nor in the instance).
7183
7184 return True;
7185 end;
7186 end if;
7187 else
7188 return False;
7189 end if;
7190 end Is_Non_Overriding_Operation;
7191
7192 -------------------------------------
7193 -- List_Inherited_Pre_Post_Aspects --
7194 -------------------------------------
7195
7196 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
7197 begin
7198 if Opt.List_Inherited_Aspects
7199 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
7200 then
7201 declare
7202 Inherited : constant Subprogram_List :=
7203 Inherited_Subprograms (E);
7204 P : Node_Id;
7205
7206 begin
7207 for J in Inherited'Range loop
7208 P := Spec_PPC_List (Inherited (J));
7209 while Present (P) loop
7210 Error_Msg_Sloc := Sloc (P);
7211
7212 if Class_Present (P) and then not Split_PPC (P) then
7213 if Pragma_Name (P) = Name_Precondition then
7214 Error_Msg_N
7215 ("?info: & inherits `Pre''Class` aspect from #", E);
7216 else
7217 Error_Msg_N
7218 ("?info: & inherits `Post''Class` aspect from #", E);
7219 end if;
7220 end if;
7221
7222 P := Next_Pragma (P);
7223 end loop;
7224 end loop;
7225 end;
7226 end if;
7227 end List_Inherited_Pre_Post_Aspects;
7228
7229 ------------------------------
7230 -- Make_Inequality_Operator --
7231 ------------------------------
7232
7233 -- S is the defining identifier of an equality operator. We build a
7234 -- subprogram declaration with the right signature. This operation is
7235 -- intrinsic, because it is always expanded as the negation of the
7236 -- call to the equality function.
7237
7238 procedure Make_Inequality_Operator (S : Entity_Id) is
7239 Loc : constant Source_Ptr := Sloc (S);
7240 Decl : Node_Id;
7241 Formals : List_Id;
7242 Op_Name : Entity_Id;
7243
7244 FF : constant Entity_Id := First_Formal (S);
7245 NF : constant Entity_Id := Next_Formal (FF);
7246
7247 begin
7248 -- Check that equality was properly defined, ignore call if not
7249
7250 if No (NF) then
7251 return;
7252 end if;
7253
7254 declare
7255 A : constant Entity_Id :=
7256 Make_Defining_Identifier (Sloc (FF),
7257 Chars => Chars (FF));
7258
7259 B : constant Entity_Id :=
7260 Make_Defining_Identifier (Sloc (NF),
7261 Chars => Chars (NF));
7262
7263 begin
7264 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
7265
7266 Formals := New_List (
7267 Make_Parameter_Specification (Loc,
7268 Defining_Identifier => A,
7269 Parameter_Type =>
7270 New_Reference_To (Etype (First_Formal (S)),
7271 Sloc (Etype (First_Formal (S))))),
7272
7273 Make_Parameter_Specification (Loc,
7274 Defining_Identifier => B,
7275 Parameter_Type =>
7276 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
7277 Sloc (Etype (Next_Formal (First_Formal (S)))))));
7278
7279 Decl :=
7280 Make_Subprogram_Declaration (Loc,
7281 Specification =>
7282 Make_Function_Specification (Loc,
7283 Defining_Unit_Name => Op_Name,
7284 Parameter_Specifications => Formals,
7285 Result_Definition =>
7286 New_Reference_To (Standard_Boolean, Loc)));
7287
7288 -- Insert inequality right after equality if it is explicit or after
7289 -- the derived type when implicit. These entities are created only
7290 -- for visibility purposes, and eventually replaced in the course of
7291 -- expansion, so they do not need to be attached to the tree and seen
7292 -- by the back-end. Keeping them internal also avoids spurious
7293 -- freezing problems. The declaration is inserted in the tree for
7294 -- analysis, and removed afterwards. If the equality operator comes
7295 -- from an explicit declaration, attach the inequality immediately
7296 -- after. Else the equality is inherited from a derived type
7297 -- declaration, so insert inequality after that declaration.
7298
7299 if No (Alias (S)) then
7300 Insert_After (Unit_Declaration_Node (S), Decl);
7301 elsif Is_List_Member (Parent (S)) then
7302 Insert_After (Parent (S), Decl);
7303 else
7304 Insert_After (Parent (Etype (First_Formal (S))), Decl);
7305 end if;
7306
7307 Mark_Rewrite_Insertion (Decl);
7308 Set_Is_Intrinsic_Subprogram (Op_Name);
7309 Analyze (Decl);
7310 Remove (Decl);
7311 Set_Has_Completion (Op_Name);
7312 Set_Corresponding_Equality (Op_Name, S);
7313 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
7314 end;
7315 end Make_Inequality_Operator;
7316
7317 ----------------------
7318 -- May_Need_Actuals --
7319 ----------------------
7320
7321 procedure May_Need_Actuals (Fun : Entity_Id) is
7322 F : Entity_Id;
7323 B : Boolean;
7324
7325 begin
7326 F := First_Formal (Fun);
7327 B := True;
7328 while Present (F) loop
7329 if No (Default_Value (F)) then
7330 B := False;
7331 exit;
7332 end if;
7333
7334 Next_Formal (F);
7335 end loop;
7336
7337 Set_Needs_No_Actuals (Fun, B);
7338 end May_Need_Actuals;
7339
7340 ---------------------
7341 -- Mode_Conformant --
7342 ---------------------
7343
7344 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7345 Result : Boolean;
7346 begin
7347 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
7348 return Result;
7349 end Mode_Conformant;
7350
7351 ---------------------------
7352 -- New_Overloaded_Entity --
7353 ---------------------------
7354
7355 procedure New_Overloaded_Entity
7356 (S : Entity_Id;
7357 Derived_Type : Entity_Id := Empty)
7358 is
7359 Overridden_Subp : Entity_Id := Empty;
7360 -- Set if the current scope has an operation that is type-conformant
7361 -- with S, and becomes hidden by S.
7362
7363 Is_Primitive_Subp : Boolean;
7364 -- Set to True if the new subprogram is primitive
7365
7366 E : Entity_Id;
7367 -- Entity that S overrides
7368
7369 Prev_Vis : Entity_Id := Empty;
7370 -- Predecessor of E in Homonym chain
7371
7372 procedure Check_For_Primitive_Subprogram
7373 (Is_Primitive : out Boolean;
7374 Is_Overriding : Boolean := False);
7375 -- If the subprogram being analyzed is a primitive operation of the type
7376 -- of a formal or result, set the Has_Primitive_Operations flag on the
7377 -- type, and set Is_Primitive to True (otherwise set to False). Set the
7378 -- corresponding flag on the entity itself for later use.
7379
7380 procedure Check_Synchronized_Overriding
7381 (Def_Id : Entity_Id;
7382 Overridden_Subp : out Entity_Id);
7383 -- First determine if Def_Id is an entry or a subprogram either defined
7384 -- in the scope of a task or protected type, or is a primitive of such
7385 -- a type. Check whether Def_Id overrides a subprogram of an interface
7386 -- implemented by the synchronized type, return the overridden entity
7387 -- or Empty.
7388
7389 function Is_Private_Declaration (E : Entity_Id) return Boolean;
7390 -- Check that E is declared in the private part of the current package,
7391 -- or in the package body, where it may hide a previous declaration.
7392 -- We can't use In_Private_Part by itself because this flag is also
7393 -- set when freezing entities, so we must examine the place of the
7394 -- declaration in the tree, and recognize wrapper packages as well.
7395
7396 function Is_Overriding_Alias
7397 (Old_E : Entity_Id;
7398 New_E : Entity_Id) return Boolean;
7399 -- Check whether new subprogram and old subprogram are both inherited
7400 -- from subprograms that have distinct dispatch table entries. This can
7401 -- occur with derivations from instances with accidental homonyms.
7402 -- The function is conservative given that the converse is only true
7403 -- within instances that contain accidental overloadings.
7404
7405 ------------------------------------
7406 -- Check_For_Primitive_Subprogram --
7407 ------------------------------------
7408
7409 procedure Check_For_Primitive_Subprogram
7410 (Is_Primitive : out Boolean;
7411 Is_Overriding : Boolean := False)
7412 is
7413 Formal : Entity_Id;
7414 F_Typ : Entity_Id;
7415 B_Typ : Entity_Id;
7416
7417 function Visible_Part_Type (T : Entity_Id) return Boolean;
7418 -- Returns true if T is declared in the visible part of the current
7419 -- package scope; otherwise returns false. Assumes that T is declared
7420 -- in a package.
7421
7422 procedure Check_Private_Overriding (T : Entity_Id);
7423 -- Checks that if a primitive abstract subprogram of a visible
7424 -- abstract type is declared in a private part, then it must override
7425 -- an abstract subprogram declared in the visible part. Also checks
7426 -- that if a primitive function with a controlling result is declared
7427 -- in a private part, then it must override a function declared in
7428 -- the visible part.
7429
7430 ------------------------------
7431 -- Check_Private_Overriding --
7432 ------------------------------
7433
7434 procedure Check_Private_Overriding (T : Entity_Id) is
7435 begin
7436 if Is_Package_Or_Generic_Package (Current_Scope)
7437 and then In_Private_Part (Current_Scope)
7438 and then Visible_Part_Type (T)
7439 and then not In_Instance
7440 then
7441 if Is_Abstract_Type (T)
7442 and then Is_Abstract_Subprogram (S)
7443 and then (not Is_Overriding
7444 or else not Is_Abstract_Subprogram (E))
7445 then
7446 Error_Msg_N
7447 ("abstract subprograms must be visible "
7448 & "(RM 3.9.3(10))!", S);
7449
7450 elsif Ekind (S) = E_Function
7451 and then not Is_Overriding
7452 then
7453 if Is_Tagged_Type (T)
7454 and then T = Base_Type (Etype (S))
7455 then
7456 Error_Msg_N
7457 ("private function with tagged result must"
7458 & " override visible-part function", S);
7459 Error_Msg_N
7460 ("\move subprogram to the visible part"
7461 & " (RM 3.9.3(10))", S);
7462
7463 -- AI05-0073: extend this test to the case of a function
7464 -- with a controlling access result.
7465
7466 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
7467 and then Is_Tagged_Type (Designated_Type (Etype (S)))
7468 and then
7469 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
7470 and then Ada_Version >= Ada_2012
7471 then
7472 Error_Msg_N
7473 ("private function with controlling access result "
7474 & "must override visible-part function", S);
7475 Error_Msg_N
7476 ("\move subprogram to the visible part"
7477 & " (RM 3.9.3(10))", S);
7478 end if;
7479 end if;
7480 end if;
7481 end Check_Private_Overriding;
7482
7483 -----------------------
7484 -- Visible_Part_Type --
7485 -----------------------
7486
7487 function Visible_Part_Type (T : Entity_Id) return Boolean is
7488 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
7489 N : Node_Id;
7490
7491 begin
7492 -- If the entity is a private type, then it must be declared in a
7493 -- visible part.
7494
7495 if Ekind (T) in Private_Kind then
7496 return True;
7497 end if;
7498
7499 -- Otherwise, we traverse the visible part looking for its
7500 -- corresponding declaration. We cannot use the declaration
7501 -- node directly because in the private part the entity of a
7502 -- private type is the one in the full view, which does not
7503 -- indicate that it is the completion of something visible.
7504
7505 N := First (Visible_Declarations (Specification (P)));
7506 while Present (N) loop
7507 if Nkind (N) = N_Full_Type_Declaration
7508 and then Present (Defining_Identifier (N))
7509 and then T = Defining_Identifier (N)
7510 then
7511 return True;
7512
7513 elsif Nkind_In (N, N_Private_Type_Declaration,
7514 N_Private_Extension_Declaration)
7515 and then Present (Defining_Identifier (N))
7516 and then T = Full_View (Defining_Identifier (N))
7517 then
7518 return True;
7519 end if;
7520
7521 Next (N);
7522 end loop;
7523
7524 return False;
7525 end Visible_Part_Type;
7526
7527 -- Start of processing for Check_For_Primitive_Subprogram
7528
7529 begin
7530 Is_Primitive := False;
7531
7532 if not Comes_From_Source (S) then
7533 null;
7534
7535 -- If subprogram is at library level, it is not primitive operation
7536
7537 elsif Current_Scope = Standard_Standard then
7538 null;
7539
7540 elsif (Is_Package_Or_Generic_Package (Current_Scope)
7541 and then not In_Package_Body (Current_Scope))
7542 or else Is_Overriding
7543 then
7544 -- For function, check return type
7545
7546 if Ekind (S) = E_Function then
7547 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
7548 F_Typ := Designated_Type (Etype (S));
7549 else
7550 F_Typ := Etype (S);
7551 end if;
7552
7553 B_Typ := Base_Type (F_Typ);
7554
7555 if Scope (B_Typ) = Current_Scope
7556 and then not Is_Class_Wide_Type (B_Typ)
7557 and then not Is_Generic_Type (B_Typ)
7558 then
7559 Is_Primitive := True;
7560 Set_Has_Primitive_Operations (B_Typ);
7561 Set_Is_Primitive (S);
7562 Check_Private_Overriding (B_Typ);
7563 end if;
7564 end if;
7565
7566 -- For all subprograms, check formals
7567
7568 Formal := First_Formal (S);
7569 while Present (Formal) loop
7570 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
7571 F_Typ := Designated_Type (Etype (Formal));
7572 else
7573 F_Typ := Etype (Formal);
7574 end if;
7575
7576 B_Typ := Base_Type (F_Typ);
7577
7578 if Ekind (B_Typ) = E_Access_Subtype then
7579 B_Typ := Base_Type (B_Typ);
7580 end if;
7581
7582 if Scope (B_Typ) = Current_Scope
7583 and then not Is_Class_Wide_Type (B_Typ)
7584 and then not Is_Generic_Type (B_Typ)
7585 then
7586 Is_Primitive := True;
7587 Set_Is_Primitive (S);
7588 Set_Has_Primitive_Operations (B_Typ);
7589 Check_Private_Overriding (B_Typ);
7590 end if;
7591
7592 Next_Formal (Formal);
7593 end loop;
7594 end if;
7595 end Check_For_Primitive_Subprogram;
7596
7597 -----------------------------------
7598 -- Check_Synchronized_Overriding --
7599 -----------------------------------
7600
7601 procedure Check_Synchronized_Overriding
7602 (Def_Id : Entity_Id;
7603 Overridden_Subp : out Entity_Id)
7604 is
7605 Ifaces_List : Elist_Id;
7606 In_Scope : Boolean;
7607 Typ : Entity_Id;
7608
7609 function Matches_Prefixed_View_Profile
7610 (Prim_Params : List_Id;
7611 Iface_Params : List_Id) return Boolean;
7612 -- Determine whether a subprogram's parameter profile Prim_Params
7613 -- matches that of a potentially overridden interface subprogram
7614 -- Iface_Params. Also determine if the type of first parameter of
7615 -- Iface_Params is an implemented interface.
7616
7617 -----------------------------------
7618 -- Matches_Prefixed_View_Profile --
7619 -----------------------------------
7620
7621 function Matches_Prefixed_View_Profile
7622 (Prim_Params : List_Id;
7623 Iface_Params : List_Id) return Boolean
7624 is
7625 Iface_Id : Entity_Id;
7626 Iface_Param : Node_Id;
7627 Iface_Typ : Entity_Id;
7628 Prim_Id : Entity_Id;
7629 Prim_Param : Node_Id;
7630 Prim_Typ : Entity_Id;
7631
7632 function Is_Implemented
7633 (Ifaces_List : Elist_Id;
7634 Iface : Entity_Id) return Boolean;
7635 -- Determine if Iface is implemented by the current task or
7636 -- protected type.
7637
7638 --------------------
7639 -- Is_Implemented --
7640 --------------------
7641
7642 function Is_Implemented
7643 (Ifaces_List : Elist_Id;
7644 Iface : Entity_Id) return Boolean
7645 is
7646 Iface_Elmt : Elmt_Id;
7647
7648 begin
7649 Iface_Elmt := First_Elmt (Ifaces_List);
7650 while Present (Iface_Elmt) loop
7651 if Node (Iface_Elmt) = Iface then
7652 return True;
7653 end if;
7654
7655 Next_Elmt (Iface_Elmt);
7656 end loop;
7657
7658 return False;
7659 end Is_Implemented;
7660
7661 -- Start of processing for Matches_Prefixed_View_Profile
7662
7663 begin
7664 Iface_Param := First (Iface_Params);
7665 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7666
7667 if Is_Access_Type (Iface_Typ) then
7668 Iface_Typ := Designated_Type (Iface_Typ);
7669 end if;
7670
7671 Prim_Param := First (Prim_Params);
7672
7673 -- The first parameter of the potentially overridden subprogram
7674 -- must be an interface implemented by Prim.
7675
7676 if not Is_Interface (Iface_Typ)
7677 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7678 then
7679 return False;
7680 end if;
7681
7682 -- The checks on the object parameters are done, move onto the
7683 -- rest of the parameters.
7684
7685 if not In_Scope then
7686 Prim_Param := Next (Prim_Param);
7687 end if;
7688
7689 Iface_Param := Next (Iface_Param);
7690 while Present (Iface_Param) and then Present (Prim_Param) loop
7691 Iface_Id := Defining_Identifier (Iface_Param);
7692 Iface_Typ := Find_Parameter_Type (Iface_Param);
7693
7694 Prim_Id := Defining_Identifier (Prim_Param);
7695 Prim_Typ := Find_Parameter_Type (Prim_Param);
7696
7697 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7698 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7699 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7700 then
7701 Iface_Typ := Designated_Type (Iface_Typ);
7702 Prim_Typ := Designated_Type (Prim_Typ);
7703 end if;
7704
7705 -- Case of multiple interface types inside a parameter profile
7706
7707 -- (Obj_Param : in out Iface; ...; Param : Iface)
7708
7709 -- If the interface type is implemented, then the matching type
7710 -- in the primitive should be the implementing record type.
7711
7712 if Ekind (Iface_Typ) = E_Record_Type
7713 and then Is_Interface (Iface_Typ)
7714 and then Is_Implemented (Ifaces_List, Iface_Typ)
7715 then
7716 if Prim_Typ /= Typ then
7717 return False;
7718 end if;
7719
7720 -- The two parameters must be both mode and subtype conformant
7721
7722 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7723 or else not
7724 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7725 then
7726 return False;
7727 end if;
7728
7729 Next (Iface_Param);
7730 Next (Prim_Param);
7731 end loop;
7732
7733 -- One of the two lists contains more parameters than the other
7734
7735 if Present (Iface_Param) or else Present (Prim_Param) then
7736 return False;
7737 end if;
7738
7739 return True;
7740 end Matches_Prefixed_View_Profile;
7741
7742 -- Start of processing for Check_Synchronized_Overriding
7743
7744 begin
7745 Overridden_Subp := Empty;
7746
7747 -- Def_Id must be an entry or a subprogram. We should skip predefined
7748 -- primitives internally generated by the frontend; however at this
7749 -- stage predefined primitives are still not fully decorated. As a
7750 -- minor optimization we skip here internally generated subprograms.
7751
7752 if (Ekind (Def_Id) /= E_Entry
7753 and then Ekind (Def_Id) /= E_Function
7754 and then Ekind (Def_Id) /= E_Procedure)
7755 or else not Comes_From_Source (Def_Id)
7756 then
7757 return;
7758 end if;
7759
7760 -- Search for the concurrent declaration since it contains the list
7761 -- of all implemented interfaces. In this case, the subprogram is
7762 -- declared within the scope of a protected or a task type.
7763
7764 if Present (Scope (Def_Id))
7765 and then Is_Concurrent_Type (Scope (Def_Id))
7766 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7767 then
7768 Typ := Scope (Def_Id);
7769 In_Scope := True;
7770
7771 -- The enclosing scope is not a synchronized type and the subprogram
7772 -- has no formals.
7773
7774 elsif No (First_Formal (Def_Id)) then
7775 return;
7776
7777 -- The subprogram has formals and hence it may be a primitive of a
7778 -- concurrent type.
7779
7780 else
7781 Typ := Etype (First_Formal (Def_Id));
7782
7783 if Is_Access_Type (Typ) then
7784 Typ := Directly_Designated_Type (Typ);
7785 end if;
7786
7787 if Is_Concurrent_Type (Typ)
7788 and then not Is_Generic_Actual_Type (Typ)
7789 then
7790 In_Scope := False;
7791
7792 -- This case occurs when the concurrent type is declared within
7793 -- a generic unit. As a result the corresponding record has been
7794 -- built and used as the type of the first formal, we just have
7795 -- to retrieve the corresponding concurrent type.
7796
7797 elsif Is_Concurrent_Record_Type (Typ)
7798 and then Present (Corresponding_Concurrent_Type (Typ))
7799 then
7800 Typ := Corresponding_Concurrent_Type (Typ);
7801 In_Scope := False;
7802
7803 else
7804 return;
7805 end if;
7806 end if;
7807
7808 -- There is no overriding to check if is an inherited operation in a
7809 -- type derivation on for a generic actual.
7810
7811 Collect_Interfaces (Typ, Ifaces_List);
7812
7813 if Is_Empty_Elmt_List (Ifaces_List) then
7814 return;
7815 end if;
7816
7817 -- Determine whether entry or subprogram Def_Id overrides a primitive
7818 -- operation that belongs to one of the interfaces in Ifaces_List.
7819
7820 declare
7821 Candidate : Entity_Id := Empty;
7822 Hom : Entity_Id := Empty;
7823 Iface_Typ : Entity_Id;
7824 Subp : Entity_Id := Empty;
7825
7826 begin
7827 -- Traverse the homonym chain, looking for a potentially
7828 -- overridden subprogram that belongs to an implemented
7829 -- interface.
7830
7831 Hom := Current_Entity_In_Scope (Def_Id);
7832 while Present (Hom) loop
7833 Subp := Hom;
7834
7835 if Subp = Def_Id
7836 or else not Is_Overloadable (Subp)
7837 or else not Is_Primitive (Subp)
7838 or else not Is_Dispatching_Operation (Subp)
7839 or else not Present (Find_Dispatching_Type (Subp))
7840 or else not Is_Interface (Find_Dispatching_Type (Subp))
7841 then
7842 null;
7843
7844 -- Entries and procedures can override abstract or null
7845 -- interface procedures.
7846
7847 elsif (Ekind (Def_Id) = E_Procedure
7848 or else Ekind (Def_Id) = E_Entry)
7849 and then Ekind (Subp) = E_Procedure
7850 and then Matches_Prefixed_View_Profile
7851 (Parameter_Specifications (Parent (Def_Id)),
7852 Parameter_Specifications (Parent (Subp)))
7853 then
7854 Candidate := Subp;
7855
7856 -- For an overridden subprogram Subp, check whether the mode
7857 -- of its first parameter is correct depending on the kind
7858 -- of synchronized type.
7859
7860 declare
7861 Formal : constant Node_Id := First_Formal (Candidate);
7862
7863 begin
7864 -- In order for an entry or a protected procedure to
7865 -- override, the first parameter of the overridden
7866 -- routine must be of mode "out", "in out" or
7867 -- access-to-variable.
7868
7869 if (Ekind (Candidate) = E_Entry
7870 or else Ekind (Candidate) = E_Procedure)
7871 and then Is_Protected_Type (Typ)
7872 and then Ekind (Formal) /= E_In_Out_Parameter
7873 and then Ekind (Formal) /= E_Out_Parameter
7874 and then Nkind (Parameter_Type (Parent (Formal)))
7875 /= N_Access_Definition
7876 then
7877 null;
7878
7879 -- All other cases are OK since a task entry or routine
7880 -- does not have a restriction on the mode of the first
7881 -- parameter of the overridden interface routine.
7882
7883 else
7884 Overridden_Subp := Candidate;
7885 return;
7886 end if;
7887 end;
7888
7889 -- Functions can override abstract interface functions
7890
7891 elsif Ekind (Def_Id) = E_Function
7892 and then Ekind (Subp) = E_Function
7893 and then Matches_Prefixed_View_Profile
7894 (Parameter_Specifications (Parent (Def_Id)),
7895 Parameter_Specifications (Parent (Subp)))
7896 and then Etype (Result_Definition (Parent (Def_Id))) =
7897 Etype (Result_Definition (Parent (Subp)))
7898 then
7899 Overridden_Subp := Subp;
7900 return;
7901 end if;
7902
7903 Hom := Homonym (Hom);
7904 end loop;
7905
7906 -- After examining all candidates for overriding, we are left with
7907 -- the best match which is a mode incompatible interface routine.
7908 -- Do not emit an error if the Expander is active since this error
7909 -- will be detected later on after all concurrent types are
7910 -- expanded and all wrappers are built. This check is meant for
7911 -- spec-only compilations.
7912
7913 if Present (Candidate) and then not Expander_Active then
7914 Iface_Typ :=
7915 Find_Parameter_Type (Parent (First_Formal (Candidate)));
7916
7917 -- Def_Id is primitive of a protected type, declared inside the
7918 -- type, and the candidate is primitive of a limited or
7919 -- synchronized interface.
7920
7921 if In_Scope
7922 and then Is_Protected_Type (Typ)
7923 and then
7924 (Is_Limited_Interface (Iface_Typ)
7925 or else Is_Protected_Interface (Iface_Typ)
7926 or else Is_Synchronized_Interface (Iface_Typ)
7927 or else Is_Task_Interface (Iface_Typ))
7928 then
7929 Error_Msg_NE
7930 ("first formal of & must be of mode `OUT`, `IN OUT`"
7931 & " or access-to-variable", Typ, Candidate);
7932 Error_Msg_N
7933 ("\in order to be overridden by protected procedure or "
7934 & "entry (RM 9.4(11.9/2))", Typ);
7935 end if;
7936 end if;
7937
7938 Overridden_Subp := Candidate;
7939 return;
7940 end;
7941 end Check_Synchronized_Overriding;
7942
7943 ----------------------------
7944 -- Is_Private_Declaration --
7945 ----------------------------
7946
7947 function Is_Private_Declaration (E : Entity_Id) return Boolean is
7948 Priv_Decls : List_Id;
7949 Decl : constant Node_Id := Unit_Declaration_Node (E);
7950
7951 begin
7952 if Is_Package_Or_Generic_Package (Current_Scope)
7953 and then In_Private_Part (Current_Scope)
7954 then
7955 Priv_Decls :=
7956 Private_Declarations (
7957 Specification (Unit_Declaration_Node (Current_Scope)));
7958
7959 return In_Package_Body (Current_Scope)
7960 or else
7961 (Is_List_Member (Decl)
7962 and then List_Containing (Decl) = Priv_Decls)
7963 or else (Nkind (Parent (Decl)) = N_Package_Specification
7964 and then not
7965 Is_Compilation_Unit
7966 (Defining_Entity (Parent (Decl)))
7967 and then List_Containing (Parent (Parent (Decl)))
7968 = Priv_Decls);
7969 else
7970 return False;
7971 end if;
7972 end Is_Private_Declaration;
7973
7974 --------------------------
7975 -- Is_Overriding_Alias --
7976 --------------------------
7977
7978 function Is_Overriding_Alias
7979 (Old_E : Entity_Id;
7980 New_E : Entity_Id) return Boolean
7981 is
7982 AO : constant Entity_Id := Alias (Old_E);
7983 AN : constant Entity_Id := Alias (New_E);
7984
7985 begin
7986 return Scope (AO) /= Scope (AN)
7987 or else No (DTC_Entity (AO))
7988 or else No (DTC_Entity (AN))
7989 or else DT_Position (AO) = DT_Position (AN);
7990 end Is_Overriding_Alias;
7991
7992 -- Start of processing for New_Overloaded_Entity
7993
7994 begin
7995 -- We need to look for an entity that S may override. This must be a
7996 -- homonym in the current scope, so we look for the first homonym of
7997 -- S in the current scope as the starting point for the search.
7998
7999 E := Current_Entity_In_Scope (S);
8000
8001 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
8002 -- They are directly added to the list of primitive operations of
8003 -- Derived_Type, unless this is a rederivation in the private part
8004 -- of an operation that was already derived in the visible part of
8005 -- the current package.
8006
8007 if Ada_Version >= Ada_2005
8008 and then Present (Derived_Type)
8009 and then Present (Alias (S))
8010 and then Is_Dispatching_Operation (Alias (S))
8011 and then Present (Find_Dispatching_Type (Alias (S)))
8012 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
8013 then
8014 -- For private types, when the full-view is processed we propagate to
8015 -- the full view the non-overridden entities whose attribute "alias"
8016 -- references an interface primitive. These entities were added by
8017 -- Derive_Subprograms to ensure that interface primitives are
8018 -- covered.
8019
8020 -- Inside_Freeze_Actions is non zero when S corresponds with an
8021 -- internal entity that links an interface primitive with its
8022 -- covering primitive through attribute Interface_Alias (see
8023 -- Add_Internal_Interface_Entities).
8024
8025 if Inside_Freezing_Actions = 0
8026 and then Is_Package_Or_Generic_Package (Current_Scope)
8027 and then In_Private_Part (Current_Scope)
8028 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
8029 and then Nkind (Parent (S)) = N_Full_Type_Declaration
8030 and then Full_View (Defining_Identifier (Parent (E)))
8031 = Defining_Identifier (Parent (S))
8032 and then Alias (E) = Alias (S)
8033 then
8034 Check_Operation_From_Private_View (S, E);
8035 Set_Is_Dispatching_Operation (S);
8036
8037 -- Common case
8038
8039 else
8040 Enter_Overloaded_Entity (S);
8041 Check_Dispatching_Operation (S, Empty);
8042 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8043 end if;
8044
8045 return;
8046 end if;
8047
8048 -- If there is no homonym then this is definitely not overriding
8049
8050 if No (E) then
8051 Enter_Overloaded_Entity (S);
8052 Check_Dispatching_Operation (S, Empty);
8053 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8054
8055 -- If subprogram has an explicit declaration, check whether it
8056 -- has an overriding indicator.
8057
8058 if Comes_From_Source (S) then
8059 Check_Synchronized_Overriding (S, Overridden_Subp);
8060
8061 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
8062 -- it may have overridden some hidden inherited primitive. Update
8063 -- Overridden_Subp to avoid spurious errors when checking the
8064 -- overriding indicator.
8065
8066 if Ada_Version >= Ada_2012
8067 and then No (Overridden_Subp)
8068 and then Is_Dispatching_Operation (S)
8069 and then Present (Overridden_Operation (S))
8070 then
8071 Overridden_Subp := Overridden_Operation (S);
8072 end if;
8073
8074 Check_Overriding_Indicator
8075 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
8076 end if;
8077
8078 -- If there is a homonym that is not overloadable, then we have an
8079 -- error, except for the special cases checked explicitly below.
8080
8081 elsif not Is_Overloadable (E) then
8082
8083 -- Check for spurious conflict produced by a subprogram that has the
8084 -- same name as that of the enclosing generic package. The conflict
8085 -- occurs within an instance, between the subprogram and the renaming
8086 -- declaration for the package. After the subprogram, the package
8087 -- renaming declaration becomes hidden.
8088
8089 if Ekind (E) = E_Package
8090 and then Present (Renamed_Object (E))
8091 and then Renamed_Object (E) = Current_Scope
8092 and then Nkind (Parent (Renamed_Object (E))) =
8093 N_Package_Specification
8094 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
8095 then
8096 Set_Is_Hidden (E);
8097 Set_Is_Immediately_Visible (E, False);
8098 Enter_Overloaded_Entity (S);
8099 Set_Homonym (S, Homonym (E));
8100 Check_Dispatching_Operation (S, Empty);
8101 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
8102
8103 -- If the subprogram is implicit it is hidden by the previous
8104 -- declaration. However if it is dispatching, it must appear in the
8105 -- dispatch table anyway, because it can be dispatched to even if it
8106 -- cannot be called directly.
8107
8108 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
8109 Set_Scope (S, Current_Scope);
8110
8111 if Is_Dispatching_Operation (Alias (S)) then
8112 Check_Dispatching_Operation (S, Empty);
8113 end if;
8114
8115 return;
8116
8117 else
8118 Error_Msg_Sloc := Sloc (E);
8119
8120 -- Generate message, with useful additional warning if in generic
8121
8122 if Is_Generic_Unit (E) then
8123 Error_Msg_N ("previous generic unit cannot be overloaded", S);
8124 Error_Msg_N ("\& conflicts with declaration#", S);
8125 else
8126 Error_Msg_N ("& conflicts with declaration#", S);
8127 end if;
8128
8129 return;
8130 end if;
8131
8132 -- E exists and is overloadable
8133
8134 else
8135 Check_Synchronized_Overriding (S, Overridden_Subp);
8136
8137 -- Loop through E and its homonyms to determine if any of them is
8138 -- the candidate for overriding by S.
8139
8140 while Present (E) loop
8141
8142 -- Definitely not interesting if not in the current scope
8143
8144 if Scope (E) /= Current_Scope then
8145 null;
8146
8147 -- Check if we have type conformance
8148
8149 elsif Type_Conformant (E, S) then
8150
8151 -- If the old and new entities have the same profile and one
8152 -- is not the body of the other, then this is an error, unless
8153 -- one of them is implicitly declared.
8154
8155 -- There are some cases when both can be implicit, for example
8156 -- when both a literal and a function that overrides it are
8157 -- inherited in a derivation, or when an inherited operation
8158 -- of a tagged full type overrides the inherited operation of
8159 -- a private extension. Ada 83 had a special rule for the
8160 -- literal case. In Ada95, the later implicit operation hides
8161 -- the former, and the literal is always the former. In the
8162 -- odd case where both are derived operations declared at the
8163 -- same point, both operations should be declared, and in that
8164 -- case we bypass the following test and proceed to the next
8165 -- part. This can only occur for certain obscure cases in
8166 -- instances, when an operation on a type derived from a formal
8167 -- private type does not override a homograph inherited from
8168 -- the actual. In subsequent derivations of such a type, the
8169 -- DT positions of these operations remain distinct, if they
8170 -- have been set.
8171
8172 if Present (Alias (S))
8173 and then (No (Alias (E))
8174 or else Comes_From_Source (E)
8175 or else Is_Abstract_Subprogram (S)
8176 or else
8177 (Is_Dispatching_Operation (E)
8178 and then Is_Overriding_Alias (E, S)))
8179 and then Ekind (E) /= E_Enumeration_Literal
8180 then
8181 -- When an derived operation is overloaded it may be due to
8182 -- the fact that the full view of a private extension
8183 -- re-inherits. It has to be dealt with.
8184
8185 if Is_Package_Or_Generic_Package (Current_Scope)
8186 and then In_Private_Part (Current_Scope)
8187 then
8188 Check_Operation_From_Private_View (S, E);
8189 end if;
8190
8191 -- In any case the implicit operation remains hidden by the
8192 -- existing declaration, which is overriding. Indicate that
8193 -- E overrides the operation from which S is inherited.
8194
8195 if Present (Alias (S)) then
8196 Set_Overridden_Operation (E, Alias (S));
8197 else
8198 Set_Overridden_Operation (E, S);
8199 end if;
8200
8201 if Comes_From_Source (E) then
8202 Check_Overriding_Indicator (E, S, Is_Primitive => False);
8203 end if;
8204
8205 return;
8206
8207 -- Within an instance, the renaming declarations for actual
8208 -- subprograms may become ambiguous, but they do not hide each
8209 -- other.
8210
8211 elsif Ekind (E) /= E_Entry
8212 and then not Comes_From_Source (E)
8213 and then not Is_Generic_Instance (E)
8214 and then (Present (Alias (E))
8215 or else Is_Intrinsic_Subprogram (E))
8216 and then (not In_Instance
8217 or else No (Parent (E))
8218 or else Nkind (Unit_Declaration_Node (E)) /=
8219 N_Subprogram_Renaming_Declaration)
8220 then
8221 -- A subprogram child unit is not allowed to override an
8222 -- inherited subprogram (10.1.1(20)).
8223
8224 if Is_Child_Unit (S) then
8225 Error_Msg_N
8226 ("child unit overrides inherited subprogram in parent",
8227 S);
8228 return;
8229 end if;
8230
8231 if Is_Non_Overriding_Operation (E, S) then
8232 Enter_Overloaded_Entity (S);
8233
8234 if No (Derived_Type)
8235 or else Is_Tagged_Type (Derived_Type)
8236 then
8237 Check_Dispatching_Operation (S, Empty);
8238 end if;
8239
8240 return;
8241 end if;
8242
8243 -- E is a derived operation or an internal operator which
8244 -- is being overridden. Remove E from further visibility.
8245 -- Furthermore, if E is a dispatching operation, it must be
8246 -- replaced in the list of primitive operations of its type
8247 -- (see Override_Dispatching_Operation).
8248
8249 Overridden_Subp := E;
8250
8251 declare
8252 Prev : Entity_Id;
8253
8254 begin
8255 Prev := First_Entity (Current_Scope);
8256 while Present (Prev)
8257 and then Next_Entity (Prev) /= E
8258 loop
8259 Next_Entity (Prev);
8260 end loop;
8261
8262 -- It is possible for E to be in the current scope and
8263 -- yet not in the entity chain. This can only occur in a
8264 -- generic context where E is an implicit concatenation
8265 -- in the formal part, because in a generic body the
8266 -- entity chain starts with the formals.
8267
8268 pragma Assert
8269 (Present (Prev) or else Chars (E) = Name_Op_Concat);
8270
8271 -- E must be removed both from the entity_list of the
8272 -- current scope, and from the visibility chain
8273
8274 if Debug_Flag_E then
8275 Write_Str ("Override implicit operation ");
8276 Write_Int (Int (E));
8277 Write_Eol;
8278 end if;
8279
8280 -- If E is a predefined concatenation, it stands for four
8281 -- different operations. As a result, a single explicit
8282 -- declaration does not hide it. In a possible ambiguous
8283 -- situation, Disambiguate chooses the user-defined op,
8284 -- so it is correct to retain the previous internal one.
8285
8286 if Chars (E) /= Name_Op_Concat
8287 or else Ekind (E) /= E_Operator
8288 then
8289 -- For nondispatching derived operations that are
8290 -- overridden by a subprogram declared in the private
8291 -- part of a package, we retain the derived subprogram
8292 -- but mark it as not immediately visible. If the
8293 -- derived operation was declared in the visible part
8294 -- then this ensures that it will still be visible
8295 -- outside the package with the proper signature
8296 -- (calls from outside must also be directed to this
8297 -- version rather than the overriding one, unlike the
8298 -- dispatching case). Calls from inside the package
8299 -- will still resolve to the overriding subprogram
8300 -- since the derived one is marked as not visible
8301 -- within the package.
8302
8303 -- If the private operation is dispatching, we achieve
8304 -- the overriding by keeping the implicit operation
8305 -- but setting its alias to be the overriding one. In
8306 -- this fashion the proper body is executed in all
8307 -- cases, but the original signature is used outside
8308 -- of the package.
8309
8310 -- If the overriding is not in the private part, we
8311 -- remove the implicit operation altogether.
8312
8313 if Is_Private_Declaration (S) then
8314 if not Is_Dispatching_Operation (E) then
8315 Set_Is_Immediately_Visible (E, False);
8316 else
8317 -- Work done in Override_Dispatching_Operation,
8318 -- so nothing else need to be done here.
8319
8320 null;
8321 end if;
8322
8323 else
8324 -- Find predecessor of E in Homonym chain
8325
8326 if E = Current_Entity (E) then
8327 Prev_Vis := Empty;
8328 else
8329 Prev_Vis := Current_Entity (E);
8330 while Homonym (Prev_Vis) /= E loop
8331 Prev_Vis := Homonym (Prev_Vis);
8332 end loop;
8333 end if;
8334
8335 if Prev_Vis /= Empty then
8336
8337 -- Skip E in the visibility chain
8338
8339 Set_Homonym (Prev_Vis, Homonym (E));
8340
8341 else
8342 Set_Name_Entity_Id (Chars (E), Homonym (E));
8343 end if;
8344
8345 Set_Next_Entity (Prev, Next_Entity (E));
8346
8347 if No (Next_Entity (Prev)) then
8348 Set_Last_Entity (Current_Scope, Prev);
8349 end if;
8350 end if;
8351 end if;
8352
8353 Enter_Overloaded_Entity (S);
8354
8355 -- For entities generated by Derive_Subprograms the
8356 -- overridden operation is the inherited primitive
8357 -- (which is available through the attribute alias).
8358
8359 if not (Comes_From_Source (E))
8360 and then Is_Dispatching_Operation (E)
8361 and then Find_Dispatching_Type (E) =
8362 Find_Dispatching_Type (S)
8363 and then Present (Alias (E))
8364 and then Comes_From_Source (Alias (E))
8365 then
8366 Set_Overridden_Operation (S, Alias (E));
8367 else
8368 Set_Overridden_Operation (S, E);
8369 end if;
8370
8371 Check_Overriding_Indicator (S, E, Is_Primitive => True);
8372
8373 -- If S is a user-defined subprogram or a null procedure
8374 -- expanded to override an inherited null procedure, or a
8375 -- predefined dispatching primitive then indicate that E
8376 -- overrides the operation from which S is inherited.
8377
8378 if Comes_From_Source (S)
8379 or else
8380 (Present (Parent (S))
8381 and then
8382 Nkind (Parent (S)) = N_Procedure_Specification
8383 and then
8384 Null_Present (Parent (S)))
8385 or else
8386 (Present (Alias (E))
8387 and then
8388 Is_Predefined_Dispatching_Operation (Alias (E)))
8389 then
8390 if Present (Alias (E)) then
8391 Set_Overridden_Operation (S, Alias (E));
8392 end if;
8393 end if;
8394
8395 if Is_Dispatching_Operation (E) then
8396
8397 -- An overriding dispatching subprogram inherits the
8398 -- convention of the overridden subprogram (AI-117).
8399
8400 Set_Convention (S, Convention (E));
8401 Check_Dispatching_Operation (S, E);
8402
8403 else
8404 Check_Dispatching_Operation (S, Empty);
8405 end if;
8406
8407 Check_For_Primitive_Subprogram
8408 (Is_Primitive_Subp, Is_Overriding => True);
8409 goto Check_Inequality;
8410 end;
8411
8412 -- Apparent redeclarations in instances can occur when two
8413 -- formal types get the same actual type. The subprograms in
8414 -- in the instance are legal, even if not callable from the
8415 -- outside. Calls from within are disambiguated elsewhere.
8416 -- For dispatching operations in the visible part, the usual
8417 -- rules apply, and operations with the same profile are not
8418 -- legal (B830001).
8419
8420 elsif (In_Instance_Visible_Part
8421 and then not Is_Dispatching_Operation (E))
8422 or else In_Instance_Not_Visible
8423 then
8424 null;
8425
8426 -- Here we have a real error (identical profile)
8427
8428 else
8429 Error_Msg_Sloc := Sloc (E);
8430
8431 -- Avoid cascaded errors if the entity appears in
8432 -- subsequent calls.
8433
8434 Set_Scope (S, Current_Scope);
8435
8436 -- Generate error, with extra useful warning for the case
8437 -- of a generic instance with no completion.
8438
8439 if Is_Generic_Instance (S)
8440 and then not Has_Completion (E)
8441 then
8442 Error_Msg_N
8443 ("instantiation cannot provide body for&", S);
8444 Error_Msg_N ("\& conflicts with declaration#", S);
8445 else
8446 Error_Msg_N ("& conflicts with declaration#", S);
8447 end if;
8448
8449 return;
8450 end if;
8451
8452 else
8453 -- If one subprogram has an access parameter and the other
8454 -- a parameter of an access type, calls to either might be
8455 -- ambiguous. Verify that parameters match except for the
8456 -- access parameter.
8457
8458 if May_Hide_Profile then
8459 declare
8460 F1 : Entity_Id;
8461 F2 : Entity_Id;
8462
8463 begin
8464 F1 := First_Formal (S);
8465 F2 := First_Formal (E);
8466 while Present (F1) and then Present (F2) loop
8467 if Is_Access_Type (Etype (F1)) then
8468 if not Is_Access_Type (Etype (F2))
8469 or else not Conforming_Types
8470 (Designated_Type (Etype (F1)),
8471 Designated_Type (Etype (F2)),
8472 Type_Conformant)
8473 then
8474 May_Hide_Profile := False;
8475 end if;
8476
8477 elsif
8478 not Conforming_Types
8479 (Etype (F1), Etype (F2), Type_Conformant)
8480 then
8481 May_Hide_Profile := False;
8482 end if;
8483
8484 Next_Formal (F1);
8485 Next_Formal (F2);
8486 end loop;
8487
8488 if May_Hide_Profile
8489 and then No (F1)
8490 and then No (F2)
8491 then
8492 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
8493 end if;
8494 end;
8495 end if;
8496 end if;
8497
8498 E := Homonym (E);
8499 end loop;
8500
8501 -- On exit, we know that S is a new entity
8502
8503 Enter_Overloaded_Entity (S);
8504 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8505 Check_Overriding_Indicator
8506 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
8507
8508 -- Overloading is not allowed in SPARK or ALFA
8509
8510 Error_Msg_Sloc := Sloc (Homonym (S));
8511 Check_Formal_Restriction ("overloading not allowed with entity#", S);
8512
8513 -- If S is a derived operation for an untagged type then by
8514 -- definition it's not a dispatching operation (even if the parent
8515 -- operation was dispatching), so we don't call
8516 -- Check_Dispatching_Operation in that case.
8517
8518 if No (Derived_Type)
8519 or else Is_Tagged_Type (Derived_Type)
8520 then
8521 Check_Dispatching_Operation (S, Empty);
8522 end if;
8523 end if;
8524
8525 -- If this is a user-defined equality operator that is not a derived
8526 -- subprogram, create the corresponding inequality. If the operation is
8527 -- dispatching, the expansion is done elsewhere, and we do not create
8528 -- an explicit inequality operation.
8529
8530 <<Check_Inequality>>
8531 if Chars (S) = Name_Op_Eq
8532 and then Etype (S) = Standard_Boolean
8533 and then Present (Parent (S))
8534 and then not Is_Dispatching_Operation (S)
8535 then
8536 Make_Inequality_Operator (S);
8537
8538 if Ada_Version >= Ada_2012 then
8539 Check_Untagged_Equality (S);
8540 end if;
8541 end if;
8542 end New_Overloaded_Entity;
8543
8544 ---------------------
8545 -- Process_Formals --
8546 ---------------------
8547
8548 procedure Process_Formals
8549 (T : List_Id;
8550 Related_Nod : Node_Id)
8551 is
8552 Param_Spec : Node_Id;
8553 Formal : Entity_Id;
8554 Formal_Type : Entity_Id;
8555 Default : Node_Id;
8556 Ptype : Entity_Id;
8557
8558 Num_Out_Params : Nat := 0;
8559 First_Out_Param : Entity_Id := Empty;
8560 -- Used for setting Is_Only_Out_Parameter
8561
8562 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
8563 -- Determine whether an access type designates a type coming from a
8564 -- limited view.
8565
8566 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
8567 -- Check whether the default has a class-wide type. After analysis the
8568 -- default has the type of the formal, so we must also check explicitly
8569 -- for an access attribute.
8570
8571 -------------------------------
8572 -- Designates_From_With_Type --
8573 -------------------------------
8574
8575 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
8576 Desig : Entity_Id := Typ;
8577
8578 begin
8579 if Is_Access_Type (Desig) then
8580 Desig := Directly_Designated_Type (Desig);
8581 end if;
8582
8583 if Is_Class_Wide_Type (Desig) then
8584 Desig := Root_Type (Desig);
8585 end if;
8586
8587 return
8588 Ekind (Desig) = E_Incomplete_Type
8589 and then From_With_Type (Desig);
8590 end Designates_From_With_Type;
8591
8592 ---------------------------
8593 -- Is_Class_Wide_Default --
8594 ---------------------------
8595
8596 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
8597 begin
8598 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
8599 or else (Nkind (D) = N_Attribute_Reference
8600 and then Attribute_Name (D) = Name_Access
8601 and then Is_Class_Wide_Type (Etype (Prefix (D))));
8602 end Is_Class_Wide_Default;
8603
8604 -- Start of processing for Process_Formals
8605
8606 begin
8607 -- In order to prevent premature use of the formals in the same formal
8608 -- part, the Ekind is left undefined until all default expressions are
8609 -- analyzed. The Ekind is established in a separate loop at the end.
8610
8611 Param_Spec := First (T);
8612 while Present (Param_Spec) loop
8613 Formal := Defining_Identifier (Param_Spec);
8614 Set_Never_Set_In_Source (Formal, True);
8615 Enter_Name (Formal);
8616
8617 -- Case of ordinary parameters
8618
8619 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
8620 Find_Type (Parameter_Type (Param_Spec));
8621 Ptype := Parameter_Type (Param_Spec);
8622
8623 if Ptype = Error then
8624 goto Continue;
8625 end if;
8626
8627 Formal_Type := Entity (Ptype);
8628
8629 if Is_Incomplete_Type (Formal_Type)
8630 or else
8631 (Is_Class_Wide_Type (Formal_Type)
8632 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
8633 then
8634 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
8635 -- primitive operations, as long as their completion is
8636 -- in the same declarative part. If in the private part
8637 -- this means that the type cannot be a Taft-amendment type.
8638 -- Check is done on package exit. For access to subprograms,
8639 -- the use is legal for Taft-amendment types.
8640
8641 if Is_Tagged_Type (Formal_Type) then
8642 if Ekind (Scope (Current_Scope)) = E_Package
8643 and then In_Private_Part (Scope (Current_Scope))
8644 and then not From_With_Type (Formal_Type)
8645 and then not Is_Class_Wide_Type (Formal_Type)
8646 then
8647 if not Nkind_In
8648 (Parent (T), N_Access_Function_Definition,
8649 N_Access_Procedure_Definition)
8650 then
8651 Append_Elmt
8652 (Current_Scope,
8653 Private_Dependents (Base_Type (Formal_Type)));
8654 end if;
8655 end if;
8656
8657 -- Special handling of Value_Type for CIL case
8658
8659 elsif Is_Value_Type (Formal_Type) then
8660 null;
8661
8662 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
8663 N_Access_Procedure_Definition)
8664 then
8665
8666 -- AI05-0151: Tagged incomplete types are allowed in all
8667 -- formal parts. Untagged incomplete types are not allowed
8668 -- in bodies.
8669
8670 if Ada_Version >= Ada_2012 then
8671 if Is_Tagged_Type (Formal_Type) then
8672 null;
8673
8674 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
8675 N_Entry_Body,
8676 N_Subprogram_Body)
8677 then
8678 Error_Msg_NE
8679 ("invalid use of untagged incomplete type&",
8680 Ptype, Formal_Type);
8681 end if;
8682
8683 else
8684 Error_Msg_NE
8685 ("invalid use of incomplete type&",
8686 Param_Spec, Formal_Type);
8687
8688 -- Further checks on the legality of incomplete types
8689 -- in formal parts are delayed until the freeze point
8690 -- of the enclosing subprogram or access to subprogram.
8691 end if;
8692 end if;
8693
8694 elsif Ekind (Formal_Type) = E_Void then
8695 Error_Msg_NE
8696 ("premature use of&",
8697 Parameter_Type (Param_Spec), Formal_Type);
8698 end if;
8699
8700 -- Ada 2005 (AI-231): Create and decorate an internal subtype
8701 -- declaration corresponding to the null-excluding type of the
8702 -- formal in the enclosing scope. Finally, replace the parameter
8703 -- type of the formal with the internal subtype.
8704
8705 if Ada_Version >= Ada_2005
8706 and then Null_Exclusion_Present (Param_Spec)
8707 then
8708 if not Is_Access_Type (Formal_Type) then
8709 Error_Msg_N
8710 ("`NOT NULL` allowed only for an access type", Param_Spec);
8711
8712 else
8713 if Can_Never_Be_Null (Formal_Type)
8714 and then Comes_From_Source (Related_Nod)
8715 then
8716 Error_Msg_NE
8717 ("`NOT NULL` not allowed (& already excludes null)",
8718 Param_Spec, Formal_Type);
8719 end if;
8720
8721 Formal_Type :=
8722 Create_Null_Excluding_Itype
8723 (T => Formal_Type,
8724 Related_Nod => Related_Nod,
8725 Scope_Id => Scope (Current_Scope));
8726
8727 -- If the designated type of the itype is an itype we
8728 -- decorate it with the Has_Delayed_Freeze attribute to
8729 -- avoid problems with the backend.
8730
8731 -- Example:
8732 -- type T is access procedure;
8733 -- procedure Op (O : not null T);
8734
8735 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
8736 Set_Has_Delayed_Freeze (Formal_Type);
8737 end if;
8738 end if;
8739 end if;
8740
8741 -- An access formal type
8742
8743 else
8744 Formal_Type :=
8745 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
8746
8747 -- No need to continue if we already notified errors
8748
8749 if not Present (Formal_Type) then
8750 return;
8751 end if;
8752
8753 -- Ada 2005 (AI-254)
8754
8755 declare
8756 AD : constant Node_Id :=
8757 Access_To_Subprogram_Definition
8758 (Parameter_Type (Param_Spec));
8759 begin
8760 if Present (AD) and then Protected_Present (AD) then
8761 Formal_Type :=
8762 Replace_Anonymous_Access_To_Protected_Subprogram
8763 (Param_Spec);
8764 end if;
8765 end;
8766 end if;
8767
8768 Set_Etype (Formal, Formal_Type);
8769 Default := Expression (Param_Spec);
8770
8771 if Present (Default) then
8772
8773 Check_Formal_Restriction
8774 ("default expression is not allowed", Default);
8775
8776 if Out_Present (Param_Spec) then
8777 Error_Msg_N
8778 ("default initialization only allowed for IN parameters",
8779 Param_Spec);
8780 end if;
8781
8782 -- Do the special preanalysis of the expression (see section on
8783 -- "Handling of Default Expressions" in the spec of package Sem).
8784
8785 Preanalyze_Spec_Expression (Default, Formal_Type);
8786
8787 -- An access to constant cannot be the default for
8788 -- an access parameter that is an access to variable.
8789
8790 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8791 and then not Is_Access_Constant (Formal_Type)
8792 and then Is_Access_Type (Etype (Default))
8793 and then Is_Access_Constant (Etype (Default))
8794 then
8795 Error_Msg_N
8796 ("formal that is access to variable cannot be initialized " &
8797 "with an access-to-constant expression", Default);
8798 end if;
8799
8800 -- Check that the designated type of an access parameter's default
8801 -- is not a class-wide type unless the parameter's designated type
8802 -- is also class-wide.
8803
8804 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8805 and then not Designates_From_With_Type (Formal_Type)
8806 and then Is_Class_Wide_Default (Default)
8807 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
8808 then
8809 Error_Msg_N
8810 ("access to class-wide expression not allowed here", Default);
8811 end if;
8812
8813 -- Check incorrect use of dynamically tagged expressions
8814
8815 if Is_Tagged_Type (Formal_Type) then
8816 Check_Dynamically_Tagged_Expression
8817 (Expr => Default,
8818 Typ => Formal_Type,
8819 Related_Nod => Default);
8820 end if;
8821 end if;
8822
8823 -- Ada 2005 (AI-231): Static checks
8824
8825 if Ada_Version >= Ada_2005
8826 and then Is_Access_Type (Etype (Formal))
8827 and then Can_Never_Be_Null (Etype (Formal))
8828 then
8829 Null_Exclusion_Static_Checks (Param_Spec);
8830 end if;
8831
8832 <<Continue>>
8833 Next (Param_Spec);
8834 end loop;
8835
8836 -- If this is the formal part of a function specification, analyze the
8837 -- subtype mark in the context where the formals are visible but not
8838 -- yet usable, and may hide outer homographs.
8839
8840 if Nkind (Related_Nod) = N_Function_Specification then
8841 Analyze_Return_Type (Related_Nod);
8842 end if;
8843
8844 -- Now set the kind (mode) of each formal
8845
8846 Param_Spec := First (T);
8847 while Present (Param_Spec) loop
8848 Formal := Defining_Identifier (Param_Spec);
8849 Set_Formal_Mode (Formal);
8850
8851 if Ekind (Formal) = E_In_Parameter then
8852 Set_Default_Value (Formal, Expression (Param_Spec));
8853
8854 if Present (Expression (Param_Spec)) then
8855 Default := Expression (Param_Spec);
8856
8857 if Is_Scalar_Type (Etype (Default)) then
8858 if Nkind
8859 (Parameter_Type (Param_Spec)) /= N_Access_Definition
8860 then
8861 Formal_Type := Entity (Parameter_Type (Param_Spec));
8862
8863 else
8864 Formal_Type := Access_Definition
8865 (Related_Nod, Parameter_Type (Param_Spec));
8866 end if;
8867
8868 Apply_Scalar_Range_Check (Default, Formal_Type);
8869 end if;
8870 end if;
8871
8872 elsif Ekind (Formal) = E_Out_Parameter then
8873 Num_Out_Params := Num_Out_Params + 1;
8874
8875 if Num_Out_Params = 1 then
8876 First_Out_Param := Formal;
8877 end if;
8878
8879 elsif Ekind (Formal) = E_In_Out_Parameter then
8880 Num_Out_Params := Num_Out_Params + 1;
8881 end if;
8882
8883 Next (Param_Spec);
8884 end loop;
8885
8886 if Present (First_Out_Param) and then Num_Out_Params = 1 then
8887 Set_Is_Only_Out_Parameter (First_Out_Param);
8888 end if;
8889 end Process_Formals;
8890
8891 ------------------
8892 -- Process_PPCs --
8893 ------------------
8894
8895 procedure Process_PPCs
8896 (N : Node_Id;
8897 Spec_Id : Entity_Id;
8898 Body_Id : Entity_Id)
8899 is
8900 Loc : constant Source_Ptr := Sloc (N);
8901 Prag : Node_Id;
8902 Parms : List_Id;
8903
8904 Designator : Entity_Id;
8905 -- Subprogram designator, set from Spec_Id if present, else Body_Id
8906
8907 Precond : Node_Id := Empty;
8908 -- Set non-Empty if we prepend precondition to the declarations. This
8909 -- is used to hook up inherited preconditions (adding the condition
8910 -- expression with OR ELSE, and adding the message).
8911
8912 Inherited_Precond : Node_Id;
8913 -- Precondition inherited from parent subprogram
8914
8915 Inherited : constant Subprogram_List :=
8916 Inherited_Subprograms (Spec_Id);
8917 -- List of subprograms inherited by this subprogram
8918
8919 Plist : List_Id := No_List;
8920 -- List of generated postconditions
8921
8922 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id;
8923 -- Prag contains an analyzed precondition or postcondition pragma. This
8924 -- function copies the pragma, changes it to the corresponding Check
8925 -- pragma and returns the Check pragma as the result. If Pspec is non-
8926 -- empty, this is the case of inheriting a PPC, where we must change
8927 -- references to parameters of the inherited subprogram to point to the
8928 -- corresponding parameters of the current subprogram.
8929
8930 function Invariants_Or_Predicates_Present return Boolean;
8931 -- Determines if any invariants or predicates are present for any OUT
8932 -- or IN OUT parameters of the subprogram, or (for a function) if the
8933 -- return value has an invariant.
8934
8935 --------------
8936 -- Grab_PPC --
8937 --------------
8938
8939 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id is
8940 Nam : constant Name_Id := Pragma_Name (Prag);
8941 Map : Elist_Id;
8942 CP : Node_Id;
8943
8944 begin
8945 -- Prepare map if this is the case where we have to map entities of
8946 -- arguments in the overridden subprogram to corresponding entities
8947 -- of the current subprogram.
8948
8949 if No (Pspec) then
8950 Map := No_Elist;
8951
8952 else
8953 declare
8954 PF : Entity_Id;
8955 CF : Entity_Id;
8956
8957 begin
8958 Map := New_Elmt_List;
8959 PF := First_Formal (Pspec);
8960 CF := First_Formal (Designator);
8961 while Present (PF) loop
8962 Append_Elmt (PF, Map);
8963 Append_Elmt (CF, Map);
8964 Next_Formal (PF);
8965 Next_Formal (CF);
8966 end loop;
8967 end;
8968 end if;
8969
8970 -- Now we can copy the tree, doing any required substitutions
8971
8972 CP := New_Copy_Tree (Prag, Map => Map, New_Scope => Current_Scope);
8973
8974 -- Set Analyzed to false, since we want to reanalyze the check
8975 -- procedure. Note that it is only at the outer level that we
8976 -- do this fiddling, for the spec cases, the already preanalyzed
8977 -- parameters are not affected.
8978
8979 Set_Analyzed (CP, False);
8980
8981 -- We also make sure Comes_From_Source is False for the copy
8982
8983 Set_Comes_From_Source (CP, False);
8984
8985 -- For a postcondition pragma within a generic, preserve the pragma
8986 -- for later expansion.
8987
8988 if Nam = Name_Postcondition
8989 and then not Expander_Active
8990 then
8991 return CP;
8992 end if;
8993
8994 -- Change copy of pragma into corresponding pragma Check
8995
8996 Prepend_To (Pragma_Argument_Associations (CP),
8997 Make_Pragma_Argument_Association (Sloc (Prag),
8998 Expression => Make_Identifier (Loc, Nam)));
8999 Set_Pragma_Identifier (CP, Make_Identifier (Sloc (Prag), Name_Check));
9000
9001 -- If this is inherited case and the current message starts with
9002 -- "failed p", we change it to "failed inherited p...".
9003
9004 if Present (Pspec) then
9005 declare
9006 Msg : constant Node_Id :=
9007 Last (Pragma_Argument_Associations (CP));
9008
9009 begin
9010 if Chars (Msg) = Name_Message then
9011 String_To_Name_Buffer (Strval (Expression (Msg)));
9012
9013 if Name_Buffer (1 .. 8) = "failed p" then
9014 Insert_Str_In_Name_Buffer ("inherited ", 8);
9015 Set_Strval
9016 (Expression (Last (Pragma_Argument_Associations (CP))),
9017 String_From_Name_Buffer);
9018 end if;
9019 end if;
9020 end;
9021 end if;
9022
9023 -- Return the check pragma
9024
9025 return CP;
9026 end Grab_PPC;
9027
9028 --------------------------------------
9029 -- Invariants_Or_Predicates_Present --
9030 --------------------------------------
9031
9032 function Invariants_Or_Predicates_Present return Boolean is
9033 Formal : Entity_Id;
9034
9035 begin
9036 -- Check function return result
9037
9038 if Ekind (Designator) /= E_Procedure
9039 and then Has_Invariants (Etype (Designator))
9040 then
9041 return True;
9042 end if;
9043
9044 -- Check parameters
9045
9046 Formal := First_Formal (Designator);
9047 while Present (Formal) loop
9048 if Ekind (Formal) /= E_In_Parameter
9049 and then
9050 (Has_Invariants (Etype (Formal))
9051 or else Present (Predicate_Function (Etype (Formal))))
9052 then
9053 return True;
9054 end if;
9055
9056 Next_Formal (Formal);
9057 end loop;
9058
9059 return False;
9060 end Invariants_Or_Predicates_Present;
9061
9062 -- Start of processing for Process_PPCs
9063
9064 begin
9065 -- Capture designator from spec if present, else from body
9066
9067 if Present (Spec_Id) then
9068 Designator := Spec_Id;
9069 else
9070 Designator := Body_Id;
9071 end if;
9072
9073 -- Grab preconditions from spec
9074
9075 if Present (Spec_Id) then
9076
9077 -- Loop through PPC pragmas from spec. Note that preconditions from
9078 -- the body will be analyzed and converted when we scan the body
9079 -- declarations below.
9080
9081 Prag := Spec_PPC_List (Spec_Id);
9082 while Present (Prag) loop
9083 if Pragma_Name (Prag) = Name_Precondition then
9084
9085 -- For Pre (or Precondition pragma), we simply prepend the
9086 -- pragma to the list of declarations right away so that it
9087 -- will be executed at the start of the procedure. Note that
9088 -- this processing reverses the order of the list, which is
9089 -- what we want since new entries were chained to the head of
9090 -- the list. There can be more then one precondition when we
9091 -- use pragma Precondition
9092
9093 if not Class_Present (Prag) then
9094 Prepend (Grab_PPC, Declarations (N));
9095
9096 -- For Pre'Class there can only be one pragma, and we save
9097 -- it in Precond for now. We will add inherited Pre'Class
9098 -- stuff before inserting this pragma in the declarations.
9099 else
9100 Precond := Grab_PPC;
9101 end if;
9102 end if;
9103
9104 Prag := Next_Pragma (Prag);
9105 end loop;
9106
9107 -- Now deal with inherited preconditions
9108
9109 for J in Inherited'Range loop
9110 Prag := Spec_PPC_List (Inherited (J));
9111
9112 while Present (Prag) loop
9113 if Pragma_Name (Prag) = Name_Precondition
9114 and then Class_Present (Prag)
9115 then
9116 Inherited_Precond := Grab_PPC (Inherited (J));
9117
9118 -- No precondition so far, so establish this as the first
9119
9120 if No (Precond) then
9121 Precond := Inherited_Precond;
9122
9123 -- Here we already have a precondition, add inherited one
9124
9125 else
9126 -- Add new precondition to old one using OR ELSE
9127
9128 declare
9129 New_Expr : constant Node_Id :=
9130 Get_Pragma_Arg
9131 (Next
9132 (First
9133 (Pragma_Argument_Associations
9134 (Inherited_Precond))));
9135 Old_Expr : constant Node_Id :=
9136 Get_Pragma_Arg
9137 (Next
9138 (First
9139 (Pragma_Argument_Associations
9140 (Precond))));
9141
9142 begin
9143 if Paren_Count (Old_Expr) = 0 then
9144 Set_Paren_Count (Old_Expr, 1);
9145 end if;
9146
9147 if Paren_Count (New_Expr) = 0 then
9148 Set_Paren_Count (New_Expr, 1);
9149 end if;
9150
9151 Rewrite (Old_Expr,
9152 Make_Or_Else (Sloc (Old_Expr),
9153 Left_Opnd => Relocate_Node (Old_Expr),
9154 Right_Opnd => New_Expr));
9155 end;
9156
9157 -- Add new message in the form:
9158
9159 -- failed precondition from bla
9160 -- also failed inherited precondition from bla
9161 -- ...
9162
9163 -- Skip this if exception locations are suppressed
9164
9165 if not Exception_Locations_Suppressed then
9166 declare
9167 New_Msg : constant Node_Id :=
9168 Get_Pragma_Arg
9169 (Last
9170 (Pragma_Argument_Associations
9171 (Inherited_Precond)));
9172 Old_Msg : constant Node_Id :=
9173 Get_Pragma_Arg
9174 (Last
9175 (Pragma_Argument_Associations
9176 (Precond)));
9177 begin
9178 Start_String (Strval (Old_Msg));
9179 Store_String_Chars (ASCII.LF & " also ");
9180 Store_String_Chars (Strval (New_Msg));
9181 Set_Strval (Old_Msg, End_String);
9182 end;
9183 end if;
9184 end if;
9185 end if;
9186
9187 Prag := Next_Pragma (Prag);
9188 end loop;
9189 end loop;
9190
9191 -- If we have built a precondition for Pre'Class (including any
9192 -- Pre'Class aspects inherited from parent subprograms), then we
9193 -- insert this composite precondition at this stage.
9194
9195 if Present (Precond) then
9196 Prepend (Precond, Declarations (N));
9197 end if;
9198 end if;
9199
9200 -- Build postconditions procedure if needed and prepend the following
9201 -- declaration to the start of the declarations for the subprogram.
9202
9203 -- procedure _postconditions [(_Result : resulttype)] is
9204 -- begin
9205 -- pragma Check (Postcondition, condition [,message]);
9206 -- pragma Check (Postcondition, condition [,message]);
9207 -- ...
9208 -- Invariant_Procedure (_Result) ...
9209 -- Invariant_Procedure (Arg1)
9210 -- ...
9211 -- end;
9212
9213 -- First we deal with the postconditions in the body
9214
9215 if Is_Non_Empty_List (Declarations (N)) then
9216
9217 -- Loop through declarations
9218
9219 Prag := First (Declarations (N));
9220 while Present (Prag) loop
9221 if Nkind (Prag) = N_Pragma then
9222
9223 -- If pragma, capture if enabled postcondition, else ignore
9224
9225 if Pragma_Name (Prag) = Name_Postcondition
9226 and then Check_Enabled (Name_Postcondition)
9227 then
9228 if Plist = No_List then
9229 Plist := Empty_List;
9230 end if;
9231
9232 Analyze (Prag);
9233
9234 -- If expansion is disabled, as in a generic unit, save
9235 -- pragma for later expansion.
9236
9237 if not Expander_Active then
9238 Prepend (Grab_PPC, Declarations (N));
9239 else
9240 Append (Grab_PPC, Plist);
9241 end if;
9242 end if;
9243
9244 Next (Prag);
9245
9246 -- Not a pragma, if comes from source, then end scan
9247
9248 elsif Comes_From_Source (Prag) then
9249 exit;
9250
9251 -- Skip stuff not coming from source
9252
9253 else
9254 Next (Prag);
9255 end if;
9256 end loop;
9257 end if;
9258
9259 -- Now deal with any postconditions from the spec
9260
9261 if Present (Spec_Id) then
9262 Spec_Postconditions : declare
9263 procedure Process_Post_Conditions
9264 (Spec : Node_Id;
9265 Class : Boolean);
9266 -- This processes the Spec_PPC_List from Spec, processing any
9267 -- postconditions from the list. If Class is True, then only
9268 -- postconditions marked with Class_Present are considered.
9269 -- The caller has checked that Spec_PPC_List is non-Empty.
9270
9271 -----------------------------
9272 -- Process_Post_Conditions --
9273 -----------------------------
9274
9275 procedure Process_Post_Conditions
9276 (Spec : Node_Id;
9277 Class : Boolean)
9278 is
9279 Pspec : Node_Id;
9280
9281 begin
9282 if Class then
9283 Pspec := Spec;
9284 else
9285 Pspec := Empty;
9286 end if;
9287
9288 -- Loop through PPC pragmas from spec
9289
9290 Prag := Spec_PPC_List (Spec);
9291 loop
9292 if Pragma_Name (Prag) = Name_Postcondition
9293 and then (not Class or else Class_Present (Prag))
9294 then
9295 if Plist = No_List then
9296 Plist := Empty_List;
9297 end if;
9298
9299 if not Expander_Active then
9300 Prepend
9301 (Grab_PPC (Pspec), Declarations (N));
9302 else
9303 Append (Grab_PPC (Pspec), Plist);
9304 end if;
9305 end if;
9306
9307 Prag := Next_Pragma (Prag);
9308 exit when No (Prag);
9309 end loop;
9310 end Process_Post_Conditions;
9311
9312 -- Start of processing for Spec_Postconditions
9313
9314 begin
9315 if Present (Spec_PPC_List (Spec_Id)) then
9316 Process_Post_Conditions (Spec_Id, Class => False);
9317 end if;
9318
9319 -- Process inherited postconditions
9320
9321 for J in Inherited'Range loop
9322 if Present (Spec_PPC_List (Inherited (J))) then
9323 Process_Post_Conditions (Inherited (J), Class => True);
9324 end if;
9325 end loop;
9326 end Spec_Postconditions;
9327 end if;
9328
9329 -- If we had any postconditions and expansion is enabled, or if the
9330 -- procedure has invariants, then build the _Postconditions procedure.
9331
9332 if (Present (Plist) or else Invariants_Or_Predicates_Present)
9333 and then Expander_Active
9334 then
9335 if No (Plist) then
9336 Plist := Empty_List;
9337 end if;
9338
9339 -- Special processing for function case
9340
9341 if Ekind (Designator) /= E_Procedure then
9342 declare
9343 Rent : constant Entity_Id :=
9344 Make_Defining_Identifier (Loc,
9345 Chars => Name_uResult);
9346 Ftyp : constant Entity_Id := Etype (Designator);
9347
9348 begin
9349 Set_Etype (Rent, Ftyp);
9350
9351 -- Add argument for return
9352
9353 Parms :=
9354 New_List (
9355 Make_Parameter_Specification (Loc,
9356 Parameter_Type => New_Occurrence_Of (Ftyp, Loc),
9357 Defining_Identifier => Rent));
9358
9359 -- Add invariant call if returning type with invariants
9360
9361 if Has_Invariants (Etype (Rent))
9362 and then Present (Invariant_Procedure (Etype (Rent)))
9363 then
9364 Append_To (Plist,
9365 Make_Invariant_Call (New_Occurrence_Of (Rent, Loc)));
9366 end if;
9367 end;
9368
9369 -- Procedure rather than a function
9370
9371 else
9372 Parms := No_List;
9373 end if;
9374
9375 -- Add invariant calls and predicate calls for parameters. Note that
9376 -- this is done for functions as well, since in Ada 2012 they can
9377 -- have IN OUT args.
9378
9379 declare
9380 Formal : Entity_Id;
9381 Ftype : Entity_Id;
9382
9383 begin
9384 Formal := First_Formal (Designator);
9385 while Present (Formal) loop
9386 if Ekind (Formal) /= E_In_Parameter then
9387 Ftype := Etype (Formal);
9388
9389 if Has_Invariants (Ftype)
9390 and then Present (Invariant_Procedure (Ftype))
9391 then
9392 Append_To (Plist,
9393 Make_Invariant_Call
9394 (New_Occurrence_Of (Formal, Loc)));
9395 end if;
9396
9397 if Present (Predicate_Function (Ftype)) then
9398 Append_To (Plist,
9399 Make_Predicate_Check
9400 (Ftype, New_Occurrence_Of (Formal, Loc)));
9401 end if;
9402 end if;
9403
9404 Next_Formal (Formal);
9405 end loop;
9406 end;
9407
9408 -- Build and insert postcondition procedure
9409
9410 declare
9411 Post_Proc : constant Entity_Id :=
9412 Make_Defining_Identifier (Loc,
9413 Chars => Name_uPostconditions);
9414 -- The entity for the _Postconditions procedure
9415
9416 begin
9417 Prepend_To (Declarations (N),
9418 Make_Subprogram_Body (Loc,
9419 Specification =>
9420 Make_Procedure_Specification (Loc,
9421 Defining_Unit_Name => Post_Proc,
9422 Parameter_Specifications => Parms),
9423
9424 Declarations => Empty_List,
9425
9426 Handled_Statement_Sequence =>
9427 Make_Handled_Sequence_Of_Statements (Loc,
9428 Statements => Plist)));
9429
9430 -- If this is a procedure, set the Postcondition_Proc attribute on
9431 -- the proper defining entity for the subprogram.
9432
9433 if Ekind (Designator) = E_Procedure then
9434 Set_Postcondition_Proc (Designator, Post_Proc);
9435 end if;
9436 end;
9437
9438 Set_Has_Postconditions (Designator);
9439 end if;
9440 end Process_PPCs;
9441
9442 ----------------------------
9443 -- Reference_Body_Formals --
9444 ----------------------------
9445
9446 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
9447 Fs : Entity_Id;
9448 Fb : Entity_Id;
9449
9450 begin
9451 if Error_Posted (Spec) then
9452 return;
9453 end if;
9454
9455 -- Iterate over both lists. They may be of different lengths if the two
9456 -- specs are not conformant.
9457
9458 Fs := First_Formal (Spec);
9459 Fb := First_Formal (Bod);
9460 while Present (Fs) and then Present (Fb) loop
9461 Generate_Reference (Fs, Fb, 'b');
9462
9463 if Style_Check then
9464 Style.Check_Identifier (Fb, Fs);
9465 end if;
9466
9467 Set_Spec_Entity (Fb, Fs);
9468 Set_Referenced (Fs, False);
9469 Next_Formal (Fs);
9470 Next_Formal (Fb);
9471 end loop;
9472 end Reference_Body_Formals;
9473
9474 -------------------------
9475 -- Set_Actual_Subtypes --
9476 -------------------------
9477
9478 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
9479 Decl : Node_Id;
9480 Formal : Entity_Id;
9481 T : Entity_Id;
9482 First_Stmt : Node_Id := Empty;
9483 AS_Needed : Boolean;
9484
9485 begin
9486 -- If this is an empty initialization procedure, no need to create
9487 -- actual subtypes (small optimization).
9488
9489 if Ekind (Subp) = E_Procedure
9490 and then Is_Null_Init_Proc (Subp)
9491 then
9492 return;
9493 end if;
9494
9495 Formal := First_Formal (Subp);
9496 while Present (Formal) loop
9497 T := Etype (Formal);
9498
9499 -- We never need an actual subtype for a constrained formal
9500
9501 if Is_Constrained (T) then
9502 AS_Needed := False;
9503
9504 -- If we have unknown discriminants, then we do not need an actual
9505 -- subtype, or more accurately we cannot figure it out! Note that
9506 -- all class-wide types have unknown discriminants.
9507
9508 elsif Has_Unknown_Discriminants (T) then
9509 AS_Needed := False;
9510
9511 -- At this stage we have an unconstrained type that may need an
9512 -- actual subtype. For sure the actual subtype is needed if we have
9513 -- an unconstrained array type.
9514
9515 elsif Is_Array_Type (T) then
9516 AS_Needed := True;
9517
9518 -- The only other case needing an actual subtype is an unconstrained
9519 -- record type which is an IN parameter (we cannot generate actual
9520 -- subtypes for the OUT or IN OUT case, since an assignment can
9521 -- change the discriminant values. However we exclude the case of
9522 -- initialization procedures, since discriminants are handled very
9523 -- specially in this context, see the section entitled "Handling of
9524 -- Discriminants" in Einfo.
9525
9526 -- We also exclude the case of Discrim_SO_Functions (functions used
9527 -- in front end layout mode for size/offset values), since in such
9528 -- functions only discriminants are referenced, and not only are such
9529 -- subtypes not needed, but they cannot always be generated, because
9530 -- of order of elaboration issues.
9531
9532 elsif Is_Record_Type (T)
9533 and then Ekind (Formal) = E_In_Parameter
9534 and then Chars (Formal) /= Name_uInit
9535 and then not Is_Unchecked_Union (T)
9536 and then not Is_Discrim_SO_Function (Subp)
9537 then
9538 AS_Needed := True;
9539
9540 -- All other cases do not need an actual subtype
9541
9542 else
9543 AS_Needed := False;
9544 end if;
9545
9546 -- Generate actual subtypes for unconstrained arrays and
9547 -- unconstrained discriminated records.
9548
9549 if AS_Needed then
9550 if Nkind (N) = N_Accept_Statement then
9551
9552 -- If expansion is active, The formal is replaced by a local
9553 -- variable that renames the corresponding entry of the
9554 -- parameter block, and it is this local variable that may
9555 -- require an actual subtype.
9556
9557 if Expander_Active then
9558 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
9559 else
9560 Decl := Build_Actual_Subtype (T, Formal);
9561 end if;
9562
9563 if Present (Handled_Statement_Sequence (N)) then
9564 First_Stmt :=
9565 First (Statements (Handled_Statement_Sequence (N)));
9566 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
9567 Mark_Rewrite_Insertion (Decl);
9568 else
9569 -- If the accept statement has no body, there will be no
9570 -- reference to the actuals, so no need to compute actual
9571 -- subtypes.
9572
9573 return;
9574 end if;
9575
9576 else
9577 Decl := Build_Actual_Subtype (T, Formal);
9578 Prepend (Decl, Declarations (N));
9579 Mark_Rewrite_Insertion (Decl);
9580 end if;
9581
9582 -- The declaration uses the bounds of an existing object, and
9583 -- therefore needs no constraint checks.
9584
9585 Analyze (Decl, Suppress => All_Checks);
9586
9587 -- We need to freeze manually the generated type when it is
9588 -- inserted anywhere else than in a declarative part.
9589
9590 if Present (First_Stmt) then
9591 Insert_List_Before_And_Analyze (First_Stmt,
9592 Freeze_Entity (Defining_Identifier (Decl), N));
9593 end if;
9594
9595 if Nkind (N) = N_Accept_Statement
9596 and then Expander_Active
9597 then
9598 Set_Actual_Subtype (Renamed_Object (Formal),
9599 Defining_Identifier (Decl));
9600 else
9601 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
9602 end if;
9603 end if;
9604
9605 Next_Formal (Formal);
9606 end loop;
9607 end Set_Actual_Subtypes;
9608
9609 ---------------------
9610 -- Set_Formal_Mode --
9611 ---------------------
9612
9613 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
9614 Spec : constant Node_Id := Parent (Formal_Id);
9615
9616 begin
9617 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
9618 -- since we ensure that corresponding actuals are always valid at the
9619 -- point of the call.
9620
9621 if Out_Present (Spec) then
9622 if Ekind (Scope (Formal_Id)) = E_Function
9623 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
9624 then
9625 -- [IN] OUT parameters allowed for functions in Ada 2012
9626
9627 if Ada_Version >= Ada_2012 then
9628 if In_Present (Spec) then
9629 Set_Ekind (Formal_Id, E_In_Out_Parameter);
9630 else
9631 Set_Ekind (Formal_Id, E_Out_Parameter);
9632 end if;
9633
9634 -- But not in earlier versions of Ada
9635
9636 else
9637 Error_Msg_N ("functions can only have IN parameters", Spec);
9638 Set_Ekind (Formal_Id, E_In_Parameter);
9639 end if;
9640
9641 elsif In_Present (Spec) then
9642 Set_Ekind (Formal_Id, E_In_Out_Parameter);
9643
9644 else
9645 Set_Ekind (Formal_Id, E_Out_Parameter);
9646 Set_Never_Set_In_Source (Formal_Id, True);
9647 Set_Is_True_Constant (Formal_Id, False);
9648 Set_Current_Value (Formal_Id, Empty);
9649 end if;
9650
9651 else
9652 Set_Ekind (Formal_Id, E_In_Parameter);
9653 end if;
9654
9655 -- Set Is_Known_Non_Null for access parameters since the language
9656 -- guarantees that access parameters are always non-null. We also set
9657 -- Can_Never_Be_Null, since there is no way to change the value.
9658
9659 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
9660
9661 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
9662 -- null; In Ada 2005, only if then null_exclusion is explicit.
9663
9664 if Ada_Version < Ada_2005
9665 or else Can_Never_Be_Null (Etype (Formal_Id))
9666 then
9667 Set_Is_Known_Non_Null (Formal_Id);
9668 Set_Can_Never_Be_Null (Formal_Id);
9669 end if;
9670
9671 -- Ada 2005 (AI-231): Null-exclusion access subtype
9672
9673 elsif Is_Access_Type (Etype (Formal_Id))
9674 and then Can_Never_Be_Null (Etype (Formal_Id))
9675 then
9676 Set_Is_Known_Non_Null (Formal_Id);
9677 end if;
9678
9679 Set_Mechanism (Formal_Id, Default_Mechanism);
9680 Set_Formal_Validity (Formal_Id);
9681 end Set_Formal_Mode;
9682
9683 -------------------------
9684 -- Set_Formal_Validity --
9685 -------------------------
9686
9687 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
9688 begin
9689 -- If no validity checking, then we cannot assume anything about the
9690 -- validity of parameters, since we do not know there is any checking
9691 -- of the validity on the call side.
9692
9693 if not Validity_Checks_On then
9694 return;
9695
9696 -- If validity checking for parameters is enabled, this means we are
9697 -- not supposed to make any assumptions about argument values.
9698
9699 elsif Validity_Check_Parameters then
9700 return;
9701
9702 -- If we are checking in parameters, we will assume that the caller is
9703 -- also checking parameters, so we can assume the parameter is valid.
9704
9705 elsif Ekind (Formal_Id) = E_In_Parameter
9706 and then Validity_Check_In_Params
9707 then
9708 Set_Is_Known_Valid (Formal_Id, True);
9709
9710 -- Similar treatment for IN OUT parameters
9711
9712 elsif Ekind (Formal_Id) = E_In_Out_Parameter
9713 and then Validity_Check_In_Out_Params
9714 then
9715 Set_Is_Known_Valid (Formal_Id, True);
9716 end if;
9717 end Set_Formal_Validity;
9718
9719 ------------------------
9720 -- Subtype_Conformant --
9721 ------------------------
9722
9723 function Subtype_Conformant
9724 (New_Id : Entity_Id;
9725 Old_Id : Entity_Id;
9726 Skip_Controlling_Formals : Boolean := False) return Boolean
9727 is
9728 Result : Boolean;
9729 begin
9730 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
9731 Skip_Controlling_Formals => Skip_Controlling_Formals);
9732 return Result;
9733 end Subtype_Conformant;
9734
9735 ---------------------
9736 -- Type_Conformant --
9737 ---------------------
9738
9739 function Type_Conformant
9740 (New_Id : Entity_Id;
9741 Old_Id : Entity_Id;
9742 Skip_Controlling_Formals : Boolean := False) return Boolean
9743 is
9744 Result : Boolean;
9745 begin
9746 May_Hide_Profile := False;
9747
9748 Check_Conformance
9749 (New_Id, Old_Id, Type_Conformant, False, Result,
9750 Skip_Controlling_Formals => Skip_Controlling_Formals);
9751 return Result;
9752 end Type_Conformant;
9753
9754 -------------------------------
9755 -- Valid_Operator_Definition --
9756 -------------------------------
9757
9758 procedure Valid_Operator_Definition (Designator : Entity_Id) is
9759 N : Integer := 0;
9760 F : Entity_Id;
9761 Id : constant Name_Id := Chars (Designator);
9762 N_OK : Boolean;
9763
9764 begin
9765 F := First_Formal (Designator);
9766 while Present (F) loop
9767 N := N + 1;
9768
9769 if Present (Default_Value (F)) then
9770 Error_Msg_N
9771 ("default values not allowed for operator parameters",
9772 Parent (F));
9773 end if;
9774
9775 Next_Formal (F);
9776 end loop;
9777
9778 -- Verify that user-defined operators have proper number of arguments
9779 -- First case of operators which can only be unary
9780
9781 if Id = Name_Op_Not
9782 or else Id = Name_Op_Abs
9783 then
9784 N_OK := (N = 1);
9785
9786 -- Case of operators which can be unary or binary
9787
9788 elsif Id = Name_Op_Add
9789 or Id = Name_Op_Subtract
9790 then
9791 N_OK := (N in 1 .. 2);
9792
9793 -- All other operators can only be binary
9794
9795 else
9796 N_OK := (N = 2);
9797 end if;
9798
9799 if not N_OK then
9800 Error_Msg_N
9801 ("incorrect number of arguments for operator", Designator);
9802 end if;
9803
9804 if Id = Name_Op_Ne
9805 and then Base_Type (Etype (Designator)) = Standard_Boolean
9806 and then not Is_Intrinsic_Subprogram (Designator)
9807 then
9808 Error_Msg_N
9809 ("explicit definition of inequality not allowed", Designator);
9810 end if;
9811 end Valid_Operator_Definition;
9812
9813 end Sem_Ch6;