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