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