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