1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2022, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
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 Einfo
.Entities
; use Einfo
.Entities
;
33 with Einfo
.Utils
; use Einfo
.Utils
;
34 with Elists
; use Elists
;
35 with Errout
; use Errout
;
36 with Expander
; use Expander
;
37 with Exp_Ch6
; use Exp_Ch6
;
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
;
50 with Nlists
; use Nlists
;
51 with Nmake
; use Nmake
;
53 with Output
; use Output
;
54 with Rtsfind
; use Rtsfind
;
56 with Sem_Aux
; use Sem_Aux
;
57 with Sem_Cat
; use Sem_Cat
;
58 with Sem_Ch3
; use Sem_Ch3
;
59 with Sem_Ch4
; use Sem_Ch4
;
60 with Sem_Ch5
; use Sem_Ch5
;
61 with Sem_Ch8
; use Sem_Ch8
;
62 with Sem_Ch9
; use Sem_Ch9
;
63 with Sem_Ch10
; use Sem_Ch10
;
64 with Sem_Ch12
; use Sem_Ch12
;
65 with Sem_Ch13
; use Sem_Ch13
;
66 with Sem_Dim
; use Sem_Dim
;
67 with Sem_Disp
; use Sem_Disp
;
68 with Sem_Dist
; use Sem_Dist
;
69 with Sem_Elim
; use Sem_Elim
;
70 with Sem_Eval
; use Sem_Eval
;
71 with Sem_Mech
; use Sem_Mech
;
72 with Sem_Prag
; use Sem_Prag
;
73 with Sem_Res
; use Sem_Res
;
74 with Sem_Util
; use Sem_Util
;
75 with Sem_Type
; use Sem_Type
;
76 with Sem_Warn
; use Sem_Warn
;
77 with Sinput
; use Sinput
;
78 with Stand
; use Stand
;
79 with Sinfo
; use Sinfo
;
80 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
81 with Sinfo
.Utils
; use Sinfo
.Utils
;
82 with Sinfo
.CN
; use Sinfo
.CN
;
83 with Snames
; use Snames
;
84 with Stringt
; use Stringt
;
86 with Stylesw
; use Stylesw
;
87 with Tbuild
; use Tbuild
;
88 with Uintp
; use Uintp
;
89 with Urealp
; use Urealp
;
90 with Validsw
; use Validsw
;
91 with Warnsw
; use Warnsw
;
93 package body Sem_Ch6
is
95 May_Hide_Profile
: Boolean := False;
96 -- This flag is used to indicate that two formals in two subprograms being
97 -- checked for conformance differ only in that one is an access parameter
98 -- while the other is of a general access type with the same designated
99 -- type. In this case, if the rest of the signatures match, a call to
100 -- either subprogram may be ambiguous, which is worth a warning. The flag
101 -- is set in Compatible_Types, and the warning emitted in
102 -- New_Overloaded_Entity.
104 -----------------------
105 -- Local Subprograms --
106 -----------------------
108 procedure Analyze_Function_Return
(N
: Node_Id
);
109 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
110 -- applies to a [generic] function.
112 procedure Analyze_Generic_Subprogram_Body
(N
: Node_Id
; Gen_Id
: Entity_Id
);
113 -- Analyze a generic subprogram body. N is the body to be analyzed, and
114 -- Gen_Id is the defining entity Id for the corresponding spec.
116 procedure Analyze_Null_Procedure
118 Is_Completion
: out Boolean);
119 -- A null procedure can be a declaration or (Ada 2012) a completion
121 procedure Analyze_Return_Statement
(N
: Node_Id
);
122 -- Common processing for simple and extended return statements
124 procedure Analyze_Return_Type
(N
: Node_Id
);
125 -- Subsidiary to Process_Formals: analyze subtype mark in function
126 -- specification in a context where the formals are visible and hide
129 procedure Analyze_Subprogram_Body_Helper
(N
: Node_Id
);
130 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
131 -- that we can use RETURN but not skip the debug output at the end.
133 procedure Check_Conformance
136 Ctype
: Conformance_Type
;
138 Conforms
: out Boolean;
139 Err_Loc
: Node_Id
:= Empty
;
140 Get_Inst
: Boolean := False;
141 Skip_Controlling_Formals
: Boolean := False);
142 -- Given two entities, this procedure checks that the profiles associated
143 -- with these entities meet the conformance criterion given by the third
144 -- parameter. If they conform, Conforms is set True and control returns
145 -- to the caller. If they do not conform, Conforms is set to False, and
146 -- in addition, if Errmsg is True on the call, proper messages are output
147 -- to complain about the conformance failure. If Err_Loc is non_Empty
148 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
149 -- error messages are placed on the appropriate part of the construct
150 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
151 -- against a formal access-to-subprogram type so Get_Instance_Of must
154 procedure Check_Formal_Subprogram_Conformance
159 Conforms
: out Boolean);
160 -- Core implementation of Check_Formal_Subprogram_Conformance from spec.
161 -- Errmsg can be set to False to not emit error messages.
162 -- Conforms is set to True if there is conformance, False otherwise.
164 procedure Check_Limited_Return
168 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning limited
169 -- types. Used only for simple return statements. Expr is the expression
172 procedure Check_Subprogram_Order
(N
: Node_Id
);
173 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
174 -- the alpha ordering rule for N if this ordering requirement applicable.
176 procedure Check_Returns
180 Proc
: Entity_Id
:= Empty
);
181 -- Called to check for missing return statements in a function body, or for
182 -- returns present in a procedure body which has No_Return set. HSS is the
183 -- handled statement sequence for the subprogram body. This procedure
184 -- checks all flow paths to make sure they either have return (Mode = 'F',
185 -- used for functions) or do not have a return (Mode = 'P', used for
186 -- No_Return procedures). The flag Err is set if there are any control
187 -- paths not explicitly terminated by a return in the function case, and is
188 -- True otherwise. Proc is the entity for the procedure case and is used
189 -- in posting the warning message.
191 procedure Check_Untagged_Equality
(Eq_Op
: Entity_Id
);
192 -- In Ada 2012, a primitive equality operator for an untagged record type
193 -- must appear before the type is frozen. This procedure checks that this
194 -- rule is met, and otherwise gives an error on the subprogram declaration
195 -- and a warning on the earlier freeze point if it is easy to pinpoint. In
196 -- earlier versions of Ada, the call has not effect, unless compatibility
197 -- warnings are requested by means of Warn_On_Ada_2012_Incompatibility.
199 procedure Enter_Overloaded_Entity
(S
: Entity_Id
);
200 -- This procedure makes S, a new overloaded entity, into the first visible
201 -- entity with that name.
203 function Is_Non_Overriding_Operation
205 New_E
: Entity_Id
) return Boolean;
206 -- Enforce the rule given in 12.3(18): a private operation in an instance
207 -- overrides an inherited operation only if the corresponding operation
208 -- was overriding in the generic. This needs to be checked for primitive
209 -- operations of types derived (in the generic unit) from formal private
210 -- or formal derived types.
212 procedure Make_Inequality_Operator
(S
: Entity_Id
);
213 -- Create the declaration for an inequality operator that is implicitly
214 -- created by a user-defined equality operator that yields a boolean.
216 procedure Preanalyze_Formal_Expression
(N
: Node_Id
; T
: Entity_Id
);
217 -- Preanalysis of default expressions of subprogram formals. N is the
218 -- expression to be analyzed and T is the expected type.
220 procedure Set_Formal_Validity
(Formal_Id
: Entity_Id
);
221 -- Formal_Id is an formal parameter entity. This procedure deals with
222 -- setting the proper validity status for this entity, which depends on
223 -- the kind of parameter and the validity checking mode.
225 ---------------------------------------------
226 -- Analyze_Abstract_Subprogram_Declaration --
227 ---------------------------------------------
229 procedure Analyze_Abstract_Subprogram_Declaration
(N
: Node_Id
) is
230 Scop
: constant Entity_Id
:= Current_Scope
;
231 Subp_Id
: constant Entity_Id
:=
232 Analyze_Subprogram_Specification
(Specification
(N
));
235 Generate_Definition
(Subp_Id
);
237 -- Set the SPARK mode from the current context (may be overwritten later
238 -- with explicit pragma).
240 Set_SPARK_Pragma
(Subp_Id
, SPARK_Mode_Pragma
);
241 Set_SPARK_Pragma_Inherited
(Subp_Id
);
243 -- Preserve relevant elaboration-related attributes of the context which
244 -- are no longer available or very expensive to recompute once analysis,
245 -- resolution, and expansion are over.
247 Mark_Elaboration_Attributes
252 Set_Is_Abstract_Subprogram
(Subp_Id
);
253 New_Overloaded_Entity
(Subp_Id
);
254 Check_Delayed_Subprogram
(Subp_Id
);
256 Set_Categorization_From_Scope
(Subp_Id
, Scop
);
258 if Ekind
(Scope
(Subp_Id
)) = E_Protected_Type
then
259 Error_Msg_N
("abstract subprogram not allowed in protected type", N
);
261 -- Issue a warning if the abstract subprogram is neither a dispatching
262 -- operation nor an operation that overrides an inherited subprogram or
263 -- predefined operator, since this most likely indicates a mistake.
265 elsif Warn_On_Redundant_Constructs
266 and then not Is_Dispatching_Operation
(Subp_Id
)
267 and then not Present
(Overridden_Operation
(Subp_Id
))
268 and then (not Is_Operator_Symbol_Name
(Chars
(Subp_Id
))
269 or else Scop
/= Scope
(Etype
(First_Formal
(Subp_Id
))))
272 ("abstract subprogram is not dispatching or overriding?r?", N
);
275 Generate_Reference_To_Formals
(Subp_Id
);
276 Check_Eliminated
(Subp_Id
);
278 if Has_Aspects
(N
) then
279 Analyze_Aspect_Specifications
(N
, Subp_Id
);
281 end Analyze_Abstract_Subprogram_Declaration
;
283 ---------------------------------
284 -- Analyze_Expression_Function --
285 ---------------------------------
287 procedure Analyze_Expression_Function
(N
: Node_Id
) is
288 Expr
: constant Node_Id
:= Expression
(N
);
289 Loc
: constant Source_Ptr
:= Sloc
(N
);
290 LocX
: constant Source_Ptr
:= Sloc
(Expr
);
291 Spec
: constant Node_Id
:= Specification
(N
);
298 Orig_N
: Node_Id
:= Empty
;
300 Typ
: Entity_Id
:= Empty
;
302 Def_Id
: Entity_Id
:= Empty
;
304 -- If the expression is a completion, Prev is the entity whose
305 -- declaration is completed. Def_Id is needed to analyze the spec.
308 -- This is one of the occasions on which we transform the tree during
309 -- semantic analysis. If this is a completion, transform the expression
310 -- function into an equivalent subprogram body, and analyze it.
312 -- Expression functions are inlined unconditionally. The back-end will
313 -- determine whether this is possible.
315 Inline_Processing_Required
:= True;
317 -- Create a specification for the generated body. This must be done
318 -- prior to the analysis of the initial declaration.
320 New_Spec
:= Copy_Subprogram_Spec
(Spec
);
321 Prev
:= Current_Entity_In_Scope
(Defining_Entity
(Spec
));
323 -- If there are previous overloadable entities with the same name,
324 -- check whether any of them is completed by the expression function.
325 -- In a generic context a formal subprogram has no completion.
328 and then Is_Overloadable
(Prev
)
329 and then not Is_Formal_Subprogram
(Prev
)
331 Def_Id
:= Analyze_Subprogram_Specification
(Spec
);
332 Prev
:= Find_Corresponding_Spec
(N
);
334 Typ
:= Etype
(Def_Id
);
336 -- The previous entity may be an expression function as well, in
337 -- which case the redeclaration is illegal.
340 and then Nkind
(Original_Node
(Unit_Declaration_Node
(Prev
))) =
341 N_Expression_Function
343 Error_Msg_Sloc
:= Sloc
(Prev
);
344 Error_Msg_N
("& conflicts with declaration#", Def_Id
);
349 Ret
:= Make_Simple_Return_Statement
(LocX
, Expr
);
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
);
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.
366 if Present
(Prev
) and then Ekind
(Prev
) = E_Generic_Function
then
367 Insert_After
(N
, New_Body
);
369 -- Propagate any aspects or pragmas that apply to the expression
370 -- function to the proper body when the expression function acts
373 if Has_Aspects
(N
) then
374 Move_Aspects
(N
, To
=> New_Body
);
377 Relocate_Pragmas_To_Body
(New_Body
);
379 Rewrite
(N
, Make_Null_Statement
(Loc
));
380 Set_Has_Completion
(Prev
, False);
383 Set_Is_Inlined
(Prev
);
386 and then Is_Overloadable
(Prev
)
387 and then not Is_Formal_Subprogram
(Prev
)
389 Set_Has_Completion
(Prev
, False);
390 Set_Is_Inlined
(Prev
);
392 -- AI12-0103: Expression functions that are a completion freeze their
393 -- expression but don't freeze anything else (unlike regular bodies).
395 -- Note that we cannot defer this freezing to the analysis of the
396 -- expression itself, because a freeze node might appear in a nested
397 -- scope, leading to an elaboration order issue in gigi.
398 -- As elsewhere, we do not emit freeze nodes within a generic unit.
400 if not Inside_A_Generic
then
408 -- For navigation purposes, indicate that the function is a body
410 Generate_Reference
(Prev
, Defining_Entity
(N
), 'b', Force
=> True);
411 Rewrite
(N
, New_Body
);
413 -- Remove any existing aspects from the original node because the act
414 -- of rewriting causes the list to be shared between the two nodes.
416 Orig_N
:= Original_Node
(N
);
417 Remove_Aspects
(Orig_N
);
419 -- Propagate any pragmas that apply to expression function to the
420 -- proper body when the expression function acts as a completion.
421 -- Aspects are automatically transfered because of node rewriting.
423 Relocate_Pragmas_To_Body
(N
);
426 -- Prev is the previous entity with the same name, but it is can
427 -- be an unrelated spec that is not completed by the expression
428 -- function. In that case the relevant entity is the one in the body.
429 -- Not clear that the backend can inline it in this case ???
431 if Has_Completion
(Prev
) then
433 -- The formals of the expression function are body formals,
434 -- and do not appear in the ali file, which will only contain
435 -- references to the formals of the original subprogram spec.
442 F1
:= First_Formal
(Def_Id
);
443 F2
:= First_Formal
(Prev
);
445 while Present
(F1
) loop
446 Set_Spec_Entity
(F1
, F2
);
453 Set_Is_Inlined
(Defining_Entity
(New_Body
));
456 -- If this is not a completion, create both a declaration and a body, so
457 -- that the expression can be inlined whenever possible.
460 -- An expression function that is not a completion is not a
461 -- subprogram declaration, and thus cannot appear in a protected
464 if Nkind
(Parent
(N
)) = N_Protected_Definition
then
466 ("an expression function is not a legal protected operation", N
);
469 Rewrite
(N
, Make_Subprogram_Declaration
(Loc
, Specification
=> Spec
));
471 -- Remove any existing aspects from the original node because the act
472 -- of rewriting causes the list to be shared between the two nodes.
474 Orig_N
:= Original_Node
(N
);
475 Remove_Aspects
(Orig_N
);
479 -- If aspect SPARK_Mode was specified on the body, it needs to be
480 -- repeated both on the generated spec and the body.
482 Asp
:= Find_Aspect
(Defining_Unit_Name
(Spec
), Aspect_SPARK_Mode
);
484 if Present
(Asp
) then
485 Asp
:= New_Copy_Tree
(Asp
);
486 Set_Analyzed
(Asp
, False);
487 Set_Aspect_Specifications
(New_Body
, New_List
(Asp
));
490 Def_Id
:= Defining_Entity
(N
);
491 Set_Is_Inlined
(Def_Id
);
493 Typ
:= Etype
(Def_Id
);
495 -- Establish the linkages between the spec and the body. These are
496 -- used when the expression function acts as the prefix of attribute
497 -- 'Access in order to freeze the original expression which has been
498 -- moved to the generated body.
500 Set_Corresponding_Body
(N
, Defining_Entity
(New_Body
));
501 Set_Corresponding_Spec
(New_Body
, Def_Id
);
503 -- Within a generic preanalyze the original expression for name
504 -- capture. The body is also generated but plays no role in
505 -- this because it is not part of the original source.
506 -- If this is an ignored Ghost entity, analysis of the generated
507 -- body is needed to hide external references (as is done in
508 -- Analyze_Subprogram_Body) after which the the subprogram profile
509 -- can be frozen, which is needed to expand calls to such an ignored
512 if Inside_A_Generic
then
513 Set_Has_Completion
(Def_Id
, not Is_Ignored_Ghost_Entity
(Def_Id
));
515 Install_Formals
(Def_Id
);
516 Preanalyze_Spec_Expression
(Expr
, Typ
);
520 Install_Formals
(Def_Id
);
521 Preanalyze_Formal_Expression
(Expr
, Typ
);
522 Check_Limited_Return
(Orig_N
, Expr
, Typ
);
526 -- If this is a wrapper created in an instance for a formal
527 -- subprogram, insert body after declaration, to be analyzed when the
528 -- enclosing instance is analyzed.
531 and then Is_Generic_Actual_Subprogram
(Def_Id
)
533 Insert_After
(N
, New_Body
);
535 -- To prevent premature freeze action, insert the new body at the end
536 -- of the current declarations, or at the end of the package spec.
537 -- However, resolve usage names now, to prevent spurious visibility
538 -- on later entities. Note that the function can now be called in
539 -- the current declarative part, which will appear to be prior to the
540 -- presence of the body in the code. There are nevertheless no order
541 -- of elaboration issues because all name resolution has taken place
542 -- at the point of declaration.
546 Decls
: List_Id
:= List_Containing
(N
);
547 Par
: constant Node_Id
:= Parent
(Decls
);
550 if Nkind
(Par
) = N_Package_Specification
551 and then Decls
= Visible_Declarations
(Par
)
552 and then not Is_Empty_List
(Private_Declarations
(Par
))
554 Decls
:= Private_Declarations
(Par
);
557 Insert_After
(Last
(Decls
), New_Body
);
561 -- In the case of an expression function marked with the aspect
562 -- Static, we need to check the requirement that the function's
563 -- expression is a potentially static expression. This is done
564 -- by making a full copy of the expression tree and performing
565 -- a special preanalysis on that tree with the global flag
566 -- Checking_Potentially_Static_Expression enabled. If the
567 -- resulting expression is static, then it's OK, but if not, that
568 -- means the expression violates the requirements of the Ada 2022
569 -- RM in 4.9(3.2/5-3.4/5) and we flag an error.
571 if Is_Static_Function
(Def_Id
) then
573 -- If a potentially static expr like "Parameter / 0"
574 -- is transformed into "(raise Constraint_Error)", then we
575 -- need to copy the Original_Node.
576 function Make_Expr_Copy
return Node_Id
is
577 (New_Copy_Tree
(if Expr
in N_Raise_xxx_Error_Id
578 then Original_Node
(Expr
)
581 if not Is_Static_Expression
(Expr
) then
583 Exp_Copy
: constant Node_Id
:= Make_Expr_Copy
;
585 Set_Checking_Potentially_Static_Expression
(True);
587 Preanalyze_Formal_Expression
(Exp_Copy
, Typ
);
589 if not Is_Static_Expression
(Exp_Copy
) then
591 ("static expression function requires "
592 & "potentially static expression", Expr
);
595 Set_Checking_Potentially_Static_Expression
(False);
599 -- We also make an additional copy of the expression and
600 -- replace the expression of the expression function with
601 -- this copy, because the currently present expression is
602 -- now associated with the body created for the static
603 -- expression function, which will later be analyzed and
604 -- possibly rewritten, and we need to have the separate
605 -- unanalyzed copy available for use with later static
609 (Original_Node
(Subprogram_Spec
(Def_Id
)),
612 -- Mark static expression functions as inlined, to ensure
613 -- that even calls with nonstatic actuals will be inlined.
615 Set_Has_Pragma_Inline
(Def_Id
);
616 Set_Is_Inlined
(Def_Id
);
621 -- Check incorrect use of dynamically tagged expression. This doesn't
622 -- fall out automatically when analyzing the generated function body,
623 -- because Check_Dynamically_Tagged_Expression deliberately ignores
624 -- nodes that don't come from source.
627 and then Is_Tagged_Type
(Typ
)
629 Check_Dynamically_Tagged_Expression
632 Related_Nod
=> Orig_N
);
635 -- We must enforce checks for unreferenced formals in our newly
636 -- generated function, so we propagate the referenced flag from the
637 -- original spec to the new spec as well as setting Comes_From_Source.
639 if Present
(Parameter_Specifications
(New_Spec
)) then
641 Form_New_Def
: Entity_Id
;
642 Form_New_Spec
: Node_Id
;
643 Form_Old_Def
: Entity_Id
;
644 Form_Old_Spec
: Node_Id
;
647 Form_New_Spec
:= First
(Parameter_Specifications
(New_Spec
));
648 Form_Old_Spec
:= First
(Parameter_Specifications
(Spec
));
650 while Present
(Form_New_Spec
) and then Present
(Form_Old_Spec
) loop
651 Form_New_Def
:= Defining_Identifier
(Form_New_Spec
);
652 Form_Old_Def
:= Defining_Identifier
(Form_Old_Spec
);
654 Set_Comes_From_Source
(Form_New_Def
, True);
656 -- Because of the usefulness of unreferenced controlling
657 -- formals we exempt them from unreferenced warnings by marking
658 -- them as always referenced.
660 Set_Referenced
(Form_Old_Def
,
661 (Is_Formal
(Form_Old_Def
)
662 and then Is_Controlling_Formal
(Form_Old_Def
))
663 or else Referenced
(Form_Old_Def
));
665 Next
(Form_New_Spec
);
666 Next
(Form_Old_Spec
);
670 end Analyze_Expression_Function
;
672 ---------------------------------------
673 -- Analyze_Extended_Return_Statement --
674 ---------------------------------------
676 procedure Analyze_Extended_Return_Statement
(N
: Node_Id
) is
678 Analyze_Return_Statement
(N
);
679 end Analyze_Extended_Return_Statement
;
681 ----------------------------
682 -- Analyze_Function_Call --
683 ----------------------------
685 procedure Analyze_Function_Call
(N
: Node_Id
) is
686 Actuals
: constant List_Id
:= Parameter_Associations
(N
);
687 Func_Nam
: constant Node_Id
:= Name
(N
);
693 -- A call of the form A.B (X) may be an Ada 2005 call, which is
694 -- rewritten as B (A, X). If the rewriting is successful, the call
695 -- has been analyzed and we just return.
697 if Nkind
(Func_Nam
) = N_Selected_Component
698 and then Name
(N
) /= Func_Nam
699 and then Is_Rewrite_Substitution
(N
)
700 and then Present
(Etype
(N
))
705 -- If error analyzing name, then set Any_Type as result type and return
707 if Etype
(Func_Nam
) = Any_Type
then
708 Set_Etype
(N
, Any_Type
);
712 -- Otherwise analyze the parameters
714 Actual
:= First
(Actuals
);
715 while Present
(Actual
) loop
717 Check_Parameterless_Call
(Actual
);
722 end Analyze_Function_Call
;
724 -----------------------------
725 -- Analyze_Function_Return --
726 -----------------------------
728 procedure Analyze_Function_Return
(N
: Node_Id
) is
729 Loc
: constant Source_Ptr
:= Sloc
(N
);
730 Stm_Entity
: constant Entity_Id
:= Return_Statement_Entity
(N
);
731 Scope_Id
: constant Entity_Id
:= Return_Applies_To
(Stm_Entity
);
733 R_Type
: constant Entity_Id
:= Etype
(Scope_Id
);
734 -- Function result subtype
736 procedure Check_No_Return_Expression
(Return_Expr
: Node_Id
);
737 -- Ada 2022: Check that the return expression in a No_Return function
738 -- meets the conditions specified by RM 6.5.1(5.1/5).
740 procedure Check_Return_Construct_Accessibility
(Return_Stmt
: Node_Id
);
741 -- Apply legality rule of 6.5 (5.9) to the access discriminants of an
742 -- aggregate in a return statement.
744 procedure Check_Return_Subtype_Indication
(Obj_Decl
: Node_Id
);
745 -- Check that the return_subtype_indication properly matches the result
746 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
748 --------------------------------
749 -- Check_No_Return_Expression --
750 --------------------------------
752 procedure Check_No_Return_Expression
(Return_Expr
: Node_Id
) is
753 Kind
: constant Node_Kind
:= Nkind
(Return_Expr
);
756 if Kind
= N_Raise_Expression
then
759 elsif Kind
= N_Function_Call
760 and then Is_Entity_Name
(Name
(Return_Expr
))
761 and then Ekind
(Entity
(Name
(Return_Expr
))) in
762 E_Function | E_Generic_Function
763 and then No_Return
(Entity
(Name
(Return_Expr
)))
769 ("illegal expression in RETURN statement of No_Return function",
772 ("\must be raise expression or call to No_Return (RM 6.5.1(5.1/5))",
774 end Check_No_Return_Expression
;
776 ------------------------------------------
777 -- Check_Return_Construct_Accessibility --
778 ------------------------------------------
780 procedure Check_Return_Construct_Accessibility
(Return_Stmt
: Node_Id
) is
782 function First_Selector
(Assoc
: Node_Id
) return Node_Id
;
783 -- Obtain the first selector or choice from a given association
785 function Is_Formal_Of_Current_Function
786 (Assoc_Expr
: Entity_Id
) return Boolean;
787 -- Predicate to test if a given expression associated with a
788 -- discriminant is a formal parameter to the function in which the
789 -- return construct we checking applies to.
795 function First_Selector
(Assoc
: Node_Id
) return Node_Id
is
797 if Nkind
(Assoc
) = N_Component_Association
then
798 return First
(Choices
(Assoc
));
800 elsif Nkind
(Assoc
) = N_Discriminant_Association
then
801 return (First
(Selector_Names
(Assoc
)));
808 -----------------------------------
809 -- Is_Formal_Of_Current_Function --
810 -----------------------------------
812 function Is_Formal_Of_Current_Function
813 (Assoc_Expr
: Entity_Id
) return Boolean is
815 return Is_Entity_Name
(Assoc_Expr
)
816 and then Enclosing_Subprogram
817 (Entity
(Assoc_Expr
)) = Scope_Id
818 and then Is_Formal
(Entity
(Assoc_Expr
));
819 end Is_Formal_Of_Current_Function
;
821 -- Local declarations
823 Assoc
: Node_Id
:= Empty
;
824 -- Assoc should perhaps be renamed and declared as a
825 -- Node_Or_Entity_Id since it encompasses not only component and
826 -- discriminant associations, but also discriminant components within
827 -- a type declaration or subtype indication ???
829 Assoc_Expr
: Node_Id
;
830 Assoc_Present
: Boolean := False;
832 Check_Cond
: Node_Id
;
833 Unseen_Disc_Count
: Nat
:= 0;
834 Seen_Discs
: Elist_Id
;
836 First_Disc
: Entity_Id
;
839 Return_Con
: Node_Id
;
842 -- Start of processing for Check_Return_Construct_Accessibility
845 -- Only perform checks on record types with access discriminants and
846 -- non-internally generated functions.
848 if not Is_Record_Type
(R_Type
)
849 or else not Has_Anonymous_Access_Discriminant
(R_Type
)
850 or else not Comes_From_Source
(Return_Stmt
)
855 -- We are only interested in return statements
857 if Nkind
(Return_Stmt
) not in
858 N_Extended_Return_Statement | N_Simple_Return_Statement
863 -- Fetch the object from the return statement, in the case of a
864 -- simple return statement the expression is part of the node.
866 if Nkind
(Return_Stmt
) = N_Extended_Return_Statement
then
867 -- Obtain the object definition from the expanded extended return
869 Return_Con
:= First
(Return_Object_Declarations
(Return_Stmt
));
870 while Present
(Return_Con
) loop
871 -- Inspect the original node to avoid object declarations
872 -- expanded into renamings.
874 if Nkind
(Original_Node
(Return_Con
)) = N_Object_Declaration
875 and then Comes_From_Source
(Original_Node
(Return_Con
))
880 Nlists
.Next
(Return_Con
);
883 pragma Assert
(Present
(Return_Con
));
885 -- Could be dealing with a renaming
887 Return_Con
:= Original_Node
(Return_Con
);
889 Return_Con
:= Expression
(Return_Stmt
);
892 -- Obtain the accessibility levels of the expressions associated
893 -- with all anonymous access discriminants, then generate a
894 -- dynamic check or static error when relevant.
896 -- Note the repeated use of Original_Node to avoid checking
899 Unqual
:= Original_Node
(Unqualify
(Original_Node
(Return_Con
)));
901 -- Get the corresponding declaration based on the return object's
904 if Nkind
(Unqual
) = N_Identifier
905 and then Nkind
(Parent
(Entity
(Unqual
)))
906 in N_Object_Declaration
907 | N_Object_Renaming_Declaration
909 Obj_Decl
:= Original_Node
(Parent
(Entity
(Unqual
)));
911 -- We were passed the object declaration directly, so use it
913 elsif Nkind
(Unqual
) in N_Object_Declaration
914 | N_Object_Renaming_Declaration
918 -- Otherwise, we are looking at something else
925 -- Hop up object renamings when present
927 if Present
(Obj_Decl
)
928 and then Nkind
(Obj_Decl
) = N_Object_Renaming_Declaration
930 while Nkind
(Obj_Decl
) = N_Object_Renaming_Declaration
loop
932 if Nkind
(Name
(Obj_Decl
)) not in N_Entity
then
933 -- We may be looking at the expansion of iterators or
934 -- some other internally generated construct, so it is safe
935 -- to ignore checks ???
937 if not Comes_From_Source
(Obj_Decl
) then
941 Obj_Decl
:= Original_Node
943 (Ultimate_Prefix
(Name
(Obj_Decl
))));
945 -- Move up to the next declaration based on the object's name
948 Obj_Decl
:= Original_Node
949 (Declaration_Node
(Name
(Obj_Decl
)));
954 -- Obtain the discriminant values from the return aggregate
956 -- Do we cover extension aggregates correctly ???
958 if Nkind
(Unqual
) = N_Aggregate
then
959 if Present
(Expressions
(Unqual
)) then
960 Assoc
:= First
(Expressions
(Unqual
));
962 Assoc
:= First
(Component_Associations
(Unqual
));
965 -- There is an object declaration for the return object
967 elsif Present
(Obj_Decl
) then
968 -- When a subtype indication is present in an object declaration
969 -- it must contain the object's discriminants.
971 if Nkind
(Object_Definition
(Obj_Decl
)) = N_Subtype_Indication
then
975 (Object_Definition
(Obj_Decl
))));
977 -- The object declaration contains an aggregate
979 elsif Present
(Expression
(Obj_Decl
)) then
981 if Nkind
(Unqualify
(Expression
(Obj_Decl
))) = N_Aggregate
then
982 -- Grab the first associated discriminant expresion
985 (Expressions
(Unqualify
(Expression
(Obj_Decl
))))
989 (Unqualify
(Expression
(Obj_Decl
))));
992 (Component_Associations
993 (Unqualify
(Expression
(Obj_Decl
))));
996 -- Otherwise, this is something else
1002 -- There are no supplied discriminants in the object declaration,
1003 -- so get them from the type definition since they must be default
1006 -- Do we handle constrained subtypes correctly ???
1008 elsif Nkind
(Unqual
) = N_Object_Declaration
then
1009 Assoc
:= First_Discriminant
1010 (Etype
(Object_Definition
(Obj_Decl
)));
1013 Assoc
:= First_Discriminant
(Etype
(Unqual
));
1016 -- When we are not looking at an aggregate or an identifier, return
1017 -- since any other construct (like a function call) is not
1018 -- applicable since checks will be performed on the side of the
1025 -- Obtain the discriminants so we know the actual type in case the
1026 -- value of their associated expression gets implicitly converted.
1028 if No
(Obj_Decl
) then
1029 pragma Assert
(Nkind
(Unqual
) = N_Aggregate
);
1031 Disc
:= First_Discriminant
(Etype
(Unqual
));
1034 Disc
:= First_Discriminant
1035 (Etype
(Defining_Identifier
(Obj_Decl
)));
1038 -- Preserve the first discriminant for checking named associations
1042 -- Count the number of discriminants for processing an aggregate
1043 -- which includes an others.
1046 while Present
(Disc
) loop
1047 Unseen_Disc_Count
:= Unseen_Disc_Count
+ 1;
1049 Next_Discriminant
(Disc
);
1052 Seen_Discs
:= New_Elmt_List
;
1054 -- Loop through each of the discriminants and check each expression
1055 -- associated with an anonymous access discriminant.
1057 -- When named associations occur in the return aggregate then
1058 -- discriminants can be in any order, so we need to ensure we do
1059 -- not continue to loop when all discriminants have been seen.
1062 while Present
(Assoc
)
1063 and then (Present
(Disc
) or else Assoc_Present
)
1064 and then Unseen_Disc_Count
> 0
1066 -- Handle named associations by searching through the names of
1067 -- the relevant discriminant components.
1070 in N_Component_Association | N_Discriminant_Association
1072 Assoc_Expr
:= Expression
(Assoc
);
1073 Assoc_Present
:= True;
1075 -- We currently don't handle box initialized discriminants,
1076 -- however, since default initialized anonymous access
1077 -- discriminants are a corner case, this is ok for now ???
1079 if Nkind
(Assoc
) = N_Component_Association
1080 and then Box_Present
(Assoc
)
1082 if Nkind
(First_Selector
(Assoc
)) = N_Others_Choice
then
1083 Unseen_Disc_Count
:= 0;
1086 -- When others is present we must identify a discriminant we
1087 -- haven't already seen so as to get the appropriate type for
1088 -- the static accessibility check.
1090 -- This works because all components within an others clause
1091 -- must have the same type.
1093 elsif Nkind
(First_Selector
(Assoc
)) = N_Others_Choice
then
1096 Outer
: while Present
(Disc
) loop
1098 Current_Seen_Disc
: Elmt_Id
;
1100 -- Move through the list of identified discriminants
1102 Current_Seen_Disc
:= First_Elmt
(Seen_Discs
);
1103 while Present
(Current_Seen_Disc
) loop
1104 -- Exit the loop when we found a match
1107 Chars
(Node
(Current_Seen_Disc
)) = Chars
(Disc
);
1109 Next_Elmt
(Current_Seen_Disc
);
1112 -- When we have exited the above loop without finding
1113 -- a match then we know that Disc has not been seen.
1115 exit Outer
when No
(Current_Seen_Disc
);
1118 Next_Discriminant
(Disc
);
1121 -- If we got to an others clause with a non-zero
1122 -- discriminant count there must be a discriminant left to
1125 pragma Assert
(Present
(Disc
));
1127 -- Set the unseen discriminant count to zero because we know
1128 -- an others clause sets all remaining components of an
1131 Unseen_Disc_Count
:= 0;
1133 -- Move through each of the selectors in the named association
1134 -- and obtain a discriminant for accessibility checking if one
1135 -- is referenced in the list. Also track which discriminants
1136 -- are referenced for the purpose of handling an others clause.
1140 Assoc_Choice
: Node_Id
;
1141 Curr_Disc
: Node_Id
;
1145 Curr_Disc
:= First_Disc
;
1146 while Present
(Curr_Disc
) loop
1147 -- Check each of the choices in the associations for a
1148 -- match to the name of the current discriminant.
1150 Assoc_Choice
:= First_Selector
(Assoc
);
1151 while Present
(Assoc_Choice
) loop
1152 -- When the name matches we track that we have seen
1153 -- the discriminant, but instead of exiting the
1154 -- loop we continue iterating to make sure all the
1155 -- discriminants within the named association get
1158 if Chars
(Assoc_Choice
) = Chars
(Curr_Disc
) then
1159 Append_Elmt
(Curr_Disc
, Seen_Discs
);
1162 Unseen_Disc_Count
:= Unseen_Disc_Count
- 1;
1165 Next
(Assoc_Choice
);
1168 Next_Discriminant
(Curr_Disc
);
1173 -- Unwrap the associated expression if we are looking at a default
1174 -- initialized type declaration. In this case Assoc is not really
1175 -- an association, but a component declaration. Should Assoc be
1176 -- renamed in some way to be more clear ???
1178 -- This occurs when the return object does not initialize
1179 -- discriminant and instead relies on the type declaration for
1180 -- their supplied values.
1182 elsif Nkind
(Assoc
) in N_Entity
1183 and then Ekind
(Assoc
) = E_Discriminant
1185 Append_Elmt
(Disc
, Seen_Discs
);
1187 Assoc_Expr
:= Discriminant_Default_Value
(Assoc
);
1188 Unseen_Disc_Count
:= Unseen_Disc_Count
- 1;
1190 -- Otherwise, there is nothing to do because Assoc is an
1191 -- expression within the return aggregate itself.
1194 Append_Elmt
(Disc
, Seen_Discs
);
1196 Assoc_Expr
:= Assoc
;
1197 Unseen_Disc_Count
:= Unseen_Disc_Count
- 1;
1200 -- Check the accessibility level of the expression when the
1201 -- discriminant is of an anonymous access type.
1203 if Present
(Assoc_Expr
)
1204 and then Present
(Disc
)
1205 and then Ekind
(Etype
(Disc
)) = E_Anonymous_Access_Type
1207 -- We disable the check when we have a tagged return type and
1208 -- the associated expression for the discriminant is a formal
1209 -- parameter since the check would require us to compare the
1210 -- accessibility level of Assoc_Expr to the level of the
1211 -- Extra_Accessibility_Of_Result of the function - which is
1212 -- currently disabled for functions with tagged return types.
1213 -- This may change in the future ???
1215 -- See Needs_Result_Accessibility_Level for details.
1218 (No
(Extra_Accessibility_Of_Result
(Scope_Id
))
1219 and then Is_Formal_Of_Current_Function
(Assoc_Expr
)
1220 and then Is_Tagged_Type
(Etype
(Scope_Id
)))
1222 -- Generate a dynamic check based on the extra accessibility of
1223 -- the result or the scope of the current function.
1227 Left_Opnd
=> Accessibility_Level
1228 (Expr
=> Assoc_Expr
,
1229 Level
=> Dynamic_Level
,
1230 In_Return_Context
=> True),
1232 (if Present
(Extra_Accessibility_Of_Result
(Scope_Id
))
1234 -- When Assoc_Expr is a formal we have to look at the
1235 -- extra accessibility-level formal associated with
1238 and then Is_Formal_Of_Current_Function
(Assoc_Expr
)
1241 (Extra_Accessibility_Of_Result
(Scope_Id
), Loc
)
1243 -- Otherwise, we compare the level of Assoc_Expr to the
1244 -- scope of the current function.
1247 Make_Integer_Literal
1248 (Loc
, Scope_Depth
(Scope
(Scope_Id
)))));
1250 Insert_Before_And_Analyze
(Return_Stmt
,
1251 Make_Raise_Program_Error
(Loc
,
1252 Condition
=> Check_Cond
,
1253 Reason
=> PE_Accessibility_Check_Failed
));
1255 -- If constant folding has happened on the condition for the
1256 -- generated error, then warn about it being unconditional when
1257 -- we know an error will be raised.
1259 if Nkind
(Check_Cond
) = N_Identifier
1260 and then Entity
(Check_Cond
) = Standard_True
1263 ("access discriminant in return object would be a dangling"
1264 & " reference", Return_Stmt
);
1268 -- Iterate over the discriminants, except when we have encountered
1269 -- a named association since the discriminant order becomes
1270 -- irrelevant in that case.
1272 if not Assoc_Present
then
1273 Next_Discriminant
(Disc
);
1276 -- Iterate over associations
1278 if not Is_List_Member
(Assoc
) then
1281 Nlists
.Next
(Assoc
);
1284 end Check_Return_Construct_Accessibility
;
1286 -------------------------------------
1287 -- Check_Return_Subtype_Indication --
1288 -------------------------------------
1290 procedure Check_Return_Subtype_Indication
(Obj_Decl
: Node_Id
) is
1291 Return_Obj
: constant Node_Id
:= Defining_Identifier
(Obj_Decl
);
1293 R_Stm_Type
: constant Entity_Id
:= Etype
(Return_Obj
);
1294 -- Subtype given in the extended return statement (must match R_Type)
1296 Subtype_Ind
: constant Node_Id
:=
1297 Object_Definition
(Original_Node
(Obj_Decl
));
1299 procedure Error_No_Match
(N
: Node_Id
);
1300 -- Output error messages for case where types do not statically
1301 -- match. N is the location for the messages.
1303 --------------------
1304 -- Error_No_Match --
1305 --------------------
1307 procedure Error_No_Match
(N
: Node_Id
) is
1310 ("subtype must statically match function result subtype", N
);
1312 if not Predicates_Match
(R_Stm_Type
, R_Type
) then
1313 Error_Msg_Node_2
:= R_Type
;
1315 ("\predicate of& does not match predicate of&",
1320 -- Start of processing for Check_Return_Subtype_Indication
1323 -- First, avoid cascaded errors
1325 if Error_Posted
(Obj_Decl
) or else Error_Posted
(Subtype_Ind
) then
1329 -- "return access T" case; check that the return statement also has
1330 -- "access T", and that the subtypes statically match:
1331 -- if this is an access to subprogram the signatures must match.
1333 if Is_Anonymous_Access_Type
(R_Type
) then
1334 if Is_Anonymous_Access_Type
(R_Stm_Type
) then
1335 if Ekind
(Designated_Type
(R_Stm_Type
)) /= E_Subprogram_Type
1337 if Base_Type
(Designated_Type
(R_Stm_Type
)) /=
1338 Base_Type
(Designated_Type
(R_Type
))
1339 or else not Subtypes_Statically_Match
(R_Stm_Type
, R_Type
)
1341 Error_No_Match
(Subtype_Mark
(Subtype_Ind
));
1345 -- For two anonymous access to subprogram types, the types
1346 -- themselves must be type conformant.
1348 if not Conforming_Types
1349 (R_Stm_Type
, R_Type
, Fully_Conformant
)
1351 Error_No_Match
(Subtype_Ind
);
1356 Error_Msg_N
("must use anonymous access type", Subtype_Ind
);
1359 -- If the return object is of an anonymous access type, then report
1360 -- an error if the function's result type is not also anonymous.
1362 elsif Is_Anonymous_Access_Type
(R_Stm_Type
) then
1363 pragma Assert
(not Is_Anonymous_Access_Type
(R_Type
));
1365 ("anonymous access not allowed for function with named access "
1366 & "result", Subtype_Ind
);
1368 -- Subtype indication case: check that the return object's type is
1369 -- covered by the result type, and that the subtypes statically match
1370 -- when the result subtype is constrained. Also handle record types
1371 -- with unknown discriminants for which we have built the underlying
1372 -- record view. Coverage is needed to allow specific-type return
1373 -- objects when the result type is class-wide (see AI05-32).
1375 elsif Covers
(Base_Type
(R_Type
), Base_Type
(R_Stm_Type
))
1376 or else (Is_Underlying_Record_View
(Base_Type
(R_Stm_Type
))
1379 (Base_Type
(R_Type
),
1380 Underlying_Record_View
(Base_Type
(R_Stm_Type
))))
1382 -- A null exclusion may be present on the return type, on the
1383 -- function specification, on the object declaration or on the
1386 if Is_Access_Type
(R_Type
)
1388 (Can_Never_Be_Null
(R_Type
)
1389 or else Null_Exclusion_Present
(Parent
(Scope_Id
))) /=
1390 Can_Never_Be_Null
(R_Stm_Type
)
1392 Error_No_Match
(Subtype_Ind
);
1395 -- AI05-103: for elementary types, subtypes must statically match
1397 if Is_Constrained
(R_Type
) or else Is_Access_Type
(R_Type
) then
1398 if not Subtypes_Statically_Match
(R_Stm_Type
, R_Type
) then
1399 Error_No_Match
(Subtype_Ind
);
1403 -- All remaining cases are illegal
1405 -- Note: previous versions of this subprogram allowed the return
1406 -- value to be the ancestor of the return type if the return type
1407 -- was a null extension. This was plainly incorrect.
1411 ("wrong type for return_subtype_indication", Subtype_Ind
);
1413 end Check_Return_Subtype_Indication
;
1415 ---------------------
1416 -- Local Variables --
1417 ---------------------
1420 Obj_Decl
: Node_Id
:= Empty
;
1422 -- Start of processing for Analyze_Function_Return
1425 Set_Return_Present
(Scope_Id
);
1427 if Nkind
(N
) = N_Simple_Return_Statement
then
1428 Expr
:= Expression
(N
);
1430 -- Guard against a malformed expression. The parser may have tried to
1431 -- recover but the node is not analyzable.
1433 if Nkind
(Expr
) = N_Error
then
1434 Set_Etype
(Expr
, Any_Type
);
1435 Expander_Mode_Save_And_Set
(False);
1439 -- The resolution of a controlled [extension] aggregate associated
1440 -- with a return statement creates a temporary which needs to be
1441 -- finalized on function exit. Wrap the return statement inside a
1442 -- block so that the finalization machinery can detect this case.
1443 -- This early expansion is done only when the return statement is
1444 -- not part of a handled sequence of statements.
1446 if Nkind
(Expr
) in N_Aggregate | N_Extension_Aggregate
1447 and then Needs_Finalization
(R_Type
)
1448 and then Nkind
(Parent
(N
)) /= N_Handled_Sequence_Of_Statements
1451 Make_Block_Statement
(Loc
,
1452 Handled_Statement_Sequence
=>
1453 Make_Handled_Sequence_Of_Statements
(Loc
,
1454 Statements
=> New_List
(Relocate_Node
(N
)))));
1462 -- Ada 2005 (AI-251): If the type of the returned object is
1463 -- an access to an interface type then we add an implicit type
1464 -- conversion to force the displacement of the "this" pointer to
1465 -- reference the secondary dispatch table. We cannot delay the
1466 -- generation of this implicit conversion until the expansion
1467 -- because in this case the type resolution changes the decoration
1468 -- of the expression node to match R_Type; by contrast, if the
1469 -- returned object is a class-wide interface type then it is too
1470 -- early to generate here the implicit conversion since the return
1471 -- statement may be rewritten by the expander into an extended
1472 -- return statement whose expansion takes care of adding the
1473 -- implicit type conversion to displace the pointer to the object.
1476 and then Serious_Errors_Detected
= 0
1477 and then Is_Access_Type
(R_Type
)
1478 and then Nkind
(Expr
) not in N_Null | N_Raise_Expression
1479 and then Is_Interface
(Designated_Type
(R_Type
))
1480 and then Is_Progenitor
(Designated_Type
(R_Type
),
1481 Designated_Type
(Etype
(Expr
)))
1483 Rewrite
(Expr
, Convert_To
(R_Type
, Relocate_Node
(Expr
)));
1487 Resolve
(Expr
, R_Type
);
1488 Check_Limited_Return
(N
, Expr
, R_Type
);
1490 Check_Return_Construct_Accessibility
(N
);
1492 -- Ada 2022 (AI12-0269): Any return statement that applies to a
1493 -- nonreturning function shall be a simple_return_statement with
1494 -- an expression that is a raise_expression, or else a call on a
1495 -- nonreturning function, or else a parenthesized expression of
1498 if Ada_Version
>= Ada_2022
1499 and then No_Return
(Scope_Id
)
1500 and then Comes_From_Source
(N
)
1502 Check_No_Return_Expression
(Original_Node
(Expr
));
1506 Obj_Decl
:= Last
(Return_Object_Declarations
(N
));
1508 -- Analyze parts specific to extended_return_statement:
1511 Has_Aliased
: constant Boolean := Aliased_Present
(Obj_Decl
);
1512 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
1515 Expr
:= Expression
(Obj_Decl
);
1517 -- Note: The check for OK_For_Limited_Init will happen in
1518 -- Analyze_Object_Declaration; we treat it as a normal
1519 -- object declaration.
1521 Set_Is_Return_Object
(Defining_Identifier
(Obj_Decl
));
1524 Check_Return_Subtype_Indication
(Obj_Decl
);
1526 if Present
(HSS
) then
1529 if Present
(Exception_Handlers
(HSS
)) then
1531 -- ???Has_Nested_Block_With_Handler needs to be set.
1532 -- Probably by creating an actual N_Block_Statement.
1533 -- Probably in Expand.
1539 -- Mark the return object as referenced, since the return is an
1540 -- implicit reference of the object.
1542 Set_Referenced
(Defining_Identifier
(Obj_Decl
));
1544 Check_References
(Stm_Entity
);
1546 Check_Return_Construct_Accessibility
(N
);
1548 -- Check RM 6.5 (5.9/3)
1550 if Has_Aliased
and then not Is_Immutably_Limited_Type
(R_Type
) then
1551 if Ada_Version
< Ada_2012
1552 and then Warn_On_Ada_2012_Compatibility
1555 ("ALIASED only allowed for immutably limited return " &
1556 "objects in Ada 2012?y?", N
);
1560 ("ALIASED only allowed for immutably limited return " &
1565 -- Ada 2022 (AI12-0269): Any return statement that applies to a
1566 -- nonreturning function shall be a simple_return_statement.
1568 if Ada_Version
>= Ada_2022
1569 and then No_Return
(Scope_Id
)
1570 and then Comes_From_Source
(N
)
1573 ("extended RETURN statement not allowed in No_Return "
1579 -- Case of Expr present
1581 if Present
(Expr
) then
1583 -- Defend against previous errors
1585 if Nkind
(Expr
) = N_Empty
1586 or else No
(Etype
(Expr
))
1591 -- Apply constraint check. Note that this is done before the implicit
1592 -- conversion of the expression done for anonymous access types to
1593 -- ensure correct generation of the null-excluding check associated
1594 -- with null-excluding expressions found in return statements. We
1595 -- don't need a check if the subtype of the return object is the
1596 -- same as the result subtype of the function.
1598 if Nkind
(N
) /= N_Extended_Return_Statement
1599 or else Nkind
(Obj_Decl
) /= N_Object_Declaration
1600 or else Nkind
(Object_Definition
(Obj_Decl
)) not in N_Has_Entity
1601 or else Entity
(Object_Definition
(Obj_Decl
)) /= R_Type
1603 Apply_Constraint_Check
(Expr
, R_Type
);
1606 -- The return value is converted to the return type of the function,
1607 -- which implies a predicate check if the return type is predicated.
1608 -- We do not apply the check for an extended return statement because
1609 -- Analyze_Object_Declaration has already done it on Obj_Decl above.
1610 -- We do not apply the check to a case expression because it will
1611 -- be expanded into a series of return statements, each of which
1612 -- will receive a predicate check.
1614 if Nkind
(N
) /= N_Extended_Return_Statement
1615 and then Nkind
(Expr
) /= N_Case_Expression
1617 Apply_Predicate_Check
(Expr
, R_Type
);
1620 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1621 -- type, apply an implicit conversion of the expression to that type
1622 -- to force appropriate static and run-time accessibility checks.
1623 -- But we want to apply the checks to an extended return statement
1624 -- only once, i.e. not to the simple return statement generated at
1625 -- the end of its expansion because, prior to leaving the function,
1626 -- the accessibility level of the return object changes to be a level
1627 -- determined by the point of call (RM 3.10.2(10.8/3)).
1629 if Ada_Version
>= Ada_2005
1630 and then Ekind
(R_Type
) = E_Anonymous_Access_Type
1631 and then (Nkind
(N
) = N_Extended_Return_Statement
1632 or else not Comes_From_Extended_Return_Statement
(N
))
1634 Rewrite
(Expr
, Convert_To
(R_Type
, Relocate_Node
(Expr
)));
1635 Analyze_And_Resolve
(Expr
, R_Type
);
1637 -- If this is a local anonymous access to subprogram, the
1638 -- accessibility check can be applied statically. The return is
1639 -- illegal if the access type of the return expression is declared
1640 -- inside of the subprogram (except if it is the subtype indication
1641 -- of an extended return statement).
1643 elsif Ekind
(R_Type
) = E_Anonymous_Access_Subprogram_Type
then
1644 if not Comes_From_Source
(Current_Scope
)
1645 or else Ekind
(Current_Scope
) = E_Return_Statement
1650 Scope_Depth
(Scope
(Etype
(Expr
))) >= Scope_Depth
(Scope_Id
)
1652 Error_Msg_N
("cannot return local access to subprogram", N
);
1655 -- The expression cannot be of a formal incomplete type
1657 elsif Ekind
(Etype
(Expr
)) = E_Incomplete_Type
1658 and then Is_Generic_Type
(Etype
(Expr
))
1661 ("cannot return expression of a formal incomplete type", N
);
1664 -- If the result type is class-wide, then check that the return
1665 -- expression's type is not declared at a deeper level than the
1666 -- function (RM05-6.5(5.6/2)).
1668 if Ada_Version
>= Ada_2005
1669 and then Is_Class_Wide_Type
(R_Type
)
1671 if Type_Access_Level
(Etype
(Expr
)) >
1672 Subprogram_Access_Level
(Scope_Id
)
1675 ("level of return expression type is deeper than "
1676 & "class-wide function!", Expr
);
1680 -- Check incorrect use of dynamically tagged expression
1682 if Is_Tagged_Type
(R_Type
) then
1683 Check_Dynamically_Tagged_Expression
1689 -- Perform static accessibility checks for cases involving
1690 -- dereferences of access parameters. Runtime accessibility checks
1691 -- get generated elsewhere.
1693 if (Ada_Version
< Ada_2005
or else Debug_Flag_Dot_L
)
1694 and then Is_Limited_View
(Etype
(Scope_Id
))
1695 and then Static_Accessibility_Level
(Expr
, Zero_On_Dynamic_Level
)
1696 > Subprogram_Access_Level
(Scope_Id
)
1698 -- Suppress the message in a generic, where the rewriting
1701 if Inside_A_Generic
then
1706 Make_Raise_Program_Error
(Loc
,
1707 Reason
=> PE_Accessibility_Check_Failed
));
1710 Error_Msg_Warn
:= SPARK_Mode
/= On
;
1711 Error_Msg_N
("cannot return a local value by reference<<", N
);
1712 Error_Msg_N
("\Program_Error [<<", N
);
1716 if Known_Null
(Expr
)
1717 and then Nkind
(Parent
(Scope_Id
)) = N_Function_Specification
1718 and then Null_Exclusion_Present
(Parent
(Scope_Id
))
1720 Apply_Compile_Time_Constraint_Error
1722 Msg
=> "(Ada 2005) null not allowed for "
1723 & "null-excluding return??",
1724 Reason
=> CE_Null_Not_Allowed
);
1727 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1728 -- has no initializing expression.
1730 elsif Ada_Version
> Ada_2005
and then Is_Class_Wide_Type
(R_Type
) then
1731 if Type_Access_Level
(Etype
(Defining_Identifier
(Obj_Decl
))) >
1732 Subprogram_Access_Level
(Scope_Id
)
1735 ("level of return expression type is deeper than "
1736 & "class-wide function!", Obj_Decl
);
1739 end Analyze_Function_Return
;
1741 -------------------------------------
1742 -- Analyze_Generic_Subprogram_Body --
1743 -------------------------------------
1745 procedure Analyze_Generic_Subprogram_Body
1749 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Id
);
1750 Kind
: constant Entity_Kind
:= Ekind
(Gen_Id
);
1751 Body_Id
: Entity_Id
;
1756 -- Copy body and disable expansion while analyzing the generic For a
1757 -- stub, do not copy the stub (which would load the proper body), this
1758 -- will be done when the proper body is analyzed.
1760 if Nkind
(N
) /= N_Subprogram_Body_Stub
then
1761 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
1764 -- Once the contents of the generic copy and the template are
1765 -- swapped, do the same for their respective aspect specifications.
1767 Exchange_Aspects
(N
, New_N
);
1769 -- Collect all contract-related source pragmas found within the
1770 -- template and attach them to the contract of the subprogram body.
1771 -- This contract is used in the capture of global references within
1774 Create_Generic_Contract
(N
);
1779 Spec
:= Specification
(N
);
1781 -- Within the body of the generic, the subprogram is callable, and
1782 -- behaves like the corresponding non-generic unit.
1784 Body_Id
:= Defining_Entity
(Spec
);
1786 if Kind
= E_Generic_Procedure
1787 and then Nkind
(Spec
) /= N_Procedure_Specification
1789 Error_Msg_N
("invalid body for generic procedure", Body_Id
);
1792 elsif Kind
= E_Generic_Function
1793 and then Nkind
(Spec
) /= N_Function_Specification
1795 Error_Msg_N
("invalid body for generic function", Body_Id
);
1799 Set_Corresponding_Body
(Gen_Decl
, Body_Id
);
1801 if Has_Completion
(Gen_Id
)
1802 and then Nkind
(Parent
(N
)) /= N_Subunit
1804 Error_Msg_N
("duplicate generic body", N
);
1807 Set_Has_Completion
(Gen_Id
);
1810 if Nkind
(N
) = N_Subprogram_Body_Stub
then
1811 Mutate_Ekind
(Defining_Entity
(Specification
(N
)), Kind
);
1813 Set_Corresponding_Spec
(N
, Gen_Id
);
1816 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
1817 Set_Cunit_Entity
(Current_Sem_Unit
, Defining_Entity
(N
));
1820 -- Make generic parameters immediately visible in the body. They are
1821 -- needed to process the formals declarations. Then make the formals
1822 -- visible in a separate step.
1824 Push_Scope
(Gen_Id
);
1828 First_Ent
: Entity_Id
;
1831 First_Ent
:= First_Entity
(Gen_Id
);
1834 while Present
(E
) and then not Is_Formal
(E
) loop
1839 Set_Use
(Generic_Formal_Declarations
(Gen_Decl
));
1841 -- Now generic formals are visible, and the specification can be
1842 -- analyzed, for subsequent conformance check.
1844 Body_Id
:= Analyze_Subprogram_Specification
(Spec
);
1846 -- Make formal parameters visible
1850 -- E is the first formal parameter, we loop through the formals
1851 -- installing them so that they will be visible.
1853 Set_First_Entity
(Gen_Id
, E
);
1854 while Present
(E
) loop
1860 -- Visible generic entity is callable within its own body
1862 Mutate_Ekind
(Gen_Id
, Ekind
(Body_Id
));
1863 Reinit_Field_To_Zero
(Body_Id
, F_Has_Out_Or_In_Out_Parameter
,
1865 (E_Function | E_Procedure |
1866 E_Generic_Function | E_Generic_Procedure
=> True,
1868 Mutate_Ekind
(Body_Id
, E_Subprogram_Body
);
1869 Set_Convention
(Body_Id
, Convention
(Gen_Id
));
1870 Set_Is_Obsolescent
(Body_Id
, Is_Obsolescent
(Gen_Id
));
1871 Set_Scope
(Body_Id
, Scope
(Gen_Id
));
1873 Check_Fully_Conformant
(Body_Id
, Gen_Id
, Body_Id
);
1875 if Nkind
(N
) = N_Subprogram_Body_Stub
then
1877 -- No body to analyze, so restore state of generic unit
1879 Mutate_Ekind
(Gen_Id
, Kind
);
1880 Mutate_Ekind
(Body_Id
, Kind
);
1882 if Present
(First_Ent
) then
1883 Set_First_Entity
(Gen_Id
, First_Ent
);
1890 -- If this is a compilation unit, it must be made visible explicitly,
1891 -- because the compilation of the declaration, unlike other library
1892 -- unit declarations, does not. If it is not a unit, the following
1893 -- is redundant but harmless.
1895 Set_Is_Immediately_Visible
(Gen_Id
);
1896 Reference_Body_Formals
(Gen_Id
, Body_Id
);
1898 if Is_Child_Unit
(Gen_Id
) then
1899 Generate_Reference
(Gen_Id
, Scope
(Gen_Id
), 'k', False);
1902 Set_Actual_Subtypes
(N
, Current_Scope
);
1904 Set_SPARK_Pragma
(Body_Id
, SPARK_Mode_Pragma
);
1905 Set_SPARK_Pragma_Inherited
(Body_Id
);
1907 -- Analyze any aspect specifications that appear on the generic
1910 if Has_Aspects
(N
) then
1911 Analyze_Aspects_On_Subprogram_Body_Or_Stub
(N
);
1914 Analyze_Declarations
(Declarations
(N
));
1917 -- Process the contract of the subprogram body after all declarations
1918 -- have been analyzed. This ensures that any contract-related pragmas
1919 -- are available through the N_Contract node of the body.
1921 Analyze_Entry_Or_Subprogram_Body_Contract
(Body_Id
);
1923 Analyze
(Handled_Statement_Sequence
(N
));
1924 Save_Global_References
(Original_Node
(N
));
1926 -- Prior to exiting the scope, include generic formals again (if any
1927 -- are present) in the set of local entities.
1929 if Present
(First_Ent
) then
1930 Set_First_Entity
(Gen_Id
, First_Ent
);
1933 Check_References
(Gen_Id
);
1936 Process_End_Label
(Handled_Statement_Sequence
(N
), 't', Current_Scope
);
1937 Update_Use_Clause_Chain
;
1938 Validate_Categorization_Dependency
(N
, Gen_Id
);
1940 Check_Subprogram_Order
(N
);
1942 -- Outside of its body, unit is generic again
1944 Reinit_Field_To_Zero
(Gen_Id
, F_Has_Nested_Subprogram
,
1945 Old_Ekind
=> (E_Function | E_Procedure
=> True, others => False));
1946 Mutate_Ekind
(Gen_Id
, Kind
);
1947 Generate_Reference
(Gen_Id
, Body_Id
, 'b', Set_Ref
=> False);
1950 Style
.Check_Identifier
(Body_Id
, Gen_Id
);
1954 end Analyze_Generic_Subprogram_Body
;
1956 ----------------------------
1957 -- Analyze_Null_Procedure --
1958 ----------------------------
1960 -- WARNING: This routine manages Ghost regions. Return statements must be
1961 -- replaced by gotos that jump to the end of the routine and restore the
1964 procedure Analyze_Null_Procedure
1966 Is_Completion
: out Boolean)
1968 Loc
: constant Source_Ptr
:= Sloc
(N
);
1969 Spec
: constant Node_Id
:= Specification
(N
);
1971 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
1972 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
1973 Saved_ISMP
: constant Boolean :=
1974 Ignore_SPARK_Mode_Pragmas_In_Instance
;
1975 -- Save the Ghost and SPARK mode-related data to restore on exit
1977 Designator
: Entity_Id
;
1979 Null_Body
: Node_Id
:= Empty
;
1980 Null_Stmt
: Node_Id
:= Null_Statement
(Spec
);
1984 Prev
:= Current_Entity_In_Scope
(Defining_Entity
(Spec
));
1986 -- A null procedure is Ghost when it is stand-alone and is subject to
1987 -- pragma Ghost, or when the corresponding spec is Ghost. Set the mode
1988 -- now, to ensure that any nodes generated during analysis and expansion
1989 -- are properly marked as Ghost.
1991 if Present
(Prev
) then
1992 Mark_And_Set_Ghost_Body
(N
, Prev
);
1995 -- Capture the profile of the null procedure before analysis, for
1996 -- expansion at the freeze point and at each point of call. The body is
1997 -- used if the procedure has preconditions, or if it is a completion. In
1998 -- the first case the body is analyzed at the freeze point, in the other
1999 -- it replaces the null procedure declaration.
2001 -- For a null procedure that comes from source, a NULL statement is
2002 -- provided by the parser, which carries the source location of the
2003 -- NULL keyword, and has Comes_From_Source set. For a null procedure
2004 -- from expansion, create one now.
2006 if No
(Null_Stmt
) then
2007 Null_Stmt
:= Make_Null_Statement
(Loc
);
2011 Make_Subprogram_Body
(Loc
,
2012 Specification
=> New_Copy_Tree
(Spec
),
2013 Declarations
=> New_List
,
2014 Handled_Statement_Sequence
=>
2015 Make_Handled_Sequence_Of_Statements
(Loc
,
2016 Statements
=> New_List
(Null_Stmt
)));
2018 -- Create new entities for body and formals
2020 Set_Defining_Unit_Name
(Specification
(Null_Body
),
2021 Make_Defining_Identifier
2022 (Sloc
(Defining_Entity
(N
)),
2023 Chars
(Defining_Entity
(N
))));
2025 Form
:= First
(Parameter_Specifications
(Specification
(Null_Body
)));
2026 while Present
(Form
) loop
2027 Set_Defining_Identifier
(Form
,
2028 Make_Defining_Identifier
2029 (Sloc
(Defining_Identifier
(Form
)),
2030 Chars
(Defining_Identifier
(Form
))));
2034 -- Determine whether the null procedure may be a completion of a generic
2035 -- subprogram, in which case we use the new null body as the completion
2036 -- and set minimal semantic information on the original declaration,
2037 -- which is rewritten as a null statement.
2039 if Present
(Prev
) and then Is_Generic_Subprogram
(Prev
) then
2040 Insert_Before
(N
, Null_Body
);
2041 Mutate_Ekind
(Defining_Entity
(N
), Ekind
(Prev
));
2043 Rewrite
(N
, Make_Null_Statement
(Loc
));
2044 Analyze_Generic_Subprogram_Body
(Null_Body
, Prev
);
2045 Is_Completion
:= True;
2047 -- Mark the newly generated subprogram body as trivial
2049 Set_Is_Trivial_Subprogram
2050 (Defining_Unit_Name
(Specification
(Null_Body
)));
2055 -- Resolve the types of the formals now, because the freeze point may
2056 -- appear in a different context, e.g. an instantiation.
2058 Form
:= First
(Parameter_Specifications
(Specification
(Null_Body
)));
2059 while Present
(Form
) loop
2060 if Nkind
(Parameter_Type
(Form
)) /= N_Access_Definition
then
2061 Find_Type
(Parameter_Type
(Form
));
2063 elsif No
(Access_To_Subprogram_Definition
2064 (Parameter_Type
(Form
)))
2066 Find_Type
(Subtype_Mark
(Parameter_Type
(Form
)));
2068 -- The case of a null procedure with a formal that is an
2069 -- access-to-subprogram type, and that is used as an actual
2070 -- in an instantiation is left to the enthusiastic reader.
2080 -- If there are previous overloadable entities with the same name, check
2081 -- whether any of them is completed by the null procedure.
2083 if Present
(Prev
) and then Is_Overloadable
(Prev
) then
2084 Designator
:= Analyze_Subprogram_Specification
(Spec
);
2085 Prev
:= Find_Corresponding_Spec
(N
);
2088 if No
(Prev
) or else not Comes_From_Source
(Prev
) then
2089 Designator
:= Analyze_Subprogram_Specification
(Spec
);
2090 Set_Has_Completion
(Designator
);
2092 -- Signal to caller that this is a procedure declaration
2094 Is_Completion
:= False;
2096 -- Null procedures are always inlined, but generic formal subprograms
2097 -- which appear as such in the internal instance of formal packages,
2098 -- need no completion and are not marked Inline.
2101 and then Nkind
(N
) /= N_Formal_Concrete_Subprogram_Declaration
2103 Set_Corresponding_Body
(N
, Defining_Entity
(Null_Body
));
2104 Set_Body_To_Inline
(N
, Null_Body
);
2105 Set_Is_Inlined
(Designator
);
2109 -- The null procedure is a completion. We unconditionally rewrite
2110 -- this as a null body (even if expansion is not active), because
2111 -- there are various error checks that are applied on this body
2112 -- when it is analyzed (e.g. correct aspect placement).
2114 if Has_Completion
(Prev
) then
2115 Error_Msg_Sloc
:= Sloc
(Prev
);
2116 Error_Msg_NE
("duplicate body for & declared#", N
, Prev
);
2119 Check_Previous_Null_Procedure
(N
, Prev
);
2121 Is_Completion
:= True;
2122 Rewrite
(N
, Null_Body
);
2127 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
2128 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
2129 end Analyze_Null_Procedure
;
2131 -----------------------------
2132 -- Analyze_Operator_Symbol --
2133 -----------------------------
2135 -- An operator symbol such as "+" or "and" may appear in context where the
2136 -- literal denotes an entity name, such as "+"(x, y) or in context when it
2137 -- is just a string, as in (conjunction = "or"). In these cases the parser
2138 -- generates this node, and the semantics does the disambiguation. Other
2139 -- such case are actuals in an instantiation, the generic unit in an
2140 -- instantiation, pragma arguments, and aspect specifications.
2142 procedure Analyze_Operator_Symbol
(N
: Node_Id
) is
2143 Par
: constant Node_Id
:= Parent
(N
);
2145 Maybe_Aspect_Spec
: Node_Id
:= Par
;
2147 if Nkind
(Maybe_Aspect_Spec
) /= N_Aspect_Specification
then
2148 -- deal with N_Aggregate nodes
2149 Maybe_Aspect_Spec
:= Parent
(Maybe_Aspect_Spec
);
2152 if (Nkind
(Par
) = N_Function_Call
and then N
= Name
(Par
))
2153 or else Nkind
(Par
) = N_Function_Instantiation
2154 or else (Nkind
(Par
) = N_Indexed_Component
and then N
= Prefix
(Par
))
2155 or else (Nkind
(Par
) = N_Pragma_Argument_Association
2156 and then not Is_Pragma_String_Literal
(Par
))
2157 or else Nkind
(Par
) = N_Subprogram_Renaming_Declaration
2158 or else (Nkind
(Par
) = N_Attribute_Reference
2159 and then Attribute_Name
(Par
) /= Name_Value
)
2160 or else (Nkind
(Maybe_Aspect_Spec
) = N_Aspect_Specification
2161 and then Get_Aspect_Id
(Maybe_Aspect_Spec
)
2163 -- Include aspects that can be specified by a
2164 -- subprogram name, which can be an operator.
2166 in Aspect_Stable_Properties
2167 | Aspect_Integer_Literal
2168 | Aspect_Real_Literal
2169 | Aspect_String_Literal
2172 Find_Direct_Name
(N
);
2175 Change_Operator_Symbol_To_String_Literal
(N
);
2178 end Analyze_Operator_Symbol
;
2180 -----------------------------------
2181 -- Analyze_Parameter_Association --
2182 -----------------------------------
2184 procedure Analyze_Parameter_Association
(N
: Node_Id
) is
2186 Analyze
(Explicit_Actual_Parameter
(N
));
2187 end Analyze_Parameter_Association
;
2189 ----------------------------
2190 -- Analyze_Procedure_Call --
2191 ----------------------------
2193 -- WARNING: This routine manages Ghost regions. Return statements must be
2194 -- replaced by gotos that jump to the end of the routine and restore the
2197 procedure Analyze_Procedure_Call
(N
: Node_Id
) is
2198 procedure Analyze_Call_And_Resolve
;
2199 -- Do Analyze and Resolve calls for procedure call. At the end, check
2200 -- for illegal order dependence.
2201 -- ??? where is the check for illegal order dependencies?
2203 ------------------------------
2204 -- Analyze_Call_And_Resolve --
2205 ------------------------------
2207 procedure Analyze_Call_And_Resolve
is
2209 if Nkind
(N
) = N_Procedure_Call_Statement
then
2211 Resolve
(N
, Standard_Void_Type
);
2215 end Analyze_Call_And_Resolve
;
2219 Actuals
: constant List_Id
:= Parameter_Associations
(N
);
2220 Loc
: constant Source_Ptr
:= Sloc
(N
);
2221 P
: constant Node_Id
:= Name
(N
);
2223 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
2224 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
2225 -- Save the Ghost-related attributes to restore on exit
2230 -- Start of processing for Analyze_Procedure_Call
2233 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
2234 -- a procedure call or an entry call. The prefix may denote an access
2235 -- to subprogram type, in which case an implicit dereference applies.
2236 -- If the prefix is an indexed component (without implicit dereference)
2237 -- then the construct denotes a call to a member of an entire family.
2238 -- If the prefix is a simple name, it may still denote a call to a
2239 -- parameterless member of an entry family. Resolution of these various
2240 -- interpretations is delicate.
2242 -- Do not analyze machine code statements to avoid rejecting them in
2245 if CodePeer_Mode
and then Nkind
(P
) = N_Qualified_Expression
then
2246 Set_Etype
(P
, Standard_Void_Type
);
2251 -- If this is a call of the form Obj.Op, the call may have been analyzed
2252 -- and possibly rewritten into a block, in which case we are done.
2254 if Analyzed
(N
) then
2257 -- If there is an error analyzing the name (which may have been
2258 -- rewritten if the original call was in prefix notation) then error
2259 -- has been emitted already, mark node and return.
2261 elsif Error_Posted
(N
) or else Etype
(Name
(N
)) = Any_Type
then
2262 Set_Etype
(N
, Any_Type
);
2266 -- A procedure call is Ghost when its name denotes a Ghost procedure.
2267 -- Set the mode now to ensure that any nodes generated during analysis
2268 -- and expansion are properly marked as Ghost.
2270 Mark_And_Set_Ghost_Procedure_Call
(N
);
2272 -- Otherwise analyze the parameters
2274 Actual
:= First
(Actuals
);
2276 while Present
(Actual
) loop
2278 Check_Parameterless_Call
(Actual
);
2282 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
2284 if Nkind
(P
) = N_Attribute_Reference
2285 and then Attribute_Name
(P
) in Name_Elab_Spec
2287 | Name_Elab_Subp_Body
2289 if Present
(Actuals
) then
2291 ("no parameters allowed for this call", First
(Actuals
));
2295 Set_Etype
(N
, Standard_Void_Type
);
2298 elsif Is_Entity_Name
(P
)
2299 and then Is_Record_Type
(Etype
(Entity
(P
)))
2300 and then Remote_AST_I_Dereference
(P
)
2304 elsif Is_Entity_Name
(P
)
2305 and then Ekind
(Entity
(P
)) /= E_Entry_Family
2307 if Is_Access_Type
(Etype
(P
))
2308 and then Ekind
(Designated_Type
(Etype
(P
))) = E_Subprogram_Type
2309 and then No
(Actuals
)
2310 and then Comes_From_Source
(N
)
2312 Error_Msg_N
("missing explicit dereference in call", N
);
2314 elsif Ekind
(Entity
(P
)) = E_Operator
then
2315 Error_Msg_Name_1
:= Chars
(P
);
2316 Error_Msg_N
("operator % cannot be used as a procedure", N
);
2319 Analyze_Call_And_Resolve
;
2321 -- If the prefix is the simple name of an entry family, this is a
2322 -- parameterless call from within the task body itself.
2324 elsif Is_Entity_Name
(P
)
2325 and then Nkind
(P
) = N_Identifier
2326 and then Ekind
(Entity
(P
)) = E_Entry_Family
2327 and then Present
(Actuals
)
2328 and then No
(Next
(First
(Actuals
)))
2330 -- Can be call to parameterless entry family. What appears to be the
2331 -- sole argument is in fact the entry index. Rewrite prefix of node
2332 -- accordingly. Source representation is unchanged by this
2336 Make_Indexed_Component
(Loc
,
2338 Make_Selected_Component
(Loc
,
2339 Prefix
=> New_Occurrence_Of
(Scope
(Entity
(P
)), Loc
),
2340 Selector_Name
=> New_Occurrence_Of
(Entity
(P
), Loc
)),
2341 Expressions
=> Actuals
);
2342 Set_Name
(N
, New_N
);
2343 Set_Etype
(New_N
, Standard_Void_Type
);
2344 Set_Parameter_Associations
(N
, No_List
);
2345 Analyze_Call_And_Resolve
;
2347 elsif Nkind
(P
) = N_Explicit_Dereference
then
2348 if Ekind
(Etype
(P
)) = E_Subprogram_Type
then
2349 Analyze_Call_And_Resolve
;
2351 Error_Msg_N
("expect access to procedure in call", P
);
2354 -- The name can be a selected component or an indexed component that
2355 -- yields an access to subprogram. Such a prefix is legal if the call
2356 -- has parameter associations.
2358 elsif Is_Access_Type
(Etype
(P
))
2359 and then Ekind
(Designated_Type
(Etype
(P
))) = E_Subprogram_Type
2361 if Present
(Actuals
) then
2362 Analyze_Call_And_Resolve
;
2364 Error_Msg_N
("missing explicit dereference in call", N
);
2367 -- If not an access to subprogram, then the prefix must resolve to the
2368 -- name of an entry, entry family, or protected operation.
2370 -- For the case of a simple entry call, P is a selected component where
2371 -- the prefix is the task and the selector name is the entry. A call to
2372 -- a protected procedure will have the same syntax. If the protected
2373 -- object contains overloaded operations, the entity may appear as a
2374 -- function, the context will select the operation whose type is Void.
2376 elsif Nkind
(P
) = N_Selected_Component
2377 and then Ekind
(Entity
(Selector_Name
(P
)))
2378 in E_Entry | E_Function | E_Procedure
2380 -- When front-end inlining is enabled, as with SPARK_Mode, a call
2381 -- in prefix notation may still be missing its controlling argument,
2382 -- so perform the transformation now.
2384 if SPARK_Mode
= On
and then In_Inlined_Body
then
2386 Subp
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
2387 Typ
: constant Entity_Id
:= Etype
(Prefix
(P
));
2390 if Is_Tagged_Type
(Typ
)
2391 and then Present
(First_Formal
(Subp
))
2392 and then (Etype
(First_Formal
(Subp
)) = Typ
2394 Class_Wide_Type
(Etype
(First_Formal
(Subp
))) = Typ
)
2395 and then Try_Object_Operation
(P
)
2400 Analyze_Call_And_Resolve
;
2405 Analyze_Call_And_Resolve
;
2408 elsif Nkind
(P
) = N_Selected_Component
2409 and then Ekind
(Entity
(Selector_Name
(P
))) = E_Entry_Family
2410 and then Present
(Actuals
)
2411 and then No
(Next
(First
(Actuals
)))
2413 -- Can be call to parameterless entry family. What appears to be the
2414 -- sole argument is in fact the entry index. Rewrite prefix of node
2415 -- accordingly. Source representation is unchanged by this
2419 Make_Indexed_Component
(Loc
,
2420 Prefix
=> New_Copy
(P
),
2421 Expressions
=> Actuals
);
2422 Set_Name
(N
, New_N
);
2423 Set_Etype
(New_N
, Standard_Void_Type
);
2424 Set_Parameter_Associations
(N
, No_List
);
2425 Analyze_Call_And_Resolve
;
2427 -- For the case of a reference to an element of an entry family, P is
2428 -- an indexed component whose prefix is a selected component (task and
2429 -- entry family), and whose index is the entry family index.
2431 elsif Nkind
(P
) = N_Indexed_Component
2432 and then Nkind
(Prefix
(P
)) = N_Selected_Component
2433 and then Ekind
(Entity
(Selector_Name
(Prefix
(P
)))) = E_Entry_Family
2435 Analyze_Call_And_Resolve
;
2437 -- If the prefix is the name of an entry family, it is a call from
2438 -- within the task body itself.
2440 elsif Nkind
(P
) = N_Indexed_Component
2441 and then Nkind
(Prefix
(P
)) = N_Identifier
2442 and then Ekind
(Entity
(Prefix
(P
))) = E_Entry_Family
2445 Make_Selected_Component
(Loc
,
2447 New_Occurrence_Of
(Scope
(Entity
(Prefix
(P
))), Loc
),
2448 Selector_Name
=> New_Occurrence_Of
(Entity
(Prefix
(P
)), Loc
));
2449 Rewrite
(Prefix
(P
), New_N
);
2451 Analyze_Call_And_Resolve
;
2453 -- In Ada 2012. a qualified expression is a name, but it cannot be a
2454 -- procedure name, so the construct can only be a qualified expression.
2456 elsif Nkind
(P
) = N_Qualified_Expression
2457 and then Ada_Version
>= Ada_2012
2459 Rewrite
(N
, Make_Code_Statement
(Loc
, Expression
=> P
));
2462 -- Anything else is an error
2465 Error_Msg_N
("invalid procedure or entry call", N
);
2467 -- Specialize the error message in the case where both a primitive
2468 -- operation and a record component are visible at the same time.
2470 if Nkind
(P
) = N_Selected_Component
2471 and then Is_Entity_Name
(Selector_Name
(P
))
2474 Sel
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
2476 if Ekind
(Sel
) = E_Component
2477 and then Present
(Homonym
(Sel
))
2478 and then Ekind
(Homonym
(Sel
)) = E_Procedure
2480 Error_Msg_NE
("\component & conflicts with"
2481 & " homonym procedure (RM 4.1.3 (9.2/3))",
2482 Selector_Name
(P
), Sel
);
2489 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
2490 end Analyze_Procedure_Call
;
2492 ------------------------------
2493 -- Analyze_Return_Statement --
2494 ------------------------------
2496 procedure Analyze_Return_Statement
(N
: Node_Id
) is
2498 (Nkind
(N
) in N_Extended_Return_Statement | N_Simple_Return_Statement
);
2500 Returns_Object
: constant Boolean :=
2501 Nkind
(N
) = N_Extended_Return_Statement
2503 (Nkind
(N
) = N_Simple_Return_Statement
2504 and then Present
(Expression
(N
)));
2505 -- True if we're returning something; that is, "return <expression>;"
2506 -- or "return Result : T [:= ...]". False for "return;". Used for error
2507 -- checking: If Returns_Object is True, N should apply to a function
2508 -- body; otherwise N should apply to a procedure body, entry body,
2509 -- accept statement, or extended return statement.
2511 function Find_What_It_Applies_To
return Entity_Id
;
2512 -- Find the entity representing the innermost enclosing body, accept
2513 -- statement, or extended return statement. If the result is a callable
2514 -- construct or extended return statement, then this will be the value
2515 -- of the Return_Applies_To attribute. Otherwise, the program is
2516 -- illegal. See RM-6.5(4/2).
2518 -----------------------------
2519 -- Find_What_It_Applies_To --
2520 -----------------------------
2522 function Find_What_It_Applies_To
return Entity_Id
is
2523 Result
: Entity_Id
:= Empty
;
2526 -- Loop outward through the Scope_Stack, skipping blocks, and loops
2528 for J
in reverse 0 .. Scope_Stack
.Last
loop
2529 Result
:= Scope_Stack
.Table
(J
).Entity
;
2530 exit when Ekind
(Result
) not in E_Block | E_Loop
;
2533 pragma Assert
(Present
(Result
));
2535 end Find_What_It_Applies_To
;
2537 -- Local declarations
2539 Scope_Id
: constant Entity_Id
:= Find_What_It_Applies_To
;
2540 Kind
: constant Entity_Kind
:= Ekind
(Scope_Id
);
2541 Loc
: constant Source_Ptr
:= Sloc
(N
);
2542 Stm_Entity
: constant Entity_Id
:=
2544 (E_Return_Statement
, Current_Scope
, Loc
, 'R');
2546 -- Start of processing for Analyze_Return_Statement
2549 Set_Return_Statement_Entity
(N
, Stm_Entity
);
2551 Set_Etype
(Stm_Entity
, Standard_Void_Type
);
2552 Set_Return_Applies_To
(Stm_Entity
, Scope_Id
);
2554 -- Place Return entity on scope stack, to simplify enforcement of 6.5
2555 -- (4/2): an inner return statement will apply to this extended return.
2557 if Nkind
(N
) = N_Extended_Return_Statement
then
2558 Push_Scope
(Stm_Entity
);
2561 -- Check that pragma No_Return is obeyed. Don't complain about the
2562 -- implicitly-generated return that is placed at the end.
2564 if No_Return
(Scope_Id
)
2565 and then Kind
in E_Procedure | E_Generic_Procedure
2566 and then Comes_From_Source
(N
)
2569 ("RETURN statement not allowed in No_Return procedure", N
);
2572 -- Warn on any unassigned OUT parameters if in procedure
2574 if Ekind
(Scope_Id
) = E_Procedure
then
2575 Warn_On_Unassigned_Out_Parameter
(N
, Scope_Id
);
2578 -- Check that functions return objects, and other things do not
2580 if Kind
in E_Function | E_Generic_Function
then
2581 if not Returns_Object
then
2582 Error_Msg_N
("missing expression in return from function", N
);
2585 elsif Kind
in E_Procedure | E_Generic_Procedure
then
2586 if Returns_Object
then
2587 Error_Msg_N
("procedure cannot return value (use function)", N
);
2590 elsif Kind
in E_Entry | E_Entry_Family
then
2591 if Returns_Object
then
2592 if Is_Protected_Type
(Scope
(Scope_Id
)) then
2593 Error_Msg_N
("entry body cannot return value", N
);
2595 Error_Msg_N
("accept statement cannot return value", N
);
2599 elsif Kind
= E_Return_Statement
then
2601 -- We are nested within another return statement, which must be an
2602 -- extended_return_statement.
2604 if Returns_Object
then
2605 if Nkind
(N
) = N_Extended_Return_Statement
then
2607 ("extended return statement cannot be nested (use `RETURN;`)",
2610 -- Case of a simple return statement with a value inside extended
2611 -- return statement.
2615 ("return nested in extended return statement cannot return "
2616 & "value (use `RETURN;`)", N
);
2621 Error_Msg_N
("illegal context for return statement", N
);
2624 if Kind
in E_Function | E_Generic_Function
then
2625 Analyze_Function_Return
(N
);
2627 elsif Kind
in E_Procedure | E_Generic_Procedure
then
2628 Set_Return_Present
(Scope_Id
);
2631 if Nkind
(N
) = N_Extended_Return_Statement
then
2635 Kill_Current_Values
(Last_Assignment_Only
=> True);
2636 Check_Unreachable_Code
(N
);
2638 Analyze_Dimension
(N
);
2639 end Analyze_Return_Statement
;
2641 -----------------------------------
2642 -- Analyze_Return_When_Statement --
2643 -----------------------------------
2645 procedure Analyze_Return_When_Statement
(N
: Node_Id
) is
2647 -- Verify the condition is a Boolean expression
2649 Analyze_And_Resolve
(Condition
(N
), Any_Boolean
);
2650 Check_Unset_Reference
(Condition
(N
));
2651 end Analyze_Return_When_Statement
;
2653 -------------------------------------
2654 -- Analyze_Simple_Return_Statement --
2655 -------------------------------------
2657 procedure Analyze_Simple_Return_Statement
(N
: Node_Id
) is
2659 if Present
(Expression
(N
)) then
2660 Mark_Coextensions
(N
, Expression
(N
));
2663 Analyze_Return_Statement
(N
);
2664 end Analyze_Simple_Return_Statement
;
2666 -------------------------
2667 -- Analyze_Return_Type --
2668 -------------------------
2670 procedure Analyze_Return_Type
(N
: Node_Id
) is
2671 Designator
: constant Entity_Id
:= Defining_Entity
(N
);
2672 Typ
: Entity_Id
:= Empty
;
2675 -- Normal case where result definition does not indicate an error
2677 if Result_Definition
(N
) /= Error
then
2678 if Nkind
(Result_Definition
(N
)) = N_Access_Definition
then
2680 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
2683 AD
: constant Node_Id
:=
2684 Access_To_Subprogram_Definition
(Result_Definition
(N
));
2686 if Present
(AD
) and then Protected_Present
(AD
) then
2687 Typ
:= Replace_Anonymous_Access_To_Protected_Subprogram
(N
);
2689 Typ
:= Access_Definition
(N
, Result_Definition
(N
));
2693 Set_Parent
(Typ
, Result_Definition
(N
));
2694 Set_Is_Local_Anonymous_Access
(Typ
);
2695 Set_Etype
(Designator
, Typ
);
2697 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2699 Null_Exclusion_Static_Checks
(N
);
2701 -- Subtype_Mark case
2704 Find_Type
(Result_Definition
(N
));
2705 Typ
:= Entity
(Result_Definition
(N
));
2706 Set_Etype
(Designator
, Typ
);
2708 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2710 Null_Exclusion_Static_Checks
(N
);
2712 -- If a null exclusion is imposed on the result type, then create
2713 -- a null-excluding itype (an access subtype) and use it as the
2714 -- function's Etype. Note that the null exclusion checks are done
2715 -- right before this, because they don't get applied to types that
2716 -- do not come from source.
2718 if Is_Access_Type
(Typ
) and then Null_Exclusion_Present
(N
) then
2719 Set_Etype
(Designator
,
2720 Create_Null_Excluding_Itype
2723 Scope_Id
=> Scope
(Current_Scope
)));
2725 -- The new subtype must be elaborated before use because
2726 -- it is visible outside of the function. However its base
2727 -- type may not be frozen yet, so the reference that will
2728 -- force elaboration must be attached to the freezing of
2731 -- If the return specification appears on a proper body,
2732 -- the subtype will have been created already on the spec.
2734 if Is_Frozen
(Typ
) then
2735 if Nkind
(Parent
(N
)) = N_Subprogram_Body
2736 and then Nkind
(Parent
(Parent
(N
))) = N_Subunit
2740 Build_Itype_Reference
(Etype
(Designator
), Parent
(N
));
2745 IR
: constant Node_Id
:= Make_Itype_Reference
(Sloc
(N
));
2747 Set_Itype
(IR
, Etype
(Designator
));
2748 Append_Freeze_Action
(Typ
, IR
);
2753 Set_Etype
(Designator
, Typ
);
2756 if Ekind
(Typ
) = E_Incomplete_Type
2757 or else (Is_Class_Wide_Type
(Typ
)
2758 and then Ekind
(Root_Type
(Typ
)) = E_Incomplete_Type
)
2760 -- AI05-0151: Tagged incomplete types are allowed in all formal
2761 -- parts. Untagged incomplete types are not allowed in bodies.
2762 -- As a consequence, limited views cannot appear in a basic
2763 -- declaration that is itself within a body, because there is
2764 -- no point at which the non-limited view will become visible.
2766 if Ada_Version
>= Ada_2012
then
2767 if From_Limited_With
(Typ
) and then In_Package_Body
then
2769 ("invalid use of incomplete type&",
2770 Result_Definition
(N
), Typ
);
2772 -- The return type of a subprogram body cannot be of a
2773 -- formal incomplete type.
2775 elsif Is_Generic_Type
(Typ
)
2776 and then Nkind
(Parent
(N
)) = N_Subprogram_Body
2779 ("return type cannot be a formal incomplete type",
2780 Result_Definition
(N
));
2782 elsif Is_Class_Wide_Type
(Typ
)
2783 and then Is_Generic_Type
(Root_Type
(Typ
))
2784 and then Nkind
(Parent
(N
)) = N_Subprogram_Body
2787 ("return type cannot be a formal incomplete type",
2788 Result_Definition
(N
));
2790 elsif Is_Tagged_Type
(Typ
) then
2793 -- Use is legal in a thunk generated for an operation
2794 -- inherited from a progenitor.
2796 elsif Is_Thunk
(Designator
)
2797 and then Present
(Non_Limited_View
(Typ
))
2801 elsif Nkind
(Parent
(N
)) = N_Subprogram_Body
2802 or else Nkind
(Parent
(Parent
(N
))) in
2803 N_Accept_Statement | N_Entry_Body
2806 ("invalid use of untagged incomplete type&",
2810 -- The type must be completed in the current package. This
2811 -- is checked at the end of the package declaration when
2812 -- Taft-amendment types are identified. If the return type
2813 -- is class-wide, there is no required check, the type can
2814 -- be a bona fide TAT.
2816 if Ekind
(Scope
(Current_Scope
)) = E_Package
2817 and then In_Private_Part
(Scope
(Current_Scope
))
2818 and then not Is_Class_Wide_Type
(Typ
)
2820 Append_Elmt
(Designator
, Private_Dependents
(Typ
));
2825 ("invalid use of incomplete type&", Designator
, Typ
);
2830 -- Case where result definition does indicate an error
2833 Set_Etype
(Designator
, Any_Type
);
2835 end Analyze_Return_Type
;
2837 -----------------------------
2838 -- Analyze_Subprogram_Body --
2839 -----------------------------
2841 procedure Analyze_Subprogram_Body
(N
: Node_Id
) is
2842 Loc
: constant Source_Ptr
:= Sloc
(N
);
2843 Body_Spec
: constant Node_Id
:= Specification
(N
);
2844 Body_Id
: constant Entity_Id
:= Defining_Entity
(Body_Spec
);
2847 if Debug_Flag_C
then
2848 Write_Str
("==> subprogram body ");
2849 Write_Name
(Chars
(Body_Id
));
2850 Write_Str
(" from ");
2851 Write_Location
(Loc
);
2856 Trace_Scope
(N
, Body_Id
, " Analyze subprogram: ");
2858 -- The real work is split out into the helper, so it can do "return;"
2859 -- without skipping the debug output:
2861 Analyze_Subprogram_Body_Helper
(N
);
2863 if Debug_Flag_C
then
2865 Write_Str
("<== subprogram body ");
2866 Write_Name
(Chars
(Body_Id
));
2867 Write_Str
(" from ");
2868 Write_Location
(Loc
);
2871 end Analyze_Subprogram_Body
;
2873 ------------------------------------
2874 -- Analyze_Subprogram_Body_Helper --
2875 ------------------------------------
2877 -- This procedure is called for regular subprogram bodies, generic bodies,
2878 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2879 -- specification matters, and is used to create a proper declaration for
2880 -- the subprogram, or to perform conformance checks.
2882 -- WARNING: This routine manages Ghost regions. Return statements must be
2883 -- replaced by gotos that jump to the end of the routine and restore the
2886 procedure Analyze_Subprogram_Body_Helper
(N
: Node_Id
) is
2887 Body_Spec
: Node_Id
:= Specification
(N
);
2888 Body_Id
: Entity_Id
:= Defining_Entity
(Body_Spec
);
2889 Loc
: constant Source_Ptr
:= Sloc
(N
);
2890 Prev_Id
: constant Entity_Id
:= Current_Entity_In_Scope
(Body_Id
);
2892 Body_Nod
: Node_Id
:= Empty
;
2893 Minimum_Acc_Objs
: List_Id
:= No_List
;
2895 Conformant
: Boolean;
2896 Desig_View
: Entity_Id
:= Empty
;
2897 Exch_Views
: Elist_Id
:= No_Elist
;
2899 Mask_Types
: Elist_Id
:= No_Elist
;
2900 Prot_Typ
: Entity_Id
:= Empty
;
2901 Spec_Decl
: Node_Id
:= Empty
;
2902 Spec_Id
: Entity_Id
;
2904 Last_Real_Spec_Entity
: Entity_Id
:= Empty
;
2905 -- When we analyze a separate spec, the entity chain ends up containing
2906 -- the formals, as well as any itypes generated during analysis of the
2907 -- default expressions for parameters, or the arguments of associated
2908 -- precondition/postcondition pragmas (which are analyzed in the context
2909 -- of the spec since they have visibility on formals).
2911 -- These entities belong with the spec and not the body. However we do
2912 -- the analysis of the body in the context of the spec (again to obtain
2913 -- visibility to the formals), and all the entities generated during
2914 -- this analysis end up also chained to the entity chain of the spec.
2915 -- But they really belong to the body, and there is circuitry to move
2916 -- them from the spec to the body.
2918 -- However, when we do this move, we don't want to move the real spec
2919 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2920 -- variable points to the last real spec entity, so we only move those
2921 -- chained beyond that point. It is initialized to Empty to deal with
2922 -- the case where there is no separate spec.
2924 function Body_Has_Contract
return Boolean;
2925 -- Check whether unanalyzed body has an aspect or pragma that may
2926 -- generate a SPARK contract.
2928 function Body_Has_SPARK_Mode_On
return Boolean;
2929 -- Check whether SPARK_Mode On applies to the subprogram body, either
2930 -- because it is specified directly on the body, or because it is
2931 -- inherited from the enclosing subprogram or package.
2933 function Build_Internal_Protected_Declaration
2934 (N
: Node_Id
) return Entity_Id
;
2935 -- A subprogram body without a previous spec that appears in a protected
2936 -- body must be expanded separately to create a subprogram declaration
2937 -- for it, in order to resolve internal calls to it from other protected
2940 -- Possibly factor this with Exp_Dist.Copy_Specification ???
2942 procedure Build_Subprogram_Declaration
;
2943 -- Create a matching subprogram declaration for subprogram body N
2945 procedure Check_Anonymous_Return
;
2946 -- Ada 2005: if a function returns an access type that denotes a task,
2947 -- or a type that contains tasks, we must create a master entity for
2948 -- the anonymous type, which typically will be used in an allocator
2949 -- in the body of the function.
2951 procedure Check_Inline_Pragma
(Spec
: in out Node_Id
);
2952 -- Look ahead to recognize a pragma that may appear after the body.
2953 -- If there is a previous spec, check that it appears in the same
2954 -- declarative part. If the pragma is Inline_Always, perform inlining
2955 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2956 -- If the body acts as a spec, and inlining is required, we create a
2957 -- subprogram declaration for it, in order to attach the body to inline.
2958 -- If pragma does not appear after the body, check whether there is
2959 -- an inline pragma before any local declarations.
2961 procedure Check_Missing_Return
;
2962 -- Checks for a function with a no return statements, and also performs
2963 -- the warning checks implemented by Check_Returns.
2965 function Disambiguate_Spec
return Entity_Id
;
2966 -- When a primitive is declared between the private view and the full
2967 -- view of a concurrent type which implements an interface, a special
2968 -- mechanism is used to find the corresponding spec of the primitive
2971 function Exchange_Limited_Views
(Subp_Id
: Entity_Id
) return Elist_Id
;
2972 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2973 -- incomplete types coming from a limited context and replace their
2974 -- limited views with the non-limited ones. Return the list of changes
2975 -- to be used to undo the transformation.
2977 procedure Generate_Minimum_Accessibility
2978 (Extra_Access
: Entity_Id
;
2979 Related_Form
: Entity_Id
:= Empty
);
2980 -- Generate a minimum accessibility object for a given extra
2981 -- accessibility formal (Extra_Access) and its related formal if it
2984 function Is_Private_Concurrent_Primitive
2985 (Subp_Id
: Entity_Id
) return Boolean;
2986 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2987 -- type that implements an interface and has a private view.
2989 function Mask_Unfrozen_Types
(Spec_Id
: Entity_Id
) return Elist_Id
;
2990 -- N is the body generated for an expression function that is not a
2991 -- completion and Spec_Id the defining entity of its spec. Mark all
2992 -- the not-yet-frozen types referenced by the simple return statement
2993 -- of the function as formally frozen.
2995 procedure Move_Pragmas
(From
: Node_Id
; To
: Node_Id
);
2996 -- Find all suitable source pragmas at the top of subprogram body
2997 -- From's declarations and move them after arbitrary node To.
2998 -- One exception is pragma SPARK_Mode which is copied rather than moved,
2999 -- as it applies to the body too.
3001 procedure Restore_Limited_Views
(Restore_List
: Elist_Id
);
3002 -- Undo the transformation done by Exchange_Limited_Views.
3004 procedure Set_Trivial_Subprogram
(N
: Node_Id
);
3005 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
3006 -- subprogram whose body is being analyzed. N is the statement node
3007 -- causing the flag to be set, if the following statement is a return
3008 -- of an entity, we mark the entity as set in source to suppress any
3009 -- warning on the stylized use of function stubs with a dummy return.
3011 procedure Unmask_Unfrozen_Types
(Unmask_List
: Elist_Id
);
3012 -- Undo the transformation done by Mask_Unfrozen_Types
3014 procedure Verify_Overriding_Indicator
;
3015 -- If there was a previous spec, the entity has been entered in the
3016 -- current scope previously. If the body itself carries an overriding
3017 -- indicator, check that it is consistent with the known status of the
3020 -----------------------
3021 -- Body_Has_Contract --
3022 -----------------------
3024 function Body_Has_Contract
return Boolean is
3025 Decls
: constant List_Id
:= Declarations
(N
);
3029 -- Check for aspects that may generate a contract
3031 Item
:= First
(Aspect_Specifications
(N
));
3032 while Present
(Item
) loop
3033 if Is_Subprogram_Contract_Annotation
(Item
) then
3040 -- Check for pragmas that may generate a contract
3042 Item
:= First
(Decls
);
3043 while Present
(Item
) loop
3044 if Nkind
(Item
) = N_Pragma
3045 and then Is_Subprogram_Contract_Annotation
(Item
)
3054 end Body_Has_Contract
;
3056 ----------------------------
3057 -- Body_Has_SPARK_Mode_On --
3058 ----------------------------
3060 function Body_Has_SPARK_Mode_On
return Boolean is
3061 Decls
: constant List_Id
:= Declarations
(N
);
3065 -- Check for SPARK_Mode aspect
3067 Item
:= First
(Aspect_Specifications
(N
));
3068 while Present
(Item
) loop
3069 if Get_Aspect_Id
(Item
) = Aspect_SPARK_Mode
then
3070 return Get_SPARK_Mode_From_Annotation
(Item
) = On
;
3076 -- Check for SPARK_Mode pragma
3078 Item
:= First
(Decls
);
3079 while Present
(Item
) loop
3081 -- Pragmas that apply to a subprogram body are usually grouped
3082 -- together. Look for a potential pragma SPARK_Mode among them.
3084 if Nkind
(Item
) = N_Pragma
then
3085 if Get_Pragma_Id
(Item
) = Pragma_SPARK_Mode
then
3086 return Get_SPARK_Mode_From_Annotation
(Item
) = On
;
3089 -- Otherwise the first non-pragma declarative item terminates the
3090 -- region where pragma SPARK_Mode may appear.
3099 -- Otherwise, the applicable SPARK_Mode is inherited from the
3100 -- enclosing subprogram or package.
3102 return SPARK_Mode
= On
;
3103 end Body_Has_SPARK_Mode_On
;
3105 ------------------------------------------
3106 -- Build_Internal_Protected_Declaration --
3107 ------------------------------------------
3109 function Build_Internal_Protected_Declaration
3110 (N
: Node_Id
) return Entity_Id
3112 procedure Analyze_Pragmas
(From
: Node_Id
);
3113 -- Analyze all pragmas which follow arbitrary node From
3115 ---------------------
3116 -- Analyze_Pragmas --
3117 ---------------------
3119 procedure Analyze_Pragmas
(From
: Node_Id
) is
3123 Decl
:= Next
(From
);
3124 while Present
(Decl
) loop
3125 if Nkind
(Decl
) = N_Pragma
then
3126 Analyze_Pragma
(Decl
);
3128 -- No candidate pragmas are available for analysis
3136 end Analyze_Pragmas
;
3140 Body_Id
: constant Entity_Id
:= Defining_Entity
(N
);
3141 Loc
: constant Source_Ptr
:= Sloc
(N
);
3146 Spec_Id
: Entity_Id
;
3148 -- Start of processing for Build_Internal_Protected_Declaration
3151 Formal
:= First_Formal
(Body_Id
);
3153 -- The protected operation always has at least one formal, namely the
3154 -- object itself, but it is only placed in the parameter list if
3155 -- expansion is enabled.
3157 if Present
(Formal
) or else Expander_Active
then
3158 Formals
:= Copy_Parameter_List
(Body_Id
);
3164 Make_Defining_Identifier
(Sloc
(Body_Id
),
3165 Chars
=> Chars
(Body_Id
));
3167 -- Indicate that the entity comes from source, to ensure that cross-
3168 -- reference information is properly generated. The body itself is
3169 -- rewritten during expansion, and the body entity will not appear in
3170 -- calls to the operation.
3172 Set_Comes_From_Source
(Spec_Id
, True);
3174 if Nkind
(Specification
(N
)) = N_Procedure_Specification
then
3176 Make_Procedure_Specification
(Loc
,
3177 Defining_Unit_Name
=> Spec_Id
,
3178 Parameter_Specifications
=> Formals
);
3181 Make_Function_Specification
(Loc
,
3182 Defining_Unit_Name
=> Spec_Id
,
3183 Parameter_Specifications
=> Formals
,
3184 Result_Definition
=>
3185 New_Occurrence_Of
(Etype
(Body_Id
), Loc
));
3188 Decl
:= Make_Subprogram_Declaration
(Loc
, Specification
=> Spec
);
3189 Set_Corresponding_Body
(Decl
, Body_Id
);
3190 Set_Corresponding_Spec
(N
, Spec_Id
);
3192 Insert_Before
(N
, Decl
);
3194 -- Associate all aspects and pragmas of the body with the spec. This
3195 -- ensures that these annotations apply to the initial declaration of
3196 -- the subprogram body.
3198 Move_Aspects
(From
=> N
, To
=> Decl
);
3199 Move_Pragmas
(From
=> N
, To
=> Decl
);
3203 -- The analysis of the spec may generate pragmas which require manual
3204 -- analysis. Since the generation of the spec and the relocation of
3205 -- the annotations is driven by the expansion of the stand-alone
3206 -- body, the pragmas will not be analyzed in a timely manner. Do this
3209 Analyze_Pragmas
(Decl
);
3211 -- This subprogram has convention Intrinsic as per RM 6.3.1(10/2)
3212 -- ensuring in particular that 'Access is illegal.
3214 Set_Convention
(Spec_Id
, Convention_Intrinsic
);
3215 Set_Has_Completion
(Spec_Id
);
3218 end Build_Internal_Protected_Declaration
;
3220 ----------------------------------
3221 -- Build_Subprogram_Declaration --
3222 ----------------------------------
3224 procedure Build_Subprogram_Declaration
is
3226 Subp_Decl
: Node_Id
;
3229 -- Create a matching subprogram spec using the profile of the body.
3230 -- The structure of the tree is identical, but has new entities for
3231 -- the defining unit name and formal parameters.
3234 Make_Subprogram_Declaration
(Loc
,
3235 Specification
=> Copy_Subprogram_Spec
(Body_Spec
));
3236 Set_Comes_From_Source
(Subp_Decl
, True);
3238 -- Also mark parameters as coming from source
3240 if Present
(Parameter_Specifications
(Specification
(Subp_Decl
))) then
3245 First
(Parameter_Specifications
(Specification
(Subp_Decl
)));
3247 while Present
(Form
) loop
3248 Set_Comes_From_Source
(Defining_Identifier
(Form
), True);
3254 -- Relocate the aspects and relevant pragmas from the subprogram body
3255 -- to the generated spec because it acts as the initial declaration.
3257 Insert_Before
(N
, Subp_Decl
);
3258 Move_Aspects
(N
, To
=> Subp_Decl
);
3259 Move_Pragmas
(N
, To
=> Subp_Decl
);
3261 -- Ensure that the generated corresponding spec and original body
3262 -- share the same SPARK_Mode pragma or aspect. As a result, both have
3263 -- the same SPARK_Mode attributes, and the global SPARK_Mode value is
3264 -- correctly set for local subprograms.
3266 Copy_SPARK_Mode_Aspect
(Subp_Decl
, To
=> N
);
3268 Analyze
(Subp_Decl
);
3270 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
3271 -- the body since the expander may generate calls using that entity.
3272 -- Required to ensure that Expand_Call rewrites calls to this
3273 -- function by calls to the built procedure.
3275 if Transform_Function_Array
3276 and then Nkind
(Body_Spec
) = N_Function_Specification
3278 Rewritten_For_C
(Defining_Entity
(Specification
(Subp_Decl
)))
3280 Set_Rewritten_For_C
(Defining_Entity
(Body_Spec
));
3281 Set_Corresponding_Procedure
(Defining_Entity
(Body_Spec
),
3282 Corresponding_Procedure
3283 (Defining_Entity
(Specification
(Subp_Decl
))));
3286 -- Analyze any relocated source pragmas or pragmas created for aspect
3289 Decl
:= Next
(Subp_Decl
);
3290 while Present
(Decl
) loop
3292 -- Stop the search for pragmas once the body has been reached as
3293 -- this terminates the region where pragmas may appear.
3298 elsif Nkind
(Decl
) = N_Pragma
then
3305 Spec_Id
:= Defining_Entity
(Subp_Decl
);
3306 Set_Corresponding_Spec
(N
, Spec_Id
);
3308 -- Mark the generated spec as a source construct to ensure that all
3309 -- calls to it are properly registered in ALI files for GNATprove.
3311 Set_Comes_From_Source
(Spec_Id
, True);
3313 -- Ensure that the specs of the subprogram declaration and its body
3314 -- are identical, otherwise they will appear non-conformant due to
3315 -- rewritings in the default values of formal parameters.
3317 Body_Spec
:= Copy_Subprogram_Spec
(Body_Spec
);
3318 Set_Specification
(N
, Body_Spec
);
3319 Body_Id
:= Analyze_Subprogram_Specification
(Body_Spec
);
3320 end Build_Subprogram_Declaration
;
3322 ----------------------------
3323 -- Check_Anonymous_Return --
3324 ----------------------------
3326 procedure Check_Anonymous_Return
is
3332 if Present
(Spec_Id
) then
3338 if Ekind
(Scop
) = E_Function
3339 and then Ekind
(Etype
(Scop
)) = E_Anonymous_Access_Type
3340 and then not Is_Thunk
(Scop
)
3342 -- Skip internally built functions which handle the case of
3343 -- a null access (see Expand_Interface_Conversion)
3345 and then not (Is_Interface
(Designated_Type
(Etype
(Scop
)))
3346 and then not Comes_From_Source
(Parent
(Scop
)))
3348 and then (Has_Task
(Designated_Type
(Etype
(Scop
)))
3350 (Is_Class_Wide_Type
(Designated_Type
(Etype
(Scop
)))
3352 Is_Limited_Record
(Designated_Type
(Etype
(Scop
)))))
3353 and then Expander_Active
3355 Decl
:= Build_Master_Declaration
(Loc
);
3357 if Present
(Declarations
(N
)) then
3358 Prepend
(Decl
, Declarations
(N
));
3360 Set_Declarations
(N
, New_List
(Decl
));
3363 Set_Master_Id
(Etype
(Scop
), Defining_Identifier
(Decl
));
3364 Set_Has_Master_Entity
(Scop
);
3366 -- Now mark the containing scope as a task master
3369 while Nkind
(Par
) /= N_Compilation_Unit
loop
3370 Par
:= Parent
(Par
);
3371 pragma Assert
(Present
(Par
));
3373 -- If we fall off the top, we are at the outer level, and
3374 -- the environment task is our effective master, so nothing
3378 in N_Task_Body | N_Block_Statement | N_Subprogram_Body
3380 Set_Is_Task_Master
(Par
, True);
3385 end Check_Anonymous_Return
;
3387 -------------------------
3388 -- Check_Inline_Pragma --
3389 -------------------------
3391 procedure Check_Inline_Pragma
(Spec
: in out Node_Id
) is
3395 function Is_Inline_Pragma
(N
: Node_Id
) return Boolean;
3396 -- True when N is a pragma Inline or Inline_Always that applies
3397 -- to this subprogram.
3399 -----------------------
3400 -- Is_Inline_Pragma --
3401 -----------------------
3403 function Is_Inline_Pragma
(N
: Node_Id
) return Boolean is
3405 if Nkind
(N
) = N_Pragma
3407 (Pragma_Name_Unmapped
(N
) = Name_Inline_Always
3408 or else (Pragma_Name_Unmapped
(N
) = Name_Inline
3410 (Front_End_Inlining
or else Optimization_Level
> 0)))
3411 and then Present
(Pragma_Argument_Associations
(N
))
3414 Pragma_Arg
: Node_Id
:=
3415 Expression
(First
(Pragma_Argument_Associations
(N
)));
3417 if Nkind
(Pragma_Arg
) = N_Selected_Component
then
3418 Pragma_Arg
:= Selector_Name
(Pragma_Arg
);
3421 return Chars
(Pragma_Arg
) = Chars
(Body_Id
);
3427 end Is_Inline_Pragma
;
3429 -- Start of processing for Check_Inline_Pragma
3432 if not Expander_Active
then
3436 if Is_List_Member
(N
)
3437 and then Present
(Next
(N
))
3438 and then Is_Inline_Pragma
(Next
(N
))
3442 elsif Nkind
(N
) /= N_Subprogram_Body_Stub
3443 and then Present
(Declarations
(N
))
3444 and then Is_Inline_Pragma
(First
(Declarations
(N
)))
3446 Prag
:= First
(Declarations
(N
));
3452 if Present
(Prag
) and then Is_List_Member
(N
) then
3453 if Present
(Spec_Id
) then
3454 if Is_List_Member
(Unit_Declaration_Node
(Spec_Id
))
3455 and then In_Same_List
(N
, Unit_Declaration_Node
(Spec_Id
))
3460 -- Create a subprogram declaration, to make treatment uniform.
3461 -- Make the sloc of the subprogram name that of the entity in
3462 -- the body, so that style checks find identical strings.
3465 Subp
: constant Entity_Id
:=
3466 Make_Defining_Identifier
3467 (Sloc
(Body_Id
), Chars
(Body_Id
));
3468 Decl
: constant Node_Id
:=
3469 Make_Subprogram_Declaration
(Loc
,
3471 New_Copy_Tree
(Specification
(N
)));
3474 -- Link the body and the generated spec
3476 Set_Corresponding_Body
(Decl
, Body_Id
);
3478 if Nkind
(N
) = N_Subprogram_Body_Stub
then
3479 Set_Corresponding_Spec_Of_Stub
(N
, Subp
);
3481 Set_Corresponding_Spec
(N
, Subp
);
3484 Set_Defining_Unit_Name
(Specification
(Decl
), Subp
);
3486 -- To ensure proper coverage when body is inlined, indicate
3487 -- whether the subprogram comes from source.
3489 Preserve_Comes_From_Source
(Subp
, N
);
3491 if Present
(First_Formal
(Body_Id
)) then
3492 Plist
:= Copy_Parameter_List
(Body_Id
);
3493 Set_Parameter_Specifications
3494 (Specification
(Decl
), Plist
);
3497 -- Move aspects to the new spec
3499 if Has_Aspects
(N
) then
3500 Move_Aspects
(N
, To
=> Decl
);
3503 Insert_Before
(N
, Decl
);
3506 Set_Has_Pragma_Inline
(Subp
);
3508 if Pragma_Name
(Prag
) = Name_Inline_Always
then
3509 Set_Is_Inlined
(Subp
);
3510 Set_Has_Pragma_Inline_Always
(Subp
);
3513 -- Prior to copying the subprogram body to create a template
3514 -- for it for subsequent inlining, remove the pragma from
3515 -- the current body so that the copy that will produce the
3516 -- new body will start from a completely unanalyzed tree.
3518 if Nkind
(Parent
(Prag
)) = N_Subprogram_Body
then
3519 Rewrite
(Prag
, Make_Null_Statement
(Sloc
(Prag
)));
3526 end Check_Inline_Pragma
;
3528 --------------------------
3529 -- Check_Missing_Return --
3530 --------------------------
3532 procedure Check_Missing_Return
is
3534 Missing_Ret
: Boolean;
3537 if Nkind
(Body_Spec
) = N_Function_Specification
then
3538 if Present
(Spec_Id
) then
3544 -- A function body shall contain at least one return statement
3545 -- that applies to the function body, unless the function contains
3546 -- code_statements; RM 6.5(5).
3548 if Return_Present
(Id
) then
3549 Check_Returns
(HSS
, 'F', Missing_Ret
);
3552 Set_Has_Missing_Return
(Id
);
3555 -- Within a premature instantiation of a package with no body, we
3556 -- build completions of the functions therein, with a Raise
3557 -- statement. No point in complaining about a missing return in
3560 elsif Ekind
(Id
) = E_Function
3561 and then In_Instance
3562 and then Present
(Statements
(HSS
))
3563 and then Nkind
(First
(Statements
(HSS
))) = N_Raise_Program_Error
3567 elsif Is_Generic_Subprogram
(Id
)
3568 or else not Is_Machine_Code_Subprogram
(Id
)
3570 Error_Msg_N
("missing RETURN statement in function body", N
);
3573 -- If procedure with No_Return, check returns
3575 elsif Nkind
(Body_Spec
) = N_Procedure_Specification
then
3576 if Present
(Spec_Id
) then
3582 if No_Return
(Id
) then
3583 Check_Returns
(HSS
, 'P', Missing_Ret
, Id
);
3586 end Check_Missing_Return
;
3588 -----------------------
3589 -- Disambiguate_Spec --
3590 -----------------------
3592 function Disambiguate_Spec
return Entity_Id
is
3593 Priv_Spec
: Entity_Id
;
3596 procedure Replace_Types
(To_Corresponding
: Boolean);
3597 -- Depending on the flag, replace the type of formal parameters of
3598 -- Body_Id if it is a concurrent type implementing interfaces with
3599 -- the corresponding record type or the other way around.
3601 procedure Replace_Types
(To_Corresponding
: Boolean) is
3603 Formal_Typ
: Entity_Id
;
3606 Formal
:= First_Formal
(Body_Id
);
3607 while Present
(Formal
) loop
3608 Formal_Typ
:= Etype
(Formal
);
3610 if Is_Class_Wide_Type
(Formal_Typ
) then
3611 Formal_Typ
:= Root_Type
(Formal_Typ
);
3614 -- From concurrent type to corresponding record
3616 if To_Corresponding
then
3617 if Is_Concurrent_Type
(Formal_Typ
)
3618 and then Present
(Corresponding_Record_Type
(Formal_Typ
))
3621 (Corresponding_Record_Type
(Formal_Typ
)))
3624 Corresponding_Record_Type
(Formal_Typ
));
3627 -- From corresponding record to concurrent type
3630 if Is_Concurrent_Record_Type
(Formal_Typ
)
3631 and then Present
(Interfaces
(Formal_Typ
))
3634 Corresponding_Concurrent_Type
(Formal_Typ
));
3638 Next_Formal
(Formal
);
3642 -- Start of processing for Disambiguate_Spec
3645 -- Try to retrieve the specification of the body as is. All error
3646 -- messages are suppressed because the body may not have a spec in
3647 -- its current state.
3649 Spec_N
:= Find_Corresponding_Spec
(N
, False);
3651 -- It is possible that this is the body of a primitive declared
3652 -- between a private and a full view of a concurrent type. The
3653 -- controlling parameter of the spec carries the concurrent type,
3654 -- not the corresponding record type as transformed by Analyze_
3655 -- Subprogram_Specification. In such cases, we undo the change
3656 -- made by the analysis of the specification and try to find the
3659 -- Note that wrappers already have their corresponding specs and
3660 -- bodies set during their creation, so if the candidate spec is
3661 -- a wrapper, then we definitely need to swap all types to their
3662 -- original concurrent status.
3665 or else Is_Primitive_Wrapper
(Spec_N
)
3667 -- Restore all references of corresponding record types to the
3668 -- original concurrent types.
3670 Replace_Types
(To_Corresponding
=> False);
3671 Priv_Spec
:= Find_Corresponding_Spec
(N
, False);
3673 -- The current body truly belongs to a primitive declared between
3674 -- a private and a full view. We leave the modified body as is,
3675 -- and return the true spec.
3677 if Present
(Priv_Spec
)
3678 and then Is_Private_Primitive
(Priv_Spec
)
3683 -- In case that this is some sort of error, restore the original
3684 -- state of the body.
3686 Replace_Types
(To_Corresponding
=> True);
3690 end Disambiguate_Spec
;
3692 ----------------------------
3693 -- Exchange_Limited_Views --
3694 ----------------------------
3696 function Exchange_Limited_Views
(Subp_Id
: Entity_Id
) return Elist_Id
is
3697 Result
: Elist_Id
:= No_Elist
;
3699 procedure Detect_And_Exchange
(Id
: Entity_Id
);
3700 -- Determine whether Id's type denotes an incomplete type associated
3701 -- with a limited with clause and exchange the limited view with the
3702 -- non-limited one when available. Note that the non-limited view
3703 -- may exist because of a with_clause in another unit in the context,
3704 -- but cannot be used because the current view of the enclosing unit
3705 -- is still a limited view.
3707 -------------------------
3708 -- Detect_And_Exchange --
3709 -------------------------
3711 procedure Detect_And_Exchange
(Id
: Entity_Id
) is
3712 Typ
: constant Entity_Id
:= Etype
(Id
);
3715 if From_Limited_With
(Typ
)
3716 and then Has_Non_Limited_View
(Typ
)
3717 and then not From_Limited_With
(Scope
(Typ
))
3720 Result
:= New_Elmt_List
;
3723 Prepend_Elmt
(Typ
, Result
);
3724 Prepend_Elmt
(Id
, Result
);
3725 Set_Etype
(Id
, Non_Limited_View
(Typ
));
3727 end Detect_And_Exchange
;
3733 -- Start of processing for Exchange_Limited_Views
3736 -- Do not process subprogram bodies as they already use the non-
3737 -- limited view of types.
3739 if Ekind
(Subp_Id
) not in E_Function | E_Procedure
then
3743 -- Examine all formals and swap views when applicable
3745 Formal
:= First_Formal
(Subp_Id
);
3746 while Present
(Formal
) loop
3747 Detect_And_Exchange
(Formal
);
3749 Next_Formal
(Formal
);
3752 -- Process the return type of a function
3754 if Ekind
(Subp_Id
) = E_Function
then
3755 Detect_And_Exchange
(Subp_Id
);
3759 end Exchange_Limited_Views
;
3761 ------------------------------------
3762 -- Generate_Minimum_Accessibility --
3763 ------------------------------------
3765 procedure Generate_Minimum_Accessibility
3766 (Extra_Access
: Entity_Id
;
3767 Related_Form
: Entity_Id
:= Empty
)
3769 Loc
: constant Source_Ptr
:= Sloc
(Body_Nod
);
3773 -- When no related formal exists then we are dealing with an
3774 -- extra accessibility formal for a function result.
3776 if No
(Related_Form
) then
3777 Form
:= Extra_Access
;
3779 Form
:= Related_Form
;
3782 -- Create the minimum accessibility object
3785 Make_Object_Declaration
(Loc
,
3786 Defining_Identifier
=>
3788 (Loc
, 'A', Extra_Access
),
3789 Object_Definition
=> New_Occurrence_Of
3790 (Standard_Natural
, Loc
),
3792 Make_Attribute_Reference
(Loc
,
3793 Prefix
=> New_Occurrence_Of
3794 (Standard_Natural
, Loc
),
3795 Attribute_Name
=> Name_Min
,
3796 Expressions
=> New_List
(
3797 Make_Integer_Literal
(Loc
,
3798 Scope_Depth
(Body_Id
)),
3800 (Extra_Access
, Loc
))));
3802 -- Add the new local object to the Minimum_Acc_Obj to
3803 -- be later prepended to the subprogram's list of
3804 -- declarations after we are sure all expansion is
3807 if Present
(Minimum_Acc_Objs
) then
3808 Prepend
(Obj_Node
, Minimum_Acc_Objs
);
3810 Minimum_Acc_Objs
:= New_List
(Obj_Node
);
3813 -- Register the object and analyze it
3815 Set_Minimum_Accessibility
3816 (Form
, Defining_Identifier
(Obj_Node
));
3819 end Generate_Minimum_Accessibility
;
3821 -------------------------------------
3822 -- Is_Private_Concurrent_Primitive --
3823 -------------------------------------
3825 function Is_Private_Concurrent_Primitive
3826 (Subp_Id
: Entity_Id
) return Boolean
3828 Formal_Typ
: Entity_Id
;
3831 if Present
(First_Formal
(Subp_Id
)) then
3832 Formal_Typ
:= Etype
(First_Formal
(Subp_Id
));
3834 if Is_Concurrent_Record_Type
(Formal_Typ
) then
3835 if Is_Class_Wide_Type
(Formal_Typ
) then
3836 Formal_Typ
:= Root_Type
(Formal_Typ
);
3839 Formal_Typ
:= Corresponding_Concurrent_Type
(Formal_Typ
);
3842 -- The type of the first formal is a concurrent tagged type with
3846 Is_Concurrent_Type
(Formal_Typ
)
3847 and then Is_Tagged_Type
(Formal_Typ
)
3848 and then Has_Private_Declaration
(Formal_Typ
);
3852 end Is_Private_Concurrent_Primitive
;
3854 -------------------------
3855 -- Mask_Unfrozen_Types --
3856 -------------------------
3858 function Mask_Unfrozen_Types
(Spec_Id
: Entity_Id
) return Elist_Id
is
3859 Result
: Elist_Id
:= No_Elist
;
3861 function Mask_Type_Refs
(Node
: Node_Id
) return Traverse_Result
;
3862 -- Mask all types referenced in the subtree rooted at Node as
3865 --------------------
3866 -- Mask_Type_Refs --
3867 --------------------
3869 function Mask_Type_Refs
(Node
: Node_Id
) return Traverse_Result
is
3870 procedure Mask_Type
(Typ
: Entity_Id
);
3871 -- Mask a given type as formally frozen when outside the current
3872 -- scope, or else freeze the type.
3878 procedure Mask_Type
(Typ
: Entity_Id
) is
3880 -- Skip Itypes created by the preanalysis
3883 and then Scope_Within_Or_Same
(Scope
(Typ
), Spec_Id
)
3888 if not Is_Frozen
(Typ
) then
3889 if Scope
(Typ
) /= Current_Scope
then
3890 Set_Is_Frozen
(Typ
);
3891 Append_New_Elmt
(Typ
, Result
);
3893 Freeze_Before
(N
, Typ
);
3898 -- Start of processing for Mask_Type_Refs
3901 if Is_Entity_Name
(Node
) and then Present
(Entity
(Node
)) then
3902 Mask_Type
(Etype
(Entity
(Node
)));
3904 if Ekind
(Entity
(Node
)) in E_Component | E_Discriminant
then
3905 Mask_Type
(Scope
(Entity
(Node
)));
3908 elsif Nkind
(Node
) in N_Aggregate | N_Null | N_Type_Conversion
3909 and then Present
(Etype
(Node
))
3911 Mask_Type
(Etype
(Node
));
3917 procedure Mask_References
is new Traverse_Proc
(Mask_Type_Refs
);
3921 Return_Stmt
: constant Node_Id
:=
3922 First
(Statements
(Handled_Statement_Sequence
(N
)));
3924 -- Start of processing for Mask_Unfrozen_Types
3927 pragma Assert
(Nkind
(Return_Stmt
) = N_Simple_Return_Statement
);
3929 Mask_References
(Expression
(Return_Stmt
));
3932 end Mask_Unfrozen_Types
;
3938 procedure Move_Pragmas
(From
: Node_Id
; To
: Node_Id
) is
3940 Insert_Nod
: Node_Id
;
3941 Next_Decl
: Node_Id
;
3944 pragma Assert
(Nkind
(From
) = N_Subprogram_Body
);
3946 -- The pragmas are moved in an order-preserving fashion
3950 -- Inspect the declarations of the subprogram body and relocate all
3951 -- candidate pragmas.
3953 Decl
:= First
(Declarations
(From
));
3954 while Present
(Decl
) loop
3956 -- Preserve the following declaration for iteration purposes, due
3957 -- to possible relocation of a pragma.
3959 Next_Decl
:= Next
(Decl
);
3961 if Nkind
(Decl
) = N_Pragma
then
3962 -- Copy pragma SPARK_Mode if present in the declarative list
3963 -- of subprogram body From and insert it after node To. This
3964 -- pragma should not be moved, as it applies to the body too.
3966 if Pragma_Name_Unmapped
(Decl
) = Name_SPARK_Mode
then
3967 Insert_After
(Insert_Nod
, New_Copy_Tree
(Decl
));
3969 -- Move relevant pragmas to the spec
3971 elsif Pragma_Name_Unmapped
(Decl
) in Name_Depends
3977 | Name_Refined_Depends
3978 | Name_Refined_Global
3981 | Name_Pure_Function
3982 | Name_Volatile_Function
3985 Insert_After
(Insert_Nod
, Decl
);
3989 -- Skip internally generated code
3991 elsif not Comes_From_Source
(Decl
) then
3994 -- No candidate pragmas are available for relocation
4004 ---------------------------
4005 -- Restore_Limited_Views --
4006 ---------------------------
4008 procedure Restore_Limited_Views
(Restore_List
: Elist_Id
) is
4009 Elmt
: Elmt_Id
:= First_Elmt
(Restore_List
);
4013 while Present
(Elmt
) loop
4016 Set_Etype
(Id
, Node
(Elmt
));
4019 end Restore_Limited_Views
;
4021 ----------------------------
4022 -- Set_Trivial_Subprogram --
4023 ----------------------------
4025 procedure Set_Trivial_Subprogram
(N
: Node_Id
) is
4026 Nxt
: constant Node_Id
:= Next
(N
);
4029 Set_Is_Trivial_Subprogram
(Body_Id
);
4031 if Present
(Spec_Id
) then
4032 Set_Is_Trivial_Subprogram
(Spec_Id
);
4036 and then Nkind
(Nxt
) = N_Simple_Return_Statement
4037 and then No
(Next
(Nxt
))
4038 and then Present
(Expression
(Nxt
))
4039 and then Is_Entity_Name
(Expression
(Nxt
))
4041 Set_Never_Set_In_Source
(Entity
(Expression
(Nxt
)), False);
4043 end Set_Trivial_Subprogram
;
4045 ---------------------------
4046 -- Unmask_Unfrozen_Types --
4047 ---------------------------
4049 procedure Unmask_Unfrozen_Types
(Unmask_List
: Elist_Id
) is
4050 Elmt
: Elmt_Id
:= First_Elmt
(Unmask_List
);
4053 while Present
(Elmt
) loop
4054 Set_Is_Frozen
(Node
(Elmt
), False);
4057 end Unmask_Unfrozen_Types
;
4059 ---------------------------------
4060 -- Verify_Overriding_Indicator --
4061 ---------------------------------
4063 procedure Verify_Overriding_Indicator
is
4065 if Must_Override
(Body_Spec
) then
4066 if Nkind
(Spec_Id
) = N_Defining_Operator_Symbol
4067 and then Operator_Matches_Spec
(Spec_Id
, Spec_Id
)
4071 -- Overridden controlled primitives may have had their
4072 -- Overridden_Operation field cleared according to the setting of
4073 -- the Is_Hidden flag. An issue arises, however, when analyzing
4074 -- an instance that may have manipulated the flag during
4075 -- expansion. As a result, we add an exception for this case.
4077 elsif not Present
(Overridden_Operation
(Spec_Id
))
4078 and then not (Chars
(Spec_Id
) in Name_Adjust
4081 and then In_Instance
)
4084 ("subprogram& is not overriding", Body_Spec
, Spec_Id
);
4086 -- Overriding indicators aren't allowed for protected subprogram
4087 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
4088 -- this to a warning if -gnatd.E is enabled.
4090 elsif Ekind
(Scope
(Spec_Id
)) = E_Protected_Type
then
4091 Error_Msg_Warn
:= Error_To_Warning
;
4093 ("<<overriding indicator not allowed for protected "
4094 & "subprogram body", Body_Spec
);
4097 elsif Must_Not_Override
(Body_Spec
) then
4098 if Present
(Overridden_Operation
(Spec_Id
)) then
4100 ("subprogram& overrides inherited operation",
4101 Body_Spec
, Spec_Id
);
4103 elsif Nkind
(Spec_Id
) = N_Defining_Operator_Symbol
4104 and then Operator_Matches_Spec
(Spec_Id
, Spec_Id
)
4107 ("subprogram& overrides predefined operator",
4108 Body_Spec
, Spec_Id
);
4110 -- Overriding indicators aren't allowed for protected subprogram
4111 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
4112 -- this to a warning if -gnatd.E is enabled.
4114 elsif Ekind
(Scope
(Spec_Id
)) = E_Protected_Type
then
4115 Error_Msg_Warn
:= Error_To_Warning
;
4118 ("<<overriding indicator not allowed "
4119 & "for protected subprogram body", Body_Spec
);
4121 -- If this is not a primitive operation, then the overriding
4122 -- indicator is altogether illegal.
4124 elsif not Is_Primitive
(Spec_Id
) then
4126 ("overriding indicator only allowed "
4127 & "if subprogram is primitive", Body_Spec
);
4130 -- If checking the style rule and the operation overrides, then
4131 -- issue a warning about a missing overriding_indicator. Protected
4132 -- subprogram bodies are excluded from this style checking, since
4133 -- they aren't primitives (even though their declarations can
4134 -- override) and aren't allowed to have an overriding_indicator.
4137 and then Present
(Overridden_Operation
(Spec_Id
))
4138 and then Ekind
(Scope
(Spec_Id
)) /= E_Protected_Type
4140 pragma Assert
(Unit_Declaration_Node
(Body_Id
) = N
);
4141 Style
.Missing_Overriding
(N
, Body_Id
);
4144 and then Can_Override_Operator
(Spec_Id
)
4145 and then not In_Predefined_Unit
(Spec_Id
)
4147 pragma Assert
(Unit_Declaration_Node
(Body_Id
) = N
);
4148 Style
.Missing_Overriding
(N
, Body_Id
);
4150 end Verify_Overriding_Indicator
;
4154 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
4155 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
4156 Saved_EA
: constant Boolean := Expander_Active
;
4157 Saved_ISMP
: constant Boolean :=
4158 Ignore_SPARK_Mode_Pragmas_In_Instance
;
4159 -- Save the Ghost and SPARK mode-related data to restore on exit
4161 -- Start of processing for Analyze_Subprogram_Body_Helper
4164 -- A [generic] subprogram body freezes the contract of the nearest
4165 -- enclosing package body and all other contracts encountered in the
4166 -- same declarative part up to and excluding the subprogram body:
4168 -- package body Nearest_Enclosing_Package
4169 -- with Refined_State => (State => Constit)
4173 -- procedure Freezes_Enclosing_Package_Body
4174 -- with Refined_Depends => (Input => Constit) ...
4176 -- This ensures that any annotations referenced by the contract of the
4177 -- [generic] subprogram body are available. This form of freezing is
4178 -- decoupled from the usual Freeze_xxx mechanism because it must also
4179 -- work in the context of generics where normal freezing is disabled.
4181 -- Only bodies coming from source should cause this type of freezing.
4182 -- Expression functions that act as bodies and complete an initial
4183 -- declaration must be included in this category, hence the use of
4186 if Comes_From_Source
(Original_Node
(N
)) then
4187 Freeze_Previous_Contracts
(N
);
4190 -- Generic subprograms are handled separately. They always have a
4191 -- generic specification. Determine whether current scope has a
4192 -- previous declaration.
4194 -- If the subprogram body is defined within an instance of the same
4195 -- name, the instance appears as a package renaming, and will be hidden
4196 -- within the subprogram.
4198 if Present
(Prev_Id
)
4199 and then not Is_Overloadable
(Prev_Id
)
4200 and then (Nkind
(Parent
(Prev_Id
)) /= N_Package_Renaming_Declaration
4201 or else Comes_From_Source
(Prev_Id
))
4203 if Is_Generic_Subprogram
(Prev_Id
) then
4206 -- A subprogram body is Ghost when it is stand-alone and subject
4207 -- to pragma Ghost or when the corresponding spec is Ghost. Set
4208 -- the mode now to ensure that any nodes generated during analysis
4209 -- and expansion are properly marked as Ghost.
4211 Mark_And_Set_Ghost_Body
(N
, Spec_Id
);
4213 -- If the body completes the initial declaration of a compilation
4214 -- unit which is subject to pragma Elaboration_Checks, set the
4215 -- model specified by the pragma because it applies to all parts
4218 Install_Elaboration_Model
(Spec_Id
);
4220 Set_Is_Compilation_Unit
(Body_Id
, Is_Compilation_Unit
(Spec_Id
));
4221 Set_Is_Child_Unit
(Body_Id
, Is_Child_Unit
(Spec_Id
));
4223 Analyze_Generic_Subprogram_Body
(N
, Spec_Id
);
4225 if Nkind
(N
) = N_Subprogram_Body
then
4226 HSS
:= Handled_Statement_Sequence
(N
);
4227 Check_Missing_Return
;
4232 -- Otherwise a previous entity conflicts with the subprogram name.
4233 -- Attempting to enter name will post error.
4236 Enter_Name
(Body_Id
);
4240 -- Non-generic case, find the subprogram declaration, if one was seen,
4241 -- or enter new overloaded entity in the current scope. If the
4242 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
4243 -- part of the context of one of its subunits. No need to redo the
4246 elsif Prev_Id
= Body_Id
and then Has_Completion
(Body_Id
) then
4250 Body_Id
:= Analyze_Subprogram_Specification
(Body_Spec
);
4252 if Nkind
(N
) = N_Subprogram_Body_Stub
4253 or else No
(Corresponding_Spec
(N
))
4255 if Is_Private_Concurrent_Primitive
(Body_Id
) then
4256 Spec_Id
:= Disambiguate_Spec
;
4258 -- A subprogram body is Ghost when it is stand-alone and
4259 -- subject to pragma Ghost or when the corresponding spec is
4260 -- Ghost. Set the mode now to ensure that any nodes generated
4261 -- during analysis and expansion are properly marked as Ghost.
4263 Mark_And_Set_Ghost_Body
(N
, Spec_Id
);
4265 -- If the body completes a compilation unit which is subject
4266 -- to pragma Elaboration_Checks, set the model specified by
4267 -- the pragma because it applies to all parts of the unit.
4269 Install_Elaboration_Model
(Spec_Id
);
4272 Spec_Id
:= Find_Corresponding_Spec
(N
);
4274 -- A subprogram body is Ghost when it is stand-alone and
4275 -- subject to pragma Ghost or when the corresponding spec is
4276 -- Ghost. Set the mode now to ensure that any nodes generated
4277 -- during analysis and expansion are properly marked as Ghost.
4279 Mark_And_Set_Ghost_Body
(N
, Spec_Id
);
4281 -- If the body completes a compilation unit which is subject
4282 -- to pragma Elaboration_Checks, set the model specified by
4283 -- the pragma because it applies to all parts of the unit.
4285 Install_Elaboration_Model
(Spec_Id
);
4287 -- In GNATprove mode, if the body has no previous spec, create
4288 -- one so that the inlining machinery can operate properly.
4289 -- Transfer aspects, if any, to the new spec, so that they
4290 -- are legal and can be processed ahead of the body.
4291 -- We make two copies of the given spec, one for the new
4292 -- declaration, and one for the body.
4293 -- ??? This should be conditioned on front-end inlining rather
4294 -- than GNATprove_Mode.
4296 if No
(Spec_Id
) and then GNATprove_Mode
4298 -- Inlining does not apply during preanalysis of code
4300 and then Full_Analysis
4302 -- Inlining only applies to full bodies, not stubs
4304 and then Nkind
(N
) /= N_Subprogram_Body_Stub
4306 -- Inlining only applies to bodies in the source code, not to
4307 -- those generated by the compiler. In particular, expression
4308 -- functions, whose body is generated by the compiler, are
4309 -- treated specially by GNATprove.
4311 and then Comes_From_Source
(Body_Id
)
4313 -- This cannot be done for a compilation unit, which is not
4314 -- in a context where we can insert a new spec.
4316 and then Is_List_Member
(N
)
4318 -- Inlining only applies to subprograms without contracts,
4319 -- as a contract is a sign that GNATprove should perform a
4320 -- modular analysis of the subprogram instead of a contextual
4321 -- analysis at each call site. The same test is performed in
4322 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
4323 -- here in another form (because the contract has not been
4324 -- attached to the body) to avoid front-end errors in case
4325 -- pragmas are used instead of aspects, because the
4326 -- corresponding pragmas in the body would not be transferred
4327 -- to the spec, leading to legality errors.
4329 and then not Body_Has_Contract
4330 and then not Inside_A_Generic
4332 Build_Subprogram_Declaration
;
4334 -- If this is a function that returns a constrained array, and
4335 -- Transform_Function_Array is set, create subprogram
4336 -- declaration to simplify e.g. subsequent C generation.
4339 and then Transform_Function_Array
4340 and then Nkind
(Body_Spec
) = N_Function_Specification
4341 and then Is_Array_Type
(Etype
(Body_Id
))
4342 and then Is_Constrained
(Etype
(Body_Id
))
4344 Build_Subprogram_Declaration
;
4348 -- If this is a duplicate body, no point in analyzing it
4350 if Error_Posted
(N
) then
4354 -- A subprogram body should cause freezing of its own declaration,
4355 -- so, if the body and spec are compilation units, we must do it
4356 -- manually here. Moreover, if the return type is anonymous access
4357 -- to protected subprogram, it must be frozen before the body
4358 -- because its expansion has generated an equivalent type that is
4359 -- used when elaborating the body.
4361 if Present
(Spec_Id
)
4362 and then Nkind
(Parent
(N
)) = N_Compilation_Unit
4364 Freeze_Before
(N
, Spec_Id
);
4366 elsif Is_Access_Subprogram_Type
(Etype
(Body_Id
)) then
4367 Freeze_Before
(N
, Etype
(Body_Id
));
4371 Spec_Id
:= Corresponding_Spec
(N
);
4373 -- A subprogram body is Ghost when it is stand-alone and subject
4374 -- to pragma Ghost or when the corresponding spec is Ghost. Set
4375 -- the mode now to ensure that any nodes generated during analysis
4376 -- and expansion are properly marked as Ghost.
4378 Mark_And_Set_Ghost_Body
(N
, Spec_Id
);
4380 -- If the body completes the initial declaration of a compilation
4381 -- unit which is subject to pragma Elaboration_Checks, set the
4382 -- model specified by the pragma because it applies to all parts
4385 Install_Elaboration_Model
(Spec_Id
);
4389 -- Deactivate expansion inside the body of ignored Ghost entities,
4390 -- as this code will ultimately be ignored. This avoids requiring the
4391 -- presence of run-time units which are not needed. Only do this for
4392 -- user entities, as internally generated entitities might still need
4393 -- to be expanded (e.g. those generated for types).
4395 if Present
(Ignored_Ghost_Region
)
4396 and then Comes_From_Source
(Body_Id
)
4398 Expander_Active
:= False;
4401 -- Previously we scanned the body to look for nested subprograms, and
4402 -- rejected an inline directive if nested subprograms were present,
4403 -- because the back-end would generate conflicting symbols for the
4404 -- nested bodies. This is now unnecessary.
4406 -- Look ahead to recognize a pragma Inline that appears after the body
4408 Check_Inline_Pragma
(Spec_Id
);
4410 -- Deal with special case of a fully private operation in the body of
4411 -- the protected type. We must create a declaration for the subprogram,
4412 -- in order to attach the subprogram that will be used in internal
4413 -- calls. We exclude compiler generated bodies from the expander since
4414 -- the issue does not arise for those cases.
4417 and then Comes_From_Source
(N
)
4418 and then Is_Protected_Type
(Current_Scope
)
4420 Spec_Id
:= Build_Internal_Protected_Declaration
(N
);
4423 -- If Transform_Function_Array is set and this is a function returning a
4424 -- constrained array type for which we must create a procedure with an
4425 -- extra out parameter, build and analyze the body now. The procedure
4426 -- declaration has already been created. We reuse the source body of the
4427 -- function, because in an instance it may contain global references
4428 -- that cannot be reanalyzed. The source function itself is not used any
4429 -- further, so we mark it as having a completion. If the subprogram is a
4430 -- stub the transformation is done later, when the proper body is
4434 and then Transform_Function_Array
4435 and then Nkind
(N
) /= N_Subprogram_Body_Stub
4438 S
: constant Entity_Id
:=
4439 (if Present
(Spec_Id
)
4441 else Defining_Unit_Name
(Specification
(N
)));
4442 Proc_Body
: Node_Id
;
4445 if Ekind
(S
) = E_Function
and then Rewritten_For_C
(S
) then
4446 Set_Has_Completion
(S
);
4447 Proc_Body
:= Build_Procedure_Body_Form
(S
, N
);
4449 if Present
(Spec_Id
) then
4450 Rewrite
(N
, Proc_Body
);
4453 -- The entity for the created procedure must remain
4454 -- invisible, so it does not participate in resolution of
4455 -- subsequent references to the function.
4457 Set_Is_Immediately_Visible
(Corresponding_Spec
(N
), False);
4459 -- If we do not have a separate spec for N, build one and
4460 -- insert the new body right after.
4464 Make_Subprogram_Declaration
(Loc
,
4465 Specification
=> Relocate_Node
(Specification
(N
))));
4467 Insert_After_And_Analyze
(N
, Proc_Body
);
4468 Set_Is_Immediately_Visible
4469 (Corresponding_Spec
(Proc_Body
), False);
4477 -- If a separate spec is present, then deal with freezing issues
4479 if Present
(Spec_Id
) then
4480 Spec_Decl
:= Unit_Declaration_Node
(Spec_Id
);
4481 Verify_Overriding_Indicator
;
4483 -- In general, the spec will be frozen when we start analyzing the
4484 -- body. However, for internally generated operations, such as
4485 -- wrapper functions for inherited operations with controlling
4486 -- results, the spec may not have been frozen by the time we expand
4487 -- the freeze actions that include the bodies. In particular, extra
4488 -- formals for accessibility or for return-in-place may need to be
4489 -- generated. Freeze nodes, if any, are inserted before the current
4490 -- body. These freeze actions are also needed in Compile_Only mode to
4491 -- enable the proper back-end type annotations.
4492 -- They are necessary in any case to ensure proper elaboration order
4495 if Nkind
(N
) = N_Subprogram_Body
4496 and then Was_Expression_Function
(N
)
4497 and then not Has_Completion
(Spec_Id
)
4498 and then Serious_Errors_Detected
= 0
4499 and then (Expander_Active
4500 or else Operating_Mode
= Check_Semantics
4501 or else Is_Ignored_Ghost_Entity
(Spec_Id
))
4503 -- The body generated for an expression function that is not a
4504 -- completion is a freeze point neither for the profile nor for
4505 -- anything else. That's why, in order to prevent any freezing
4506 -- during analysis, we need to mask types declared outside the
4507 -- expression (and in an outer scope) that are not yet frozen.
4508 -- This also needs to be done in the case of an ignored Ghost
4509 -- expression function, where the expander isn't active.
4511 -- A further complication arises if the expression function is
4512 -- a primitive operation of a tagged type: in that case the
4513 -- function entity must be frozen before the dispatch table for
4514 -- the type is constructed, so it will be frozen like other local
4515 -- entities, at the end of the current scope.
4517 if not Is_Dispatching_Operation
(Spec_Id
) then
4518 Set_Is_Frozen
(Spec_Id
);
4521 Mask_Types
:= Mask_Unfrozen_Types
(Spec_Id
);
4523 elsif not Is_Frozen
(Spec_Id
)
4524 and then Serious_Errors_Detected
= 0
4526 Set_Has_Delayed_Freeze
(Spec_Id
);
4527 Freeze_Before
(N
, Spec_Id
);
4531 -- Place subprogram on scope stack, and make formals visible. If there
4532 -- is a spec, the visible entity remains that of the spec.
4534 if Present
(Spec_Id
) then
4535 Generate_Reference
(Spec_Id
, Body_Id
, 'b', Set_Ref
=> False);
4537 if Is_Child_Unit
(Spec_Id
) then
4538 Generate_Reference
(Spec_Id
, Scope
(Spec_Id
), 'k', False);
4542 Style
.Check_Identifier
(Body_Id
, Spec_Id
);
4545 Set_Is_Compilation_Unit
(Body_Id
, Is_Compilation_Unit
(Spec_Id
));
4546 Set_Is_Child_Unit
(Body_Id
, Is_Child_Unit
(Spec_Id
));
4548 if Is_Abstract_Subprogram
(Spec_Id
) then
4549 Error_Msg_N
("an abstract subprogram cannot have a body", N
);
4553 Set_Convention
(Body_Id
, Convention
(Spec_Id
));
4554 Set_Has_Completion
(Spec_Id
);
4556 if Is_Protected_Type
(Scope
(Spec_Id
)) then
4557 Prot_Typ
:= Scope
(Spec_Id
);
4560 -- If this is a body generated for a renaming, do not check for
4561 -- full conformance. The check is redundant, because the spec of
4562 -- the body is a copy of the spec in the renaming declaration,
4563 -- and the test can lead to spurious errors on nested defaults.
4565 if Present
(Spec_Decl
)
4566 and then not Comes_From_Source
(N
)
4568 (Nkind
(Original_Node
(Spec_Decl
)) =
4569 N_Subprogram_Renaming_Declaration
4570 or else (Present
(Corresponding_Body
(Spec_Decl
))
4572 Nkind
(Unit_Declaration_Node
4573 (Corresponding_Body
(Spec_Decl
))) =
4574 N_Subprogram_Renaming_Declaration
))
4578 -- Conversely, the spec may have been generated for specless body
4579 -- with an inline pragma. The entity comes from source, which is
4580 -- both semantically correct and necessary for proper inlining.
4581 -- The subprogram declaration itself is not in the source.
4583 elsif Comes_From_Source
(N
)
4584 and then Present
(Spec_Decl
)
4585 and then not Comes_From_Source
(Spec_Decl
)
4586 and then Has_Pragma_Inline
(Spec_Id
)
4590 -- Finally, a body generated for an expression function copies
4591 -- the profile of the function and no check is needed either.
4592 -- If the body is the completion of a previous function
4593 -- declared elsewhere, the conformance check is required.
4595 elsif Nkind
(N
) = N_Subprogram_Body
4596 and then Was_Expression_Function
(N
)
4597 and then Sloc
(Spec_Id
) = Sloc
(Body_Id
)
4604 Fully_Conformant
, True, Conformant
, Body_Id
);
4607 -- If the body is not fully conformant, we have to decide if we
4608 -- should analyze it or not. If it has a really messed up profile
4609 -- then we probably should not analyze it, since we will get too
4610 -- many bogus messages.
4612 -- Our decision is to go ahead in the non-fully conformant case
4613 -- only if it is at least mode conformant with the spec. Note
4614 -- that the call to Check_Fully_Conformant has issued the proper
4615 -- error messages to complain about the lack of conformance.
4618 and then not Mode_Conformant
(Body_Id
, Spec_Id
)
4624 -- In the case we are dealing with an expression function we check
4625 -- the formals attached to the spec instead of the body - so we don't
4626 -- reference body formals.
4628 if Spec_Id
/= Body_Id
4629 and then not Is_Expression_Function
(Spec_Id
)
4631 Reference_Body_Formals
(Spec_Id
, Body_Id
);
4634 Reinit_Field_To_Zero
(Body_Id
, F_Has_Out_Or_In_Out_Parameter
);
4635 Reinit_Field_To_Zero
(Body_Id
, F_Needs_No_Actuals
,
4636 Old_Ekind
=> (E_Function | E_Procedure
=> True, others => False));
4637 Reinit_Field_To_Zero
(Body_Id
, F_Is_Predicate_Function
,
4638 Old_Ekind
=> (E_Function | E_Procedure
=> True, others => False));
4639 Reinit_Field_To_Zero
(Body_Id
, F_Protected_Subprogram
,
4640 Old_Ekind
=> (E_Function | E_Procedure
=> True, others => False));
4642 if Ekind
(Body_Id
) = E_Procedure
then
4643 Reinit_Field_To_Zero
(Body_Id
, F_Receiving_Entry
);
4646 Mutate_Ekind
(Body_Id
, E_Subprogram_Body
);
4648 if Nkind
(N
) = N_Subprogram_Body_Stub
then
4649 Set_Corresponding_Spec_Of_Stub
(N
, Spec_Id
);
4654 Set_Corresponding_Spec
(N
, Spec_Id
);
4656 -- Ada 2005 (AI-345): If the operation is a primitive operation
4657 -- of a concurrent type, the type of the first parameter has been
4658 -- replaced with the corresponding record, which is the proper
4659 -- run-time structure to use. However, within the body there may
4660 -- be uses of the formals that depend on primitive operations
4661 -- of the type (in particular calls in prefixed form) for which
4662 -- we need the original concurrent type. The operation may have
4663 -- several controlling formals, so the replacement must be done
4666 if Comes_From_Source
(Spec_Id
)
4667 and then Present
(First_Entity
(Spec_Id
))
4668 and then Ekind
(Etype
(First_Entity
(Spec_Id
))) = E_Record_Type
4669 and then Is_Tagged_Type
(Etype
(First_Entity
(Spec_Id
)))
4670 and then Present
(Interfaces
(Etype
(First_Entity
(Spec_Id
))))
4671 and then Present
(Corresponding_Concurrent_Type
4672 (Etype
(First_Entity
(Spec_Id
))))
4675 Typ
: constant Entity_Id
:= Etype
(First_Entity
(Spec_Id
));
4679 Form
:= First_Formal
(Spec_Id
);
4680 while Present
(Form
) loop
4681 if Etype
(Form
) = Typ
then
4682 Set_Etype
(Form
, Corresponding_Concurrent_Type
(Typ
));
4690 -- Make the formals visible, and place subprogram on scope stack.
4691 -- This is also the point at which we set Last_Real_Spec_Entity
4692 -- to mark the entities which will not be moved to the body.
4694 Install_Formals
(Spec_Id
);
4695 Last_Real_Spec_Entity
:= Last_Entity
(Spec_Id
);
4697 -- Within an instance, add local renaming declarations so that
4698 -- gdb can retrieve the values of actuals more easily. This is
4699 -- only relevant if generating code.
4701 if Is_Generic_Instance
(Spec_Id
)
4702 and then Is_Wrapper_Package
(Current_Scope
)
4703 and then Expander_Active
4705 Build_Subprogram_Instance_Renamings
(N
, Current_Scope
);
4708 Push_Scope
(Spec_Id
);
4710 -- Make sure that the subprogram is immediately visible. For
4711 -- child units that have no separate spec this is indispensable.
4712 -- Otherwise it is safe albeit redundant.
4714 Set_Is_Immediately_Visible
(Spec_Id
);
4717 Set_Corresponding_Body
(Unit_Declaration_Node
(Spec_Id
), Body_Id
);
4718 Set_Is_Obsolescent
(Body_Id
, Is_Obsolescent
(Spec_Id
));
4719 Set_Scope
(Body_Id
, Scope
(Spec_Id
));
4721 -- Case of subprogram body with no previous spec
4724 -- Check for style warning required
4728 -- Only apply check for source level subprograms for which checks
4729 -- have not been suppressed.
4731 and then Comes_From_Source
(Body_Id
)
4732 and then not Suppress_Style_Checks
(Body_Id
)
4734 -- No warnings within an instance
4736 and then not In_Instance
4738 -- No warnings for expression functions
4740 and then (Nkind
(N
) /= N_Subprogram_Body
4741 or else not Was_Expression_Function
(N
))
4743 Style
.Body_With_No_Spec
(N
);
4746 -- First set Acts_As_Spec if appropriate
4748 if Nkind
(N
) /= N_Subprogram_Body_Stub
then
4749 Set_Acts_As_Spec
(N
);
4752 New_Overloaded_Entity
(Body_Id
);
4754 -- A subprogram body should cause freezing of its own declaration,
4755 -- but if there was no previous explicit declaration, then the
4756 -- subprogram will get frozen too late (there may be code within
4757 -- the body that depends on the subprogram having been frozen,
4758 -- such as uses of extra formals), so we force it to be frozen here.
4759 -- An exception in Ada 2012 is that the body created for expression
4760 -- functions does not freeze.
4762 if Nkind
(N
) /= N_Subprogram_Body
4763 or else not Was_Expression_Function
(N
)
4765 -- First clear the Is_Public flag on thunks since they are only
4766 -- referenced locally by dispatch tables and thus never inlined.
4768 if Is_Thunk
(Body_Id
) then
4769 Set_Is_Public
(Body_Id
, False);
4772 Freeze_Before
(N
, Body_Id
);
4775 if Nkind
(N
) /= N_Subprogram_Body_Stub
then
4776 Generate_Definition
(Body_Id
);
4778 (Body_Id
, Body_Id
, 'b', Set_Ref
=> False, Force
=> True);
4780 -- If the body is an entry wrapper created for an entry with
4781 -- preconditions, it must be compiled in the context of the
4782 -- enclosing synchronized object, because it may mention other
4783 -- operations of the type.
4785 if Is_Entry_Wrapper
(Body_Id
) then
4787 Prot
: constant Entity_Id
:= Etype
(First_Entity
(Body_Id
));
4790 Install_Declarations
(Prot
);
4794 Install_Formals
(Body_Id
);
4796 Push_Scope
(Body_Id
);
4799 -- For stubs and bodies with no previous spec, generate references to
4802 Generate_Reference_To_Formals
(Body_Id
);
4805 -- Entry barrier functions are generated outside the protected type and
4806 -- should not carry the SPARK_Mode of the enclosing context.
4808 if Nkind
(N
) = N_Subprogram_Body
4809 and then Is_Entry_Barrier_Function
(N
)
4813 -- The body is generated as part of expression function expansion. When
4814 -- the expression function appears in the visible declarations of a
4815 -- package, the body is added to the private declarations. Since both
4816 -- declarative lists may be subject to a different SPARK_Mode, inherit
4817 -- the mode of the spec.
4819 -- package P with SPARK_Mode is
4820 -- function Expr_Func ... is (...); -- original
4821 -- [function Expr_Func ...;] -- generated spec
4824 -- pragma SPARK_Mode (Off);
4825 -- [function Expr_Func ... is return ...;] -- generated body
4826 -- end P; -- mode is ON
4828 elsif not Comes_From_Source
(N
)
4829 and then Present
(Spec_Id
)
4830 and then Is_Expression_Function
(Spec_Id
)
4832 Set_SPARK_Pragma
(Body_Id
, SPARK_Pragma
(Spec_Id
));
4833 Set_SPARK_Pragma_Inherited
4834 (Body_Id
, SPARK_Pragma_Inherited
(Spec_Id
));
4836 -- Set the SPARK_Mode from the current context (may be overwritten later
4837 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
4838 -- initially on a stand-alone subprogram body, but is then relocated to
4839 -- a generated corresponding spec. In this scenario the mode is shared
4840 -- between the spec and body.
4842 elsif No
(SPARK_Pragma
(Body_Id
)) then
4843 Set_SPARK_Pragma
(Body_Id
, SPARK_Mode_Pragma
);
4844 Set_SPARK_Pragma_Inherited
(Body_Id
);
4847 -- A subprogram body may be instantiated or inlined at a later pass.
4848 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
4849 -- applied to the initial declaration of the body.
4851 if Present
(Spec_Id
) then
4852 if Ignore_SPARK_Mode_Pragmas
(Spec_Id
) then
4853 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
4857 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
4858 -- case the body is instantiated or inlined later and out of context.
4859 -- The body uses this attribute to restore the value of the global
4862 if Ignore_SPARK_Mode_Pragmas_In_Instance
then
4863 Set_Ignore_SPARK_Mode_Pragmas
(Body_Id
);
4865 elsif Ignore_SPARK_Mode_Pragmas
(Body_Id
) then
4866 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
4870 -- Preserve relevant elaboration-related attributes of the context which
4871 -- are no longer available or very expensive to recompute once analysis,
4872 -- resolution, and expansion are over.
4874 if No
(Spec_Id
) then
4875 Mark_Elaboration_Attributes
4881 -- If this is the proper body of a stub, we must verify that the stub
4882 -- conforms to the body, and to the previous spec if one was present.
4883 -- We know already that the body conforms to that spec. This test is
4884 -- only required for subprograms that come from source.
4886 if Nkind
(Parent
(N
)) = N_Subunit
4887 and then Comes_From_Source
(N
)
4888 and then not Error_Posted
(Body_Id
)
4889 and then Nkind
(Corresponding_Stub
(Parent
(N
))) =
4890 N_Subprogram_Body_Stub
4893 Old_Id
: constant Entity_Id
:=
4895 (Specification
(Corresponding_Stub
(Parent
(N
))));
4897 Conformant
: Boolean := False;
4900 if No
(Spec_Id
) then
4901 Check_Fully_Conformant
(Body_Id
, Old_Id
);
4905 (Body_Id
, Old_Id
, Fully_Conformant
, False, Conformant
);
4907 if not Conformant
then
4909 -- The stub was taken to be a new declaration. Indicate that
4912 Set_Has_Completion
(Old_Id
, False);
4918 Set_Has_Completion
(Body_Id
);
4919 Check_Eliminated
(Body_Id
);
4921 -- Analyze any aspect specifications that appear on the subprogram body
4922 -- stub. Stop the analysis now as the stub does not have a declarative
4923 -- or a statement part, and it cannot be inlined.
4925 if Nkind
(N
) = N_Subprogram_Body_Stub
then
4926 if Has_Aspects
(N
) then
4927 Analyze_Aspects_On_Subprogram_Body_Or_Stub
(N
);
4936 and then Serious_Errors_Detected
= 0
4937 and then Present
(Spec_Id
)
4938 and then Has_Pragma_Inline
(Spec_Id
)
4940 -- Legacy implementation (relying on front-end inlining)
4942 if not Back_End_Inlining
then
4943 if Has_Pragma_Inline_Always
(Spec_Id
)
4944 or else (Front_End_Inlining
4945 and then not Opt
.Disable_FE_Inline
)
4947 Build_Body_To_Inline
(N
, Spec_Id
);
4950 -- New implementation (relying on back-end inlining)
4953 if Has_Pragma_Inline_Always
(Spec_Id
)
4954 or else Optimization_Level
> 0
4956 -- Handle function returning an unconstrained type
4958 if Comes_From_Source
(Body_Id
)
4959 and then Ekind
(Spec_Id
) = E_Function
4960 and then Returns_Unconstrained_Type
(Spec_Id
)
4962 -- If function builds in place, i.e. returns a limited type,
4963 -- inlining cannot be done.
4965 and then not Is_Limited_Type
(Etype
(Spec_Id
))
4967 Check_And_Split_Unconstrained_Function
(N
, Spec_Id
, Body_Id
);
4971 Subp_Body
: constant Node_Id
:=
4972 Unit_Declaration_Node
(Body_Id
);
4973 Subp_Decl
: constant List_Id
:= Declarations
(Subp_Body
);
4976 -- Do not pass inlining to the backend if the subprogram
4977 -- has declarations or statements which cannot be inlined
4978 -- by the backend. This check is done here to emit an
4979 -- error instead of the generic warning message reported
4980 -- by the GCC backend (ie. "function might not be
4983 if Has_Excluded_Declaration
(Spec_Id
, Subp_Decl
) then
4986 elsif Has_Excluded_Statement
4989 (Handled_Statement_Sequence
(Subp_Body
)))
4993 -- If the backend inlining is available then at this
4994 -- stage we only have to mark the subprogram as inlined.
4995 -- The expander will take care of registering it in the
4996 -- table of subprograms inlined by the backend a part of
4997 -- processing calls to it (cf. Expand_Call)
5000 Set_Is_Inlined
(Spec_Id
);
5007 -- In GNATprove mode, inline only when there is a separate subprogram
5008 -- declaration for now, as inlining of subprogram bodies acting as
5009 -- declarations, or subprogram stubs, are not supported by front-end
5010 -- inlining. This inlining should occur after analysis of the body, so
5011 -- that it is known whether the value of SPARK_Mode, which can be
5012 -- defined by a pragma inside the body, is applicable to the body.
5013 -- Inlining can be disabled with switch -gnatdm
5015 elsif GNATprove_Mode
5016 and then Full_Analysis
5017 and then not Inside_A_Generic
5018 and then Present
(Spec_Id
)
5020 Nkind
(Unit_Declaration_Node
(Spec_Id
)) = N_Subprogram_Declaration
5021 and then Body_Has_SPARK_Mode_On
5022 and then Can_Be_Inlined_In_GNATprove_Mode
(Spec_Id
, Body_Id
)
5023 and then not Body_Has_Contract
5024 and then not Debug_Flag_M
5026 Build_Body_To_Inline
(N
, Spec_Id
);
5029 -- When generating code, inherited pre/postconditions are handled when
5030 -- expanding the corresponding contract.
5032 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
5033 -- of the specification we have to install the private withed units.
5034 -- This holds for child units as well.
5036 if Is_Compilation_Unit
(Body_Id
)
5037 or else Nkind
(Parent
(N
)) = N_Compilation_Unit
5039 Install_Private_With_Clauses
(Body_Id
);
5042 Check_Anonymous_Return
;
5044 -- Set the Protected_Formal field of each extra formal of the protected
5045 -- subprogram to reference the corresponding extra formal of the
5046 -- subprogram that implements it. For regular formals this occurs when
5047 -- the protected subprogram's declaration is expanded, but the extra
5048 -- formals don't get created until the subprogram is frozen. We need to
5049 -- do this before analyzing the protected subprogram's body so that any
5050 -- references to the original subprogram's extra formals will be changed
5051 -- refer to the implementing subprogram's formals (see Expand_Formal).
5053 if Present
(Spec_Id
)
5054 and then Is_Protected_Type
(Scope
(Spec_Id
))
5055 and then Present
(Protected_Body_Subprogram
(Spec_Id
))
5058 Impl_Subp
: constant Entity_Id
:=
5059 Protected_Body_Subprogram
(Spec_Id
);
5060 Prot_Ext_Formal
: Entity_Id
:= Extra_Formals
(Spec_Id
);
5061 Impl_Ext_Formal
: Entity_Id
:= Extra_Formals
(Impl_Subp
);
5064 while Present
(Prot_Ext_Formal
) loop
5065 pragma Assert
(Present
(Impl_Ext_Formal
));
5066 Set_Protected_Formal
(Prot_Ext_Formal
, Impl_Ext_Formal
);
5067 Next_Formal_With_Extras
(Prot_Ext_Formal
);
5068 Next_Formal_With_Extras
(Impl_Ext_Formal
);
5073 -- Generate minimum accessibility local objects to correspond with
5074 -- any extra formal added for anonymous access types. This new local
5075 -- object can then be used instead of the formal in case it is used
5076 -- in an actual to a call to a nested subprogram.
5078 -- This method is used to supplement our "small integer model" for
5079 -- accessibility-check generation (for more information see
5080 -- Accessibility_Level).
5082 -- Because we allow accessibility values greater than our expected value
5083 -- passing along the same extra accessibility formal as an actual
5084 -- to a nested subprogram becomes a problem because high values mean
5085 -- different things to the callee even though they are the same to the
5086 -- caller. So, as described in the first section, we create a local
5087 -- object representing the minimum of the accessibility level value that
5088 -- is passed in and the accessibility level of the callee's parameter
5089 -- and locals and use it in the case of a call to a nested subprogram.
5090 -- This generated object is referred to as a "minimum accessibility
5093 if Present
(Spec_Id
) or else Present
(Body_Id
) then
5094 Body_Nod
:= Unit_Declaration_Node
(Body_Id
);
5099 -- Grab the appropriate formal depending on whether there exists
5100 -- an actual spec for the subprogram or whether we are dealing
5101 -- with a protected subprogram.
5103 if Present
(Spec_Id
) then
5104 if Present
(Protected_Body_Subprogram
(Spec_Id
)) then
5105 Form
:= First_Formal
(Protected_Body_Subprogram
(Spec_Id
));
5107 Form
:= First_Formal
(Spec_Id
);
5110 Form
:= First_Formal
(Body_Id
);
5113 -- Loop through formals if the subprogram is capable of accepting
5114 -- a generated local object. If it is not then it is also not
5115 -- capable of having local subprograms meaning it would not need
5116 -- a minimum accessibility level object anyway.
5118 if Present
(Body_Nod
)
5119 and then Has_Declarations
(Body_Nod
)
5120 and then Nkind
(Body_Nod
) /= N_Package_Specification
5122 while Present
(Form
) loop
5124 if Present
(Extra_Accessibility
(Form
))
5125 and then No
(Minimum_Accessibility
(Form
))
5127 -- Generate the minimum accessibility level object
5129 -- A60b : constant natural := natural'min(1, paramL);
5131 Generate_Minimum_Accessibility
5132 (Extra_Accessibility
(Form
), Form
);
5138 -- Generate the minimum accessibility level object for the
5139 -- function's Extra_Accessibility_Of_Result.
5141 -- A31b : constant natural := natural'min (2, funcL);
5143 if Ekind
(Body_Id
) = E_Function
5144 and then Present
(Extra_Accessibility_Of_Result
(Body_Id
))
5146 Generate_Minimum_Accessibility
5147 (Extra_Accessibility_Of_Result
(Body_Id
));
5149 -- Replace the Extra_Accessibility_Of_Result with the new
5150 -- minimum accessibility object.
5152 Set_Extra_Accessibility_Of_Result
5153 (Body_Id
, Minimum_Accessibility
5154 (Extra_Accessibility_Of_Result
(Body_Id
)));
5160 -- Now we can go on to analyze the body
5162 HSS
:= Handled_Statement_Sequence
(N
);
5163 Set_Actual_Subtypes
(N
, Current_Scope
);
5165 -- Add a declaration for the Protection object, renaming declarations
5166 -- for discriminals and privals and finally a declaration for the entry
5167 -- family index (if applicable). This form of early expansion is done
5168 -- when the Expander is active because Install_Private_Data_Declarations
5169 -- references entities which were created during regular expansion. The
5170 -- subprogram entity must come from source, and not be an internally
5171 -- generated subprogram.
5174 and then Present
(Prot_Typ
)
5175 and then Present
(Spec_Id
)
5176 and then Comes_From_Source
(Spec_Id
)
5177 and then not Is_Eliminated
(Spec_Id
)
5179 Install_Private_Data_Declarations
5180 (Sloc
(N
), Spec_Id
, Prot_Typ
, N
, Declarations
(N
));
5183 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
5184 -- may now appear in parameter and result profiles. Since the analysis
5185 -- of a subprogram body may use the parameter and result profile of the
5186 -- spec, swap any limited views with their non-limited counterpart.
5188 if Ada_Version
>= Ada_2012
and then Present
(Spec_Id
) then
5189 Exch_Views
:= Exchange_Limited_Views
(Spec_Id
);
5192 -- If the return type is an anonymous access type whose designated type
5193 -- is the limited view of a class-wide type and the non-limited view is
5194 -- available, update the return type accordingly.
5196 if Ada_Version
>= Ada_2005
5197 and then Present
(Spec_Id
)
5198 and then Ekind
(Etype
(Spec_Id
)) = E_Anonymous_Access_Type
5204 Etyp
:= Directly_Designated_Type
(Etype
(Spec_Id
));
5206 if Is_Class_Wide_Type
(Etyp
)
5207 and then From_Limited_With
(Etyp
)
5208 and then Has_Non_Limited_View
(Etyp
)
5211 Etyp
:= Non_Limited_View
(Etyp
);
5213 -- If the class-wide type has been created by the completion of
5214 -- an incomplete tagged type declaration, get the class-wide
5215 -- type of the incomplete tagged type to match Find_Type_Name.
5217 if Nkind
(Parent
(Etyp
)) = N_Full_Type_Declaration
5218 and then Present
(Incomplete_View
(Parent
(Etyp
)))
5220 Etyp
:= Class_Wide_Type
(Incomplete_View
(Parent
(Etyp
)));
5223 Set_Directly_Designated_Type
(Etype
(Spec_Id
), Etyp
);
5228 -- Analyze any aspect specifications that appear on the subprogram body
5230 if Has_Aspects
(N
) then
5231 Analyze_Aspects_On_Subprogram_Body_Or_Stub
(N
);
5234 Analyze_Declarations
(Declarations
(N
));
5236 -- Verify that the SPARK_Mode of the body agrees with that of its spec
5238 if Present
(Spec_Id
) and then Present
(SPARK_Pragma
(Body_Id
)) then
5239 if Present
(SPARK_Pragma
(Spec_Id
)) then
5240 if Get_SPARK_Mode_From_Annotation
(SPARK_Pragma
(Spec_Id
)) = Off
5242 Get_SPARK_Mode_From_Annotation
(SPARK_Pragma
(Body_Id
)) = On
5244 Error_Msg_Sloc
:= Sloc
(SPARK_Pragma
(Body_Id
));
5245 Error_Msg_N
("incorrect application of SPARK_Mode#", N
);
5246 Error_Msg_Sloc
:= Sloc
(SPARK_Pragma
(Spec_Id
));
5248 ("\value Off was set for SPARK_Mode on & #", N
, Spec_Id
);
5251 elsif Nkind
(Parent
(Parent
(Spec_Id
))) = N_Subprogram_Body_Stub
then
5254 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
5255 -- as specified in source code, or because SPARK_Mode On is ignored
5256 -- in an instance where the context is SPARK_Mode Off/Auto.
5258 elsif Get_SPARK_Mode_From_Annotation
(SPARK_Pragma
(Body_Id
)) = Off
5259 and then (Is_Generic_Unit
(Spec_Id
) or else In_Instance
)
5264 Error_Msg_Sloc
:= Sloc
(SPARK_Pragma
(Body_Id
));
5265 Error_Msg_N
("incorrect application of SPARK_Mode #", N
);
5266 Error_Msg_Sloc
:= Sloc
(Spec_Id
);
5268 ("\no value was set for SPARK_Mode on & #", N
, Spec_Id
);
5272 -- A subprogram body freezes its own contract. Analyze the contract
5273 -- after the declarations of the body have been processed as pragmas
5274 -- are now chained on the contract of the subprogram body.
5276 Analyze_Entry_Or_Subprogram_Body_Contract
(Body_Id
);
5278 -- Check completion, and analyze the statements
5281 Inspect_Deferred_Constant_Completion
(Declarations
(N
));
5284 -- Add the generated minimum accessibility objects to the subprogram
5285 -- body's list of declarations after analysis of the statements and
5288 while Is_Non_Empty_List
(Minimum_Acc_Objs
) loop
5289 if Present
(Declarations
(Body_Nod
)) then
5290 Prepend
(Remove_Head
(Minimum_Acc_Objs
), Declarations
(Body_Nod
));
5293 (Body_Nod
, New_List
(Remove_Head
(Minimum_Acc_Objs
)));
5297 -- Deal with end of scope processing for the body
5299 Process_End_Label
(HSS
, 't', Current_Scope
);
5300 Update_Use_Clause_Chain
;
5303 -- If we are compiling an entry wrapper, remove the enclosing
5304 -- synchronized object from the stack.
5306 if Is_Entry_Wrapper
(Body_Id
) then
5310 Check_Subprogram_Order
(N
);
5311 Set_Analyzed
(Body_Id
);
5313 -- If we have a separate spec, then the analysis of the declarations
5314 -- caused the entities in the body to be chained to the spec id, but
5315 -- we want them chained to the body id. Only the formal parameters
5316 -- end up chained to the spec id in this case.
5318 if Present
(Spec_Id
) then
5320 -- We must conform to the categorization of our spec
5322 Validate_Categorization_Dependency
(N
, Spec_Id
);
5324 -- And if this is a child unit, the parent units must conform
5326 if Is_Child_Unit
(Spec_Id
) then
5327 Validate_Categorization_Dependency
5328 (Unit_Declaration_Node
(Spec_Id
), Spec_Id
);
5331 -- Here is where we move entities from the spec to the body
5333 -- Case where there are entities that stay with the spec
5335 if Present
(Last_Real_Spec_Entity
) then
5337 -- No body entities (happens when the only real spec entities come
5338 -- from precondition and postcondition pragmas).
5340 if No
(Last_Entity
(Body_Id
)) then
5341 Set_First_Entity
(Body_Id
, Next_Entity
(Last_Real_Spec_Entity
));
5343 -- Body entities present (formals), so chain stuff past them
5347 (Last_Entity
(Body_Id
), Next_Entity
(Last_Real_Spec_Entity
));
5350 Set_Next_Entity
(Last_Real_Spec_Entity
, Empty
);
5351 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
5352 Set_Last_Entity
(Spec_Id
, Last_Real_Spec_Entity
);
5354 -- Case where there are no spec entities, in this case there can be
5355 -- no body entities either, so just move everything.
5357 -- If the body is generated for an expression function, it may have
5358 -- been preanalyzed already, if 'access was applied to it.
5361 if Nkind
(Original_Node
(Unit_Declaration_Node
(Spec_Id
))) /=
5362 N_Expression_Function
5364 pragma Assert
(No
(Last_Entity
(Body_Id
)));
5368 Set_First_Entity
(Body_Id
, First_Entity
(Spec_Id
));
5369 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
5370 Set_First_Entity
(Spec_Id
, Empty
);
5371 Set_Last_Entity
(Spec_Id
, Empty
);
5374 -- Otherwise the body does not complete a previous declaration. Check
5375 -- the categorization of the body against the units it withs.
5378 Validate_Categorization_Dependency
(N
, Body_Id
);
5381 Check_Missing_Return
;
5383 -- Now we are going to check for variables that are never modified in
5384 -- the body of the procedure. But first we deal with a special case
5385 -- where we want to modify this check. If the body of the subprogram
5386 -- starts with a raise statement or its equivalent, or if the body
5387 -- consists entirely of a null statement, then it is pretty obvious that
5388 -- it is OK to not reference the parameters. For example, this might be
5389 -- the following common idiom for a stubbed function: statement of the
5390 -- procedure raises an exception. In particular this deals with the
5391 -- common idiom of a stubbed function, which appears something like:
5393 -- function F (A : Integer) return Some_Type;
5396 -- raise Program_Error;
5400 -- Here the purpose of X is simply to satisfy the annoying requirement
5401 -- in Ada that there be at least one return, and we certainly do not
5402 -- want to go posting warnings on X that it is not initialized. On
5403 -- the other hand, if X is entirely unreferenced that should still
5406 -- What we do is to detect these cases, and if we find them, flag the
5407 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
5408 -- suppress unwanted warnings. For the case of the function stub above
5409 -- we have a special test to set X as apparently assigned to suppress
5413 Stm
: Node_Id
:= First
(Statements
(HSS
));
5415 -- Skip call markers installed by the ABE mechanism, labels, and
5416 -- Push_xxx_Error_Label to find the first real statement.
5418 while Nkind
(Stm
) in N_Call_Marker | N_Label | N_Push_xxx_Label
loop
5422 -- Do the test on the original statement before expansion
5425 Ostm
: constant Node_Id
:= Original_Node
(Stm
);
5428 -- If explicit raise statement, turn on flag
5430 if Nkind
(Ostm
) = N_Raise_Statement
then
5431 Set_Trivial_Subprogram
(Stm
);
5433 -- If null statement, and no following statements, turn on flag
5435 elsif Nkind
(Stm
) = N_Null_Statement
5436 and then Comes_From_Source
(Stm
)
5437 and then No
(Next
(Stm
))
5439 Set_Trivial_Subprogram
(Stm
);
5441 -- Check for explicit call cases which likely raise an exception
5443 elsif Nkind
(Ostm
) = N_Procedure_Call_Statement
then
5444 if Is_Entity_Name
(Name
(Ostm
)) then
5446 Ent
: constant Entity_Id
:= Entity
(Name
(Ostm
));
5449 -- If the procedure is marked No_Return, then likely it
5450 -- raises an exception, but in any case it is not coming
5451 -- back here, so turn on the flag.
5454 and then Ekind
(Ent
) = E_Procedure
5455 and then No_Return
(Ent
)
5457 Set_Trivial_Subprogram
(Stm
);
5465 -- Check if a Body_To_Inline was created, but the subprogram has
5466 -- references to object renamings which will be replaced by the special
5467 -- SPARK expansion into nodes of a different kind, which is not expected
5468 -- by the inlining mechanism. In that case, the Body_To_Inline is
5469 -- deleted prior to being analyzed. This check needs to take place
5470 -- after analysis of the subprogram body.
5473 and then Present
(Spec_Id
)
5475 Nkind
(Unit_Declaration_Node
(Spec_Id
)) = N_Subprogram_Declaration
5476 and then Present
(Body_To_Inline
(Unit_Declaration_Node
(Spec_Id
)))
5478 Check_Object_Renaming_In_GNATprove_Mode
(Spec_Id
);
5481 -- Check for variables that are never modified
5488 -- If there is a separate spec, then transfer Never_Set_In_Source
5489 -- flags from out parameters to the corresponding entities in the
5490 -- body. The reason we do that is we want to post error flags on
5491 -- the body entities, not the spec entities.
5493 if Present
(Spec_Id
) then
5494 F1
:= First_Formal
(Spec_Id
);
5495 while Present
(F1
) loop
5496 if Ekind
(F1
) = E_Out_Parameter
then
5497 F2
:= First_Formal
(Body_Id
);
5498 while Present
(F2
) loop
5499 exit when Chars
(F1
) = Chars
(F2
);
5503 if Present
(F2
) then
5504 Set_Never_Set_In_Source
(F2
, Never_Set_In_Source
(F1
));
5512 -- Check references of the subprogram spec when we are dealing with
5513 -- an expression function due to it having a generated body.
5514 -- Otherwise, we simply check the formals of the subprogram body.
5516 if Present
(Spec_Id
)
5517 and then Is_Expression_Function
(Spec_Id
)
5519 Check_References
(Spec_Id
);
5521 Check_References
(Body_Id
);
5525 -- Check for nested subprogram, and mark outer level subprogram if so
5531 if Present
(Spec_Id
) then
5538 Ent
:= Enclosing_Subprogram
(Ent
);
5539 exit when No
(Ent
) or else Is_Subprogram
(Ent
);
5542 if Present
(Ent
) then
5543 Set_Has_Nested_Subprogram
(Ent
);
5547 -- Restore the limited views in the spec, if any, to let the back end
5548 -- process it without running into circularities.
5550 if Present
(Exch_Views
) then
5551 Restore_Limited_Views
(Exch_Views
);
5554 if Present
(Mask_Types
) then
5555 Unmask_Unfrozen_Types
(Mask_Types
);
5558 if Present
(Desig_View
) then
5559 Set_Directly_Designated_Type
(Etype
(Spec_Id
), Desig_View
);
5563 if Present
(Ignored_Ghost_Region
) then
5564 Expander_Active
:= Saved_EA
;
5567 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
5568 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
5569 end Analyze_Subprogram_Body_Helper
;
5571 ------------------------------------
5572 -- Analyze_Subprogram_Declaration --
5573 ------------------------------------
5575 procedure Analyze_Subprogram_Declaration
(N
: Node_Id
) is
5576 Scop
: constant Entity_Id
:= Current_Scope
;
5577 Designator
: Entity_Id
;
5579 Is_Completion
: Boolean;
5580 -- Indicates whether a null procedure declaration is a completion
5583 -- Null procedures are not allowed in SPARK
5585 if Nkind
(Specification
(N
)) = N_Procedure_Specification
5586 and then Null_Present
(Specification
(N
))
5588 -- Null procedures are allowed in protected types, following the
5589 -- recent AI12-0147.
5591 if Is_Protected_Type
(Current_Scope
)
5592 and then Ada_Version
< Ada_2012
5594 Error_Msg_N
("protected operation cannot be a null procedure", N
);
5597 Analyze_Null_Procedure
(N
, Is_Completion
);
5599 -- The null procedure acts as a body, nothing further is needed
5601 if Is_Completion
then
5606 Designator
:= Analyze_Subprogram_Specification
(Specification
(N
));
5608 -- A reference may already have been generated for the unit name, in
5609 -- which case the following call is redundant. However it is needed for
5610 -- declarations that are the rewriting of an expression function.
5612 Generate_Definition
(Designator
);
5614 -- Set the SPARK mode from the current context (may be overwritten later
5615 -- with explicit pragma). This is not done for entry barrier functions
5616 -- because they are generated outside the protected type and should not
5617 -- carry the mode of the enclosing context.
5619 if Nkind
(N
) = N_Subprogram_Declaration
5620 and then Is_Entry_Barrier_Function
(N
)
5625 Set_SPARK_Pragma
(Designator
, SPARK_Mode_Pragma
);
5626 Set_SPARK_Pragma_Inherited
(Designator
);
5629 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
5630 -- the body of this subprogram is instantiated or inlined later and out
5631 -- of context. The body uses this attribute to restore the value of the
5634 if Ignore_SPARK_Mode_Pragmas_In_Instance
then
5635 Set_Ignore_SPARK_Mode_Pragmas
(Designator
);
5638 -- Preserve relevant elaboration-related attributes of the context which
5639 -- are no longer available or very expensive to recompute once analysis,
5640 -- resolution, and expansion are over.
5642 Mark_Elaboration_Attributes
5643 (N_Id
=> Designator
,
5647 if Debug_Flag_C
then
5648 Write_Str
("==> subprogram spec ");
5649 Write_Name
(Chars
(Designator
));
5650 Write_Str
(" from ");
5651 Write_Location
(Sloc
(N
));
5656 Validate_RCI_Subprogram_Declaration
(N
);
5657 New_Overloaded_Entity
(Designator
);
5658 Check_Delayed_Subprogram
(Designator
);
5660 -- If the type of the first formal of the current subprogram is a non-
5661 -- generic tagged private type, mark the subprogram as being a private
5662 -- primitive. Ditto if this is a function with controlling result, and
5663 -- the return type is currently private. In both cases, the type of the
5664 -- controlling argument or result must be in the current scope for the
5665 -- operation to be primitive.
5667 if Has_Controlling_Result
(Designator
)
5668 and then Is_Private_Type
(Etype
(Designator
))
5669 and then Scope
(Etype
(Designator
)) = Current_Scope
5670 and then not Is_Generic_Actual_Type
(Etype
(Designator
))
5672 Set_Is_Private_Primitive
(Designator
);
5674 elsif Present
(First_Formal
(Designator
)) then
5676 Formal_Typ
: constant Entity_Id
:=
5677 Etype
(First_Formal
(Designator
));
5679 Set_Is_Private_Primitive
(Designator
,
5680 Is_Tagged_Type
(Formal_Typ
)
5681 and then Scope
(Formal_Typ
) = Current_Scope
5682 and then Is_Private_Type
(Formal_Typ
)
5683 and then not Is_Generic_Actual_Type
(Formal_Typ
));
5687 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
5690 if Ada_Version
>= Ada_2005
5691 and then Comes_From_Source
(N
)
5692 and then Is_Dispatching_Operation
(Designator
)
5699 if Has_Controlling_Result
(Designator
) then
5700 Etyp
:= Etype
(Designator
);
5703 E
:= First_Entity
(Designator
);
5705 and then Is_Formal
(E
)
5706 and then not Is_Controlling_Formal
(E
)
5714 if Is_Access_Type
(Etyp
) then
5715 Etyp
:= Directly_Designated_Type
(Etyp
);
5718 if Is_Interface
(Etyp
)
5719 and then not Is_Abstract_Subprogram
(Designator
)
5720 and then not (Ekind
(Designator
) = E_Procedure
5721 and then Null_Present
(Specification
(N
)))
5723 Error_Msg_Name_1
:= Chars
(Defining_Entity
(N
));
5725 -- Specialize error message based on procedures vs. functions,
5726 -- since functions can't be null subprograms.
5728 if Ekind
(Designator
) = E_Procedure
then
5730 ("interface procedure % must be abstract or null", N
);
5733 ("interface function % must be abstract", N
);
5739 -- For a compilation unit, set body required. This flag will only be
5740 -- reset if a valid Import or Interface pragma is processed later on.
5742 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5743 Set_Body_Required
(Parent
(N
), True);
5745 if Ada_Version
>= Ada_2005
5746 and then Nkind
(Specification
(N
)) = N_Procedure_Specification
5747 and then Null_Present
(Specification
(N
))
5750 ("null procedure cannot be declared at library level", N
);
5754 Generate_Reference_To_Formals
(Designator
);
5755 Check_Eliminated
(Designator
);
5757 if Debug_Flag_C
then
5759 Write_Str
("<== subprogram spec ");
5760 Write_Name
(Chars
(Designator
));
5761 Write_Str
(" from ");
5762 Write_Location
(Sloc
(N
));
5766 -- Indicate that this is a protected operation, because it may be used
5767 -- in subsequent declarations within the protected type.
5769 if Is_Protected_Type
(Current_Scope
) then
5770 Set_Convention
(Designator
, Convention_Protected
);
5773 List_Inherited_Pre_Post_Aspects
(Designator
);
5775 -- Process the aspects before establishing the proper categorization in
5776 -- case the subprogram is a compilation unit and one of its aspects is
5777 -- converted into a categorization pragma.
5779 if Has_Aspects
(N
) then
5780 Analyze_Aspect_Specifications
(N
, Designator
);
5783 if Scop
/= Standard_Standard
and then not Is_Child_Unit
(Designator
) then
5784 Set_Categorization_From_Scope
(Designator
, Scop
);
5786 -- Otherwise the unit is a compilation unit and/or a child unit. Set the
5787 -- proper categorization of the unit based on its pragmas.
5790 Push_Scope
(Designator
);
5791 Set_Categorization_From_Pragmas
(N
);
5792 Validate_Categorization_Dependency
(N
, Designator
);
5795 end Analyze_Subprogram_Declaration
;
5797 --------------------------------------
5798 -- Analyze_Subprogram_Specification --
5799 --------------------------------------
5801 -- Reminder: N here really is a subprogram specification (not a subprogram
5802 -- declaration). This procedure is called to analyze the specification in
5803 -- both subprogram bodies and subprogram declarations (specs).
5805 function Analyze_Subprogram_Specification
(N
: Node_Id
) return Entity_Id
is
5806 function Is_Invariant_Procedure_Or_Body
(E
: Entity_Id
) return Boolean;
5807 -- Determine whether entity E denotes the spec or body of an invariant
5810 ------------------------------------
5811 -- Is_Invariant_Procedure_Or_Body --
5812 ------------------------------------
5814 function Is_Invariant_Procedure_Or_Body
(E
: Entity_Id
) return Boolean is
5815 Decl
: constant Node_Id
:= Unit_Declaration_Node
(E
);
5819 if Nkind
(Decl
) = N_Subprogram_Body
then
5820 Spec
:= Corresponding_Spec
(Decl
);
5827 and then Ekind
(Spec
) = E_Procedure
5828 and then (Is_Partial_Invariant_Procedure
(Spec
)
5829 or else Is_Invariant_Procedure
(Spec
));
5830 end Is_Invariant_Procedure_Or_Body
;
5834 Designator
: constant Entity_Id
:= Defining_Entity
(N
);
5835 Formals
: constant List_Id
:= Parameter_Specifications
(N
);
5837 -- Start of processing for Analyze_Subprogram_Specification
5840 -- Proceed with analysis. Do not emit a cross-reference entry if the
5841 -- specification comes from an expression function, because it may be
5842 -- the completion of a previous declaration. If it is not, the cross-
5843 -- reference entry will be emitted for the new subprogram declaration.
5845 if Nkind
(Parent
(N
)) /= N_Expression_Function
then
5846 Generate_Definition
(Designator
);
5849 if Nkind
(N
) = N_Function_Specification
then
5850 Mutate_Ekind
(Designator
, E_Function
);
5851 Set_Mechanism
(Designator
, Default_Mechanism
);
5853 Mutate_Ekind
(Designator
, E_Procedure
);
5854 Set_Etype
(Designator
, Standard_Void_Type
);
5857 -- Flag Is_Inlined_Always is True by default, and reversed to False for
5858 -- those subprograms which could be inlined in GNATprove mode (because
5859 -- Body_To_Inline is non-Empty) but should not be inlined.
5861 if GNATprove_Mode
then
5862 Set_Is_Inlined_Always
(Designator
);
5865 -- Introduce new scope for analysis of the formals and the return type
5867 Set_Scope
(Designator
, Current_Scope
);
5869 if Present
(Formals
) then
5870 Push_Scope
(Designator
);
5871 Process_Formals
(Formals
, N
);
5873 -- Check dimensions in N for formals with default expression
5875 Analyze_Dimension_Formals
(N
, Formals
);
5877 -- Ada 2005 (AI-345): If this is an overriding operation of an
5878 -- inherited interface operation, and the controlling type is
5879 -- a synchronized type, replace the type with its corresponding
5880 -- record, to match the proper signature of an overriding operation.
5881 -- Same processing for an access parameter whose designated type is
5882 -- derived from a synchronized interface.
5884 -- This modification is not done for invariant procedures because
5885 -- the corresponding record may not necessarely be visible when the
5886 -- concurrent type acts as the full view of a private type.
5889 -- type Prot is private with Type_Invariant => ...;
5890 -- procedure ConcInvariant (Obj : Prot);
5892 -- protected type Prot is ...;
5893 -- type Concurrent_Record_Prot is record ...;
5894 -- procedure ConcInvariant (Obj : Prot) is
5896 -- end ConcInvariant;
5899 -- In the example above, both the spec and body of the invariant
5900 -- procedure must utilize the private type as the controlling type.
5902 if Ada_Version
>= Ada_2005
5903 and then not Is_Invariant_Procedure_Or_Body
(Designator
)
5907 Formal_Typ
: Entity_Id
;
5908 Rec_Typ
: Entity_Id
;
5909 Desig_Typ
: Entity_Id
;
5912 Formal
:= First_Formal
(Designator
);
5913 while Present
(Formal
) loop
5914 Formal_Typ
:= Etype
(Formal
);
5916 if Is_Concurrent_Type
(Formal_Typ
)
5917 and then Present
(Corresponding_Record_Type
(Formal_Typ
))
5919 Rec_Typ
:= Corresponding_Record_Type
(Formal_Typ
);
5921 if Present
(Interfaces
(Rec_Typ
)) then
5922 Set_Etype
(Formal
, Rec_Typ
);
5925 elsif Ekind
(Formal_Typ
) = E_Anonymous_Access_Type
then
5926 Desig_Typ
:= Designated_Type
(Formal_Typ
);
5928 if Is_Concurrent_Type
(Desig_Typ
)
5929 and then Present
(Corresponding_Record_Type
(Desig_Typ
))
5931 Rec_Typ
:= Corresponding_Record_Type
(Desig_Typ
);
5933 if Present
(Interfaces
(Rec_Typ
)) then
5934 Set_Directly_Designated_Type
(Formal_Typ
, Rec_Typ
);
5939 Next_Formal
(Formal
);
5946 -- The subprogram scope is pushed and popped around the processing of
5947 -- the return type for consistency with call above to Process_Formals
5948 -- (which itself can call Analyze_Return_Type), and to ensure that any
5949 -- itype created for the return type will be associated with the proper
5952 elsif Nkind
(N
) = N_Function_Specification
then
5953 Push_Scope
(Designator
);
5954 Analyze_Return_Type
(N
);
5960 if Nkind
(N
) = N_Function_Specification
then
5962 -- Deal with operator symbol case
5964 if Nkind
(Designator
) = N_Defining_Operator_Symbol
then
5965 Valid_Operator_Definition
(Designator
);
5968 May_Need_Actuals
(Designator
);
5970 -- Ada 2005 (AI-251): If the return type is abstract, verify that
5971 -- the subprogram is abstract also. This does not apply to renaming
5972 -- declarations, where abstractness is inherited, and to subprogram
5973 -- bodies generated for stream operations, which become renamings as
5974 -- bodies. We also skip the check for thunks.
5976 -- In case of primitives associated with abstract interface types
5977 -- the check is applied later (see Analyze_Subprogram_Declaration).
5979 if Nkind
(Original_Node
(Parent
(N
))) not in
5980 N_Abstract_Subprogram_Declaration |
5981 N_Formal_Abstract_Subprogram_Declaration |
5982 N_Subprogram_Renaming_Declaration
5983 and then not Is_Thunk
(Designator
)
5985 if Is_Abstract_Type
(Etype
(Designator
)) then
5987 ("function that returns abstract type must be abstract", N
);
5989 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
5990 -- access result whose designated type is abstract.
5992 elsif Ada_Version
>= Ada_2012
5993 and then Nkind
(Result_Definition
(N
)) = N_Access_Definition
5995 not Is_Class_Wide_Type
(Designated_Type
(Etype
(Designator
)))
5996 and then Is_Abstract_Type
(Designated_Type
(Etype
(Designator
)))
5999 ("function whose access result designates abstract type "
6000 & "must be abstract", N
);
6006 end Analyze_Subprogram_Specification
;
6008 -----------------------
6009 -- Check_Conformance --
6010 -----------------------
6012 procedure Check_Conformance
6013 (New_Id
: Entity_Id
;
6015 Ctype
: Conformance_Type
;
6017 Conforms
: out Boolean;
6018 Err_Loc
: Node_Id
:= Empty
;
6019 Get_Inst
: Boolean := False;
6020 Skip_Controlling_Formals
: Boolean := False)
6022 procedure Conformance_Error
(Msg
: String; N
: Node_Id
:= New_Id
);
6023 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
6024 -- If Errmsg is True, then processing continues to post an error message
6025 -- for conformance error on given node. Two messages are output. The
6026 -- first message points to the previous declaration with a general "no
6027 -- conformance" message. The second is the detailed reason, supplied as
6028 -- Msg. The parameter N provide information for a possible & insertion
6029 -- in the message, and also provides the location for posting the
6030 -- message in the absence of a specified Err_Loc location.
6032 function Conventions_Match
(Id1
, Id2
: Entity_Id
) return Boolean;
6033 -- True if the conventions of entities Id1 and Id2 match.
6035 function Null_Exclusions_Match
(F1
, F2
: Entity_Id
) return Boolean;
6036 -- True if the null exclusions of two formals of anonymous access type
6039 function Subprogram_Subtypes_Have_Same_Declaration
6041 Decl_Subtype
: Entity_Id
;
6042 Body_Subtype
: Entity_Id
) return Boolean;
6043 -- Checks whether corresponding subtypes named within a subprogram
6044 -- declaration and body originate from the same declaration, and returns
6045 -- True when they do. In the case of anonymous access-to-object types,
6046 -- checks the designated types. Also returns True when GNAT_Mode is
6047 -- enabled, or when the subprogram is marked Is_Internal or occurs
6048 -- within a generic instantiation or internal unit (GNAT library unit).
6050 -----------------------
6051 -- Conformance_Error --
6052 -----------------------
6054 procedure Conformance_Error
(Msg
: String; N
: Node_Id
:= New_Id
) is
6061 if No
(Err_Loc
) then
6067 Error_Msg_Sloc
:= Sloc
(Old_Id
);
6070 when Type_Conformant
=>
6071 Error_Msg_N
-- CODEFIX
6072 ("not type conformant with declaration#!", Enode
);
6074 when Mode_Conformant
=>
6075 if Nkind
(Parent
(Old_Id
)) = N_Full_Type_Declaration
then
6077 ("not mode conformant with operation inherited#!",
6081 ("not mode conformant with declaration#!", Enode
);
6084 when Subtype_Conformant
=>
6085 if Nkind
(Parent
(Old_Id
)) = N_Full_Type_Declaration
then
6087 ("not subtype conformant with operation inherited#!",
6091 ("not subtype conformant with declaration#!", Enode
);
6094 when Fully_Conformant
=>
6095 if Nkind
(Parent
(Old_Id
)) = N_Full_Type_Declaration
then
6096 Error_Msg_N
-- CODEFIX
6097 ("not fully conformant with operation inherited#!",
6100 Error_Msg_N
-- CODEFIX
6101 ("not fully conformant with declaration#!", Enode
);
6105 Error_Msg_NE
(Msg
, Enode
, N
);
6107 end Conformance_Error
;
6109 -----------------------
6110 -- Conventions_Match --
6111 -----------------------
6113 function Conventions_Match
6115 Id2
: Entity_Id
) return Boolean
6118 -- Ignore the conventions of anonymous access-to-subprogram types
6119 -- and subprogram types because these are internally generated and
6120 -- the only way these may receive a convention is if they inherit
6121 -- the convention of a related subprogram.
6123 if Ekind
(Id1
) in E_Anonymous_Access_Subprogram_Type
6126 Ekind
(Id2
) in E_Anonymous_Access_Subprogram_Type
6131 -- Otherwise compare the conventions directly
6134 return Convention
(Id1
) = Convention
(Id2
);
6136 end Conventions_Match
;
6138 ---------------------------
6139 -- Null_Exclusions_Match --
6140 ---------------------------
6142 function Null_Exclusions_Match
(F1
, F2
: Entity_Id
) return Boolean is
6144 if not Is_Anonymous_Access_Type
(Etype
(F1
))
6145 or else not Is_Anonymous_Access_Type
(Etype
(F2
))
6150 -- AI12-0289-1: Case of controlling access parameter; False if the
6151 -- partial view is untagged, the full view is tagged, and no explicit
6152 -- "not null". Note that at this point, we're processing the package
6153 -- body, so private/full types have been swapped. The Sloc test below
6154 -- is to detect the (legal) case where F1 comes after the full type
6155 -- declaration. This part is disabled pre-2005, because "not null" is
6156 -- not allowed on those language versions.
6158 if Ada_Version
>= Ada_2005
6159 and then Is_Controlling_Formal
(F1
)
6160 and then not Null_Exclusion_Present
(Parent
(F1
))
6161 and then not Null_Exclusion_Present
(Parent
(F2
))
6164 D
: constant Entity_Id
:= Directly_Designated_Type
(Etype
(F1
));
6165 Partial_View_Of_Desig
: constant Entity_Id
:=
6166 Incomplete_Or_Partial_View
(D
);
6168 return No
(Partial_View_Of_Desig
)
6169 or else Is_Tagged_Type
(Partial_View_Of_Desig
)
6170 or else Sloc
(D
) < Sloc
(F1
);
6173 -- Not a controlling parameter, or one or both views have an explicit
6177 return Null_Exclusion_Present
(Parent
(F1
)) =
6178 Null_Exclusion_Present
(Parent
(F2
));
6180 end Null_Exclusions_Match
;
6182 function Subprogram_Subtypes_Have_Same_Declaration
6184 Decl_Subtype
: Entity_Id
;
6185 Body_Subtype
: Entity_Id
) return Boolean
6188 function Nonlimited_View_Of_Subtype
6189 (Subt
: Entity_Id
) return Entity_Id
;
6190 -- Returns the nonlimited view of a type or subtype that is an
6191 -- incomplete or class-wide type that comes from a limited view of
6192 -- a package (From_Limited_With is True for the entity), or the
6193 -- full view when the subtype is an incomplete type. Otherwise
6194 -- returns the entity passed in.
6196 function Nonlimited_View_Of_Subtype
6197 (Subt
: Entity_Id
) return Entity_Id
6199 Subt_Temp
: Entity_Id
:= Subt
;
6201 if Ekind
(Subt
) in Incomplete_Kind | E_Class_Wide_Type
6202 and then From_Limited_With
(Subt
)
6204 Subt_Temp
:= Non_Limited_View
(Subt
);
6207 -- If the subtype is incomplete, return full view if present
6208 -- (and accounts for the case where a type from a limited view
6209 -- is itself an incomplete type).
6211 if Ekind
(Subt_Temp
) in Incomplete_Kind
6212 and then Present
(Full_View
(Subt_Temp
))
6214 Subt_Temp
:= Full_View
(Subt_Temp
);
6218 end Nonlimited_View_Of_Subtype
;
6220 -- Start of processing for Subprogram_Subtypes_Have_Same_Declaration
6224 and then not In_Internal_Unit
(Subp
)
6225 and then not Is_Internal
(Subp
)
6226 and then not GNAT_Mode
6228 Ekind
(Etype
(Decl_Subtype
)) not in Access_Subprogram_Kind
6230 if Ekind
(Etype
(Decl_Subtype
)) = E_Anonymous_Access_Type
then
6231 if Nonlimited_View_Of_Subtype
(Designated_Type
(Decl_Subtype
))
6232 /= Nonlimited_View_Of_Subtype
(Designated_Type
(Body_Subtype
))
6237 elsif Nonlimited_View_Of_Subtype
(Decl_Subtype
)
6238 /= Nonlimited_View_Of_Subtype
(Body_Subtype
)
6240 -- Avoid returning False (and a false-positive warning) for
6241 -- the case of "not null" itypes, which will appear to be
6242 -- different subtypes even when the subtype_marks denote
6243 -- the same subtype.
6245 if Ekind
(Decl_Subtype
) = E_Access_Subtype
6246 and then Ekind
(Body_Subtype
) = E_Access_Subtype
6247 and then Is_Itype
(Body_Subtype
)
6248 and then Can_Never_Be_Null
(Body_Subtype
)
6249 and then Etype
(Decl_Subtype
) = Etype
(Body_Subtype
)
6260 end Subprogram_Subtypes_Have_Same_Declaration
;
6264 Old_Type
: constant Entity_Id
:= Etype
(Old_Id
);
6265 New_Type
: constant Entity_Id
:= Etype
(New_Id
);
6266 Old_Formal
: Entity_Id
;
6267 New_Formal
: Entity_Id
;
6268 Old_Formal_Base
: Entity_Id
;
6269 New_Formal_Base
: Entity_Id
;
6271 -- Start of processing for Check_Conformance
6276 -- We need a special case for operators, since they don't appear
6279 if Ctype
= Type_Conformant
then
6280 if Ekind
(New_Id
) = E_Operator
6281 and then Operator_Matches_Spec
(New_Id
, Old_Id
)
6287 -- If both are functions/operators, check return types conform
6289 if Old_Type
/= Standard_Void_Type
6291 New_Type
/= Standard_Void_Type
6293 -- If we are checking interface conformance we omit controlling
6294 -- arguments and result, because we are only checking the conformance
6295 -- of the remaining parameters.
6297 if Has_Controlling_Result
(Old_Id
)
6298 and then Has_Controlling_Result
(New_Id
)
6299 and then Skip_Controlling_Formals
6303 elsif not Conforming_Types
(Old_Type
, New_Type
, Ctype
, Get_Inst
) then
6304 if Ctype
>= Subtype_Conformant
6305 and then not Predicates_Match
(Old_Type
, New_Type
)
6308 ("\predicate of return type does not match!", New_Id
);
6311 ("\return type does not match!", New_Id
);
6316 -- If the result subtypes conform and pedantic checks are enabled,
6317 -- check to see whether the subtypes originate from different
6318 -- declarations, and issue a warning when they do.
6320 elsif Ctype
= Fully_Conformant
6321 and then Warn_On_Pedantic_Checks
6322 and then not Subprogram_Subtypes_Have_Same_Declaration
6323 (Old_Id
, Old_Type
, New_Type
)
6325 Error_Msg_N
("result subtypes conform but come from different "
6326 & "declarations?_p?", New_Id
);
6329 -- Ada 2005 (AI-231): In case of anonymous access types check the
6330 -- null-exclusion and access-to-constant attributes match.
6332 if Ada_Version
>= Ada_2005
6333 and then Ekind
(Etype
(Old_Type
)) = E_Anonymous_Access_Type
6335 (Can_Never_Be_Null
(Old_Type
) /= Can_Never_Be_Null
(New_Type
)
6336 or else Is_Access_Constant
(Etype
(Old_Type
)) /=
6337 Is_Access_Constant
(Etype
(New_Type
)))
6339 Conformance_Error
("\return type does not match!", New_Id
);
6343 -- If either is a function/operator and the other isn't, error
6345 elsif Old_Type
/= Standard_Void_Type
6346 or else New_Type
/= Standard_Void_Type
6348 Conformance_Error
("\functions can only match functions!", New_Id
);
6352 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
6353 -- If this is a renaming as body, refine error message to indicate that
6354 -- the conflict is with the original declaration. If the entity is not
6355 -- frozen, the conventions don't have to match, the one of the renamed
6356 -- entity is inherited.
6358 if Ctype
>= Subtype_Conformant
then
6359 if not Conventions_Match
(Old_Id
, New_Id
) then
6360 if not Is_Frozen
(New_Id
) then
6363 elsif Present
(Err_Loc
)
6364 and then Nkind
(Err_Loc
) = N_Subprogram_Renaming_Declaration
6365 and then Present
(Corresponding_Spec
(Err_Loc
))
6367 Error_Msg_Name_1
:= Chars
(New_Id
);
6369 Name_Ada
+ Convention_Id
'Pos (Convention
(New_Id
));
6370 Conformance_Error
("\prior declaration for% has convention %!");
6374 Conformance_Error
("\calling conventions do not match!");
6378 Check_Formal_Subprogram_Conformance
6379 (New_Id
, Old_Id
, Err_Loc
, Errmsg
, Conforms
);
6381 if not Conforms
then
6387 -- Deal with parameters
6389 -- Note: we use the entity information, rather than going directly
6390 -- to the specification in the tree. This is not only simpler, but
6391 -- absolutely necessary for some cases of conformance tests between
6392 -- operators, where the declaration tree simply does not exist.
6394 Old_Formal
:= First_Formal
(Old_Id
);
6395 New_Formal
:= First_Formal
(New_Id
);
6396 while Present
(Old_Formal
) and then Present
(New_Formal
) loop
6397 if Is_Controlling_Formal
(Old_Formal
)
6398 and then Is_Controlling_Formal
(New_Formal
)
6399 and then Skip_Controlling_Formals
6401 -- The controlling formals will have different types when
6402 -- comparing an interface operation with its match, but both
6403 -- or neither must be access parameters.
6405 if Is_Access_Type
(Etype
(Old_Formal
))
6407 Is_Access_Type
(Etype
(New_Formal
))
6409 goto Skip_Controlling_Formal
;
6412 ("\access parameter does not match!", New_Formal
);
6416 -- Ada 2012: Mode conformance also requires that formal parameters
6417 -- be both aliased, or neither.
6419 if Ctype
>= Mode_Conformant
and then Ada_Version
>= Ada_2012
then
6420 if Is_Aliased
(Old_Formal
) /= Is_Aliased
(New_Formal
) then
6422 ("\aliased parameter mismatch!", New_Formal
);
6426 if Ctype
= Fully_Conformant
then
6428 -- Names must match. Error message is more accurate if we do
6429 -- this before checking that the types of the formals match.
6431 if Chars
(Old_Formal
) /= Chars
(New_Formal
) then
6432 Conformance_Error
("\name& does not match!", New_Formal
);
6434 -- Set error posted flag on new formal as well to stop
6435 -- junk cascaded messages in some cases.
6437 Set_Error_Posted
(New_Formal
);
6441 -- Null exclusion must match
6443 if not Relaxed_RM_Semantics
6444 and then not Null_Exclusions_Match
(Old_Formal
, New_Formal
)
6447 ("\null exclusion for& does not match", New_Formal
);
6449 -- Mark error posted on the new formal to avoid duplicated
6450 -- complaint about types not matching.
6452 Set_Error_Posted
(New_Formal
);
6456 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
6457 -- case occurs whenever a subprogram is being renamed and one of its
6458 -- parameters imposes a null exclusion. For example:
6460 -- type T is null record;
6461 -- type Acc_T is access T;
6462 -- subtype Acc_T_Sub is Acc_T;
6464 -- procedure P (Obj : not null Acc_T_Sub); -- itype
6465 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
6468 Old_Formal_Base
:= Etype
(Old_Formal
);
6469 New_Formal_Base
:= Etype
(New_Formal
);
6472 Old_Formal_Base
:= Get_Instance_Of
(Old_Formal_Base
);
6473 New_Formal_Base
:= Get_Instance_Of
(New_Formal_Base
);
6476 -- Types must always match. In the visible part of an instance,
6477 -- usual overloading rules for dispatching operations apply, and
6478 -- we check base types (not the actual subtypes).
6480 if In_Instance_Visible_Part
6481 and then Is_Dispatching_Operation
(New_Id
)
6483 if not Conforming_Types
6484 (T1
=> Base_Type
(Etype
(Old_Formal
)),
6485 T2
=> Base_Type
(Etype
(New_Formal
)),
6487 Get_Inst
=> Get_Inst
)
6489 Conformance_Error
("\type of & does not match!", New_Formal
);
6493 elsif not Conforming_Types
6494 (T1
=> Old_Formal_Base
,
6495 T2
=> New_Formal_Base
,
6497 Get_Inst
=> Get_Inst
)
6499 -- Don't give error message if old type is Any_Type. This test
6500 -- avoids some cascaded errors, e.g. in case of a bad spec.
6502 if Errmsg
and then Old_Formal_Base
= Any_Type
then
6505 if Ctype
>= Subtype_Conformant
6507 not Predicates_Match
(Old_Formal_Base
, New_Formal_Base
)
6510 ("\predicate of & does not match!", New_Formal
);
6513 ("\type of & does not match!", New_Formal
);
6515 if not Dimensions_Match
(Old_Formal_Base
, New_Formal_Base
)
6517 Error_Msg_N
("\dimensions mismatch!", New_Formal
);
6524 -- If the formals' subtypes conform and pedantic checks are enabled,
6525 -- check to see whether the subtypes originate from different
6526 -- declarations, and issue a warning when they do.
6528 elsif Ctype
= Fully_Conformant
6529 and then Warn_On_Pedantic_Checks
6530 and then not Subprogram_Subtypes_Have_Same_Declaration
6531 (Old_Id
, Old_Formal_Base
, New_Formal_Base
)
6533 Error_Msg_N
("formal subtypes conform but come from "
6534 & "different declarations?_p?", New_Formal
);
6537 -- For mode conformance, mode must match
6539 if Ctype
>= Mode_Conformant
then
6540 if Parameter_Mode
(Old_Formal
) /= Parameter_Mode
(New_Formal
) then
6541 if Ekind
(New_Id
) not in E_Function | E_Procedure
6542 or else not Is_Primitive_Wrapper
(New_Id
)
6544 Conformance_Error
("\mode of & does not match!", New_Formal
);
6548 T
: constant Entity_Id
:= Find_Dispatching_Type
(New_Id
);
6550 if Is_Protected_Type
(Corresponding_Concurrent_Type
(T
))
6555 Error_Msg_PT
(New_Id
, Ultimate_Alias
(Old_Id
));
6559 ("\mode of & does not match!", New_Formal
);
6566 elsif Is_Access_Type
(Old_Formal_Base
)
6567 and then Is_Access_Type
(New_Formal_Base
)
6568 and then Is_Access_Constant
(Old_Formal_Base
) /=
6569 Is_Access_Constant
(New_Formal_Base
)
6572 ("\constant modifier does not match!", New_Formal
);
6577 if Ctype
>= Subtype_Conformant
then
6579 -- Ada 2005 (AI-231): In case of anonymous access types check
6580 -- the null-exclusion and access-to-constant attributes must
6581 -- match. For null exclusion, we test the types rather than the
6582 -- formals themselves, since the attribute is only set reliably
6583 -- on the formals in the Ada 95 case, and we exclude the case
6584 -- where Old_Formal is marked as controlling, to avoid errors
6585 -- when matching completing bodies with dispatching declarations
6586 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6588 if Ada_Version
>= Ada_2005
6589 and then Is_Anonymous_Access_Type
(Etype
(Old_Formal
))
6590 and then Is_Anonymous_Access_Type
(Etype
(New_Formal
))
6592 ((Can_Never_Be_Null
(Etype
(Old_Formal
)) /=
6593 Can_Never_Be_Null
(Etype
(New_Formal
))
6595 not Is_Controlling_Formal
(Old_Formal
))
6597 Is_Access_Constant
(Etype
(Old_Formal
)) /=
6598 Is_Access_Constant
(Etype
(New_Formal
)))
6600 -- Do not complain if error already posted on New_Formal. This
6601 -- avoids some redundant error messages.
6603 and then not Error_Posted
(New_Formal
)
6605 -- It is allowed to omit the null-exclusion in case of stream
6606 -- attribute subprograms. We recognize stream subprograms
6607 -- through their TSS-generated suffix.
6610 TSS_Name
: constant TSS_Name_Type
:= Get_TSS_Name
(New_Id
);
6613 if TSS_Name
/= TSS_Stream_Read
6614 and then TSS_Name
/= TSS_Stream_Write
6615 and then TSS_Name
/= TSS_Stream_Input
6616 and then TSS_Name
/= TSS_Stream_Output
6618 -- Here we have a definite conformance error. It is worth
6619 -- special casing the error message for the case of a
6620 -- controlling formal (which excludes null).
6622 if Is_Controlling_Formal
(New_Formal
) then
6623 Error_Msg_Node_2
:= Scope
(New_Formal
);
6625 ("\controlling formal & of & excludes null, "
6626 & "declaration must exclude null as well",
6629 -- Normal case (couldn't we give more detail here???)
6633 ("\type of & does not match!", New_Formal
);
6642 -- Full conformance checks
6644 if Ctype
= Fully_Conformant
then
6646 -- We have checked already that names match
6648 if Parameter_Mode
(Old_Formal
) = E_In_Parameter
then
6650 -- Check default expressions for in parameters
6653 NewD
: constant Boolean :=
6654 Present
(Default_Value
(New_Formal
));
6655 OldD
: constant Boolean :=
6656 Present
(Default_Value
(Old_Formal
));
6658 if NewD
or OldD
then
6660 -- The old default value has been analyzed because the
6661 -- current full declaration will have frozen everything
6662 -- before. The new default value has not been analyzed,
6663 -- so analyze it now before we check for conformance.
6666 Push_Scope
(New_Id
);
6667 Preanalyze_Spec_Expression
6668 (Default_Value
(New_Formal
), Etype
(New_Formal
));
6672 if not (NewD
and OldD
)
6673 or else not Fully_Conformant_Expressions
6674 (Default_Value
(Old_Formal
),
6675 Default_Value
(New_Formal
))
6678 ("\default expression for & does not match!",
6687 -- A couple of special checks for Ada 83 mode. These checks are
6688 -- skipped if either entity is an operator in package Standard,
6689 -- or if either old or new instance is not from the source program.
6691 if Ada_Version
= Ada_83
6692 and then Sloc
(Old_Id
) > Standard_Location
6693 and then Sloc
(New_Id
) > Standard_Location
6694 and then Comes_From_Source
(Old_Id
)
6695 and then Comes_From_Source
(New_Id
)
6698 Old_Param
: constant Node_Id
:= Declaration_Node
(Old_Formal
);
6699 New_Param
: constant Node_Id
:= Declaration_Node
(New_Formal
);
6702 -- Explicit IN must be present or absent in both cases. This
6703 -- test is required only in the full conformance case.
6705 if In_Present
(Old_Param
) /= In_Present
(New_Param
)
6706 and then Ctype
= Fully_Conformant
6709 ("\(Ada 83) IN must appear in both declarations",
6714 -- Grouping (use of comma in param lists) must be the same
6715 -- This is where we catch a misconformance like:
6718 -- A : Integer; B : Integer
6720 -- which are represented identically in the tree except
6721 -- for the setting of the flags More_Ids and Prev_Ids.
6723 if More_Ids
(Old_Param
) /= More_Ids
(New_Param
)
6724 or else Prev_Ids
(Old_Param
) /= Prev_Ids
(New_Param
)
6727 ("\grouping of & does not match!", New_Formal
);
6733 -- This label is required when skipping controlling formals
6735 <<Skip_Controlling_Formal
>>
6737 Next_Formal
(Old_Formal
);
6738 Next_Formal
(New_Formal
);
6741 if Present
(Old_Formal
) then
6742 Conformance_Error
("\too few parameters!");
6745 elsif Present
(New_Formal
) then
6746 Conformance_Error
("\too many parameters!", New_Formal
);
6749 end Check_Conformance
;
6751 -----------------------
6752 -- Check_Conventions --
6753 -----------------------
6755 procedure Check_Conventions
(Typ
: Entity_Id
) is
6756 Ifaces_List
: Elist_Id
;
6758 procedure Check_Convention
(Op
: Entity_Id
);
6759 -- Verify that the convention of inherited dispatching operation Op is
6760 -- consistent among all subprograms it overrides. In order to minimize
6761 -- the search, Search_From is utilized to designate a specific point in
6762 -- the list rather than iterating over the whole list once more.
6764 ----------------------
6765 -- Check_Convention --
6766 ----------------------
6768 procedure Check_Convention
(Op
: Entity_Id
) is
6769 Op_Conv
: constant Convention_Id
:= Convention
(Op
);
6770 Iface_Conv
: Convention_Id
;
6771 Iface_Elmt
: Elmt_Id
;
6772 Iface_Prim_Elmt
: Elmt_Id
;
6773 Iface_Prim
: Entity_Id
;
6776 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
6777 while Present
(Iface_Elmt
) loop
6779 First_Elmt
(Primitive_Operations
(Node
(Iface_Elmt
)));
6780 while Present
(Iface_Prim_Elmt
) loop
6781 Iface_Prim
:= Node
(Iface_Prim_Elmt
);
6782 Iface_Conv
:= Convention
(Iface_Prim
);
6784 if Is_Interface_Conformant
(Typ
, Iface_Prim
, Op
)
6785 and then Iface_Conv
/= Op_Conv
6788 ("inconsistent conventions in primitive operations", Typ
);
6790 Error_Msg_Name_1
:= Chars
(Op
);
6791 Error_Msg_Name_2
:= Get_Convention_Name
(Op_Conv
);
6792 Error_Msg_Sloc
:= Sloc
(Op
);
6794 if Comes_From_Source
(Op
) or else No
(Alias
(Op
)) then
6795 if not Present
(Overridden_Operation
(Op
)) then
6796 Error_Msg_N
("\\primitive % defined #", Typ
);
6799 ("\\overriding operation % with "
6800 & "convention % defined #", Typ
);
6803 else pragma Assert
(Present
(Alias
(Op
)));
6804 Error_Msg_Sloc
:= Sloc
(Alias
(Op
));
6805 Error_Msg_N
("\\inherited operation % with "
6806 & "convention % defined #", Typ
);
6809 Error_Msg_Name_1
:= Chars
(Op
);
6810 Error_Msg_Name_2
:= Get_Convention_Name
(Iface_Conv
);
6811 Error_Msg_Sloc
:= Sloc
(Iface_Prim
);
6812 Error_Msg_N
("\\overridden operation % with "
6813 & "convention % defined #", Typ
);
6815 -- Avoid cascading errors
6820 Next_Elmt
(Iface_Prim_Elmt
);
6823 Next_Elmt
(Iface_Elmt
);
6825 end Check_Convention
;
6829 Prim_Op
: Entity_Id
;
6830 Prim_Op_Elmt
: Elmt_Id
;
6832 -- Start of processing for Check_Conventions
6835 if not Has_Interfaces
(Typ
) then
6839 Collect_Interfaces
(Typ
, Ifaces_List
);
6841 -- The algorithm checks every overriding dispatching operation against
6842 -- all the corresponding overridden dispatching operations, detecting
6843 -- differences in conventions.
6845 Prim_Op_Elmt
:= First_Elmt
(Primitive_Operations
(Typ
));
6846 while Present
(Prim_Op_Elmt
) loop
6847 Prim_Op
:= Node
(Prim_Op_Elmt
);
6849 -- A small optimization: skip the predefined dispatching operations
6850 -- since they always have the same convention.
6852 if not Is_Predefined_Dispatching_Operation
(Prim_Op
) then
6853 Check_Convention
(Prim_Op
);
6856 Next_Elmt
(Prim_Op_Elmt
);
6858 end Check_Conventions
;
6860 ------------------------------
6861 -- Check_Delayed_Subprogram --
6862 ------------------------------
6864 procedure Check_Delayed_Subprogram
(Designator
: Entity_Id
) is
6865 procedure Possible_Freeze
(T
: Entity_Id
);
6866 -- T is the type of either a formal parameter or of the return type. If
6867 -- T is not yet frozen and needs a delayed freeze, then the subprogram
6868 -- itself must be delayed.
6870 ---------------------
6871 -- Possible_Freeze --
6872 ---------------------
6874 procedure Possible_Freeze
(T
: Entity_Id
) is
6875 Scop
: constant Entity_Id
:= Scope
(Designator
);
6878 -- If the subprogram appears within a package instance (which may be
6879 -- the wrapper package of a subprogram instance) the freeze node for
6880 -- that package will freeze the subprogram at the proper place, so
6881 -- do not emit a freeze node for the subprogram, given that it may
6882 -- appear in the wrong scope.
6884 if Ekind
(Scop
) = E_Package
6885 and then not Comes_From_Source
(Scop
)
6886 and then Is_Generic_Instance
(Scop
)
6890 elsif Has_Delayed_Freeze
(T
) and then not Is_Frozen
(T
) then
6891 Set_Has_Delayed_Freeze
(Designator
);
6893 elsif Is_Access_Type
(T
)
6894 and then Has_Delayed_Freeze
(Designated_Type
(T
))
6895 and then not Is_Frozen
(Designated_Type
(T
))
6897 Set_Has_Delayed_Freeze
(Designator
);
6899 end Possible_Freeze
;
6905 -- Start of processing for Check_Delayed_Subprogram
6908 -- All subprograms, including abstract subprograms, may need a freeze
6909 -- node if some formal type or the return type needs one.
6911 Possible_Freeze
(Etype
(Designator
));
6912 Possible_Freeze
(Base_Type
(Etype
(Designator
))); -- needed ???
6914 -- Need delayed freeze if any of the formal types themselves need a
6915 -- delayed freeze and are not yet frozen.
6917 F
:= First_Formal
(Designator
);
6918 while Present
(F
) loop
6919 Possible_Freeze
(Etype
(F
));
6920 Possible_Freeze
(Base_Type
(Etype
(F
))); -- needed ???
6924 -- Mark functions that return by reference. Note that it cannot be done
6925 -- for delayed_freeze subprograms because the underlying returned type
6926 -- may not be known yet (for private types).
6928 if not Has_Delayed_Freeze
(Designator
) and then Expander_Active
then
6929 Compute_Returns_By_Ref
(Designator
);
6931 end Check_Delayed_Subprogram
;
6933 ------------------------------------
6934 -- Check_Discriminant_Conformance --
6935 ------------------------------------
6937 procedure Check_Discriminant_Conformance
6942 Old_Discr
: Entity_Id
:= First_Discriminant
(Prev
);
6943 New_Discr
: Node_Id
:= First
(Discriminant_Specifications
(N
));
6944 New_Discr_Id
: Entity_Id
;
6945 New_Discr_Type
: Entity_Id
;
6947 procedure Conformance_Error
(Msg
: String; N
: Node_Id
);
6948 -- Post error message for conformance error on given node. Two messages
6949 -- are output. The first points to the previous declaration with a
6950 -- general "no conformance" message. The second is the detailed reason,
6951 -- supplied as Msg. The parameter N provide information for a possible
6952 -- & insertion in the message.
6954 -----------------------
6955 -- Conformance_Error --
6956 -----------------------
6958 procedure Conformance_Error
(Msg
: String; N
: Node_Id
) is
6960 Error_Msg_Sloc
:= Sloc
(Prev_Loc
);
6961 Error_Msg_N
-- CODEFIX
6962 ("not fully conformant with declaration#!", N
);
6963 Error_Msg_NE
(Msg
, N
, N
);
6964 end Conformance_Error
;
6966 -- Start of processing for Check_Discriminant_Conformance
6969 while Present
(Old_Discr
) and then Present
(New_Discr
) loop
6970 New_Discr_Id
:= Defining_Identifier
(New_Discr
);
6972 -- The subtype mark of the discriminant on the full type has not
6973 -- been analyzed so we do it here. For an access discriminant a new
6976 if Nkind
(Discriminant_Type
(New_Discr
)) = N_Access_Definition
then
6978 Access_Definition
(N
, Discriminant_Type
(New_Discr
));
6981 Find_Type
(Discriminant_Type
(New_Discr
));
6982 New_Discr_Type
:= Etype
(Discriminant_Type
(New_Discr
));
6984 -- Ada 2005: if the discriminant definition carries a null
6985 -- exclusion, create an itype to check properly for consistency
6986 -- with partial declaration.
6988 if Is_Access_Type
(New_Discr_Type
)
6989 and then Null_Exclusion_Present
(New_Discr
)
6992 Create_Null_Excluding_Itype
6993 (T
=> New_Discr_Type
,
6994 Related_Nod
=> New_Discr
,
6995 Scope_Id
=> Current_Scope
);
6999 if not Conforming_Types
7000 (Etype
(Old_Discr
), New_Discr_Type
, Fully_Conformant
)
7002 Conformance_Error
("type of & does not match!", New_Discr_Id
);
7005 -- Treat the new discriminant as an occurrence of the old one,
7006 -- for navigation purposes, and fill in some semantic
7007 -- information, for completeness.
7009 Generate_Reference
(Old_Discr
, New_Discr_Id
, 'r');
7010 Set_Etype
(New_Discr_Id
, Etype
(Old_Discr
));
7011 Set_Scope
(New_Discr_Id
, Scope
(Old_Discr
));
7016 if Chars
(Old_Discr
) /= Chars
(Defining_Identifier
(New_Discr
)) then
7017 Conformance_Error
("name & does not match!", New_Discr_Id
);
7021 -- Default expressions must match
7024 NewD
: constant Boolean :=
7025 Present
(Expression
(New_Discr
));
7026 OldD
: constant Boolean :=
7027 Present
(Expression
(Parent
(Old_Discr
)));
7030 if NewD
or OldD
then
7032 -- The old default value has been analyzed and expanded,
7033 -- because the current full declaration will have frozen
7034 -- everything before. The new default values have not been
7035 -- expanded, so expand now to check conformance.
7038 Preanalyze_Spec_Expression
7039 (Expression
(New_Discr
), New_Discr_Type
);
7042 if not (NewD
and OldD
)
7043 or else not Fully_Conformant_Expressions
7044 (Expression
(Parent
(Old_Discr
)),
7045 Expression
(New_Discr
))
7049 ("default expression for & does not match!",
7056 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
7058 if Ada_Version
= Ada_83
then
7060 Old_Disc
: constant Node_Id
:= Declaration_Node
(Old_Discr
);
7063 -- Grouping (use of comma in param lists) must be the same
7064 -- This is where we catch a misconformance like:
7067 -- A : Integer; B : Integer
7069 -- which are represented identically in the tree except
7070 -- for the setting of the flags More_Ids and Prev_Ids.
7072 if More_Ids
(Old_Disc
) /= More_Ids
(New_Discr
)
7073 or else Prev_Ids
(Old_Disc
) /= Prev_Ids
(New_Discr
)
7076 ("grouping of & does not match!", New_Discr_Id
);
7082 Next_Discriminant
(Old_Discr
);
7086 if Present
(Old_Discr
) then
7087 Conformance_Error
("too few discriminants!", Defining_Identifier
(N
));
7090 elsif Present
(New_Discr
) then
7092 ("too many discriminants!", Defining_Identifier
(New_Discr
));
7095 end Check_Discriminant_Conformance
;
7097 -----------------------------------------
7098 -- Check_Formal_Subprogram_Conformance --
7099 -----------------------------------------
7101 procedure Check_Formal_Subprogram_Conformance
7102 (New_Id
: Entity_Id
;
7106 Conforms
: out Boolean)
7112 if Is_Formal_Subprogram
(Old_Id
)
7113 or else Is_Formal_Subprogram
(New_Id
)
7114 or else (Is_Subprogram
(New_Id
)
7115 and then Present
(Alias
(New_Id
))
7116 and then Is_Formal_Subprogram
(Alias
(New_Id
)))
7118 if Present
(Err_Loc
) then
7127 Error_Msg_Sloc
:= Sloc
(Old_Id
);
7128 Error_Msg_N
("not subtype conformant with declaration#!", N
);
7130 ("\formal subprograms are not subtype conformant "
7131 & "(RM 6.3.1 (17/3))", N
, New_Id
);
7134 end Check_Formal_Subprogram_Conformance
;
7136 procedure Check_Formal_Subprogram_Conformance
7137 (New_Id
: Entity_Id
;
7139 Err_Loc
: Node_Id
:= Empty
)
7143 Check_Formal_Subprogram_Conformance
7144 (New_Id
, Old_Id
, Err_Loc
, True, Ignore
);
7145 end Check_Formal_Subprogram_Conformance
;
7147 ----------------------------
7148 -- Check_Fully_Conformant --
7149 ----------------------------
7151 procedure Check_Fully_Conformant
7152 (New_Id
: Entity_Id
;
7154 Err_Loc
: Node_Id
:= Empty
)
7157 pragma Warnings
(Off
, Result
);
7160 (New_Id
, Old_Id
, Fully_Conformant
, True, Result
, Err_Loc
);
7161 end Check_Fully_Conformant
;
7163 --------------------------
7164 -- Check_Limited_Return --
7165 --------------------------
7167 procedure Check_Limited_Return
7173 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
7174 -- replaced by anonymous access results. This is an incompatibility with
7175 -- Ada 95. Not clear whether this should be enforced yet or perhaps
7176 -- controllable with special switch. ???
7178 -- A limited interface that is not immutably limited is OK
7180 if Is_Limited_Interface
(R_Type
)
7181 and then not Is_Concurrent_Interface
(R_Type
)
7185 elsif Is_Limited_Type
(R_Type
)
7186 and then not Is_Interface
(R_Type
)
7187 and then not (Nkind
(N
) = N_Simple_Return_Statement
7188 and then Comes_From_Extended_Return_Statement
(N
))
7189 and then not In_Instance_Body
7190 and then not OK_For_Limited_Init_In_05
(R_Type
, Expr
)
7192 -- Error in Ada 2005
7194 if Ada_Version
>= Ada_2005
7195 and then not Debug_Flag_Dot_L
7196 and then not GNAT_Mode
7199 ("(Ada 2005) cannot copy object of a limited type "
7200 & "(RM-2005 6.5(5.5/2))", Expr
);
7202 if Is_Limited_View
(R_Type
) then
7204 ("\return by reference not permitted in Ada 2005", Expr
);
7207 -- Warn in Ada 95 mode, to give folks a heads up about this
7210 -- In GNAT mode, this is just a warning, to allow it to be evilly
7211 -- turned off. Otherwise it is a real error.
7213 -- In a generic context, simplify the warning because it makes no
7214 -- sense to discuss pass-by-reference or copy.
7216 elsif Warn_On_Ada_2005_Compatibility
or GNAT_Mode
then
7217 if Inside_A_Generic
then
7219 ("return of limited object not permitted in Ada 2005 "
7220 & "(RM-2005 6.5(5.5/2))?y?", Expr
);
7222 elsif Is_Limited_View
(R_Type
) then
7224 ("return by reference not permitted in Ada 2005 "
7225 & "(RM-2005 6.5(5.5/2))?y?", Expr
);
7228 ("cannot copy object of a limited type in Ada 2005 "
7229 & "(RM-2005 6.5(5.5/2))?y?", Expr
);
7232 -- Ada 95 mode, and compatibility warnings disabled
7235 pragma Assert
(Ada_Version
<= Ada_95
);
7236 pragma Assert
(not (Warn_On_Ada_2005_Compatibility
or GNAT_Mode
));
7237 return; -- skip continuation messages below
7240 if not Inside_A_Generic
then
7242 ("\consider switching to return of access type", Expr
);
7243 Explain_Limited_Type
(R_Type
, Expr
);
7246 end Check_Limited_Return
;
7248 ---------------------------
7249 -- Check_Mode_Conformant --
7250 ---------------------------
7252 procedure Check_Mode_Conformant
7253 (New_Id
: Entity_Id
;
7255 Err_Loc
: Node_Id
:= Empty
;
7256 Get_Inst
: Boolean := False)
7259 pragma Warnings
(Off
, Result
);
7262 (New_Id
, Old_Id
, Mode_Conformant
, True, Result
, Err_Loc
, Get_Inst
);
7263 end Check_Mode_Conformant
;
7265 --------------------------------
7266 -- Check_Overriding_Indicator --
7267 --------------------------------
7269 procedure Check_Overriding_Indicator
7271 Overridden_Subp
: Entity_Id
;
7272 Is_Primitive
: Boolean)
7278 -- No overriding indicator for literals
7280 if Ekind
(Subp
) = E_Enumeration_Literal
then
7283 elsif Ekind
(Subp
) = E_Entry
then
7284 Decl
:= Parent
(Subp
);
7286 -- No point in analyzing a malformed operator
7288 elsif Nkind
(Subp
) = N_Defining_Operator_Symbol
7289 and then Error_Posted
(Subp
)
7294 Decl
:= Unit_Declaration_Node
(Subp
);
7297 if Nkind
(Decl
) in N_Subprogram_Body
7298 | N_Subprogram_Body_Stub
7299 | N_Subprogram_Declaration
7300 | N_Abstract_Subprogram_Declaration
7301 | N_Subprogram_Renaming_Declaration
7303 Spec
:= Specification
(Decl
);
7305 elsif Nkind
(Decl
) = N_Entry_Declaration
then
7312 -- An overriding indication is illegal on a subprogram declared
7313 -- in a protected body, where there is no operation to override.
7315 if (Must_Override
(Spec
) or else Must_Not_Override
(Spec
))
7316 and then Is_List_Member
(Decl
)
7317 and then Present
(Parent
(List_Containing
(Decl
)))
7318 and then Nkind
(Parent
(List_Containing
(Decl
))) = N_Protected_Body
7321 ("illegal overriding indication in protected body", Decl
);
7325 -- The overriding operation is type conformant with the overridden one,
7326 -- but the names of the formals are not required to match. If the names
7327 -- appear permuted in the overriding operation, this is a possible
7328 -- source of confusion that is worth diagnosing. Controlling formals
7329 -- often carry names that reflect the type, and it is not worthwhile
7330 -- requiring that their names match.
7332 if Present
(Overridden_Subp
)
7333 and then Nkind
(Subp
) /= N_Defining_Operator_Symbol
7340 Form1
:= First_Formal
(Subp
);
7341 Form2
:= First_Formal
(Overridden_Subp
);
7343 -- If the overriding operation is a synchronized operation, skip
7344 -- the first parameter of the overridden operation, which is
7345 -- implicit in the new one. If the operation is declared in the
7346 -- body it is not primitive and all formals must match.
7348 if Is_Concurrent_Type
(Scope
(Subp
))
7349 and then Is_Tagged_Type
(Scope
(Subp
))
7350 and then not Has_Completion
(Scope
(Subp
))
7352 Form2
:= Next_Formal
(Form2
);
7355 if Present
(Form1
) then
7356 Form1
:= Next_Formal
(Form1
);
7357 Form2
:= Next_Formal
(Form2
);
7360 while Present
(Form1
) loop
7361 if not Is_Controlling_Formal
(Form1
)
7362 and then Present
(Next_Formal
(Form2
))
7363 and then Chars
(Form1
) = Chars
(Next_Formal
(Form2
))
7365 Error_Msg_Node_2
:= Alias
(Overridden_Subp
);
7366 Error_Msg_Sloc
:= Sloc
(Error_Msg_Node_2
);
7368 ("& does not match corresponding formal of&#",
7373 Next_Formal
(Form1
);
7374 Next_Formal
(Form2
);
7379 -- If there is an overridden subprogram, then check that there is no
7380 -- "not overriding" indicator, and mark the subprogram as overriding.
7382 -- This is not done if the overridden subprogram is marked as hidden,
7383 -- which can occur for the case of inherited controlled operations
7384 -- (see Derive_Subprogram), unless the inherited subprogram's parent
7385 -- subprogram is not itself hidden or we are within a generic instance,
7386 -- in which case the hidden flag may have been modified for the
7387 -- expansion of the instance.
7389 -- (Note: This condition could probably be simplified, leaving out the
7390 -- testing for the specific controlled cases, but it seems safer and
7391 -- clearer this way, and echoes similar special-case tests of this
7392 -- kind in other places.)
7394 if Present
(Overridden_Subp
)
7395 and then (not Is_Hidden
(Overridden_Subp
)
7397 (Chars
(Overridden_Subp
) in Name_Initialize
7400 and then Present
(Alias
(Overridden_Subp
))
7401 and then (not Is_Hidden
(Alias
(Overridden_Subp
))
7402 or else In_Instance
)))
7404 if Must_Not_Override
(Spec
) then
7405 Error_Msg_Sloc
:= Sloc
(Overridden_Subp
);
7407 if Ekind
(Subp
) = E_Entry
then
7409 ("entry & overrides inherited operation #", Spec
, Subp
);
7412 ("subprogram & overrides inherited operation #", Spec
, Subp
);
7415 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
7416 -- as an extension of Root_Controlled, and thus has a useless Adjust
7417 -- operation. This operation should not be inherited by other limited
7418 -- controlled types. An explicit Adjust for them is not overriding.
7420 elsif Must_Override
(Spec
)
7421 and then Chars
(Overridden_Subp
) = Name_Adjust
7422 and then Is_Limited_Type
(Etype
(First_Formal
(Subp
)))
7423 and then Present
(Alias
(Overridden_Subp
))
7424 and then In_Predefined_Unit
(Alias
(Overridden_Subp
))
7427 (Unit_File_Name
(Get_Source_Unit
(Alias
(Overridden_Subp
))));
7428 Error_Msg_NE
("subprogram & is not overriding", Spec
, Subp
);
7430 elsif Is_Subprogram
(Subp
) then
7431 if Is_Init_Proc
(Subp
) then
7434 elsif No
(Overridden_Operation
(Subp
)) then
7436 -- For entities generated by Derive_Subprograms the overridden
7437 -- operation is the inherited primitive (which is available
7438 -- through the attribute alias)
7440 if (Is_Dispatching_Operation
(Subp
)
7441 or else Is_Dispatching_Operation
(Overridden_Subp
))
7442 and then not Comes_From_Source
(Overridden_Subp
)
7443 and then Find_Dispatching_Type
(Overridden_Subp
) =
7444 Find_Dispatching_Type
(Subp
)
7445 and then Present
(Alias
(Overridden_Subp
))
7446 and then Comes_From_Source
(Alias
(Overridden_Subp
))
7448 Set_Overridden_Operation
(Subp
, Alias
(Overridden_Subp
));
7449 Inherit_Subprogram_Contract
(Subp
, Alias
(Overridden_Subp
));
7450 Set_Is_Ada_2022_Only
(Subp
,
7451 Is_Ada_2022_Only
(Alias
(Overridden_Subp
)));
7454 Set_Overridden_Operation
(Subp
, Overridden_Subp
);
7455 Inherit_Subprogram_Contract
(Subp
, Overridden_Subp
);
7456 Set_Is_Ada_2022_Only
(Subp
,
7457 Is_Ada_2022_Only
(Overridden_Subp
));
7462 -- If primitive flag is set or this is a protected operation, then
7463 -- the operation is overriding at the point of its declaration, so
7464 -- warn if necessary. Otherwise it may have been declared before the
7465 -- operation it overrides and no check is required.
7468 and then not Must_Override
(Spec
)
7469 and then (Is_Primitive
7470 or else Ekind
(Scope
(Subp
)) = E_Protected_Type
)
7472 Style
.Missing_Overriding
(Decl
, Subp
);
7475 -- If Subp is an operator, it may override a predefined operation, if
7476 -- it is defined in the same scope as the type to which it applies.
7477 -- In that case Overridden_Subp is empty because of our implicit
7478 -- representation for predefined operators. We have to check whether the
7479 -- signature of Subp matches that of a predefined operator. Note that
7480 -- first argument provides the name of the operator, and the second
7481 -- argument the signature that may match that of a standard operation.
7482 -- If the indicator is overriding, then the operator must match a
7483 -- predefined signature, because we know already that there is no
7484 -- explicit overridden operation.
7486 elsif Chars
(Subp
) in Any_Operator_Name
then
7487 if Must_Not_Override
(Spec
) then
7489 -- If this is not a primitive or a protected subprogram, then
7490 -- "not overriding" is illegal.
7493 and then Ekind
(Scope
(Subp
)) /= E_Protected_Type
7495 Error_Msg_N
("overriding indicator only allowed "
7496 & "if subprogram is primitive", Subp
);
7498 elsif Can_Override_Operator
(Subp
) then
7500 ("subprogram& overrides predefined operator", Spec
, Subp
);
7503 elsif Must_Override
(Spec
) then
7504 if No
(Overridden_Operation
(Subp
))
7505 and then not Can_Override_Operator
(Subp
)
7507 Error_Msg_NE
("subprogram & is not overriding", Spec
, Subp
);
7510 elsif not Error_Posted
(Subp
)
7511 and then Style_Check
7512 and then Can_Override_Operator
(Subp
)
7513 and then not In_Predefined_Unit
(Subp
)
7515 -- If style checks are enabled, indicate that the indicator is
7516 -- missing. However, at the point of declaration, the type of
7517 -- which this is a primitive operation may be private, in which
7518 -- case the indicator would be premature.
7520 if Has_Private_Declaration
(Etype
(Subp
))
7521 or else Has_Private_Declaration
(Etype
(First_Formal
(Subp
)))
7525 Style
.Missing_Overriding
(Decl
, Subp
);
7529 elsif Must_Override
(Spec
) then
7530 if Ekind
(Subp
) = E_Entry
then
7531 Error_Msg_NE
("entry & is not overriding", Spec
, Subp
);
7533 Error_Msg_NE
("subprogram & is not overriding", Spec
, Subp
);
7536 -- If the operation is marked "not overriding" and it's not primitive
7537 -- then an error is issued, unless this is an operation of a task or
7538 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
7539 -- has been specified have already been checked above.
7541 elsif Must_Not_Override
(Spec
)
7542 and then not Is_Primitive
7543 and then Ekind
(Subp
) /= E_Entry
7544 and then Ekind
(Scope
(Subp
)) /= E_Protected_Type
7547 ("overriding indicator only allowed if subprogram is primitive",
7551 end Check_Overriding_Indicator
;
7557 -- Note: this procedure needs to know far too much about how the expander
7558 -- messes with exceptions. The use of the flag Exception_Junk and the
7559 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
7560 -- works, but is not very clean. It would be better if the expansion
7561 -- routines would leave Original_Node working nicely, and we could use
7562 -- Original_Node here to ignore all the peculiar expander messing ???
7564 procedure Check_Returns
7568 Proc
: Entity_Id
:= Empty
)
7570 pragma Assert
(Mode
in 'F' |
'P');
7571 pragma Assert
(if Mode
= 'F' then No
(Proc
));
7574 procedure Check_Statement_Sequence
(L
: List_Id
);
7575 -- Internal recursive procedure to check a list of statements for proper
7576 -- termination by a return statement (or a transfer of control or a
7577 -- compound statement that is itself internally properly terminated).
7579 ------------------------------
7580 -- Check_Statement_Sequence --
7581 ------------------------------
7583 procedure Check_Statement_Sequence
(L
: List_Id
) is
7588 function Assert_False
return Boolean;
7589 -- Returns True if Last_Stm is a pragma Assert (False) that has been
7590 -- rewritten as a null statement when assertions are off. The assert
7591 -- is not active, but it is still enough to kill the warning.
7597 function Assert_False
return Boolean is
7598 Orig
: constant Node_Id
:= Original_Node
(Last_Stm
);
7601 if Nkind
(Orig
) = N_Pragma
7602 and then Pragma_Name
(Orig
) = Name_Assert
7603 and then not Error_Posted
(Orig
)
7606 Arg
: constant Node_Id
:=
7607 First
(Pragma_Argument_Associations
(Orig
));
7608 Exp
: constant Node_Id
:= Expression
(Arg
);
7610 return Nkind
(Exp
) = N_Identifier
7611 and then Chars
(Exp
) = Name_False
;
7621 Raise_Exception_Call
: Boolean := False;
7622 -- Set True if statement sequence terminated by Raise_Exception call
7623 -- or a Reraise_Occurrence call.
7625 -- Start of processing for Check_Statement_Sequence
7628 -- Get last real statement
7630 Last_Stm
:= Last
(L
);
7632 -- Deal with digging out exception handler statement sequences that
7633 -- have been transformed by the local raise to goto optimization.
7634 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7635 -- optimization has occurred, we are looking at something like:
7638 -- original stmts in block
7642 -- goto L1; | omitted if No_Exception_Propagation
7647 -- goto L3; -- skip handler when exception not raised
7649 -- <<L1>> -- target label for local exception
7663 -- and what we have to do is to dig out the estmts1 and estmts2
7664 -- sequences (which were the original sequences of statements in
7665 -- the exception handlers) and check them.
7667 if Nkind
(Last_Stm
) = N_Label
and then Exception_Junk
(Last_Stm
) then
7672 exit when Nkind
(Stm
) /= N_Block_Statement
;
7673 exit when not Exception_Junk
(Stm
);
7676 exit when Nkind
(Stm
) /= N_Label
;
7677 exit when not Exception_Junk
(Stm
);
7678 Check_Statement_Sequence
7679 (Statements
(Handled_Statement_Sequence
(Next
(Stm
))));
7684 exit when Nkind
(Stm
) /= N_Goto_Statement
;
7685 exit when not Exception_Junk
(Stm
);
7689 -- Don't count pragmas
7691 while Nkind
(Last_Stm
) = N_Pragma
7693 -- Don't count call to SS_Release (can happen after
7694 -- Raise_Exception).
7697 (Nkind
(Last_Stm
) = N_Procedure_Call_Statement
7699 Nkind
(Name
(Last_Stm
)) = N_Identifier
7701 Is_RTE
(Entity
(Name
(Last_Stm
)), RE_SS_Release
))
7703 -- Don't count exception junk
7706 (Nkind
(Last_Stm
) in
7707 N_Goto_Statement | N_Label | N_Object_Declaration
7708 and then Exception_Junk
(Last_Stm
))
7709 or else Nkind
(Last_Stm
) in N_Push_xxx_Label | N_Pop_xxx_Label
7711 -- Inserted code, such as finalization calls, is irrelevant; we
7712 -- only need to check original source. If we see a transfer of
7713 -- control, we stop.
7715 or else (Is_Rewrite_Insertion
(Last_Stm
)
7716 and then not Is_Transfer
(Last_Stm
))
7721 -- Here we have the "real" last statement
7723 Kind
:= Nkind
(Last_Stm
);
7725 -- Transfer of control, OK. Note that in the No_Return procedure
7726 -- case, we already diagnosed any explicit return statements, so
7727 -- we can treat them as OK in this context.
7729 if Is_Transfer
(Last_Stm
) then
7732 -- Check cases of explicit non-indirect procedure calls
7734 elsif Kind
= N_Procedure_Call_Statement
7735 and then Is_Entity_Name
(Name
(Last_Stm
))
7737 -- Check call to Raise_Exception procedure which is treated
7738 -- specially, as is a call to Reraise_Occurrence.
7740 -- We suppress the warning in these cases since it is likely that
7741 -- the programmer really does not expect to deal with the case
7742 -- of Null_Occurrence, and thus would find a warning about a
7743 -- missing return curious, and raising Program_Error does not
7744 -- seem such a bad behavior if this does occur.
7746 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7747 -- behavior will be to raise Constraint_Error (see AI-329).
7749 if Is_RTE
(Entity
(Name
(Last_Stm
)), RE_Raise_Exception
)
7751 Is_RTE
(Entity
(Name
(Last_Stm
)), RE_Reraise_Occurrence
)
7753 Raise_Exception_Call
:= True;
7755 -- For Raise_Exception call, test first argument, if it is
7756 -- an attribute reference for a 'Identity call, then we know
7757 -- that the call cannot possibly return.
7760 Arg
: constant Node_Id
:=
7761 Original_Node
(First_Actual
(Last_Stm
));
7763 if Nkind
(Arg
) = N_Attribute_Reference
7764 and then Attribute_Name
(Arg
) = Name_Identity
7771 -- If statement, need to look inside if there is an else and check
7772 -- each constituent statement sequence for proper termination.
7774 elsif Kind
= N_If_Statement
7775 and then Present
(Else_Statements
(Last_Stm
))
7777 Check_Statement_Sequence
(Then_Statements
(Last_Stm
));
7778 Check_Statement_Sequence
(Else_Statements
(Last_Stm
));
7781 Elsif_Part
: Node_Id
:= First
(Elsif_Parts
(Last_Stm
));
7784 while Present
(Elsif_Part
) loop
7785 Check_Statement_Sequence
(Then_Statements
(Elsif_Part
));
7792 -- Case statement, check each case for proper termination
7794 elsif Kind
= N_Case_Statement
then
7798 Case_Alt
:= First_Non_Pragma
(Alternatives
(Last_Stm
));
7799 while Present
(Case_Alt
) loop
7800 Check_Statement_Sequence
(Statements
(Case_Alt
));
7801 Next_Non_Pragma
(Case_Alt
);
7807 -- Block statement, check its handled sequence of statements
7809 elsif Kind
= N_Block_Statement
then
7815 (Handled_Statement_Sequence
(Last_Stm
), Mode
, Err1
);
7824 -- Loop statement. If there is an iteration scheme, we can definitely
7825 -- fall out of the loop. Similarly if there is an exit statement, we
7826 -- can fall out. In either case we need a following return.
7828 elsif Kind
= N_Loop_Statement
then
7829 if Present
(Iteration_Scheme
(Last_Stm
))
7830 or else Has_Exit
(Entity
(Identifier
(Last_Stm
)))
7834 -- A loop with no exit statement or iteration scheme is either
7835 -- an infinite loop, or it has some other exit (raise/return).
7836 -- In either case, no warning is required.
7842 -- Timed entry call, check entry call and delay alternatives
7844 -- Note: in expanded code, the timed entry call has been converted
7845 -- to a set of expanded statements on which the check will work
7846 -- correctly in any case.
7848 elsif Kind
= N_Timed_Entry_Call
then
7850 ECA
: constant Node_Id
:= Entry_Call_Alternative
(Last_Stm
);
7851 DCA
: constant Node_Id
:= Delay_Alternative
(Last_Stm
);
7854 -- If statement sequence of entry call alternative is missing,
7855 -- then we can definitely fall through, and we post the error
7856 -- message on the entry call alternative itself.
7858 if No
(Statements
(ECA
)) then
7861 -- If statement sequence of delay alternative is missing, then
7862 -- we can definitely fall through, and we post the error
7863 -- message on the delay alternative itself.
7865 -- Note: if both ECA and DCA are missing the return, then we
7866 -- post only one message, should be enough to fix the bugs.
7867 -- If not we will get a message next time on the DCA when the
7870 elsif No
(Statements
(DCA
)) then
7873 -- Else check both statement sequences
7876 Check_Statement_Sequence
(Statements
(ECA
));
7877 Check_Statement_Sequence
(Statements
(DCA
));
7882 -- Conditional entry call, check entry call and else part
7884 -- Note: in expanded code, the conditional entry call has been
7885 -- converted to a set of expanded statements on which the check
7886 -- will work correctly in any case.
7888 elsif Kind
= N_Conditional_Entry_Call
then
7890 ECA
: constant Node_Id
:= Entry_Call_Alternative
(Last_Stm
);
7893 -- If statement sequence of entry call alternative is missing,
7894 -- then we can definitely fall through, and we post the error
7895 -- message on the entry call alternative itself.
7897 if No
(Statements
(ECA
)) then
7900 -- Else check statement sequence and else part
7903 Check_Statement_Sequence
(Statements
(ECA
));
7904 Check_Statement_Sequence
(Else_Statements
(Last_Stm
));
7910 -- If we fall through, issue appropriate message
7914 -- Kill warning if last statement is a raise exception call,
7915 -- or a pragma Assert (False). Note that with assertions enabled,
7916 -- such a pragma has been converted into a raise exception call
7917 -- already, so the Assert_False is for the assertions off case.
7919 if not Raise_Exception_Call
and then not Assert_False
then
7921 -- In GNATprove mode, it is an error to have a missing return
7923 Error_Msg_Warn
:= SPARK_Mode
/= On
;
7925 -- Issue error message or warning
7928 ("RETURN statement missing following this statement<<!",
7931 ("\Program_Error [<<!", Last_Stm
);
7934 -- Note: we set Err even though we have not issued a warning
7935 -- because we still have a case of a missing return. This is
7936 -- an extremely marginal case, probably will never be noticed
7937 -- but we might as well get it right.
7941 -- Otherwise we have the case of a procedure marked No_Return
7944 if not Raise_Exception_Call
then
7945 if GNATprove_Mode
then
7947 ("implied return after this statement would have raised "
7948 & "Program_Error", Last_Stm
);
7950 -- In normal compilation mode, do not warn on a generated call
7951 -- (e.g. in the body of a renaming as completion).
7953 elsif Comes_From_Source
(Last_Stm
) then
7955 ("implied return after this statement will raise "
7956 & "Program_Error??", Last_Stm
);
7959 Error_Msg_Warn
:= SPARK_Mode
/= On
;
7961 ("\procedure & is marked as No_Return<<!", Last_Stm
, Proc
);
7965 RE
: constant Node_Id
:=
7966 Make_Raise_Program_Error
(Sloc
(Last_Stm
),
7967 Reason
=> PE_Implicit_Return
);
7969 Insert_After
(Last_Stm
, RE
);
7973 end Check_Statement_Sequence
;
7975 -- Start of processing for Check_Returns
7979 Check_Statement_Sequence
(Statements
(HSS
));
7981 if Present
(Exception_Handlers
(HSS
)) then
7982 Handler
:= First_Non_Pragma
(Exception_Handlers
(HSS
));
7983 while Present
(Handler
) loop
7984 Check_Statement_Sequence
(Statements
(Handler
));
7985 Next_Non_Pragma
(Handler
);
7990 ----------------------------
7991 -- Check_Subprogram_Order --
7992 ----------------------------
7994 procedure Check_Subprogram_Order
(N
: Node_Id
) is
7996 function Subprogram_Name_Greater
(S1
, S2
: String) return Boolean;
7997 -- This is used to check if S1 > S2 in the sense required by this test,
7998 -- for example nameab < namec, but name2 < name10.
8000 -----------------------------
8001 -- Subprogram_Name_Greater --
8002 -----------------------------
8004 function Subprogram_Name_Greater
(S1
, S2
: String) return Boolean is
8009 -- Deal with special case where names are identical except for a
8010 -- numerical suffix. These are handled specially, taking the numeric
8011 -- ordering from the suffix into account.
8014 while S1
(L1
) in '0' .. '9' loop
8019 while S2
(L2
) in '0' .. '9' loop
8023 -- If non-numeric parts non-equal, do straight compare
8025 if S1
(S1
'First .. L1
) /= S2
(S2
'First .. L2
) then
8028 -- If non-numeric parts equal, compare suffixed numeric parts. Note
8029 -- that a missing suffix is treated as numeric zero in this test.
8033 while L1
< S1
'Last loop
8035 N1
:= N1
* 10 + Character'Pos (S1
(L1
)) - Character'Pos ('0');
8039 while L2
< S2
'Last loop
8041 N2
:= N2
* 10 + Character'Pos (S2
(L2
)) - Character'Pos ('0');
8046 end Subprogram_Name_Greater
;
8048 -- Start of processing for Check_Subprogram_Order
8051 -- Check body in alpha order if this is option
8054 and then Style_Check_Order_Subprograms
8055 and then Nkind
(N
) = N_Subprogram_Body
8056 and then Comes_From_Source
(N
)
8057 and then In_Extended_Main_Source_Unit
(N
)
8061 renames Scope_Stack
.Table
8062 (Scope_Stack
.Last
).Last_Subprogram_Name
;
8064 Body_Id
: constant Entity_Id
:=
8065 Defining_Entity
(Specification
(N
));
8068 Get_Decoded_Name_String
(Chars
(Body_Id
));
8071 if Subprogram_Name_Greater
8072 (LSN
.all, Name_Buffer
(1 .. Name_Len
))
8074 Style
.Subprogram_Not_In_Alpha_Order
(Body_Id
);
8080 LSN
:= new String'(Name_Buffer (1 .. Name_Len));
8083 end Check_Subprogram_Order;
8085 ------------------------------
8086 -- Check_Subtype_Conformant --
8087 ------------------------------
8089 procedure Check_Subtype_Conformant
8090 (New_Id : Entity_Id;
8092 Err_Loc : Node_Id := Empty;
8093 Skip_Controlling_Formals : Boolean := False;
8094 Get_Inst : Boolean := False)
8097 pragma Warnings (Off, Result);
8100 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
8101 Skip_Controlling_Formals => Skip_Controlling_Formals,
8102 Get_Inst => Get_Inst);
8103 end Check_Subtype_Conformant;
8105 -----------------------------------
8106 -- Check_Synchronized_Overriding --
8107 -----------------------------------
8109 procedure Check_Synchronized_Overriding
8110 (Def_Id : Entity_Id;
8111 Overridden_Subp : out Entity_Id)
8113 Ifaces_List : Elist_Id;
8117 function Is_Valid_Formal (F : Entity_Id) return Boolean;
8118 -- Predicate for legality rule in 9.4 (11.9/2): If an inherited
8119 -- subprogram is implemented by a protected procedure or entry,
8120 -- its first parameter must be out, in out, or access-to-variable.
8122 function Matches_Prefixed_View_Profile
8123 (Prim_Params : List_Id;
8124 Iface_Params : List_Id) return Boolean;
8125 -- Determine whether a subprogram's parameter profile Prim_Params
8126 -- matches that of a potentially overridden interface subprogram
8127 -- Iface_Params. Also determine if the type of first parameter of
8128 -- Iface_Params is an implemented interface.
8130 ----------------------
8131 -- Is_Valid_Formal --
8132 ----------------------
8134 function Is_Valid_Formal (F : Entity_Id) return Boolean is
8137 Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
8139 (Nkind (Parameter_Type (Parent (F))) = N_Access_Definition
8140 and then not Constant_Present (Parameter_Type (Parent (F))));
8141 end Is_Valid_Formal;
8143 -----------------------------------
8144 -- Matches_Prefixed_View_Profile --
8145 -----------------------------------
8147 function Matches_Prefixed_View_Profile
8148 (Prim_Params : List_Id;
8149 Iface_Params : List_Id) return Boolean
8151 function Is_Implemented
8152 (Ifaces_List : Elist_Id;
8153 Iface : Entity_Id) return Boolean;
8154 -- Determine if Iface is implemented by the current task or
8157 --------------------
8158 -- Is_Implemented --
8159 --------------------
8161 function Is_Implemented
8162 (Ifaces_List : Elist_Id;
8163 Iface : Entity_Id) return Boolean
8165 Iface_Elmt : Elmt_Id;
8168 Iface_Elmt := First_Elmt (Ifaces_List);
8169 while Present (Iface_Elmt) loop
8170 if Node (Iface_Elmt) = Iface then
8174 Next_Elmt (Iface_Elmt);
8182 Iface_Id : Entity_Id;
8183 Iface_Param : Node_Id;
8184 Iface_Typ : Entity_Id;
8185 Prim_Id : Entity_Id;
8186 Prim_Param : Node_Id;
8187 Prim_Typ : Entity_Id;
8189 -- Start of processing for Matches_Prefixed_View_Profile
8192 Iface_Param := First (Iface_Params);
8193 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
8195 if Is_Access_Type (Iface_Typ) then
8196 Iface_Typ := Designated_Type (Iface_Typ);
8199 Prim_Param := First (Prim_Params);
8201 -- The first parameter of the potentially overridden subprogram must
8202 -- be an interface implemented by Prim.
8204 if not Is_Interface (Iface_Typ)
8205 or else not Is_Implemented (Ifaces_List, Iface_Typ)
8210 -- The checks on the object parameters are done, so move on to the
8211 -- rest of the parameters.
8213 if not In_Scope then
8218 while Present (Iface_Param) and then Present (Prim_Param) loop
8219 Iface_Id := Defining_Identifier (Iface_Param);
8220 Iface_Typ := Find_Parameter_Type (Iface_Param);
8222 Prim_Id := Defining_Identifier (Prim_Param);
8223 Prim_Typ := Find_Parameter_Type (Prim_Param);
8225 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
8226 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
8227 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
8229 Iface_Typ := Designated_Type (Iface_Typ);
8230 Prim_Typ := Designated_Type (Prim_Typ);
8233 -- Case of multiple interface types inside a parameter profile
8235 -- (Obj_Param : in out Iface; ...; Param : Iface)
8237 -- If the interface type is implemented, then the matching type in
8238 -- the primitive should be the implementing record type.
8240 if Ekind (Iface_Typ) = E_Record_Type
8241 and then Is_Interface (Iface_Typ)
8242 and then Is_Implemented (Ifaces_List, Iface_Typ)
8244 if Prim_Typ /= Typ then
8248 -- The two parameters must be both mode and subtype conformant
8250 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
8252 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
8261 -- One of the two lists contains more parameters than the other
8263 if Present (Iface_Param) or else Present (Prim_Param) then
8268 end Matches_Prefixed_View_Profile;
8270 -- Start of processing for Check_Synchronized_Overriding
8273 Overridden_Subp := Empty;
8275 -- Def_Id must be an entry or a subprogram. We should skip predefined
8276 -- primitives internally generated by the front end; however at this
8277 -- stage predefined primitives are still not fully decorated. As a
8278 -- minor optimization we skip here internally generated subprograms.
8280 if (Ekind (Def_Id) /= E_Entry
8281 and then Ekind (Def_Id) /= E_Function
8282 and then Ekind (Def_Id) /= E_Procedure)
8283 or else not Comes_From_Source (Def_Id)
8288 -- Search for the concurrent declaration since it contains the list of
8289 -- all implemented interfaces. In this case, the subprogram is declared
8290 -- within the scope of a protected or a task type.
8292 if Present (Scope (Def_Id))
8293 and then Is_Concurrent_Type (Scope (Def_Id))
8294 and then not Is_Generic_Actual_Type (Scope (Def_Id))
8296 Typ := Scope (Def_Id);
8299 -- The enclosing scope is not a synchronized type and the subprogram
8302 elsif No (First_Formal (Def_Id)) then
8305 -- The subprogram has formals and hence it may be a primitive of a
8309 Typ := Etype (First_Formal (Def_Id));
8311 if Is_Access_Type (Typ) then
8312 Typ := Directly_Designated_Type (Typ);
8315 if Is_Concurrent_Type (Typ)
8316 and then not Is_Generic_Actual_Type (Typ)
8320 -- This case occurs when the concurrent type is declared within a
8321 -- generic unit. As a result the corresponding record has been built
8322 -- and used as the type of the first formal, we just have to retrieve
8323 -- the corresponding concurrent type.
8325 elsif Is_Concurrent_Record_Type (Typ)
8326 and then not Is_Class_Wide_Type (Typ)
8327 and then Present (Corresponding_Concurrent_Type (Typ))
8329 Typ := Corresponding_Concurrent_Type (Typ);
8337 -- There is no overriding to check if this is an inherited operation in
8338 -- a type derivation for a generic actual.
8340 Collect_Interfaces (Typ, Ifaces_List);
8342 if Is_Empty_Elmt_List (Ifaces_List) then
8346 -- Determine whether entry or subprogram Def_Id overrides a primitive
8347 -- operation that belongs to one of the interfaces in Ifaces_List.
8350 Candidate : Entity_Id := Empty;
8351 Hom : Entity_Id := Empty;
8352 Subp : Entity_Id := Empty;
8355 -- Traverse the homonym chain, looking for a potentially overridden
8356 -- subprogram that belongs to an implemented interface.
8358 Hom := Current_Entity_In_Scope (Def_Id);
8359 while Present (Hom) loop
8363 or else not Is_Overloadable (Subp)
8364 or else not Is_Primitive (Subp)
8365 or else not Is_Dispatching_Operation (Subp)
8366 or else not Present (Find_Dispatching_Type (Subp))
8367 or else not Is_Interface (Find_Dispatching_Type (Subp))
8371 -- Entries and procedures can override abstract or null interface
8374 elsif Ekind (Def_Id) in E_Entry | E_Procedure
8375 and then Ekind (Subp) = E_Procedure
8376 and then Matches_Prefixed_View_Profile
8377 (Parameter_Specifications (Parent (Def_Id)),
8378 Parameter_Specifications (Parent (Subp)))
8382 -- For an overridden subprogram Subp, check whether the mode
8383 -- of its first parameter is correct depending on the kind of
8384 -- synchronized type.
8387 Formal : constant Node_Id := First_Formal (Candidate);
8390 -- In order for an entry or a protected procedure to
8391 -- override, the first parameter of the overridden routine
8392 -- must be of mode "out", "in out", or access-to-variable.
8394 if Ekind (Candidate) in E_Entry | E_Procedure
8395 and then Is_Protected_Type (Typ)
8396 and then not Is_Valid_Formal (Formal)
8400 -- All other cases are OK since a task entry or routine does
8401 -- not have a restriction on the mode of the first parameter
8402 -- of the overridden interface routine.
8405 Overridden_Subp := Candidate;
8410 -- Functions can override abstract interface functions. Return
8411 -- types must be subtype conformant.
8413 elsif Ekind (Def_Id) = E_Function
8414 and then Ekind (Subp) = E_Function
8415 and then Matches_Prefixed_View_Profile
8416 (Parameter_Specifications (Parent (Def_Id)),
8417 Parameter_Specifications (Parent (Subp)))
8418 and then Conforming_Types
8419 (Etype (Def_Id), Etype (Subp), Subtype_Conformant)
8423 -- If an inherited subprogram is implemented by a protected
8424 -- function, then the first parameter of the inherited
8425 -- subprogram shall be of mode in, but not an access-to-
8426 -- variable parameter (RM 9.4(11/9)).
8428 if Present (First_Formal (Subp))
8429 and then Ekind (First_Formal (Subp)) = E_In_Parameter
8431 (not Is_Access_Type (Etype (First_Formal (Subp)))
8433 Is_Access_Constant (Etype (First_Formal (Subp))))
8435 Overridden_Subp := Subp;
8440 Hom := Homonym (Hom);
8443 -- After examining all candidates for overriding, we are left with
8444 -- the best match, which is a mode-incompatible interface routine.
8446 if In_Scope and then Present (Candidate) then
8447 Error_Msg_PT (Def_Id, Candidate);
8450 Overridden_Subp := Candidate;
8453 end Check_Synchronized_Overriding;
8455 ---------------------------
8456 -- Check_Type_Conformant --
8457 ---------------------------
8459 procedure Check_Type_Conformant
8460 (New_Id : Entity_Id;
8462 Err_Loc : Node_Id := Empty)
8465 pragma Warnings (Off, Result);
8468 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
8469 end Check_Type_Conformant;
8471 ---------------------------
8472 -- Can_Override_Operator --
8473 ---------------------------
8475 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
8479 -- Return False if not an operator. We test the name rather than testing
8480 -- that the Nkind is N_Defining_Operator_Symbol, because there are cases
8481 -- where an operator entity can be an N_Defining_Identifier (such as for
8482 -- function instantiations).
8484 if Chars (Subp) not in Any_Operator_Name then
8488 Typ := Base_Type (Etype (First_Formal (Subp)));
8490 -- Check explicitly that the operation is a primitive of the type
8492 return Operator_Matches_Spec (Subp, Subp)
8493 and then not Is_Generic_Type (Typ)
8494 and then Scope (Subp) = Scope (Typ)
8495 and then not Is_Class_Wide_Type (Typ);
8497 end Can_Override_Operator;
8499 ----------------------
8500 -- Conforming_Types --
8501 ----------------------
8503 function Conforming_Types
8506 Ctype : Conformance_Type;
8507 Get_Inst : Boolean := False) return Boolean
8509 function Base_Types_Match
8511 Typ_2 : Entity_Id) return Boolean;
8512 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
8513 -- in different scopes (e.g. parent and child instances), then verify
8514 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
8515 -- the same subtype chain. The whole purpose of this procedure is to
8516 -- prevent spurious ambiguities in an instantiation that may arise if
8517 -- two distinct generic types are instantiated with the same actual.
8519 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
8520 -- An access parameter can designate an incomplete type. If the
8521 -- incomplete type is the limited view of a type from a limited_
8522 -- with_clause, check whether the non-limited view is available.
8523 -- If it is a (non-limited) incomplete type, get the full view.
8525 function Matches_Limited_With_View
8527 Typ_2 : Entity_Id) return Boolean;
8528 -- Returns True if and only if either Typ_1 denotes a limited view of
8529 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
8530 -- the limited with view of a type is used in a subprogram declaration
8531 -- and the subprogram body is in the scope of a regular with clause for
8532 -- the same unit. In such a case, the two type entities are considered
8533 -- identical for purposes of conformance checking.
8535 ----------------------
8536 -- Base_Types_Match --
8537 ----------------------
8539 function Base_Types_Match
8541 Typ_2 : Entity_Id) return Boolean
8543 Base_1 : constant Entity_Id := Base_Type (Typ_1);
8544 Base_2 : constant Entity_Id := Base_Type (Typ_2);
8547 if Typ_1 = Typ_2 then
8550 elsif Base_1 = Base_2 then
8552 -- The following is too permissive. A more precise test should
8553 -- check that the generic actual is an ancestor subtype of the
8556 -- See code in Find_Corresponding_Spec that applies an additional
8557 -- filter to handle accidental amiguities in instances.
8560 not Is_Generic_Actual_Type (Typ_1)
8561 or else not Is_Generic_Actual_Type (Typ_2)
8562 or else Scope (Typ_1) /= Scope (Typ_2);
8564 -- If Typ_2 is a generic actual type it is declared as the subtype of
8565 -- the actual. If that actual is itself a subtype we need to use its
8566 -- own base type to check for compatibility.
8568 elsif Ekind (Base_2) = Ekind (Typ_2)
8569 and then Base_1 = Base_Type (Base_2)
8573 elsif Ekind (Base_1) = Ekind (Typ_1)
8574 and then Base_2 = Base_Type (Base_1)
8581 end Base_Types_Match;
8583 --------------------------
8584 -- Find_Designated_Type --
8585 --------------------------
8587 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
8591 Desig := Directly_Designated_Type (Typ);
8593 if Ekind (Desig) = E_Incomplete_Type then
8595 -- If regular incomplete type, get full view if available
8597 if Present (Full_View (Desig)) then
8598 Desig := Full_View (Desig);
8600 -- If limited view of a type, get non-limited view if available,
8601 -- and check again for a regular incomplete type.
8603 elsif Present (Non_Limited_View (Desig)) then
8604 Desig := Get_Full_View (Non_Limited_View (Desig));
8609 end Find_Designated_Type;
8611 -------------------------------
8612 -- Matches_Limited_With_View --
8613 -------------------------------
8615 function Matches_Limited_With_View
8617 Typ_2 : Entity_Id) return Boolean
8619 function Is_Matching_Limited_View
8621 View : Entity_Id) return Boolean;
8622 -- Determine whether non-limited view View denotes type Typ in some
8623 -- conformant fashion.
8625 ------------------------------
8626 -- Is_Matching_Limited_View --
8627 ------------------------------
8629 function Is_Matching_Limited_View
8631 View : Entity_Id) return Boolean
8633 Root_Typ : Entity_Id;
8634 Root_View : Entity_Id;
8637 -- The non-limited view directly denotes the type
8642 -- The type is a subtype of the non-limited view
8644 elsif Is_Subtype_Of (Typ, View) then
8647 -- Both the non-limited view and the type denote class-wide types
8649 elsif Is_Class_Wide_Type (Typ)
8650 and then Is_Class_Wide_Type (View)
8652 Root_Typ := Root_Type (Typ);
8653 Root_View := Root_Type (View);
8655 if Root_Typ = Root_View then
8658 -- An incomplete tagged type and its full view may receive two
8659 -- distinct class-wide types when the related package has not
8660 -- been analyzed yet.
8663 -- type T is tagged; -- CW_1
8664 -- type T is tagged null record; -- CW_2
8667 -- This is because the package lacks any semantic information
8668 -- that may eventually link both views of T. As a consequence,
8669 -- a client of the limited view of Pack will see CW_2 while a
8670 -- client of the non-limited view of Pack will see CW_1.
8672 elsif Is_Incomplete_Type (Root_Typ)
8673 and then Present (Full_View (Root_Typ))
8674 and then Full_View (Root_Typ) = Root_View
8678 elsif Is_Incomplete_Type (Root_View)
8679 and then Present (Full_View (Root_View))
8680 and then Full_View (Root_View) = Root_Typ
8687 end Is_Matching_Limited_View;
8689 -- Start of processing for Matches_Limited_With_View
8692 -- In some cases a type imported through a limited_with clause, and
8693 -- its non-limited view are both visible, for example in an anonymous
8694 -- access-to-class-wide type in a formal, or when building the body
8695 -- for a subprogram renaming after the subprogram has been frozen.
8696 -- In these cases both entities designate the same type. In addition,
8697 -- if one of them is an actual in an instance, it may be a subtype of
8698 -- the non-limited view of the other.
8700 if From_Limited_With (Typ_1)
8701 and then From_Limited_With (Typ_2)
8702 and then Available_View (Typ_1) = Available_View (Typ_2)
8706 elsif From_Limited_With (Typ_1) then
8707 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
8709 elsif From_Limited_With (Typ_2) then
8710 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
8715 end Matches_Limited_With_View;
8719 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
8721 Type_1 : Entity_Id := T1;
8722 Type_2 : Entity_Id := T2;
8724 -- Start of processing for Conforming_Types
8727 -- The context is an instance association for a formal access-to-
8728 -- subprogram type; the formal parameter types require mapping because
8729 -- they may denote other formal parameters of the generic unit.
8732 Type_1 := Get_Instance_Of (T1);
8733 Type_2 := Get_Instance_Of (T2);
8736 -- If one of the types is a view of the other introduced by a limited
8737 -- with clause, treat these as conforming for all purposes.
8739 if Matches_Limited_With_View (T1, T2) then
8742 elsif Base_Types_Match (Type_1, Type_2) then
8743 if Ctype <= Mode_Conformant then
8748 Subtypes_Statically_Match (Type_1, Type_2)
8749 and then Dimensions_Match (Type_1, Type_2);
8752 elsif Is_Incomplete_Or_Private_Type (Type_1)
8753 and then Present (Full_View (Type_1))
8754 and then Base_Types_Match (Full_View (Type_1), Type_2)
8757 Ctype <= Mode_Conformant
8758 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
8760 elsif Ekind (Type_2) = E_Incomplete_Type
8761 and then Present (Full_View (Type_2))
8762 and then Base_Types_Match (Type_1, Full_View (Type_2))
8765 Ctype <= Mode_Conformant
8766 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8768 elsif Is_Private_Type (Type_2)
8769 and then In_Instance
8770 and then Present (Full_View (Type_2))
8771 and then Base_Types_Match (Type_1, Full_View (Type_2))
8774 Ctype <= Mode_Conformant
8775 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8777 -- Another confusion between views in a nested instance with an
8778 -- actual private type whose full view is not in scope.
8780 elsif Ekind (Type_2) = E_Private_Subtype
8781 and then In_Instance
8782 and then Etype (Type_2) = Type_1
8786 -- In Ada 2012, incomplete types (including limited views) can appear
8787 -- as actuals in instantiations, where they are conformant to the
8788 -- corresponding incomplete formal.
8790 elsif Is_Incomplete_Type (Type_1)
8791 and then Is_Incomplete_Type (Type_2)
8792 and then In_Instance
8793 and then (Used_As_Generic_Actual (Type_1)
8794 or else Used_As_Generic_Actual (Type_2))
8799 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
8800 -- treated recursively because they carry a signature. As far as
8801 -- conformance is concerned, convention plays no role, and either
8802 -- or both could be access to protected subprograms.
8804 Are_Anonymous_Access_To_Subprogram_Types :=
8805 Ekind (Type_1) in E_Anonymous_Access_Subprogram_Type
8806 | E_Anonymous_Access_Protected_Subprogram_Type
8808 Ekind (Type_2) in E_Anonymous_Access_Subprogram_Type
8809 | E_Anonymous_Access_Protected_Subprogram_Type;
8811 -- Test anonymous access type case. For this case, static subtype
8812 -- matching is required for mode conformance (RM 6.3.1(15)). We check
8813 -- the base types because we may have built internal subtype entities
8814 -- to handle null-excluding types (see Process_Formals).
8816 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
8818 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
8820 -- Ada 2005 (AI-254)
8822 or else Are_Anonymous_Access_To_Subprogram_Types
8825 Desig_1 : Entity_Id;
8826 Desig_2 : Entity_Id;
8829 -- In Ada 2005, access constant indicators must match for
8830 -- subtype conformance.
8832 if Ada_Version >= Ada_2005
8833 and then Ctype >= Subtype_Conformant
8835 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
8840 Desig_1 := Find_Designated_Type (Type_1);
8841 Desig_2 := Find_Designated_Type (Type_2);
8843 -- If the context is an instance association for a formal
8844 -- access-to-subprogram type; formal access parameter designated
8845 -- types require mapping because they may denote other formal
8846 -- parameters of the generic unit.
8849 Desig_1 := Get_Instance_Of (Desig_1);
8850 Desig_2 := Get_Instance_Of (Desig_2);
8853 -- It is possible for a Class_Wide_Type to be introduced for an
8854 -- incomplete type, in which case there is a separate class_ wide
8855 -- type for the full view. The types conform if their Etypes
8856 -- conform, i.e. one may be the full view of the other. This can
8857 -- only happen in the context of an access parameter, other uses
8858 -- of an incomplete Class_Wide_Type are illegal.
8860 if Is_Class_Wide_Type (Desig_1)
8862 Is_Class_Wide_Type (Desig_2)
8866 (Etype (Base_Type (Desig_1)),
8867 Etype (Base_Type (Desig_2)), Ctype);
8869 elsif Are_Anonymous_Access_To_Subprogram_Types then
8870 if Ada_Version < Ada_2005 then
8872 Ctype = Type_Conformant
8873 or else Subtypes_Statically_Match (Desig_1, Desig_2);
8875 -- We must check the conformance of the signatures themselves
8879 Conformant : Boolean;
8882 (Desig_1, Desig_2, Ctype, False, Conformant);
8887 -- A limited view of an actual matches the corresponding
8888 -- incomplete formal.
8890 elsif Ekind (Desig_2) = E_Incomplete_Subtype
8891 and then From_Limited_With (Desig_2)
8892 and then Used_As_Generic_Actual (Etype (Desig_2))
8897 return Base_Type (Desig_1) = Base_Type (Desig_2)
8898 and then (Ctype = Type_Conformant
8900 Subtypes_Statically_Match (Desig_1, Desig_2));
8904 -- Otherwise definitely no match
8907 if ((Ekind (Type_1) = E_Anonymous_Access_Type
8908 and then Is_Access_Type (Type_2))
8909 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8910 and then Is_Access_Type (Type_1)))
8913 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
8915 May_Hide_Profile := True;
8920 end Conforming_Types;
8922 --------------------------
8923 -- Create_Extra_Formals --
8924 --------------------------
8926 procedure Create_Extra_Formals (E : Entity_Id) is
8927 First_Extra : Entity_Id := Empty;
8929 Last_Extra : Entity_Id := Empty;
8931 function Add_Extra_Formal
8932 (Assoc_Entity : Entity_Id;
8935 Suffix : String) return Entity_Id;
8936 -- Add an extra formal to the current list of formals and extra formals.
8937 -- The extra formal is added to the end of the list of extra formals,
8938 -- and also returned as the result. These formals are always of mode IN.
8939 -- The new formal has the type Typ, is declared in Scope, and its name
8940 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
8941 -- The following suffixes are currently used. They should not be changed
8942 -- without coordinating with CodePeer, which makes use of these to
8943 -- provide better messages.
8945 -- O denotes the Constrained bit.
8946 -- L denotes the accessibility level.
8947 -- BIP_xxx denotes an extra formal for a build-in-place function. See
8948 -- the full list in exp_ch6.BIP_Formal_Kind.
8950 ----------------------
8951 -- Add_Extra_Formal --
8952 ----------------------
8954 function Add_Extra_Formal
8955 (Assoc_Entity : Entity_Id;
8958 Suffix : String) return Entity_Id
8960 EF : constant Entity_Id :=
8961 Make_Defining_Identifier (Sloc (Assoc_Entity),
8962 Chars => New_External_Name (Chars (Assoc_Entity),
8966 -- A little optimization. Never generate an extra formal for the
8967 -- _init operand of an initialization procedure, since it could
8970 if Chars (Formal) = Name_uInit then
8974 Mutate_Ekind (EF, E_In_Parameter);
8975 Set_Actual_Subtype (EF, Typ);
8976 Set_Etype (EF, Typ);
8977 Set_Scope (EF, Scope);
8978 Set_Mechanism (EF, Default_Mechanism);
8979 Set_Formal_Validity (EF);
8981 if No (First_Extra) then
8983 Set_Extra_Formals (Scope, EF);
8986 if Present (Last_Extra) then
8987 Set_Extra_Formal (Last_Extra, EF);
8993 end Add_Extra_Formal;
8997 Formal_Type : Entity_Id;
8998 P_Formal : Entity_Id;
9000 -- Start of processing for Create_Extra_Formals
9003 -- We never generate extra formals if expansion is not active because we
9004 -- don't need them unless we are generating code.
9006 if not Expander_Active then
9010 -- No need to generate extra formals in thunks whose target has no extra
9011 -- formals, but we can have two of them chained (interface and stack).
9013 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Target (E))) then
9017 -- If this is a derived subprogram then the subtypes of the parent
9018 -- subprogram's formal parameters will be used to determine the need
9019 -- for extra formals.
9021 if Is_Overloadable (E) and then Present (Alias (E)) then
9022 P_Formal := First_Formal (Alias (E));
9027 Formal := First_Formal (E);
9028 while Present (Formal) loop
9029 Last_Extra := Formal;
9030 Next_Formal (Formal);
9033 -- If Extra_Formals were already created, don't do it again. This
9034 -- situation may arise for subprogram types created as part of
9035 -- dispatching calls (see Expand_Dispatching_Call).
9037 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
9041 -- If the subprogram is a predefined dispatching subprogram then don't
9042 -- generate any extra constrained or accessibility level formals. In
9043 -- general we suppress these for internal subprograms (by not calling
9044 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
9045 -- generated stream attributes do get passed through because extra
9046 -- build-in-place formals are needed in some cases (limited 'Input
).
9048 if Is_Predefined_Internal_Operation
(E
) then
9049 goto Test_For_Func_Result_Extras
;
9052 Formal
:= First_Formal
(E
);
9053 while Present
(Formal
) loop
9055 -- Create extra formal for supporting the attribute 'Constrained.
9056 -- The case of a private type view without discriminants also
9057 -- requires the extra formal if the underlying type has defaulted
9060 if Ekind
(Formal
) /= E_In_Parameter
then
9061 if Present
(P_Formal
) then
9062 Formal_Type
:= Etype
(P_Formal
);
9064 Formal_Type
:= Etype
(Formal
);
9067 -- Do not produce extra formals for Unchecked_Union parameters.
9068 -- Jump directly to the end of the loop.
9070 if Is_Unchecked_Union
(Base_Type
(Formal_Type
)) then
9071 goto Skip_Extra_Formal_Generation
;
9074 if not Has_Discriminants
(Formal_Type
)
9075 and then Is_Private_Type
(Formal_Type
)
9076 and then Present
(Underlying_Type
(Formal_Type
))
9078 Formal_Type
:= Underlying_Type
(Formal_Type
);
9081 -- Suppress the extra formal if formal's subtype is constrained or
9082 -- indefinite, or we're compiling for Ada 2012 and the underlying
9083 -- type is tagged and limited. In Ada 2012, a limited tagged type
9084 -- can have defaulted discriminants, but 'Constrained is required
9085 -- to return True, so the formal is never needed (see AI05-0214).
9086 -- Note that this ensures consistency of calling sequences for
9087 -- dispatching operations when some types in a class have defaults
9088 -- on discriminants and others do not (and requiring the extra
9089 -- formal would introduce distributed overhead).
9091 -- If the type does not have a completion yet, treat as prior to
9092 -- Ada 2012 for consistency.
9094 if Has_Discriminants
(Formal_Type
)
9095 and then not Is_Constrained
(Formal_Type
)
9096 and then Is_Definite_Subtype
(Formal_Type
)
9097 and then (Ada_Version
< Ada_2012
9098 or else No
(Underlying_Type
(Formal_Type
))
9100 (Is_Limited_Type
(Formal_Type
)
9103 (Underlying_Type
(Formal_Type
)))))
9105 Set_Extra_Constrained
9106 (Formal
, Add_Extra_Formal
(Formal
, Standard_Boolean
, E
, "O"));
9110 -- Create extra formal for supporting accessibility checking. This
9111 -- is done for both anonymous access formals and formals of named
9112 -- access types that are marked as controlling formals. The latter
9113 -- case can occur when Expand_Dispatching_Call creates a subprogram
9114 -- type and substitutes the types of access-to-class-wide actuals
9115 -- for the anonymous access-to-specific-type of controlling formals.
9116 -- Base_Type is applied because in cases where there is a null
9117 -- exclusion the formal may have an access subtype.
9119 -- This is suppressed if we specifically suppress accessibility
9120 -- checks at the package level for either the subprogram, or the
9121 -- package in which it resides. However, we do not suppress it
9122 -- simply if the scope has accessibility checks suppressed, since
9123 -- this could cause trouble when clients are compiled with a
9124 -- different suppression setting. The explicit checks at the
9125 -- package level are safe from this point of view.
9127 if (Ekind
(Base_Type
(Etype
(Formal
))) = E_Anonymous_Access_Type
9128 or else (Is_Controlling_Formal
(Formal
)
9129 and then Is_Access_Type
(Base_Type
(Etype
(Formal
)))))
9131 (Explicit_Suppress
(E
, Accessibility_Check
)
9133 Explicit_Suppress
(Scope
(E
), Accessibility_Check
))
9136 or else Present
(Extra_Accessibility
(P_Formal
)))
9138 Set_Extra_Accessibility
9139 (Formal
, Add_Extra_Formal
(Formal
, Standard_Natural
, E
, "L"));
9142 -- This label is required when skipping extra formal generation for
9143 -- Unchecked_Union parameters.
9145 <<Skip_Extra_Formal_Generation
>>
9147 if Present
(P_Formal
) then
9148 Next_Formal
(P_Formal
);
9151 Next_Formal
(Formal
);
9154 <<Test_For_Func_Result_Extras
>>
9156 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
9157 -- function call is ... determined by the point of call ...".
9159 if Needs_Result_Accessibility_Level
(E
) then
9160 Set_Extra_Accessibility_Of_Result
9161 (E
, Add_Extra_Formal
(E
, Standard_Natural
, E
, "L"));
9164 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
9165 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
9167 if Is_Build_In_Place_Function
(E
) then
9169 Result_Subt
: constant Entity_Id
:= Etype
(E
);
9170 Formal_Typ
: Entity_Id
;
9171 Subp_Decl
: Node_Id
;
9172 Discard
: Entity_Id
;
9175 -- In the case of functions with unconstrained result subtypes,
9176 -- add a 4-state formal indicating whether the return object is
9177 -- allocated by the caller (1), or should be allocated by the
9178 -- callee on the secondary stack (2), in the global heap (3), or
9179 -- in a user-defined storage pool (4). For the moment we just use
9180 -- Natural for the type of this formal. Note that this formal
9181 -- isn't usually needed in the case where the result subtype is
9182 -- constrained, but it is needed when the function has a tagged
9183 -- result, because generally such functions can be called in a
9184 -- dispatching context and such calls must be handled like calls
9185 -- to a class-wide function.
9187 if Needs_BIP_Alloc_Form
(E
) then
9190 (E
, Standard_Natural
,
9191 E
, BIP_Formal_Suffix
(BIP_Alloc_Form
));
9193 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
9194 -- use a user-defined pool. This formal is not added on
9195 -- ZFP as those targets do not support pools.
9197 if RTE_Available
(RE_Root_Storage_Pool_Ptr
) then
9200 (E
, RTE
(RE_Root_Storage_Pool_Ptr
),
9201 E
, BIP_Formal_Suffix
(BIP_Storage_Pool
));
9205 -- In the case of functions whose result type needs finalization,
9206 -- add an extra formal which represents the finalization master.
9208 if Needs_BIP_Finalization_Master
(E
) then
9211 (E
, RTE
(RE_Finalization_Master_Ptr
),
9212 E
, BIP_Formal_Suffix
(BIP_Finalization_Master
));
9215 -- When the result type contains tasks, add two extra formals: the
9216 -- master of the tasks to be created, and the caller's activation
9219 if Needs_BIP_Task_Actuals
(E
) then
9222 (E
, Standard_Integer
,
9223 E
, BIP_Formal_Suffix
(BIP_Task_Master
));
9225 Set_Has_Master_Entity
(E
);
9229 (E
, RTE
(RE_Activation_Chain_Access
),
9230 E
, BIP_Formal_Suffix
(BIP_Activation_Chain
));
9233 -- All build-in-place functions get an extra formal that will be
9234 -- passed the address of the return object within the caller.
9237 Create_Itype
(E_Anonymous_Access_Type
, E
, Scope_Id
=> Scope
(E
));
9239 -- Incomplete_View_From_Limited_With is needed here because
9240 -- gigi gets confused if the designated type is the full view
9241 -- coming from a limited-with'ed package. In the normal case,
9242 -- (no limited with) Incomplete_View_From_Limited_With
9243 -- returns Result_Subt.
9245 Set_Directly_Designated_Type
9246 (Formal_Typ
, Incomplete_View_From_Limited_With
(Result_Subt
));
9247 Set_Etype
(Formal_Typ
, Formal_Typ
);
9248 Set_Depends_On_Private
9249 (Formal_Typ
, Has_Private_Component
(Formal_Typ
));
9250 Set_Is_Public
(Formal_Typ
, Is_Public
(Scope
(Formal_Typ
)));
9251 Set_Is_Access_Constant
(Formal_Typ
, False);
9253 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
9254 -- the designated type comes from the limited view (for back-end
9257 Set_From_Limited_With
9258 (Formal_Typ
, From_Limited_With
(Result_Subt
));
9260 Layout_Type
(Formal_Typ
);
9262 -- Force the definition of the Itype in case of internal function
9263 -- calls within the same or nested scope.
9265 if Is_Subprogram_Or_Generic_Subprogram
(E
)
9266 and then not Is_Compilation_Unit
(E
)
9268 Subp_Decl
:= Parent
(E
);
9270 -- The insertion point for an Itype reference should be after
9271 -- the unit declaration node of the subprogram. An exception
9272 -- to this are inherited operations from a parent type in which
9273 -- case the derived type acts as their parent.
9275 if Nkind
(Subp_Decl
) in N_Function_Specification
9276 | N_Procedure_Specification
9278 Subp_Decl
:= Parent
(Subp_Decl
);
9281 Build_Itype_Reference
(Formal_Typ
, Subp_Decl
);
9286 (E
, Formal_Typ
, E
, BIP_Formal_Suffix
(BIP_Object_Access
));
9290 -- If this is an instance of a generic, we need to have extra formals
9293 if Is_Generic_Instance
(E
) and then Present
(Alias
(E
)) then
9294 Set_Extra_Formals
(Alias
(E
), Extra_Formals
(E
));
9296 end Create_Extra_Formals
;
9298 -----------------------------
9299 -- Enter_Overloaded_Entity --
9300 -----------------------------
9302 procedure Enter_Overloaded_Entity
(S
: Entity_Id
) is
9303 function Matches_Predefined_Op
return Boolean;
9304 -- This returns an approximation of whether S matches a predefined
9305 -- operator, based on the operator symbol, and the parameter and result
9306 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
9308 ---------------------------
9309 -- Matches_Predefined_Op --
9310 ---------------------------
9312 function Matches_Predefined_Op
return Boolean is
9313 Formal_1
: constant Entity_Id
:= First_Formal
(S
);
9314 Formal_2
: constant Entity_Id
:= Next_Formal
(Formal_1
);
9315 Op
: constant Name_Id
:= Chars
(S
);
9316 Result_Type
: constant Entity_Id
:= Base_Type
(Etype
(S
));
9317 Type_1
: constant Entity_Id
:= Base_Type
(Etype
(Formal_1
));
9322 if Present
(Formal_2
) then
9324 Type_2
: constant Entity_Id
:= Base_Type
(Etype
(Formal_2
));
9327 -- All but "&" and "**" have same-types parameters
9336 if Type_1
/= Type_2
then
9341 -- Check parameter and result types
9349 Is_Boolean_Type
(Result_Type
)
9350 and then Result_Type
= Type_1
;
9356 Is_Integer_Type
(Result_Type
)
9357 and then Result_Type
= Type_1
;
9365 Is_Numeric_Type
(Result_Type
)
9366 and then Result_Type
= Type_1
;
9372 Is_Boolean_Type
(Result_Type
)
9373 and then not Is_Limited_Type
(Type_1
);
9381 Is_Boolean_Type
(Result_Type
)
9382 and then (Is_Array_Type
(Type_1
)
9383 or else Is_Scalar_Type
(Type_1
));
9385 when Name_Op_Concat
=>
9386 return Is_Array_Type
(Result_Type
);
9388 when Name_Op_Expon
=>
9390 (Is_Integer_Type
(Result_Type
)
9391 or else Is_Floating_Point_Type
(Result_Type
))
9392 and then Result_Type
= Type_1
9393 and then Type_2
= Standard_Integer
;
9396 raise Program_Error
;
9409 Is_Numeric_Type
(Result_Type
)
9410 and then Result_Type
= Type_1
;
9414 Is_Boolean_Type
(Result_Type
)
9415 and then Result_Type
= Type_1
;
9418 raise Program_Error
;
9421 end Matches_Predefined_Op
;
9425 E
: Entity_Id
:= Current_Entity_In_Scope
(S
);
9426 C_E
: Entity_Id
:= Current_Entity
(S
);
9428 -- Start of processing for Enter_Overloaded_Entity
9432 Set_Has_Homonym
(E
);
9433 Set_Has_Homonym
(S
);
9436 Set_Is_Immediately_Visible
(S
);
9437 Set_Scope
(S
, Current_Scope
);
9439 -- Chain new entity if front of homonym in current scope, so that
9440 -- homonyms are contiguous.
9442 if Present
(E
) and then E
/= C_E
then
9443 while Homonym
(C_E
) /= E
loop
9444 C_E
:= Homonym
(C_E
);
9447 Set_Homonym
(C_E
, S
);
9451 Set_Current_Entity
(S
);
9456 if Is_Inherited_Operation
(S
) then
9457 Append_Inherited_Subprogram
(S
);
9459 Append_Entity
(S
, Current_Scope
);
9462 Set_Public_Status
(S
);
9464 if Debug_Flag_E
then
9465 Write_Str
("New overloaded entity chain: ");
9466 Write_Name
(Chars
(S
));
9469 while Present
(E
) loop
9470 Write_Str
(" "); Write_Int
(Int
(E
));
9477 -- Generate warning for hiding
9480 and then Comes_From_Source
(S
)
9481 and then In_Extended_Main_Source_Unit
(S
)
9488 -- Warn unless genuine overloading. Do not emit warning on
9489 -- hiding predefined operators in Standard (these are either an
9490 -- artifact of our implicit declarations, or simple noise) but
9491 -- keep warning on a operator defined on a local subtype, because
9492 -- of the real danger that different operators may be applied in
9493 -- various parts of the program.
9495 -- Note that if E and S have the same scope, there is never any
9496 -- hiding. Either the two conflict, and the program is illegal,
9497 -- or S is overriding an implicit inherited subprogram.
9499 if Scope
(E
) /= Scope
(S
)
9500 and then (not Is_Overloadable
(E
)
9501 or else Subtype_Conformant
(E
, S
))
9502 and then (Is_Immediately_Visible
(E
)
9503 or else Is_Potentially_Use_Visible
(S
))
9505 if Scope
(E
) = Standard_Standard
then
9506 if Nkind
(S
) = N_Defining_Operator_Symbol
9507 and then Scope
(Base_Type
(Etype
(First_Formal
(S
)))) /=
9509 and then Matches_Predefined_Op
9512 ("declaration of & hides predefined operator?h?", S
);
9515 -- E not immediately within Standard
9518 Error_Msg_Sloc
:= Sloc
(E
);
9519 Error_Msg_N
("declaration of & hides one #?h?", S
);
9524 end Enter_Overloaded_Entity
;
9526 -----------------------------
9527 -- Check_Untagged_Equality --
9528 -----------------------------
9530 procedure Check_Untagged_Equality
(Eq_Op
: Entity_Id
) is
9531 Eq_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Eq_Op
);
9532 Typ
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
9534 procedure Freezing_Point_Warning
(N
: Node_Id
; S
: String);
9535 -- Output a warning about the freezing point N of Typ
9537 function Is_Actual_Of_Instantiation
9539 Inst
: Node_Id
) return Boolean;
9540 -- Return True if E is an actual parameter of instantiation Inst
9542 -----------------------------------
9543 -- Output_Freezing_Point_Warning --
9544 -----------------------------------
9546 procedure Freezing_Point_Warning
(N
: Node_Id
; S
: String) is
9548 Error_Msg_String
(1 .. S
'Length) := S
;
9549 Error_Msg_Strlen
:= S
'Length;
9551 if Ada_Version
>= Ada_2012
then
9552 Error_Msg_NE
("type& is frozen by ~??", N
, Typ
);
9554 ("\an equality operator cannot be declared after this point??",
9558 Error_Msg_NE
("type& is frozen by ~ (Ada 2012)?y?", N
, Typ
);
9560 ("\an equality operator cannot be declared after this point"
9561 & " (Ada 2012)?y?", N
);
9563 end Freezing_Point_Warning
;
9565 --------------------------------
9566 -- Is_Actual_Of_Instantiation --
9567 --------------------------------
9569 function Is_Actual_Of_Instantiation
9571 Inst
: Node_Id
) return Boolean
9576 if Present
(Generic_Associations
(Inst
)) then
9577 Assoc
:= First
(Generic_Associations
(Inst
));
9579 while Present
(Assoc
) loop
9580 if Present
(Explicit_Generic_Actual_Parameter
(Assoc
))
9582 Is_Entity_Name
(Explicit_Generic_Actual_Parameter
(Assoc
))
9584 Entity
(Explicit_Generic_Actual_Parameter
(Assoc
)) = E
9594 end Is_Actual_Of_Instantiation
;
9600 -- Start of processing for Check_Untagged_Equality
9603 -- This check applies only if we have a subprogram declaration or a
9604 -- subprogram body that is not a completion, for an untagged record
9605 -- type, and that is conformant with the predefined operator.
9607 if (Nkind
(Eq_Decl
) /= N_Subprogram_Declaration
9608 and then not (Nkind
(Eq_Decl
) = N_Subprogram_Body
9609 and then Acts_As_Spec
(Eq_Decl
)))
9610 or else not Is_Record_Type
(Typ
)
9611 or else Is_Tagged_Type
(Typ
)
9612 or else not Is_User_Defined_Equality
(Eq_Op
)
9617 -- In Ada 2012 case, we will output errors or warnings depending on
9618 -- the setting of debug flag -gnatd.E.
9620 if Ada_Version
>= Ada_2012
then
9621 Error_Msg_Warn
:= Debug_Flag_Dot_EE
;
9623 -- In earlier versions of Ada, nothing to do unless we are warning on
9624 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
9627 if not Warn_On_Ada_2012_Compatibility
then
9632 -- Cases where the type has already been frozen
9634 if Is_Frozen
(Typ
) then
9636 -- The check applies to a primitive operation, so check that type
9637 -- and equality operation are in the same scope.
9639 if Scope
(Typ
) /= Current_Scope
then
9642 -- If the type is a generic actual (sub)type, the operation is not
9643 -- primitive either because the base type is declared elsewhere.
9645 elsif Is_Generic_Actual_Type
(Typ
) then
9648 -- Here we may have an error of declaration after freezing, but we
9649 -- must make sure not to flag the equality operator itself causing
9650 -- the freezing when it is a subprogram body.
9653 Decl
:= Next
(Declaration_Node
(Typ
));
9655 while Present
(Decl
) and then Decl
/= Eq_Decl
loop
9657 -- The declaration of an object of the type
9659 if Nkind
(Decl
) = N_Object_Declaration
9660 and then Etype
(Defining_Identifier
(Decl
)) = Typ
9662 Freezing_Point_Warning
(Decl
, "declaration");
9665 -- The instantiation of a generic on the type
9667 elsif Nkind
(Decl
) in N_Generic_Instantiation
9668 and then Is_Actual_Of_Instantiation
(Typ
, Decl
)
9670 Freezing_Point_Warning
(Decl
, "instantiation");
9673 -- A noninstance proper body, body stub or entry body
9675 elsif Nkind
(Decl
) in N_Proper_Body
9678 and then not Is_Generic_Instance
(Defining_Entity
(Decl
))
9680 Freezing_Point_Warning
(Decl
, "body");
9683 -- If we have reached the freeze node and immediately after we
9684 -- have the body or generated code for the body, then it is the
9685 -- body that caused the freezing and this is legal.
9687 elsif Nkind
(Decl
) = N_Freeze_Entity
9688 and then Entity
(Decl
) = Typ
9689 and then (Next
(Decl
) = Eq_Decl
9691 Sloc
(Next
(Decl
)) = Sloc
(Eq_Decl
))
9699 -- Here we have a definite error of declaration after freezing
9701 if Ada_Version
>= Ada_2012
then
9703 ("equality operator must be declared before type & is "
9704 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op
, Typ
);
9706 -- In Ada 2012 mode with error turned to warning, output one
9707 -- more warning to warn that the equality operation may not
9708 -- compose. This is the consequence of ignoring the error.
9710 if Error_Msg_Warn
then
9711 Error_Msg_N
("\equality operation may not compose??", Eq_Op
);
9716 ("equality operator must be declared before type& is "
9717 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op
, Typ
);
9720 -- If we have found no freezing point and the declaration of the
9721 -- operator could not be reached from that of the type and we are
9722 -- in a package body, this must be because the type is declared
9723 -- in the spec of the package. Add a message tailored to this.
9725 if No
(Decl
) and then In_Package_Body
(Scope
(Typ
)) then
9726 if Ada_Version
>= Ada_2012
then
9727 if Nkind
(Eq_Decl
) = N_Subprogram_Body
then
9729 ("\put declaration in package spec<<", Eq_Op
);
9732 ("\move declaration to package spec<<", Eq_Op
);
9736 if Nkind
(Eq_Decl
) = N_Subprogram_Body
then
9738 ("\put declaration in package spec (Ada 2012)?y?",
9742 ("\move declaration to package spec (Ada 2012)?y?",
9749 -- Now check for AI12-0352: the declaration of a user-defined primitive
9750 -- equality operation for a record type T is illegal if it occurs after
9751 -- a type has been derived from T.
9754 Decl
:= Next
(Declaration_Node
(Typ
));
9756 while Present
(Decl
) and then Decl
/= Eq_Decl
loop
9757 if Nkind
(Decl
) = N_Full_Type_Declaration
9758 and then Etype
(Defining_Identifier
(Decl
)) = Typ
9761 ("equality operator cannot appear after derivation", Eq_Op
);
9763 ("an equality operator for& cannot be declared after "
9771 end Check_Untagged_Equality
;
9773 -----------------------------
9774 -- Find_Corresponding_Spec --
9775 -----------------------------
9777 function Find_Corresponding_Spec
9779 Post_Error
: Boolean := True) return Entity_Id
9781 Spec
: constant Node_Id
:= Specification
(N
);
9782 Designator
: constant Entity_Id
:= Defining_Entity
(Spec
);
9786 function Different_Generic_Profile
(E
: Entity_Id
) return Boolean;
9787 -- Even if fully conformant, a body may depend on a generic actual when
9788 -- the spec does not, or vice versa, in which case they were distinct
9789 -- entities in the generic.
9791 -------------------------------
9792 -- Different_Generic_Profile --
9793 -------------------------------
9795 function Different_Generic_Profile
(E
: Entity_Id
) return Boolean is
9798 function Same_Generic_Actual
(T1
, T2
: Entity_Id
) return Boolean;
9799 -- Check that the types of corresponding formals have the same
9800 -- generic actual if any. We have to account for subtypes of a
9801 -- generic formal, declared between a spec and a body, which may
9802 -- appear distinct in an instance but matched in the generic, and
9803 -- the subtype may be used either in the spec or the body of the
9804 -- subprogram being checked.
9806 -------------------------
9807 -- Same_Generic_Actual --
9808 -------------------------
9810 function Same_Generic_Actual
(T1
, T2
: Entity_Id
) return Boolean is
9812 function Is_Declared_Subtype
(S1
, S2
: Entity_Id
) return Boolean;
9813 -- Predicate to check whether S1 is a subtype of S2 in the source
9816 -------------------------
9817 -- Is_Declared_Subtype --
9818 -------------------------
9820 function Is_Declared_Subtype
(S1
, S2
: Entity_Id
) return Boolean is
9822 return Comes_From_Source
(Parent
(S1
))
9823 and then Nkind
(Parent
(S1
)) = N_Subtype_Declaration
9824 and then Is_Entity_Name
(Subtype_Indication
(Parent
(S1
)))
9825 and then Entity
(Subtype_Indication
(Parent
(S1
))) = S2
;
9826 end Is_Declared_Subtype
;
9828 -- Start of processing for Same_Generic_Actual
9831 return Is_Generic_Actual_Type
(T1
) = Is_Generic_Actual_Type
(T2
)
9832 or else Is_Declared_Subtype
(T1
, T2
)
9833 or else Is_Declared_Subtype
(T2
, T1
);
9834 end Same_Generic_Actual
;
9836 -- Start of processing for Different_Generic_Profile
9839 if not In_Instance
then
9842 elsif Ekind
(E
) = E_Function
9843 and then not Same_Generic_Actual
(Etype
(E
), Etype
(Designator
))
9848 F1
:= First_Formal
(Designator
);
9849 F2
:= First_Formal
(E
);
9850 while Present
(F1
) loop
9851 if not Same_Generic_Actual
(Etype
(F1
), Etype
(F2
)) then
9860 end Different_Generic_Profile
;
9862 -- Start of processing for Find_Corresponding_Spec
9865 E
:= Current_Entity
(Designator
);
9866 while Present
(E
) loop
9868 -- We are looking for a matching spec. It must have the same scope,
9869 -- and the same name, and either be type conformant, or be the case
9870 -- of a library procedure spec and its body (which belong to one
9871 -- another regardless of whether they are type conformant or not).
9873 if Scope
(E
) = Current_Scope
then
9874 if Current_Scope
= Standard_Standard
9875 or else (Ekind
(E
) = Ekind
(Designator
)
9876 and then Type_Conformant
(E
, Designator
))
9878 -- Within an instantiation, we know that spec and body are
9879 -- subtype conformant, because they were subtype conformant in
9880 -- the generic. We choose the subtype-conformant entity here as
9881 -- well, to resolve spurious ambiguities in the instance that
9882 -- were not present in the generic (i.e. when two different
9883 -- types are given the same actual). If we are looking for a
9884 -- spec to match a body, full conformance is expected.
9888 -- Inherit the convention and "ghostness" of the matching
9889 -- spec to ensure proper full and subtype conformance.
9891 Set_Convention
(Designator
, Convention
(E
));
9893 -- Skip past subprogram bodies and subprogram renamings that
9894 -- may appear to have a matching spec, but that aren't fully
9895 -- conformant with it. That can occur in cases where an
9896 -- actual type causes unrelated homographs in the instance.
9898 if Nkind
(N
) in N_Subprogram_Body
9899 | N_Subprogram_Renaming_Declaration
9900 and then Present
(Homonym
(E
))
9901 and then not Fully_Conformant
(Designator
, E
)
9905 elsif not Subtype_Conformant
(Designator
, E
) then
9908 elsif Different_Generic_Profile
(E
) then
9913 -- Ada 2012 (AI05-0165): For internally generated bodies of
9914 -- null procedures locate the internally generated spec. We
9915 -- enforce mode conformance since a tagged type may inherit
9916 -- from interfaces several null primitives which differ only
9917 -- in the mode of the formals.
9919 if not (Comes_From_Source
(E
))
9920 and then Is_Null_Procedure
(E
)
9921 and then not Mode_Conformant
(Designator
, E
)
9925 -- For null procedures coming from source that are completions,
9926 -- analysis of the generated body will establish the link.
9928 elsif Comes_From_Source
(E
)
9929 and then Nkind
(Spec
) = N_Procedure_Specification
9930 and then Null_Present
(Spec
)
9934 -- Expression functions can be completions, but cannot be
9935 -- completed by an explicit body.
9937 elsif Comes_From_Source
(E
)
9938 and then Comes_From_Source
(N
)
9939 and then Nkind
(N
) = N_Subprogram_Body
9940 and then Nkind
(Original_Node
(Unit_Declaration_Node
(E
))) =
9941 N_Expression_Function
9943 Error_Msg_Sloc
:= Sloc
(E
);
9944 Error_Msg_N
("body conflicts with expression function#", N
);
9947 elsif not Has_Completion
(E
) then
9948 if Nkind
(N
) /= N_Subprogram_Body_Stub
then
9949 Set_Corresponding_Spec
(N
, E
);
9952 Set_Has_Completion
(E
);
9955 elsif Nkind
(Parent
(N
)) = N_Subunit
then
9957 -- If this is the proper body of a subunit, the completion
9958 -- flag is set when analyzing the stub.
9962 -- If E is an internal function with a controlling result that
9963 -- was created for an operation inherited by a null extension,
9964 -- it may be overridden by a body without a previous spec (one
9965 -- more reason why these should be shunned). In that case we
9966 -- remove the generated body if present, because the current
9967 -- one is the explicit overriding.
9969 elsif Ekind
(E
) = E_Function
9970 and then Ada_Version
>= Ada_2005
9971 and then not Comes_From_Source
(E
)
9972 and then Has_Controlling_Result
(E
)
9973 and then (not Is_Class_Wide_Type
(Etype
(E
))
9974 and then Is_Null_Extension
(Etype
(E
)))
9975 and then Comes_From_Source
(Spec
)
9977 Set_Has_Completion
(E
, False);
9980 and then Nkind
(Parent
(E
)) = N_Function_Specification
9983 (Unit_Declaration_Node
9984 (Corresponding_Body
(Unit_Declaration_Node
(E
))));
9988 -- If expansion is disabled, or if the wrapper function has
9989 -- not been generated yet, this a late body overriding an
9990 -- inherited operation, or it is an overriding by some other
9991 -- declaration before the controlling result is frozen. In
9992 -- either case this is a declaration of a new entity.
9998 -- If the body already exists, then this is an error unless
9999 -- the previous declaration is the implicit declaration of a
10000 -- derived subprogram. It is also legal for an instance to
10001 -- contain type conformant overloadable declarations (but the
10002 -- generic declaration may not), per 8.3(26/2).
10004 elsif No
(Alias
(E
))
10005 and then not Is_Intrinsic_Subprogram
(E
)
10006 and then not In_Instance
10007 and then Post_Error
10009 Error_Msg_Sloc
:= Sloc
(E
);
10011 if Is_Imported
(E
) then
10013 ("body not allowed for imported subprogram & declared#",
10016 Error_Msg_NE
("duplicate body for & declared#", N
, E
);
10020 -- Child units cannot be overloaded, so a conformance mismatch
10021 -- between body and a previous spec is an error.
10023 elsif Is_Child_Unit
(E
)
10025 Nkind
(Unit_Declaration_Node
(Designator
)) = N_Subprogram_Body
10027 Nkind
(Parent
(Unit_Declaration_Node
(Designator
))) =
10029 and then Post_Error
10032 ("body of child unit does not match previous declaration", N
);
10040 -- On exit, we know that no previous declaration of subprogram exists
10043 end Find_Corresponding_Spec
;
10045 ----------------------
10046 -- Fully_Conformant --
10047 ----------------------
10049 function Fully_Conformant
(New_Id
, Old_Id
: Entity_Id
) return Boolean is
10052 Check_Conformance
(New_Id
, Old_Id
, Fully_Conformant
, False, Result
);
10054 end Fully_Conformant
;
10056 ----------------------------------
10057 -- Fully_Conformant_Expressions --
10058 ----------------------------------
10060 function Fully_Conformant_Expressions
10061 (Given_E1
: Node_Id
;
10062 Given_E2
: Node_Id
;
10063 Report
: Boolean := False) return Boolean
10065 E1
: constant Node_Id
:= Original_Node
(Given_E1
);
10066 E2
: constant Node_Id
:= Original_Node
(Given_E2
);
10067 -- We always test conformance on original nodes, since it is possible
10068 -- for analysis and/or expansion to make things look as though they
10069 -- conform when they do not, e.g. by converting 1+2 into 3.
10071 function FCE
(Given_E1
: Node_Id
; Given_E2
: Node_Id
) return Boolean;
10072 -- Convenience function to abbreviate recursive calls to
10073 -- Fully_Conformant_Expressions without having to pass Report.
10075 function FCL
(L1
: List_Id
; L2
: List_Id
) return Boolean;
10076 -- Compare elements of two lists for conformance. Elements have to be
10077 -- conformant, and actuals inserted as default parameters do not match
10078 -- explicit actuals with the same value.
10080 function FCO
(Op_Node
: Node_Id
; Call_Node
: Node_Id
) return Boolean;
10081 -- Compare an operator node with a function call
10087 function FCE
(Given_E1
: Node_Id
; Given_E2
: Node_Id
) return Boolean is
10089 return Fully_Conformant_Expressions
(Given_E1
, Given_E2
, Report
);
10096 function FCL
(L1
: List_Id
; L2
: List_Id
) return Boolean is
10104 -- Compare two lists, skipping rewrite insertions (we want to compare
10105 -- the original trees, not the expanded versions).
10108 if Is_Rewrite_Insertion
(N1
) then
10110 elsif Is_Rewrite_Insertion
(N2
) then
10116 elsif not FCE
(N1
, N2
) then
10129 function FCO
(Op_Node
: Node_Id
; Call_Node
: Node_Id
) return Boolean is
10130 Actuals
: constant List_Id
:= Parameter_Associations
(Call_Node
);
10135 or else Entity
(Op_Node
) /= Entity
(Name
(Call_Node
))
10140 Act
:= First
(Actuals
);
10142 if Nkind
(Op_Node
) in N_Binary_Op
then
10143 if not FCE
(Left_Opnd
(Op_Node
), Act
) then
10150 return Present
(Act
)
10151 and then FCE
(Right_Opnd
(Op_Node
), Act
)
10152 and then No
(Next
(Act
));
10156 function User_Defined_Numeric_Literal_Mismatch
return Boolean;
10157 -- Usually literals with the same value like 12345 and 12_345
10158 -- or 123.0 and 123.00 conform, but not if they are
10159 -- user-defined literals.
10161 -------------------------------------------
10162 -- User_Defined_Numeric_Literal_Mismatch --
10163 -------------------------------------------
10165 function User_Defined_Numeric_Literal_Mismatch
return Boolean is
10166 E1_Is_User_Defined
: constant Boolean :=
10167 Nkind
(Given_E1
) not in N_Integer_Literal | N_Real_Literal
;
10168 E2_Is_User_Defined
: constant Boolean :=
10169 Nkind
(Given_E2
) not in N_Integer_Literal | N_Real_Literal
;
10172 pragma Assert
(E1_Is_User_Defined
= E2_Is_User_Defined
);
10174 return E1_Is_User_Defined
and then
10175 not String_Equal
(String_From_Numeric_Literal
(E1
),
10176 String_From_Numeric_Literal
(E2
));
10177 end User_Defined_Numeric_Literal_Mismatch
;
10183 -- Start of processing for Fully_Conformant_Expressions
10188 -- Nonconformant if paren count does not match. Note: if some idiot
10189 -- complains that we don't do this right for more than 3 levels of
10190 -- parentheses, they will be treated with the respect they deserve.
10192 if Paren_Count
(E1
) /= Paren_Count
(E2
) then
10195 -- If same entities are referenced, then they are conformant even if
10196 -- they have different forms (RM 8.3.1(19-20)).
10198 elsif Is_Entity_Name
(E1
) and then Is_Entity_Name
(E2
) then
10199 if Present
(Entity
(E1
)) then
10200 Result
:= Entity
(E1
) = Entity
(E2
)
10202 -- One may be a discriminant that has been replaced by the
10203 -- corresponding discriminal.
10206 (Chars
(Entity
(E1
)) = Chars
(Entity
(E2
))
10207 and then Ekind
(Entity
(E1
)) = E_Discriminant
10208 and then Ekind
(Entity
(E2
)) = E_In_Parameter
)
10210 -- The discriminant of a protected type is transformed into
10211 -- a local constant and then into a parameter of a protected
10215 (Ekind
(Entity
(E1
)) = E_Constant
10216 and then Ekind
(Entity
(E2
)) = E_In_Parameter
10217 and then Present
(Discriminal_Link
(Entity
(E1
)))
10218 and then Discriminal_Link
(Entity
(E1
)) =
10219 Discriminal_Link
(Entity
(E2
)))
10221 -- AI12-050: The entities of quantified expressions match if they
10222 -- have the same identifier, even if they may be distinct nodes.
10225 (Chars
(Entity
(E1
)) = Chars
(Entity
(E2
))
10226 and then Is_Entity_Of_Quantified_Expression
(Entity
(E1
))
10227 and then Is_Entity_Of_Quantified_Expression
(Entity
(E2
)))
10229 -- A call to an instantiation of Unchecked_Conversion is
10230 -- rewritten with the name of the generated function created for
10231 -- the instance, and this must be special-cased.
10234 (Ekind
(Entity
(E1
)) = E_Function
10235 and then Is_Intrinsic_Subprogram
(Entity
(E1
))
10236 and then Is_Generic_Instance
(Entity
(E1
))
10237 and then Entity
(E2
) = Alias
(Entity
(E1
)));
10238 if Report
and not Result
then
10240 Text_Ptr
'Max (Sloc
(Entity
(E1
)), Sloc
(Entity
(E2
)));
10242 ("meaning of& differs because of declaration#", E1
, E2
);
10247 elsif Nkind
(E1
) = N_Expanded_Name
10248 and then Nkind
(E2
) = N_Expanded_Name
10249 and then Nkind
(Selector_Name
(E1
)) = N_Character_Literal
10250 and then Nkind
(Selector_Name
(E2
)) = N_Character_Literal
10252 return Chars
(Selector_Name
(E1
)) = Chars
(Selector_Name
(E2
));
10255 -- Identifiers in component associations don't always have
10256 -- entities, but their names must conform.
10258 return Nkind
(E1
) = N_Identifier
10259 and then Nkind
(E2
) = N_Identifier
10260 and then Chars
(E1
) = Chars
(E2
);
10263 elsif Nkind
(E1
) = N_Character_Literal
10264 and then Nkind
(E2
) = N_Expanded_Name
10266 return Nkind
(Selector_Name
(E2
)) = N_Character_Literal
10267 and then Chars
(E1
) = Chars
(Selector_Name
(E2
));
10269 elsif Nkind
(E2
) = N_Character_Literal
10270 and then Nkind
(E1
) = N_Expanded_Name
10272 return Nkind
(Selector_Name
(E1
)) = N_Character_Literal
10273 and then Chars
(E2
) = Chars
(Selector_Name
(E1
));
10275 elsif Nkind
(E1
) in N_Op
and then Nkind
(E2
) = N_Function_Call
then
10276 return FCO
(E1
, E2
);
10278 elsif Nkind
(E2
) in N_Op
and then Nkind
(E1
) = N_Function_Call
then
10279 return FCO
(E2
, E1
);
10281 -- Otherwise we must have the same syntactic entity
10283 elsif Nkind
(E1
) /= Nkind
(E2
) then
10286 -- At this point, we specialize by node type
10290 when N_Aggregate
=>
10292 FCL
(Expressions
(E1
), Expressions
(E2
))
10294 FCL
(Component_Associations
(E1
),
10295 Component_Associations
(E2
));
10297 when N_Allocator
=>
10298 if Nkind
(Expression
(E1
)) = N_Qualified_Expression
10300 Nkind
(Expression
(E2
)) = N_Qualified_Expression
10302 return FCE
(Expression
(E1
), Expression
(E2
));
10304 -- Check that the subtype marks and any constraints
10309 Indic1
: constant Node_Id
:= Expression
(E1
);
10310 Indic2
: constant Node_Id
:= Expression
(E2
);
10315 if Nkind
(Indic1
) /= N_Subtype_Indication
then
10317 Nkind
(Indic2
) /= N_Subtype_Indication
10318 and then Entity
(Indic1
) = Entity
(Indic2
);
10320 elsif Nkind
(Indic2
) /= N_Subtype_Indication
then
10322 Nkind
(Indic1
) /= N_Subtype_Indication
10323 and then Entity
(Indic1
) = Entity
(Indic2
);
10326 if Entity
(Subtype_Mark
(Indic1
)) /=
10327 Entity
(Subtype_Mark
(Indic2
))
10332 Elt1
:= First
(Constraints
(Constraint
(Indic1
)));
10333 Elt2
:= First
(Constraints
(Constraint
(Indic2
)));
10334 while Present
(Elt1
) and then Present
(Elt2
) loop
10335 if not FCE
(Elt1
, Elt2
) then
10348 when N_Attribute_Reference
=>
10350 Attribute_Name
(E1
) = Attribute_Name
(E2
)
10351 and then FCL
(Expressions
(E1
), Expressions
(E2
));
10353 when N_Binary_Op
=>
10355 Entity
(E1
) = Entity
(E2
)
10356 and then FCE
(Left_Opnd
(E1
), Left_Opnd
(E2
))
10357 and then FCE
(Right_Opnd
(E1
), Right_Opnd
(E2
));
10359 when N_Membership_Test
10363 FCE
(Left_Opnd
(E1
), Left_Opnd
(E2
))
10365 FCE
(Right_Opnd
(E1
), Right_Opnd
(E2
));
10367 when N_Case_Expression
=>
10373 if not FCE
(Expression
(E1
), Expression
(E2
)) then
10377 Alt1
:= First
(Alternatives
(E1
));
10378 Alt2
:= First
(Alternatives
(E2
));
10380 if Present
(Alt1
) /= Present
(Alt2
) then
10382 elsif No
(Alt1
) then
10386 if not FCE
(Expression
(Alt1
), Expression
(Alt2
))
10387 or else not FCL
(Discrete_Choices
(Alt1
),
10388 Discrete_Choices
(Alt2
))
10399 when N_Character_Literal
=>
10401 Char_Literal_Value
(E1
) = Char_Literal_Value
(E2
);
10403 when N_Component_Association
=>
10405 FCL
(Choices
(E1
), Choices
(E2
))
10407 FCE
(Expression
(E1
), Expression
(E2
));
10409 when N_Explicit_Dereference
=>
10411 FCE
(Prefix
(E1
), Prefix
(E2
));
10413 when N_Extension_Aggregate
=>
10415 FCL
(Expressions
(E1
), Expressions
(E2
))
10416 and then Null_Record_Present
(E1
) =
10417 Null_Record_Present
(E2
)
10418 and then FCL
(Component_Associations
(E1
),
10419 Component_Associations
(E2
));
10421 when N_Function_Call
=>
10423 FCE
(Name
(E1
), Name
(E2
))
10425 FCL
(Parameter_Associations
(E1
),
10426 Parameter_Associations
(E2
));
10428 when N_If_Expression
=>
10430 FCL
(Expressions
(E1
), Expressions
(E2
));
10432 when N_Indexed_Component
=>
10434 FCE
(Prefix
(E1
), Prefix
(E2
))
10436 FCL
(Expressions
(E1
), Expressions
(E2
));
10438 when N_Integer_Literal
=>
10439 return (Intval
(E1
) = Intval
(E2
))
10440 and then not User_Defined_Numeric_Literal_Mismatch
;
10445 when N_Operator_Symbol
=>
10447 Chars
(E1
) = Chars
(E2
);
10449 when N_Others_Choice
=>
10452 when N_Parameter_Association
=>
10454 Chars
(Selector_Name
(E1
)) = Chars
(Selector_Name
(E2
))
10455 and then FCE
(Explicit_Actual_Parameter
(E1
),
10456 Explicit_Actual_Parameter
(E2
));
10458 when N_Qualified_Expression
10459 | N_Type_Conversion
10460 | N_Unchecked_Type_Conversion
10463 FCE
(Subtype_Mark
(E1
), Subtype_Mark
(E2
))
10465 FCE
(Expression
(E1
), Expression
(E2
));
10467 when N_Quantified_Expression
=>
10468 if not FCE
(Condition
(E1
), Condition
(E2
)) then
10472 if Present
(Loop_Parameter_Specification
(E1
))
10473 and then Present
(Loop_Parameter_Specification
(E2
))
10476 L1
: constant Node_Id
:=
10477 Loop_Parameter_Specification
(E1
);
10478 L2
: constant Node_Id
:=
10479 Loop_Parameter_Specification
(E2
);
10483 Reverse_Present
(L1
) = Reverse_Present
(L2
)
10485 FCE
(Defining_Identifier
(L1
),
10486 Defining_Identifier
(L2
))
10488 FCE
(Discrete_Subtype_Definition
(L1
),
10489 Discrete_Subtype_Definition
(L2
));
10492 elsif Present
(Iterator_Specification
(E1
))
10493 and then Present
(Iterator_Specification
(E2
))
10496 I1
: constant Node_Id
:= Iterator_Specification
(E1
);
10497 I2
: constant Node_Id
:= Iterator_Specification
(E2
);
10501 FCE
(Defining_Identifier
(I1
),
10502 Defining_Identifier
(I2
))
10504 Of_Present
(I1
) = Of_Present
(I2
)
10506 Reverse_Present
(I1
) = Reverse_Present
(I2
)
10507 and then FCE
(Name
(I1
), Name
(I2
))
10508 and then FCE
(Subtype_Indication
(I1
),
10509 Subtype_Indication
(I2
));
10512 -- The quantified expressions used different specifications to
10513 -- walk their respective ranges.
10521 FCE
(Low_Bound
(E1
), Low_Bound
(E2
))
10523 FCE
(High_Bound
(E1
), High_Bound
(E2
));
10525 when N_Real_Literal
=>
10526 return (Realval
(E1
) = Realval
(E2
))
10527 and then not User_Defined_Numeric_Literal_Mismatch
;
10529 when N_Selected_Component
=>
10531 FCE
(Prefix
(E1
), Prefix
(E2
))
10533 FCE
(Selector_Name
(E1
), Selector_Name
(E2
));
10537 FCE
(Prefix
(E1
), Prefix
(E2
))
10539 FCE
(Discrete_Range
(E1
), Discrete_Range
(E2
));
10541 when N_String_Literal
=>
10543 S1
: constant String_Id
:= Strval
(E1
);
10544 S2
: constant String_Id
:= Strval
(E2
);
10545 L1
: constant Nat
:= String_Length
(S1
);
10546 L2
: constant Nat
:= String_Length
(S2
);
10553 for J
in 1 .. L1
loop
10554 if Get_String_Char
(S1
, J
) /=
10555 Get_String_Char
(S2
, J
)
10567 Entity
(E1
) = Entity
(E2
)
10569 FCE
(Right_Opnd
(E1
), Right_Opnd
(E2
));
10571 -- All other node types cannot appear in this context. Strictly
10572 -- we should raise a fatal internal error. Instead we just ignore
10573 -- the nodes. This means that if anyone makes a mistake in the
10574 -- expander and mucks an expression tree irretrievably, the result
10575 -- will be a failure to detect a (probably very obscure) case
10576 -- of non-conformance, which is better than bombing on some
10577 -- case where two expressions do in fact conform.
10583 end Fully_Conformant_Expressions
;
10585 ----------------------------------------
10586 -- Fully_Conformant_Discrete_Subtypes --
10587 ----------------------------------------
10589 function Fully_Conformant_Discrete_Subtypes
10590 (Given_S1
: Node_Id
;
10591 Given_S2
: Node_Id
) return Boolean
10593 S1
: constant Node_Id
:= Original_Node
(Given_S1
);
10594 S2
: constant Node_Id
:= Original_Node
(Given_S2
);
10596 function Conforming_Bounds
(B1
, B2
: Node_Id
) return Boolean;
10597 -- Special-case for a bound given by a discriminant, which in the body
10598 -- is replaced with the discriminal of the enclosing type.
10600 function Conforming_Ranges
(R1
, R2
: Node_Id
) return Boolean;
10601 -- Check both bounds
10603 -----------------------
10604 -- Conforming_Bounds --
10605 -----------------------
10607 function Conforming_Bounds
(B1
, B2
: Node_Id
) return Boolean is
10609 if Is_Entity_Name
(B1
)
10610 and then Is_Entity_Name
(B2
)
10611 and then Ekind
(Entity
(B1
)) = E_Discriminant
10613 return Chars
(B1
) = Chars
(B2
);
10616 return Fully_Conformant_Expressions
(B1
, B2
);
10618 end Conforming_Bounds
;
10620 -----------------------
10621 -- Conforming_Ranges --
10622 -----------------------
10624 function Conforming_Ranges
(R1
, R2
: Node_Id
) return Boolean is
10627 Conforming_Bounds
(Low_Bound
(R1
), Low_Bound
(R2
))
10629 Conforming_Bounds
(High_Bound
(R1
), High_Bound
(R2
));
10630 end Conforming_Ranges
;
10632 -- Start of processing for Fully_Conformant_Discrete_Subtypes
10635 if Nkind
(S1
) /= Nkind
(S2
) then
10638 elsif Is_Entity_Name
(S1
) then
10639 return Entity
(S1
) = Entity
(S2
);
10641 elsif Nkind
(S1
) = N_Range
then
10642 return Conforming_Ranges
(S1
, S2
);
10644 elsif Nkind
(S1
) = N_Subtype_Indication
then
10646 Entity
(Subtype_Mark
(S1
)) = Entity
(Subtype_Mark
(S2
))
10649 (Range_Expression
(Constraint
(S1
)),
10650 Range_Expression
(Constraint
(S2
)));
10654 end Fully_Conformant_Discrete_Subtypes
;
10656 --------------------
10657 -- Install_Entity --
10658 --------------------
10660 procedure Install_Entity
(E
: Entity_Id
) is
10661 Prev
: constant Entity_Id
:= Current_Entity
(E
);
10663 Set_Is_Immediately_Visible
(E
);
10664 Set_Current_Entity
(E
);
10665 pragma Assert
(Prev
/= E
);
10666 Set_Homonym
(E
, Prev
);
10667 end Install_Entity
;
10669 ---------------------
10670 -- Install_Formals --
10671 ---------------------
10673 procedure Install_Formals
(Id
: Entity_Id
) is
10676 F
:= First_Formal
(Id
);
10677 while Present
(F
) loop
10678 Install_Entity
(F
);
10681 end Install_Formals
;
10683 -----------------------------
10684 -- Is_Interface_Conformant --
10685 -----------------------------
10687 function Is_Interface_Conformant
10688 (Tagged_Type
: Entity_Id
;
10689 Iface_Prim
: Entity_Id
;
10690 Prim
: Entity_Id
) return Boolean
10692 -- The operation may in fact be an inherited (implicit) operation
10693 -- rather than the original interface primitive, so retrieve the
10694 -- ultimate ancestor.
10696 Iface
: constant Entity_Id
:=
10697 Find_Dispatching_Type
(Ultimate_Alias
(Iface_Prim
));
10698 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Prim
);
10700 function Controlling_Formal
(Prim
: Entity_Id
) return Entity_Id
;
10701 -- Return the controlling formal of Prim
10703 ------------------------
10704 -- Controlling_Formal --
10705 ------------------------
10707 function Controlling_Formal
(Prim
: Entity_Id
) return Entity_Id
is
10711 E
:= First_Entity
(Prim
);
10712 while Present
(E
) loop
10713 if Is_Formal
(E
) and then Is_Controlling_Formal
(E
) then
10721 end Controlling_Formal
;
10725 Iface_Ctrl_F
: constant Entity_Id
:= Controlling_Formal
(Iface_Prim
);
10726 Prim_Ctrl_F
: constant Entity_Id
:= Controlling_Formal
(Prim
);
10728 -- Start of processing for Is_Interface_Conformant
10731 pragma Assert
(Is_Subprogram
(Iface_Prim
)
10732 and then Is_Subprogram
(Prim
)
10733 and then Is_Dispatching_Operation
(Iface_Prim
)
10734 and then Is_Dispatching_Operation
(Prim
));
10736 pragma Assert
(Is_Interface
(Iface
)
10737 or else (Present
(Alias
(Iface_Prim
))
10740 (Find_Dispatching_Type
(Ultimate_Alias
(Iface_Prim
)))));
10742 if Prim
= Iface_Prim
10743 or else not Is_Subprogram
(Prim
)
10744 or else Ekind
(Prim
) /= Ekind
(Iface_Prim
)
10745 or else not Is_Dispatching_Operation
(Prim
)
10746 or else Scope
(Prim
) /= Scope
(Tagged_Type
)
10748 or else Base_Type
(Typ
) /= Base_Type
(Tagged_Type
)
10749 or else not Primitive_Names_Match
(Iface_Prim
, Prim
)
10753 -- The mode of the controlling formals must match
10755 elsif Present
(Iface_Ctrl_F
)
10756 and then Present
(Prim_Ctrl_F
)
10757 and then Ekind
(Iface_Ctrl_F
) /= Ekind
(Prim_Ctrl_F
)
10761 -- Case of a procedure, or a function whose result type matches the
10762 -- result type of the interface primitive, or a function that has no
10763 -- controlling result (I or access I).
10765 elsif Ekind
(Iface_Prim
) = E_Procedure
10766 or else Etype
(Prim
) = Etype
(Iface_Prim
)
10767 or else not Has_Controlling_Result
(Prim
)
10769 return Type_Conformant
10770 (Iface_Prim
, Prim
, Skip_Controlling_Formals
=> True);
10772 -- Case of a function returning an interface, or an access to one. Check
10773 -- that the return types correspond.
10775 elsif Implements_Interface
(Typ
, Iface
) then
10776 if (Ekind
(Etype
(Prim
)) = E_Anonymous_Access_Type
)
10778 (Ekind
(Etype
(Iface_Prim
)) = E_Anonymous_Access_Type
)
10783 Type_Conformant
(Prim
, Ultimate_Alias
(Iface_Prim
),
10784 Skip_Controlling_Formals
=> True);
10790 end Is_Interface_Conformant
;
10792 ---------------------------------
10793 -- Is_Non_Overriding_Operation --
10794 ---------------------------------
10796 function Is_Non_Overriding_Operation
10797 (Prev_E
: Entity_Id
;
10798 New_E
: Entity_Id
) return Boolean
10800 Formal
: Entity_Id
;
10802 G_Typ
: Entity_Id
:= Empty
;
10804 function Get_Generic_Parent_Type
(F_Typ
: Entity_Id
) return Entity_Id
;
10805 -- If F_Type is a derived type associated with a generic actual subtype,
10806 -- then return its Generic_Parent_Type attribute, else return Empty.
10808 function Types_Correspond
10809 (P_Type
: Entity_Id
;
10810 N_Type
: Entity_Id
) return Boolean;
10811 -- Returns true if and only if the types (or designated types in the
10812 -- case of anonymous access types) are the same or N_Type is derived
10813 -- directly or indirectly from P_Type.
10815 -----------------------------
10816 -- Get_Generic_Parent_Type --
10817 -----------------------------
10819 function Get_Generic_Parent_Type
(F_Typ
: Entity_Id
) return Entity_Id
is
10825 if Is_Derived_Type
(F_Typ
)
10826 and then Nkind
(Parent
(F_Typ
)) = N_Full_Type_Declaration
10828 -- The tree must be traversed to determine the parent subtype in
10829 -- the generic unit, which unfortunately isn't always available
10830 -- via semantic attributes. ??? (Note: The use of Original_Node
10831 -- is needed for cases where a full derived type has been
10834 -- If the parent type is a scalar type, the derivation creates
10835 -- an anonymous base type for it, and the source type is its
10838 if Is_Scalar_Type
(F_Typ
)
10839 and then not Comes_From_Source
(F_Typ
)
10843 (Original_Node
(Parent
(First_Subtype
(F_Typ
))));
10845 Defn
:= Type_Definition
(Original_Node
(Parent
(F_Typ
)));
10847 if Nkind
(Defn
) = N_Derived_Type_Definition
then
10848 Indic
:= Subtype_Indication
(Defn
);
10850 if Nkind
(Indic
) = N_Subtype_Indication
then
10851 G_Typ
:= Entity
(Subtype_Mark
(Indic
));
10853 G_Typ
:= Entity
(Indic
);
10856 if Nkind
(Parent
(G_Typ
)) = N_Subtype_Declaration
10857 and then Present
(Generic_Parent_Type
(Parent
(G_Typ
)))
10859 return Generic_Parent_Type
(Parent
(G_Typ
));
10865 end Get_Generic_Parent_Type
;
10867 ----------------------
10868 -- Types_Correspond --
10869 ----------------------
10871 function Types_Correspond
10872 (P_Type
: Entity_Id
;
10873 N_Type
: Entity_Id
) return Boolean
10875 Prev_Type
: Entity_Id
:= Base_Type
(P_Type
);
10876 New_Type
: Entity_Id
:= Base_Type
(N_Type
);
10879 if Ekind
(Prev_Type
) = E_Anonymous_Access_Type
then
10880 Prev_Type
:= Designated_Type
(Prev_Type
);
10883 if Ekind
(New_Type
) = E_Anonymous_Access_Type
then
10884 New_Type
:= Designated_Type
(New_Type
);
10887 if Prev_Type
= New_Type
then
10890 elsif not Is_Class_Wide_Type
(New_Type
) then
10891 while Etype
(New_Type
) /= New_Type
loop
10892 New_Type
:= Etype
(New_Type
);
10894 if New_Type
= Prev_Type
then
10900 end Types_Correspond
;
10902 -- Start of processing for Is_Non_Overriding_Operation
10905 -- In the case where both operations are implicit derived subprograms
10906 -- then neither overrides the other. This can only occur in certain
10907 -- obscure cases (e.g., derivation from homographs created in a generic
10910 if Present
(Alias
(Prev_E
)) and then Present
(Alias
(New_E
)) then
10913 elsif Ekind
(Current_Scope
) = E_Package
10914 and then Is_Generic_Instance
(Current_Scope
)
10915 and then In_Private_Part
(Current_Scope
)
10916 and then Comes_From_Source
(New_E
)
10918 -- We examine the formals and result type of the inherited operation,
10919 -- to determine whether their type is derived from (the instance of)
10920 -- a generic type. The first such formal or result type is the one
10923 Formal
:= First_Formal
(Prev_E
);
10925 while Present
(Formal
) loop
10926 F_Typ
:= Base_Type
(Etype
(Formal
));
10928 if Ekind
(F_Typ
) = E_Anonymous_Access_Type
then
10929 F_Typ
:= Designated_Type
(F_Typ
);
10932 G_Typ
:= Get_Generic_Parent_Type
(F_Typ
);
10933 exit when Present
(G_Typ
);
10935 Next_Formal
(Formal
);
10938 -- If the function dispatches on result check the result type
10940 if No
(G_Typ
) and then Ekind
(Prev_E
) = E_Function
then
10941 G_Typ
:= Get_Generic_Parent_Type
(Base_Type
(Etype
(Prev_E
)));
10948 -- If the generic type is a private type, then the original operation
10949 -- was not overriding in the generic, because there was no primitive
10950 -- operation to override.
10952 if Nkind
(Parent
(G_Typ
)) = N_Formal_Type_Declaration
10953 and then Nkind
(Formal_Type_Definition
(Parent
(G_Typ
))) =
10954 N_Formal_Private_Type_Definition
10958 -- The generic parent type is the ancestor of a formal derived
10959 -- type declaration. We need to check whether it has a primitive
10960 -- operation that should be overridden by New_E in the generic.
10964 P_Formal
: Entity_Id
;
10965 N_Formal
: Entity_Id
;
10968 P_Prim
: Entity_Id
;
10969 Prim_Elt
: Elmt_Id
:= First_Elmt
(Primitive_Operations
(G_Typ
));
10972 while Present
(Prim_Elt
) loop
10973 P_Prim
:= Node
(Prim_Elt
);
10975 if Chars
(P_Prim
) = Chars
(New_E
)
10976 and then Ekind
(P_Prim
) = Ekind
(New_E
)
10978 P_Formal
:= First_Formal
(P_Prim
);
10979 N_Formal
:= First_Formal
(New_E
);
10980 while Present
(P_Formal
) and then Present
(N_Formal
) loop
10981 P_Typ
:= Etype
(P_Formal
);
10982 N_Typ
:= Etype
(N_Formal
);
10984 if not Types_Correspond
(P_Typ
, N_Typ
) then
10988 Next_Formal
(P_Formal
);
10989 Next_Formal
(N_Formal
);
10992 -- Found a matching primitive operation belonging to the
10993 -- formal ancestor type, so the new subprogram is
10997 and then No
(N_Formal
)
10998 and then (Ekind
(New_E
) /= E_Function
11001 (Etype
(P_Prim
), Etype
(New_E
)))
11007 Next_Elmt
(Prim_Elt
);
11010 -- If no match found, then the new subprogram does not override
11011 -- in the generic (nor in the instance).
11013 -- If the type in question is not abstract, and the subprogram
11014 -- is, this will be an error if the new operation is in the
11015 -- private part of the instance. Emit a warning now, which will
11016 -- make the subsequent error message easier to understand.
11018 if Present
(F_Typ
) and then not Is_Abstract_Type
(F_Typ
)
11019 and then Is_Abstract_Subprogram
(Prev_E
)
11020 and then In_Private_Part
(Current_Scope
)
11022 Error_Msg_Node_2
:= F_Typ
;
11024 ("private operation& in generic unit does not override "
11025 & "any primitive operation of& (RM 12.3(18))??",
11035 end Is_Non_Overriding_Operation
;
11037 -------------------------------------
11038 -- List_Inherited_Pre_Post_Aspects --
11039 -------------------------------------
11041 procedure List_Inherited_Pre_Post_Aspects
(E
: Entity_Id
) is
11043 if Opt
.List_Inherited_Aspects
11044 and then Is_Subprogram_Or_Generic_Subprogram
(E
)
11047 Subps
: constant Subprogram_List
:= Inherited_Subprograms
(E
);
11052 for Index
in Subps
'Range loop
11053 Items
:= Contract
(Subps
(Index
));
11055 if Present
(Items
) then
11056 Prag
:= Pre_Post_Conditions
(Items
);
11057 while Present
(Prag
) loop
11058 Error_Msg_Sloc
:= Sloc
(Prag
);
11060 if Class_Present
(Prag
)
11061 and then not Split_PPC
(Prag
)
11063 if Pragma_Name
(Prag
) = Name_Precondition
then
11065 ("info: & inherits `Pre''Class` aspect from "
11069 ("info: & inherits `Post''Class` aspect from "
11074 Prag
:= Next_Pragma
(Prag
);
11080 end List_Inherited_Pre_Post_Aspects
;
11082 ------------------------------
11083 -- Make_Inequality_Operator --
11084 ------------------------------
11086 -- S is the defining identifier of an equality operator. We build a
11087 -- subprogram declaration with the right signature. This operation is
11088 -- intrinsic, because it is always expanded as the negation of the
11089 -- call to the equality function.
11091 procedure Make_Inequality_Operator
(S
: Entity_Id
) is
11092 Loc
: constant Source_Ptr
:= Sloc
(S
);
11095 Op_Name
: Entity_Id
;
11097 FF
: constant Entity_Id
:= First_Formal
(S
);
11098 NF
: constant Entity_Id
:= Next_Formal
(FF
);
11101 -- Check that equality was properly defined, ignore call if not
11108 A
: constant Entity_Id
:=
11109 Make_Defining_Identifier
(Sloc
(FF
),
11110 Chars
=> Chars
(FF
));
11112 B
: constant Entity_Id
:=
11113 Make_Defining_Identifier
(Sloc
(NF
),
11114 Chars
=> Chars
(NF
));
11117 Op_Name
:= Make_Defining_Operator_Symbol
(Loc
, Name_Op_Ne
);
11119 Formals
:= New_List
(
11120 Make_Parameter_Specification
(Loc
,
11121 Defining_Identifier
=> A
,
11123 New_Occurrence_Of
(Etype
(First_Formal
(S
)),
11124 Sloc
(Etype
(First_Formal
(S
))))),
11126 Make_Parameter_Specification
(Loc
,
11127 Defining_Identifier
=> B
,
11129 New_Occurrence_Of
(Etype
(Next_Formal
(First_Formal
(S
))),
11130 Sloc
(Etype
(Next_Formal
(First_Formal
(S
)))))));
11133 Make_Subprogram_Declaration
(Loc
,
11135 Make_Function_Specification
(Loc
,
11136 Defining_Unit_Name
=> Op_Name
,
11137 Parameter_Specifications
=> Formals
,
11138 Result_Definition
=>
11139 New_Occurrence_Of
(Standard_Boolean
, Loc
)));
11141 -- Insert inequality right after equality if it is explicit or after
11142 -- the derived type when implicit. These entities are created only
11143 -- for visibility purposes, and eventually replaced in the course
11144 -- of expansion, so they do not need to be attached to the tree and
11145 -- seen by the back-end. Keeping them internal also avoids spurious
11146 -- freezing problems. The declaration is inserted in the tree for
11147 -- analysis, and removed afterwards. If the equality operator comes
11148 -- from an explicit declaration, attach the inequality immediately
11149 -- after. Else the equality is inherited from a derived type
11150 -- declaration, so insert inequality after that declaration.
11152 if No
(Alias
(S
)) then
11153 Insert_After
(Unit_Declaration_Node
(S
), Decl
);
11154 elsif Is_List_Member
(Parent
(S
)) then
11155 Insert_After
(Parent
(S
), Decl
);
11157 Insert_After
(Parent
(Etype
(First_Formal
(S
))), Decl
);
11160 Mark_Rewrite_Insertion
(Decl
);
11161 Set_Is_Intrinsic_Subprogram
(Op_Name
);
11164 Set_Has_Completion
(Op_Name
);
11165 Set_Corresponding_Equality
(Op_Name
, S
);
11166 Set_Is_Abstract_Subprogram
(Op_Name
, Is_Abstract_Subprogram
(S
));
11168 end Make_Inequality_Operator
;
11170 ----------------------
11171 -- May_Need_Actuals --
11172 ----------------------
11174 procedure May_Need_Actuals
(Fun
: Entity_Id
) is
11179 F
:= First_Formal
(Fun
);
11181 while Present
(F
) loop
11182 if No
(Default_Value
(F
)) then
11190 Set_Needs_No_Actuals
(Fun
, B
);
11191 end May_Need_Actuals
;
11193 ---------------------
11194 -- Mode_Conformant --
11195 ---------------------
11197 function Mode_Conformant
(New_Id
, Old_Id
: Entity_Id
) return Boolean is
11200 Check_Conformance
(New_Id
, Old_Id
, Mode_Conformant
, False, Result
);
11202 end Mode_Conformant
;
11204 ---------------------------
11205 -- New_Overloaded_Entity --
11206 ---------------------------
11208 procedure New_Overloaded_Entity
11210 Derived_Type
: Entity_Id
:= Empty
)
11212 Overridden_Subp
: Entity_Id
:= Empty
;
11213 -- Set if the current scope has an operation that is type-conformant
11214 -- with S, and becomes hidden by S.
11216 Is_Primitive_Subp
: Boolean;
11217 -- Set to True if the new subprogram is primitive
11220 -- Entity that S overrides
11222 procedure Check_For_Primitive_Subprogram
11223 (Is_Primitive
: out Boolean;
11224 Is_Overriding
: Boolean := False);
11225 -- If the subprogram being analyzed is a primitive operation of the type
11226 -- of a formal or result, set the Has_Primitive_Operations flag on the
11227 -- type, and set Is_Primitive to True (otherwise set to False). Set the
11228 -- corresponding flag on the entity itself for later use.
11230 function Has_Matching_Entry_Or_Subprogram
(E
: Entity_Id
) return Boolean;
11231 -- True if a) E is a subprogram whose first formal is a concurrent type
11232 -- defined in the scope of E that has some entry or subprogram whose
11233 -- profile matches E, or b) E is an internally built dispatching
11234 -- subprogram of a protected type and there is a matching subprogram
11235 -- defined in the enclosing scope of the protected type, or c) E is
11236 -- an entry of a synchronized type and a matching procedure has been
11237 -- previously defined in the enclosing scope of the synchronized type.
11239 function Is_Private_Declaration
(E
: Entity_Id
) return Boolean;
11240 -- Check that E is declared in the private part of the current package,
11241 -- or in the package body, where it may hide a previous declaration.
11242 -- We can't use In_Private_Part by itself because this flag is also
11243 -- set when freezing entities, so we must examine the place of the
11244 -- declaration in the tree, and recognize wrapper packages as well.
11246 function Is_Overriding_Alias
11247 (Old_E
: Entity_Id
;
11248 New_E
: Entity_Id
) return Boolean;
11249 -- Check whether new subprogram and old subprogram are both inherited
11250 -- from subprograms that have distinct dispatch table entries. This can
11251 -- occur with derivations from instances with accidental homonyms. The
11252 -- function is conservative given that the converse is only true within
11253 -- instances that contain accidental overloadings.
11255 procedure Report_Conflict
(S
: Entity_Id
; E
: Entity_Id
);
11256 -- Report conflict between entities S and E
11258 ------------------------------------
11259 -- Check_For_Primitive_Subprogram --
11260 ------------------------------------
11262 procedure Check_For_Primitive_Subprogram
11263 (Is_Primitive
: out Boolean;
11264 Is_Overriding
: Boolean := False)
11266 procedure Add_Or_Replace_Untagged_Primitive
(Typ
: Entity_Id
);
11267 -- Either add the new subprogram to the list of primitives for
11268 -- untagged type Typ, or if it overrides a primitive of Typ, then
11269 -- replace the overridden primitive in Typ's primitives list with
11270 -- the new subprogram.
11272 function Visible_Part_Type
(T
: Entity_Id
) return Boolean;
11273 -- Returns true if T is declared in the visible part of the current
11274 -- package scope; otherwise returns false. Assumes that T is declared
11277 procedure Check_Private_Overriding
(T
: Entity_Id
);
11278 -- Checks that if a primitive abstract subprogram of a visible
11279 -- abstract type is declared in a private part, then it must override
11280 -- an abstract subprogram declared in the visible part. Also checks
11281 -- that if a primitive function with a controlling result is declared
11282 -- in a private part, then it must override a function declared in
11283 -- the visible part.
11285 ---------------------------------------
11286 -- Add_Or_Replace_Untagged_Primitive --
11287 ---------------------------------------
11289 procedure Add_Or_Replace_Untagged_Primitive
(Typ
: Entity_Id
) is
11290 Replaced_Overridden_Subp
: Boolean := False;
11293 pragma Assert
(not Is_Tagged_Type
(Typ
));
11295 -- Anonymous access types don't have a primitives list. Normally
11296 -- such types wouldn't make it here, but the case of anonymous
11297 -- access-to-subprogram types can.
11299 if not Is_Anonymous_Access_Type
(Typ
) then
11301 -- If S overrides a subprogram that's a primitive of
11302 -- the formal's type, then replace the overridden
11303 -- subprogram with the new subprogram in the type's
11304 -- list of primitives.
11306 if Is_Overriding
then
11307 pragma Assert
(Present
(Overridden_Subp
)
11308 and then Overridden_Subp
= E
); -- Added for now
11311 Prim_Ops
: constant Elist_Id
:=
11312 Primitive_Operations
(Typ
);
11315 if Present
(Prim_Ops
) then
11316 Elmt
:= First_Elmt
(Prim_Ops
);
11318 while Present
(Elmt
)
11319 and then Node
(Elmt
) /= Overridden_Subp
11324 if Present
(Elmt
) then
11325 Replace_Elmt
(Elmt
, S
);
11326 Replaced_Overridden_Subp
:= True;
11332 -- If the new subprogram did not override an operation
11333 -- of the formal's type, then add it to the primitives
11334 -- list of the type.
11336 if not Replaced_Overridden_Subp
then
11337 Append_Unique_Elmt
(S
, Primitive_Operations
(Typ
));
11340 end Add_Or_Replace_Untagged_Primitive
;
11342 ------------------------------
11343 -- Check_Private_Overriding --
11344 ------------------------------
11346 procedure Check_Private_Overriding
(T
: Entity_Id
) is
11347 function Overrides_Private_Part_Op
return Boolean;
11348 -- This detects the special case where the overriding subprogram
11349 -- is overriding a subprogram that was declared in the same
11350 -- private part. That case is illegal by 3.9.3(10).
11352 function Overrides_Visible_Function
11353 (Partial_View
: Entity_Id
) return Boolean;
11354 -- True if S overrides a function in the visible part. The
11355 -- overridden function could be explicitly or implicitly declared.
11357 -------------------------------
11358 -- Overrides_Private_Part_Op --
11359 -------------------------------
11361 function Overrides_Private_Part_Op
return Boolean is
11362 Over_Decl
: constant Node_Id
:=
11363 Unit_Declaration_Node
11364 (Ultimate_Alias
(Overridden_Operation
(S
)));
11365 Subp_Decl
: constant Node_Id
:= Unit_Declaration_Node
(S
);
11368 pragma Assert
(Is_Overriding
);
11370 (Nkind
(Over_Decl
) = N_Abstract_Subprogram_Declaration
);
11372 (Nkind
(Subp_Decl
) = N_Abstract_Subprogram_Declaration
);
11374 return In_Same_List
(Over_Decl
, Subp_Decl
);
11375 end Overrides_Private_Part_Op
;
11377 --------------------------------
11378 -- Overrides_Visible_Function --
11379 --------------------------------
11381 function Overrides_Visible_Function
11382 (Partial_View
: Entity_Id
) return Boolean
11385 if not Is_Overriding
or else not Has_Homonym
(S
) then
11389 if not Present
(Partial_View
) then
11393 -- Search through all the homonyms H of S in the current
11394 -- package spec, and return True if we find one that matches.
11395 -- Note that Parent (H) will be the declaration of the
11396 -- partial view of T for a match.
11399 H
: Entity_Id
:= S
;
11403 exit when not Present
(H
) or else Scope
(H
) /= Scope
(S
);
11405 if Nkind
(Parent
(H
)) in
11406 N_Private_Extension_Declaration |
11407 N_Private_Type_Declaration
11408 and then Defining_Identifier
(Parent
(H
)) = Partial_View
11416 end Overrides_Visible_Function
;
11418 -- Start of processing for Check_Private_Overriding
11421 if Is_Package_Or_Generic_Package
(Current_Scope
)
11422 and then In_Private_Part
(Current_Scope
)
11423 and then Visible_Part_Type
(T
)
11424 and then not In_Instance
11426 if Is_Abstract_Type
(T
)
11427 and then Is_Abstract_Subprogram
(S
)
11428 and then (not Is_Overriding
11429 or else not Is_Abstract_Subprogram
(E
)
11430 or else Overrides_Private_Part_Op
)
11433 ("abstract subprograms must be visible (RM 3.9.3(10))!",
11436 elsif Ekind
(S
) = E_Function
then
11438 Partial_View
: constant Entity_Id
:=
11439 Incomplete_Or_Partial_View
(T
);
11442 if not Overrides_Visible_Function
(Partial_View
) then
11444 -- Here, S is "function ... return T;" declared in
11445 -- the private part, not overriding some visible
11446 -- operation. That's illegal in the tagged case
11447 -- (but not if the private type is untagged).
11449 if ((Present
(Partial_View
)
11450 and then Is_Tagged_Type
(Partial_View
))
11451 or else (not Present
(Partial_View
)
11452 and then Is_Tagged_Type
(T
)))
11453 and then T
= Base_Type
(Etype
(S
))
11456 ("private function with tagged result must"
11457 & " override visible-part function", S
);
11459 ("\move subprogram to the visible part"
11460 & " (RM 3.9.3(10))", S
);
11462 -- Ada 2012 (AI05-0073): Extend this check to the case
11463 -- of a function whose result subtype is defined by an
11464 -- access_definition designating specific tagged type.
11466 elsif Ekind
(Etype
(S
)) = E_Anonymous_Access_Type
11467 and then Is_Tagged_Type
(Designated_Type
(Etype
(S
)))
11469 not Is_Class_Wide_Type
11470 (Designated_Type
(Etype
(S
)))
11471 and then Ada_Version
>= Ada_2012
11474 ("private function with controlling access "
11475 & "result must override visible-part function",
11478 ("\move subprogram to the visible part"
11479 & " (RM 3.9.3(10))", S
);
11485 end Check_Private_Overriding
;
11487 -----------------------
11488 -- Visible_Part_Type --
11489 -----------------------
11491 function Visible_Part_Type
(T
: Entity_Id
) return Boolean is
11492 P
: constant Node_Id
:= Unit_Declaration_Node
(Scope
(T
));
11495 -- If the entity is a private type, then it must be declared in a
11498 if Is_Private_Type
(T
) then
11501 elsif Is_Type
(T
) and then Has_Private_Declaration
(T
) then
11504 elsif Is_List_Member
(Declaration_Node
(T
))
11505 and then List_Containing
(Declaration_Node
(T
)) =
11506 Visible_Declarations
(Specification
(P
))
11513 end Visible_Part_Type
;
11517 Formal
: Entity_Id
;
11521 -- Start of processing for Check_For_Primitive_Subprogram
11524 Is_Primitive
:= False;
11526 if not Comes_From_Source
(S
) then
11528 -- Add an inherited primitive for an untagged derived type to
11529 -- Derived_Type's list of primitives. Tagged primitives are
11530 -- dealt with in Check_Dispatching_Operation. Do this even when
11531 -- Extensions_Allowed is False to issue better error messages.
11533 if Present
(Derived_Type
)
11534 and then not Is_Tagged_Type
(Derived_Type
)
11536 Append_Unique_Elmt
(S
, Primitive_Operations
(Derived_Type
));
11539 -- If subprogram is at library level, it is not primitive operation
11541 elsif Current_Scope
= Standard_Standard
then
11544 elsif (Is_Package_Or_Generic_Package
(Current_Scope
)
11545 and then not In_Package_Body
(Current_Scope
))
11546 or else Is_Overriding
11548 -- For function, check return type
11550 if Ekind
(S
) = E_Function
then
11551 if Ekind
(Etype
(S
)) = E_Anonymous_Access_Type
then
11552 F_Typ
:= Designated_Type
(Etype
(S
));
11554 F_Typ
:= Etype
(S
);
11557 B_Typ
:= Base_Type
(F_Typ
);
11559 if Scope
(B_Typ
) = Current_Scope
11560 and then not Is_Class_Wide_Type
(B_Typ
)
11561 and then not Is_Generic_Type
(B_Typ
)
11563 Is_Primitive
:= True;
11564 Set_Has_Primitive_Operations
(B_Typ
);
11565 Set_Is_Primitive
(S
);
11567 -- Add a primitive for an untagged type to B_Typ's
11568 -- list of primitives. Tagged primitives are dealt with
11569 -- in Check_Dispatching_Operation. Do this even when
11570 -- Extensions_Allowed is False to issue better error
11573 if not Is_Tagged_Type
(B_Typ
) then
11574 Add_Or_Replace_Untagged_Primitive
(B_Typ
);
11577 Check_Private_Overriding
(B_Typ
);
11578 -- The Ghost policy in effect at the point of declaration
11579 -- or a tagged type and a primitive operation must match
11580 -- (SPARK RM 6.9(16)).
11582 Check_Ghost_Primitive
(S
, B_Typ
);
11586 -- For all subprograms, check formals
11588 Formal
:= First_Formal
(S
);
11589 while Present
(Formal
) loop
11590 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
11591 F_Typ
:= Designated_Type
(Etype
(Formal
));
11593 F_Typ
:= Etype
(Formal
);
11596 B_Typ
:= Base_Type
(F_Typ
);
11598 if Ekind
(B_Typ
) = E_Access_Subtype
then
11599 B_Typ
:= Base_Type
(B_Typ
);
11602 if Scope
(B_Typ
) = Current_Scope
11603 and then not Is_Class_Wide_Type
(B_Typ
)
11604 and then not Is_Generic_Type
(B_Typ
)
11606 Is_Primitive
:= True;
11607 Set_Is_Primitive
(S
);
11608 Set_Has_Primitive_Operations
(B_Typ
);
11610 -- Add a primitive for an untagged type to B_Typ's list
11611 -- of primitives. Tagged primitives are dealt with in
11612 -- Check_Dispatching_Operation. Do this even when
11613 -- Extensions_Allowed is False to issue better error
11616 if not Is_Tagged_Type
(B_Typ
) then
11617 Add_Or_Replace_Untagged_Primitive
(B_Typ
);
11620 Check_Private_Overriding
(B_Typ
);
11622 -- The Ghost policy in effect at the point of declaration
11623 -- of a tagged type and a primitive operation must match
11624 -- (SPARK RM 6.9(16)).
11626 Check_Ghost_Primitive
(S
, B_Typ
);
11629 Next_Formal
(Formal
);
11632 -- Special case: An equality function can be redefined for a type
11633 -- occurring in a declarative part, and won't otherwise be treated as
11634 -- a primitive because it doesn't occur in a package spec and doesn't
11635 -- override an inherited subprogram. It's important that we mark it
11636 -- primitive so it can be returned by Collect_Primitive_Operations
11637 -- and be used in composing the equality operation of later types
11638 -- that have a component of the type.
11640 elsif Chars
(S
) = Name_Op_Eq
11641 and then Etype
(S
) = Standard_Boolean
11643 B_Typ
:= Base_Type
(Etype
(First_Formal
(S
)));
11645 if Scope
(B_Typ
) = Current_Scope
11647 Base_Type
(Etype
(Next_Formal
(First_Formal
(S
)))) = B_Typ
11648 and then not Is_Limited_Type
(B_Typ
)
11650 Is_Primitive
:= True;
11651 Set_Is_Primitive
(S
);
11652 Set_Has_Primitive_Operations
(B_Typ
);
11653 Check_Private_Overriding
(B_Typ
);
11655 -- The Ghost policy in effect at the point of declaration of a
11656 -- tagged type and a primitive operation must match
11657 -- (SPARK RM 6.9(16)).
11659 Check_Ghost_Primitive
(S
, B_Typ
);
11662 end Check_For_Primitive_Subprogram
;
11664 --------------------------------------
11665 -- Has_Matching_Entry_Or_Subprogram --
11666 --------------------------------------
11668 function Has_Matching_Entry_Or_Subprogram
11669 (E
: Entity_Id
) return Boolean
11671 function Check_Conforming_Parameters
11672 (E1_Param
: Node_Id
;
11673 E2_Param
: Node_Id
;
11674 Ctype
: Conformance_Type
) return Boolean;
11675 -- Starting from the given parameters, check that all the parameters
11676 -- of two entries or subprograms are conformant. Used to skip
11677 -- the check on the controlling argument.
11679 function Matching_Entry_Or_Subprogram
11680 (Conc_Typ
: Entity_Id
;
11681 Subp
: Entity_Id
) return Entity_Id
;
11682 -- Return the first entry or subprogram of the given concurrent type
11683 -- whose name matches the name of Subp and has a profile conformant
11684 -- with Subp; return Empty if not found.
11686 function Matching_Dispatching_Subprogram
11687 (Conc_Typ
: Entity_Id
;
11688 Ent
: Entity_Id
) return Entity_Id
;
11689 -- Return the first dispatching primitive of Conc_Type defined in the
11690 -- enclosing scope of Conc_Type (i.e. before the full definition of
11691 -- this concurrent type) whose name matches the entry Ent and has a
11692 -- profile conformant with the profile of the corresponding (not yet
11693 -- built) dispatching primitive of Ent; return Empty if not found.
11695 function Matching_Original_Protected_Subprogram
11696 (Prot_Typ
: Entity_Id
;
11697 Subp
: Entity_Id
) return Entity_Id
;
11698 -- Return the first subprogram defined in the enclosing scope of
11699 -- Prot_Typ (before the full definition of this protected type)
11700 -- whose name matches the original name of Subp and has a profile
11701 -- conformant with the profile of Subp; return Empty if not found.
11703 function Normalized_First_Parameter_Type
11704 (E
: Entity_Id
) return Entity_Id
;
11705 -- Return the type of the first parameter unless that type
11706 -- is an anonymous access type, in which case return the
11707 -- designated type. Used to treat anonymous-access-to-synchronized
11708 -- the same as synchronized for purposes of checking for
11709 -- prefixed view profile conflicts.
11711 ---------------------------------
11712 -- Check_Conforming_Parameters --
11713 ---------------------------------
11715 function Check_Conforming_Parameters
11716 (E1_Param
: Node_Id
;
11717 E2_Param
: Node_Id
;
11718 Ctype
: Conformance_Type
) return Boolean
11720 Param_E1
: Node_Id
:= E1_Param
;
11721 Param_E2
: Node_Id
:= E2_Param
;
11724 while Present
(Param_E1
) and then Present
(Param_E2
) loop
11725 if (Ctype
>= Mode_Conformant
) and then
11726 Ekind
(Defining_Identifier
(Param_E1
)) /=
11727 Ekind
(Defining_Identifier
(Param_E2
))
11732 (Find_Parameter_Type
(Param_E1
),
11733 Find_Parameter_Type
(Param_E2
),
11743 -- The candidate is not valid if one of the two lists contains
11744 -- more parameters than the other
11746 return No
(Param_E1
) and then No
(Param_E2
);
11747 end Check_Conforming_Parameters
;
11749 ----------------------------------
11750 -- Matching_Entry_Or_Subprogram --
11751 ----------------------------------
11753 function Matching_Entry_Or_Subprogram
11754 (Conc_Typ
: Entity_Id
;
11755 Subp
: Entity_Id
) return Entity_Id
11760 E
:= First_Entity
(Conc_Typ
);
11761 while Present
(E
) loop
11762 if Chars
(Subp
) = Chars
(E
)
11763 and then (Ekind
(E
) = E_Entry
or else Is_Subprogram
(E
))
11765 Check_Conforming_Parameters
11766 (First
(Parameter_Specifications
(Parent
(E
))),
11767 Next
(First
(Parameter_Specifications
(Parent
(Subp
)))),
11777 end Matching_Entry_Or_Subprogram
;
11779 -------------------------------------
11780 -- Matching_Dispatching_Subprogram --
11781 -------------------------------------
11783 function Matching_Dispatching_Subprogram
11784 (Conc_Typ
: Entity_Id
;
11785 Ent
: Entity_Id
) return Entity_Id
11790 -- Search for entities in the enclosing scope of this synchronized
11793 pragma Assert
(Is_Concurrent_Type
(Conc_Typ
));
11794 Push_Scope
(Scope
(Conc_Typ
));
11795 E
:= Current_Entity_In_Scope
(Ent
);
11798 while Present
(E
) loop
11799 if Scope
(E
) = Scope
(Conc_Typ
)
11800 and then Comes_From_Source
(E
)
11801 and then Ekind
(E
) = E_Procedure
11802 and then Present
(First_Entity
(E
))
11803 and then Is_Controlling_Formal
(First_Entity
(E
))
11804 and then Etype
(First_Entity
(E
)) = Conc_Typ
11806 Check_Conforming_Parameters
11807 (First
(Parameter_Specifications
(Parent
(Ent
))),
11808 Next
(First
(Parameter_Specifications
(Parent
(E
)))),
11809 Subtype_Conformant
)
11818 end Matching_Dispatching_Subprogram
;
11820 --------------------------------------------
11821 -- Matching_Original_Protected_Subprogram --
11822 --------------------------------------------
11824 function Matching_Original_Protected_Subprogram
11825 (Prot_Typ
: Entity_Id
;
11826 Subp
: Entity_Id
) return Entity_Id
11828 ICF
: constant Boolean :=
11829 Is_Controlling_Formal
(First_Entity
(Subp
));
11833 -- Temporarily decorate the first parameter of Subp as controlling
11834 -- formal, required to invoke Subtype_Conformant.
11836 Set_Is_Controlling_Formal
(First_Entity
(Subp
));
11839 Current_Entity_In_Scope
(Original_Protected_Subprogram
(Subp
));
11841 while Present
(E
) loop
11842 if Scope
(E
) = Scope
(Prot_Typ
)
11843 and then Comes_From_Source
(E
)
11844 and then Ekind
(Subp
) = Ekind
(E
)
11845 and then Present
(First_Entity
(E
))
11846 and then Is_Controlling_Formal
(First_Entity
(E
))
11847 and then Etype
(First_Entity
(E
)) = Prot_Typ
11848 and then Subtype_Conformant
(Subp
, E
,
11849 Skip_Controlling_Formals
=> True)
11851 Set_Is_Controlling_Formal
(First_Entity
(Subp
), ICF
);
11858 Set_Is_Controlling_Formal
(First_Entity
(Subp
), ICF
);
11861 end Matching_Original_Protected_Subprogram
;
11863 -------------------------------------
11864 -- Normalized_First_Parameter_Type --
11865 -------------------------------------
11867 function Normalized_First_Parameter_Type
11868 (E
: Entity_Id
) return Entity_Id
11870 Result
: Entity_Id
:= Etype
(First_Entity
(E
));
11872 if Ekind
(Result
) = E_Anonymous_Access_Type
then
11873 Result
:= Designated_Type
(Result
);
11876 end Normalized_First_Parameter_Type
;
11878 -- Start of processing for Has_Matching_Entry_Or_Subprogram
11881 -- Case 1: E is a subprogram whose first formal is a concurrent type
11882 -- defined in the scope of E that has an entry or subprogram whose
11883 -- profile matches E.
11885 if Comes_From_Source
(E
)
11886 and then Is_Subprogram
(E
)
11887 and then Present
(First_Entity
(E
))
11888 and then Is_Concurrent_Record_Type
11889 (Normalized_First_Parameter_Type
(E
))
11892 Scope
(Corresponding_Concurrent_Type
11893 (Normalized_First_Parameter_Type
(E
)))
11896 (Matching_Entry_Or_Subprogram
11897 (Corresponding_Concurrent_Type
11898 (Normalized_First_Parameter_Type
(E
)),
11901 Report_Conflict
(E
,
11902 Matching_Entry_Or_Subprogram
11903 (Corresponding_Concurrent_Type
11904 (Normalized_First_Parameter_Type
(E
)),
11909 -- Case 2: E is an internally built dispatching subprogram of a
11910 -- protected type and there is a subprogram defined in the enclosing
11911 -- scope of the protected type that has the original name of E and
11912 -- its profile is conformant with the profile of E. We check the
11913 -- name of the original protected subprogram associated with E since
11914 -- the expander builds dispatching primitives of protected functions
11915 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
11917 elsif not Comes_From_Source
(E
)
11918 and then Is_Subprogram
(E
)
11919 and then Present
(First_Entity
(E
))
11920 and then Is_Concurrent_Record_Type
(Etype
(First_Entity
(E
)))
11921 and then Present
(Original_Protected_Subprogram
(E
))
11924 (Matching_Original_Protected_Subprogram
11925 (Corresponding_Concurrent_Type
(Etype
(First_Entity
(E
))),
11928 Report_Conflict
(E
,
11929 Matching_Original_Protected_Subprogram
11930 (Corresponding_Concurrent_Type
(Etype
(First_Entity
(E
))),
11934 -- Case 3: E is an entry of a synchronized type and a matching
11935 -- procedure has been previously defined in the enclosing scope
11936 -- of the synchronized type.
11938 elsif Comes_From_Source
(E
)
11939 and then Ekind
(E
) = E_Entry
11941 Present
(Matching_Dispatching_Subprogram
(Current_Scope
, E
))
11943 Report_Conflict
(E
,
11944 Matching_Dispatching_Subprogram
(Current_Scope
, E
));
11949 end Has_Matching_Entry_Or_Subprogram
;
11951 ----------------------------
11952 -- Is_Private_Declaration --
11953 ----------------------------
11955 function Is_Private_Declaration
(E
: Entity_Id
) return Boolean is
11956 Decl
: constant Node_Id
:= Unit_Declaration_Node
(E
);
11957 Priv_Decls
: List_Id
;
11960 if Is_Package_Or_Generic_Package
(Current_Scope
)
11961 and then In_Private_Part
(Current_Scope
)
11964 Private_Declarations
(Package_Specification
(Current_Scope
));
11966 return In_Package_Body
(Current_Scope
)
11968 (Is_List_Member
(Decl
)
11969 and then List_Containing
(Decl
) = Priv_Decls
)
11970 or else (Nkind
(Parent
(Decl
)) = N_Package_Specification
11972 Is_Compilation_Unit
11973 (Defining_Entity
(Parent
(Decl
)))
11974 and then List_Containing
(Parent
(Parent
(Decl
))) =
11979 end Is_Private_Declaration
;
11981 --------------------------
11982 -- Is_Overriding_Alias --
11983 --------------------------
11985 function Is_Overriding_Alias
11986 (Old_E
: Entity_Id
;
11987 New_E
: Entity_Id
) return Boolean
11989 AO
: constant Entity_Id
:= Alias
(Old_E
);
11990 AN
: constant Entity_Id
:= Alias
(New_E
);
11993 return Scope
(AO
) /= Scope
(AN
)
11994 or else No
(DTC_Entity
(AO
))
11995 or else No
(DTC_Entity
(AN
))
11996 or else DT_Position
(AO
) = DT_Position
(AN
);
11997 end Is_Overriding_Alias
;
11999 ---------------------
12000 -- Report_Conflict --
12001 ---------------------
12003 procedure Report_Conflict
(S
: Entity_Id
; E
: Entity_Id
) is
12005 Error_Msg_Sloc
:= Sloc
(E
);
12007 -- Generate message, with useful additional warning if in generic
12009 if Is_Generic_Unit
(E
) then
12010 Error_Msg_N
("previous generic unit cannot be overloaded", S
);
12011 Error_Msg_N
("\& conflicts with declaration#", S
);
12013 Error_Msg_N
("& conflicts with declaration#", S
);
12015 end Report_Conflict
;
12017 -- Start of processing for New_Overloaded_Entity
12020 -- We need to look for an entity that S may override. This must be a
12021 -- homonym in the current scope, so we look for the first homonym of
12022 -- S in the current scope as the starting point for the search.
12024 E
:= Current_Entity_In_Scope
(S
);
12026 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
12027 -- They are directly added to the list of primitive operations of
12028 -- Derived_Type, unless this is a rederivation in the private part
12029 -- of an operation that was already derived in the visible part of
12030 -- the current package.
12032 if Ada_Version
>= Ada_2005
12033 and then Present
(Derived_Type
)
12034 and then Present
(Alias
(S
))
12035 and then Is_Dispatching_Operation
(Alias
(S
))
12036 and then Present
(Find_Dispatching_Type
(Alias
(S
)))
12037 and then Is_Interface
(Find_Dispatching_Type
(Alias
(S
)))
12039 -- For private types, when the full-view is processed we propagate to
12040 -- the full view the non-overridden entities whose attribute "alias"
12041 -- references an interface primitive. These entities were added by
12042 -- Derive_Subprograms to ensure that interface primitives are
12045 -- Inside_Freeze_Actions is non zero when S corresponds with an
12046 -- internal entity that links an interface primitive with its
12047 -- covering primitive through attribute Interface_Alias (see
12048 -- Add_Internal_Interface_Entities).
12050 if Inside_Freezing_Actions
= 0
12051 and then Is_Package_Or_Generic_Package
(Current_Scope
)
12052 and then In_Private_Part
(Current_Scope
)
12053 and then Parent_Kind
(E
) = N_Private_Extension_Declaration
12054 and then Nkind
(Parent
(S
)) = N_Full_Type_Declaration
12055 and then Full_View
(Defining_Identifier
(Parent
(E
)))
12056 = Defining_Identifier
(Parent
(S
))
12057 and then Alias
(E
) = Alias
(S
)
12059 Check_Operation_From_Private_View
(S
, E
);
12060 Set_Is_Dispatching_Operation
(S
);
12065 Enter_Overloaded_Entity
(S
);
12066 Check_Dispatching_Operation
(S
, Empty
);
12067 Check_For_Primitive_Subprogram
(Is_Primitive_Subp
);
12073 -- For synchronized types check conflicts of this entity with previously
12074 -- defined entities.
12076 if Ada_Version
>= Ada_2005
12077 and then Has_Matching_Entry_Or_Subprogram
(S
)
12082 -- If there is no homonym then this is definitely not overriding
12085 Enter_Overloaded_Entity
(S
);
12086 Check_Dispatching_Operation
(S
, Empty
);
12087 Check_For_Primitive_Subprogram
(Is_Primitive_Subp
);
12089 -- If subprogram has an explicit declaration, check whether it has an
12090 -- overriding indicator.
12092 if Comes_From_Source
(S
) then
12093 Check_Synchronized_Overriding
(S
, Overridden_Subp
);
12095 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
12096 -- it may have overridden some hidden inherited primitive. Update
12097 -- Overridden_Subp to avoid spurious errors when checking the
12098 -- overriding indicator.
12100 if Ada_Version
>= Ada_2012
12101 and then No
(Overridden_Subp
)
12102 and then Is_Dispatching_Operation
(S
)
12103 and then Present
(Overridden_Operation
(S
))
12105 Overridden_Subp
:= Overridden_Operation
(S
);
12108 Check_Overriding_Indicator
12109 (S
, Overridden_Subp
, Is_Primitive
=> Is_Primitive_Subp
);
12111 -- The Ghost policy in effect at the point of declaration of a
12112 -- parent subprogram and an overriding subprogram must match
12113 -- (SPARK RM 6.9(17)).
12115 Check_Ghost_Overriding
(S
, Overridden_Subp
);
12118 -- If there is a homonym that is not overloadable, then we have an
12119 -- error, except for the special cases checked explicitly below.
12121 elsif not Is_Overloadable
(E
) then
12123 -- Check for spurious conflict produced by a subprogram that has the
12124 -- same name as that of the enclosing generic package. The conflict
12125 -- occurs within an instance, between the subprogram and the renaming
12126 -- declaration for the package. After the subprogram, the package
12127 -- renaming declaration becomes hidden.
12129 if Ekind
(E
) = E_Package
12130 and then Present
(Renamed_Entity
(E
))
12131 and then Renamed_Entity
(E
) = Current_Scope
12132 and then Nkind
(Parent
(Renamed_Entity
(E
))) =
12133 N_Package_Specification
12134 and then Present
(Generic_Parent
(Parent
(Renamed_Entity
(E
))))
12137 Set_Is_Immediately_Visible
(E
, False);
12138 Enter_Overloaded_Entity
(S
);
12139 Set_Homonym
(S
, Homonym
(E
));
12140 Check_Dispatching_Operation
(S
, Empty
);
12141 Check_Overriding_Indicator
(S
, Empty
, Is_Primitive
=> False);
12143 -- If the subprogram is implicit it is hidden by the previous
12144 -- declaration. However if it is dispatching, it must appear in the
12145 -- dispatch table anyway, because it can be dispatched to even if it
12146 -- cannot be called directly.
12148 elsif Present
(Alias
(S
)) and then not Comes_From_Source
(S
) then
12149 Set_Scope
(S
, Current_Scope
);
12151 if Is_Dispatching_Operation
(Alias
(S
)) then
12152 Check_Dispatching_Operation
(S
, Empty
);
12158 Report_Conflict
(S
, E
);
12162 -- E exists and is overloadable
12165 Check_Synchronized_Overriding
(S
, Overridden_Subp
);
12167 -- Loop through E and its homonyms to determine if any of them is
12168 -- the candidate for overriding by S.
12170 while Present
(E
) loop
12172 -- Definitely not interesting if not in the current scope
12174 if Scope
(E
) /= Current_Scope
then
12177 -- A function can overload the name of an abstract state. The
12178 -- state can be viewed as a function with a profile that cannot
12179 -- be matched by anything.
12181 elsif Ekind
(S
) = E_Function
12182 and then Ekind
(E
) = E_Abstract_State
12184 Enter_Overloaded_Entity
(S
);
12187 -- Ada 2012 (AI05-0165): For internally generated bodies of null
12188 -- procedures locate the internally generated spec. We enforce
12189 -- mode conformance since a tagged type may inherit from
12190 -- interfaces several null primitives which differ only in
12191 -- the mode of the formals.
12193 elsif not Comes_From_Source
(S
)
12194 and then Is_Null_Procedure
(S
)
12195 and then not Mode_Conformant
(E
, S
)
12199 -- Check if we have type conformance
12201 elsif Type_Conformant
(E
, S
) then
12203 -- If the old and new entities have the same profile and one
12204 -- is not the body of the other, then this is an error, unless
12205 -- one of them is implicitly declared.
12207 -- There are some cases when both can be implicit, for example
12208 -- when both a literal and a function that overrides it are
12209 -- inherited in a derivation, or when an inherited operation
12210 -- of a tagged full type overrides the inherited operation of
12211 -- a private extension. Ada 83 had a special rule for the
12212 -- literal case. In Ada 95, the later implicit operation hides
12213 -- the former, and the literal is always the former. In the
12214 -- odd case where both are derived operations declared at the
12215 -- same point, both operations should be declared, and in that
12216 -- case we bypass the following test and proceed to the next
12217 -- part. This can only occur for certain obscure cases in
12218 -- instances, when an operation on a type derived from a formal
12219 -- private type does not override a homograph inherited from
12220 -- the actual. In subsequent derivations of such a type, the
12221 -- DT positions of these operations remain distinct, if they
12224 if Present
(Alias
(S
))
12225 and then (No
(Alias
(E
))
12226 or else Comes_From_Source
(E
)
12227 or else Is_Abstract_Subprogram
(S
)
12229 (Is_Dispatching_Operation
(E
)
12230 and then Is_Overriding_Alias
(E
, S
)))
12231 and then Ekind
(E
) /= E_Enumeration_Literal
12233 -- When an derived operation is overloaded it may be due to
12234 -- the fact that the full view of a private extension
12235 -- re-inherits. It has to be dealt with.
12237 if Is_Package_Or_Generic_Package
(Current_Scope
)
12238 and then In_Private_Part
(Current_Scope
)
12240 Check_Operation_From_Private_View
(S
, E
);
12243 -- In any case the implicit operation remains hidden by the
12244 -- existing declaration, which is overriding. Indicate that
12245 -- E overrides the operation from which S is inherited.
12247 if Present
(Alias
(S
)) then
12248 Set_Overridden_Operation
(E
, Alias
(S
));
12249 Inherit_Subprogram_Contract
(E
, Alias
(S
));
12250 Set_Is_Ada_2022_Only
(E
,
12251 Is_Ada_2022_Only
(Alias
(S
)));
12254 Set_Overridden_Operation
(E
, S
);
12255 Inherit_Subprogram_Contract
(E
, S
);
12256 Set_Is_Ada_2022_Only
(E
, Is_Ada_2022_Only
(S
));
12259 -- When a dispatching operation overrides an inherited
12260 -- subprogram, it shall be subtype conformant with the
12261 -- inherited subprogram (RM 3.9.2 (10.2)).
12263 if Comes_From_Source
(E
)
12264 and then Is_Dispatching_Operation
(E
)
12265 and then Find_Dispatching_Type
(S
)
12266 = Find_Dispatching_Type
(E
)
12268 Check_Subtype_Conformant
(E
, S
);
12271 if Comes_From_Source
(E
) then
12272 Check_Overriding_Indicator
(E
, S
, Is_Primitive
=> False);
12274 -- The Ghost policy in effect at the point of declaration
12275 -- of a parent subprogram and an overriding subprogram
12276 -- must match (SPARK RM 6.9(17)).
12278 Check_Ghost_Overriding
(E
, S
);
12283 -- Within an instance, the renaming declarations for actual
12284 -- subprograms may become ambiguous, but they do not hide each
12287 elsif Ekind
(E
) /= E_Entry
12288 and then not Comes_From_Source
(E
)
12289 and then not Is_Generic_Instance
(E
)
12290 and then (Present
(Alias
(E
))
12291 or else Is_Intrinsic_Subprogram
(E
))
12292 and then (not In_Instance
12293 or else No
(Parent
(E
))
12294 or else Nkind
(Unit_Declaration_Node
(E
)) /=
12295 N_Subprogram_Renaming_Declaration
)
12297 -- A subprogram child unit is not allowed to override an
12298 -- inherited subprogram (10.1.1(20)).
12300 if Is_Child_Unit
(S
) then
12302 ("child unit overrides inherited subprogram in parent",
12307 if Is_Non_Overriding_Operation
(E
, S
) then
12308 Enter_Overloaded_Entity
(S
);
12310 if No
(Derived_Type
)
12311 or else Is_Tagged_Type
(Derived_Type
)
12313 Check_Dispatching_Operation
(S
, Empty
);
12319 -- E is a derived operation or an internal operator which
12320 -- is being overridden. Remove E from further visibility.
12321 -- Furthermore, if E is a dispatching operation, it must be
12322 -- replaced in the list of primitive operations of its type
12323 -- (see Override_Dispatching_Operation).
12325 Overridden_Subp
:= E
;
12327 -- It is possible for E to be in the current scope and
12328 -- yet not in the entity chain. This can only occur in a
12329 -- generic context where E is an implicit concatenation
12330 -- in the formal part, because in a generic body the
12331 -- entity chain starts with the formals.
12333 -- In GNATprove mode, a wrapper for an operation with
12334 -- axiomatization may be a homonym of another declaration
12335 -- for an actual subprogram (needs refinement ???).
12337 if No
(Prev_Entity
(E
)) then
12339 and then GNATprove_Mode
12341 Nkind
(Original_Node
(Unit_Declaration_Node
(S
))) =
12342 N_Subprogram_Renaming_Declaration
12346 pragma Assert
(Chars
(E
) = Name_Op_Concat
);
12351 -- E must be removed both from the entity_list of the
12352 -- current scope, and from the visibility chain.
12354 if Debug_Flag_E
then
12355 Write_Str
("Override implicit operation ");
12356 Write_Int
(Int
(E
));
12360 -- If E is a predefined concatenation, it stands for four
12361 -- different operations. As a result, a single explicit
12362 -- declaration does not hide it. In a possible ambiguous
12363 -- situation, Disambiguate chooses the user-defined op,
12364 -- so it is correct to retain the previous internal one.
12366 if Chars
(E
) /= Name_Op_Concat
12367 or else Ekind
(E
) /= E_Operator
12369 -- For nondispatching derived operations that are
12370 -- overridden by a subprogram declared in the private
12371 -- part of a package, we retain the derived subprogram
12372 -- but mark it as not immediately visible. If the
12373 -- derived operation was declared in the visible part
12374 -- then this ensures that it will still be visible
12375 -- outside the package with the proper signature
12376 -- (calls from outside must also be directed to this
12377 -- version rather than the overriding one, unlike the
12378 -- dispatching case). Calls from inside the package
12379 -- will still resolve to the overriding subprogram
12380 -- since the derived one is marked as not visible
12381 -- within the package.
12383 -- If the private operation is dispatching, we achieve
12384 -- the overriding by keeping the implicit operation
12385 -- but setting its alias to be the overriding one. In
12386 -- this fashion the proper body is executed in all
12387 -- cases, but the original signature is used outside
12390 -- If the overriding is not in the private part, we
12391 -- remove the implicit operation altogether.
12393 if Is_Private_Declaration
(S
) then
12394 if not Is_Dispatching_Operation
(E
) then
12395 Set_Is_Immediately_Visible
(E
, False);
12397 -- Work done in Override_Dispatching_Operation, so
12398 -- nothing else needs to be done here.
12400 -- ??? Special case to keep supporting the hiding
12401 -- of the predefined "=" operator for a nonlimited
12402 -- tagged type by a user-defined "=" operator for
12403 -- its class-wide type when the type is private.
12405 if Chars
(E
) = Name_Op_Eq
then
12407 Typ
: constant Entity_Id
12408 := Etype
(First_Entity
(E
));
12409 H
: Entity_Id
:= Homonym
(E
);
12413 and then Scope
(H
) = Scope
(E
)
12415 if Is_User_Defined_Equality
(H
)
12416 and then Is_Immediately_Visible
(H
)
12417 and then Etype
(First_Entity
(H
))
12418 = Class_Wide_Type
(Typ
)
12420 Remove_Entity_And_Homonym
(E
);
12431 Remove_Entity_And_Homonym
(E
);
12435 Enter_Overloaded_Entity
(S
);
12437 -- For entities generated by Derive_Subprograms the
12438 -- overridden operation is the inherited primitive
12439 -- (which is available through the attribute alias).
12441 if not (Comes_From_Source
(E
))
12442 and then Is_Dispatching_Operation
(E
)
12443 and then Find_Dispatching_Type
(E
) =
12444 Find_Dispatching_Type
(S
)
12445 and then Present
(Alias
(E
))
12446 and then Comes_From_Source
(Alias
(E
))
12448 Set_Overridden_Operation
(S
, Alias
(E
));
12449 Inherit_Subprogram_Contract
(S
, Alias
(E
));
12450 Set_Is_Ada_2022_Only
(S
,
12451 Is_Ada_2022_Only
(Alias
(E
)));
12453 -- Normal case of setting entity as overridden
12455 -- Note: Static_Initialization and Overridden_Operation
12456 -- attributes use the same field in subprogram entities.
12457 -- Static_Initialization is only defined for internal
12458 -- initialization procedures, where Overridden_Operation
12459 -- is irrelevant. Therefore the setting of this attribute
12460 -- must check whether the target is an init_proc.
12462 elsif not Is_Init_Proc
(S
) then
12464 -- LSP wrappers must override the ultimate alias of their
12465 -- wrapped dispatching primitive E; required to traverse
12466 -- the chain of ancestor primitives (c.f. Map_Primitives)
12467 -- They don't inherit contracts.
12470 and then Present
(LSP_Subprogram
(S
))
12472 Set_Overridden_Operation
(S
, Ultimate_Alias
(E
));
12474 Set_Overridden_Operation
(S
, E
);
12475 Inherit_Subprogram_Contract
(S
, E
);
12478 Set_Is_Ada_2022_Only
(S
, Is_Ada_2022_Only
(E
));
12481 Check_Overriding_Indicator
(S
, E
, Is_Primitive
=> True);
12483 -- The Ghost policy in effect at the point of declaration
12484 -- of a parent subprogram and an overriding subprogram
12485 -- must match (SPARK RM 6.9(17)).
12487 Check_Ghost_Overriding
(S
, E
);
12489 -- If S is a user-defined subprogram or a null procedure
12490 -- expanded to override an inherited null procedure, or a
12491 -- predefined dispatching primitive then indicate that E
12492 -- overrides the operation from which S is inherited.
12494 if Comes_From_Source
(S
)
12496 (Present
(Parent
(S
))
12497 and then Nkind
(Parent
(S
)) = N_Procedure_Specification
12498 and then Null_Present
(Parent
(S
)))
12500 (Present
(Alias
(E
))
12502 Is_Predefined_Dispatching_Operation
(Alias
(E
)))
12504 if Present
(Alias
(E
)) then
12506 -- LSP wrappers must override the ultimate alias of
12507 -- their wrapped dispatching primitive E; required to
12508 -- traverse the chain of ancestor primitives (see
12509 -- Map_Primitives). They don't inherit contracts.
12512 and then Present
(LSP_Subprogram
(S
))
12514 Set_Overridden_Operation
(S
, Ultimate_Alias
(E
));
12516 Set_Overridden_Operation
(S
, Alias
(E
));
12517 Inherit_Subprogram_Contract
(S
, Alias
(E
));
12520 Set_Is_Ada_2022_Only
(S
, Is_Ada_2022_Only
(Alias
(E
)));
12524 if Is_Dispatching_Operation
(E
) then
12526 -- An overriding dispatching subprogram inherits the
12527 -- convention of the overridden subprogram (AI-117).
12529 Set_Convention
(S
, Convention
(E
));
12530 Check_Dispatching_Operation
(S
, E
);
12533 Check_Dispatching_Operation
(S
, Empty
);
12536 Check_For_Primitive_Subprogram
12537 (Is_Primitive_Subp
, Is_Overriding
=> True);
12538 goto Check_Inequality
;
12540 -- Apparent redeclarations in instances can occur when two
12541 -- formal types get the same actual type. The subprograms in
12542 -- in the instance are legal, even if not callable from the
12543 -- outside. Calls from within are disambiguated elsewhere.
12544 -- For dispatching operations in the visible part, the usual
12545 -- rules apply, and operations with the same profile are not
12546 -- legal (B830001).
12548 elsif (In_Instance_Visible_Part
12549 and then not Is_Dispatching_Operation
(E
))
12550 or else In_Instance_Not_Visible
12554 -- Here we have a real error (identical profile)
12557 Error_Msg_Sloc
:= Sloc
(E
);
12559 -- Avoid cascaded errors if the entity appears in
12560 -- subsequent calls.
12562 Set_Scope
(S
, Current_Scope
);
12564 -- Generate error, with extra useful warning for the case
12565 -- of a generic instance with no completion.
12567 if Is_Generic_Instance
(S
)
12568 and then not Has_Completion
(E
)
12571 ("instantiation cannot provide body for&", S
);
12572 Error_Msg_N
("\& conflicts with declaration#", S
);
12574 Error_Msg_N
("& conflicts with declaration#", S
);
12581 -- If one subprogram has an access parameter and the other
12582 -- a parameter of an access type, calls to either might be
12583 -- ambiguous. Verify that parameters match except for the
12584 -- access parameter.
12586 if May_Hide_Profile
then
12592 F1
:= First_Formal
(S
);
12593 F2
:= First_Formal
(E
);
12594 while Present
(F1
) and then Present
(F2
) loop
12595 if Is_Access_Type
(Etype
(F1
)) then
12596 if not Is_Access_Type
(Etype
(F2
))
12597 or else not Conforming_Types
12598 (Designated_Type
(Etype
(F1
)),
12599 Designated_Type
(Etype
(F2
)),
12602 May_Hide_Profile
:= False;
12606 not Conforming_Types
12607 (Etype
(F1
), Etype
(F2
), Type_Conformant
)
12609 May_Hide_Profile
:= False;
12616 if May_Hide_Profile
12620 Error_Msg_NE
("calls to& may be ambiguous??", S
, S
);
12629 -- On exit, we know that S is a new entity
12631 Enter_Overloaded_Entity
(S
);
12632 Check_For_Primitive_Subprogram
(Is_Primitive_Subp
);
12633 Check_Overriding_Indicator
12634 (S
, Overridden_Subp
, Is_Primitive
=> Is_Primitive_Subp
);
12636 -- The Ghost policy in effect at the point of declaration of a parent
12637 -- subprogram and an overriding subprogram must match
12638 -- (SPARK RM 6.9(17)).
12640 Check_Ghost_Overriding
(S
, Overridden_Subp
);
12642 -- If S is a derived operation for an untagged type then by
12643 -- definition it's not a dispatching operation (even if the parent
12644 -- operation was dispatching), so Check_Dispatching_Operation is not
12645 -- called in that case.
12647 if No
(Derived_Type
)
12648 or else Is_Tagged_Type
(Derived_Type
)
12650 Check_Dispatching_Operation
(S
, Empty
);
12654 -- If this is a user-defined equality operator that is not a derived
12655 -- subprogram, create the corresponding inequality. If the operation is
12656 -- dispatching, the expansion is done elsewhere, and we do not create
12657 -- an explicit inequality operation.
12659 <<Check_Inequality
>>
12660 if Chars
(S
) = Name_Op_Eq
12661 and then Etype
(S
) = Standard_Boolean
12662 and then Present
(Parent
(S
))
12663 and then not Is_Dispatching_Operation
(S
)
12665 Make_Inequality_Operator
(S
);
12666 Check_Untagged_Equality
(S
);
12668 end New_Overloaded_Entity
;
12670 ----------------------------------
12671 -- Preanalyze_Formal_Expression --
12672 ----------------------------------
12674 procedure Preanalyze_Formal_Expression
(N
: Node_Id
; T
: Entity_Id
) is
12675 Save_In_Spec_Expression
: constant Boolean := In_Spec_Expression
;
12677 In_Spec_Expression
:= True;
12678 Preanalyze_With_Freezing_And_Resolve
(N
, T
);
12679 In_Spec_Expression
:= Save_In_Spec_Expression
;
12680 end Preanalyze_Formal_Expression
;
12682 ---------------------
12683 -- Process_Formals --
12684 ---------------------
12686 procedure Process_Formals
12688 Related_Nod
: Node_Id
)
12690 function Designates_From_Limited_With
(Typ
: Entity_Id
) return Boolean;
12691 -- Determine whether an access type designates a type coming from a
12694 function Is_Class_Wide_Default
(D
: Node_Id
) return Boolean;
12695 -- Check whether the default has a class-wide type. After analysis the
12696 -- default has the type of the formal, so we must also check explicitly
12697 -- for an access attribute.
12699 ----------------------------------
12700 -- Designates_From_Limited_With --
12701 ----------------------------------
12703 function Designates_From_Limited_With
(Typ
: Entity_Id
) return Boolean is
12704 Desig
: Entity_Id
:= Typ
;
12707 if Is_Access_Type
(Desig
) then
12708 Desig
:= Directly_Designated_Type
(Desig
);
12711 if Is_Class_Wide_Type
(Desig
) then
12712 Desig
:= Root_Type
(Desig
);
12716 Ekind
(Desig
) = E_Incomplete_Type
12717 and then From_Limited_With
(Desig
);
12718 end Designates_From_Limited_With
;
12720 ---------------------------
12721 -- Is_Class_Wide_Default --
12722 ---------------------------
12724 function Is_Class_Wide_Default
(D
: Node_Id
) return Boolean is
12726 return Is_Class_Wide_Type
(Designated_Type
(Etype
(D
)))
12727 or else (Nkind
(D
) = N_Attribute_Reference
12728 and then Attribute_Name
(D
) = Name_Access
12729 and then Is_Class_Wide_Type
(Etype
(Prefix
(D
))));
12730 end Is_Class_Wide_Default
;
12734 Context
: constant Node_Id
:= Parent
(Parent
(T
));
12736 Formal
: Entity_Id
;
12737 Formal_Type
: Entity_Id
;
12738 Param_Spec
: Node_Id
;
12741 Num_Out_Params
: Nat
:= 0;
12742 First_Out_Param
: Entity_Id
:= Empty
;
12743 -- Used for setting Is_Only_Out_Parameter
12745 -- Start of processing for Process_Formals
12748 -- In order to prevent premature use of the formals in the same formal
12749 -- part, the Ekind is left undefined until all default expressions are
12750 -- analyzed. The Ekind is established in a separate loop at the end.
12752 Param_Spec
:= First
(T
);
12753 while Present
(Param_Spec
) loop
12754 Formal
:= Defining_Identifier
(Param_Spec
);
12755 Set_Never_Set_In_Source
(Formal
, True);
12756 Enter_Name
(Formal
);
12758 -- Case of ordinary parameters
12760 if Nkind
(Parameter_Type
(Param_Spec
)) /= N_Access_Definition
then
12761 Find_Type
(Parameter_Type
(Param_Spec
));
12762 Ptype
:= Parameter_Type
(Param_Spec
);
12764 if Ptype
= Error
then
12768 -- Protect against malformed parameter types
12770 if Nkind
(Ptype
) not in N_Has_Entity
then
12771 Formal_Type
:= Any_Type
;
12773 Formal_Type
:= Entity
(Ptype
);
12776 if Is_Incomplete_Type
(Formal_Type
)
12778 (Is_Class_Wide_Type
(Formal_Type
)
12779 and then Is_Incomplete_Type
(Root_Type
(Formal_Type
)))
12781 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
12782 -- primitive operations, as long as their completion is
12783 -- in the same declarative part. If in the private part
12784 -- this means that the type cannot be a Taft-amendment type.
12785 -- Check is done on package exit. For access to subprograms,
12786 -- the use is legal for Taft-amendment types.
12788 -- Ada 2012: tagged incomplete types are allowed as generic
12789 -- formal types. They do not introduce dependencies and the
12790 -- corresponding generic subprogram does not have a delayed
12791 -- freeze, because it does not need a freeze node. However,
12792 -- it is still the case that untagged incomplete types cannot
12793 -- be Taft-amendment types and must be completed in private
12794 -- part, so the subprogram must appear in the list of private
12795 -- dependents of the type.
12797 if Is_Tagged_Type
(Formal_Type
)
12798 or else (Ada_Version
>= Ada_2012
12799 and then not From_Limited_With
(Formal_Type
)
12800 and then not Is_Generic_Type
(Formal_Type
))
12802 if Ekind
(Scope
(Current_Scope
)) = E_Package
12803 and then not Is_Generic_Type
(Formal_Type
)
12804 and then not Is_Class_Wide_Type
(Formal_Type
)
12806 if Nkind
(Parent
(T
)) not in
12807 N_Access_Function_Definition |
12808 N_Access_Procedure_Definition
12810 Append_Elmt
(Current_Scope
,
12811 Private_Dependents
(Base_Type
(Formal_Type
)));
12813 -- Freezing is delayed to ensure that Register_Prim
12814 -- will get called for this operation, which is needed
12815 -- in cases where static dispatch tables aren't built.
12816 -- (Note that the same is done for controlling access
12817 -- parameter cases in function Access_Definition.)
12819 if not Is_Thunk
(Current_Scope
) then
12820 Set_Has_Delayed_Freeze
(Current_Scope
);
12825 elsif Nkind
(Parent
(T
)) not in N_Access_Function_Definition
12826 | N_Access_Procedure_Definition
12828 -- AI05-0151: Tagged incomplete types are allowed in all
12829 -- formal parts. Untagged incomplete types are not allowed
12830 -- in bodies. Limited views of either kind are not allowed
12831 -- if there is no place at which the non-limited view can
12832 -- become available.
12834 -- Incomplete formal untagged types are not allowed in
12835 -- subprogram bodies (but are legal in their declarations).
12836 -- This excludes bodies created for null procedures, which
12837 -- are basic declarations.
12839 if Is_Generic_Type
(Formal_Type
)
12840 and then not Is_Tagged_Type
(Formal_Type
)
12841 and then Nkind
(Parent
(Related_Nod
)) = N_Subprogram_Body
12844 ("invalid use of formal incomplete type", Param_Spec
);
12846 elsif Ada_Version
>= Ada_2012
then
12847 if Is_Tagged_Type
(Formal_Type
)
12848 and then (not From_Limited_With
(Formal_Type
)
12849 or else not In_Package_Body
)
12853 elsif Nkind
(Context
) in N_Accept_Statement
12854 | N_Accept_Alternative
12856 or else (Nkind
(Context
) = N_Subprogram_Body
12857 and then Comes_From_Source
(Context
))
12860 ("invalid use of untagged incomplete type &",
12861 Ptype
, Formal_Type
);
12866 ("invalid use of incomplete type&",
12867 Param_Spec
, Formal_Type
);
12869 -- Further checks on the legality of incomplete types
12870 -- in formal parts are delayed until the freeze point
12871 -- of the enclosing subprogram or access to subprogram.
12875 elsif Ekind
(Formal_Type
) = E_Void
then
12877 ("premature use of&",
12878 Parameter_Type
(Param_Spec
), Formal_Type
);
12881 -- Ada 2012 (AI-142): Handle aliased parameters
12883 if Ada_Version
>= Ada_2012
12884 and then Aliased_Present
(Param_Spec
)
12886 Set_Is_Aliased
(Formal
);
12888 -- AI12-001: All aliased objects are considered to be specified
12889 -- as independently addressable (RM C.6(8.1/4)).
12891 Set_Is_Independent
(Formal
);
12894 -- Ada 2005 (AI-231): Create and decorate an internal subtype
12895 -- declaration corresponding to the null-excluding type of the
12896 -- formal in the enclosing scope. Finally, replace the parameter
12897 -- type of the formal with the internal subtype.
12899 if Ada_Version
>= Ada_2005
12900 and then Null_Exclusion_Present
(Param_Spec
)
12902 if not Is_Access_Type
(Formal_Type
) then
12904 ("`NOT NULL` allowed only for an access type", Param_Spec
);
12907 if Can_Never_Be_Null
(Formal_Type
)
12908 and then Comes_From_Source
(Related_Nod
)
12911 ("`NOT NULL` not allowed (& already excludes null)",
12912 Param_Spec
, Formal_Type
);
12916 Create_Null_Excluding_Itype
12918 Related_Nod
=> Related_Nod
,
12919 Scope_Id
=> Scope
(Current_Scope
));
12921 -- If the designated type of the itype is an itype that is
12922 -- not frozen yet, we set the Has_Delayed_Freeze attribute
12923 -- on the access subtype, to prevent order-of-elaboration
12924 -- issues in the backend.
12927 -- type T is access procedure;
12928 -- procedure Op (O : not null T);
12930 if Is_Itype
(Directly_Designated_Type
(Formal_Type
))
12932 not Is_Frozen
(Directly_Designated_Type
(Formal_Type
))
12934 Set_Has_Delayed_Freeze
(Formal_Type
);
12939 -- An access formal type
12943 Access_Definition
(Related_Nod
, Parameter_Type
(Param_Spec
));
12945 -- No need to continue if we already notified errors
12947 if not Present
(Formal_Type
) then
12951 -- Ada 2005 (AI-254)
12954 AD
: constant Node_Id
:=
12955 Access_To_Subprogram_Definition
12956 (Parameter_Type
(Param_Spec
));
12958 if Present
(AD
) and then Protected_Present
(AD
) then
12960 Replace_Anonymous_Access_To_Protected_Subprogram
12966 Set_Etype
(Formal
, Formal_Type
);
12968 -- Deal with default expression if present
12970 Default
:= Expression
(Param_Spec
);
12972 if Present
(Default
) then
12973 if Out_Present
(Param_Spec
) then
12975 ("default initialization only allowed for IN parameters",
12979 -- Do the special preanalysis of the expression (see section on
12980 -- "Handling of Default Expressions" in the spec of package Sem).
12982 Preanalyze_Formal_Expression
(Default
, Formal_Type
);
12984 -- An access to constant cannot be the default for
12985 -- an access parameter that is an access to variable.
12987 if Ekind
(Formal_Type
) = E_Anonymous_Access_Type
12988 and then not Is_Access_Constant
(Formal_Type
)
12989 and then Is_Access_Type
(Etype
(Default
))
12990 and then Is_Access_Constant
(Etype
(Default
))
12993 ("formal that is access to variable cannot be initialized "
12994 & "with an access-to-constant expression", Default
);
12997 -- Check that the designated type of an access parameter's default
12998 -- is not a class-wide type unless the parameter's designated type
12999 -- is also class-wide.
13001 if Ekind
(Formal_Type
) = E_Anonymous_Access_Type
13002 and then not Designates_From_Limited_With
(Formal_Type
)
13003 and then Is_Class_Wide_Default
(Default
)
13004 and then not Is_Class_Wide_Type
(Designated_Type
(Formal_Type
))
13007 ("access to class-wide expression not allowed here", Default
);
13010 -- Check incorrect use of dynamically tagged expressions
13012 if Is_Tagged_Type
(Formal_Type
) then
13013 Check_Dynamically_Tagged_Expression
13015 Typ
=> Formal_Type
,
13016 Related_Nod
=> Default
);
13020 -- Ada 2005 (AI-231): Static checks
13022 if Ada_Version
>= Ada_2005
13023 and then Is_Access_Type
(Etype
(Formal
))
13024 and then Can_Never_Be_Null
(Etype
(Formal
))
13026 Null_Exclusion_Static_Checks
(Param_Spec
);
13029 -- The following checks are relevant only when SPARK_Mode is on as
13030 -- these are not standard Ada legality rules.
13032 if SPARK_Mode
= On
then
13033 if Ekind
(Scope
(Formal
)) in E_Function | E_Generic_Function
then
13035 -- A function cannot have a parameter of mode IN OUT or OUT
13038 if Ekind
(Formal
) in E_In_Out_Parameter | E_Out_Parameter
then
13040 ("function cannot have parameter of mode `OUT` or "
13041 & "`IN OUT`", Formal
);
13044 -- A procedure cannot have an effectively volatile formal
13045 -- parameter of mode IN because it behaves as a constant
13046 -- (SPARK RM 7.1.3(4)).
13048 elsif Ekind
(Scope
(Formal
)) = E_Procedure
13049 and then Ekind
(Formal
) = E_In_Parameter
13050 and then Is_Effectively_Volatile
(Formal
)
13053 ("formal parameter of mode `IN` cannot be volatile", Formal
);
13057 -- Deal with aspects on formal parameters. Only Unreferenced is
13058 -- supported for the time being.
13060 if Has_Aspects
(Param_Spec
) then
13062 Aspect
: Node_Id
:= First
(Aspect_Specifications
(Param_Spec
));
13064 while Present
(Aspect
) loop
13065 if Chars
(Identifier
(Aspect
)) = Name_Unreferenced
then
13066 Set_Has_Pragma_Unreferenced
(Formal
);
13069 ("unsupported aspect& on parameter",
13070 Aspect
, Identifier
(Aspect
));
13082 -- If this is the formal part of a function specification, analyze the
13083 -- subtype mark in the context where the formals are visible but not
13084 -- yet usable, and may hide outer homographs.
13086 if Nkind
(Related_Nod
) = N_Function_Specification
then
13087 Analyze_Return_Type
(Related_Nod
);
13090 -- Now set the kind (mode) of each formal
13092 Param_Spec
:= First
(T
);
13093 while Present
(Param_Spec
) loop
13094 Formal
:= Defining_Identifier
(Param_Spec
);
13095 Set_Formal_Mode
(Formal
);
13097 if Ekind
(Formal
) = E_In_Parameter
then
13098 Default
:= Expression
(Param_Spec
);
13100 if Present
(Default
) then
13101 Set_Default_Value
(Formal
, Default
);
13103 if Is_Scalar_Type
(Etype
(Default
)) then
13104 if Nkind
(Parameter_Type
(Param_Spec
)) /=
13105 N_Access_Definition
13107 Formal_Type
:= Entity
(Parameter_Type
(Param_Spec
));
13111 (Related_Nod
, Parameter_Type
(Param_Spec
));
13114 Apply_Scalar_Range_Check
(Default
, Formal_Type
);
13118 elsif Ekind
(Formal
) = E_Out_Parameter
then
13119 Num_Out_Params
:= Num_Out_Params
+ 1;
13121 if Num_Out_Params
= 1 then
13122 First_Out_Param
:= Formal
;
13125 elsif Ekind
(Formal
) = E_In_Out_Parameter
then
13126 Num_Out_Params
:= Num_Out_Params
+ 1;
13129 -- Skip remaining processing if formal type was in error
13131 if Etype
(Formal
) = Any_Type
or else Error_Posted
(Formal
) then
13132 goto Next_Parameter
;
13135 -- Force call by reference if aliased
13138 Conv
: constant Convention_Id
:= Convention
(Etype
(Formal
));
13140 if Is_Aliased
(Formal
) then
13141 Set_Mechanism
(Formal
, By_Reference
);
13143 -- Warn if user asked this to be passed by copy
13145 if Conv
= Convention_Ada_Pass_By_Copy
then
13147 ("cannot pass aliased parameter & by copy??", Formal
);
13150 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
13152 elsif Conv
= Convention_Ada_Pass_By_Copy
then
13153 Set_Mechanism
(Formal
, By_Copy
);
13155 elsif Conv
= Convention_Ada_Pass_By_Reference
then
13156 Set_Mechanism
(Formal
, By_Reference
);
13164 if Present
(First_Out_Param
) and then Num_Out_Params
= 1 then
13165 Set_Is_Only_Out_Parameter
(First_Out_Param
);
13167 end Process_Formals
;
13169 ----------------------------
13170 -- Reference_Body_Formals --
13171 ----------------------------
13173 procedure Reference_Body_Formals
(Spec
: Entity_Id
; Bod
: Entity_Id
) is
13178 if Error_Posted
(Spec
) then
13182 -- Iterate over both lists. They may be of different lengths if the two
13183 -- specs are not conformant.
13185 Fs
:= First_Formal
(Spec
);
13186 Fb
:= First_Formal
(Bod
);
13187 while Present
(Fs
) and then Present
(Fb
) loop
13188 Generate_Reference
(Fs
, Fb
, 'b');
13190 if Style_Check
then
13191 Style
.Check_Identifier
(Fb
, Fs
);
13194 Set_Spec_Entity
(Fb
, Fs
);
13195 Set_Referenced
(Fs
, False);
13199 end Reference_Body_Formals
;
13201 -------------------------
13202 -- Set_Actual_Subtypes --
13203 -------------------------
13205 procedure Set_Actual_Subtypes
(N
: Node_Id
; Subp
: Entity_Id
) is
13207 Formal
: Entity_Id
;
13209 First_Stmt
: Node_Id
:= Empty
;
13210 AS_Needed
: Boolean;
13213 -- If this is an empty initialization procedure, no need to create
13214 -- actual subtypes (small optimization).
13216 if Ekind
(Subp
) = E_Procedure
and then Is_Null_Init_Proc
(Subp
) then
13219 -- Within a predicate function we do not want to generate local
13220 -- subtypes that may generate nested predicate functions.
13222 elsif Is_Subprogram
(Subp
) and then Is_Predicate_Function
(Subp
) then
13226 -- The subtype declarations may freeze the formals. The body generated
13227 -- for an expression function is not a freeze point, so do not emit
13228 -- these declarations (small loss of efficiency in rare cases).
13230 if Nkind
(N
) = N_Subprogram_Body
13231 and then Was_Expression_Function
(N
)
13236 Formal
:= First_Formal
(Subp
);
13237 while Present
(Formal
) loop
13238 T
:= Etype
(Formal
);
13240 -- We never need an actual subtype for a constrained formal
13242 if Is_Constrained
(T
) then
13243 AS_Needed
:= False;
13245 -- If we have unknown discriminants, then we do not need an actual
13246 -- subtype, or more accurately we cannot figure it out. Note that
13247 -- all class-wide types have unknown discriminants.
13249 elsif Has_Unknown_Discriminants
(T
) then
13250 AS_Needed
:= False;
13252 -- At this stage we have an unconstrained type that may need an
13253 -- actual subtype. For sure the actual subtype is needed if we have
13254 -- an unconstrained array type. However, in an instance, the type
13255 -- may appear as a subtype of the full view, while the actual is
13256 -- in fact private (in which case no actual subtype is needed) so
13257 -- check the kind of the base type.
13259 elsif Is_Array_Type
(Base_Type
(T
)) then
13262 -- The only other case needing an actual subtype is an unconstrained
13263 -- record type which is an IN parameter (we cannot generate actual
13264 -- subtypes for the OUT or IN OUT case, since an assignment can
13265 -- change the discriminant values. However we exclude the case of
13266 -- initialization procedures, since discriminants are handled very
13267 -- specially in this context, see the section entitled "Handling of
13268 -- Discriminants" in Einfo.
13270 -- We also exclude the case of Discrim_SO_Functions (functions used
13271 -- in front-end layout mode for size/offset values), since in such
13272 -- functions only discriminants are referenced, and not only are such
13273 -- subtypes not needed, but they cannot always be generated, because
13274 -- of order of elaboration issues.
13276 elsif Is_Record_Type
(T
)
13277 and then Ekind
(Formal
) = E_In_Parameter
13278 and then Chars
(Formal
) /= Name_uInit
13279 and then not Is_Unchecked_Union
(T
)
13280 and then not Is_Discrim_SO_Function
(Subp
)
13284 -- All other cases do not need an actual subtype
13287 AS_Needed
:= False;
13290 -- Generate actual subtypes for unconstrained arrays and
13291 -- unconstrained discriminated records.
13294 if Nkind
(N
) = N_Accept_Statement
then
13296 -- If expansion is active, the formal is replaced by a local
13297 -- variable that renames the corresponding entry of the
13298 -- parameter block, and it is this local variable that may
13299 -- require an actual subtype.
13301 if Expander_Active
then
13302 Decl
:= Build_Actual_Subtype
(T
, Renamed_Object
(Formal
));
13304 Decl
:= Build_Actual_Subtype
(T
, Formal
);
13307 if Present
(Handled_Statement_Sequence
(N
)) then
13309 First
(Statements
(Handled_Statement_Sequence
(N
)));
13310 Prepend
(Decl
, Statements
(Handled_Statement_Sequence
(N
)));
13311 Mark_Rewrite_Insertion
(Decl
);
13313 -- If the accept statement has no body, there will be no
13314 -- reference to the actuals, so no need to compute actual
13321 Decl
:= Build_Actual_Subtype
(T
, Formal
);
13322 Prepend
(Decl
, Declarations
(N
));
13323 Mark_Rewrite_Insertion
(Decl
);
13326 -- The declaration uses the bounds of an existing object, and
13327 -- therefore needs no constraint checks.
13329 Analyze
(Decl
, Suppress
=> All_Checks
);
13330 Set_Is_Actual_Subtype
(Defining_Identifier
(Decl
));
13332 -- We need to freeze manually the generated type when it is
13333 -- inserted anywhere else than in a declarative part.
13335 if Present
(First_Stmt
) then
13336 Insert_List_Before_And_Analyze
(First_Stmt
,
13337 Freeze_Entity
(Defining_Identifier
(Decl
), N
));
13339 -- Ditto if the type has a dynamic predicate, because the
13340 -- generated function will mention the actual subtype. The
13341 -- predicate may come from an explicit aspect of be inherited.
13343 elsif Has_Predicates
(T
) then
13344 Insert_List_After_And_Analyze
(Decl
,
13345 Freeze_Entity
(Defining_Identifier
(Decl
), N
));
13348 if Nkind
(N
) = N_Accept_Statement
13349 and then Expander_Active
13351 Set_Actual_Subtype
(Renamed_Object
(Formal
),
13352 Defining_Identifier
(Decl
));
13354 Set_Actual_Subtype
(Formal
, Defining_Identifier
(Decl
));
13358 Next_Formal
(Formal
);
13360 end Set_Actual_Subtypes
;
13362 ---------------------
13363 -- Set_Formal_Mode --
13364 ---------------------
13366 procedure Set_Formal_Mode
(Formal_Id
: Entity_Id
) is
13367 Spec
: constant Node_Id
:= Parent
(Formal_Id
);
13368 Id
: constant Entity_Id
:= Scope
(Formal_Id
);
13371 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
13372 -- since we ensure that corresponding actuals are always valid at the
13373 -- point of the call.
13375 if Out_Present
(Spec
) then
13377 or else Is_Subprogram_Or_Generic_Subprogram
(Id
)
13379 Set_Has_Out_Or_In_Out_Parameter
(Id
, True);
13382 if Ekind
(Id
) in E_Function | E_Generic_Function
then
13384 -- [IN] OUT parameters allowed for functions in Ada 2012
13386 if Ada_Version
>= Ada_2012
then
13388 -- Even in Ada 2012 operators can only have IN parameters
13390 if Is_Operator_Symbol_Name
(Chars
(Scope
(Formal_Id
))) then
13391 Error_Msg_N
("operators can only have IN parameters", Spec
);
13394 if In_Present
(Spec
) then
13395 Mutate_Ekind
(Formal_Id
, E_In_Out_Parameter
);
13397 Mutate_Ekind
(Formal_Id
, E_Out_Parameter
);
13400 -- But not in earlier versions of Ada
13403 Error_Msg_N
("functions can only have IN parameters", Spec
);
13404 Mutate_Ekind
(Formal_Id
, E_In_Parameter
);
13407 elsif In_Present
(Spec
) then
13408 Mutate_Ekind
(Formal_Id
, E_In_Out_Parameter
);
13411 Mutate_Ekind
(Formal_Id
, E_Out_Parameter
);
13412 Set_Is_True_Constant
(Formal_Id
, False);
13413 Set_Current_Value
(Formal_Id
, Empty
);
13417 Mutate_Ekind
(Formal_Id
, E_In_Parameter
);
13420 -- Set Is_Known_Non_Null for access parameters since the language
13421 -- guarantees that access parameters are always non-null. We also set
13422 -- Can_Never_Be_Null, since there is no way to change the value.
13424 if Nkind
(Parameter_Type
(Spec
)) = N_Access_Definition
then
13426 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
13427 -- null; In Ada 2005, only if then null_exclusion is explicit.
13429 if Ada_Version
< Ada_2005
13430 or else Can_Never_Be_Null
(Etype
(Formal_Id
))
13432 Set_Is_Known_Non_Null
(Formal_Id
);
13433 Set_Can_Never_Be_Null
(Formal_Id
);
13436 -- Ada 2005 (AI-231): Null-exclusion access subtype
13438 elsif Is_Access_Type
(Etype
(Formal_Id
))
13439 and then Can_Never_Be_Null
(Etype
(Formal_Id
))
13441 Set_Is_Known_Non_Null
(Formal_Id
);
13443 -- We can also set Can_Never_Be_Null (thus preventing some junk
13444 -- access checks) for the case of an IN parameter, which cannot
13445 -- be changed, or for an IN OUT parameter, which can be changed but
13446 -- not to a null value. But for an OUT parameter, the initial value
13447 -- passed in can be null, so we can't set this flag in that case.
13449 if Ekind
(Formal_Id
) /= E_Out_Parameter
then
13450 Set_Can_Never_Be_Null
(Formal_Id
);
13454 Set_Mechanism
(Formal_Id
, Default_Mechanism
);
13455 Set_Formal_Validity
(Formal_Id
);
13456 end Set_Formal_Mode
;
13458 -------------------------
13459 -- Set_Formal_Validity --
13460 -------------------------
13462 procedure Set_Formal_Validity
(Formal_Id
: Entity_Id
) is
13464 -- If no validity checking, then we cannot assume anything about the
13465 -- validity of parameters, since we do not know there is any checking
13466 -- of the validity on the call side.
13468 if not Validity_Checks_On
then
13471 -- If validity checking for parameters is enabled, this means we are
13472 -- not supposed to make any assumptions about argument values.
13474 elsif Validity_Check_Parameters
then
13477 -- If we are checking in parameters, we will assume that the caller is
13478 -- also checking parameters, so we can assume the parameter is valid.
13480 elsif Ekind
(Formal_Id
) = E_In_Parameter
13481 and then Validity_Check_In_Params
13483 Set_Is_Known_Valid
(Formal_Id
, True);
13485 -- Similar treatment for IN OUT parameters
13487 elsif Ekind
(Formal_Id
) = E_In_Out_Parameter
13488 and then Validity_Check_In_Out_Params
13490 Set_Is_Known_Valid
(Formal_Id
, True);
13492 end Set_Formal_Validity
;
13494 ------------------------
13495 -- Subtype_Conformant --
13496 ------------------------
13498 function Subtype_Conformant
13499 (New_Id
: Entity_Id
;
13500 Old_Id
: Entity_Id
;
13501 Skip_Controlling_Formals
: Boolean := False) return Boolean
13505 Check_Conformance
(New_Id
, Old_Id
, Subtype_Conformant
, False, Result
,
13506 Skip_Controlling_Formals
=> Skip_Controlling_Formals
);
13508 end Subtype_Conformant
;
13510 ---------------------
13511 -- Type_Conformant --
13512 ---------------------
13514 function Type_Conformant
13515 (New_Id
: Entity_Id
;
13516 Old_Id
: Entity_Id
;
13517 Skip_Controlling_Formals
: Boolean := False) return Boolean
13521 May_Hide_Profile
:= False;
13523 (New_Id
, Old_Id
, Type_Conformant
, False, Result
,
13524 Skip_Controlling_Formals
=> Skip_Controlling_Formals
);
13526 end Type_Conformant
;
13528 -------------------------------
13529 -- Valid_Operator_Definition --
13530 -------------------------------
13532 procedure Valid_Operator_Definition
(Designator
: Entity_Id
) is
13535 Id
: constant Name_Id
:= Chars
(Designator
);
13539 F
:= First_Formal
(Designator
);
13540 while Present
(F
) loop
13543 if Present
(Default_Value
(F
)) then
13545 ("default values not allowed for operator parameters",
13548 -- For function instantiations that are operators, we must check
13549 -- separately that the corresponding generic only has in-parameters.
13550 -- For subprogram declarations this is done in Set_Formal_Mode. Such
13551 -- an error could not arise in earlier versions of the language.
13553 elsif Ekind
(F
) /= E_In_Parameter
then
13554 Error_Msg_N
("operators can only have IN parameters", F
);
13560 -- Verify that user-defined operators have proper number of arguments
13561 -- First case of operators which can only be unary
13563 if Id
in Name_Op_Not | Name_Op_Abs
then
13566 -- Case of operators which can be unary or binary
13568 elsif Id
in Name_Op_Add | Name_Op_Subtract
then
13569 N_OK
:= (N
in 1 .. 2);
13571 -- All other operators can only be binary
13579 ("incorrect number of arguments for operator", Designator
);
13583 and then Base_Type
(Etype
(Designator
)) = Standard_Boolean
13584 and then not Is_Intrinsic_Subprogram
(Designator
)
13587 ("explicit definition of inequality not allowed", Designator
);
13589 end Valid_Operator_Definition
;