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