1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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 Debug
; use Debug
;
30 with Einfo
; use Einfo
;
31 with Elists
; use Elists
;
32 with Errout
; use Errout
;
33 with Expander
; use Expander
;
34 with Exp_Ch6
; use Exp_Ch6
;
35 with Exp_Ch7
; use Exp_Ch7
;
36 with Exp_Ch9
; use Exp_Ch9
;
37 with Exp_Dbug
; use Exp_Dbug
;
38 with Exp_Disp
; use Exp_Disp
;
39 with Exp_Tss
; use Exp_Tss
;
40 with Exp_Util
; use Exp_Util
;
41 with Fname
; use Fname
;
42 with Freeze
; use Freeze
;
43 with Itypes
; use Itypes
;
44 with Lib
.Xref
; use Lib
.Xref
;
45 with Layout
; use Layout
;
46 with Namet
; use Namet
;
48 with Nlists
; use Nlists
;
49 with Nmake
; use Nmake
;
51 with Output
; use Output
;
52 with Restrict
; use Restrict
;
53 with Rident
; use Rident
;
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_Ch10
; use Sem_Ch10
;
63 with Sem_Ch12
; use Sem_Ch12
;
64 with Sem_Ch13
; use Sem_Ch13
;
65 with Sem_Dim
; use Sem_Dim
;
66 with Sem_Disp
; use Sem_Disp
;
67 with Sem_Dist
; use Sem_Dist
;
68 with Sem_Elim
; use Sem_Elim
;
69 with Sem_Eval
; use Sem_Eval
;
70 with Sem_Mech
; use Sem_Mech
;
71 with Sem_Prag
; use Sem_Prag
;
72 with Sem_Res
; use Sem_Res
;
73 with Sem_Util
; use Sem_Util
;
74 with Sem_Type
; use Sem_Type
;
75 with Sem_Warn
; use Sem_Warn
;
76 with Sinput
; use Sinput
;
77 with Stand
; use Stand
;
78 with Sinfo
; use Sinfo
;
79 with Sinfo
.CN
; use Sinfo
.CN
;
80 with Snames
; use Snames
;
81 with Stringt
; use Stringt
;
83 with Stylesw
; use Stylesw
;
84 with Targparm
; use Targparm
;
85 with Tbuild
; use Tbuild
;
86 with Uintp
; use Uintp
;
87 with Urealp
; use Urealp
;
88 with Validsw
; use Validsw
;
90 package body Sem_Ch6
is
92 May_Hide_Profile
: Boolean := False;
93 -- This flag is used to indicate that two formals in two subprograms being
94 -- checked for conformance differ only in that one is an access parameter
95 -- while the other is of a general access type with the same designated
96 -- type. In this case, if the rest of the signatures match, a call to
97 -- either subprogram may be ambiguous, which is worth a warning. The flag
98 -- is set in Compatible_Types, and the warning emitted in
99 -- New_Overloaded_Entity.
101 -----------------------
102 -- Local Subprograms --
103 -----------------------
105 procedure Analyze_Null_Procedure
107 Is_Completion
: out Boolean);
108 -- A null procedure can be a declaration or (Ada 2012) a completion.
110 procedure Analyze_Return_Statement
(N
: Node_Id
);
111 -- Common processing for simple and extended return statements
113 procedure Analyze_Function_Return
(N
: Node_Id
);
114 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
115 -- applies to a [generic] function.
117 procedure Analyze_Return_Type
(N
: Node_Id
);
118 -- Subsidiary to Process_Formals: analyze subtype mark in function
119 -- specification in a context where the formals are visible and hide
122 procedure Analyze_Subprogram_Body_Helper
(N
: Node_Id
);
123 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
124 -- that we can use RETURN but not skip the debug output at the end.
126 procedure Analyze_Generic_Subprogram_Body
(N
: Node_Id
; Gen_Id
: Entity_Id
);
127 -- Analyze a generic subprogram body. N is the body to be analyzed, and
128 -- Gen_Id is the defining entity Id for the corresponding spec.
130 procedure Build_Body_To_Inline
(N
: Node_Id
; Subp
: Entity_Id
);
131 -- If a subprogram has pragma Inline and inlining is active, use generic
132 -- machinery to build an unexpanded body for the subprogram. This body is
133 -- subsequently used for inline expansions at call sites. If subprogram can
134 -- be inlined (depending on size and nature of local declarations) this
135 -- function returns true. Otherwise subprogram body is treated normally.
136 -- If proper warnings are enabled and the subprogram contains a construct
137 -- that cannot be inlined, the offending construct is flagged accordingly.
139 function Can_Override_Operator
(Subp
: Entity_Id
) return Boolean;
140 -- Returns true if Subp can override a predefined operator.
142 procedure Check_And_Build_Body_To_Inline
145 Body_Id
: Entity_Id
);
146 -- Spec_Id and Body_Id are the entities of the specification and body of
147 -- the subprogram body N. If N can be inlined by the frontend (supported
148 -- cases documented in Check_Body_To_Inline) then build the body-to-inline
149 -- associated with N and attach it to the declaration node of Spec_Id.
151 procedure Check_Conformance
154 Ctype
: Conformance_Type
;
156 Conforms
: out Boolean;
157 Err_Loc
: Node_Id
:= Empty
;
158 Get_Inst
: Boolean := False;
159 Skip_Controlling_Formals
: Boolean := False);
160 -- Given two entities, this procedure checks that the profiles associated
161 -- with these entities meet the conformance criterion given by the third
162 -- parameter. If they conform, Conforms is set True and control returns
163 -- to the caller. If they do not conform, Conforms is set to False, and
164 -- in addition, if Errmsg is True on the call, proper messages are output
165 -- to complain about the conformance failure. If Err_Loc is non_Empty
166 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
167 -- error messages are placed on the appropriate part of the construct
168 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
169 -- against a formal access-to-subprogram type so Get_Instance_Of must
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 on an untagged record type
193 -- must appear before the type is frozen, and have the same visibility as
194 -- that of the type. This procedure checks that this rule is met, and
195 -- otherwise emits an error on the subprogram declaration and a warning
196 -- on the earlier freeze point if it is easy to locate.
198 procedure Enter_Overloaded_Entity
(S
: Entity_Id
);
199 -- This procedure makes S, a new overloaded entity, into the first visible
200 -- entity with that name.
202 function Is_Non_Overriding_Operation
204 New_E
: Entity_Id
) return Boolean;
205 -- Enforce the rule given in 12.3(18): a private operation in an instance
206 -- overrides an inherited operation only if the corresponding operation
207 -- was overriding in the generic. This needs to be checked for primitive
208 -- operations of types derived (in the generic unit) from formal private
209 -- or formal derived types.
211 procedure Make_Inequality_Operator
(S
: Entity_Id
);
212 -- Create the declaration for an inequality operator that is implicitly
213 -- created by a user-defined equality operator that yields a boolean.
215 procedure Set_Formal_Validity
(Formal_Id
: Entity_Id
);
216 -- Formal_Id is an formal parameter entity. This procedure deals with
217 -- setting the proper validity status for this entity, which depends on
218 -- the kind of parameter and the validity checking mode.
220 ---------------------------------------------
221 -- Analyze_Abstract_Subprogram_Declaration --
222 ---------------------------------------------
224 procedure Analyze_Abstract_Subprogram_Declaration
(N
: Node_Id
) is
225 Designator
: constant Entity_Id
:=
226 Analyze_Subprogram_Specification
(Specification
(N
));
227 Scop
: constant Entity_Id
:= Current_Scope
;
230 Check_SPARK_Restriction
("abstract subprogram is not allowed", N
);
232 Generate_Definition
(Designator
);
233 Set_Contract
(Designator
, Make_Contract
(Sloc
(Designator
)));
234 Set_Is_Abstract_Subprogram
(Designator
);
235 New_Overloaded_Entity
(Designator
);
236 Check_Delayed_Subprogram
(Designator
);
238 Set_Categorization_From_Scope
(Designator
, Scop
);
240 if Ekind
(Scope
(Designator
)) = E_Protected_Type
then
242 ("abstract subprogram not allowed in protected type", N
);
244 -- Issue a warning if the abstract subprogram is neither a dispatching
245 -- operation nor an operation that overrides an inherited subprogram or
246 -- predefined operator, since this most likely indicates a mistake.
248 elsif Warn_On_Redundant_Constructs
249 and then not Is_Dispatching_Operation
(Designator
)
250 and then not Present
(Overridden_Operation
(Designator
))
251 and then (not Is_Operator_Symbol_Name
(Chars
(Designator
))
252 or else Scop
/= Scope
(Etype
(First_Formal
(Designator
))))
255 ("abstract subprogram is not dispatching or overriding?r?", N
);
258 Generate_Reference_To_Formals
(Designator
);
259 Check_Eliminated
(Designator
);
261 if Has_Aspects
(N
) then
262 Analyze_Aspect_Specifications
(N
, Designator
);
264 end Analyze_Abstract_Subprogram_Declaration
;
266 ---------------------------------
267 -- Analyze_Expression_Function --
268 ---------------------------------
270 procedure Analyze_Expression_Function
(N
: Node_Id
) is
271 Loc
: constant Source_Ptr
:= Sloc
(N
);
272 LocX
: constant Source_Ptr
:= Sloc
(Expression
(N
));
273 Expr
: constant Node_Id
:= Expression
(N
);
274 Spec
: constant Node_Id
:= Specification
(N
);
279 -- If the expression is a completion, Prev is the entity whose
280 -- declaration is completed. Def_Id is needed to analyze the spec.
288 -- This is one of the occasions on which we transform the tree during
289 -- semantic analysis. If this is a completion, transform the expression
290 -- function into an equivalent subprogram body, and analyze it.
292 -- Expression functions are inlined unconditionally. The back-end will
293 -- determine whether this is possible.
295 Inline_Processing_Required
:= True;
297 -- Create a specification for the generated body. Types and defauts in
298 -- the profile are copies of the spec, but new entities must be created
299 -- for the unit name and the formals.
301 New_Spec
:= New_Copy_Tree
(Spec
);
302 Set_Defining_Unit_Name
(New_Spec
,
303 Make_Defining_Identifier
(Sloc
(Defining_Unit_Name
(Spec
)),
304 Chars
(Defining_Unit_Name
(Spec
))));
306 if Present
(Parameter_Specifications
(New_Spec
)) then
308 Formal_Spec
: Node_Id
;
310 Formal_Spec
:= First
(Parameter_Specifications
(New_Spec
));
311 while Present
(Formal_Spec
) loop
312 Set_Defining_Identifier
314 Make_Defining_Identifier
(Sloc
(Formal_Spec
),
315 Chars
=> Chars
(Defining_Identifier
(Formal_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.
326 if Present
(Prev
) and then Is_Overloadable
(Prev
) then
327 Def_Id
:= Analyze_Subprogram_Specification
(Spec
);
328 Prev
:= Find_Corresponding_Spec
(N
);
331 Ret
:= Make_Simple_Return_Statement
(LocX
, Expression
(N
));
334 Make_Subprogram_Body
(Loc
,
335 Specification
=> New_Spec
,
336 Declarations
=> Empty_List
,
337 Handled_Statement_Sequence
=>
338 Make_Handled_Sequence_Of_Statements
(LocX
,
339 Statements
=> New_List
(Ret
)));
341 -- If the expression completes a generic subprogram, we must create a
342 -- separate node for the body, because at instantiation the original
343 -- node of the generic copy must be a generic subprogram body, and
344 -- cannot be a expression function. Otherwise we just rewrite the
345 -- expression with the non-generic body.
347 if Present
(Prev
) and then Ekind
(Prev
) = E_Generic_Function
then
348 Insert_After
(N
, New_Body
);
350 -- Propagate any aspects or pragmas that apply to the expression
351 -- function to the proper body when the expression function acts
354 if Has_Aspects
(N
) then
355 Move_Aspects
(N
, To
=> New_Body
);
358 Relocate_Pragmas_To_Body
(New_Body
);
360 Rewrite
(N
, Make_Null_Statement
(Loc
));
361 Set_Has_Completion
(Prev
, False);
364 Set_Is_Inlined
(Prev
);
366 elsif Present
(Prev
) and then Comes_From_Source
(Prev
) then
367 Set_Has_Completion
(Prev
, False);
369 -- For navigation purposes, indicate that the function is a body
371 Generate_Reference
(Prev
, Defining_Entity
(N
), 'b', Force
=> True);
372 Rewrite
(N
, New_Body
);
374 -- Propagate any pragmas that apply to the expression function to the
375 -- proper body when the expression function acts as a completion.
376 -- Aspects are automatically transfered because of node rewriting.
378 Relocate_Pragmas_To_Body
(N
);
381 -- Prev is the previous entity with the same name, but it is can
382 -- be an unrelated spec that is not completed by the expression
383 -- function. In that case the relevant entity is the one in the body.
384 -- Not clear that the backend can inline it in this case ???
386 if Has_Completion
(Prev
) then
387 Set_Is_Inlined
(Prev
);
389 -- The formals of the expression function are body formals,
390 -- and do not appear in the ali file, which will only contain
391 -- references to the formals of the original subprogram spec.
398 F1
:= First_Formal
(Def_Id
);
399 F2
:= First_Formal
(Prev
);
401 while Present
(F1
) loop
402 Set_Spec_Entity
(F1
, F2
);
409 Set_Is_Inlined
(Defining_Entity
(New_Body
));
412 -- If this is not a completion, create both a declaration and a body, so
413 -- that the expression can be inlined whenever possible.
416 -- An expression function that is not a completion is not a
417 -- subprogram declaration, and thus cannot appear in a protected
420 if Nkind
(Parent
(N
)) = N_Protected_Definition
then
422 ("an expression function is not a legal protected operation", N
);
426 Make_Subprogram_Declaration
(Loc
, Specification
=> Spec
);
428 Rewrite
(N
, New_Decl
);
430 Set_Is_Inlined
(Defining_Entity
(New_Decl
));
432 -- To prevent premature freeze action, insert the new body at the end
433 -- of the current declarations, or at the end of the package spec.
434 -- However, resolve usage names now, to prevent spurious visibility
435 -- on later entities.
438 Decls
: List_Id
:= List_Containing
(N
);
439 Par
: constant Node_Id
:= Parent
(Decls
);
440 Id
: constant Entity_Id
:= Defining_Entity
(New_Decl
);
443 if Nkind
(Par
) = N_Package_Specification
444 and then Decls
= Visible_Declarations
(Par
)
445 and then Present
(Private_Declarations
(Par
))
446 and then not Is_Empty_List
(Private_Declarations
(Par
))
448 Decls
:= Private_Declarations
(Par
);
451 Insert_After
(Last
(Decls
), New_Body
);
453 Install_Formals
(Id
);
455 -- Do a preanalysis of the expression on a separate copy, to
456 -- prevent visibility issues later with operators in instances.
457 -- Attach copy to tree so that parent links are available.
460 Expr
: constant Node_Id
:= New_Copy_Tree
(Expression
(Ret
));
462 Set_Parent
(Expr
, Ret
);
463 Preanalyze_Spec_Expression
(Expr
, Etype
(Id
));
470 -- If the return expression is a static constant, we suppress warning
471 -- messages on unused formals, which in most cases will be noise.
473 Set_Is_Trivial_Subprogram
(Defining_Entity
(New_Body
),
474 Is_OK_Static_Expression
(Expr
));
475 end Analyze_Expression_Function
;
477 ----------------------------------------
478 -- Analyze_Extended_Return_Statement --
479 ----------------------------------------
481 procedure Analyze_Extended_Return_Statement
(N
: Node_Id
) is
483 Analyze_Return_Statement
(N
);
484 end Analyze_Extended_Return_Statement
;
486 ----------------------------
487 -- Analyze_Function_Call --
488 ----------------------------
490 procedure Analyze_Function_Call
(N
: Node_Id
) is
491 Actuals
: constant List_Id
:= Parameter_Associations
(N
);
492 Func_Nam
: constant Node_Id
:= Name
(N
);
498 -- A call of the form A.B (X) may be an Ada 2005 call, which is
499 -- rewritten as B (A, X). If the rewriting is successful, the call
500 -- has been analyzed and we just return.
502 if Nkind
(Func_Nam
) = N_Selected_Component
503 and then Name
(N
) /= Func_Nam
504 and then Is_Rewrite_Substitution
(N
)
505 and then Present
(Etype
(N
))
510 -- If error analyzing name, then set Any_Type as result type and return
512 if Etype
(Func_Nam
) = Any_Type
then
513 Set_Etype
(N
, Any_Type
);
517 -- Otherwise analyze the parameters
519 if Present
(Actuals
) then
520 Actual
:= First
(Actuals
);
521 while Present
(Actual
) loop
523 Check_Parameterless_Call
(Actual
);
529 end Analyze_Function_Call
;
531 -----------------------------
532 -- Analyze_Function_Return --
533 -----------------------------
535 procedure Analyze_Function_Return
(N
: Node_Id
) is
536 Loc
: constant Source_Ptr
:= Sloc
(N
);
537 Stm_Entity
: constant Entity_Id
:= Return_Statement_Entity
(N
);
538 Scope_Id
: constant Entity_Id
:= Return_Applies_To
(Stm_Entity
);
540 R_Type
: constant Entity_Id
:= Etype
(Scope_Id
);
541 -- Function result subtype
543 procedure Check_Limited_Return
(Expr
: Node_Id
);
544 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
545 -- limited types. Used only for simple return statements.
546 -- Expr is the expression returned.
548 procedure Check_Return_Subtype_Indication
(Obj_Decl
: Node_Id
);
549 -- Check that the return_subtype_indication properly matches the result
550 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
552 --------------------------
553 -- Check_Limited_Return --
554 --------------------------
556 procedure Check_Limited_Return
(Expr
: Node_Id
) is
558 -- Ada 2005 (AI-318-02): Return-by-reference types have been
559 -- removed and replaced by anonymous access results. This is an
560 -- incompatibility with Ada 95. Not clear whether this should be
561 -- enforced yet or perhaps controllable with special switch. ???
563 -- A limited interface that is not immutably limited is OK.
565 if Is_Limited_Interface
(R_Type
)
567 not (Is_Task_Interface
(R_Type
)
568 or else Is_Protected_Interface
(R_Type
)
569 or else Is_Synchronized_Interface
(R_Type
))
573 elsif Is_Limited_Type
(R_Type
)
574 and then not Is_Interface
(R_Type
)
575 and then Comes_From_Source
(N
)
576 and then not In_Instance_Body
577 and then not OK_For_Limited_Init_In_05
(R_Type
, Expr
)
581 if Ada_Version
>= Ada_2005
582 and then not Debug_Flag_Dot_L
583 and then not GNAT_Mode
586 ("(Ada 2005) cannot copy object of a limited type " &
587 "(RM-2005 6.5(5.5/2))", Expr
);
589 if Is_Limited_View
(R_Type
) then
591 ("\return by reference not permitted in Ada 2005", Expr
);
594 -- Warn in Ada 95 mode, to give folks a heads up about this
597 -- In GNAT mode, this is just a warning, to allow it to be
598 -- evilly turned off. Otherwise it is a real error.
600 -- In a generic context, simplify the warning because it makes
601 -- no sense to discuss pass-by-reference or copy.
603 elsif Warn_On_Ada_2005_Compatibility
or GNAT_Mode
then
604 if Inside_A_Generic
then
606 ("return of limited object not permitted in Ada 2005 "
607 & "(RM-2005 6.5(5.5/2))?y?", Expr
);
609 elsif Is_Limited_View
(R_Type
) then
611 ("return by reference not permitted in Ada 2005 "
612 & "(RM-2005 6.5(5.5/2))?y?", Expr
);
615 ("cannot copy object of a limited type in Ada 2005 "
616 & "(RM-2005 6.5(5.5/2))?y?", Expr
);
619 -- Ada 95 mode, compatibility warnings disabled
622 return; -- skip continuation messages below
625 if not Inside_A_Generic
then
627 ("\consider switching to return of access type", Expr
);
628 Explain_Limited_Type
(R_Type
, Expr
);
631 end Check_Limited_Return
;
633 -------------------------------------
634 -- Check_Return_Subtype_Indication --
635 -------------------------------------
637 procedure Check_Return_Subtype_Indication
(Obj_Decl
: Node_Id
) is
638 Return_Obj
: constant Node_Id
:= Defining_Identifier
(Obj_Decl
);
640 R_Stm_Type
: constant Entity_Id
:= Etype
(Return_Obj
);
641 -- Subtype given in the extended return statement (must match R_Type)
643 Subtype_Ind
: constant Node_Id
:=
644 Object_Definition
(Original_Node
(Obj_Decl
));
646 R_Type_Is_Anon_Access
:
648 Ekind
(R_Type
) = E_Anonymous_Access_Subprogram_Type
650 Ekind
(R_Type
) = E_Anonymous_Access_Protected_Subprogram_Type
652 Ekind
(R_Type
) = E_Anonymous_Access_Type
;
653 -- True if return type of the function is an anonymous access type
654 -- Can't we make Is_Anonymous_Access_Type in einfo ???
656 R_Stm_Type_Is_Anon_Access
:
658 Ekind
(R_Stm_Type
) = E_Anonymous_Access_Subprogram_Type
660 Ekind
(R_Stm_Type
) = E_Anonymous_Access_Protected_Subprogram_Type
662 Ekind
(R_Stm_Type
) = E_Anonymous_Access_Type
;
663 -- True if type of the return object is an anonymous access type
666 -- First, avoid cascaded errors
668 if Error_Posted
(Obj_Decl
) or else Error_Posted
(Subtype_Ind
) then
672 -- "return access T" case; check that the return statement also has
673 -- "access T", and that the subtypes statically match:
674 -- if this is an access to subprogram the signatures must match.
676 if R_Type_Is_Anon_Access
then
677 if R_Stm_Type_Is_Anon_Access
then
679 Ekind
(Designated_Type
(R_Stm_Type
)) /= E_Subprogram_Type
681 if Base_Type
(Designated_Type
(R_Stm_Type
)) /=
682 Base_Type
(Designated_Type
(R_Type
))
683 or else not Subtypes_Statically_Match
(R_Stm_Type
, R_Type
)
686 ("subtype must statically match function result subtype",
687 Subtype_Mark
(Subtype_Ind
));
691 -- For two anonymous access to subprogram types, the
692 -- types themselves must be type conformant.
694 if not Conforming_Types
695 (R_Stm_Type
, R_Type
, Fully_Conformant
)
698 ("subtype must statically match function result subtype",
704 Error_Msg_N
("must use anonymous access type", Subtype_Ind
);
707 -- If the return object is of an anonymous access type, then report
708 -- an error if the function's result type is not also anonymous.
710 elsif R_Stm_Type_Is_Anon_Access
711 and then not R_Type_Is_Anon_Access
713 Error_Msg_N
("anonymous access not allowed for function with " &
714 "named access result", Subtype_Ind
);
716 -- Subtype indication case: check that the return object's type is
717 -- covered by the result type, and that the subtypes statically match
718 -- when the result subtype is constrained. Also handle record types
719 -- with unknown discriminants for which we have built the underlying
720 -- record view. Coverage is needed to allow specific-type return
721 -- objects when the result type is class-wide (see AI05-32).
723 elsif Covers
(Base_Type
(R_Type
), Base_Type
(R_Stm_Type
))
724 or else (Is_Underlying_Record_View
(Base_Type
(R_Stm_Type
))
728 Underlying_Record_View
(Base_Type
(R_Stm_Type
))))
730 -- A null exclusion may be present on the return type, on the
731 -- function specification, on the object declaration or on the
734 if Is_Access_Type
(R_Type
)
736 (Can_Never_Be_Null
(R_Type
)
737 or else Null_Exclusion_Present
(Parent
(Scope_Id
))) /=
738 Can_Never_Be_Null
(R_Stm_Type
)
741 ("subtype must statically match function result subtype",
745 -- AI05-103: for elementary types, subtypes must statically match
747 if Is_Constrained
(R_Type
)
748 or else Is_Access_Type
(R_Type
)
750 if not Subtypes_Statically_Match
(R_Stm_Type
, R_Type
) then
752 ("subtype must statically match function result subtype",
757 elsif Etype
(Base_Type
(R_Type
)) = R_Stm_Type
758 and then Is_Null_Extension
(Base_Type
(R_Type
))
764 ("wrong type for return_subtype_indication", Subtype_Ind
);
766 end Check_Return_Subtype_Indication
;
768 ---------------------
769 -- Local Variables --
770 ---------------------
774 -- Start of processing for Analyze_Function_Return
777 Set_Return_Present
(Scope_Id
);
779 if Nkind
(N
) = N_Simple_Return_Statement
then
780 Expr
:= Expression
(N
);
782 -- Guard against a malformed expression. The parser may have tried to
783 -- recover but the node is not analyzable.
785 if Nkind
(Expr
) = N_Error
then
786 Set_Etype
(Expr
, Any_Type
);
787 Expander_Mode_Save_And_Set
(False);
791 -- The resolution of a controlled [extension] aggregate associated
792 -- with a return statement creates a temporary which needs to be
793 -- finalized on function exit. Wrap the return statement inside a
794 -- block so that the finalization machinery can detect this case.
795 -- This early expansion is done only when the return statement is
796 -- not part of a handled sequence of statements.
798 if Nkind_In
(Expr
, N_Aggregate
,
799 N_Extension_Aggregate
)
800 and then Needs_Finalization
(R_Type
)
801 and then Nkind
(Parent
(N
)) /= N_Handled_Sequence_Of_Statements
804 Make_Block_Statement
(Loc
,
805 Handled_Statement_Sequence
=>
806 Make_Handled_Sequence_Of_Statements
(Loc
,
807 Statements
=> New_List
(Relocate_Node
(N
)))));
813 Analyze_And_Resolve
(Expr
, R_Type
);
814 Check_Limited_Return
(Expr
);
817 -- RETURN only allowed in SPARK as the last statement in function
819 if Nkind
(Parent
(N
)) /= N_Handled_Sequence_Of_Statements
821 (Nkind
(Parent
(Parent
(N
))) /= N_Subprogram_Body
822 or else Present
(Next
(N
)))
824 Check_SPARK_Restriction
825 ("RETURN should be the last statement in function", N
);
829 Check_SPARK_Restriction
("extended RETURN is not allowed", N
);
831 -- Analyze parts specific to extended_return_statement:
834 Obj_Decl
: constant Node_Id
:=
835 Last
(Return_Object_Declarations
(N
));
836 Has_Aliased
: constant Boolean := Aliased_Present
(Obj_Decl
);
837 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
840 Expr
:= Expression
(Obj_Decl
);
842 -- Note: The check for OK_For_Limited_Init will happen in
843 -- Analyze_Object_Declaration; we treat it as a normal
844 -- object declaration.
846 Set_Is_Return_Object
(Defining_Identifier
(Obj_Decl
));
849 Check_Return_Subtype_Indication
(Obj_Decl
);
851 if Present
(HSS
) then
854 if Present
(Exception_Handlers
(HSS
)) then
856 -- ???Has_Nested_Block_With_Handler needs to be set.
857 -- Probably by creating an actual N_Block_Statement.
858 -- Probably in Expand.
864 -- Mark the return object as referenced, since the return is an
865 -- implicit reference of the object.
867 Set_Referenced
(Defining_Identifier
(Obj_Decl
));
869 Check_References
(Stm_Entity
);
871 -- Check RM 6.5 (5.9/3)
874 if Ada_Version
< Ada_2012
then
876 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
877 -- Can it really happen (extended return???)
880 ("aliased only allowed for limited"
881 & " return objects in Ada 2012?", N
);
883 elsif not Is_Limited_View
(R_Type
) then
884 Error_Msg_N
("aliased only allowed for limited"
885 & " return objects", N
);
891 -- Case of Expr present
895 -- Defend against previous errors
897 and then Nkind
(Expr
) /= N_Empty
898 and then Present
(Etype
(Expr
))
900 -- Apply constraint check. Note that this is done before the implicit
901 -- conversion of the expression done for anonymous access types to
902 -- ensure correct generation of the null-excluding check associated
903 -- with null-excluding expressions found in return statements.
905 Apply_Constraint_Check
(Expr
, R_Type
);
907 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
908 -- type, apply an implicit conversion of the expression to that type
909 -- to force appropriate static and run-time accessibility checks.
911 if Ada_Version
>= Ada_2005
912 and then Ekind
(R_Type
) = E_Anonymous_Access_Type
914 Rewrite
(Expr
, Convert_To
(R_Type
, Relocate_Node
(Expr
)));
915 Analyze_And_Resolve
(Expr
, R_Type
);
917 -- If this is a local anonymous access to subprogram, the
918 -- accessibility check can be applied statically. The return is
919 -- illegal if the access type of the return expression is declared
920 -- inside of the subprogram (except if it is the subtype indication
921 -- of an extended return statement).
923 elsif Ekind
(R_Type
) = E_Anonymous_Access_Subprogram_Type
then
924 if not Comes_From_Source
(Current_Scope
)
925 or else Ekind
(Current_Scope
) = E_Return_Statement
930 Scope_Depth
(Scope
(Etype
(Expr
))) >= Scope_Depth
(Scope_Id
)
932 Error_Msg_N
("cannot return local access to subprogram", N
);
936 -- If the result type is class-wide, then check that the return
937 -- expression's type is not declared at a deeper level than the
938 -- function (RM05-6.5(5.6/2)).
940 if Ada_Version
>= Ada_2005
941 and then Is_Class_Wide_Type
(R_Type
)
943 if Type_Access_Level
(Etype
(Expr
)) >
944 Subprogram_Access_Level
(Scope_Id
)
947 ("level of return expression type is deeper than " &
948 "class-wide function!", Expr
);
952 -- Check incorrect use of dynamically tagged expression
954 if Is_Tagged_Type
(R_Type
) then
955 Check_Dynamically_Tagged_Expression
961 -- ??? A real run-time accessibility check is needed in cases
962 -- involving dereferences of access parameters. For now we just
963 -- check the static cases.
965 if (Ada_Version
< Ada_2005
or else Debug_Flag_Dot_L
)
966 and then Is_Limited_View
(Etype
(Scope_Id
))
967 and then Object_Access_Level
(Expr
) >
968 Subprogram_Access_Level
(Scope_Id
)
970 -- Suppress the message in a generic, where the rewriting
973 if Inside_A_Generic
then
978 Make_Raise_Program_Error
(Loc
,
979 Reason
=> PE_Accessibility_Check_Failed
));
983 ("cannot return a local value by reference??", N
);
985 ("\& will be raised at run time??",
986 N
, Standard_Program_Error
);
991 and then Nkind
(Parent
(Scope_Id
)) = N_Function_Specification
992 and then Null_Exclusion_Present
(Parent
(Scope_Id
))
994 Apply_Compile_Time_Constraint_Error
996 Msg
=> "(Ada 2005) null not allowed for "
997 & "null-excluding return??",
998 Reason
=> CE_Null_Not_Allowed
);
1001 end Analyze_Function_Return
;
1003 -------------------------------------
1004 -- Analyze_Generic_Subprogram_Body --
1005 -------------------------------------
1007 procedure Analyze_Generic_Subprogram_Body
1011 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Id
);
1012 Kind
: constant Entity_Kind
:= Ekind
(Gen_Id
);
1013 Body_Id
: Entity_Id
;
1018 -- Copy body and disable expansion while analyzing the generic For a
1019 -- stub, do not copy the stub (which would load the proper body), this
1020 -- will be done when the proper body is analyzed.
1022 if Nkind
(N
) /= N_Subprogram_Body_Stub
then
1023 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
1028 Spec
:= Specification
(N
);
1030 -- Within the body of the generic, the subprogram is callable, and
1031 -- behaves like the corresponding non-generic unit.
1033 Body_Id
:= Defining_Entity
(Spec
);
1035 if Kind
= E_Generic_Procedure
1036 and then Nkind
(Spec
) /= N_Procedure_Specification
1038 Error_Msg_N
("invalid body for generic procedure ", Body_Id
);
1041 elsif Kind
= E_Generic_Function
1042 and then Nkind
(Spec
) /= N_Function_Specification
1044 Error_Msg_N
("invalid body for generic function ", Body_Id
);
1048 Set_Corresponding_Body
(Gen_Decl
, Body_Id
);
1050 if Has_Completion
(Gen_Id
)
1051 and then Nkind
(Parent
(N
)) /= N_Subunit
1053 Error_Msg_N
("duplicate generic body", N
);
1056 Set_Has_Completion
(Gen_Id
);
1059 if Nkind
(N
) = N_Subprogram_Body_Stub
then
1060 Set_Ekind
(Defining_Entity
(Specification
(N
)), Kind
);
1062 Set_Corresponding_Spec
(N
, Gen_Id
);
1065 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
1066 Set_Cunit_Entity
(Current_Sem_Unit
, Defining_Entity
(N
));
1069 -- Make generic parameters immediately visible in the body. They are
1070 -- needed to process the formals declarations. Then make the formals
1071 -- visible in a separate step.
1073 Push_Scope
(Gen_Id
);
1077 First_Ent
: Entity_Id
;
1080 First_Ent
:= First_Entity
(Gen_Id
);
1083 while Present
(E
) and then not Is_Formal
(E
) loop
1088 Set_Use
(Generic_Formal_Declarations
(Gen_Decl
));
1090 -- Now generic formals are visible, and the specification can be
1091 -- analyzed, for subsequent conformance check.
1093 Body_Id
:= Analyze_Subprogram_Specification
(Spec
);
1095 -- Make formal parameters visible
1099 -- E is the first formal parameter, we loop through the formals
1100 -- installing them so that they will be visible.
1102 Set_First_Entity
(Gen_Id
, E
);
1103 while Present
(E
) loop
1109 -- Visible generic entity is callable within its own body
1111 Set_Ekind
(Gen_Id
, Ekind
(Body_Id
));
1112 Set_Contract
(Body_Id
, Make_Contract
(Sloc
(Body_Id
)));
1113 Set_Ekind
(Body_Id
, E_Subprogram_Body
);
1114 Set_Convention
(Body_Id
, Convention
(Gen_Id
));
1115 Set_Is_Obsolescent
(Body_Id
, Is_Obsolescent
(Gen_Id
));
1116 Set_Scope
(Body_Id
, Scope
(Gen_Id
));
1117 Check_Fully_Conformant
(Body_Id
, Gen_Id
, Body_Id
);
1119 if Nkind
(N
) = N_Subprogram_Body_Stub
then
1121 -- No body to analyze, so restore state of generic unit
1123 Set_Ekind
(Gen_Id
, Kind
);
1124 Set_Ekind
(Body_Id
, Kind
);
1126 if Present
(First_Ent
) then
1127 Set_First_Entity
(Gen_Id
, First_Ent
);
1134 -- If this is a compilation unit, it must be made visible explicitly,
1135 -- because the compilation of the declaration, unlike other library
1136 -- unit declarations, does not. If it is not a unit, the following
1137 -- is redundant but harmless.
1139 Set_Is_Immediately_Visible
(Gen_Id
);
1140 Reference_Body_Formals
(Gen_Id
, Body_Id
);
1142 if Is_Child_Unit
(Gen_Id
) then
1143 Generate_Reference
(Gen_Id
, Scope
(Gen_Id
), 'k', False);
1146 Set_Actual_Subtypes
(N
, Current_Scope
);
1148 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
1149 -- invariants and predicates associated with the body and its spec.
1150 -- Note that this is not pure expansion as Expand_Subprogram_Contract
1151 -- prepares the contract assertions for generic subprograms or for
1152 -- ASIS. Do not generate contract checks in SPARK mode.
1154 if not SPARK_Mode
then
1155 Expand_Subprogram_Contract
(N
, Gen_Id
, Body_Id
);
1158 -- If the generic unit carries pre- or post-conditions, copy them
1159 -- to the original generic tree, so that they are properly added
1160 -- to any instantiation.
1163 Orig
: constant Node_Id
:= Original_Node
(N
);
1167 Cond
:= First
(Declarations
(N
));
1168 while Present
(Cond
) loop
1169 if Nkind
(Cond
) = N_Pragma
1170 and then Pragma_Name
(Cond
) = Name_Check
1172 Prepend
(New_Copy_Tree
(Cond
), Declarations
(Orig
));
1174 elsif Nkind
(Cond
) = N_Pragma
1175 and then Pragma_Name
(Cond
) = Name_Postcondition
1177 Set_Ekind
(Defining_Entity
(Orig
), Ekind
(Gen_Id
));
1178 Prepend
(New_Copy_Tree
(Cond
), Declarations
(Orig
));
1187 Analyze_Declarations
(Declarations
(N
));
1189 Analyze
(Handled_Statement_Sequence
(N
));
1191 Save_Global_References
(Original_Node
(N
));
1193 -- Prior to exiting the scope, include generic formals again (if any
1194 -- are present) in the set of local entities.
1196 if Present
(First_Ent
) then
1197 Set_First_Entity
(Gen_Id
, First_Ent
);
1200 Check_References
(Gen_Id
);
1203 Process_End_Label
(Handled_Statement_Sequence
(N
), 't', Current_Scope
);
1205 Check_Subprogram_Order
(N
);
1207 -- Outside of its body, unit is generic again
1209 Set_Ekind
(Gen_Id
, Kind
);
1210 Generate_Reference
(Gen_Id
, Body_Id
, 'b', Set_Ref
=> False);
1213 Style
.Check_Identifier
(Body_Id
, Gen_Id
);
1217 end Analyze_Generic_Subprogram_Body
;
1219 ----------------------------
1220 -- Analyze_Null_Procedure --
1221 ----------------------------
1223 procedure Analyze_Null_Procedure
1225 Is_Completion
: out Boolean)
1227 Loc
: constant Source_Ptr
:= Sloc
(N
);
1228 Spec
: constant Node_Id
:= Specification
(N
);
1229 Designator
: Entity_Id
;
1231 Null_Body
: Node_Id
:= Empty
;
1235 -- Capture the profile of the null procedure before analysis, for
1236 -- expansion at the freeze point and at each point of call. The body is
1237 -- used if the procedure has preconditions, or if it is a completion. In
1238 -- the first case the body is analyzed at the freeze point, in the other
1239 -- it replaces the null procedure declaration.
1242 Make_Subprogram_Body
(Loc
,
1243 Specification
=> New_Copy_Tree
(Spec
),
1244 Declarations
=> New_List
,
1245 Handled_Statement_Sequence
=>
1246 Make_Handled_Sequence_Of_Statements
(Loc
,
1247 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
1249 -- Create new entities for body and formals
1251 Set_Defining_Unit_Name
(Specification
(Null_Body
),
1252 Make_Defining_Identifier
(Loc
, Chars
(Defining_Entity
(N
))));
1254 Form
:= First
(Parameter_Specifications
(Specification
(Null_Body
)));
1255 while Present
(Form
) loop
1256 Set_Defining_Identifier
(Form
,
1257 Make_Defining_Identifier
(Loc
, Chars
(Defining_Identifier
(Form
))));
1261 -- Determine whether the null procedure may be a completion of a generic
1262 -- suprogram, in which case we use the new null body as the completion
1263 -- and set minimal semantic information on the original declaration,
1264 -- which is rewritten as a null statement.
1266 Prev
:= Current_Entity_In_Scope
(Defining_Entity
(Spec
));
1268 if Present
(Prev
) and then Is_Generic_Subprogram
(Prev
) then
1269 Insert_Before
(N
, Null_Body
);
1270 Set_Ekind
(Defining_Entity
(N
), Ekind
(Prev
));
1271 Set_Contract
(Defining_Entity
(N
), Make_Contract
(Loc
));
1273 Rewrite
(N
, Make_Null_Statement
(Loc
));
1274 Analyze_Generic_Subprogram_Body
(Null_Body
, Prev
);
1275 Is_Completion
:= True;
1280 -- Resolve the types of the formals now, because the freeze point
1281 -- may appear in a different context, e.g. an instantiation.
1283 Form
:= First
(Parameter_Specifications
(Specification
(Null_Body
)));
1284 while Present
(Form
) loop
1285 if Nkind
(Parameter_Type
(Form
)) /= N_Access_Definition
then
1286 Find_Type
(Parameter_Type
(Form
));
1289 No
(Access_To_Subprogram_Definition
(Parameter_Type
(Form
)))
1291 Find_Type
(Subtype_Mark
(Parameter_Type
(Form
)));
1294 -- The case of a null procedure with a formal that is an
1295 -- access_to_subprogram type, and that is used as an actual
1296 -- in an instantiation is left to the enthusiastic reader.
1305 -- If there are previous overloadable entities with the same name,
1306 -- check whether any of them is completed by the null procedure.
1308 if Present
(Prev
) and then Is_Overloadable
(Prev
) then
1309 Designator
:= Analyze_Subprogram_Specification
(Spec
);
1310 Prev
:= Find_Corresponding_Spec
(N
);
1313 if No
(Prev
) or else not Comes_From_Source
(Prev
) then
1314 Designator
:= Analyze_Subprogram_Specification
(Spec
);
1315 Set_Has_Completion
(Designator
);
1317 -- Signal to caller that this is a procedure declaration
1319 Is_Completion
:= False;
1321 -- Null procedures are always inlined, but generic formal subprograms
1322 -- which appear as such in the internal instance of formal packages,
1323 -- need no completion and are not marked Inline.
1326 and then Nkind
(N
) /= N_Formal_Concrete_Subprogram_Declaration
1328 Set_Corresponding_Body
(N
, Defining_Entity
(Null_Body
));
1329 Set_Body_To_Inline
(N
, Null_Body
);
1330 Set_Is_Inlined
(Designator
);
1334 -- The null procedure is a completion
1336 Is_Completion
:= True;
1338 if Expander_Active
then
1339 Rewrite
(N
, Null_Body
);
1343 Designator
:= Analyze_Subprogram_Specification
(Spec
);
1344 Set_Has_Completion
(Designator
);
1345 Set_Has_Completion
(Prev
);
1348 end Analyze_Null_Procedure
;
1350 -----------------------------
1351 -- Analyze_Operator_Symbol --
1352 -----------------------------
1354 -- An operator symbol such as "+" or "and" may appear in context where the
1355 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1356 -- is just a string, as in (conjunction = "or"). In these cases the parser
1357 -- generates this node, and the semantics does the disambiguation. Other
1358 -- such case are actuals in an instantiation, the generic unit in an
1359 -- instantiation, and pragma arguments.
1361 procedure Analyze_Operator_Symbol
(N
: Node_Id
) is
1362 Par
: constant Node_Id
:= Parent
(N
);
1365 if (Nkind
(Par
) = N_Function_Call
1366 and then N
= Name
(Par
))
1367 or else Nkind
(Par
) = N_Function_Instantiation
1368 or else (Nkind
(Par
) = N_Indexed_Component
1369 and then N
= Prefix
(Par
))
1370 or else (Nkind
(Par
) = N_Pragma_Argument_Association
1371 and then not Is_Pragma_String_Literal
(Par
))
1372 or else Nkind
(Par
) = N_Subprogram_Renaming_Declaration
1373 or else (Nkind
(Par
) = N_Attribute_Reference
1374 and then Attribute_Name
(Par
) /= Name_Value
)
1376 Find_Direct_Name
(N
);
1379 Change_Operator_Symbol_To_String_Literal
(N
);
1382 end Analyze_Operator_Symbol
;
1384 -----------------------------------
1385 -- Analyze_Parameter_Association --
1386 -----------------------------------
1388 procedure Analyze_Parameter_Association
(N
: Node_Id
) is
1390 Analyze
(Explicit_Actual_Parameter
(N
));
1391 end Analyze_Parameter_Association
;
1393 ----------------------------
1394 -- Analyze_Procedure_Call --
1395 ----------------------------
1397 procedure Analyze_Procedure_Call
(N
: Node_Id
) is
1398 Loc
: constant Source_Ptr
:= Sloc
(N
);
1399 P
: constant Node_Id
:= Name
(N
);
1400 Actuals
: constant List_Id
:= Parameter_Associations
(N
);
1404 procedure Analyze_Call_And_Resolve
;
1405 -- Do Analyze and Resolve calls for procedure call
1406 -- At end, check illegal order dependence.
1408 ------------------------------
1409 -- Analyze_Call_And_Resolve --
1410 ------------------------------
1412 procedure Analyze_Call_And_Resolve
is
1414 if Nkind
(N
) = N_Procedure_Call_Statement
then
1416 Resolve
(N
, Standard_Void_Type
);
1420 end Analyze_Call_And_Resolve
;
1422 -- Start of processing for Analyze_Procedure_Call
1425 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1426 -- a procedure call or an entry call. The prefix may denote an access
1427 -- to subprogram type, in which case an implicit dereference applies.
1428 -- If the prefix is an indexed component (without implicit dereference)
1429 -- then the construct denotes a call to a member of an entire family.
1430 -- If the prefix is a simple name, it may still denote a call to a
1431 -- parameterless member of an entry family. Resolution of these various
1432 -- interpretations is delicate.
1436 -- If this is a call of the form Obj.Op, the call may have been
1437 -- analyzed and possibly rewritten into a block, in which case
1440 if Analyzed
(N
) then
1444 -- If there is an error analyzing the name (which may have been
1445 -- rewritten if the original call was in prefix notation) then error
1446 -- has been emitted already, mark node and return.
1448 if Error_Posted
(N
) or else Etype
(Name
(N
)) = Any_Type
then
1449 Set_Etype
(N
, Any_Type
);
1453 -- Otherwise analyze the parameters
1455 if Present
(Actuals
) then
1456 Actual
:= First
(Actuals
);
1458 while Present
(Actual
) loop
1460 Check_Parameterless_Call
(Actual
);
1465 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1467 if Nkind
(P
) = N_Attribute_Reference
1468 and then Nam_In
(Attribute_Name
(P
), Name_Elab_Spec
,
1470 Name_Elab_Subp_Body
)
1472 if Present
(Actuals
) then
1474 ("no parameters allowed for this call", First
(Actuals
));
1478 Set_Etype
(N
, Standard_Void_Type
);
1481 elsif Is_Entity_Name
(P
)
1482 and then Is_Record_Type
(Etype
(Entity
(P
)))
1483 and then Remote_AST_I_Dereference
(P
)
1487 elsif Is_Entity_Name
(P
)
1488 and then Ekind
(Entity
(P
)) /= E_Entry_Family
1490 if Is_Access_Type
(Etype
(P
))
1491 and then Ekind
(Designated_Type
(Etype
(P
))) = E_Subprogram_Type
1492 and then No
(Actuals
)
1493 and then Comes_From_Source
(N
)
1495 Error_Msg_N
("missing explicit dereference in call", N
);
1498 Analyze_Call_And_Resolve
;
1500 -- If the prefix is the simple name of an entry family, this is
1501 -- a parameterless call from within the task body itself.
1503 elsif Is_Entity_Name
(P
)
1504 and then Nkind
(P
) = N_Identifier
1505 and then Ekind
(Entity
(P
)) = E_Entry_Family
1506 and then Present
(Actuals
)
1507 and then No
(Next
(First
(Actuals
)))
1509 -- Can be call to parameterless entry family. What appears to be the
1510 -- sole argument is in fact the entry index. Rewrite prefix of node
1511 -- accordingly. Source representation is unchanged by this
1515 Make_Indexed_Component
(Loc
,
1517 Make_Selected_Component
(Loc
,
1518 Prefix
=> New_Occurrence_Of
(Scope
(Entity
(P
)), Loc
),
1519 Selector_Name
=> New_Occurrence_Of
(Entity
(P
), Loc
)),
1520 Expressions
=> Actuals
);
1521 Set_Name
(N
, New_N
);
1522 Set_Etype
(New_N
, Standard_Void_Type
);
1523 Set_Parameter_Associations
(N
, No_List
);
1524 Analyze_Call_And_Resolve
;
1526 elsif Nkind
(P
) = N_Explicit_Dereference
then
1527 if Ekind
(Etype
(P
)) = E_Subprogram_Type
then
1528 Analyze_Call_And_Resolve
;
1530 Error_Msg_N
("expect access to procedure in call", P
);
1533 -- The name can be a selected component or an indexed component that
1534 -- yields an access to subprogram. Such a prefix is legal if the call
1535 -- has parameter associations.
1537 elsif Is_Access_Type
(Etype
(P
))
1538 and then Ekind
(Designated_Type
(Etype
(P
))) = E_Subprogram_Type
1540 if Present
(Actuals
) then
1541 Analyze_Call_And_Resolve
;
1543 Error_Msg_N
("missing explicit dereference in call ", N
);
1546 -- If not an access to subprogram, then the prefix must resolve to the
1547 -- name of an entry, entry family, or protected operation.
1549 -- For the case of a simple entry call, P is a selected component where
1550 -- the prefix is the task and the selector name is the entry. A call to
1551 -- a protected procedure will have the same syntax. If the protected
1552 -- object contains overloaded operations, the entity may appear as a
1553 -- function, the context will select the operation whose type is Void.
1555 elsif Nkind
(P
) = N_Selected_Component
1556 and then Ekind_In
(Entity
(Selector_Name
(P
)), E_Entry
,
1560 Analyze_Call_And_Resolve
;
1562 elsif Nkind
(P
) = N_Selected_Component
1563 and then Ekind
(Entity
(Selector_Name
(P
))) = E_Entry_Family
1564 and then Present
(Actuals
)
1565 and then No
(Next
(First
(Actuals
)))
1567 -- Can be call to parameterless entry family. What appears to be the
1568 -- sole argument is in fact the entry index. Rewrite prefix of node
1569 -- accordingly. Source representation is unchanged by this
1573 Make_Indexed_Component
(Loc
,
1574 Prefix
=> New_Copy
(P
),
1575 Expressions
=> Actuals
);
1576 Set_Name
(N
, New_N
);
1577 Set_Etype
(New_N
, Standard_Void_Type
);
1578 Set_Parameter_Associations
(N
, No_List
);
1579 Analyze_Call_And_Resolve
;
1581 -- For the case of a reference to an element of an entry family, P is
1582 -- an indexed component whose prefix is a selected component (task and
1583 -- entry family), and whose index is the entry family index.
1585 elsif Nkind
(P
) = N_Indexed_Component
1586 and then Nkind
(Prefix
(P
)) = N_Selected_Component
1587 and then Ekind
(Entity
(Selector_Name
(Prefix
(P
)))) = E_Entry_Family
1589 Analyze_Call_And_Resolve
;
1591 -- If the prefix is the name of an entry family, it is a call from
1592 -- within the task body itself.
1594 elsif Nkind
(P
) = N_Indexed_Component
1595 and then Nkind
(Prefix
(P
)) = N_Identifier
1596 and then Ekind
(Entity
(Prefix
(P
))) = E_Entry_Family
1599 Make_Selected_Component
(Loc
,
1600 Prefix
=> New_Occurrence_Of
(Scope
(Entity
(Prefix
(P
))), Loc
),
1601 Selector_Name
=> New_Occurrence_Of
(Entity
(Prefix
(P
)), Loc
));
1602 Rewrite
(Prefix
(P
), New_N
);
1604 Analyze_Call_And_Resolve
;
1606 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1607 -- procedure name, so the construct can only be a qualified expression.
1609 elsif Nkind
(P
) = N_Qualified_Expression
1610 and then Ada_Version
>= Ada_2012
1612 Rewrite
(N
, Make_Code_Statement
(Loc
, Expression
=> P
));
1615 -- Anything else is an error
1618 Error_Msg_N
("invalid procedure or entry call", N
);
1620 end Analyze_Procedure_Call
;
1622 ------------------------------
1623 -- Analyze_Return_Statement --
1624 ------------------------------
1626 procedure Analyze_Return_Statement
(N
: Node_Id
) is
1628 pragma Assert
(Nkind_In
(N
, N_Simple_Return_Statement
,
1629 N_Extended_Return_Statement
));
1631 Returns_Object
: constant Boolean :=
1632 Nkind
(N
) = N_Extended_Return_Statement
1634 (Nkind
(N
) = N_Simple_Return_Statement
1635 and then Present
(Expression
(N
)));
1636 -- True if we're returning something; that is, "return <expression>;"
1637 -- or "return Result : T [:= ...]". False for "return;". Used for error
1638 -- checking: If Returns_Object is True, N should apply to a function
1639 -- body; otherwise N should apply to a procedure body, entry body,
1640 -- accept statement, or extended return statement.
1642 function Find_What_It_Applies_To
return Entity_Id
;
1643 -- Find the entity representing the innermost enclosing body, accept
1644 -- statement, or extended return statement. If the result is a callable
1645 -- construct or extended return statement, then this will be the value
1646 -- of the Return_Applies_To attribute. Otherwise, the program is
1647 -- illegal. See RM-6.5(4/2).
1649 -----------------------------
1650 -- Find_What_It_Applies_To --
1651 -----------------------------
1653 function Find_What_It_Applies_To
return Entity_Id
is
1654 Result
: Entity_Id
:= Empty
;
1657 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1658 -- and postconditions.
1660 for J
in reverse 0 .. Scope_Stack
.Last
loop
1661 Result
:= Scope_Stack
.Table
(J
).Entity
;
1662 exit when not Ekind_In
(Result
, E_Block
, E_Loop
)
1663 and then Chars
(Result
) /= Name_uPostconditions
;
1666 pragma Assert
(Present
(Result
));
1668 end Find_What_It_Applies_To
;
1670 -- Local declarations
1672 Scope_Id
: constant Entity_Id
:= Find_What_It_Applies_To
;
1673 Kind
: constant Entity_Kind
:= Ekind
(Scope_Id
);
1674 Loc
: constant Source_Ptr
:= Sloc
(N
);
1675 Stm_Entity
: constant Entity_Id
:=
1677 (E_Return_Statement
, Current_Scope
, Loc
, 'R');
1679 -- Start of processing for Analyze_Return_Statement
1682 Set_Return_Statement_Entity
(N
, Stm_Entity
);
1684 Set_Etype
(Stm_Entity
, Standard_Void_Type
);
1685 Set_Return_Applies_To
(Stm_Entity
, Scope_Id
);
1687 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1688 -- (4/2): an inner return statement will apply to this extended return.
1690 if Nkind
(N
) = N_Extended_Return_Statement
then
1691 Push_Scope
(Stm_Entity
);
1694 -- Check that pragma No_Return is obeyed. Don't complain about the
1695 -- implicitly-generated return that is placed at the end.
1697 if No_Return
(Scope_Id
) and then Comes_From_Source
(N
) then
1698 Error_Msg_N
("RETURN statement not allowed (No_Return)", N
);
1701 -- Warn on any unassigned OUT parameters if in procedure
1703 if Ekind
(Scope_Id
) = E_Procedure
then
1704 Warn_On_Unassigned_Out_Parameter
(N
, Scope_Id
);
1707 -- Check that functions return objects, and other things do not
1709 if Kind
= E_Function
or else Kind
= E_Generic_Function
then
1710 if not Returns_Object
then
1711 Error_Msg_N
("missing expression in return from function", N
);
1714 elsif Kind
= E_Procedure
or else Kind
= E_Generic_Procedure
then
1715 if Returns_Object
then
1716 Error_Msg_N
("procedure cannot return value (use function)", N
);
1719 elsif Kind
= E_Entry
or else Kind
= E_Entry_Family
then
1720 if Returns_Object
then
1721 if Is_Protected_Type
(Scope
(Scope_Id
)) then
1722 Error_Msg_N
("entry body cannot return value", N
);
1724 Error_Msg_N
("accept statement cannot return value", N
);
1728 elsif Kind
= E_Return_Statement
then
1730 -- We are nested within another return statement, which must be an
1731 -- extended_return_statement.
1733 if Returns_Object
then
1734 if Nkind
(N
) = N_Extended_Return_Statement
then
1736 ("extended return statement cannot be nested (use `RETURN;`)",
1739 -- Case of a simple return statement with a value inside extended
1740 -- return statement.
1744 ("return nested in extended return statement cannot return " &
1745 "value (use `RETURN;`)", N
);
1750 Error_Msg_N
("illegal context for return statement", N
);
1753 if Ekind_In
(Kind
, E_Function
, E_Generic_Function
) then
1754 Analyze_Function_Return
(N
);
1756 elsif Ekind_In
(Kind
, E_Procedure
, E_Generic_Procedure
) then
1757 Set_Return_Present
(Scope_Id
);
1760 if Nkind
(N
) = N_Extended_Return_Statement
then
1764 Kill_Current_Values
(Last_Assignment_Only
=> True);
1765 Check_Unreachable_Code
(N
);
1767 Analyze_Dimension
(N
);
1768 end Analyze_Return_Statement
;
1770 -------------------------------------
1771 -- Analyze_Simple_Return_Statement --
1772 -------------------------------------
1774 procedure Analyze_Simple_Return_Statement
(N
: Node_Id
) is
1776 if Present
(Expression
(N
)) then
1777 Mark_Coextensions
(N
, Expression
(N
));
1780 Analyze_Return_Statement
(N
);
1781 end Analyze_Simple_Return_Statement
;
1783 -------------------------
1784 -- Analyze_Return_Type --
1785 -------------------------
1787 procedure Analyze_Return_Type
(N
: Node_Id
) is
1788 Designator
: constant Entity_Id
:= Defining_Entity
(N
);
1789 Typ
: Entity_Id
:= Empty
;
1792 -- Normal case where result definition does not indicate an error
1794 if Result_Definition
(N
) /= Error
then
1795 if Nkind
(Result_Definition
(N
)) = N_Access_Definition
then
1796 Check_SPARK_Restriction
1797 ("access result is not allowed", Result_Definition
(N
));
1799 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1802 AD
: constant Node_Id
:=
1803 Access_To_Subprogram_Definition
(Result_Definition
(N
));
1805 if Present
(AD
) and then Protected_Present
(AD
) then
1806 Typ
:= Replace_Anonymous_Access_To_Protected_Subprogram
(N
);
1808 Typ
:= Access_Definition
(N
, Result_Definition
(N
));
1812 Set_Parent
(Typ
, Result_Definition
(N
));
1813 Set_Is_Local_Anonymous_Access
(Typ
);
1814 Set_Etype
(Designator
, Typ
);
1816 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1818 Null_Exclusion_Static_Checks
(N
);
1820 -- Subtype_Mark case
1823 Find_Type
(Result_Definition
(N
));
1824 Typ
:= Entity
(Result_Definition
(N
));
1825 Set_Etype
(Designator
, Typ
);
1827 -- Unconstrained array as result is not allowed in SPARK
1829 if Is_Array_Type
(Typ
) and then not Is_Constrained
(Typ
) then
1830 Check_SPARK_Restriction
1831 ("returning an unconstrained array is not allowed",
1832 Result_Definition
(N
));
1835 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1837 Null_Exclusion_Static_Checks
(N
);
1839 -- If a null exclusion is imposed on the result type, then create
1840 -- a null-excluding itype (an access subtype) and use it as the
1841 -- function's Etype. Note that the null exclusion checks are done
1842 -- right before this, because they don't get applied to types that
1843 -- do not come from source.
1845 if Is_Access_Type
(Typ
) and then Null_Exclusion_Present
(N
) then
1846 Set_Etype
(Designator
,
1847 Create_Null_Excluding_Itype
1850 Scope_Id
=> Scope
(Current_Scope
)));
1852 -- The new subtype must be elaborated before use because
1853 -- it is visible outside of the function. However its base
1854 -- type may not be frozen yet, so the reference that will
1855 -- force elaboration must be attached to the freezing of
1858 -- If the return specification appears on a proper body,
1859 -- the subtype will have been created already on the spec.
1861 if Is_Frozen
(Typ
) then
1862 if Nkind
(Parent
(N
)) = N_Subprogram_Body
1863 and then Nkind
(Parent
(Parent
(N
))) = N_Subunit
1867 Build_Itype_Reference
(Etype
(Designator
), Parent
(N
));
1871 Ensure_Freeze_Node
(Typ
);
1874 IR
: constant Node_Id
:= Make_Itype_Reference
(Sloc
(N
));
1876 Set_Itype
(IR
, Etype
(Designator
));
1877 Append_Freeze_Actions
(Typ
, New_List
(IR
));
1882 Set_Etype
(Designator
, Typ
);
1885 if Ekind
(Typ
) = E_Incomplete_Type
1886 and then Is_Value_Type
(Typ
)
1890 elsif Ekind
(Typ
) = E_Incomplete_Type
1891 or else (Is_Class_Wide_Type
(Typ
)
1892 and then Ekind
(Root_Type
(Typ
)) = E_Incomplete_Type
)
1894 -- AI05-0151: Tagged incomplete types are allowed in all formal
1895 -- parts. Untagged incomplete types are not allowed in bodies.
1897 if Ada_Version
>= Ada_2012
then
1898 if Is_Tagged_Type
(Typ
) then
1901 elsif Nkind_In
(Parent
(Parent
(N
)),
1907 ("invalid use of untagged incomplete type&",
1911 -- The type must be completed in the current package. This
1912 -- is checked at the end of the package declaraton, when
1913 -- Taft-amendment types are identified. If the return type
1914 -- is class-wide, there is no required check, the type can
1915 -- be a bona fide TAT.
1917 if Ekind
(Scope
(Current_Scope
)) = E_Package
1918 and then In_Private_Part
(Scope
(Current_Scope
))
1919 and then not Is_Class_Wide_Type
(Typ
)
1921 Append_Elmt
(Designator
, Private_Dependents
(Typ
));
1926 ("invalid use of incomplete type&", Designator
, Typ
);
1931 -- Case where result definition does indicate an error
1934 Set_Etype
(Designator
, Any_Type
);
1936 end Analyze_Return_Type
;
1938 -----------------------------
1939 -- Analyze_Subprogram_Body --
1940 -----------------------------
1942 procedure Analyze_Subprogram_Body
(N
: Node_Id
) is
1943 Loc
: constant Source_Ptr
:= Sloc
(N
);
1944 Body_Spec
: constant Node_Id
:= Specification
(N
);
1945 Body_Id
: constant Entity_Id
:= Defining_Entity
(Body_Spec
);
1948 if Debug_Flag_C
then
1949 Write_Str
("==> subprogram body ");
1950 Write_Name
(Chars
(Body_Id
));
1951 Write_Str
(" from ");
1952 Write_Location
(Loc
);
1957 Trace_Scope
(N
, Body_Id
, " Analyze subprogram: ");
1959 -- The real work is split out into the helper, so it can do "return;"
1960 -- without skipping the debug output:
1962 Analyze_Subprogram_Body_Helper
(N
);
1964 if Debug_Flag_C
then
1966 Write_Str
("<== subprogram body ");
1967 Write_Name
(Chars
(Body_Id
));
1968 Write_Str
(" from ");
1969 Write_Location
(Loc
);
1972 end Analyze_Subprogram_Body
;
1974 --------------------------------------
1975 -- Analyze_Subprogram_Body_Contract --
1976 --------------------------------------
1978 procedure Analyze_Subprogram_Body_Contract
(Body_Id
: Entity_Id
) is
1979 Body_Decl
: constant Node_Id
:= Parent
(Parent
(Body_Id
));
1980 Spec_Id
: constant Entity_Id
:= Corresponding_Spec
(Body_Decl
);
1982 Ref_Depends
: Node_Id
:= Empty
;
1983 Ref_Global
: Node_Id
:= Empty
;
1986 -- When a subprogram body declaration is erroneous, its defining entity
1987 -- is left unanalyzed. There is nothing left to do in this case because
1988 -- the body lacks a contract.
1990 if not Analyzed
(Body_Id
) then
1994 -- Locate and store pragmas Refined_Depends and Refined_Global since
1995 -- their order of analysis matters.
1997 Prag
:= Classifications
(Contract
(Body_Id
));
1998 while Present
(Prag
) loop
1999 if Pragma_Name
(Prag
) = Name_Refined_Depends
then
2000 Ref_Depends
:= Prag
;
2001 elsif Pragma_Name
(Prag
) = Name_Refined_Global
then
2005 Prag
:= Next_Pragma
(Prag
);
2008 -- Analyze Refined_Global first as Refined_Depends may mention items
2009 -- classified in the global refinement.
2011 if Present
(Ref_Global
) then
2012 Analyze_Refined_Global_In_Decl_Part
(Ref_Global
);
2014 -- When the corresponding Global aspect/pragma references a state with
2015 -- visible refinement, the body requires Refined_Global.
2017 elsif Present
(Spec_Id
) then
2018 Prag
:= Get_Pragma
(Spec_Id
, Pragma_Global
);
2020 if Present
(Prag
) and then Contains_Refined_State
(Prag
) then
2022 ("body of subprogram & requires global refinement",
2023 Body_Decl
, Spec_Id
);
2027 -- Refined_Depends must be analyzed after Refined_Global in order to see
2028 -- the modes of all global refinements.
2030 if Present
(Ref_Depends
) then
2031 Analyze_Refined_Depends_In_Decl_Part
(Ref_Depends
);
2033 -- When the corresponding Depends aspect/pragma references a state with
2034 -- visible refinement, the body requires Refined_Depends.
2036 elsif Present
(Spec_Id
) then
2037 Prag
:= Get_Pragma
(Spec_Id
, Pragma_Depends
);
2039 if Present
(Prag
) and then Contains_Refined_State
(Prag
) then
2041 ("body of subprogram & requires dependance refinement",
2042 Body_Decl
, Spec_Id
);
2045 end Analyze_Subprogram_Body_Contract
;
2047 ------------------------------------
2048 -- Analyze_Subprogram_Body_Helper --
2049 ------------------------------------
2051 -- This procedure is called for regular subprogram bodies, generic bodies,
2052 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2053 -- specification matters, and is used to create a proper declaration for
2054 -- the subprogram, or to perform conformance checks.
2056 procedure Analyze_Subprogram_Body_Helper
(N
: Node_Id
) is
2057 Loc
: constant Source_Ptr
:= Sloc
(N
);
2058 Body_Spec
: constant Node_Id
:= Specification
(N
);
2059 Body_Id
: Entity_Id
:= Defining_Entity
(Body_Spec
);
2060 Prev_Id
: constant Entity_Id
:= Current_Entity_In_Scope
(Body_Id
);
2061 Conformant
: Boolean;
2063 Prot_Typ
: Entity_Id
:= Empty
;
2064 Spec_Id
: Entity_Id
;
2065 Spec_Decl
: Node_Id
:= Empty
;
2067 Last_Real_Spec_Entity
: Entity_Id
:= Empty
;
2068 -- When we analyze a separate spec, the entity chain ends up containing
2069 -- the formals, as well as any itypes generated during analysis of the
2070 -- default expressions for parameters, or the arguments of associated
2071 -- precondition/postcondition pragmas (which are analyzed in the context
2072 -- of the spec since they have visibility on formals).
2074 -- These entities belong with the spec and not the body. However we do
2075 -- the analysis of the body in the context of the spec (again to obtain
2076 -- visibility to the formals), and all the entities generated during
2077 -- this analysis end up also chained to the entity chain of the spec.
2078 -- But they really belong to the body, and there is circuitry to move
2079 -- them from the spec to the body.
2081 -- However, when we do this move, we don't want to move the real spec
2082 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2083 -- variable points to the last real spec entity, so we only move those
2084 -- chained beyond that point. It is initialized to Empty to deal with
2085 -- the case where there is no separate spec.
2087 procedure Check_Anonymous_Return
;
2088 -- Ada 2005: if a function returns an access type that denotes a task,
2089 -- or a type that contains tasks, we must create a master entity for
2090 -- the anonymous type, which typically will be used in an allocator
2091 -- in the body of the function.
2093 procedure Check_Inline_Pragma
(Spec
: in out Node_Id
);
2094 -- Look ahead to recognize a pragma that may appear after the body.
2095 -- If there is a previous spec, check that it appears in the same
2096 -- declarative part. If the pragma is Inline_Always, perform inlining
2097 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2098 -- If the body acts as a spec, and inlining is required, we create a
2099 -- subprogram declaration for it, in order to attach the body to inline.
2100 -- If pragma does not appear after the body, check whether there is
2101 -- an inline pragma before any local declarations.
2103 procedure Check_Missing_Return
;
2104 -- Checks for a function with a no return statements, and also performs
2105 -- the warning checks implemented by Check_Returns. In formal mode, also
2106 -- verify that a function ends with a RETURN and that a procedure does
2107 -- not contain any RETURN.
2109 function Disambiguate_Spec
return Entity_Id
;
2110 -- When a primitive is declared between the private view and the full
2111 -- view of a concurrent type which implements an interface, a special
2112 -- mechanism is used to find the corresponding spec of the primitive
2115 procedure Exchange_Limited_Views
(Subp_Id
: Entity_Id
);
2116 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2117 -- incomplete types coming from a limited context and swap their limited
2118 -- views with the non-limited ones.
2120 function Is_Private_Concurrent_Primitive
2121 (Subp_Id
: Entity_Id
) return Boolean;
2122 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2123 -- type that implements an interface and has a private view.
2125 procedure Set_Trivial_Subprogram
(N
: Node_Id
);
2126 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2127 -- subprogram whose body is being analyzed. N is the statement node
2128 -- causing the flag to be set, if the following statement is a return
2129 -- of an entity, we mark the entity as set in source to suppress any
2130 -- warning on the stylized use of function stubs with a dummy return.
2132 procedure Verify_Overriding_Indicator
;
2133 -- If there was a previous spec, the entity has been entered in the
2134 -- current scope previously. If the body itself carries an overriding
2135 -- indicator, check that it is consistent with the known status of the
2138 ----------------------------
2139 -- Check_Anonymous_Return --
2140 ----------------------------
2142 procedure Check_Anonymous_Return
is
2148 if Present
(Spec_Id
) then
2154 if Ekind
(Scop
) = E_Function
2155 and then Ekind
(Etype
(Scop
)) = E_Anonymous_Access_Type
2156 and then not Is_Thunk
(Scop
)
2157 and then (Has_Task
(Designated_Type
(Etype
(Scop
)))
2159 (Is_Class_Wide_Type
(Designated_Type
(Etype
(Scop
)))
2161 Is_Limited_Record
(Designated_Type
(Etype
(Scop
)))))
2162 and then Expander_Active
2164 -- Avoid cases with no tasking support
2166 and then RTE_Available
(RE_Current_Master
)
2167 and then not Restriction_Active
(No_Task_Hierarchy
)
2170 Make_Object_Declaration
(Loc
,
2171 Defining_Identifier
=>
2172 Make_Defining_Identifier
(Loc
, Name_uMaster
),
2173 Constant_Present
=> True,
2174 Object_Definition
=>
2175 New_Reference_To
(RTE
(RE_Master_Id
), Loc
),
2177 Make_Explicit_Dereference
(Loc
,
2178 New_Reference_To
(RTE
(RE_Current_Master
), Loc
)));
2180 if Present
(Declarations
(N
)) then
2181 Prepend
(Decl
, Declarations
(N
));
2183 Set_Declarations
(N
, New_List
(Decl
));
2186 Set_Master_Id
(Etype
(Scop
), Defining_Identifier
(Decl
));
2187 Set_Has_Master_Entity
(Scop
);
2189 -- Now mark the containing scope as a task master
2192 while Nkind
(Par
) /= N_Compilation_Unit
loop
2193 Par
:= Parent
(Par
);
2194 pragma Assert
(Present
(Par
));
2196 -- If we fall off the top, we are at the outer level, and
2197 -- the environment task is our effective master, so nothing
2201 (Par
, N_Task_Body
, N_Block_Statement
, N_Subprogram_Body
)
2203 Set_Is_Task_Master
(Par
, True);
2208 end Check_Anonymous_Return
;
2210 -------------------------
2211 -- Check_Inline_Pragma --
2212 -------------------------
2214 procedure Check_Inline_Pragma
(Spec
: in out Node_Id
) is
2218 function Is_Inline_Pragma
(N
: Node_Id
) return Boolean;
2219 -- True when N is a pragma Inline or Inline_Always that applies
2220 -- to this subprogram.
2222 -----------------------
2223 -- Is_Inline_Pragma --
2224 -----------------------
2226 function Is_Inline_Pragma
(N
: Node_Id
) return Boolean is
2229 Nkind
(N
) = N_Pragma
2231 (Pragma_Name
(N
) = Name_Inline_Always
2234 and then Pragma_Name
(N
) = Name_Inline
))
2237 (Expression
(First
(Pragma_Argument_Associations
(N
)))) =
2239 end Is_Inline_Pragma
;
2241 -- Start of processing for Check_Inline_Pragma
2244 if not Expander_Active
then
2248 if Is_List_Member
(N
)
2249 and then Present
(Next
(N
))
2250 and then Is_Inline_Pragma
(Next
(N
))
2254 elsif Nkind
(N
) /= N_Subprogram_Body_Stub
2255 and then Present
(Declarations
(N
))
2256 and then Is_Inline_Pragma
(First
(Declarations
(N
)))
2258 Prag
:= First
(Declarations
(N
));
2264 if Present
(Prag
) then
2265 if Present
(Spec_Id
) then
2266 if In_Same_List
(N
, Unit_Declaration_Node
(Spec_Id
)) then
2271 -- Create a subprogram declaration, to make treatment uniform
2274 Subp
: constant Entity_Id
:=
2275 Make_Defining_Identifier
(Loc
, Chars
(Body_Id
));
2276 Decl
: constant Node_Id
:=
2277 Make_Subprogram_Declaration
(Loc
,
2279 New_Copy_Tree
(Specification
(N
)));
2282 Set_Defining_Unit_Name
(Specification
(Decl
), Subp
);
2284 if Present
(First_Formal
(Body_Id
)) then
2285 Plist
:= Copy_Parameter_List
(Body_Id
);
2286 Set_Parameter_Specifications
2287 (Specification
(Decl
), Plist
);
2290 Insert_Before
(N
, Decl
);
2293 Set_Has_Pragma_Inline
(Subp
);
2295 if Pragma_Name
(Prag
) = Name_Inline_Always
then
2296 Set_Is_Inlined
(Subp
);
2297 Set_Has_Pragma_Inline_Always
(Subp
);
2304 end Check_Inline_Pragma
;
2306 --------------------------
2307 -- Check_Missing_Return --
2308 --------------------------
2310 procedure Check_Missing_Return
is
2312 Missing_Ret
: Boolean;
2315 if Nkind
(Body_Spec
) = N_Function_Specification
then
2316 if Present
(Spec_Id
) then
2322 if Return_Present
(Id
) then
2323 Check_Returns
(HSS
, 'F', Missing_Ret
);
2326 Set_Has_Missing_Return
(Id
);
2329 elsif Is_Generic_Subprogram
(Id
)
2330 or else not Is_Machine_Code_Subprogram
(Id
)
2332 Error_Msg_N
("missing RETURN statement in function body", N
);
2335 -- If procedure with No_Return, check returns
2337 elsif Nkind
(Body_Spec
) = N_Procedure_Specification
2338 and then Present
(Spec_Id
)
2339 and then No_Return
(Spec_Id
)
2341 Check_Returns
(HSS
, 'P', Missing_Ret
, Spec_Id
);
2344 -- Special checks in SPARK mode
2346 if Nkind
(Body_Spec
) = N_Function_Specification
then
2348 -- In SPARK mode, last statement of a function should be a return
2351 Stat
: constant Node_Id
:= Last_Source_Statement
(HSS
);
2354 and then not Nkind_In
(Stat
, N_Simple_Return_Statement
,
2355 N_Extended_Return_Statement
)
2357 Check_SPARK_Restriction
2358 ("last statement in function should be RETURN", Stat
);
2362 -- In SPARK mode, verify that a procedure has no return
2364 elsif Nkind
(Body_Spec
) = N_Procedure_Specification
then
2365 if Present
(Spec_Id
) then
2371 -- Would be nice to point to return statement here, can we
2372 -- borrow the Check_Returns procedure here ???
2374 if Return_Present
(Id
) then
2375 Check_SPARK_Restriction
2376 ("procedure should not have RETURN", N
);
2379 end Check_Missing_Return
;
2381 -----------------------
2382 -- Disambiguate_Spec --
2383 -----------------------
2385 function Disambiguate_Spec
return Entity_Id
is
2386 Priv_Spec
: Entity_Id
;
2389 procedure Replace_Types
(To_Corresponding
: Boolean);
2390 -- Depending on the flag, replace the type of formal parameters of
2391 -- Body_Id if it is a concurrent type implementing interfaces with
2392 -- the corresponding record type or the other way around.
2394 procedure Replace_Types
(To_Corresponding
: Boolean) is
2396 Formal_Typ
: Entity_Id
;
2399 Formal
:= First_Formal
(Body_Id
);
2400 while Present
(Formal
) loop
2401 Formal_Typ
:= Etype
(Formal
);
2403 if Is_Class_Wide_Type
(Formal_Typ
) then
2404 Formal_Typ
:= Root_Type
(Formal_Typ
);
2407 -- From concurrent type to corresponding record
2409 if To_Corresponding
then
2410 if Is_Concurrent_Type
(Formal_Typ
)
2411 and then Present
(Corresponding_Record_Type
(Formal_Typ
))
2412 and then Present
(Interfaces
(
2413 Corresponding_Record_Type
(Formal_Typ
)))
2416 Corresponding_Record_Type
(Formal_Typ
));
2419 -- From corresponding record to concurrent type
2422 if Is_Concurrent_Record_Type
(Formal_Typ
)
2423 and then Present
(Interfaces
(Formal_Typ
))
2426 Corresponding_Concurrent_Type
(Formal_Typ
));
2430 Next_Formal
(Formal
);
2434 -- Start of processing for Disambiguate_Spec
2437 -- Try to retrieve the specification of the body as is. All error
2438 -- messages are suppressed because the body may not have a spec in
2439 -- its current state.
2441 Spec_N
:= Find_Corresponding_Spec
(N
, False);
2443 -- It is possible that this is the body of a primitive declared
2444 -- between a private and a full view of a concurrent type. The
2445 -- controlling parameter of the spec carries the concurrent type,
2446 -- not the corresponding record type as transformed by Analyze_
2447 -- Subprogram_Specification. In such cases, we undo the change
2448 -- made by the analysis of the specification and try to find the
2451 -- Note that wrappers already have their corresponding specs and
2452 -- bodies set during their creation, so if the candidate spec is
2453 -- a wrapper, then we definitely need to swap all types to their
2454 -- original concurrent status.
2457 or else Is_Primitive_Wrapper
(Spec_N
)
2459 -- Restore all references of corresponding record types to the
2460 -- original concurrent types.
2462 Replace_Types
(To_Corresponding
=> False);
2463 Priv_Spec
:= Find_Corresponding_Spec
(N
, False);
2465 -- The current body truly belongs to a primitive declared between
2466 -- a private and a full view. We leave the modified body as is,
2467 -- and return the true spec.
2469 if Present
(Priv_Spec
)
2470 and then Is_Private_Primitive
(Priv_Spec
)
2475 -- In case that this is some sort of error, restore the original
2476 -- state of the body.
2478 Replace_Types
(To_Corresponding
=> True);
2482 end Disambiguate_Spec
;
2484 ----------------------------
2485 -- Exchange_Limited_Views --
2486 ----------------------------
2488 procedure Exchange_Limited_Views
(Subp_Id
: Entity_Id
) is
2489 procedure Detect_And_Exchange
(Id
: Entity_Id
);
2490 -- Determine whether Id's type denotes an incomplete type associated
2491 -- with a limited with clause and exchange the limited view with the
2494 -------------------------
2495 -- Detect_And_Exchange --
2496 -------------------------
2498 procedure Detect_And_Exchange
(Id
: Entity_Id
) is
2499 Typ
: constant Entity_Id
:= Etype
(Id
);
2502 if Ekind
(Typ
) = E_Incomplete_Type
2503 and then From_Limited_With
(Typ
)
2504 and then Present
(Non_Limited_View
(Typ
))
2506 Set_Etype
(Id
, Non_Limited_View
(Typ
));
2508 end Detect_And_Exchange
;
2514 -- Start of processing for Exchange_Limited_Views
2517 if No
(Subp_Id
) then
2520 -- Do not process subprogram bodies as they already use the non-
2521 -- limited view of types.
2523 elsif not Ekind_In
(Subp_Id
, E_Function
, E_Procedure
) then
2527 -- Examine all formals and swap views when applicable
2529 Formal
:= First_Formal
(Subp_Id
);
2530 while Present
(Formal
) loop
2531 Detect_And_Exchange
(Formal
);
2533 Next_Formal
(Formal
);
2536 -- Process the return type of a function
2538 if Ekind
(Subp_Id
) = E_Function
then
2539 Detect_And_Exchange
(Subp_Id
);
2541 end Exchange_Limited_Views
;
2543 -------------------------------------
2544 -- Is_Private_Concurrent_Primitive --
2545 -------------------------------------
2547 function Is_Private_Concurrent_Primitive
2548 (Subp_Id
: Entity_Id
) return Boolean
2550 Formal_Typ
: Entity_Id
;
2553 if Present
(First_Formal
(Subp_Id
)) then
2554 Formal_Typ
:= Etype
(First_Formal
(Subp_Id
));
2556 if Is_Concurrent_Record_Type
(Formal_Typ
) then
2557 if Is_Class_Wide_Type
(Formal_Typ
) then
2558 Formal_Typ
:= Root_Type
(Formal_Typ
);
2561 Formal_Typ
:= Corresponding_Concurrent_Type
(Formal_Typ
);
2564 -- The type of the first formal is a concurrent tagged type with
2568 Is_Concurrent_Type
(Formal_Typ
)
2569 and then Is_Tagged_Type
(Formal_Typ
)
2570 and then Has_Private_Declaration
(Formal_Typ
);
2574 end Is_Private_Concurrent_Primitive
;
2576 ----------------------------
2577 -- Set_Trivial_Subprogram --
2578 ----------------------------
2580 procedure Set_Trivial_Subprogram
(N
: Node_Id
) is
2581 Nxt
: constant Node_Id
:= Next
(N
);
2584 Set_Is_Trivial_Subprogram
(Body_Id
);
2586 if Present
(Spec_Id
) then
2587 Set_Is_Trivial_Subprogram
(Spec_Id
);
2591 and then Nkind
(Nxt
) = N_Simple_Return_Statement
2592 and then No
(Next
(Nxt
))
2593 and then Present
(Expression
(Nxt
))
2594 and then Is_Entity_Name
(Expression
(Nxt
))
2596 Set_Never_Set_In_Source
(Entity
(Expression
(Nxt
)), False);
2598 end Set_Trivial_Subprogram
;
2600 ---------------------------------
2601 -- Verify_Overriding_Indicator --
2602 ---------------------------------
2604 procedure Verify_Overriding_Indicator
is
2606 if Must_Override
(Body_Spec
) then
2607 if Nkind
(Spec_Id
) = N_Defining_Operator_Symbol
2608 and then Operator_Matches_Spec
(Spec_Id
, Spec_Id
)
2612 elsif not Present
(Overridden_Operation
(Spec_Id
)) then
2614 ("subprogram& is not overriding", Body_Spec
, Spec_Id
);
2617 elsif Must_Not_Override
(Body_Spec
) then
2618 if Present
(Overridden_Operation
(Spec_Id
)) then
2620 ("subprogram& overrides inherited operation",
2621 Body_Spec
, Spec_Id
);
2623 elsif Nkind
(Spec_Id
) = N_Defining_Operator_Symbol
2624 and then Operator_Matches_Spec
(Spec_Id
, Spec_Id
)
2627 ("subprogram & overrides predefined operator ",
2628 Body_Spec
, Spec_Id
);
2630 -- If this is not a primitive operation or protected subprogram,
2631 -- then the overriding indicator is altogether illegal.
2633 elsif not Is_Primitive
(Spec_Id
)
2634 and then Ekind
(Scope
(Spec_Id
)) /= E_Protected_Type
2637 ("overriding indicator only allowed " &
2638 "if subprogram is primitive",
2643 and then Present
(Overridden_Operation
(Spec_Id
))
2645 pragma Assert
(Unit_Declaration_Node
(Body_Id
) = N
);
2646 Style
.Missing_Overriding
(N
, Body_Id
);
2649 and then Can_Override_Operator
(Spec_Id
)
2650 and then not Is_Predefined_File_Name
2651 (Unit_File_Name
(Get_Source_Unit
(Spec_Id
)))
2653 pragma Assert
(Unit_Declaration_Node
(Body_Id
) = N
);
2654 Style
.Missing_Overriding
(N
, Body_Id
);
2656 end Verify_Overriding_Indicator
;
2658 -- Start of processing for Analyze_Subprogram_Body_Helper
2661 -- Generic subprograms are handled separately. They always have a
2662 -- generic specification. Determine whether current scope has a
2663 -- previous declaration.
2665 -- If the subprogram body is defined within an instance of the same
2666 -- name, the instance appears as a package renaming, and will be hidden
2667 -- within the subprogram.
2669 if Present
(Prev_Id
)
2670 and then not Is_Overloadable
(Prev_Id
)
2671 and then (Nkind
(Parent
(Prev_Id
)) /= N_Package_Renaming_Declaration
2672 or else Comes_From_Source
(Prev_Id
))
2674 if Is_Generic_Subprogram
(Prev_Id
) then
2676 Set_Is_Compilation_Unit
(Body_Id
, Is_Compilation_Unit
(Spec_Id
));
2677 Set_Is_Child_Unit
(Body_Id
, Is_Child_Unit
(Spec_Id
));
2679 Analyze_Generic_Subprogram_Body
(N
, Spec_Id
);
2681 if Nkind
(N
) = N_Subprogram_Body
then
2682 HSS
:= Handled_Statement_Sequence
(N
);
2683 Check_Missing_Return
;
2689 -- Previous entity conflicts with subprogram name. Attempting to
2690 -- enter name will post error.
2692 Enter_Name
(Body_Id
);
2696 -- Non-generic case, find the subprogram declaration, if one was seen,
2697 -- or enter new overloaded entity in the current scope. If the
2698 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2699 -- part of the context of one of its subunits. No need to redo the
2702 elsif Prev_Id
= Body_Id
and then Has_Completion
(Body_Id
) then
2706 Body_Id
:= Analyze_Subprogram_Specification
(Body_Spec
);
2708 if Nkind
(N
) = N_Subprogram_Body_Stub
2709 or else No
(Corresponding_Spec
(N
))
2711 if Is_Private_Concurrent_Primitive
(Body_Id
) then
2712 Spec_Id
:= Disambiguate_Spec
;
2714 Spec_Id
:= Find_Corresponding_Spec
(N
);
2717 -- If this is a duplicate body, no point in analyzing it
2719 if Error_Posted
(N
) then
2723 -- A subprogram body should cause freezing of its own declaration,
2724 -- but if there was no previous explicit declaration, then the
2725 -- subprogram will get frozen too late (there may be code within
2726 -- the body that depends on the subprogram having been frozen,
2727 -- such as uses of extra formals), so we force it to be frozen
2728 -- here. Same holds if the body and spec are compilation units.
2729 -- Finally, if the return type is an anonymous access to protected
2730 -- subprogram, it must be frozen before the body because its
2731 -- expansion has generated an equivalent type that is used when
2732 -- elaborating the body.
2734 -- An exception in the case of Ada 2012, AI05-177: The bodies
2735 -- created for expression functions do not freeze.
2738 and then Nkind
(Original_Node
(N
)) /= N_Expression_Function
2740 Freeze_Before
(N
, Body_Id
);
2742 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
2743 Freeze_Before
(N
, Spec_Id
);
2745 elsif Is_Access_Subprogram_Type
(Etype
(Body_Id
)) then
2746 Freeze_Before
(N
, Etype
(Body_Id
));
2750 Spec_Id
:= Corresponding_Spec
(N
);
2754 -- Language-defined aspects cannot appear in a subprogram body [stub] if
2755 -- the subprogram has a separate spec. Certainly implementation-defined
2756 -- aspects are allowed to appear (per Aspects_On_Body_Of_Stub_OK).
2758 if Has_Aspects
(N
) then
2759 if Present
(Spec_Id
)
2760 and then not Aspects_On_Body_Or_Stub_OK
(N
)
2762 -- Do not emit an error on a subprogram body stub that act as
2765 and then Nkind
(Parent
(Parent
(Spec_Id
))) /= N_Subprogram_Body_Stub
2768 ("aspect specifications must appear in subprogram declaration",
2771 -- Delay the analysis of aspect specifications that apply to a body
2772 -- stub until the proper body is analyzed. If the corresponding body
2773 -- is missing, the aspects are still analyzed in Analyze_Proper_Body.
2775 elsif Nkind
(N
) in N_Body_Stub
then
2779 Analyze_Aspect_Specifications
(N
, Body_Id
);
2783 -- Previously we scanned the body to look for nested subprograms, and
2784 -- rejected an inline directive if nested subprograms were present,
2785 -- because the back-end would generate conflicting symbols for the
2786 -- nested bodies. This is now unnecessary.
2788 -- Look ahead to recognize a pragma Inline that appears after the body
2790 Check_Inline_Pragma
(Spec_Id
);
2792 -- Deal with special case of a fully private operation in the body of
2793 -- the protected type. We must create a declaration for the subprogram,
2794 -- in order to attach the protected subprogram that will be used in
2795 -- internal calls. We exclude compiler generated bodies from the
2796 -- expander since the issue does not arise for those cases.
2799 and then Comes_From_Source
(N
)
2800 and then Is_Protected_Type
(Current_Scope
)
2802 Spec_Id
:= Build_Private_Protected_Declaration
(N
);
2805 -- If a separate spec is present, then deal with freezing issues
2807 if Present
(Spec_Id
) then
2808 Spec_Decl
:= Unit_Declaration_Node
(Spec_Id
);
2809 Verify_Overriding_Indicator
;
2811 -- In general, the spec will be frozen when we start analyzing the
2812 -- body. However, for internally generated operations, such as
2813 -- wrapper functions for inherited operations with controlling
2814 -- results, the spec may not have been frozen by the time we expand
2815 -- the freeze actions that include the bodies. In particular, extra
2816 -- formals for accessibility or for return-in-place may need to be
2817 -- generated. Freeze nodes, if any, are inserted before the current
2818 -- body. These freeze actions are also needed in ASIS mode to enable
2819 -- the proper back-annotations.
2821 if not Is_Frozen
(Spec_Id
)
2822 and then (Expander_Active
or ASIS_Mode
)
2824 -- Force the generation of its freezing node to ensure proper
2825 -- management of access types in the backend.
2827 -- This is definitely needed for some cases, but it is not clear
2828 -- why, to be investigated further???
2830 Set_Has_Delayed_Freeze
(Spec_Id
);
2831 Freeze_Before
(N
, Spec_Id
);
2835 -- Mark presence of postcondition procedure in current scope and mark
2836 -- the procedure itself as needing debug info. The latter is important
2837 -- when analyzing decision coverage (for example, for MC/DC coverage).
2839 if Chars
(Body_Id
) = Name_uPostconditions
then
2840 Set_Has_Postconditions
(Current_Scope
);
2841 Set_Debug_Info_Needed
(Body_Id
);
2844 -- Place subprogram on scope stack, and make formals visible. If there
2845 -- is a spec, the visible entity remains that of the spec.
2847 if Present
(Spec_Id
) then
2848 Generate_Reference
(Spec_Id
, Body_Id
, 'b', Set_Ref
=> False);
2850 if Is_Child_Unit
(Spec_Id
) then
2851 Generate_Reference
(Spec_Id
, Scope
(Spec_Id
), 'k', False);
2855 Style
.Check_Identifier
(Body_Id
, Spec_Id
);
2858 Set_Is_Compilation_Unit
(Body_Id
, Is_Compilation_Unit
(Spec_Id
));
2859 Set_Is_Child_Unit
(Body_Id
, Is_Child_Unit
(Spec_Id
));
2861 if Is_Abstract_Subprogram
(Spec_Id
) then
2862 Error_Msg_N
("an abstract subprogram cannot have a body", N
);
2866 Set_Convention
(Body_Id
, Convention
(Spec_Id
));
2867 Set_Has_Completion
(Spec_Id
);
2869 if Is_Protected_Type
(Scope
(Spec_Id
)) then
2870 Prot_Typ
:= Scope
(Spec_Id
);
2873 -- If this is a body generated for a renaming, do not check for
2874 -- full conformance. The check is redundant, because the spec of
2875 -- the body is a copy of the spec in the renaming declaration,
2876 -- and the test can lead to spurious errors on nested defaults.
2878 if Present
(Spec_Decl
)
2879 and then not Comes_From_Source
(N
)
2881 (Nkind
(Original_Node
(Spec_Decl
)) =
2882 N_Subprogram_Renaming_Declaration
2883 or else (Present
(Corresponding_Body
(Spec_Decl
))
2885 Nkind
(Unit_Declaration_Node
2886 (Corresponding_Body
(Spec_Decl
))) =
2887 N_Subprogram_Renaming_Declaration
))
2891 -- Conversely, the spec may have been generated for specless body
2892 -- with an inline pragma.
2894 elsif Comes_From_Source
(N
)
2895 and then not Comes_From_Source
(Spec_Id
)
2896 and then Has_Pragma_Inline
(Spec_Id
)
2903 Fully_Conformant
, True, Conformant
, Body_Id
);
2906 -- If the body is not fully conformant, we have to decide if we
2907 -- should analyze it or not. If it has a really messed up profile
2908 -- then we probably should not analyze it, since we will get too
2909 -- many bogus messages.
2911 -- Our decision is to go ahead in the non-fully conformant case
2912 -- only if it is at least mode conformant with the spec. Note
2913 -- that the call to Check_Fully_Conformant has issued the proper
2914 -- error messages to complain about the lack of conformance.
2917 and then not Mode_Conformant
(Body_Id
, Spec_Id
)
2923 if Spec_Id
/= Body_Id
then
2924 Reference_Body_Formals
(Spec_Id
, Body_Id
);
2927 if Nkind
(N
) = N_Subprogram_Body_Stub
then
2928 Set_Corresponding_Spec_Of_Stub
(N
, Spec_Id
);
2933 Set_Corresponding_Spec
(N
, Spec_Id
);
2935 -- Ada 2005 (AI-345): If the operation is a primitive operation
2936 -- of a concurrent type, the type of the first parameter has been
2937 -- replaced with the corresponding record, which is the proper
2938 -- run-time structure to use. However, within the body there may
2939 -- be uses of the formals that depend on primitive operations
2940 -- of the type (in particular calls in prefixed form) for which
2941 -- we need the original concurrent type. The operation may have
2942 -- several controlling formals, so the replacement must be done
2945 if Comes_From_Source
(Spec_Id
)
2946 and then Present
(First_Entity
(Spec_Id
))
2947 and then Ekind
(Etype
(First_Entity
(Spec_Id
))) = E_Record_Type
2948 and then Is_Tagged_Type
(Etype
(First_Entity
(Spec_Id
)))
2949 and then Present
(Interfaces
(Etype
(First_Entity
(Spec_Id
))))
2950 and then Present
(Corresponding_Concurrent_Type
2951 (Etype
(First_Entity
(Spec_Id
))))
2954 Typ
: constant Entity_Id
:= Etype
(First_Entity
(Spec_Id
));
2958 Form
:= First_Formal
(Spec_Id
);
2959 while Present
(Form
) loop
2960 if Etype
(Form
) = Typ
then
2961 Set_Etype
(Form
, Corresponding_Concurrent_Type
(Typ
));
2969 -- Make the formals visible, and place subprogram on scope stack.
2970 -- This is also the point at which we set Last_Real_Spec_Entity
2971 -- to mark the entities which will not be moved to the body.
2973 Install_Formals
(Spec_Id
);
2974 Last_Real_Spec_Entity
:= Last_Entity
(Spec_Id
);
2976 -- Within an instance, add local renaming declarations so that
2977 -- gdb can retrieve the values of actuals more easily. This is
2978 -- only relevant if generating code (and indeed we definitely
2979 -- do not want these definitions -gnatc mode, because that would
2982 if Is_Generic_Instance
(Spec_Id
)
2983 and then Is_Wrapper_Package
(Current_Scope
)
2984 and then Expander_Active
2986 Build_Subprogram_Instance_Renamings
(N
, Current_Scope
);
2989 Push_Scope
(Spec_Id
);
2991 -- Make sure that the subprogram is immediately visible. For
2992 -- child units that have no separate spec this is indispensable.
2993 -- Otherwise it is safe albeit redundant.
2995 Set_Is_Immediately_Visible
(Spec_Id
);
2998 Set_Corresponding_Body
(Unit_Declaration_Node
(Spec_Id
), Body_Id
);
2999 Set_Contract
(Body_Id
, Make_Contract
(Sloc
(Body_Id
)));
3000 Set_Ekind
(Body_Id
, E_Subprogram_Body
);
3001 Set_Scope
(Body_Id
, Scope
(Spec_Id
));
3002 Set_Is_Obsolescent
(Body_Id
, Is_Obsolescent
(Spec_Id
));
3004 -- Case of subprogram body with no previous spec
3007 -- Check for style warning required
3011 -- Only apply check for source level subprograms for which checks
3012 -- have not been suppressed.
3014 and then Comes_From_Source
(Body_Id
)
3015 and then not Suppress_Style_Checks
(Body_Id
)
3017 -- No warnings within an instance
3019 and then not In_Instance
3021 -- No warnings for expression functions
3023 and then Nkind
(Original_Node
(N
)) /= N_Expression_Function
3025 Style
.Body_With_No_Spec
(N
);
3028 New_Overloaded_Entity
(Body_Id
);
3030 if Nkind
(N
) /= N_Subprogram_Body_Stub
then
3031 Set_Acts_As_Spec
(N
);
3032 Generate_Definition
(Body_Id
);
3033 Set_Contract
(Body_Id
, Make_Contract
(Sloc
(Body_Id
)));
3035 (Body_Id
, Body_Id
, 'b', Set_Ref
=> False, Force
=> True);
3036 Install_Formals
(Body_Id
);
3037 Push_Scope
(Body_Id
);
3040 -- For stubs and bodies with no previous spec, generate references to
3043 Generate_Reference_To_Formals
(Body_Id
);
3046 -- If the return type is an anonymous access type whose designated type
3047 -- is the limited view of a class-wide type and the non-limited view is
3048 -- available, update the return type accordingly.
3050 if Ada_Version
>= Ada_2005
and then Comes_From_Source
(N
) then
3056 Rtyp
:= Etype
(Current_Scope
);
3058 if Ekind
(Rtyp
) = E_Anonymous_Access_Type
then
3059 Etyp
:= Directly_Designated_Type
(Rtyp
);
3061 if Is_Class_Wide_Type
(Etyp
)
3062 and then From_Limited_With
(Etyp
)
3064 Set_Directly_Designated_Type
3065 (Etype
(Current_Scope
), Available_View
(Etyp
));
3071 -- If this is the proper body of a stub, we must verify that the stub
3072 -- conforms to the body, and to the previous spec if one was present.
3073 -- We know already that the body conforms to that spec. This test is
3074 -- only required for subprograms that come from source.
3076 if Nkind
(Parent
(N
)) = N_Subunit
3077 and then Comes_From_Source
(N
)
3078 and then not Error_Posted
(Body_Id
)
3079 and then Nkind
(Corresponding_Stub
(Parent
(N
))) =
3080 N_Subprogram_Body_Stub
3083 Old_Id
: constant Entity_Id
:=
3085 (Specification
(Corresponding_Stub
(Parent
(N
))));
3087 Conformant
: Boolean := False;
3090 if No
(Spec_Id
) then
3091 Check_Fully_Conformant
(Body_Id
, Old_Id
);
3095 (Body_Id
, Old_Id
, Fully_Conformant
, False, Conformant
);
3097 if not Conformant
then
3099 -- The stub was taken to be a new declaration. Indicate that
3102 Set_Has_Completion
(Old_Id
, False);
3108 Set_Has_Completion
(Body_Id
);
3109 Check_Eliminated
(Body_Id
);
3111 if Nkind
(N
) = N_Subprogram_Body_Stub
then
3115 -- Handle frontend inlining. There is no need to prepare us for inlining
3116 -- if we will not generate the code.
3120 if not Debug_Flag_Dot_K
then
3121 if Present
(Spec_Id
)
3122 and then Expander_Active
3124 (Has_Pragma_Inline_Always
(Spec_Id
)
3125 or else (Has_Pragma_Inline
(Spec_Id
) and Front_End_Inlining
))
3127 Build_Body_To_Inline
(N
, Spec_Id
);
3132 elsif Expander_Active
3133 and then Serious_Errors_Detected
= 0
3134 and then Present
(Spec_Id
)
3135 and then Has_Pragma_Inline
(Spec_Id
)
3137 Check_And_Build_Body_To_Inline
(N
, Spec_Id
, Body_Id
);
3140 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3141 -- of the specification we have to install the private withed units.
3142 -- This holds for child units as well.
3144 if Is_Compilation_Unit
(Body_Id
)
3145 or else Nkind
(Parent
(N
)) = N_Compilation_Unit
3147 Install_Private_With_Clauses
(Body_Id
);
3150 Check_Anonymous_Return
;
3152 -- Set the Protected_Formal field of each extra formal of the protected
3153 -- subprogram to reference the corresponding extra formal of the
3154 -- subprogram that implements it. For regular formals this occurs when
3155 -- the protected subprogram's declaration is expanded, but the extra
3156 -- formals don't get created until the subprogram is frozen. We need to
3157 -- do this before analyzing the protected subprogram's body so that any
3158 -- references to the original subprogram's extra formals will be changed
3159 -- refer to the implementing subprogram's formals (see Expand_Formal).
3161 if Present
(Spec_Id
)
3162 and then Is_Protected_Type
(Scope
(Spec_Id
))
3163 and then Present
(Protected_Body_Subprogram
(Spec_Id
))
3166 Impl_Subp
: constant Entity_Id
:=
3167 Protected_Body_Subprogram
(Spec_Id
);
3168 Prot_Ext_Formal
: Entity_Id
:= Extra_Formals
(Spec_Id
);
3169 Impl_Ext_Formal
: Entity_Id
:= Extra_Formals
(Impl_Subp
);
3171 while Present
(Prot_Ext_Formal
) loop
3172 pragma Assert
(Present
(Impl_Ext_Formal
));
3173 Set_Protected_Formal
(Prot_Ext_Formal
, Impl_Ext_Formal
);
3174 Next_Formal_With_Extras
(Prot_Ext_Formal
);
3175 Next_Formal_With_Extras
(Impl_Ext_Formal
);
3180 -- Now we can go on to analyze the body
3182 HSS
:= Handled_Statement_Sequence
(N
);
3183 Set_Actual_Subtypes
(N
, Current_Scope
);
3185 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
3186 -- invariants and predicates associated with the body and its spec.
3187 -- Note that this is not pure expansion as Expand_Subprogram_Contract
3188 -- prepares the contract assertions for generic subprograms or for ASIS.
3189 -- Do not generate contract checks in SPARK mode.
3191 if not SPARK_Mode
then
3192 Expand_Subprogram_Contract
(N
, Spec_Id
, Body_Id
);
3195 -- Add a declaration for the Protection object, renaming declarations
3196 -- for discriminals and privals and finally a declaration for the entry
3197 -- family index (if applicable). This form of early expansion is done
3198 -- when the Expander is active because Install_Private_Data_Declarations
3199 -- references entities which were created during regular expansion. The
3200 -- body may be the rewritting of an expression function, and we need to
3201 -- verify that the original node is in the source.
3203 if Full_Expander_Active
3204 and then Comes_From_Source
(Original_Node
(N
))
3205 and then Present
(Prot_Typ
)
3206 and then Present
(Spec_Id
)
3207 and then not Is_Eliminated
(Spec_Id
)
3209 Install_Private_Data_Declarations
3210 (Sloc
(N
), Spec_Id
, Prot_Typ
, N
, Declarations
(N
));
3213 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3214 -- may now appear in parameter and result profiles. Since the analysis
3215 -- of a subprogram body may use the parameter and result profile of the
3216 -- spec, swap any limited views with their non-limited counterpart.
3218 if Ada_Version
>= Ada_2012
then
3219 Exchange_Limited_Views
(Spec_Id
);
3222 -- Analyze the declarations (this call will analyze the precondition
3223 -- Check pragmas we prepended to the list, as well as the declaration
3224 -- of the _Postconditions procedure).
3226 Analyze_Declarations
(Declarations
(N
));
3228 -- Check completion, and analyze the statements
3231 Inspect_Deferred_Constant_Completion
(Declarations
(N
));
3234 -- Deal with end of scope processing for the body
3236 Process_End_Label
(HSS
, 't', Current_Scope
);
3238 Check_Subprogram_Order
(N
);
3239 Set_Analyzed
(Body_Id
);
3241 -- If we have a separate spec, then the analysis of the declarations
3242 -- caused the entities in the body to be chained to the spec id, but
3243 -- we want them chained to the body id. Only the formal parameters
3244 -- end up chained to the spec id in this case.
3246 if Present
(Spec_Id
) then
3248 -- We must conform to the categorization of our spec
3250 Validate_Categorization_Dependency
(N
, Spec_Id
);
3252 -- And if this is a child unit, the parent units must conform
3254 if Is_Child_Unit
(Spec_Id
) then
3255 Validate_Categorization_Dependency
3256 (Unit_Declaration_Node
(Spec_Id
), Spec_Id
);
3259 -- Here is where we move entities from the spec to the body
3261 -- Case where there are entities that stay with the spec
3263 if Present
(Last_Real_Spec_Entity
) then
3265 -- No body entities (happens when the only real spec entities come
3266 -- from precondition and postcondition pragmas).
3268 if No
(Last_Entity
(Body_Id
)) then
3270 (Body_Id
, Next_Entity
(Last_Real_Spec_Entity
));
3272 -- Body entities present (formals), so chain stuff past them
3276 (Last_Entity
(Body_Id
), Next_Entity
(Last_Real_Spec_Entity
));
3279 Set_Next_Entity
(Last_Real_Spec_Entity
, Empty
);
3280 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
3281 Set_Last_Entity
(Spec_Id
, Last_Real_Spec_Entity
);
3283 -- Case where there are no spec entities, in this case there can be
3284 -- no body entities either, so just move everything.
3287 pragma Assert
(No
(Last_Entity
(Body_Id
)));
3288 Set_First_Entity
(Body_Id
, First_Entity
(Spec_Id
));
3289 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
3290 Set_First_Entity
(Spec_Id
, Empty
);
3291 Set_Last_Entity
(Spec_Id
, Empty
);
3295 Check_Missing_Return
;
3297 -- Now we are going to check for variables that are never modified in
3298 -- the body of the procedure. But first we deal with a special case
3299 -- where we want to modify this check. If the body of the subprogram
3300 -- starts with a raise statement or its equivalent, or if the body
3301 -- consists entirely of a null statement, then it is pretty obvious
3302 -- that it is OK to not reference the parameters. For example, this
3303 -- might be the following common idiom for a stubbed function:
3304 -- statement of the procedure raises an exception. In particular this
3305 -- deals with the common idiom of a stubbed function, which might
3306 -- appear as something like:
3308 -- function F (A : Integer) return Some_Type;
3311 -- raise Program_Error;
3315 -- Here the purpose of X is simply to satisfy the annoying requirement
3316 -- in Ada that there be at least one return, and we certainly do not
3317 -- want to go posting warnings on X that it is not initialized! On
3318 -- the other hand, if X is entirely unreferenced that should still
3321 -- What we do is to detect these cases, and if we find them, flag the
3322 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3323 -- suppress unwanted warnings. For the case of the function stub above
3324 -- we have a special test to set X as apparently assigned to suppress
3331 -- Skip initial labels (for one thing this occurs when we are in
3332 -- front end ZCX mode, but in any case it is irrelevant), and also
3333 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3335 Stm
:= First
(Statements
(HSS
));
3336 while Nkind
(Stm
) = N_Label
3337 or else Nkind
(Stm
) in N_Push_xxx_Label
3342 -- Do the test on the original statement before expansion
3345 Ostm
: constant Node_Id
:= Original_Node
(Stm
);
3348 -- If explicit raise statement, turn on flag
3350 if Nkind
(Ostm
) = N_Raise_Statement
then
3351 Set_Trivial_Subprogram
(Stm
);
3353 -- If null statement, and no following statements, turn on flag
3355 elsif Nkind
(Stm
) = N_Null_Statement
3356 and then Comes_From_Source
(Stm
)
3357 and then No
(Next
(Stm
))
3359 Set_Trivial_Subprogram
(Stm
);
3361 -- Check for explicit call cases which likely raise an exception
3363 elsif Nkind
(Ostm
) = N_Procedure_Call_Statement
then
3364 if Is_Entity_Name
(Name
(Ostm
)) then
3366 Ent
: constant Entity_Id
:= Entity
(Name
(Ostm
));
3369 -- If the procedure is marked No_Return, then likely it
3370 -- raises an exception, but in any case it is not coming
3371 -- back here, so turn on the flag.
3374 and then Ekind
(Ent
) = E_Procedure
3375 and then No_Return
(Ent
)
3377 Set_Trivial_Subprogram
(Stm
);
3385 -- Check for variables that are never modified
3391 -- If there is a separate spec, then transfer Never_Set_In_Source
3392 -- flags from out parameters to the corresponding entities in the
3393 -- body. The reason we do that is we want to post error flags on
3394 -- the body entities, not the spec entities.
3396 if Present
(Spec_Id
) then
3397 E1
:= First_Entity
(Spec_Id
);
3398 while Present
(E1
) loop
3399 if Ekind
(E1
) = E_Out_Parameter
then
3400 E2
:= First_Entity
(Body_Id
);
3401 while Present
(E2
) loop
3402 exit when Chars
(E1
) = Chars
(E2
);
3406 if Present
(E2
) then
3407 Set_Never_Set_In_Source
(E2
, Never_Set_In_Source
(E1
));
3415 -- Check references in body
3417 Check_References
(Body_Id
);
3419 end Analyze_Subprogram_Body_Helper
;
3421 ---------------------------------
3422 -- Analyze_Subprogram_Contract --
3423 ---------------------------------
3425 procedure Analyze_Subprogram_Contract
(Subp
: Entity_Id
) is
3426 Result_Seen
: Boolean := False;
3427 -- A flag which keeps track of whether at least one postcondition or
3428 -- contract-case mentions attribute 'Result (set True if so).
3430 procedure Check_Result_And_Post_State
3432 Error_Nod
: in out Node_Id
);
3433 -- Determine whether pragma Prag mentions attribute 'Result and whether
3434 -- the pragma contains an expression that evaluates differently in pre-
3435 -- and post-state. Prag is a postcondition or a contract-cases pragma.
3436 -- Error_Nod denotes the proper error node.
3438 ---------------------------------
3439 -- Check_Result_And_Post_State --
3440 ---------------------------------
3442 procedure Check_Result_And_Post_State
3444 Error_Nod
: in out Node_Id
)
3446 procedure Check_Expression
(Expr
: Node_Id
);
3447 -- Perform the 'Result and post-state checks on a given expression
3449 function Is_Function_Result
(N
: Node_Id
) return Traverse_Result
;
3450 -- Attempt to find attribute 'Result in a subtree denoted by N
3452 function Is_Trivial_Boolean
(N
: Node_Id
) return Boolean;
3453 -- Determine whether source node N denotes "True" or "False"
3455 function Mentions_Post_State
(N
: Node_Id
) return Boolean;
3456 -- Determine whether a subtree denoted by N mentions any construct
3457 -- that denotes a post-state.
3459 procedure Check_Function_Result
is
3460 new Traverse_Proc
(Is_Function_Result
);
3462 ----------------------
3463 -- Check_Expression --
3464 ----------------------
3466 procedure Check_Expression
(Expr
: Node_Id
) is
3468 if not Is_Trivial_Boolean
(Expr
) then
3469 Check_Function_Result
(Expr
);
3471 if not Mentions_Post_State
(Expr
) then
3472 if Pragma_Name
(Prag
) = Name_Contract_Cases
then
3474 ("contract case refers only to pre-state?T?", Expr
);
3477 ("postcondition refers only to pre-state?T?", Prag
);
3481 end Check_Expression
;
3483 ------------------------
3484 -- Is_Function_Result --
3485 ------------------------
3487 function Is_Function_Result
(N
: Node_Id
) return Traverse_Result
is
3489 if Nkind
(N
) = N_Attribute_Reference
3490 and then Attribute_Name
(N
) = Name_Result
3492 Result_Seen
:= True;
3495 -- Continue the traversal
3500 end Is_Function_Result
;
3502 ------------------------
3503 -- Is_Trivial_Boolean --
3504 ------------------------
3506 function Is_Trivial_Boolean
(N
: Node_Id
) return Boolean is
3509 Comes_From_Source
(N
)
3510 and then Is_Entity_Name
(N
)
3511 and then (Entity
(N
) = Standard_True
3512 or else Entity
(N
) = Standard_False
);
3513 end Is_Trivial_Boolean
;
3515 -------------------------
3516 -- Mentions_Post_State --
3517 -------------------------
3519 function Mentions_Post_State
(N
: Node_Id
) return Boolean is
3520 Post_State_Seen
: Boolean := False;
3522 function Is_Post_State
(N
: Node_Id
) return Traverse_Result
;
3523 -- Attempt to find a construct that denotes a post-state. If this
3524 -- is the case, set flag Post_State_Seen.
3530 function Is_Post_State
(N
: Node_Id
) return Traverse_Result
is
3534 if Nkind_In
(N
, N_Explicit_Dereference
, N_Function_Call
) then
3535 Post_State_Seen
:= True;
3538 elsif Nkind_In
(N
, N_Expanded_Name
, N_Identifier
) then
3541 if No
(Ent
) or else Ekind
(Ent
) in Assignable_Kind
then
3542 Post_State_Seen
:= True;
3546 elsif Nkind
(N
) = N_Attribute_Reference
then
3547 if Attribute_Name
(N
) = Name_Old
then
3549 elsif Attribute_Name
(N
) = Name_Result
then
3550 Post_State_Seen
:= True;
3558 procedure Find_Post_State
is new Traverse_Proc
(Is_Post_State
);
3560 -- Start of processing for Mentions_Post_State
3563 Find_Post_State
(N
);
3564 return Post_State_Seen
;
3565 end Mentions_Post_State
;
3569 Expr
: constant Node_Id
:=
3570 Expression
(First
(Pragma_Argument_Associations
(Prag
)));
3571 Nam
: constant Name_Id
:= Pragma_Name
(Prag
);
3574 -- Start of processing for Check_Result_And_Post_State
3577 if No
(Error_Nod
) then
3581 -- Examine all consequences
3583 if Nam
= Name_Contract_Cases
then
3584 CCase
:= First
(Component_Associations
(Expr
));
3585 while Present
(CCase
) loop
3586 Check_Expression
(Expression
(CCase
));
3591 -- Examine the expression of a postcondition
3594 pragma Assert
(Nam
= Name_Postcondition
);
3595 Check_Expression
(Expr
);
3597 end Check_Result_And_Post_State
;
3601 Items
: constant Node_Id
:= Contract
(Subp
);
3602 Depends
: Node_Id
:= Empty
;
3603 Error_CCase
: Node_Id
:= Empty
;
3604 Error_Post
: Node_Id
:= Empty
;
3605 Global
: Node_Id
:= Empty
;
3609 -- Start of processing for Analyze_Subprogram_Contract
3612 if Present
(Items
) then
3614 -- Analyze pre- and postconditions
3616 Prag
:= Pre_Post_Conditions
(Items
);
3617 while Present
(Prag
) loop
3618 Analyze_Pre_Post_Condition_In_Decl_Part
(Prag
, Subp
);
3620 -- Verify whether a postcondition mentions attribute 'Result and
3621 -- its expression introduces a post-state.
3623 if Warn_On_Suspicious_Contract
3624 and then Pragma_Name
(Prag
) = Name_Postcondition
3626 Check_Result_And_Post_State
(Prag
, Error_Post
);
3629 Prag
:= Next_Pragma
(Prag
);
3632 -- Analyze contract-cases and test-cases
3634 Prag
:= Contract_Test_Cases
(Items
);
3635 while Present
(Prag
) loop
3636 Nam
:= Pragma_Name
(Prag
);
3638 if Nam
= Name_Contract_Cases
then
3639 Analyze_Contract_Cases_In_Decl_Part
(Prag
);
3641 -- Verify whether contract-cases mention attribute 'Result and
3642 -- its expression introduces a post-state. Perform the check
3643 -- only when the pragma is legal.
3645 if Warn_On_Suspicious_Contract
3646 and then not Error_Posted
(Prag
)
3648 Check_Result_And_Post_State
(Prag
, Error_CCase
);
3652 pragma Assert
(Nam
= Name_Test_Case
);
3653 Analyze_Test_Case_In_Decl_Part
(Prag
, Subp
);
3656 Prag
:= Next_Pragma
(Prag
);
3659 -- Analyze classification pragmas
3661 Prag
:= Classifications
(Contract
(Subp
));
3662 while Present
(Prag
) loop
3663 Nam
:= Pragma_Name
(Prag
);
3665 if Nam
= Name_Depends
then
3667 else pragma Assert
(Nam
= Name_Global
);
3671 Prag
:= Next_Pragma
(Prag
);
3674 -- Analyze Global first as Depends may mention items classified in
3675 -- the global categorization.
3677 if Present
(Global
) then
3678 Analyze_Global_In_Decl_Part
(Global
);
3681 -- Depends must be analyzed after Global in order to see the modes of
3682 -- all global items.
3684 if Present
(Depends
) then
3685 Analyze_Depends_In_Decl_Part
(Depends
);
3689 -- Emit an error when none of the postconditions or contract-cases
3690 -- mention attribute 'Result in the context of a function.
3692 if Warn_On_Suspicious_Contract
3693 and then Ekind_In
(Subp
, E_Function
, E_Generic_Function
)
3694 and then not Result_Seen
3696 if Present
(Error_Post
) and then Present
(Error_CCase
) then
3698 ("neither function postcondition nor contract cases mention "
3699 & "result?T?", Error_Post
);
3701 elsif Present
(Error_Post
) then
3703 ("function postcondition does not mention result?T?",
3706 elsif Present
(Error_CCase
) then
3708 ("contract cases do not mention result?T?", Error_CCase
);
3711 end Analyze_Subprogram_Contract
;
3713 ------------------------------------
3714 -- Analyze_Subprogram_Declaration --
3715 ------------------------------------
3717 procedure Analyze_Subprogram_Declaration
(N
: Node_Id
) is
3718 Scop
: constant Entity_Id
:= Current_Scope
;
3719 Designator
: Entity_Id
;
3720 Is_Completion
: Boolean;
3721 -- Indicates whether a null procedure declaration is a completion
3724 -- Null procedures are not allowed in SPARK
3726 if Nkind
(Specification
(N
)) = N_Procedure_Specification
3727 and then Null_Present
(Specification
(N
))
3729 Check_SPARK_Restriction
("null procedure is not allowed", N
);
3731 if Is_Protected_Type
(Current_Scope
) then
3732 Error_Msg_N
("protected operation cannot be a null procedure", N
);
3735 Analyze_Null_Procedure
(N
, Is_Completion
);
3737 if Is_Completion
then
3739 -- The null procedure acts as a body, nothing further is needed.
3745 Designator
:= Analyze_Subprogram_Specification
(Specification
(N
));
3747 -- A reference may already have been generated for the unit name, in
3748 -- which case the following call is redundant. However it is needed for
3749 -- declarations that are the rewriting of an expression function.
3751 Generate_Definition
(Designator
);
3753 if Debug_Flag_C
then
3754 Write_Str
("==> subprogram spec ");
3755 Write_Name
(Chars
(Designator
));
3756 Write_Str
(" from ");
3757 Write_Location
(Sloc
(N
));
3762 Validate_RCI_Subprogram_Declaration
(N
);
3763 New_Overloaded_Entity
(Designator
);
3764 Check_Delayed_Subprogram
(Designator
);
3766 -- If the type of the first formal of the current subprogram is a
3767 -- non-generic tagged private type, mark the subprogram as being a
3768 -- private primitive. Ditto if this is a function with controlling
3769 -- result, and the return type is currently private. In both cases,
3770 -- the type of the controlling argument or result must be in the
3771 -- current scope for the operation to be primitive.
3773 if Has_Controlling_Result
(Designator
)
3774 and then Is_Private_Type
(Etype
(Designator
))
3775 and then Scope
(Etype
(Designator
)) = Current_Scope
3776 and then not Is_Generic_Actual_Type
(Etype
(Designator
))
3778 Set_Is_Private_Primitive
(Designator
);
3780 elsif Present
(First_Formal
(Designator
)) then
3782 Formal_Typ
: constant Entity_Id
:=
3783 Etype
(First_Formal
(Designator
));
3785 Set_Is_Private_Primitive
(Designator
,
3786 Is_Tagged_Type
(Formal_Typ
)
3787 and then Scope
(Formal_Typ
) = Current_Scope
3788 and then Is_Private_Type
(Formal_Typ
)
3789 and then not Is_Generic_Actual_Type
(Formal_Typ
));
3793 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
3796 if Ada_Version
>= Ada_2005
3797 and then Comes_From_Source
(N
)
3798 and then Is_Dispatching_Operation
(Designator
)
3805 if Has_Controlling_Result
(Designator
) then
3806 Etyp
:= Etype
(Designator
);
3809 E
:= First_Entity
(Designator
);
3811 and then Is_Formal
(E
)
3812 and then not Is_Controlling_Formal
(E
)
3820 if Is_Access_Type
(Etyp
) then
3821 Etyp
:= Directly_Designated_Type
(Etyp
);
3824 if Is_Interface
(Etyp
)
3825 and then not Is_Abstract_Subprogram
(Designator
)
3826 and then not (Ekind
(Designator
) = E_Procedure
3827 and then Null_Present
(Specification
(N
)))
3829 Error_Msg_Name_1
:= Chars
(Defining_Entity
(N
));
3831 -- Specialize error message based on procedures vs. functions,
3832 -- since functions can't be null subprograms.
3834 if Ekind
(Designator
) = E_Procedure
then
3836 ("interface procedure % must be abstract or null", N
);
3838 Error_Msg_N
("interface function % must be abstract", N
);
3844 -- What is the following code for, it used to be
3846 -- ??? Set_Suppress_Elaboration_Checks
3847 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3849 -- The following seems equivalent, but a bit dubious
3851 if Elaboration_Checks_Suppressed
(Designator
) then
3852 Set_Kill_Elaboration_Checks
(Designator
);
3855 if Scop
/= Standard_Standard
and then not Is_Child_Unit
(Designator
) then
3856 Set_Categorization_From_Scope
(Designator
, Scop
);
3859 -- For a compilation unit, check for library-unit pragmas
3861 Push_Scope
(Designator
);
3862 Set_Categorization_From_Pragmas
(N
);
3863 Validate_Categorization_Dependency
(N
, Designator
);
3867 -- For a compilation unit, set body required. This flag will only be
3868 -- reset if a valid Import or Interface pragma is processed later on.
3870 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3871 Set_Body_Required
(Parent
(N
), True);
3873 if Ada_Version
>= Ada_2005
3874 and then Nkind
(Specification
(N
)) = N_Procedure_Specification
3875 and then Null_Present
(Specification
(N
))
3878 ("null procedure cannot be declared at library level", N
);
3882 Generate_Reference_To_Formals
(Designator
);
3883 Check_Eliminated
(Designator
);
3885 if Debug_Flag_C
then
3887 Write_Str
("<== subprogram spec ");
3888 Write_Name
(Chars
(Designator
));
3889 Write_Str
(" from ");
3890 Write_Location
(Sloc
(N
));
3894 if Is_Protected_Type
(Current_Scope
) then
3896 -- Indicate that this is a protected operation, because it may be
3897 -- used in subsequent declarations within the protected type.
3899 Set_Convention
(Designator
, Convention_Protected
);
3902 List_Inherited_Pre_Post_Aspects
(Designator
);
3904 if Has_Aspects
(N
) then
3905 Analyze_Aspect_Specifications
(N
, Designator
);
3907 end Analyze_Subprogram_Declaration
;
3909 --------------------------------------
3910 -- Analyze_Subprogram_Specification --
3911 --------------------------------------
3913 -- Reminder: N here really is a subprogram specification (not a subprogram
3914 -- declaration). This procedure is called to analyze the specification in
3915 -- both subprogram bodies and subprogram declarations (specs).
3917 function Analyze_Subprogram_Specification
(N
: Node_Id
) return Entity_Id
is
3918 Designator
: constant Entity_Id
:= Defining_Entity
(N
);
3919 Formals
: constant List_Id
:= Parameter_Specifications
(N
);
3921 -- Start of processing for Analyze_Subprogram_Specification
3924 -- User-defined operator is not allowed in SPARK, except as a renaming
3926 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Operator_Symbol
3927 and then Nkind
(Parent
(N
)) /= N_Subprogram_Renaming_Declaration
3929 Check_SPARK_Restriction
("user-defined operator is not allowed", N
);
3932 -- Proceed with analysis. Do not emit a cross-reference entry if the
3933 -- specification comes from an expression function, because it may be
3934 -- the completion of a previous declaration. It is is not, the cross-
3935 -- reference entry will be emitted for the new subprogram declaration.
3937 if Nkind
(Parent
(N
)) /= N_Expression_Function
then
3938 Generate_Definition
(Designator
);
3941 Set_Contract
(Designator
, Make_Contract
(Sloc
(Designator
)));
3943 if Nkind
(N
) = N_Function_Specification
then
3944 Set_Ekind
(Designator
, E_Function
);
3945 Set_Mechanism
(Designator
, Default_Mechanism
);
3947 Set_Ekind
(Designator
, E_Procedure
);
3948 Set_Etype
(Designator
, Standard_Void_Type
);
3951 -- Introduce new scope for analysis of the formals and the return type
3953 Set_Scope
(Designator
, Current_Scope
);
3955 if Present
(Formals
) then
3956 Push_Scope
(Designator
);
3957 Process_Formals
(Formals
, N
);
3959 -- Check dimensions in N for formals with default expression
3961 Analyze_Dimension_Formals
(N
, Formals
);
3963 -- Ada 2005 (AI-345): If this is an overriding operation of an
3964 -- inherited interface operation, and the controlling type is
3965 -- a synchronized type, replace the type with its corresponding
3966 -- record, to match the proper signature of an overriding operation.
3967 -- Same processing for an access parameter whose designated type is
3968 -- derived from a synchronized interface.
3970 if Ada_Version
>= Ada_2005
then
3973 Formal_Typ
: Entity_Id
;
3974 Rec_Typ
: Entity_Id
;
3975 Desig_Typ
: Entity_Id
;
3978 Formal
:= First_Formal
(Designator
);
3979 while Present
(Formal
) loop
3980 Formal_Typ
:= Etype
(Formal
);
3982 if Is_Concurrent_Type
(Formal_Typ
)
3983 and then Present
(Corresponding_Record_Type
(Formal_Typ
))
3985 Rec_Typ
:= Corresponding_Record_Type
(Formal_Typ
);
3987 if Present
(Interfaces
(Rec_Typ
)) then
3988 Set_Etype
(Formal
, Rec_Typ
);
3991 elsif Ekind
(Formal_Typ
) = E_Anonymous_Access_Type
then
3992 Desig_Typ
:= Designated_Type
(Formal_Typ
);
3994 if Is_Concurrent_Type
(Desig_Typ
)
3995 and then Present
(Corresponding_Record_Type
(Desig_Typ
))
3997 Rec_Typ
:= Corresponding_Record_Type
(Desig_Typ
);
3999 if Present
(Interfaces
(Rec_Typ
)) then
4000 Set_Directly_Designated_Type
(Formal_Typ
, Rec_Typ
);
4005 Next_Formal
(Formal
);
4012 -- The subprogram scope is pushed and popped around the processing of
4013 -- the return type for consistency with call above to Process_Formals
4014 -- (which itself can call Analyze_Return_Type), and to ensure that any
4015 -- itype created for the return type will be associated with the proper
4018 elsif Nkind
(N
) = N_Function_Specification
then
4019 Push_Scope
(Designator
);
4020 Analyze_Return_Type
(N
);
4026 if Nkind
(N
) = N_Function_Specification
then
4028 -- Deal with operator symbol case
4030 if Nkind
(Designator
) = N_Defining_Operator_Symbol
then
4031 Valid_Operator_Definition
(Designator
);
4034 May_Need_Actuals
(Designator
);
4036 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4037 -- the subprogram is abstract also. This does not apply to renaming
4038 -- declarations, where abstractness is inherited, and to subprogram
4039 -- bodies generated for stream operations, which become renamings as
4042 -- In case of primitives associated with abstract interface types
4043 -- the check is applied later (see Analyze_Subprogram_Declaration).
4045 if not Nkind_In
(Original_Node
(Parent
(N
)),
4046 N_Subprogram_Renaming_Declaration
,
4047 N_Abstract_Subprogram_Declaration
,
4048 N_Formal_Abstract_Subprogram_Declaration
)
4050 if Is_Abstract_Type
(Etype
(Designator
))
4051 and then not Is_Interface
(Etype
(Designator
))
4054 ("function that returns abstract type must be abstract", N
);
4056 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4057 -- access result whose designated type is abstract.
4059 elsif Nkind
(Result_Definition
(N
)) = N_Access_Definition
4061 not Is_Class_Wide_Type
(Designated_Type
(Etype
(Designator
)))
4062 and then Is_Abstract_Type
(Designated_Type
(Etype
(Designator
)))
4063 and then Ada_Version
>= Ada_2012
4065 Error_Msg_N
("function whose access result designates "
4066 & "abstract type must be abstract", N
);
4072 end Analyze_Subprogram_Specification
;
4074 --------------------------
4075 -- Build_Body_To_Inline --
4076 --------------------------
4078 procedure Build_Body_To_Inline
(N
: Node_Id
; Subp
: Entity_Id
) is
4079 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
4080 Original_Body
: Node_Id
;
4081 Body_To_Analyze
: Node_Id
;
4082 Max_Size
: constant := 10;
4083 Stat_Count
: Integer := 0;
4085 function Has_Excluded_Declaration
(Decls
: List_Id
) return Boolean;
4086 -- Check for declarations that make inlining not worthwhile
4088 function Has_Excluded_Statement
(Stats
: List_Id
) return Boolean;
4089 -- Check for statements that make inlining not worthwhile: any tasking
4090 -- statement, nested at any level. Keep track of total number of
4091 -- elementary statements, as a measure of acceptable size.
4093 function Has_Pending_Instantiation
return Boolean;
4094 -- If some enclosing body contains instantiations that appear before the
4095 -- corresponding generic body, the enclosing body has a freeze node so
4096 -- that it can be elaborated after the generic itself. This might
4097 -- conflict with subsequent inlinings, so that it is unsafe to try to
4098 -- inline in such a case.
4100 function Has_Single_Return
return Boolean;
4101 -- In general we cannot inline functions that return unconstrained type.
4102 -- However, we can handle such functions if all return statements return
4103 -- a local variable that is the only declaration in the body of the
4104 -- function. In that case the call can be replaced by that local
4105 -- variable as is done for other inlined calls.
4107 procedure Remove_Pragmas
;
4108 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
4109 -- parameter has no meaning when the body is inlined and the formals
4110 -- are rewritten. Remove it from body to inline. The analysis of the
4111 -- non-inlined body will handle the pragma properly.
4113 function Uses_Secondary_Stack
(Bod
: Node_Id
) return Boolean;
4114 -- If the body of the subprogram includes a call that returns an
4115 -- unconstrained type, the secondary stack is involved, and it
4116 -- is not worth inlining.
4118 ------------------------------
4119 -- Has_Excluded_Declaration --
4120 ------------------------------
4122 function Has_Excluded_Declaration
(Decls
: List_Id
) return Boolean is
4125 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean;
4126 -- Nested subprograms make a given body ineligible for inlining, but
4127 -- we make an exception for instantiations of unchecked conversion.
4128 -- The body has not been analyzed yet, so check the name, and verify
4129 -- that the visible entity with that name is the predefined unit.
4131 -----------------------------
4132 -- Is_Unchecked_Conversion --
4133 -----------------------------
4135 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean is
4136 Id
: constant Node_Id
:= Name
(D
);
4140 if Nkind
(Id
) = N_Identifier
4141 and then Chars
(Id
) = Name_Unchecked_Conversion
4143 Conv
:= Current_Entity
(Id
);
4145 elsif Nkind_In
(Id
, N_Selected_Component
, N_Expanded_Name
)
4146 and then Chars
(Selector_Name
(Id
)) = Name_Unchecked_Conversion
4148 Conv
:= Current_Entity
(Selector_Name
(Id
));
4153 return Present
(Conv
)
4154 and then Is_Predefined_File_Name
4155 (Unit_File_Name
(Get_Source_Unit
(Conv
)))
4156 and then Is_Intrinsic_Subprogram
(Conv
);
4157 end Is_Unchecked_Conversion
;
4159 -- Start of processing for Has_Excluded_Declaration
4163 while Present
(D
) loop
4164 if (Nkind
(D
) = N_Function_Instantiation
4165 and then not Is_Unchecked_Conversion
(D
))
4166 or else Nkind_In
(D
, N_Protected_Type_Declaration
,
4167 N_Package_Declaration
,
4168 N_Package_Instantiation
,
4170 N_Procedure_Instantiation
,
4171 N_Task_Type_Declaration
)
4174 ("cannot inline & (non-allowed declaration)?", D
, Subp
);
4182 end Has_Excluded_Declaration
;
4184 ----------------------------
4185 -- Has_Excluded_Statement --
4186 ----------------------------
4188 function Has_Excluded_Statement
(Stats
: List_Id
) return Boolean is
4194 while Present
(S
) loop
4195 Stat_Count
:= Stat_Count
+ 1;
4197 if Nkind_In
(S
, N_Abort_Statement
,
4198 N_Asynchronous_Select
,
4199 N_Conditional_Entry_Call
,
4200 N_Delay_Relative_Statement
,
4201 N_Delay_Until_Statement
,
4206 ("cannot inline & (non-allowed statement)?", S
, Subp
);
4209 elsif Nkind
(S
) = N_Block_Statement
then
4210 if Present
(Declarations
(S
))
4211 and then Has_Excluded_Declaration
(Declarations
(S
))
4215 elsif Present
(Handled_Statement_Sequence
(S
))
4218 (Exception_Handlers
(Handled_Statement_Sequence
(S
)))
4220 Has_Excluded_Statement
4221 (Statements
(Handled_Statement_Sequence
(S
))))
4226 elsif Nkind
(S
) = N_Case_Statement
then
4227 E
:= First
(Alternatives
(S
));
4228 while Present
(E
) loop
4229 if Has_Excluded_Statement
(Statements
(E
)) then
4236 elsif Nkind
(S
) = N_If_Statement
then
4237 if Has_Excluded_Statement
(Then_Statements
(S
)) then
4241 if Present
(Elsif_Parts
(S
)) then
4242 E
:= First
(Elsif_Parts
(S
));
4243 while Present
(E
) loop
4244 if Has_Excluded_Statement
(Then_Statements
(E
)) then
4252 if Present
(Else_Statements
(S
))
4253 and then Has_Excluded_Statement
(Else_Statements
(S
))
4258 elsif Nkind
(S
) = N_Loop_Statement
4259 and then Has_Excluded_Statement
(Statements
(S
))
4263 elsif Nkind
(S
) = N_Extended_Return_Statement
then
4264 if Has_Excluded_Statement
4265 (Statements
(Handled_Statement_Sequence
(S
)))
4267 (Exception_Handlers
(Handled_Statement_Sequence
(S
)))
4277 end Has_Excluded_Statement
;
4279 -------------------------------
4280 -- Has_Pending_Instantiation --
4281 -------------------------------
4283 function Has_Pending_Instantiation
return Boolean is
4288 while Present
(S
) loop
4289 if Is_Compilation_Unit
(S
)
4290 or else Is_Child_Unit
(S
)
4294 elsif Ekind
(S
) = E_Package
4295 and then Has_Forward_Instantiation
(S
)
4304 end Has_Pending_Instantiation
;
4306 ------------------------
4307 -- Has_Single_Return --
4308 ------------------------
4310 function Has_Single_Return
return Boolean is
4311 Return_Statement
: Node_Id
:= Empty
;
4313 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
4319 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
4321 if Nkind
(N
) = N_Simple_Return_Statement
then
4322 if Present
(Expression
(N
))
4323 and then Is_Entity_Name
(Expression
(N
))
4325 if No
(Return_Statement
) then
4326 Return_Statement
:= N
;
4329 elsif Chars
(Expression
(N
)) =
4330 Chars
(Expression
(Return_Statement
))
4338 -- A return statement within an extended return is a noop
4341 elsif No
(Expression
(N
))
4342 and then Nkind
(Parent
(Parent
(N
))) =
4343 N_Extended_Return_Statement
4348 -- Expression has wrong form
4353 -- We can only inline a build-in-place function if
4354 -- it has a single extended return.
4356 elsif Nkind
(N
) = N_Extended_Return_Statement
then
4357 if No
(Return_Statement
) then
4358 Return_Statement
:= N
;
4370 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
4372 -- Start of processing for Has_Single_Return
4375 if Check_All_Returns
(N
) /= OK
then
4378 elsif Nkind
(Return_Statement
) = N_Extended_Return_Statement
then
4382 return Present
(Declarations
(N
))
4383 and then Present
(First
(Declarations
(N
)))
4384 and then Chars
(Expression
(Return_Statement
)) =
4385 Chars
(Defining_Identifier
(First
(Declarations
(N
))));
4387 end Has_Single_Return
;
4389 --------------------
4390 -- Remove_Pragmas --
4391 --------------------
4393 procedure Remove_Pragmas
is
4398 Decl
:= First
(Declarations
(Body_To_Analyze
));
4399 while Present
(Decl
) loop
4402 if Nkind
(Decl
) = N_Pragma
4403 and then Nam_In
(Pragma_Name
(Decl
), Name_Unreferenced
,
4413 --------------------------
4414 -- Uses_Secondary_Stack --
4415 --------------------------
4417 function Uses_Secondary_Stack
(Bod
: Node_Id
) return Boolean is
4418 function Check_Call
(N
: Node_Id
) return Traverse_Result
;
4419 -- Look for function calls that return an unconstrained type
4425 function Check_Call
(N
: Node_Id
) return Traverse_Result
is
4427 if Nkind
(N
) = N_Function_Call
4428 and then Is_Entity_Name
(Name
(N
))
4429 and then Is_Composite_Type
(Etype
(Entity
(Name
(N
))))
4430 and then not Is_Constrained
(Etype
(Entity
(Name
(N
))))
4433 ("cannot inline & (call returns unconstrained type)?",
4441 function Check_Calls
is new Traverse_Func
(Check_Call
);
4444 return Check_Calls
(Bod
) = Abandon
;
4445 end Uses_Secondary_Stack
;
4447 -- Start of processing for Build_Body_To_Inline
4450 -- Return immediately if done already
4452 if Nkind
(Decl
) = N_Subprogram_Declaration
4453 and then Present
(Body_To_Inline
(Decl
))
4457 -- Functions that return unconstrained composite types require
4458 -- secondary stack handling, and cannot currently be inlined, unless
4459 -- all return statements return a local variable that is the first
4460 -- local declaration in the body.
4462 elsif Ekind
(Subp
) = E_Function
4463 and then not Is_Scalar_Type
(Etype
(Subp
))
4464 and then not Is_Access_Type
(Etype
(Subp
))
4465 and then not Is_Constrained
(Etype
(Subp
))
4467 if not Has_Single_Return
then
4469 ("cannot inline & (unconstrained return type)?", N
, Subp
);
4473 -- Ditto for functions that return controlled types, where controlled
4474 -- actions interfere in complex ways with inlining.
4476 elsif Ekind
(Subp
) = E_Function
4477 and then Needs_Finalization
(Etype
(Subp
))
4480 ("cannot inline & (controlled return type)?", N
, Subp
);
4484 if Present
(Declarations
(N
))
4485 and then Has_Excluded_Declaration
(Declarations
(N
))
4490 if Present
(Handled_Statement_Sequence
(N
)) then
4491 if Present
(Exception_Handlers
(Handled_Statement_Sequence
(N
))) then
4493 ("cannot inline& (exception handler)?",
4494 First
(Exception_Handlers
(Handled_Statement_Sequence
(N
))),
4498 Has_Excluded_Statement
4499 (Statements
(Handled_Statement_Sequence
(N
)))
4505 -- We do not inline a subprogram that is too large, unless it is
4506 -- marked Inline_Always. This pragma does not suppress the other
4507 -- checks on inlining (forbidden declarations, handlers, etc).
4509 if Stat_Count
> Max_Size
4510 and then not Has_Pragma_Inline_Always
(Subp
)
4512 Cannot_Inline
("cannot inline& (body too large)?", N
, Subp
);
4516 if Has_Pending_Instantiation
then
4518 ("cannot inline& (forward instance within enclosing body)?",
4523 -- Within an instance, the body to inline must be treated as a nested
4524 -- generic, so that the proper global references are preserved.
4526 -- Note that we do not do this at the library level, because it is not
4527 -- needed, and furthermore this causes trouble if front end inlining
4528 -- is activated (-gnatN).
4530 if In_Instance
and then Scope
(Current_Scope
) /= Standard_Standard
then
4531 Save_Env
(Scope
(Current_Scope
), Scope
(Current_Scope
));
4532 Original_Body
:= Copy_Generic_Node
(N
, Empty
, True);
4534 Original_Body
:= Copy_Separate_Tree
(N
);
4537 -- We need to capture references to the formals in order to substitute
4538 -- the actuals at the point of inlining, i.e. instantiation. To treat
4539 -- the formals as globals to the body to inline, we nest it within
4540 -- a dummy parameterless subprogram, declared within the real one.
4541 -- To avoid generating an internal name (which is never public, and
4542 -- which affects serial numbers of other generated names), we use
4543 -- an internal symbol that cannot conflict with user declarations.
4545 Set_Parameter_Specifications
(Specification
(Original_Body
), No_List
);
4546 Set_Defining_Unit_Name
4547 (Specification
(Original_Body
),
4548 Make_Defining_Identifier
(Sloc
(N
), Name_uParent
));
4549 Set_Corresponding_Spec
(Original_Body
, Empty
);
4551 Body_To_Analyze
:= Copy_Generic_Node
(Original_Body
, Empty
, False);
4553 -- Set return type of function, which is also global and does not need
4556 if Ekind
(Subp
) = E_Function
then
4557 Set_Result_Definition
(Specification
(Body_To_Analyze
),
4558 New_Occurrence_Of
(Etype
(Subp
), Sloc
(N
)));
4561 if No
(Declarations
(N
)) then
4562 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
4564 Append
(Body_To_Analyze
, Declarations
(N
));
4567 Expander_Mode_Save_And_Set
(False);
4570 Analyze
(Body_To_Analyze
);
4571 Push_Scope
(Defining_Entity
(Body_To_Analyze
));
4572 Save_Global_References
(Original_Body
);
4574 Remove
(Body_To_Analyze
);
4576 Expander_Mode_Restore
;
4578 -- Restore environment if previously saved
4580 if In_Instance
and then Scope
(Current_Scope
) /= Standard_Standard
then
4584 -- If secondary stk used there is no point in inlining. We have
4585 -- already issued the warning in this case, so nothing to do.
4587 if Uses_Secondary_Stack
(Body_To_Analyze
) then
4591 Set_Body_To_Inline
(Decl
, Original_Body
);
4592 Set_Ekind
(Defining_Entity
(Original_Body
), Ekind
(Subp
));
4593 Set_Is_Inlined
(Subp
);
4594 end Build_Body_To_Inline
;
4600 procedure Cannot_Inline
4604 Is_Serious
: Boolean := False)
4607 pragma Assert
(Msg
(Msg
'Last) = '?');
4611 if not Debug_Flag_Dot_K
then
4613 -- Do not emit warning if this is a predefined unit which is not
4614 -- the main unit. With validity checks enabled, some predefined
4615 -- subprograms may contain nested subprograms and become ineligible
4618 if Is_Predefined_File_Name
(Unit_File_Name
(Get_Source_Unit
(Subp
)))
4619 and then not In_Extended_Main_Source_Unit
(Subp
)
4623 elsif Has_Pragma_Inline_Always
(Subp
) then
4625 -- Remove last character (question mark) to make this into an
4626 -- error, because the Inline_Always pragma cannot be obeyed.
4628 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
4630 elsif Ineffective_Inline_Warnings
then
4631 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
4638 elsif Is_Serious
then
4640 -- Remove last character (question mark) to make this into an error.
4642 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
4644 elsif Optimization_Level
= 0 then
4646 -- Do not emit warning if this is a predefined unit which is not
4647 -- the main unit. This behavior is currently provided for backward
4648 -- compatibility but it will be removed when we enforce the
4649 -- strictness of the new rules.
4651 if Is_Predefined_File_Name
(Unit_File_Name
(Get_Source_Unit
(Subp
)))
4652 and then not In_Extended_Main_Source_Unit
(Subp
)
4656 elsif Has_Pragma_Inline_Always
(Subp
) then
4658 -- Emit a warning if this is a call to a runtime subprogram
4659 -- which is located inside a generic. Previously this call
4660 -- was silently skipped!
4662 if Is_Generic_Instance
(Subp
) then
4664 Gen_P
: constant Entity_Id
:= Generic_Parent
(Parent
(Subp
));
4666 if Is_Predefined_File_Name
4667 (Unit_File_Name
(Get_Source_Unit
(Gen_P
)))
4669 Set_Is_Inlined
(Subp
, False);
4670 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
4676 -- Remove last character (question mark) to make this into an
4677 -- error, because the Inline_Always pragma cannot be obeyed.
4679 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
4681 else pragma Assert
(Front_End_Inlining
);
4682 Set_Is_Inlined
(Subp
, False);
4684 -- When inlining cannot take place we must issue an error.
4685 -- For backward compatibility we still report a warning.
4687 if Ineffective_Inline_Warnings
then
4688 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
4692 -- Compiling with optimizations enabled it is too early to report
4693 -- problems since the backend may still perform inlining. In order
4694 -- to report unhandled inlinings the program must be compiled with
4695 -- -Winline and the error is reported by the backend.
4702 ------------------------------------
4703 -- Check_And_Build_Body_To_Inline --
4704 ------------------------------------
4706 procedure Check_And_Build_Body_To_Inline
4708 Spec_Id
: Entity_Id
;
4709 Body_Id
: Entity_Id
)
4711 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
);
4712 -- Use generic machinery to build an unexpanded body for the subprogram.
4713 -- This body is subsequently used for inline expansions at call sites.
4715 function Can_Split_Unconstrained_Function
(N
: Node_Id
) return Boolean;
4716 -- Return true if we generate code for the function body N, the function
4717 -- body N has no local declarations and its unique statement is a single
4718 -- extended return statement with a handled statements sequence.
4720 function Check_Body_To_Inline
4722 Subp
: Entity_Id
) return Boolean;
4723 -- N is the N_Subprogram_Body of Subp. Return true if Subp can be
4724 -- inlined by the frontend. These are the rules:
4725 -- * At -O0 use fe inlining when inline_always is specified except if
4726 -- the function returns a controlled type.
4727 -- * At other optimization levels use the fe inlining for both inline
4728 -- and inline_always in the following cases:
4729 -- - function returning a known at compile time constant
4730 -- - function returning a call to an intrinsic function
4731 -- - function returning an unconstrained type (see Can_Split
4732 -- Unconstrained_Function).
4733 -- - function returning a call to a frontend-inlined function
4734 -- Use the back-end mechanism otherwise
4736 -- In addition, in the following cases the function cannot be inlined by
4738 -- - functions that uses the secondary stack
4739 -- - functions that have declarations of:
4740 -- - Concurrent types
4744 -- - functions that have some of the following statements:
4746 -- - asynchronous-select
4747 -- - conditional-entry-call
4750 -- - selective-accept
4751 -- - timed-entry-call
4752 -- - functions that have exception handlers
4753 -- - functions that have some enclosing body containing instantiations
4754 -- that appear before the corresponding generic body.
4756 procedure Generate_Body_To_Inline
4758 Body_To_Inline
: out Node_Id
);
4759 -- Generate a parameterless duplicate of subprogram body N. Occurrences
4760 -- of pragmas referencing the formals are removed since they have no
4761 -- meaning when the body is inlined and the formals are rewritten (the
4762 -- analysis of the non-inlined body will handle these pragmas properly).
4763 -- A new internal name is associated with Body_To_Inline.
4765 procedure Split_Unconstrained_Function
4767 Spec_Id
: Entity_Id
);
4768 -- N is an inlined function body that returns an unconstrained type and
4769 -- has a single extended return statement. Split N in two subprograms:
4770 -- a procedure P' and a function F'. The formals of P' duplicate the
4771 -- formals of N plus an extra formal which is used return a value;
4772 -- its body is composed by the declarations and list of statements
4773 -- of the extended return statement of N.
4775 --------------------------
4776 -- Build_Body_To_Inline --
4777 --------------------------
4779 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
4780 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
4781 Original_Body
: Node_Id
;
4782 Body_To_Analyze
: Node_Id
;
4785 pragma Assert
(Current_Scope
= Spec_Id
);
4787 -- Within an instance, the body to inline must be treated as a nested
4788 -- generic, so that the proper global references are preserved. We
4789 -- do not do this at the library level, because it is not needed, and
4790 -- furthermore this causes trouble if front end inlining is activated
4794 and then Scope
(Current_Scope
) /= Standard_Standard
4796 Save_Env
(Scope
(Current_Scope
), Scope
(Current_Scope
));
4799 -- We need to capture references to the formals in order
4800 -- to substitute the actuals at the point of inlining, i.e.
4801 -- instantiation. To treat the formals as globals to the body to
4802 -- inline, we nest it within a dummy parameterless subprogram,
4803 -- declared within the real one.
4805 Generate_Body_To_Inline
(N
, Original_Body
);
4806 Body_To_Analyze
:= Copy_Generic_Node
(Original_Body
, Empty
, False);
4808 -- Set return type of function, which is also global and does not
4809 -- need to be resolved.
4811 if Ekind
(Spec_Id
) = E_Function
then
4812 Set_Result_Definition
(Specification
(Body_To_Analyze
),
4813 New_Occurrence_Of
(Etype
(Spec_Id
), Sloc
(N
)));
4816 if No
(Declarations
(N
)) then
4817 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
4819 Append_To
(Declarations
(N
), Body_To_Analyze
);
4822 Preanalyze
(Body_To_Analyze
);
4824 Push_Scope
(Defining_Entity
(Body_To_Analyze
));
4825 Save_Global_References
(Original_Body
);
4827 Remove
(Body_To_Analyze
);
4829 -- Restore environment if previously saved
4832 and then Scope
(Current_Scope
) /= Standard_Standard
4837 pragma Assert
(No
(Body_To_Inline
(Decl
)));
4838 Set_Body_To_Inline
(Decl
, Original_Body
);
4839 Set_Ekind
(Defining_Entity
(Original_Body
), Ekind
(Spec_Id
));
4840 end Build_Body_To_Inline
;
4842 --------------------------
4843 -- Check_Body_To_Inline --
4844 --------------------------
4846 function Check_Body_To_Inline
4848 Subp
: Entity_Id
) return Boolean
4850 Max_Size
: constant := 10;
4851 Stat_Count
: Integer := 0;
4853 function Has_Excluded_Declaration
(Decls
: List_Id
) return Boolean;
4854 -- Check for declarations that make inlining not worthwhile
4856 function Has_Excluded_Statement
(Stats
: List_Id
) return Boolean;
4857 -- Check for statements that make inlining not worthwhile: any
4858 -- tasking statement, nested at any level. Keep track of total
4859 -- number of elementary statements, as a measure of acceptable size.
4861 function Has_Pending_Instantiation
return Boolean;
4862 -- Return True if some enclosing body contains instantiations that
4863 -- appear before the corresponding generic body.
4865 function Returns_Compile_Time_Constant
(N
: Node_Id
) return Boolean;
4866 -- Return True if all the return statements of the function body N
4867 -- are simple return statements and return a compile time constant
4869 function Returns_Intrinsic_Function_Call
(N
: Node_Id
) return Boolean;
4870 -- Return True if all the return statements of the function body N
4871 -- are simple return statements and return an intrinsic function call
4873 function Uses_Secondary_Stack
(N
: Node_Id
) return Boolean;
4874 -- If the body of the subprogram includes a call that returns an
4875 -- unconstrained type, the secondary stack is involved, and it
4876 -- is not worth inlining.
4878 ------------------------------
4879 -- Has_Excluded_Declaration --
4880 ------------------------------
4882 function Has_Excluded_Declaration
(Decls
: List_Id
) return Boolean is
4885 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean;
4886 -- Nested subprograms make a given body ineligible for inlining,
4887 -- but we make an exception for instantiations of unchecked
4888 -- conversion. The body has not been analyzed yet, so check the
4889 -- name, and verify that the visible entity with that name is the
4892 -----------------------------
4893 -- Is_Unchecked_Conversion --
4894 -----------------------------
4896 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean is
4897 Id
: constant Node_Id
:= Name
(D
);
4901 if Nkind
(Id
) = N_Identifier
4902 and then Chars
(Id
) = Name_Unchecked_Conversion
4904 Conv
:= Current_Entity
(Id
);
4906 elsif Nkind_In
(Id
, N_Selected_Component
, N_Expanded_Name
)
4908 Chars
(Selector_Name
(Id
)) = Name_Unchecked_Conversion
4910 Conv
:= Current_Entity
(Selector_Name
(Id
));
4915 return Present
(Conv
)
4916 and then Is_Predefined_File_Name
4917 (Unit_File_Name
(Get_Source_Unit
(Conv
)))
4918 and then Is_Intrinsic_Subprogram
(Conv
);
4919 end Is_Unchecked_Conversion
;
4921 -- Start of processing for Has_Excluded_Declaration
4925 while Present
(D
) loop
4926 if (Nkind
(D
) = N_Function_Instantiation
4927 and then not Is_Unchecked_Conversion
(D
))
4928 or else Nkind_In
(D
, N_Protected_Type_Declaration
,
4929 N_Package_Declaration
,
4930 N_Package_Instantiation
,
4932 N_Procedure_Instantiation
,
4933 N_Task_Type_Declaration
)
4936 ("cannot inline & (non-allowed declaration)?", D
, Subp
);
4945 end Has_Excluded_Declaration
;
4947 ----------------------------
4948 -- Has_Excluded_Statement --
4949 ----------------------------
4951 function Has_Excluded_Statement
(Stats
: List_Id
) return Boolean is
4957 while Present
(S
) loop
4958 Stat_Count
:= Stat_Count
+ 1;
4960 if Nkind_In
(S
, N_Abort_Statement
,
4961 N_Asynchronous_Select
,
4962 N_Conditional_Entry_Call
,
4963 N_Delay_Relative_Statement
,
4964 N_Delay_Until_Statement
,
4969 ("cannot inline & (non-allowed statement)?", S
, Subp
);
4972 elsif Nkind
(S
) = N_Block_Statement
then
4973 if Present
(Declarations
(S
))
4974 and then Has_Excluded_Declaration
(Declarations
(S
))
4978 elsif Present
(Handled_Statement_Sequence
(S
)) then
4980 (Exception_Handlers
(Handled_Statement_Sequence
(S
)))
4983 ("cannot inline& (exception handler)?",
4984 First
(Exception_Handlers
4985 (Handled_Statement_Sequence
(S
))),
4989 elsif Has_Excluded_Statement
4990 (Statements
(Handled_Statement_Sequence
(S
)))
4996 elsif Nkind
(S
) = N_Case_Statement
then
4997 E
:= First
(Alternatives
(S
));
4998 while Present
(E
) loop
4999 if Has_Excluded_Statement
(Statements
(E
)) then
5006 elsif Nkind
(S
) = N_If_Statement
then
5007 if Has_Excluded_Statement
(Then_Statements
(S
)) then
5011 if Present
(Elsif_Parts
(S
)) then
5012 E
:= First
(Elsif_Parts
(S
));
5013 while Present
(E
) loop
5014 if Has_Excluded_Statement
(Then_Statements
(E
)) then
5021 if Present
(Else_Statements
(S
))
5022 and then Has_Excluded_Statement
(Else_Statements
(S
))
5027 elsif Nkind
(S
) = N_Loop_Statement
5028 and then Has_Excluded_Statement
(Statements
(S
))
5032 elsif Nkind
(S
) = N_Extended_Return_Statement
then
5033 if Present
(Handled_Statement_Sequence
(S
))
5035 Has_Excluded_Statement
5036 (Statements
(Handled_Statement_Sequence
(S
)))
5040 elsif Present
(Handled_Statement_Sequence
(S
))
5042 Present
(Exception_Handlers
5043 (Handled_Statement_Sequence
(S
)))
5046 ("cannot inline& (exception handler)?",
5047 First
(Exception_Handlers
5048 (Handled_Statement_Sequence
(S
))),
5058 end Has_Excluded_Statement
;
5060 -------------------------------
5061 -- Has_Pending_Instantiation --
5062 -------------------------------
5064 function Has_Pending_Instantiation
return Boolean is
5069 while Present
(S
) loop
5070 if Is_Compilation_Unit
(S
)
5071 or else Is_Child_Unit
(S
)
5075 elsif Ekind
(S
) = E_Package
5076 and then Has_Forward_Instantiation
(S
)
5085 end Has_Pending_Instantiation
;
5087 ------------------------------------
5088 -- Returns_Compile_Time_Constant --
5089 ------------------------------------
5091 function Returns_Compile_Time_Constant
(N
: Node_Id
) return Boolean is
5093 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
5099 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
5101 if Nkind
(N
) = N_Extended_Return_Statement
then
5104 elsif Nkind
(N
) = N_Simple_Return_Statement
then
5105 if Present
(Expression
(N
)) then
5107 Orig_Expr
: constant Node_Id
:=
5108 Original_Node
(Expression
(N
));
5111 if Nkind_In
(Orig_Expr
, N_Integer_Literal
,
5113 N_Character_Literal
)
5117 elsif Is_Entity_Name
(Orig_Expr
)
5118 and then Ekind
(Entity
(Orig_Expr
)) = E_Constant
5119 and then Is_Static_Expression
(Orig_Expr
)
5127 -- Expression has wrong form
5133 -- Continue analyzing statements
5140 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
5142 -- Start of processing for Returns_Compile_Time_Constant
5145 return Check_All_Returns
(N
) = OK
;
5146 end Returns_Compile_Time_Constant
;
5148 --------------------------------------
5149 -- Returns_Intrinsic_Function_Call --
5150 --------------------------------------
5152 function Returns_Intrinsic_Function_Call
5153 (N
: Node_Id
) return Boolean
5155 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
5161 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
5163 if Nkind
(N
) = N_Extended_Return_Statement
then
5166 elsif Nkind
(N
) = N_Simple_Return_Statement
then
5167 if Present
(Expression
(N
)) then
5169 Orig_Expr
: constant Node_Id
:=
5170 Original_Node
(Expression
(N
));
5173 if Nkind
(Orig_Expr
) in N_Op
5174 and then Is_Intrinsic_Subprogram
(Entity
(Orig_Expr
))
5178 elsif Nkind
(Orig_Expr
) in N_Has_Entity
5179 and then Present
(Entity
(Orig_Expr
))
5180 and then Ekind
(Entity
(Orig_Expr
)) = E_Function
5181 and then Is_Inlined
(Entity
(Orig_Expr
))
5185 elsif Nkind
(Orig_Expr
) in N_Has_Entity
5186 and then Present
(Entity
(Orig_Expr
))
5187 and then Is_Intrinsic_Subprogram
(Entity
(Orig_Expr
))
5196 -- Expression has wrong form
5202 -- Continue analyzing statements
5209 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
5211 -- Start of processing for Returns_Intrinsic_Function_Call
5214 return Check_All_Returns
(N
) = OK
;
5215 end Returns_Intrinsic_Function_Call
;
5217 --------------------------
5218 -- Uses_Secondary_Stack --
5219 --------------------------
5221 function Uses_Secondary_Stack
(N
: Node_Id
) return Boolean is
5223 function Check_Call
(N
: Node_Id
) return Traverse_Result
;
5224 -- Look for function calls that return an unconstrained type
5230 function Check_Call
(N
: Node_Id
) return Traverse_Result
is
5232 if Nkind
(N
) = N_Function_Call
5233 and then Is_Entity_Name
(Name
(N
))
5234 and then Is_Composite_Type
(Etype
(Entity
(Name
(N
))))
5235 and then not Is_Constrained
(Etype
(Entity
(Name
(N
))))
5238 ("cannot inline & (call returns unconstrained type)?",
5247 function Check_Calls
is new Traverse_Func
(Check_Call
);
5249 -- Start of processing for Uses_Secondary_Stack
5252 return Check_Calls
(N
) = Abandon
;
5253 end Uses_Secondary_Stack
;
5257 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
5258 May_Inline
: constant Boolean :=
5259 Has_Pragma_Inline_Always
(Spec_Id
)
5260 or else (Has_Pragma_Inline
(Spec_Id
)
5261 and then ((Optimization_Level
> 0
5262 and then Ekind
(Spec_Id
)
5264 or else Front_End_Inlining
));
5265 Body_To_Analyze
: Node_Id
;
5267 -- Start of processing for Check_Body_To_Inline
5270 -- No action needed in stubs since the attribute Body_To_Inline
5273 if Nkind
(Decl
) = N_Subprogram_Body_Stub
then
5276 -- Cannot build the body to inline if the attribute is already set.
5277 -- This attribute may have been set if this is a subprogram renaming
5278 -- declarations (see Freeze.Build_Renamed_Body).
5280 elsif Present
(Body_To_Inline
(Decl
)) then
5283 -- No action needed if the subprogram does not fulfill the minimum
5284 -- conditions to be inlined by the frontend
5286 elsif not May_Inline
then
5290 -- Check excluded declarations
5292 if Present
(Declarations
(N
))
5293 and then Has_Excluded_Declaration
(Declarations
(N
))
5298 -- Check excluded statements
5300 if Present
(Handled_Statement_Sequence
(N
)) then
5302 (Exception_Handlers
(Handled_Statement_Sequence
(N
)))
5305 ("cannot inline& (exception handler)?",
5307 (Exception_Handlers
(Handled_Statement_Sequence
(N
))),
5312 elsif Has_Excluded_Statement
5313 (Statements
(Handled_Statement_Sequence
(N
)))
5319 -- For backward compatibility, compiling under -gnatN we do not
5320 -- inline a subprogram that is too large, unless it is marked
5321 -- Inline_Always. This pragma does not suppress the other checks
5322 -- on inlining (forbidden declarations, handlers, etc).
5324 if Front_End_Inlining
5325 and then not Has_Pragma_Inline_Always
(Subp
)
5326 and then Stat_Count
> Max_Size
5328 Cannot_Inline
("cannot inline& (body too large)?", N
, Subp
);
5332 -- If some enclosing body contains instantiations that appear before
5333 -- the corresponding generic body, the enclosing body has a freeze
5334 -- node so that it can be elaborated after the generic itself. This
5335 -- might conflict with subsequent inlinings, so that it is unsafe to
5336 -- try to inline in such a case.
5338 if Has_Pending_Instantiation
then
5340 ("cannot inline& (forward instance within enclosing body)?",
5346 -- Generate and preanalyze the body to inline (needed to perform
5347 -- the rest of the checks)
5349 Generate_Body_To_Inline
(N
, Body_To_Analyze
);
5351 if Ekind
(Subp
) = E_Function
then
5352 Set_Result_Definition
(Specification
(Body_To_Analyze
),
5353 New_Occurrence_Of
(Etype
(Subp
), Sloc
(N
)));
5356 -- Nest the body to analyze within the real one
5358 if No
(Declarations
(N
)) then
5359 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
5361 Append_To
(Declarations
(N
), Body_To_Analyze
);
5364 Preanalyze
(Body_To_Analyze
);
5365 Remove
(Body_To_Analyze
);
5367 -- Keep separate checks needed when compiling without optimizations
5369 if Optimization_Level
= 0
5371 -- AAMP and VM targets have no support for inlining in the backend
5372 -- and hence we use frontend inlining at all optimization levels.
5374 or else AAMP_On_Target
5375 or else VM_Target
/= No_VM
5377 -- Cannot inline functions whose body has a call that returns an
5378 -- unconstrained type since the secondary stack is involved, and
5379 -- it is not worth inlining.
5381 if Uses_Secondary_Stack
(Body_To_Analyze
) then
5384 -- Cannot inline functions that return controlled types since
5385 -- controlled actions interfere in complex ways with inlining.
5387 elsif Ekind
(Subp
) = E_Function
5388 and then Needs_Finalization
(Etype
(Subp
))
5391 ("cannot inline & (controlled return type)?", N
, Subp
);
5394 elsif Returns_Unconstrained_Type
(Subp
) then
5396 ("cannot inline & (unconstrained return type)?", N
, Subp
);
5400 -- Compiling with optimizations enabled
5403 -- Procedures are never frontend inlined in this case!
5405 if Ekind
(Subp
) /= E_Function
then
5408 -- Functions returning unconstrained types are tested
5409 -- separately (see Can_Split_Unconstrained_Function).
5411 elsif Returns_Unconstrained_Type
(Subp
) then
5414 -- Check supported cases
5416 elsif not Returns_Compile_Time_Constant
(Body_To_Analyze
)
5417 and then Convention
(Subp
) /= Convention_Intrinsic
5418 and then not Returns_Intrinsic_Function_Call
(Body_To_Analyze
)
5425 end Check_Body_To_Inline
;
5427 --------------------------------------
5428 -- Can_Split_Unconstrained_Function --
5429 --------------------------------------
5431 function Can_Split_Unconstrained_Function
(N
: Node_Id
) return Boolean
5433 Ret_Node
: constant Node_Id
:=
5434 First
(Statements
(Handled_Statement_Sequence
(N
)));
5438 -- No user defined declarations allowed in the function except inside
5439 -- the unique return statement; implicit labels are the only allowed
5442 if not Is_Empty_List
(Declarations
(N
)) then
5443 D
:= First
(Declarations
(N
));
5444 while Present
(D
) loop
5445 if Nkind
(D
) /= N_Implicit_Label_Declaration
then
5453 -- We only split the inlined function when we are generating the code
5454 -- of its body; otherwise we leave duplicated split subprograms in
5455 -- the tree which (if referenced) generate wrong references at link
5458 return In_Extended_Main_Code_Unit
(N
)
5459 and then Present
(Ret_Node
)
5460 and then Nkind
(Ret_Node
) = N_Extended_Return_Statement
5461 and then No
(Next
(Ret_Node
))
5462 and then Present
(Handled_Statement_Sequence
(Ret_Node
));
5463 end Can_Split_Unconstrained_Function
;
5465 -----------------------------
5466 -- Generate_Body_To_Inline --
5467 -----------------------------
5469 procedure Generate_Body_To_Inline
5471 Body_To_Inline
: out Node_Id
)
5473 procedure Remove_Pragmas
(N
: Node_Id
);
5474 -- Remove occurrences of pragmas that may reference the formals of
5475 -- N. The analysis of the non-inlined body will handle these pragmas
5478 --------------------
5479 -- Remove_Pragmas --
5480 --------------------
5482 procedure Remove_Pragmas
(N
: Node_Id
) is
5487 Decl
:= First
(Declarations
(N
));
5488 while Present
(Decl
) loop
5491 if Nkind
(Decl
) = N_Pragma
5492 and then Nam_In
(Pragma_Name
(Decl
), Name_Unreferenced
,
5502 -- Start of processing for Generate_Body_To_Inline
5505 -- Within an instance, the body to inline must be treated as a nested
5506 -- generic, so that the proper global references are preserved.
5508 -- Note that we do not do this at the library level, because it
5509 -- is not needed, and furthermore this causes trouble if front
5510 -- end inlining is activated (-gnatN).
5513 and then Scope
(Current_Scope
) /= Standard_Standard
5515 Body_To_Inline
:= Copy_Generic_Node
(N
, Empty
, True);
5517 Body_To_Inline
:= Copy_Separate_Tree
(N
);
5520 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
5521 -- parameter has no meaning when the body is inlined and the formals
5522 -- are rewritten. Remove it from body to inline. The analysis of the
5523 -- non-inlined body will handle the pragma properly.
5525 Remove_Pragmas
(Body_To_Inline
);
5527 -- We need to capture references to the formals in order
5528 -- to substitute the actuals at the point of inlining, i.e.
5529 -- instantiation. To treat the formals as globals to the body to
5530 -- inline, we nest it within a dummy parameterless subprogram,
5531 -- declared within the real one.
5533 Set_Parameter_Specifications
5534 (Specification
(Body_To_Inline
), No_List
);
5536 -- A new internal name is associated with Body_To_Inline to avoid
5537 -- conflicts when the non-inlined body N is analyzed.
5539 Set_Defining_Unit_Name
(Specification
(Body_To_Inline
),
5540 Make_Defining_Identifier
(Sloc
(N
), New_Internal_Name
('P')));
5541 Set_Corresponding_Spec
(Body_To_Inline
, Empty
);
5542 end Generate_Body_To_Inline
;
5544 ----------------------------------
5545 -- Split_Unconstrained_Function --
5546 ----------------------------------
5548 procedure Split_Unconstrained_Function
5550 Spec_Id
: Entity_Id
)
5552 Loc
: constant Source_Ptr
:= Sloc
(N
);
5553 Ret_Node
: constant Node_Id
:=
5554 First
(Statements
(Handled_Statement_Sequence
(N
)));
5555 Ret_Obj
: constant Node_Id
:=
5556 First
(Return_Object_Declarations
(Ret_Node
));
5558 procedure Build_Procedure
5559 (Proc_Id
: out Entity_Id
;
5560 Decl_List
: out List_Id
);
5561 -- Build a procedure containing the statements found in the extended
5562 -- return statement of the unconstrained function body N.
5564 procedure Build_Procedure
5565 (Proc_Id
: out Entity_Id
;
5566 Decl_List
: out List_Id
)
5569 Formal_List
: constant List_Id
:= New_List
;
5570 Proc_Spec
: Node_Id
;
5571 Proc_Body
: Node_Id
;
5572 Subp_Name
: constant Name_Id
:= New_Internal_Name
('F');
5573 Body_Decl_List
: List_Id
:= No_List
;
5574 Param_Type
: Node_Id
;
5577 if Nkind
(Object_Definition
(Ret_Obj
)) = N_Identifier
then
5578 Param_Type
:= New_Copy
(Object_Definition
(Ret_Obj
));
5581 New_Copy
(Subtype_Mark
(Object_Definition
(Ret_Obj
)));
5584 Append_To
(Formal_List
,
5585 Make_Parameter_Specification
(Loc
,
5586 Defining_Identifier
=>
5587 Make_Defining_Identifier
(Loc
,
5588 Chars
=> Chars
(Defining_Identifier
(Ret_Obj
))),
5589 In_Present
=> False,
5590 Out_Present
=> True,
5591 Null_Exclusion_Present
=> False,
5592 Parameter_Type
=> Param_Type
));
5594 Formal
:= First_Formal
(Spec_Id
);
5595 while Present
(Formal
) loop
5596 Append_To
(Formal_List
,
5597 Make_Parameter_Specification
(Loc
,
5598 Defining_Identifier
=>
5599 Make_Defining_Identifier
(Sloc
(Formal
),
5600 Chars
=> Chars
(Formal
)),
5601 In_Present
=> In_Present
(Parent
(Formal
)),
5602 Out_Present
=> Out_Present
(Parent
(Formal
)),
5603 Null_Exclusion_Present
=>
5604 Null_Exclusion_Present
(Parent
(Formal
)),
5606 New_Reference_To
(Etype
(Formal
), Loc
),
5608 Copy_Separate_Tree
(Expression
(Parent
(Formal
)))));
5610 Next_Formal
(Formal
);
5614 Make_Defining_Identifier
(Loc
, Chars
=> Subp_Name
);
5617 Make_Procedure_Specification
(Loc
,
5618 Defining_Unit_Name
=> Proc_Id
,
5619 Parameter_Specifications
=> Formal_List
);
5621 Decl_List
:= New_List
;
5623 Append_To
(Decl_List
,
5624 Make_Subprogram_Declaration
(Loc
, Proc_Spec
));
5626 -- Can_Convert_Unconstrained_Function checked that the function
5627 -- has no local declarations except implicit label declarations.
5628 -- Copy these declarations to the built procedure.
5630 if Present
(Declarations
(N
)) then
5631 Body_Decl_List
:= New_List
;
5638 D
:= First
(Declarations
(N
));
5639 while Present
(D
) loop
5640 pragma Assert
(Nkind
(D
) = N_Implicit_Label_Declaration
);
5643 Make_Implicit_Label_Declaration
(Loc
,
5644 Make_Defining_Identifier
(Loc
,
5645 Chars
=> Chars
(Defining_Identifier
(D
))),
5646 Label_Construct
=> Empty
);
5647 Append_To
(Body_Decl_List
, New_D
);
5654 pragma Assert
(Present
(Handled_Statement_Sequence
(Ret_Node
)));
5657 Make_Subprogram_Body
(Loc
,
5658 Specification
=> Copy_Separate_Tree
(Proc_Spec
),
5659 Declarations
=> Body_Decl_List
,
5660 Handled_Statement_Sequence
=>
5661 Copy_Separate_Tree
(Handled_Statement_Sequence
(Ret_Node
)));
5663 Set_Defining_Unit_Name
(Specification
(Proc_Body
),
5664 Make_Defining_Identifier
(Loc
, Subp_Name
));
5666 Append_To
(Decl_List
, Proc_Body
);
5667 end Build_Procedure
;
5671 New_Obj
: constant Node_Id
:= Copy_Separate_Tree
(Ret_Obj
);
5673 Proc_Id
: Entity_Id
;
5674 Proc_Call
: Node_Id
;
5676 -- Start of processing for Split_Unconstrained_Function
5679 -- Build the associated procedure, analyze it and insert it before
5680 -- the function body N
5683 Scope
: constant Entity_Id
:= Current_Scope
;
5684 Decl_List
: List_Id
;
5687 Build_Procedure
(Proc_Id
, Decl_List
);
5688 Insert_Actions
(N
, Decl_List
);
5692 -- Build the call to the generated procedure
5695 Actual_List
: constant List_Id
:= New_List
;
5699 Append_To
(Actual_List
,
5700 New_Reference_To
(Defining_Identifier
(New_Obj
), Loc
));
5702 Formal
:= First_Formal
(Spec_Id
);
5703 while Present
(Formal
) loop
5704 Append_To
(Actual_List
, New_Reference_To
(Formal
, Loc
));
5706 -- Avoid spurious warning on unreferenced formals
5708 Set_Referenced
(Formal
);
5709 Next_Formal
(Formal
);
5713 Make_Procedure_Call_Statement
(Loc
,
5714 Name
=> New_Reference_To
(Proc_Id
, Loc
),
5715 Parameter_Associations
=> Actual_List
);
5723 -- main_1__F1b (New_Obj, ...);
5728 Make_Block_Statement
(Loc
,
5729 Declarations
=> New_List
(New_Obj
),
5730 Handled_Statement_Sequence
=>
5731 Make_Handled_Sequence_Of_Statements
(Loc
,
5732 Statements
=> New_List
(
5736 Make_Simple_Return_Statement
(Loc
,
5739 (Defining_Identifier
(New_Obj
), Loc
)))));
5741 Rewrite
(Ret_Node
, Blk_Stmt
);
5742 end Split_Unconstrained_Function
;
5744 -- Start of processing for Check_And_Build_Body_To_Inline
5747 -- Do not inline any subprogram that contains nested subprograms, since
5748 -- the backend inlining circuit seems to generate uninitialized
5749 -- references in this case. We know this happens in the case of front
5750 -- end ZCX support, but it also appears it can happen in other cases as
5751 -- well. The backend often rejects attempts to inline in the case of
5752 -- nested procedures anyway, so little if anything is lost by this.
5753 -- Note that this is test is for the benefit of the back-end. There is
5754 -- a separate test for front-end inlining that also rejects nested
5757 -- Do not do this test if errors have been detected, because in some
5758 -- error cases, this code blows up, and we don't need it anyway if
5759 -- there have been errors, since we won't get to the linker anyway.
5761 if Comes_From_Source
(Body_Id
)
5762 and then (Has_Pragma_Inline_Always
(Spec_Id
)
5763 or else Optimization_Level
> 0)
5764 and then Serious_Errors_Detected
= 0
5772 P_Ent
:= Scope
(P_Ent
);
5773 exit when No
(P_Ent
) or else P_Ent
= Standard_Standard
;
5775 if Is_Subprogram
(P_Ent
) then
5776 Set_Is_Inlined
(P_Ent
, False);
5778 if Comes_From_Source
(P_Ent
)
5779 and then Has_Pragma_Inline
(P_Ent
)
5782 ("cannot inline& (nested subprogram)?", N
, P_Ent
,
5783 Is_Serious
=> True);
5790 -- Build the body to inline only if really needed!
5792 if Check_Body_To_Inline
(N
, Spec_Id
)
5793 and then Serious_Errors_Detected
= 0
5795 if Returns_Unconstrained_Type
(Spec_Id
) then
5796 if Can_Split_Unconstrained_Function
(N
) then
5797 Split_Unconstrained_Function
(N
, Spec_Id
);
5798 Build_Body_To_Inline
(N
, Spec_Id
);
5799 Set_Is_Inlined
(Spec_Id
);
5802 Build_Body_To_Inline
(N
, Spec_Id
);
5803 Set_Is_Inlined
(Spec_Id
);
5806 end Check_And_Build_Body_To_Inline
;
5808 -----------------------
5809 -- Check_Conformance --
5810 -----------------------
5812 procedure Check_Conformance
5813 (New_Id
: Entity_Id
;
5815 Ctype
: Conformance_Type
;
5817 Conforms
: out Boolean;
5818 Err_Loc
: Node_Id
:= Empty
;
5819 Get_Inst
: Boolean := False;
5820 Skip_Controlling_Formals
: Boolean := False)
5822 procedure Conformance_Error
(Msg
: String; N
: Node_Id
:= New_Id
);
5823 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5824 -- If Errmsg is True, then processing continues to post an error message
5825 -- for conformance error on given node. Two messages are output. The
5826 -- first message points to the previous declaration with a general "no
5827 -- conformance" message. The second is the detailed reason, supplied as
5828 -- Msg. The parameter N provide information for a possible & insertion
5829 -- in the message, and also provides the location for posting the
5830 -- message in the absence of a specified Err_Loc location.
5832 -----------------------
5833 -- Conformance_Error --
5834 -----------------------
5836 procedure Conformance_Error
(Msg
: String; N
: Node_Id
:= New_Id
) is
5843 if No
(Err_Loc
) then
5849 Error_Msg_Sloc
:= Sloc
(Old_Id
);
5852 when Type_Conformant
=>
5853 Error_Msg_N
-- CODEFIX
5854 ("not type conformant with declaration#!", Enode
);
5856 when Mode_Conformant
=>
5857 if Nkind
(Parent
(Old_Id
)) = N_Full_Type_Declaration
then
5859 ("not mode conformant with operation inherited#!",
5863 ("not mode conformant with declaration#!", Enode
);
5866 when Subtype_Conformant
=>
5867 if Nkind
(Parent
(Old_Id
)) = N_Full_Type_Declaration
then
5869 ("not subtype conformant with operation inherited#!",
5873 ("not subtype conformant with declaration#!", Enode
);
5876 when Fully_Conformant
=>
5877 if Nkind
(Parent
(Old_Id
)) = N_Full_Type_Declaration
then
5878 Error_Msg_N
-- CODEFIX
5879 ("not fully conformant with operation inherited#!",
5882 Error_Msg_N
-- CODEFIX
5883 ("not fully conformant with declaration#!", Enode
);
5887 Error_Msg_NE
(Msg
, Enode
, N
);
5889 end Conformance_Error
;
5893 Old_Type
: constant Entity_Id
:= Etype
(Old_Id
);
5894 New_Type
: constant Entity_Id
:= Etype
(New_Id
);
5895 Old_Formal
: Entity_Id
;
5896 New_Formal
: Entity_Id
;
5897 Access_Types_Match
: Boolean;
5898 Old_Formal_Base
: Entity_Id
;
5899 New_Formal_Base
: Entity_Id
;
5901 -- Start of processing for Check_Conformance
5906 -- We need a special case for operators, since they don't appear
5909 if Ctype
= Type_Conformant
then
5910 if Ekind
(New_Id
) = E_Operator
5911 and then Operator_Matches_Spec
(New_Id
, Old_Id
)
5917 -- If both are functions/operators, check return types conform
5919 if Old_Type
/= Standard_Void_Type
5920 and then New_Type
/= Standard_Void_Type
5923 -- If we are checking interface conformance we omit controlling
5924 -- arguments and result, because we are only checking the conformance
5925 -- of the remaining parameters.
5927 if Has_Controlling_Result
(Old_Id
)
5928 and then Has_Controlling_Result
(New_Id
)
5929 and then Skip_Controlling_Formals
5933 elsif not Conforming_Types
(Old_Type
, New_Type
, Ctype
, Get_Inst
) then
5934 Conformance_Error
("\return type does not match!", New_Id
);
5938 -- Ada 2005 (AI-231): In case of anonymous access types check the
5939 -- null-exclusion and access-to-constant attributes match.
5941 if Ada_Version
>= Ada_2005
5942 and then Ekind
(Etype
(Old_Type
)) = E_Anonymous_Access_Type
5944 (Can_Never_Be_Null
(Old_Type
) /= Can_Never_Be_Null
(New_Type
)
5945 or else Is_Access_Constant
(Etype
(Old_Type
)) /=
5946 Is_Access_Constant
(Etype
(New_Type
)))
5948 Conformance_Error
("\return type does not match!", New_Id
);
5952 -- If either is a function/operator and the other isn't, error
5954 elsif Old_Type
/= Standard_Void_Type
5955 or else New_Type
/= Standard_Void_Type
5957 Conformance_Error
("\functions can only match functions!", New_Id
);
5961 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5962 -- If this is a renaming as body, refine error message to indicate that
5963 -- the conflict is with the original declaration. If the entity is not
5964 -- frozen, the conventions don't have to match, the one of the renamed
5965 -- entity is inherited.
5967 if Ctype
>= Subtype_Conformant
then
5968 if Convention
(Old_Id
) /= Convention
(New_Id
) then
5969 if not Is_Frozen
(New_Id
) then
5972 elsif Present
(Err_Loc
)
5973 and then Nkind
(Err_Loc
) = N_Subprogram_Renaming_Declaration
5974 and then Present
(Corresponding_Spec
(Err_Loc
))
5976 Error_Msg_Name_1
:= Chars
(New_Id
);
5978 Name_Ada
+ Convention_Id
'Pos (Convention
(New_Id
));
5979 Conformance_Error
("\prior declaration for% has convention %!");
5982 Conformance_Error
("\calling conventions do not match!");
5987 elsif Is_Formal_Subprogram
(Old_Id
)
5988 or else Is_Formal_Subprogram
(New_Id
)
5990 Conformance_Error
("\formal subprograms not allowed!");
5995 -- Deal with parameters
5997 -- Note: we use the entity information, rather than going directly
5998 -- to the specification in the tree. This is not only simpler, but
5999 -- absolutely necessary for some cases of conformance tests between
6000 -- operators, where the declaration tree simply does not exist!
6002 Old_Formal
:= First_Formal
(Old_Id
);
6003 New_Formal
:= First_Formal
(New_Id
);
6004 while Present
(Old_Formal
) and then Present
(New_Formal
) loop
6005 if Is_Controlling_Formal
(Old_Formal
)
6006 and then Is_Controlling_Formal
(New_Formal
)
6007 and then Skip_Controlling_Formals
6009 -- The controlling formals will have different types when
6010 -- comparing an interface operation with its match, but both
6011 -- or neither must be access parameters.
6013 if Is_Access_Type
(Etype
(Old_Formal
))
6015 Is_Access_Type
(Etype
(New_Formal
))
6017 goto Skip_Controlling_Formal
;
6020 ("\access parameter does not match!", New_Formal
);
6024 -- Ada 2012: Mode conformance also requires that formal parameters
6025 -- be both aliased, or neither.
6027 if Ctype
>= Mode_Conformant
and then Ada_Version
>= Ada_2012
then
6028 if Is_Aliased
(Old_Formal
) /= Is_Aliased
(New_Formal
) then
6030 ("\aliased parameter mismatch!", New_Formal
);
6034 if Ctype
= Fully_Conformant
then
6036 -- Names must match. Error message is more accurate if we do
6037 -- this before checking that the types of the formals match.
6039 if Chars
(Old_Formal
) /= Chars
(New_Formal
) then
6040 Conformance_Error
("\name & does not match!", New_Formal
);
6042 -- Set error posted flag on new formal as well to stop
6043 -- junk cascaded messages in some cases.
6045 Set_Error_Posted
(New_Formal
);
6049 -- Null exclusion must match
6051 if Null_Exclusion_Present
(Parent
(Old_Formal
))
6053 Null_Exclusion_Present
(Parent
(New_Formal
))
6055 -- Only give error if both come from source. This should be
6056 -- investigated some time, since it should not be needed ???
6058 if Comes_From_Source
(Old_Formal
)
6060 Comes_From_Source
(New_Formal
)
6063 ("\null exclusion for & does not match", New_Formal
);
6065 -- Mark error posted on the new formal to avoid duplicated
6066 -- complaint about types not matching.
6068 Set_Error_Posted
(New_Formal
);
6073 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
6074 -- case occurs whenever a subprogram is being renamed and one of its
6075 -- parameters imposes a null exclusion. For example:
6077 -- type T is null record;
6078 -- type Acc_T is access T;
6079 -- subtype Acc_T_Sub is Acc_T;
6081 -- procedure P (Obj : not null Acc_T_Sub); -- itype
6082 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
6085 Old_Formal_Base
:= Etype
(Old_Formal
);
6086 New_Formal_Base
:= Etype
(New_Formal
);
6089 Old_Formal_Base
:= Get_Instance_Of
(Old_Formal_Base
);
6090 New_Formal_Base
:= Get_Instance_Of
(New_Formal_Base
);
6093 Access_Types_Match
:= Ada_Version
>= Ada_2005
6095 -- Ensure that this rule is only applied when New_Id is a
6096 -- renaming of Old_Id.
6098 and then Nkind
(Parent
(Parent
(New_Id
))) =
6099 N_Subprogram_Renaming_Declaration
6100 and then Nkind
(Name
(Parent
(Parent
(New_Id
)))) in N_Has_Entity
6101 and then Present
(Entity
(Name
(Parent
(Parent
(New_Id
)))))
6102 and then Entity
(Name
(Parent
(Parent
(New_Id
)))) = Old_Id
6104 -- Now handle the allowed access-type case
6106 and then Is_Access_Type
(Old_Formal_Base
)
6107 and then Is_Access_Type
(New_Formal_Base
)
6109 -- The type kinds must match. The only exception occurs with
6110 -- multiple generics of the form:
6113 -- type F is private; type A is private;
6114 -- type F_Ptr is access F; type A_Ptr is access A;
6115 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
6116 -- package F_Pack is ... package A_Pack is
6117 -- package F_Inst is
6118 -- new F_Pack (A, A_Ptr, A_P);
6120 -- When checking for conformance between the parameters of A_P
6121 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
6122 -- because the compiler has transformed A_Ptr into a subtype of
6123 -- F_Ptr. We catch this case in the code below.
6125 and then (Ekind
(Old_Formal_Base
) = Ekind
(New_Formal_Base
)
6127 (Is_Generic_Type
(Old_Formal_Base
)
6128 and then Is_Generic_Type
(New_Formal_Base
)
6129 and then Is_Internal
(New_Formal_Base
)
6130 and then Etype
(Etype
(New_Formal_Base
)) =
6132 and then Directly_Designated_Type
(Old_Formal_Base
) =
6133 Directly_Designated_Type
(New_Formal_Base
)
6134 and then ((Is_Itype
(Old_Formal_Base
)
6135 and then Can_Never_Be_Null
(Old_Formal_Base
))
6137 (Is_Itype
(New_Formal_Base
)
6138 and then Can_Never_Be_Null
(New_Formal_Base
)));
6140 -- Types must always match. In the visible part of an instance,
6141 -- usual overloading rules for dispatching operations apply, and
6142 -- we check base types (not the actual subtypes).
6144 if In_Instance_Visible_Part
6145 and then Is_Dispatching_Operation
(New_Id
)
6147 if not Conforming_Types
6148 (T1
=> Base_Type
(Etype
(Old_Formal
)),
6149 T2
=> Base_Type
(Etype
(New_Formal
)),
6151 Get_Inst
=> Get_Inst
)
6152 and then not Access_Types_Match
6154 Conformance_Error
("\type of & does not match!", New_Formal
);
6158 elsif not Conforming_Types
6159 (T1
=> Old_Formal_Base
,
6160 T2
=> New_Formal_Base
,
6162 Get_Inst
=> Get_Inst
)
6163 and then not Access_Types_Match
6165 -- Don't give error message if old type is Any_Type. This test
6166 -- avoids some cascaded errors, e.g. in case of a bad spec.
6168 if Errmsg
and then Old_Formal_Base
= Any_Type
then
6171 Conformance_Error
("\type of & does not match!", New_Formal
);
6177 -- For mode conformance, mode must match
6179 if Ctype
>= Mode_Conformant
then
6180 if Parameter_Mode
(Old_Formal
) /= Parameter_Mode
(New_Formal
) then
6181 if not Ekind_In
(New_Id
, E_Function
, E_Procedure
)
6182 or else not Is_Primitive_Wrapper
(New_Id
)
6184 Conformance_Error
("\mode of & does not match!", New_Formal
);
6188 T
: constant Entity_Id
:= Find_Dispatching_Type
(New_Id
);
6190 if Is_Protected_Type
6191 (Corresponding_Concurrent_Type
(T
))
6193 Error_Msg_PT
(T
, New_Id
);
6196 ("\mode of & does not match!", New_Formal
);
6203 -- Part of mode conformance for access types is having the same
6204 -- constant modifier.
6206 elsif Access_Types_Match
6207 and then Is_Access_Constant
(Old_Formal_Base
) /=
6208 Is_Access_Constant
(New_Formal_Base
)
6211 ("\constant modifier does not match!", New_Formal
);
6216 if Ctype
>= Subtype_Conformant
then
6218 -- Ada 2005 (AI-231): In case of anonymous access types check
6219 -- the null-exclusion and access-to-constant attributes must
6220 -- match. For null exclusion, we test the types rather than the
6221 -- formals themselves, since the attribute is only set reliably
6222 -- on the formals in the Ada 95 case, and we exclude the case
6223 -- where Old_Formal is marked as controlling, to avoid errors
6224 -- when matching completing bodies with dispatching declarations
6225 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6227 if Ada_Version
>= Ada_2005
6228 and then Ekind
(Etype
(Old_Formal
)) = E_Anonymous_Access_Type
6229 and then Ekind
(Etype
(New_Formal
)) = E_Anonymous_Access_Type
6231 ((Can_Never_Be_Null
(Etype
(Old_Formal
)) /=
6232 Can_Never_Be_Null
(Etype
(New_Formal
))
6234 not Is_Controlling_Formal
(Old_Formal
))
6236 Is_Access_Constant
(Etype
(Old_Formal
)) /=
6237 Is_Access_Constant
(Etype
(New_Formal
)))
6239 -- Do not complain if error already posted on New_Formal. This
6240 -- avoids some redundant error messages.
6242 and then not Error_Posted
(New_Formal
)
6244 -- It is allowed to omit the null-exclusion in case of stream
6245 -- attribute subprograms. We recognize stream subprograms
6246 -- through their TSS-generated suffix.
6249 TSS_Name
: constant TSS_Name_Type
:= Get_TSS_Name
(New_Id
);
6252 if TSS_Name
/= TSS_Stream_Read
6253 and then TSS_Name
/= TSS_Stream_Write
6254 and then TSS_Name
/= TSS_Stream_Input
6255 and then TSS_Name
/= TSS_Stream_Output
6257 -- Here we have a definite conformance error. It is worth
6258 -- special casing the error message for the case of a
6259 -- controlling formal (which excludes null).
6261 if Is_Controlling_Formal
(New_Formal
) then
6262 Error_Msg_Node_2
:= Scope
(New_Formal
);
6264 ("\controlling formal& of& excludes null, "
6265 & "declaration must exclude null as well",
6268 -- Normal case (couldn't we give more detail here???)
6272 ("\type of & does not match!", New_Formal
);
6281 -- Full conformance checks
6283 if Ctype
= Fully_Conformant
then
6285 -- We have checked already that names match
6287 if Parameter_Mode
(Old_Formal
) = E_In_Parameter
then
6289 -- Check default expressions for in parameters
6292 NewD
: constant Boolean :=
6293 Present
(Default_Value
(New_Formal
));
6294 OldD
: constant Boolean :=
6295 Present
(Default_Value
(Old_Formal
));
6297 if NewD
or OldD
then
6299 -- The old default value has been analyzed because the
6300 -- current full declaration will have frozen everything
6301 -- before. The new default value has not been analyzed,
6302 -- so analyze it now before we check for conformance.
6305 Push_Scope
(New_Id
);
6306 Preanalyze_Spec_Expression
6307 (Default_Value
(New_Formal
), Etype
(New_Formal
));
6311 if not (NewD
and OldD
)
6312 or else not Fully_Conformant_Expressions
6313 (Default_Value
(Old_Formal
),
6314 Default_Value
(New_Formal
))
6317 ("\default expression for & does not match!",
6326 -- A couple of special checks for Ada 83 mode. These checks are
6327 -- skipped if either entity is an operator in package Standard,
6328 -- or if either old or new instance is not from the source program.
6330 if Ada_Version
= Ada_83
6331 and then Sloc
(Old_Id
) > Standard_Location
6332 and then Sloc
(New_Id
) > Standard_Location
6333 and then Comes_From_Source
(Old_Id
)
6334 and then Comes_From_Source
(New_Id
)
6337 Old_Param
: constant Node_Id
:= Declaration_Node
(Old_Formal
);
6338 New_Param
: constant Node_Id
:= Declaration_Node
(New_Formal
);
6341 -- Explicit IN must be present or absent in both cases. This
6342 -- test is required only in the full conformance case.
6344 if In_Present
(Old_Param
) /= In_Present
(New_Param
)
6345 and then Ctype
= Fully_Conformant
6348 ("\(Ada 83) IN must appear in both declarations",
6353 -- Grouping (use of comma in param lists) must be the same
6354 -- This is where we catch a misconformance like:
6357 -- A : Integer; B : Integer
6359 -- which are represented identically in the tree except
6360 -- for the setting of the flags More_Ids and Prev_Ids.
6362 if More_Ids
(Old_Param
) /= More_Ids
(New_Param
)
6363 or else Prev_Ids
(Old_Param
) /= Prev_Ids
(New_Param
)
6366 ("\grouping of & does not match!", New_Formal
);
6372 -- This label is required when skipping controlling formals
6374 <<Skip_Controlling_Formal
>>
6376 Next_Formal
(Old_Formal
);
6377 Next_Formal
(New_Formal
);
6380 if Present
(Old_Formal
) then
6381 Conformance_Error
("\too few parameters!");
6384 elsif Present
(New_Formal
) then
6385 Conformance_Error
("\too many parameters!", New_Formal
);
6388 end Check_Conformance
;
6390 -----------------------
6391 -- Check_Conventions --
6392 -----------------------
6394 procedure Check_Conventions
(Typ
: Entity_Id
) is
6395 Ifaces_List
: Elist_Id
;
6397 procedure Check_Convention
(Op
: Entity_Id
);
6398 -- Verify that the convention of inherited dispatching operation Op is
6399 -- consistent among all subprograms it overrides. In order to minimize
6400 -- the search, Search_From is utilized to designate a specific point in
6401 -- the list rather than iterating over the whole list once more.
6403 ----------------------
6404 -- Check_Convention --
6405 ----------------------
6407 procedure Check_Convention
(Op
: Entity_Id
) is
6408 function Convention_Of
(Id
: Entity_Id
) return Convention_Id
;
6409 -- Given an entity, return its convention. The function treats Ghost
6410 -- as convention Ada because the two have the same dynamic semantics.
6416 function Convention_Of
(Id
: Entity_Id
) return Convention_Id
is
6417 Conv
: constant Convention_Id
:= Convention
(Id
);
6419 if Conv
= Convention_Ghost
then
6420 return Convention_Ada
;
6428 Op_Conv
: constant Convention_Id
:= Convention_Of
(Op
);
6429 Iface_Conv
: Convention_Id
;
6430 Iface_Elmt
: Elmt_Id
;
6431 Iface_Prim_Elmt
: Elmt_Id
;
6432 Iface_Prim
: Entity_Id
;
6434 -- Start of processing for Check_Convention
6437 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
6438 while Present
(Iface_Elmt
) loop
6440 First_Elmt
(Primitive_Operations
(Node
(Iface_Elmt
)));
6441 while Present
(Iface_Prim_Elmt
) loop
6442 Iface_Prim
:= Node
(Iface_Prim_Elmt
);
6443 Iface_Conv
:= Convention_Of
(Iface_Prim
);
6445 if Is_Interface_Conformant
(Typ
, Iface_Prim
, Op
)
6446 and then Iface_Conv
/= Op_Conv
6449 ("inconsistent conventions in primitive operations", Typ
);
6451 Error_Msg_Name_1
:= Chars
(Op
);
6452 Error_Msg_Name_2
:= Get_Convention_Name
(Op_Conv
);
6453 Error_Msg_Sloc
:= Sloc
(Op
);
6455 if Comes_From_Source
(Op
) or else No
(Alias
(Op
)) then
6456 if not Present
(Overridden_Operation
(Op
)) then
6457 Error_Msg_N
("\\primitive % defined #", Typ
);
6460 ("\\overriding operation % with " &
6461 "convention % defined #", Typ
);
6464 else pragma Assert
(Present
(Alias
(Op
)));
6465 Error_Msg_Sloc
:= Sloc
(Alias
(Op
));
6467 ("\\inherited operation % with " &
6468 "convention % defined #", Typ
);
6471 Error_Msg_Name_1
:= Chars
(Op
);
6472 Error_Msg_Name_2
:= Get_Convention_Name
(Iface_Conv
);
6473 Error_Msg_Sloc
:= Sloc
(Iface_Prim
);
6475 ("\\overridden operation % with " &
6476 "convention % defined #", Typ
);
6478 -- Avoid cascading errors
6483 Next_Elmt
(Iface_Prim_Elmt
);
6486 Next_Elmt
(Iface_Elmt
);
6488 end Check_Convention
;
6492 Prim_Op
: Entity_Id
;
6493 Prim_Op_Elmt
: Elmt_Id
;
6495 -- Start of processing for Check_Conventions
6498 if not Has_Interfaces
(Typ
) then
6502 Collect_Interfaces
(Typ
, Ifaces_List
);
6504 -- The algorithm checks every overriding dispatching operation against
6505 -- all the corresponding overridden dispatching operations, detecting
6506 -- differences in conventions.
6508 Prim_Op_Elmt
:= First_Elmt
(Primitive_Operations
(Typ
));
6509 while Present
(Prim_Op_Elmt
) loop
6510 Prim_Op
:= Node
(Prim_Op_Elmt
);
6512 -- A small optimization: skip the predefined dispatching operations
6513 -- since they always have the same convention.
6515 if not Is_Predefined_Dispatching_Operation
(Prim_Op
) then
6516 Check_Convention
(Prim_Op
);
6519 Next_Elmt
(Prim_Op_Elmt
);
6521 end Check_Conventions
;
6523 ------------------------------
6524 -- Check_Delayed_Subprogram --
6525 ------------------------------
6527 procedure Check_Delayed_Subprogram
(Designator
: Entity_Id
) is
6530 procedure Possible_Freeze
(T
: Entity_Id
);
6531 -- T is the type of either a formal parameter or of the return type.
6532 -- If T is not yet frozen and needs a delayed freeze, then the
6533 -- subprogram itself must be delayed. If T is the limited view of an
6534 -- incomplete type the subprogram must be frozen as well, because
6535 -- T may depend on local types that have not been frozen yet.
6537 ---------------------
6538 -- Possible_Freeze --
6539 ---------------------
6541 procedure Possible_Freeze
(T
: Entity_Id
) is
6543 if Has_Delayed_Freeze
(T
) and then not Is_Frozen
(T
) then
6544 Set_Has_Delayed_Freeze
(Designator
);
6546 elsif Is_Access_Type
(T
)
6547 and then Has_Delayed_Freeze
(Designated_Type
(T
))
6548 and then not Is_Frozen
(Designated_Type
(T
))
6550 Set_Has_Delayed_Freeze
(Designator
);
6552 elsif Ekind
(T
) = E_Incomplete_Type
6553 and then From_Limited_With
(T
)
6555 Set_Has_Delayed_Freeze
(Designator
);
6557 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
6558 -- of a subprogram or entry declaration.
6560 elsif Ekind
(T
) = E_Incomplete_Type
6561 and then Ada_Version
>= Ada_2012
6563 Set_Has_Delayed_Freeze
(Designator
);
6566 end Possible_Freeze
;
6568 -- Start of processing for Check_Delayed_Subprogram
6571 -- All subprograms, including abstract subprograms, may need a freeze
6572 -- node if some formal type or the return type needs one.
6574 Possible_Freeze
(Etype
(Designator
));
6575 Possible_Freeze
(Base_Type
(Etype
(Designator
))); -- needed ???
6577 -- Need delayed freeze if any of the formal types themselves need
6578 -- a delayed freeze and are not yet frozen.
6580 F
:= First_Formal
(Designator
);
6581 while Present
(F
) loop
6582 Possible_Freeze
(Etype
(F
));
6583 Possible_Freeze
(Base_Type
(Etype
(F
))); -- needed ???
6587 -- Mark functions that return by reference. Note that it cannot be
6588 -- done for delayed_freeze subprograms because the underlying
6589 -- returned type may not be known yet (for private types)
6591 if not Has_Delayed_Freeze
(Designator
) and then Expander_Active
then
6593 Typ
: constant Entity_Id
:= Etype
(Designator
);
6594 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
6596 if Is_Limited_View
(Typ
) then
6597 Set_Returns_By_Ref
(Designator
);
6598 elsif Present
(Utyp
) and then CW_Or_Has_Controlled_Part
(Utyp
) then
6599 Set_Returns_By_Ref
(Designator
);
6603 end Check_Delayed_Subprogram
;
6605 ------------------------------------
6606 -- Check_Discriminant_Conformance --
6607 ------------------------------------
6609 procedure Check_Discriminant_Conformance
6614 Old_Discr
: Entity_Id
:= First_Discriminant
(Prev
);
6615 New_Discr
: Node_Id
:= First
(Discriminant_Specifications
(N
));
6616 New_Discr_Id
: Entity_Id
;
6617 New_Discr_Type
: Entity_Id
;
6619 procedure Conformance_Error
(Msg
: String; N
: Node_Id
);
6620 -- Post error message for conformance error on given node. Two messages
6621 -- are output. The first points to the previous declaration with a
6622 -- general "no conformance" message. The second is the detailed reason,
6623 -- supplied as Msg. The parameter N provide information for a possible
6624 -- & insertion in the message.
6626 -----------------------
6627 -- Conformance_Error --
6628 -----------------------
6630 procedure Conformance_Error
(Msg
: String; N
: Node_Id
) is
6632 Error_Msg_Sloc
:= Sloc
(Prev_Loc
);
6633 Error_Msg_N
-- CODEFIX
6634 ("not fully conformant with declaration#!", N
);
6635 Error_Msg_NE
(Msg
, N
, N
);
6636 end Conformance_Error
;
6638 -- Start of processing for Check_Discriminant_Conformance
6641 while Present
(Old_Discr
) and then Present
(New_Discr
) loop
6642 New_Discr_Id
:= Defining_Identifier
(New_Discr
);
6644 -- The subtype mark of the discriminant on the full type has not
6645 -- been analyzed so we do it here. For an access discriminant a new
6648 if Nkind
(Discriminant_Type
(New_Discr
)) = N_Access_Definition
then
6650 Access_Definition
(N
, Discriminant_Type
(New_Discr
));
6653 Analyze
(Discriminant_Type
(New_Discr
));
6654 New_Discr_Type
:= Etype
(Discriminant_Type
(New_Discr
));
6656 -- Ada 2005: if the discriminant definition carries a null
6657 -- exclusion, create an itype to check properly for consistency
6658 -- with partial declaration.
6660 if Is_Access_Type
(New_Discr_Type
)
6661 and then Null_Exclusion_Present
(New_Discr
)
6664 Create_Null_Excluding_Itype
6665 (T
=> New_Discr_Type
,
6666 Related_Nod
=> New_Discr
,
6667 Scope_Id
=> Current_Scope
);
6671 if not Conforming_Types
6672 (Etype
(Old_Discr
), New_Discr_Type
, Fully_Conformant
)
6674 Conformance_Error
("type of & does not match!", New_Discr_Id
);
6677 -- Treat the new discriminant as an occurrence of the old one,
6678 -- for navigation purposes, and fill in some semantic
6679 -- information, for completeness.
6681 Generate_Reference
(Old_Discr
, New_Discr_Id
, 'r');
6682 Set_Etype
(New_Discr_Id
, Etype
(Old_Discr
));
6683 Set_Scope
(New_Discr_Id
, Scope
(Old_Discr
));
6688 if Chars
(Old_Discr
) /= Chars
(Defining_Identifier
(New_Discr
)) then
6689 Conformance_Error
("name & does not match!", New_Discr_Id
);
6693 -- Default expressions must match
6696 NewD
: constant Boolean :=
6697 Present
(Expression
(New_Discr
));
6698 OldD
: constant Boolean :=
6699 Present
(Expression
(Parent
(Old_Discr
)));
6702 if NewD
or OldD
then
6704 -- The old default value has been analyzed and expanded,
6705 -- because the current full declaration will have frozen
6706 -- everything before. The new default values have not been
6707 -- expanded, so expand now to check conformance.
6710 Preanalyze_Spec_Expression
6711 (Expression
(New_Discr
), New_Discr_Type
);
6714 if not (NewD
and OldD
)
6715 or else not Fully_Conformant_Expressions
6716 (Expression
(Parent
(Old_Discr
)),
6717 Expression
(New_Discr
))
6721 ("default expression for & does not match!",
6728 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6730 if Ada_Version
= Ada_83
then
6732 Old_Disc
: constant Node_Id
:= Declaration_Node
(Old_Discr
);
6735 -- Grouping (use of comma in param lists) must be the same
6736 -- This is where we catch a misconformance like:
6739 -- A : Integer; B : Integer
6741 -- which are represented identically in the tree except
6742 -- for the setting of the flags More_Ids and Prev_Ids.
6744 if More_Ids
(Old_Disc
) /= More_Ids
(New_Discr
)
6745 or else Prev_Ids
(Old_Disc
) /= Prev_Ids
(New_Discr
)
6748 ("grouping of & does not match!", New_Discr_Id
);
6754 Next_Discriminant
(Old_Discr
);
6758 if Present
(Old_Discr
) then
6759 Conformance_Error
("too few discriminants!", Defining_Identifier
(N
));
6762 elsif Present
(New_Discr
) then
6764 ("too many discriminants!", Defining_Identifier
(New_Discr
));
6767 end Check_Discriminant_Conformance
;
6769 ----------------------------
6770 -- Check_Fully_Conformant --
6771 ----------------------------
6773 procedure Check_Fully_Conformant
6774 (New_Id
: Entity_Id
;
6776 Err_Loc
: Node_Id
:= Empty
)
6779 pragma Warnings
(Off
, Result
);
6782 (New_Id
, Old_Id
, Fully_Conformant
, True, Result
, Err_Loc
);
6783 end Check_Fully_Conformant
;
6785 ---------------------------
6786 -- Check_Mode_Conformant --
6787 ---------------------------
6789 procedure Check_Mode_Conformant
6790 (New_Id
: Entity_Id
;
6792 Err_Loc
: Node_Id
:= Empty
;
6793 Get_Inst
: Boolean := False)
6796 pragma Warnings
(Off
, Result
);
6799 (New_Id
, Old_Id
, Mode_Conformant
, True, Result
, Err_Loc
, Get_Inst
);
6800 end Check_Mode_Conformant
;
6802 --------------------------------
6803 -- Check_Overriding_Indicator --
6804 --------------------------------
6806 procedure Check_Overriding_Indicator
6808 Overridden_Subp
: Entity_Id
;
6809 Is_Primitive
: Boolean)
6815 -- No overriding indicator for literals
6817 if Ekind
(Subp
) = E_Enumeration_Literal
then
6820 elsif Ekind
(Subp
) = E_Entry
then
6821 Decl
:= Parent
(Subp
);
6823 -- No point in analyzing a malformed operator
6825 elsif Nkind
(Subp
) = N_Defining_Operator_Symbol
6826 and then Error_Posted
(Subp
)
6831 Decl
:= Unit_Declaration_Node
(Subp
);
6834 if Nkind_In
(Decl
, N_Subprogram_Body
,
6835 N_Subprogram_Body_Stub
,
6836 N_Subprogram_Declaration
,
6837 N_Abstract_Subprogram_Declaration
,
6838 N_Subprogram_Renaming_Declaration
)
6840 Spec
:= Specification
(Decl
);
6842 elsif Nkind
(Decl
) = N_Entry_Declaration
then
6849 -- The overriding operation is type conformant with the overridden one,
6850 -- but the names of the formals are not required to match. If the names
6851 -- appear permuted in the overriding operation, this is a possible
6852 -- source of confusion that is worth diagnosing. Controlling formals
6853 -- often carry names that reflect the type, and it is not worthwhile
6854 -- requiring that their names match.
6856 if Present
(Overridden_Subp
)
6857 and then Nkind
(Subp
) /= N_Defining_Operator_Symbol
6864 Form1
:= First_Formal
(Subp
);
6865 Form2
:= First_Formal
(Overridden_Subp
);
6867 -- If the overriding operation is a synchronized operation, skip
6868 -- the first parameter of the overridden operation, which is
6869 -- implicit in the new one. If the operation is declared in the
6870 -- body it is not primitive and all formals must match.
6872 if Is_Concurrent_Type
(Scope
(Subp
))
6873 and then Is_Tagged_Type
(Scope
(Subp
))
6874 and then not Has_Completion
(Scope
(Subp
))
6876 Form2
:= Next_Formal
(Form2
);
6879 if Present
(Form1
) then
6880 Form1
:= Next_Formal
(Form1
);
6881 Form2
:= Next_Formal
(Form2
);
6884 while Present
(Form1
) loop
6885 if not Is_Controlling_Formal
(Form1
)
6886 and then Present
(Next_Formal
(Form2
))
6887 and then Chars
(Form1
) = Chars
(Next_Formal
(Form2
))
6889 Error_Msg_Node_2
:= Alias
(Overridden_Subp
);
6890 Error_Msg_Sloc
:= Sloc
(Error_Msg_Node_2
);
6892 ("& does not match corresponding formal of&#",
6897 Next_Formal
(Form1
);
6898 Next_Formal
(Form2
);
6903 -- If there is an overridden subprogram, then check that there is no
6904 -- "not overriding" indicator, and mark the subprogram as overriding.
6905 -- This is not done if the overridden subprogram is marked as hidden,
6906 -- which can occur for the case of inherited controlled operations
6907 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6908 -- subprogram is not itself hidden. (Note: This condition could probably
6909 -- be simplified, leaving out the testing for the specific controlled
6910 -- cases, but it seems safer and clearer this way, and echoes similar
6911 -- special-case tests of this kind in other places.)
6913 if Present
(Overridden_Subp
)
6914 and then (not Is_Hidden
(Overridden_Subp
)
6916 (Nam_In
(Chars
(Overridden_Subp
), Name_Initialize
,
6919 and then Present
(Alias
(Overridden_Subp
))
6920 and then not Is_Hidden
(Alias
(Overridden_Subp
))))
6922 if Must_Not_Override
(Spec
) then
6923 Error_Msg_Sloc
:= Sloc
(Overridden_Subp
);
6925 if Ekind
(Subp
) = E_Entry
then
6927 ("entry & overrides inherited operation #", Spec
, Subp
);
6930 ("subprogram & overrides inherited operation #", Spec
, Subp
);
6933 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6934 -- as an extension of Root_Controlled, and thus has a useless Adjust
6935 -- operation. This operation should not be inherited by other limited
6936 -- controlled types. An explicit Adjust for them is not overriding.
6938 elsif Must_Override
(Spec
)
6939 and then Chars
(Overridden_Subp
) = Name_Adjust
6940 and then Is_Limited_Type
(Etype
(First_Formal
(Subp
)))
6941 and then Present
(Alias
(Overridden_Subp
))
6943 Is_Predefined_File_Name
6944 (Unit_File_Name
(Get_Source_Unit
(Alias
(Overridden_Subp
))))
6946 Error_Msg_NE
("subprogram & is not overriding", Spec
, Subp
);
6948 elsif Is_Subprogram
(Subp
) then
6949 if Is_Init_Proc
(Subp
) then
6952 elsif No
(Overridden_Operation
(Subp
)) then
6954 -- For entities generated by Derive_Subprograms the overridden
6955 -- operation is the inherited primitive (which is available
6956 -- through the attribute alias)
6958 if (Is_Dispatching_Operation
(Subp
)
6959 or else Is_Dispatching_Operation
(Overridden_Subp
))
6960 and then not Comes_From_Source
(Overridden_Subp
)
6961 and then Find_Dispatching_Type
(Overridden_Subp
) =
6962 Find_Dispatching_Type
(Subp
)
6963 and then Present
(Alias
(Overridden_Subp
))
6964 and then Comes_From_Source
(Alias
(Overridden_Subp
))
6966 Set_Overridden_Operation
(Subp
, Alias
(Overridden_Subp
));
6969 Set_Overridden_Operation
(Subp
, Overridden_Subp
);
6974 -- If primitive flag is set or this is a protected operation, then
6975 -- the operation is overriding at the point of its declaration, so
6976 -- warn if necessary. Otherwise it may have been declared before the
6977 -- operation it overrides and no check is required.
6980 and then not Must_Override
(Spec
)
6981 and then (Is_Primitive
6982 or else Ekind
(Scope
(Subp
)) = E_Protected_Type
)
6984 Style
.Missing_Overriding
(Decl
, Subp
);
6987 -- If Subp is an operator, it may override a predefined operation, if
6988 -- it is defined in the same scope as the type to which it applies.
6989 -- In that case Overridden_Subp is empty because of our implicit
6990 -- representation for predefined operators. We have to check whether the
6991 -- signature of Subp matches that of a predefined operator. Note that
6992 -- first argument provides the name of the operator, and the second
6993 -- argument the signature that may match that of a standard operation.
6994 -- If the indicator is overriding, then the operator must match a
6995 -- predefined signature, because we know already that there is no
6996 -- explicit overridden operation.
6998 elsif Nkind
(Subp
) = N_Defining_Operator_Symbol
then
6999 if Must_Not_Override
(Spec
) then
7001 -- If this is not a primitive or a protected subprogram, then
7002 -- "not overriding" is illegal.
7005 and then Ekind
(Scope
(Subp
)) /= E_Protected_Type
7008 ("overriding indicator only allowed "
7009 & "if subprogram is primitive", Subp
);
7011 elsif Can_Override_Operator
(Subp
) then
7013 ("subprogram& overrides predefined operator ", Spec
, Subp
);
7016 elsif Must_Override
(Spec
) then
7017 if No
(Overridden_Operation
(Subp
))
7018 and then not Can_Override_Operator
(Subp
)
7020 Error_Msg_NE
("subprogram & is not overriding", Spec
, Subp
);
7023 elsif not Error_Posted
(Subp
)
7024 and then Style_Check
7025 and then Can_Override_Operator
(Subp
)
7027 not Is_Predefined_File_Name
7028 (Unit_File_Name
(Get_Source_Unit
(Subp
)))
7030 -- If style checks are enabled, indicate that the indicator is
7031 -- missing. However, at the point of declaration, the type of
7032 -- which this is a primitive operation may be private, in which
7033 -- case the indicator would be premature.
7035 if Has_Private_Declaration
(Etype
(Subp
))
7036 or else Has_Private_Declaration
(Etype
(First_Formal
(Subp
)))
7040 Style
.Missing_Overriding
(Decl
, Subp
);
7044 elsif Must_Override
(Spec
) then
7045 if Ekind
(Subp
) = E_Entry
then
7046 Error_Msg_NE
("entry & is not overriding", Spec
, Subp
);
7048 Error_Msg_NE
("subprogram & is not overriding", Spec
, Subp
);
7051 -- If the operation is marked "not overriding" and it's not primitive
7052 -- then an error is issued, unless this is an operation of a task or
7053 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
7054 -- has been specified have already been checked above.
7056 elsif Must_Not_Override
(Spec
)
7057 and then not Is_Primitive
7058 and then Ekind
(Subp
) /= E_Entry
7059 and then Ekind
(Scope
(Subp
)) /= E_Protected_Type
7062 ("overriding indicator only allowed if subprogram is primitive",
7066 end Check_Overriding_Indicator
;
7072 -- Note: this procedure needs to know far too much about how the expander
7073 -- messes with exceptions. The use of the flag Exception_Junk and the
7074 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
7075 -- works, but is not very clean. It would be better if the expansion
7076 -- routines would leave Original_Node working nicely, and we could use
7077 -- Original_Node here to ignore all the peculiar expander messing ???
7079 procedure Check_Returns
7083 Proc
: Entity_Id
:= Empty
)
7087 procedure Check_Statement_Sequence
(L
: List_Id
);
7088 -- Internal recursive procedure to check a list of statements for proper
7089 -- termination by a return statement (or a transfer of control or a
7090 -- compound statement that is itself internally properly terminated).
7092 ------------------------------
7093 -- Check_Statement_Sequence --
7094 ------------------------------
7096 procedure Check_Statement_Sequence
(L
: List_Id
) is
7101 Raise_Exception_Call
: Boolean;
7102 -- Set True if statement sequence terminated by Raise_Exception call
7103 -- or a Reraise_Occurrence call.
7106 Raise_Exception_Call
:= False;
7108 -- Get last real statement
7110 Last_Stm
:= Last
(L
);
7112 -- Deal with digging out exception handler statement sequences that
7113 -- have been transformed by the local raise to goto optimization.
7114 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7115 -- optimization has occurred, we are looking at something like:
7118 -- original stmts in block
7122 -- goto L1; | omitted if No_Exception_Propagation
7127 -- goto L3; -- skip handler when exception not raised
7129 -- <<L1>> -- target label for local exception
7143 -- and what we have to do is to dig out the estmts1 and estmts2
7144 -- sequences (which were the original sequences of statements in
7145 -- the exception handlers) and check them.
7147 if Nkind
(Last_Stm
) = N_Label
and then Exception_Junk
(Last_Stm
) then
7152 exit when Nkind
(Stm
) /= N_Block_Statement
;
7153 exit when not Exception_Junk
(Stm
);
7156 exit when Nkind
(Stm
) /= N_Label
;
7157 exit when not Exception_Junk
(Stm
);
7158 Check_Statement_Sequence
7159 (Statements
(Handled_Statement_Sequence
(Next
(Stm
))));
7164 exit when Nkind
(Stm
) /= N_Goto_Statement
;
7165 exit when not Exception_Junk
(Stm
);
7169 -- Don't count pragmas
7171 while Nkind
(Last_Stm
) = N_Pragma
7173 -- Don't count call to SS_Release (can happen after Raise_Exception)
7176 (Nkind
(Last_Stm
) = N_Procedure_Call_Statement
7178 Nkind
(Name
(Last_Stm
)) = N_Identifier
7180 Is_RTE
(Entity
(Name
(Last_Stm
)), RE_SS_Release
))
7182 -- Don't count exception junk
7185 (Nkind_In
(Last_Stm
, N_Goto_Statement
,
7187 N_Object_Declaration
)
7188 and then Exception_Junk
(Last_Stm
))
7189 or else Nkind
(Last_Stm
) in N_Push_xxx_Label
7190 or else Nkind
(Last_Stm
) in N_Pop_xxx_Label
7192 -- Inserted code, such as finalization calls, is irrelevant: we only
7193 -- need to check original source.
7195 or else Is_Rewrite_Insertion
(Last_Stm
)
7200 -- Here we have the "real" last statement
7202 Kind
:= Nkind
(Last_Stm
);
7204 -- Transfer of control, OK. Note that in the No_Return procedure
7205 -- case, we already diagnosed any explicit return statements, so
7206 -- we can treat them as OK in this context.
7208 if Is_Transfer
(Last_Stm
) then
7211 -- Check cases of explicit non-indirect procedure calls
7213 elsif Kind
= N_Procedure_Call_Statement
7214 and then Is_Entity_Name
(Name
(Last_Stm
))
7216 -- Check call to Raise_Exception procedure which is treated
7217 -- specially, as is a call to Reraise_Occurrence.
7219 -- We suppress the warning in these cases since it is likely that
7220 -- the programmer really does not expect to deal with the case
7221 -- of Null_Occurrence, and thus would find a warning about a
7222 -- missing return curious, and raising Program_Error does not
7223 -- seem such a bad behavior if this does occur.
7225 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7226 -- behavior will be to raise Constraint_Error (see AI-329).
7228 if Is_RTE
(Entity
(Name
(Last_Stm
)), RE_Raise_Exception
)
7230 Is_RTE
(Entity
(Name
(Last_Stm
)), RE_Reraise_Occurrence
)
7232 Raise_Exception_Call
:= True;
7234 -- For Raise_Exception call, test first argument, if it is
7235 -- an attribute reference for a 'Identity call, then we know
7236 -- that the call cannot possibly return.
7239 Arg
: constant Node_Id
:=
7240 Original_Node
(First_Actual
(Last_Stm
));
7242 if Nkind
(Arg
) = N_Attribute_Reference
7243 and then Attribute_Name
(Arg
) = Name_Identity
7250 -- If statement, need to look inside if there is an else and check
7251 -- each constituent statement sequence for proper termination.
7253 elsif Kind
= N_If_Statement
7254 and then Present
(Else_Statements
(Last_Stm
))
7256 Check_Statement_Sequence
(Then_Statements
(Last_Stm
));
7257 Check_Statement_Sequence
(Else_Statements
(Last_Stm
));
7259 if Present
(Elsif_Parts
(Last_Stm
)) then
7261 Elsif_Part
: Node_Id
:= First
(Elsif_Parts
(Last_Stm
));
7264 while Present
(Elsif_Part
) loop
7265 Check_Statement_Sequence
(Then_Statements
(Elsif_Part
));
7273 -- Case statement, check each case for proper termination
7275 elsif Kind
= N_Case_Statement
then
7279 Case_Alt
:= First_Non_Pragma
(Alternatives
(Last_Stm
));
7280 while Present
(Case_Alt
) loop
7281 Check_Statement_Sequence
(Statements
(Case_Alt
));
7282 Next_Non_Pragma
(Case_Alt
);
7288 -- Block statement, check its handled sequence of statements
7290 elsif Kind
= N_Block_Statement
then
7296 (Handled_Statement_Sequence
(Last_Stm
), Mode
, Err1
);
7305 -- Loop statement. If there is an iteration scheme, we can definitely
7306 -- fall out of the loop. Similarly if there is an exit statement, we
7307 -- can fall out. In either case we need a following return.
7309 elsif Kind
= N_Loop_Statement
then
7310 if Present
(Iteration_Scheme
(Last_Stm
))
7311 or else Has_Exit
(Entity
(Identifier
(Last_Stm
)))
7315 -- A loop with no exit statement or iteration scheme is either
7316 -- an infinite loop, or it has some other exit (raise/return).
7317 -- In either case, no warning is required.
7323 -- Timed entry call, check entry call and delay alternatives
7325 -- Note: in expanded code, the timed entry call has been converted
7326 -- to a set of expanded statements on which the check will work
7327 -- correctly in any case.
7329 elsif Kind
= N_Timed_Entry_Call
then
7331 ECA
: constant Node_Id
:= Entry_Call_Alternative
(Last_Stm
);
7332 DCA
: constant Node_Id
:= Delay_Alternative
(Last_Stm
);
7335 -- If statement sequence of entry call alternative is missing,
7336 -- then we can definitely fall through, and we post the error
7337 -- message on the entry call alternative itself.
7339 if No
(Statements
(ECA
)) then
7342 -- If statement sequence of delay alternative is missing, then
7343 -- we can definitely fall through, and we post the error
7344 -- message on the delay alternative itself.
7346 -- Note: if both ECA and DCA are missing the return, then we
7347 -- post only one message, should be enough to fix the bugs.
7348 -- If not we will get a message next time on the DCA when the
7351 elsif No
(Statements
(DCA
)) then
7354 -- Else check both statement sequences
7357 Check_Statement_Sequence
(Statements
(ECA
));
7358 Check_Statement_Sequence
(Statements
(DCA
));
7363 -- Conditional entry call, check entry call and else part
7365 -- Note: in expanded code, the conditional entry call has been
7366 -- converted to a set of expanded statements on which the check
7367 -- will work correctly in any case.
7369 elsif Kind
= N_Conditional_Entry_Call
then
7371 ECA
: constant Node_Id
:= Entry_Call_Alternative
(Last_Stm
);
7374 -- If statement sequence of entry call alternative is missing,
7375 -- then we can definitely fall through, and we post the error
7376 -- message on the entry call alternative itself.
7378 if No
(Statements
(ECA
)) then
7381 -- Else check statement sequence and else part
7384 Check_Statement_Sequence
(Statements
(ECA
));
7385 Check_Statement_Sequence
(Else_Statements
(Last_Stm
));
7391 -- If we fall through, issue appropriate message
7394 if not Raise_Exception_Call
then
7396 ("RETURN statement missing following this statement??!",
7399 ("\Program_Error may be raised at run time??!",
7403 -- Note: we set Err even though we have not issued a warning
7404 -- because we still have a case of a missing return. This is
7405 -- an extremely marginal case, probably will never be noticed
7406 -- but we might as well get it right.
7410 -- Otherwise we have the case of a procedure marked No_Return
7413 if not Raise_Exception_Call
then
7415 ("implied return after this statement " &
7416 "will raise Program_Error??",
7419 ("\procedure & is marked as No_Return??!",
7424 RE
: constant Node_Id
:=
7425 Make_Raise_Program_Error
(Sloc
(Last_Stm
),
7426 Reason
=> PE_Implicit_Return
);
7428 Insert_After
(Last_Stm
, RE
);
7432 end Check_Statement_Sequence
;
7434 -- Start of processing for Check_Returns
7438 Check_Statement_Sequence
(Statements
(HSS
));
7440 if Present
(Exception_Handlers
(HSS
)) then
7441 Handler
:= First_Non_Pragma
(Exception_Handlers
(HSS
));
7442 while Present
(Handler
) loop
7443 Check_Statement_Sequence
(Statements
(Handler
));
7444 Next_Non_Pragma
(Handler
);
7449 ----------------------------
7450 -- Check_Subprogram_Order --
7451 ----------------------------
7453 procedure Check_Subprogram_Order
(N
: Node_Id
) is
7455 function Subprogram_Name_Greater
(S1
, S2
: String) return Boolean;
7456 -- This is used to check if S1 > S2 in the sense required by this test,
7457 -- for example nameab < namec, but name2 < name10.
7459 -----------------------------
7460 -- Subprogram_Name_Greater --
7461 -----------------------------
7463 function Subprogram_Name_Greater
(S1
, S2
: String) return Boolean is
7468 -- Deal with special case where names are identical except for a
7469 -- numerical suffix. These are handled specially, taking the numeric
7470 -- ordering from the suffix into account.
7473 while S1
(L1
) in '0' .. '9' loop
7478 while S2
(L2
) in '0' .. '9' loop
7482 -- If non-numeric parts non-equal, do straight compare
7484 if S1
(S1
'First .. L1
) /= S2
(S2
'First .. L2
) then
7487 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7488 -- that a missing suffix is treated as numeric zero in this test.
7492 while L1
< S1
'Last loop
7494 N1
:= N1
* 10 + Character'Pos (S1
(L1
)) - Character'Pos ('0');
7498 while L2
< S2
'Last loop
7500 N2
:= N2
* 10 + Character'Pos (S2
(L2
)) - Character'Pos ('0');
7505 end Subprogram_Name_Greater
;
7507 -- Start of processing for Check_Subprogram_Order
7510 -- Check body in alpha order if this is option
7513 and then Style_Check_Order_Subprograms
7514 and then Nkind
(N
) = N_Subprogram_Body
7515 and then Comes_From_Source
(N
)
7516 and then In_Extended_Main_Source_Unit
(N
)
7520 renames Scope_Stack
.Table
7521 (Scope_Stack
.Last
).Last_Subprogram_Name
;
7523 Body_Id
: constant Entity_Id
:=
7524 Defining_Entity
(Specification
(N
));
7527 Get_Decoded_Name_String
(Chars
(Body_Id
));
7530 if Subprogram_Name_Greater
7531 (LSN
.all, Name_Buffer
(1 .. Name_Len
))
7533 Style
.Subprogram_Not_In_Alpha_Order
(Body_Id
);
7539 LSN
:= new String'(Name_Buffer (1 .. Name_Len));
7542 end Check_Subprogram_Order;
7544 ------------------------------
7545 -- Check_Subtype_Conformant --
7546 ------------------------------
7548 procedure Check_Subtype_Conformant
7549 (New_Id : Entity_Id;
7551 Err_Loc : Node_Id := Empty;
7552 Skip_Controlling_Formals : Boolean := False;
7553 Get_Inst : Boolean := False)
7556 pragma Warnings (Off, Result);
7559 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7560 Skip_Controlling_Formals => Skip_Controlling_Formals,
7561 Get_Inst => Get_Inst);
7562 end Check_Subtype_Conformant;
7564 ---------------------------
7565 -- Check_Type_Conformant --
7566 ---------------------------
7568 procedure Check_Type_Conformant
7569 (New_Id : Entity_Id;
7571 Err_Loc : Node_Id := Empty)
7574 pragma Warnings (Off, Result);
7577 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7578 end Check_Type_Conformant;
7580 ---------------------------
7581 -- Can_Override_Operator --
7582 ---------------------------
7584 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7588 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7592 Typ := Base_Type (Etype (First_Formal (Subp)));
7594 -- Check explicitly that the operation is a primitive of the type
7596 return Operator_Matches_Spec (Subp, Subp)
7597 and then not Is_Generic_Type (Typ)
7598 and then Scope (Subp) = Scope (Typ)
7599 and then not Is_Class_Wide_Type (Typ);
7601 end Can_Override_Operator;
7603 ----------------------
7604 -- Conforming_Types --
7605 ----------------------
7607 function Conforming_Types
7610 Ctype : Conformance_Type;
7611 Get_Inst : Boolean := False) return Boolean
7613 Type_1 : Entity_Id := T1;
7614 Type_2 : Entity_Id := T2;
7615 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7617 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
7618 -- If neither T1 nor T2 are generic actual types, or if they are in
7619 -- different scopes (e.g. parent and child instances), then verify that
7620 -- the base types are equal. Otherwise T1 and T2 must be on the same
7621 -- subtype chain. The whole purpose of this procedure is to prevent
7622 -- spurious ambiguities in an instantiation that may arise if two
7623 -- distinct generic types are instantiated with the same actual.
7625 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7626 -- An access parameter can designate an incomplete type. If the
7627 -- incomplete type is the limited view of a type from a limited_
7628 -- with_clause, check whether the non-limited view is available. If
7629 -- it is a (non-limited) incomplete type, get the full view.
7631 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7632 -- Returns True if and only if either T1 denotes a limited view of T2
7633 -- or T2 denotes a limited view of T1. This can arise when the limited
7634 -- with view of a type is used in a subprogram declaration and the
7635 -- subprogram body is in the scope of a regular with clause for the
7636 -- same unit. In such a case, the two type entities can be considered
7637 -- identical for purposes of conformance checking.
7639 ----------------------
7640 -- Base_Types_Match --
7641 ----------------------
7643 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7644 BT1 : constant Entity_Id := Base_Type (T1);
7645 BT2 : constant Entity_Id := Base_Type (T2);
7651 elsif BT1 = BT2 then
7653 -- The following is too permissive. A more precise test should
7654 -- check that the generic actual is an ancestor subtype of the
7657 -- See code in Find_Corresponding_Spec that applies an additional
7658 -- filter to handle accidental amiguities in instances.
7660 return not Is_Generic_Actual_Type (T1)
7661 or else not Is_Generic_Actual_Type (T2)
7662 or else Scope (T1) /= Scope (T2);
7664 -- If T2 is a generic actual type it is declared as the subtype of
7665 -- the actual. If that actual is itself a subtype we need to use its
7666 -- own base type to check for compatibility.
7668 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
7671 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
7677 end Base_Types_Match;
7679 --------------------------
7680 -- Find_Designated_Type --
7681 --------------------------
7683 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7687 Desig := Directly_Designated_Type (T);
7689 if Ekind (Desig) = E_Incomplete_Type then
7691 -- If regular incomplete type, get full view if available
7693 if Present (Full_View (Desig)) then
7694 Desig := Full_View (Desig);
7696 -- If limited view of a type, get non-limited view if available,
7697 -- and check again for a regular incomplete type.
7699 elsif Present (Non_Limited_View (Desig)) then
7700 Desig := Get_Full_View (Non_Limited_View (Desig));
7705 end Find_Designated_Type;
7707 -------------------------------
7708 -- Matches_Limited_With_View --
7709 -------------------------------
7711 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7713 -- In some cases a type imported through a limited_with clause, and
7714 -- its nonlimited view are both visible, for example in an anonymous
7715 -- access-to-class-wide type in a formal. Both entities designate the
7718 if From_Limited_With (T1) and then T2 = Available_View (T1) then
7721 elsif From_Limited_With (T2) and then T1 = Available_View (T2) then
7724 elsif From_Limited_With (T1)
7725 and then From_Limited_With (T2)
7726 and then Available_View (T1) = Available_View (T2)
7733 end Matches_Limited_With_View;
7735 -- Start of processing for Conforming_Types
7738 -- The context is an instance association for a formal access-to-
7739 -- subprogram type; the formal parameter types require mapping because
7740 -- they may denote other formal parameters of the generic unit.
7743 Type_1 := Get_Instance_Of (T1);
7744 Type_2 := Get_Instance_Of (T2);
7747 -- If one of the types is a view of the other introduced by a limited
7748 -- with clause, treat these as conforming for all purposes.
7750 if Matches_Limited_With_View (T1, T2) then
7753 elsif Base_Types_Match (Type_1, Type_2) then
7754 return Ctype <= Mode_Conformant
7755 or else Subtypes_Statically_Match (Type_1, Type_2);
7757 elsif Is_Incomplete_Or_Private_Type (Type_1)
7758 and then Present (Full_View (Type_1))
7759 and then Base_Types_Match (Full_View (Type_1), Type_2)
7761 return Ctype <= Mode_Conformant
7762 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7764 elsif Ekind (Type_2) = E_Incomplete_Type
7765 and then Present (Full_View (Type_2))
7766 and then Base_Types_Match (Type_1, Full_View (Type_2))
7768 return Ctype <= Mode_Conformant
7769 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7771 elsif Is_Private_Type (Type_2)
7772 and then In_Instance
7773 and then Present (Full_View (Type_2))
7774 and then Base_Types_Match (Type_1, Full_View (Type_2))
7776 return Ctype <= Mode_Conformant
7777 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7780 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7781 -- treated recursively because they carry a signature. As far as
7782 -- conformance is concerned, convention plays no role, and either
7783 -- or both could be access to protected subprograms.
7785 Are_Anonymous_Access_To_Subprogram_Types :=
7786 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7787 E_Anonymous_Access_Protected_Subprogram_Type)
7789 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7790 E_Anonymous_Access_Protected_Subprogram_Type);
7792 -- Test anonymous access type case. For this case, static subtype
7793 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7794 -- the base types because we may have built internal subtype entities
7795 -- to handle null-excluding types (see Process_Formals).
7797 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7799 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7801 -- Ada 2005 (AI-254)
7803 or else Are_Anonymous_Access_To_Subprogram_Types
7806 Desig_1 : Entity_Id;
7807 Desig_2 : Entity_Id;
7810 -- In Ada 2005, access constant indicators must match for
7811 -- subtype conformance.
7813 if Ada_Version >= Ada_2005
7814 and then Ctype >= Subtype_Conformant
7816 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7821 Desig_1 := Find_Designated_Type (Type_1);
7822 Desig_2 := Find_Designated_Type (Type_2);
7824 -- If the context is an instance association for a formal
7825 -- access-to-subprogram type; formal access parameter designated
7826 -- types require mapping because they may denote other formal
7827 -- parameters of the generic unit.
7830 Desig_1 := Get_Instance_Of (Desig_1);
7831 Desig_2 := Get_Instance_Of (Desig_2);
7834 -- It is possible for a Class_Wide_Type to be introduced for an
7835 -- incomplete type, in which case there is a separate class_ wide
7836 -- type for the full view. The types conform if their Etypes
7837 -- conform, i.e. one may be the full view of the other. This can
7838 -- only happen in the context of an access parameter, other uses
7839 -- of an incomplete Class_Wide_Type are illegal.
7841 if Is_Class_Wide_Type (Desig_1)
7843 Is_Class_Wide_Type (Desig_2)
7847 (Etype (Base_Type (Desig_1)),
7848 Etype (Base_Type (Desig_2)), Ctype);
7850 elsif Are_Anonymous_Access_To_Subprogram_Types then
7851 if Ada_Version < Ada_2005 then
7852 return Ctype = Type_Conformant
7854 Subtypes_Statically_Match (Desig_1, Desig_2);
7856 -- We must check the conformance of the signatures themselves
7860 Conformant : Boolean;
7863 (Desig_1, Desig_2, Ctype, False, Conformant);
7869 return Base_Type (Desig_1) = Base_Type (Desig_2)
7870 and then (Ctype = Type_Conformant
7872 Subtypes_Statically_Match (Desig_1, Desig_2));
7876 -- Otherwise definitely no match
7879 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7880 and then Is_Access_Type (Type_2))
7881 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7882 and then Is_Access_Type (Type_1)))
7885 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7887 May_Hide_Profile := True;
7892 end Conforming_Types;
7894 --------------------------
7895 -- Create_Extra_Formals --
7896 --------------------------
7898 procedure Create_Extra_Formals (E : Entity_Id) is
7900 First_Extra : Entity_Id := Empty;
7901 Last_Extra : Entity_Id;
7902 Formal_Type : Entity_Id;
7903 P_Formal : Entity_Id := Empty;
7905 function Add_Extra_Formal
7906 (Assoc_Entity : Entity_Id;
7909 Suffix : String) return Entity_Id;
7910 -- Add an extra formal to the current list of formals and extra formals.
7911 -- The extra formal is added to the end of the list of extra formals,
7912 -- and also returned as the result. These formals are always of mode IN.
7913 -- The new formal has the type Typ, is declared in Scope, and its name
7914 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7915 -- The following suffixes are currently used. They should not be changed
7916 -- without coordinating with CodePeer, which makes use of these to
7917 -- provide better messages.
7919 -- O denotes the Constrained bit.
7920 -- L denotes the accessibility level.
7921 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7922 -- the full list in exp_ch6.BIP_Formal_Kind.
7924 ----------------------
7925 -- Add_Extra_Formal --
7926 ----------------------
7928 function Add_Extra_Formal
7929 (Assoc_Entity : Entity_Id;
7932 Suffix : String) return Entity_Id
7934 EF : constant Entity_Id :=
7935 Make_Defining_Identifier (Sloc (Assoc_Entity),
7936 Chars => New_External_Name (Chars (Assoc_Entity),
7940 -- A little optimization. Never generate an extra formal for the
7941 -- _init operand of an initialization procedure, since it could
7944 if Chars (Formal) = Name_uInit then
7948 Set_Ekind (EF, E_In_Parameter);
7949 Set_Actual_Subtype (EF, Typ);
7950 Set_Etype (EF, Typ);
7951 Set_Scope (EF, Scope);
7952 Set_Mechanism (EF, Default_Mechanism);
7953 Set_Formal_Validity (EF);
7955 if No (First_Extra) then
7957 Set_Extra_Formals (Scope, First_Extra);
7960 if Present (Last_Extra) then
7961 Set_Extra_Formal (Last_Extra, EF);
7967 end Add_Extra_Formal;
7969 -- Start of processing for Create_Extra_Formals
7972 -- We never generate extra formals if expansion is not active because we
7973 -- don't need them unless we are generating code.
7975 if not Expander_Active then
7979 -- No need to generate extra formals in interface thunks whose target
7980 -- primitive has no extra formals.
7982 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7986 -- If this is a derived subprogram then the subtypes of the parent
7987 -- subprogram's formal parameters will be used to determine the need
7988 -- for extra formals.
7990 if Is_Overloadable (E) and then Present (Alias (E)) then
7991 P_Formal := First_Formal (Alias (E));
7994 Last_Extra := Empty;
7995 Formal := First_Formal (E);
7996 while Present (Formal) loop
7997 Last_Extra := Formal;
7998 Next_Formal (Formal);
8001 -- If Extra_formals were already created, don't do it again. This
8002 -- situation may arise for subprogram types created as part of
8003 -- dispatching calls (see Expand_Dispatching_Call)
8005 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
8009 -- If the subprogram is a predefined dispatching subprogram then don't
8010 -- generate any extra constrained or accessibility level formals. In
8011 -- general we suppress these for internal subprograms (by not calling
8012 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
8013 -- generated stream attributes do get passed through because extra
8014 -- build-in-place formals are needed in some cases (limited 'Input
).
8016 if Is_Predefined_Internal_Operation
(E
) then
8017 goto Test_For_Func_Result_Extras
;
8020 Formal
:= First_Formal
(E
);
8021 while Present
(Formal
) loop
8023 -- Create extra formal for supporting the attribute 'Constrained.
8024 -- The case of a private type view without discriminants also
8025 -- requires the extra formal if the underlying type has defaulted
8028 if Ekind
(Formal
) /= E_In_Parameter
then
8029 if Present
(P_Formal
) then
8030 Formal_Type
:= Etype
(P_Formal
);
8032 Formal_Type
:= Etype
(Formal
);
8035 -- Do not produce extra formals for Unchecked_Union parameters.
8036 -- Jump directly to the end of the loop.
8038 if Is_Unchecked_Union
(Base_Type
(Formal_Type
)) then
8039 goto Skip_Extra_Formal_Generation
;
8042 if not Has_Discriminants
(Formal_Type
)
8043 and then Ekind
(Formal_Type
) in Private_Kind
8044 and then Present
(Underlying_Type
(Formal_Type
))
8046 Formal_Type
:= Underlying_Type
(Formal_Type
);
8049 -- Suppress the extra formal if formal's subtype is constrained or
8050 -- indefinite, or we're compiling for Ada 2012 and the underlying
8051 -- type is tagged and limited. In Ada 2012, a limited tagged type
8052 -- can have defaulted discriminants, but 'Constrained is required
8053 -- to return True, so the formal is never needed (see AI05-0214).
8054 -- Note that this ensures consistency of calling sequences for
8055 -- dispatching operations when some types in a class have defaults
8056 -- on discriminants and others do not (and requiring the extra
8057 -- formal would introduce distributed overhead).
8059 -- If the type does not have a completion yet, treat as prior to
8060 -- Ada 2012 for consistency.
8062 if Has_Discriminants
(Formal_Type
)
8063 and then not Is_Constrained
(Formal_Type
)
8064 and then not Is_Indefinite_Subtype
(Formal_Type
)
8065 and then (Ada_Version
< Ada_2012
8066 or else No
(Underlying_Type
(Formal_Type
))
8068 (Is_Limited_Type
(Formal_Type
)
8071 (Underlying_Type
(Formal_Type
)))))
8073 Set_Extra_Constrained
8074 (Formal
, Add_Extra_Formal
(Formal
, Standard_Boolean
, E
, "O"));
8078 -- Create extra formal for supporting accessibility checking. This
8079 -- is done for both anonymous access formals and formals of named
8080 -- access types that are marked as controlling formals. The latter
8081 -- case can occur when Expand_Dispatching_Call creates a subprogram
8082 -- type and substitutes the types of access-to-class-wide actuals
8083 -- for the anonymous access-to-specific-type of controlling formals.
8084 -- Base_Type is applied because in cases where there is a null
8085 -- exclusion the formal may have an access subtype.
8087 -- This is suppressed if we specifically suppress accessibility
8088 -- checks at the package level for either the subprogram, or the
8089 -- package in which it resides. However, we do not suppress it
8090 -- simply if the scope has accessibility checks suppressed, since
8091 -- this could cause trouble when clients are compiled with a
8092 -- different suppression setting. The explicit checks at the
8093 -- package level are safe from this point of view.
8095 if (Ekind
(Base_Type
(Etype
(Formal
))) = E_Anonymous_Access_Type
8096 or else (Is_Controlling_Formal
(Formal
)
8097 and then Is_Access_Type
(Base_Type
(Etype
(Formal
)))))
8099 (Explicit_Suppress
(E
, Accessibility_Check
)
8101 Explicit_Suppress
(Scope
(E
), Accessibility_Check
))
8104 or else Present
(Extra_Accessibility
(P_Formal
)))
8106 Set_Extra_Accessibility
8107 (Formal
, Add_Extra_Formal
(Formal
, Standard_Natural
, E
, "L"));
8110 -- This label is required when skipping extra formal generation for
8111 -- Unchecked_Union parameters.
8113 <<Skip_Extra_Formal_Generation
>>
8115 if Present
(P_Formal
) then
8116 Next_Formal
(P_Formal
);
8119 Next_Formal
(Formal
);
8122 <<Test_For_Func_Result_Extras
>>
8124 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8125 -- function call is ... determined by the point of call ...".
8127 if Needs_Result_Accessibility_Level
(E
) then
8128 Set_Extra_Accessibility_Of_Result
8129 (E
, Add_Extra_Formal
(E
, Standard_Natural
, E
, "L"));
8132 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8133 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8135 if Ada_Version
>= Ada_2005
and then Is_Build_In_Place_Function
(E
) then
8137 Result_Subt
: constant Entity_Id
:= Etype
(E
);
8138 Full_Subt
: constant Entity_Id
:= Available_View
(Result_Subt
);
8139 Formal_Typ
: Entity_Id
;
8141 Discard
: Entity_Id
;
8142 pragma Warnings
(Off
, Discard
);
8145 -- In the case of functions with unconstrained result subtypes,
8146 -- add a 4-state formal indicating whether the return object is
8147 -- allocated by the caller (1), or should be allocated by the
8148 -- callee on the secondary stack (2), in the global heap (3), or
8149 -- in a user-defined storage pool (4). For the moment we just use
8150 -- Natural for the type of this formal. Note that this formal
8151 -- isn't usually needed in the case where the result subtype is
8152 -- constrained, but it is needed when the function has a tagged
8153 -- result, because generally such functions can be called in a
8154 -- dispatching context and such calls must be handled like calls
8155 -- to a class-wide function.
8157 if Needs_BIP_Alloc_Form
(E
) then
8160 (E
, Standard_Natural
,
8161 E
, BIP_Formal_Suffix
(BIP_Alloc_Form
));
8163 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8164 -- use a user-defined pool. This formal is not added on
8165 -- .NET/JVM/ZFP as those targets do not support pools.
8167 if VM_Target
= No_VM
8168 and then RTE_Available
(RE_Root_Storage_Pool_Ptr
)
8172 (E
, RTE
(RE_Root_Storage_Pool_Ptr
),
8173 E
, BIP_Formal_Suffix
(BIP_Storage_Pool
));
8177 -- In the case of functions whose result type needs finalization,
8178 -- add an extra formal which represents the finalization master.
8180 if Needs_BIP_Finalization_Master
(E
) then
8183 (E
, RTE
(RE_Finalization_Master_Ptr
),
8184 E
, BIP_Formal_Suffix
(BIP_Finalization_Master
));
8187 -- When the result type contains tasks, add two extra formals: the
8188 -- master of the tasks to be created, and the caller's activation
8191 if Has_Task
(Full_Subt
) then
8194 (E
, RTE
(RE_Master_Id
),
8195 E
, BIP_Formal_Suffix
(BIP_Task_Master
));
8198 (E
, RTE
(RE_Activation_Chain_Access
),
8199 E
, BIP_Formal_Suffix
(BIP_Activation_Chain
));
8202 -- All build-in-place functions get an extra formal that will be
8203 -- passed the address of the return object within the caller.
8206 Create_Itype
(E_Anonymous_Access_Type
, E
, Scope_Id
=> Scope
(E
));
8208 Set_Directly_Designated_Type
(Formal_Typ
, Result_Subt
);
8209 Set_Etype
(Formal_Typ
, Formal_Typ
);
8210 Set_Depends_On_Private
8211 (Formal_Typ
, Has_Private_Component
(Formal_Typ
));
8212 Set_Is_Public
(Formal_Typ
, Is_Public
(Scope
(Formal_Typ
)));
8213 Set_Is_Access_Constant
(Formal_Typ
, False);
8215 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8216 -- the designated type comes from the limited view (for back-end
8219 Set_From_Limited_With
8220 (Formal_Typ
, From_Limited_With
(Result_Subt
));
8222 Layout_Type
(Formal_Typ
);
8226 (E
, Formal_Typ
, E
, BIP_Formal_Suffix
(BIP_Object_Access
));
8229 end Create_Extra_Formals
;
8231 -----------------------------
8232 -- Enter_Overloaded_Entity --
8233 -----------------------------
8235 procedure Enter_Overloaded_Entity
(S
: Entity_Id
) is
8236 E
: Entity_Id
:= Current_Entity_In_Scope
(S
);
8237 C_E
: Entity_Id
:= Current_Entity
(S
);
8241 Set_Has_Homonym
(E
);
8242 Set_Has_Homonym
(S
);
8245 Set_Is_Immediately_Visible
(S
);
8246 Set_Scope
(S
, Current_Scope
);
8248 -- Chain new entity if front of homonym in current scope, so that
8249 -- homonyms are contiguous.
8251 if Present
(E
) and then E
/= C_E
then
8252 while Homonym
(C_E
) /= E
loop
8253 C_E
:= Homonym
(C_E
);
8256 Set_Homonym
(C_E
, S
);
8260 Set_Current_Entity
(S
);
8265 if Is_Inherited_Operation
(S
) then
8266 Append_Inherited_Subprogram
(S
);
8268 Append_Entity
(S
, Current_Scope
);
8271 Set_Public_Status
(S
);
8273 if Debug_Flag_E
then
8274 Write_Str
("New overloaded entity chain: ");
8275 Write_Name
(Chars
(S
));
8278 while Present
(E
) loop
8279 Write_Str
(" "); Write_Int
(Int
(E
));
8286 -- Generate warning for hiding
8289 and then Comes_From_Source
(S
)
8290 and then In_Extended_Main_Source_Unit
(S
)
8297 -- Warn unless genuine overloading. Do not emit warning on
8298 -- hiding predefined operators in Standard (these are either an
8299 -- (artifact of our implicit declarations, or simple noise) but
8300 -- keep warning on a operator defined on a local subtype, because
8301 -- of the real danger that different operators may be applied in
8302 -- various parts of the program.
8304 -- Note that if E and S have the same scope, there is never any
8305 -- hiding. Either the two conflict, and the program is illegal,
8306 -- or S is overriding an implicit inherited subprogram.
8308 if Scope
(E
) /= Scope
(S
)
8309 and then (not Is_Overloadable
(E
)
8310 or else Subtype_Conformant
(E
, S
))
8311 and then (Is_Immediately_Visible
(E
)
8313 Is_Potentially_Use_Visible
(S
))
8315 if Scope
(E
) /= Standard_Standard
then
8316 Error_Msg_Sloc
:= Sloc
(E
);
8317 Error_Msg_N
("declaration of & hides one#?h?", S
);
8319 elsif Nkind
(S
) = N_Defining_Operator_Symbol
8321 Scope
(Base_Type
(Etype
(First_Formal
(S
)))) /= Scope
(S
)
8324 ("declaration of & hides predefined operator?h?", S
);
8329 end Enter_Overloaded_Entity
;
8331 -----------------------------
8332 -- Check_Untagged_Equality --
8333 -----------------------------
8335 procedure Check_Untagged_Equality
(Eq_Op
: Entity_Id
) is
8336 Typ
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
8337 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Eq_Op
);
8341 if Nkind
(Decl
) = N_Subprogram_Declaration
8342 and then Is_Record_Type
(Typ
)
8343 and then not Is_Tagged_Type
(Typ
)
8345 -- If the type is not declared in a package, or if we are in the
8346 -- body of the package or in some other scope, the new operation is
8347 -- not primitive, and therefore legal, though suspicious. If the
8348 -- type is a generic actual (sub)type, the operation is not primitive
8349 -- either because the base type is declared elsewhere.
8351 if Is_Frozen
(Typ
) then
8352 if Ekind
(Scope
(Typ
)) /= E_Package
8353 or else Scope
(Typ
) /= Current_Scope
8357 elsif Is_Generic_Actual_Type
(Typ
) then
8360 elsif In_Package_Body
(Scope
(Typ
)) then
8362 ("equality operator must be declared "
8363 & "before type& is frozen", Eq_Op
, Typ
);
8365 ("\move declaration to package spec", Eq_Op
);
8369 ("equality operator must be declared "
8370 & "before type& is frozen", Eq_Op
, Typ
);
8372 Obj_Decl
:= Next
(Parent
(Typ
));
8373 while Present
(Obj_Decl
) and then Obj_Decl
/= Decl
loop
8374 if Nkind
(Obj_Decl
) = N_Object_Declaration
8375 and then Etype
(Defining_Identifier
(Obj_Decl
)) = Typ
8378 ("type& is frozen by declaration??", Obj_Decl
, Typ
);
8380 ("\an equality operator cannot be declared after this "
8381 & "point (RM 4.5.2 (9.8)) (Ada 2012))??", Obj_Decl
);
8389 elsif not In_Same_List
(Parent
(Typ
), Decl
)
8390 and then not Is_Limited_Type
(Typ
)
8393 -- This makes it illegal to have a primitive equality declared in
8394 -- the private part if the type is visible.
8396 Error_Msg_N
("equality operator appears too late", Eq_Op
);
8399 end Check_Untagged_Equality
;
8401 -----------------------------
8402 -- Find_Corresponding_Spec --
8403 -----------------------------
8405 function Find_Corresponding_Spec
8407 Post_Error
: Boolean := True) return Entity_Id
8409 Spec
: constant Node_Id
:= Specification
(N
);
8410 Designator
: constant Entity_Id
:= Defining_Entity
(Spec
);
8414 function Different_Generic_Profile
(E
: Entity_Id
) return Boolean;
8415 -- Even if fully conformant, a body may depend on a generic actual when
8416 -- the spec does not, or vice versa, in which case they were distinct
8417 -- entities in the generic.
8419 -------------------------------
8420 -- Different_Generic_Profile --
8421 -------------------------------
8423 function Different_Generic_Profile
(E
: Entity_Id
) return Boolean is
8426 function Same_Generic_Actual
(T1
, T2
: Entity_Id
) return Boolean;
8427 -- Check that the types of corresponding formals have the same
8428 -- generic actual if any. We have to account for subtypes of a
8429 -- generic formal, declared between a spec and a body, which may
8430 -- appear distinct in an instance but matched in the generic.
8432 -------------------------
8433 -- Same_Generic_Actual --
8434 -------------------------
8436 function Same_Generic_Actual
(T1
, T2
: Entity_Id
) return Boolean is
8438 return Is_Generic_Actual_Type
(T1
) = Is_Generic_Actual_Type
(T2
)
8440 (Present
(Parent
(T1
))
8441 and then Comes_From_Source
(Parent
(T1
))
8442 and then Nkind
(Parent
(T1
)) = N_Subtype_Declaration
8443 and then Is_Entity_Name
(Subtype_Indication
(Parent
(T1
)))
8444 and then Entity
(Subtype_Indication
(Parent
(T1
))) = T2
);
8445 end Same_Generic_Actual
;
8447 -- Start of processing for Different_Generic_Profile
8450 if not In_Instance
then
8453 elsif Ekind
(E
) = E_Function
8454 and then not Same_Generic_Actual
(Etype
(E
), Etype
(Designator
))
8459 F1
:= First_Formal
(Designator
);
8460 F2
:= First_Formal
(E
);
8461 while Present
(F1
) loop
8462 if not Same_Generic_Actual
(Etype
(F1
), Etype
(F2
)) then
8471 end Different_Generic_Profile
;
8473 -- Start of processing for Find_Corresponding_Spec
8476 E
:= Current_Entity
(Designator
);
8477 while Present
(E
) loop
8479 -- We are looking for a matching spec. It must have the same scope,
8480 -- and the same name, and either be type conformant, or be the case
8481 -- of a library procedure spec and its body (which belong to one
8482 -- another regardless of whether they are type conformant or not).
8484 if Scope
(E
) = Current_Scope
then
8485 if Current_Scope
= Standard_Standard
8486 or else (Ekind
(E
) = Ekind
(Designator
)
8487 and then Type_Conformant
(E
, Designator
))
8489 -- Within an instantiation, we know that spec and body are
8490 -- subtype conformant, because they were subtype conformant in
8491 -- the generic. We choose the subtype-conformant entity here as
8492 -- well, to resolve spurious ambiguities in the instance that
8493 -- were not present in the generic (i.e. when two different
8494 -- types are given the same actual). If we are looking for a
8495 -- spec to match a body, full conformance is expected.
8498 Set_Convention
(Designator
, Convention
(E
));
8500 -- Skip past subprogram bodies and subprogram renamings that
8501 -- may appear to have a matching spec, but that aren't fully
8502 -- conformant with it. That can occur in cases where an
8503 -- actual type causes unrelated homographs in the instance.
8505 if Nkind_In
(N
, N_Subprogram_Body
,
8506 N_Subprogram_Renaming_Declaration
)
8507 and then Present
(Homonym
(E
))
8508 and then not Fully_Conformant
(Designator
, E
)
8512 elsif not Subtype_Conformant
(Designator
, E
) then
8515 elsif Different_Generic_Profile
(E
) then
8520 -- Ada 2012 (AI05-0165): For internally generated bodies of
8521 -- null procedures locate the internally generated spec. We
8522 -- enforce mode conformance since a tagged type may inherit
8523 -- from interfaces several null primitives which differ only
8524 -- in the mode of the formals.
8526 if not (Comes_From_Source
(E
))
8527 and then Is_Null_Procedure
(E
)
8528 and then not Mode_Conformant
(Designator
, E
)
8532 -- For null procedures coming from source that are completions,
8533 -- analysis of the generated body will establish the link.
8535 elsif Comes_From_Source
(E
)
8536 and then Nkind
(Spec
) = N_Procedure_Specification
8537 and then Null_Present
(Spec
)
8541 elsif not Has_Completion
(E
) then
8542 if Nkind
(N
) /= N_Subprogram_Body_Stub
then
8543 Set_Corresponding_Spec
(N
, E
);
8546 Set_Has_Completion
(E
);
8549 elsif Nkind
(Parent
(N
)) = N_Subunit
then
8551 -- If this is the proper body of a subunit, the completion
8552 -- flag is set when analyzing the stub.
8556 -- If E is an internal function with a controlling result that
8557 -- was created for an operation inherited by a null extension,
8558 -- it may be overridden by a body without a previous spec (one
8559 -- more reason why these should be shunned). In that case we
8560 -- remove the generated body if present, because the current
8561 -- one is the explicit overriding.
8563 elsif Ekind
(E
) = E_Function
8564 and then Ada_Version
>= Ada_2005
8565 and then not Comes_From_Source
(E
)
8566 and then Has_Controlling_Result
(E
)
8567 and then Is_Null_Extension
(Etype
(E
))
8568 and then Comes_From_Source
(Spec
)
8570 Set_Has_Completion
(E
, False);
8573 and then Nkind
(Parent
(E
)) = N_Function_Specification
8576 (Unit_Declaration_Node
8577 (Corresponding_Body
(Unit_Declaration_Node
(E
))));
8581 -- If expansion is disabled, or if the wrapper function has
8582 -- not been generated yet, this a late body overriding an
8583 -- inherited operation, or it is an overriding by some other
8584 -- declaration before the controlling result is frozen. In
8585 -- either case this is a declaration of a new entity.
8591 -- If the body already exists, then this is an error unless
8592 -- the previous declaration is the implicit declaration of a
8593 -- derived subprogram. It is also legal for an instance to
8594 -- contain type conformant overloadable declarations (but the
8595 -- generic declaration may not), per 8.3(26/2).
8597 elsif No
(Alias
(E
))
8598 and then not Is_Intrinsic_Subprogram
(E
)
8599 and then not In_Instance
8602 Error_Msg_Sloc
:= Sloc
(E
);
8604 if Is_Imported
(E
) then
8606 ("body not allowed for imported subprogram & declared#",
8609 Error_Msg_NE
("duplicate body for & declared#", N
, E
);
8613 -- Child units cannot be overloaded, so a conformance mismatch
8614 -- between body and a previous spec is an error.
8616 elsif Is_Child_Unit
(E
)
8618 Nkind
(Unit_Declaration_Node
(Designator
)) = N_Subprogram_Body
8620 Nkind
(Parent
(Unit_Declaration_Node
(Designator
))) =
8625 ("body of child unit does not match previous declaration", N
);
8633 -- On exit, we know that no previous declaration of subprogram exists
8636 end Find_Corresponding_Spec
;
8638 ----------------------
8639 -- Fully_Conformant --
8640 ----------------------
8642 function Fully_Conformant
(New_Id
, Old_Id
: Entity_Id
) return Boolean is
8645 Check_Conformance
(New_Id
, Old_Id
, Fully_Conformant
, False, Result
);
8647 end Fully_Conformant
;
8649 ----------------------------------
8650 -- Fully_Conformant_Expressions --
8651 ----------------------------------
8653 function Fully_Conformant_Expressions
8654 (Given_E1
: Node_Id
;
8655 Given_E2
: Node_Id
) return Boolean
8657 E1
: constant Node_Id
:= Original_Node
(Given_E1
);
8658 E2
: constant Node_Id
:= Original_Node
(Given_E2
);
8659 -- We always test conformance on original nodes, since it is possible
8660 -- for analysis and/or expansion to make things look as though they
8661 -- conform when they do not, e.g. by converting 1+2 into 3.
8663 function FCE
(Given_E1
, Given_E2
: Node_Id
) return Boolean
8664 renames Fully_Conformant_Expressions
;
8666 function FCL
(L1
, L2
: List_Id
) return Boolean;
8667 -- Compare elements of two lists for conformance. Elements have to be
8668 -- conformant, and actuals inserted as default parameters do not match
8669 -- explicit actuals with the same value.
8671 function FCO
(Op_Node
, Call_Node
: Node_Id
) return Boolean;
8672 -- Compare an operator node with a function call
8678 function FCL
(L1
, L2
: List_Id
) return Boolean is
8682 if L1
= No_List
then
8688 if L2
= No_List
then
8694 -- Compare two lists, skipping rewrite insertions (we want to compare
8695 -- the original trees, not the expanded versions!)
8698 if Is_Rewrite_Insertion
(N1
) then
8700 elsif Is_Rewrite_Insertion
(N2
) then
8706 elsif not FCE
(N1
, N2
) then
8719 function FCO
(Op_Node
, Call_Node
: Node_Id
) return Boolean is
8720 Actuals
: constant List_Id
:= Parameter_Associations
(Call_Node
);
8725 or else Entity
(Op_Node
) /= Entity
(Name
(Call_Node
))
8730 Act
:= First
(Actuals
);
8732 if Nkind
(Op_Node
) in N_Binary_Op
then
8733 if not FCE
(Left_Opnd
(Op_Node
), Act
) then
8740 return Present
(Act
)
8741 and then FCE
(Right_Opnd
(Op_Node
), Act
)
8742 and then No
(Next
(Act
));
8746 -- Start of processing for Fully_Conformant_Expressions
8749 -- Non-conformant if paren count does not match. Note: if some idiot
8750 -- complains that we don't do this right for more than 3 levels of
8751 -- parentheses, they will be treated with the respect they deserve!
8753 if Paren_Count
(E1
) /= Paren_Count
(E2
) then
8756 -- If same entities are referenced, then they are conformant even if
8757 -- they have different forms (RM 8.3.1(19-20)).
8759 elsif Is_Entity_Name
(E1
) and then Is_Entity_Name
(E2
) then
8760 if Present
(Entity
(E1
)) then
8761 return Entity
(E1
) = Entity
(E2
)
8762 or else (Chars
(Entity
(E1
)) = Chars
(Entity
(E2
))
8763 and then Ekind
(Entity
(E1
)) = E_Discriminant
8764 and then Ekind
(Entity
(E2
)) = E_In_Parameter
);
8766 elsif Nkind
(E1
) = N_Expanded_Name
8767 and then Nkind
(E2
) = N_Expanded_Name
8768 and then Nkind
(Selector_Name
(E1
)) = N_Character_Literal
8769 and then Nkind
(Selector_Name
(E2
)) = N_Character_Literal
8771 return Chars
(Selector_Name
(E1
)) = Chars
(Selector_Name
(E2
));
8774 -- Identifiers in component associations don't always have
8775 -- entities, but their names must conform.
8777 return Nkind
(E1
) = N_Identifier
8778 and then Nkind
(E2
) = N_Identifier
8779 and then Chars
(E1
) = Chars
(E2
);
8782 elsif Nkind
(E1
) = N_Character_Literal
8783 and then Nkind
(E2
) = N_Expanded_Name
8785 return Nkind
(Selector_Name
(E2
)) = N_Character_Literal
8786 and then Chars
(E1
) = Chars
(Selector_Name
(E2
));
8788 elsif Nkind
(E2
) = N_Character_Literal
8789 and then Nkind
(E1
) = N_Expanded_Name
8791 return Nkind
(Selector_Name
(E1
)) = N_Character_Literal
8792 and then Chars
(E2
) = Chars
(Selector_Name
(E1
));
8794 elsif Nkind
(E1
) in N_Op
and then Nkind
(E2
) = N_Function_Call
then
8795 return FCO
(E1
, E2
);
8797 elsif Nkind
(E2
) in N_Op
and then Nkind
(E1
) = N_Function_Call
then
8798 return FCO
(E2
, E1
);
8800 -- Otherwise we must have the same syntactic entity
8802 elsif Nkind
(E1
) /= Nkind
(E2
) then
8805 -- At this point, we specialize by node type
8812 FCL
(Expressions
(E1
), Expressions
(E2
))
8814 FCL
(Component_Associations
(E1
),
8815 Component_Associations
(E2
));
8818 if Nkind
(Expression
(E1
)) = N_Qualified_Expression
8820 Nkind
(Expression
(E2
)) = N_Qualified_Expression
8822 return FCE
(Expression
(E1
), Expression
(E2
));
8824 -- Check that the subtype marks and any constraints
8829 Indic1
: constant Node_Id
:= Expression
(E1
);
8830 Indic2
: constant Node_Id
:= Expression
(E2
);
8835 if Nkind
(Indic1
) /= N_Subtype_Indication
then
8837 Nkind
(Indic2
) /= N_Subtype_Indication
8838 and then Entity
(Indic1
) = Entity
(Indic2
);
8840 elsif Nkind
(Indic2
) /= N_Subtype_Indication
then
8842 Nkind
(Indic1
) /= N_Subtype_Indication
8843 and then Entity
(Indic1
) = Entity
(Indic2
);
8846 if Entity
(Subtype_Mark
(Indic1
)) /=
8847 Entity
(Subtype_Mark
(Indic2
))
8852 Elt1
:= First
(Constraints
(Constraint
(Indic1
)));
8853 Elt2
:= First
(Constraints
(Constraint
(Indic2
)));
8854 while Present
(Elt1
) and then Present
(Elt2
) loop
8855 if not FCE
(Elt1
, Elt2
) then
8868 when N_Attribute_Reference
=>
8870 Attribute_Name
(E1
) = Attribute_Name
(E2
)
8871 and then FCL
(Expressions
(E1
), Expressions
(E2
));
8875 Entity
(E1
) = Entity
(E2
)
8876 and then FCE
(Left_Opnd
(E1
), Left_Opnd
(E2
))
8877 and then FCE
(Right_Opnd
(E1
), Right_Opnd
(E2
));
8879 when N_Short_Circuit | N_Membership_Test
=>
8881 FCE
(Left_Opnd
(E1
), Left_Opnd
(E2
))
8883 FCE
(Right_Opnd
(E1
), Right_Opnd
(E2
));
8885 when N_Case_Expression
=>
8891 if not FCE
(Expression
(E1
), Expression
(E2
)) then
8895 Alt1
:= First
(Alternatives
(E1
));
8896 Alt2
:= First
(Alternatives
(E2
));
8898 if Present
(Alt1
) /= Present
(Alt2
) then
8900 elsif No
(Alt1
) then
8904 if not FCE
(Expression
(Alt1
), Expression
(Alt2
))
8905 or else not FCL
(Discrete_Choices
(Alt1
),
8906 Discrete_Choices
(Alt2
))
8917 when N_Character_Literal
=>
8919 Char_Literal_Value
(E1
) = Char_Literal_Value
(E2
);
8921 when N_Component_Association
=>
8923 FCL
(Choices
(E1
), Choices
(E2
))
8925 FCE
(Expression
(E1
), Expression
(E2
));
8927 when N_Explicit_Dereference
=>
8929 FCE
(Prefix
(E1
), Prefix
(E2
));
8931 when N_Extension_Aggregate
=>
8933 FCL
(Expressions
(E1
), Expressions
(E2
))
8934 and then Null_Record_Present
(E1
) =
8935 Null_Record_Present
(E2
)
8936 and then FCL
(Component_Associations
(E1
),
8937 Component_Associations
(E2
));
8939 when N_Function_Call
=>
8941 FCE
(Name
(E1
), Name
(E2
))
8943 FCL
(Parameter_Associations
(E1
),
8944 Parameter_Associations
(E2
));
8946 when N_If_Expression
=>
8948 FCL
(Expressions
(E1
), Expressions
(E2
));
8950 when N_Indexed_Component
=>
8952 FCE
(Prefix
(E1
), Prefix
(E2
))
8954 FCL
(Expressions
(E1
), Expressions
(E2
));
8956 when N_Integer_Literal
=>
8957 return (Intval
(E1
) = Intval
(E2
));
8962 when N_Operator_Symbol
=>
8964 Chars
(E1
) = Chars
(E2
);
8966 when N_Others_Choice
=>
8969 when N_Parameter_Association
=>
8971 Chars
(Selector_Name
(E1
)) = Chars
(Selector_Name
(E2
))
8972 and then FCE
(Explicit_Actual_Parameter
(E1
),
8973 Explicit_Actual_Parameter
(E2
));
8975 when N_Qualified_Expression
=>
8977 FCE
(Subtype_Mark
(E1
), Subtype_Mark
(E2
))
8979 FCE
(Expression
(E1
), Expression
(E2
));
8981 when N_Quantified_Expression
=>
8982 if not FCE
(Condition
(E1
), Condition
(E2
)) then
8986 if Present
(Loop_Parameter_Specification
(E1
))
8987 and then Present
(Loop_Parameter_Specification
(E2
))
8990 L1
: constant Node_Id
:=
8991 Loop_Parameter_Specification
(E1
);
8992 L2
: constant Node_Id
:=
8993 Loop_Parameter_Specification
(E2
);
8997 Reverse_Present
(L1
) = Reverse_Present
(L2
)
8999 FCE
(Defining_Identifier
(L1
),
9000 Defining_Identifier
(L2
))
9002 FCE
(Discrete_Subtype_Definition
(L1
),
9003 Discrete_Subtype_Definition
(L2
));
9006 elsif Present
(Iterator_Specification
(E1
))
9007 and then Present
(Iterator_Specification
(E2
))
9010 I1
: constant Node_Id
:= Iterator_Specification
(E1
);
9011 I2
: constant Node_Id
:= Iterator_Specification
(E2
);
9015 FCE
(Defining_Identifier
(I1
),
9016 Defining_Identifier
(I2
))
9018 Of_Present
(I1
) = Of_Present
(I2
)
9020 Reverse_Present
(I1
) = Reverse_Present
(I2
)
9021 and then FCE
(Name
(I1
), Name
(I2
))
9022 and then FCE
(Subtype_Indication
(I1
),
9023 Subtype_Indication
(I2
));
9026 -- The quantified expressions used different specifications to
9027 -- walk their respective ranges.
9035 FCE
(Low_Bound
(E1
), Low_Bound
(E2
))
9037 FCE
(High_Bound
(E1
), High_Bound
(E2
));
9039 when N_Real_Literal
=>
9040 return (Realval
(E1
) = Realval
(E2
));
9042 when N_Selected_Component
=>
9044 FCE
(Prefix
(E1
), Prefix
(E2
))
9046 FCE
(Selector_Name
(E1
), Selector_Name
(E2
));
9050 FCE
(Prefix
(E1
), Prefix
(E2
))
9052 FCE
(Discrete_Range
(E1
), Discrete_Range
(E2
));
9054 when N_String_Literal
=>
9056 S1
: constant String_Id
:= Strval
(E1
);
9057 S2
: constant String_Id
:= Strval
(E2
);
9058 L1
: constant Nat
:= String_Length
(S1
);
9059 L2
: constant Nat
:= String_Length
(S2
);
9066 for J
in 1 .. L1
loop
9067 if Get_String_Char
(S1
, J
) /=
9068 Get_String_Char
(S2
, J
)
9078 when N_Type_Conversion
=>
9080 FCE
(Subtype_Mark
(E1
), Subtype_Mark
(E2
))
9082 FCE
(Expression
(E1
), Expression
(E2
));
9086 Entity
(E1
) = Entity
(E2
)
9088 FCE
(Right_Opnd
(E1
), Right_Opnd
(E2
));
9090 when N_Unchecked_Type_Conversion
=>
9092 FCE
(Subtype_Mark
(E1
), Subtype_Mark
(E2
))
9094 FCE
(Expression
(E1
), Expression
(E2
));
9096 -- All other node types cannot appear in this context. Strictly
9097 -- we should raise a fatal internal error. Instead we just ignore
9098 -- the nodes. This means that if anyone makes a mistake in the
9099 -- expander and mucks an expression tree irretrievably, the result
9100 -- will be a failure to detect a (probably very obscure) case
9101 -- of non-conformance, which is better than bombing on some
9102 -- case where two expressions do in fact conform.
9109 end Fully_Conformant_Expressions
;
9111 ----------------------------------------
9112 -- Fully_Conformant_Discrete_Subtypes --
9113 ----------------------------------------
9115 function Fully_Conformant_Discrete_Subtypes
9116 (Given_S1
: Node_Id
;
9117 Given_S2
: Node_Id
) return Boolean
9119 S1
: constant Node_Id
:= Original_Node
(Given_S1
);
9120 S2
: constant Node_Id
:= Original_Node
(Given_S2
);
9122 function Conforming_Bounds
(B1
, B2
: Node_Id
) return Boolean;
9123 -- Special-case for a bound given by a discriminant, which in the body
9124 -- is replaced with the discriminal of the enclosing type.
9126 function Conforming_Ranges
(R1
, R2
: Node_Id
) return Boolean;
9127 -- Check both bounds
9129 -----------------------
9130 -- Conforming_Bounds --
9131 -----------------------
9133 function Conforming_Bounds
(B1
, B2
: Node_Id
) return Boolean is
9135 if Is_Entity_Name
(B1
)
9136 and then Is_Entity_Name
(B2
)
9137 and then Ekind
(Entity
(B1
)) = E_Discriminant
9139 return Chars
(B1
) = Chars
(B2
);
9142 return Fully_Conformant_Expressions
(B1
, B2
);
9144 end Conforming_Bounds
;
9146 -----------------------
9147 -- Conforming_Ranges --
9148 -----------------------
9150 function Conforming_Ranges
(R1
, R2
: Node_Id
) return Boolean is
9153 Conforming_Bounds
(Low_Bound
(R1
), Low_Bound
(R2
))
9155 Conforming_Bounds
(High_Bound
(R1
), High_Bound
(R2
));
9156 end Conforming_Ranges
;
9158 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9161 if Nkind
(S1
) /= Nkind
(S2
) then
9164 elsif Is_Entity_Name
(S1
) then
9165 return Entity
(S1
) = Entity
(S2
);
9167 elsif Nkind
(S1
) = N_Range
then
9168 return Conforming_Ranges
(S1
, S2
);
9170 elsif Nkind
(S1
) = N_Subtype_Indication
then
9172 Entity
(Subtype_Mark
(S1
)) = Entity
(Subtype_Mark
(S2
))
9175 (Range_Expression
(Constraint
(S1
)),
9176 Range_Expression
(Constraint
(S2
)));
9180 end Fully_Conformant_Discrete_Subtypes
;
9182 --------------------
9183 -- Install_Entity --
9184 --------------------
9186 procedure Install_Entity
(E
: Entity_Id
) is
9187 Prev
: constant Entity_Id
:= Current_Entity
(E
);
9189 Set_Is_Immediately_Visible
(E
);
9190 Set_Current_Entity
(E
);
9191 Set_Homonym
(E
, Prev
);
9194 ---------------------
9195 -- Install_Formals --
9196 ---------------------
9198 procedure Install_Formals
(Id
: Entity_Id
) is
9201 F
:= First_Formal
(Id
);
9202 while Present
(F
) loop
9206 end Install_Formals
;
9208 -----------------------------
9209 -- Is_Interface_Conformant --
9210 -----------------------------
9212 function Is_Interface_Conformant
9213 (Tagged_Type
: Entity_Id
;
9214 Iface_Prim
: Entity_Id
;
9215 Prim
: Entity_Id
) return Boolean
9217 -- The operation may in fact be an inherited (implicit) operation
9218 -- rather than the original interface primitive, so retrieve the
9219 -- ultimate ancestor.
9221 Iface
: constant Entity_Id
:=
9222 Find_Dispatching_Type
(Ultimate_Alias
(Iface_Prim
));
9223 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Prim
);
9225 function Controlling_Formal
(Prim
: Entity_Id
) return Entity_Id
;
9226 -- Return the controlling formal of Prim
9228 ------------------------
9229 -- Controlling_Formal --
9230 ------------------------
9232 function Controlling_Formal
(Prim
: Entity_Id
) return Entity_Id
is
9236 E
:= First_Entity
(Prim
);
9237 while Present
(E
) loop
9238 if Is_Formal
(E
) and then Is_Controlling_Formal
(E
) then
9246 end Controlling_Formal
;
9250 Iface_Ctrl_F
: constant Entity_Id
:= Controlling_Formal
(Iface_Prim
);
9251 Prim_Ctrl_F
: constant Entity_Id
:= Controlling_Formal
(Prim
);
9253 -- Start of processing for Is_Interface_Conformant
9256 pragma Assert
(Is_Subprogram
(Iface_Prim
)
9257 and then Is_Subprogram
(Prim
)
9258 and then Is_Dispatching_Operation
(Iface_Prim
)
9259 and then Is_Dispatching_Operation
(Prim
));
9261 pragma Assert
(Is_Interface
(Iface
)
9262 or else (Present
(Alias
(Iface_Prim
))
9265 (Find_Dispatching_Type
(Ultimate_Alias
(Iface_Prim
)))));
9267 if Prim
= Iface_Prim
9268 or else not Is_Subprogram
(Prim
)
9269 or else Ekind
(Prim
) /= Ekind
(Iface_Prim
)
9270 or else not Is_Dispatching_Operation
(Prim
)
9271 or else Scope
(Prim
) /= Scope
(Tagged_Type
)
9273 or else Base_Type
(Typ
) /= Base_Type
(Tagged_Type
)
9274 or else not Primitive_Names_Match
(Iface_Prim
, Prim
)
9278 -- The mode of the controlling formals must match
9280 elsif Present
(Iface_Ctrl_F
)
9281 and then Present
(Prim_Ctrl_F
)
9282 and then Ekind
(Iface_Ctrl_F
) /= Ekind
(Prim_Ctrl_F
)
9286 -- Case of a procedure, or a function whose result type matches the
9287 -- result type of the interface primitive, or a function that has no
9288 -- controlling result (I or access I).
9290 elsif Ekind
(Iface_Prim
) = E_Procedure
9291 or else Etype
(Prim
) = Etype
(Iface_Prim
)
9292 or else not Has_Controlling_Result
(Prim
)
9294 return Type_Conformant
9295 (Iface_Prim
, Prim
, Skip_Controlling_Formals
=> True);
9297 -- Case of a function returning an interface, or an access to one. Check
9298 -- that the return types correspond.
9300 elsif Implements_Interface
(Typ
, Iface
) then
9301 if (Ekind
(Etype
(Prim
)) = E_Anonymous_Access_Type
)
9303 (Ekind
(Etype
(Iface_Prim
)) = E_Anonymous_Access_Type
)
9308 Type_Conformant
(Prim
, Ultimate_Alias
(Iface_Prim
),
9309 Skip_Controlling_Formals
=> True);
9315 end Is_Interface_Conformant
;
9317 ---------------------------------
9318 -- Is_Non_Overriding_Operation --
9319 ---------------------------------
9321 function Is_Non_Overriding_Operation
9322 (Prev_E
: Entity_Id
;
9323 New_E
: Entity_Id
) return Boolean
9327 G_Typ
: Entity_Id
:= Empty
;
9329 function Get_Generic_Parent_Type
(F_Typ
: Entity_Id
) return Entity_Id
;
9330 -- If F_Type is a derived type associated with a generic actual subtype,
9331 -- then return its Generic_Parent_Type attribute, else return Empty.
9333 function Types_Correspond
9334 (P_Type
: Entity_Id
;
9335 N_Type
: Entity_Id
) return Boolean;
9336 -- Returns true if and only if the types (or designated types in the
9337 -- case of anonymous access types) are the same or N_Type is derived
9338 -- directly or indirectly from P_Type.
9340 -----------------------------
9341 -- Get_Generic_Parent_Type --
9342 -----------------------------
9344 function Get_Generic_Parent_Type
(F_Typ
: Entity_Id
) return Entity_Id
is
9350 if Is_Derived_Type
(F_Typ
)
9351 and then Nkind
(Parent
(F_Typ
)) = N_Full_Type_Declaration
9353 -- The tree must be traversed to determine the parent subtype in
9354 -- the generic unit, which unfortunately isn't always available
9355 -- via semantic attributes. ??? (Note: The use of Original_Node
9356 -- is needed for cases where a full derived type has been
9359 Defn
:= Type_Definition
(Original_Node
(Parent
(F_Typ
)));
9360 if Nkind
(Defn
) = N_Derived_Type_Definition
then
9361 Indic
:= Subtype_Indication
(Defn
);
9363 if Nkind
(Indic
) = N_Subtype_Indication
then
9364 G_Typ
:= Entity
(Subtype_Mark
(Indic
));
9366 G_Typ
:= Entity
(Indic
);
9369 if Nkind
(Parent
(G_Typ
)) = N_Subtype_Declaration
9370 and then Present
(Generic_Parent_Type
(Parent
(G_Typ
)))
9372 return Generic_Parent_Type
(Parent
(G_Typ
));
9378 end Get_Generic_Parent_Type
;
9380 ----------------------
9381 -- Types_Correspond --
9382 ----------------------
9384 function Types_Correspond
9385 (P_Type
: Entity_Id
;
9386 N_Type
: Entity_Id
) return Boolean
9388 Prev_Type
: Entity_Id
:= Base_Type
(P_Type
);
9389 New_Type
: Entity_Id
:= Base_Type
(N_Type
);
9392 if Ekind
(Prev_Type
) = E_Anonymous_Access_Type
then
9393 Prev_Type
:= Designated_Type
(Prev_Type
);
9396 if Ekind
(New_Type
) = E_Anonymous_Access_Type
then
9397 New_Type
:= Designated_Type
(New_Type
);
9400 if Prev_Type
= New_Type
then
9403 elsif not Is_Class_Wide_Type
(New_Type
) then
9404 while Etype
(New_Type
) /= New_Type
loop
9405 New_Type
:= Etype
(New_Type
);
9406 if New_Type
= Prev_Type
then
9412 end Types_Correspond
;
9414 -- Start of processing for Is_Non_Overriding_Operation
9417 -- In the case where both operations are implicit derived subprograms
9418 -- then neither overrides the other. This can only occur in certain
9419 -- obscure cases (e.g., derivation from homographs created in a generic
9422 if Present
(Alias
(Prev_E
)) and then Present
(Alias
(New_E
)) then
9425 elsif Ekind
(Current_Scope
) = E_Package
9426 and then Is_Generic_Instance
(Current_Scope
)
9427 and then In_Private_Part
(Current_Scope
)
9428 and then Comes_From_Source
(New_E
)
9430 -- We examine the formals and result type of the inherited operation,
9431 -- to determine whether their type is derived from (the instance of)
9432 -- a generic type. The first such formal or result type is the one
9435 Formal
:= First_Formal
(Prev_E
);
9436 while Present
(Formal
) loop
9437 F_Typ
:= Base_Type
(Etype
(Formal
));
9439 if Ekind
(F_Typ
) = E_Anonymous_Access_Type
then
9440 F_Typ
:= Designated_Type
(F_Typ
);
9443 G_Typ
:= Get_Generic_Parent_Type
(F_Typ
);
9444 exit when Present
(G_Typ
);
9446 Next_Formal
(Formal
);
9449 if No
(G_Typ
) and then Ekind
(Prev_E
) = E_Function
then
9450 G_Typ
:= Get_Generic_Parent_Type
(Base_Type
(Etype
(Prev_E
)));
9457 -- If the generic type is a private type, then the original operation
9458 -- was not overriding in the generic, because there was no primitive
9459 -- operation to override.
9461 if Nkind
(Parent
(G_Typ
)) = N_Formal_Type_Declaration
9462 and then Nkind
(Formal_Type_Definition
(Parent
(G_Typ
))) =
9463 N_Formal_Private_Type_Definition
9467 -- The generic parent type is the ancestor of a formal derived
9468 -- type declaration. We need to check whether it has a primitive
9469 -- operation that should be overridden by New_E in the generic.
9473 P_Formal
: Entity_Id
;
9474 N_Formal
: Entity_Id
;
9478 Prim_Elt
: Elmt_Id
:= First_Elmt
(Primitive_Operations
(G_Typ
));
9481 while Present
(Prim_Elt
) loop
9482 P_Prim
:= Node
(Prim_Elt
);
9484 if Chars
(P_Prim
) = Chars
(New_E
)
9485 and then Ekind
(P_Prim
) = Ekind
(New_E
)
9487 P_Formal
:= First_Formal
(P_Prim
);
9488 N_Formal
:= First_Formal
(New_E
);
9489 while Present
(P_Formal
) and then Present
(N_Formal
) loop
9490 P_Typ
:= Etype
(P_Formal
);
9491 N_Typ
:= Etype
(N_Formal
);
9493 if not Types_Correspond
(P_Typ
, N_Typ
) then
9497 Next_Entity
(P_Formal
);
9498 Next_Entity
(N_Formal
);
9501 -- Found a matching primitive operation belonging to the
9502 -- formal ancestor type, so the new subprogram is
9506 and then No
(N_Formal
)
9507 and then (Ekind
(New_E
) /= E_Function
9510 (Etype
(P_Prim
), Etype
(New_E
)))
9516 Next_Elmt
(Prim_Elt
);
9519 -- If no match found, then the new subprogram does not override
9520 -- in the generic (nor in the instance).
9522 -- If the type in question is not abstract, and the subprogram
9523 -- is, this will be an error if the new operation is in the
9524 -- private part of the instance. Emit a warning now, which will
9525 -- make the subsequent error message easier to understand.
9527 if not Is_Abstract_Type
(F_Typ
)
9528 and then Is_Abstract_Subprogram
(Prev_E
)
9529 and then In_Private_Part
(Current_Scope
)
9531 Error_Msg_Node_2
:= F_Typ
;
9533 ("private operation& in generic unit does not override " &
9534 "any primitive operation of& (RM 12.3 (18))??",
9544 end Is_Non_Overriding_Operation
;
9546 -------------------------------------
9547 -- List_Inherited_Pre_Post_Aspects --
9548 -------------------------------------
9550 procedure List_Inherited_Pre_Post_Aspects
(E
: Entity_Id
) is
9552 if Opt
.List_Inherited_Aspects
9553 and then (Is_Subprogram
(E
) or else Is_Generic_Subprogram
(E
))
9556 Inherited
: constant Subprogram_List
:= Inherited_Subprograms
(E
);
9560 for J
in Inherited
'Range loop
9561 P
:= Pre_Post_Conditions
(Contract
(Inherited
(J
)));
9562 while Present
(P
) loop
9563 Error_Msg_Sloc
:= Sloc
(P
);
9565 if Class_Present
(P
) and then not Split_PPC
(P
) then
9566 if Pragma_Name
(P
) = Name_Precondition
then
9568 ("info: & inherits `Pre''Class` aspect from #?L?",
9572 ("info: & inherits `Post''Class` aspect from #?L?",
9577 P
:= Next_Pragma
(P
);
9582 end List_Inherited_Pre_Post_Aspects
;
9584 ------------------------------
9585 -- Make_Inequality_Operator --
9586 ------------------------------
9588 -- S is the defining identifier of an equality operator. We build a
9589 -- subprogram declaration with the right signature. This operation is
9590 -- intrinsic, because it is always expanded as the negation of the
9591 -- call to the equality function.
9593 procedure Make_Inequality_Operator
(S
: Entity_Id
) is
9594 Loc
: constant Source_Ptr
:= Sloc
(S
);
9597 Op_Name
: Entity_Id
;
9599 FF
: constant Entity_Id
:= First_Formal
(S
);
9600 NF
: constant Entity_Id
:= Next_Formal
(FF
);
9603 -- Check that equality was properly defined, ignore call if not
9610 A
: constant Entity_Id
:=
9611 Make_Defining_Identifier
(Sloc
(FF
),
9612 Chars
=> Chars
(FF
));
9614 B
: constant Entity_Id
:=
9615 Make_Defining_Identifier
(Sloc
(NF
),
9616 Chars
=> Chars
(NF
));
9619 Op_Name
:= Make_Defining_Operator_Symbol
(Loc
, Name_Op_Ne
);
9621 Formals
:= New_List
(
9622 Make_Parameter_Specification
(Loc
,
9623 Defining_Identifier
=> A
,
9625 New_Reference_To
(Etype
(First_Formal
(S
)),
9626 Sloc
(Etype
(First_Formal
(S
))))),
9628 Make_Parameter_Specification
(Loc
,
9629 Defining_Identifier
=> B
,
9631 New_Reference_To
(Etype
(Next_Formal
(First_Formal
(S
))),
9632 Sloc
(Etype
(Next_Formal
(First_Formal
(S
)))))));
9635 Make_Subprogram_Declaration
(Loc
,
9637 Make_Function_Specification
(Loc
,
9638 Defining_Unit_Name
=> Op_Name
,
9639 Parameter_Specifications
=> Formals
,
9640 Result_Definition
=>
9641 New_Reference_To
(Standard_Boolean
, Loc
)));
9643 -- Insert inequality right after equality if it is explicit or after
9644 -- the derived type when implicit. These entities are created only
9645 -- for visibility purposes, and eventually replaced in the course
9646 -- of expansion, so they do not need to be attached to the tree and
9647 -- seen by the back-end. Keeping them internal also avoids spurious
9648 -- freezing problems. The declaration is inserted in the tree for
9649 -- analysis, and removed afterwards. If the equality operator comes
9650 -- from an explicit declaration, attach the inequality immediately
9651 -- after. Else the equality is inherited from a derived type
9652 -- declaration, so insert inequality after that declaration.
9654 if No
(Alias
(S
)) then
9655 Insert_After
(Unit_Declaration_Node
(S
), Decl
);
9656 elsif Is_List_Member
(Parent
(S
)) then
9657 Insert_After
(Parent
(S
), Decl
);
9659 Insert_After
(Parent
(Etype
(First_Formal
(S
))), Decl
);
9662 Mark_Rewrite_Insertion
(Decl
);
9663 Set_Is_Intrinsic_Subprogram
(Op_Name
);
9666 Set_Has_Completion
(Op_Name
);
9667 Set_Corresponding_Equality
(Op_Name
, S
);
9668 Set_Is_Abstract_Subprogram
(Op_Name
, Is_Abstract_Subprogram
(S
));
9670 end Make_Inequality_Operator
;
9672 ----------------------
9673 -- May_Need_Actuals --
9674 ----------------------
9676 procedure May_Need_Actuals
(Fun
: Entity_Id
) is
9681 F
:= First_Formal
(Fun
);
9683 while Present
(F
) loop
9684 if No
(Default_Value
(F
)) then
9692 Set_Needs_No_Actuals
(Fun
, B
);
9693 end May_Need_Actuals
;
9695 ---------------------
9696 -- Mode_Conformant --
9697 ---------------------
9699 function Mode_Conformant
(New_Id
, Old_Id
: Entity_Id
) return Boolean is
9702 Check_Conformance
(New_Id
, Old_Id
, Mode_Conformant
, False, Result
);
9704 end Mode_Conformant
;
9706 ---------------------------
9707 -- New_Overloaded_Entity --
9708 ---------------------------
9710 procedure New_Overloaded_Entity
9712 Derived_Type
: Entity_Id
:= Empty
)
9714 Overridden_Subp
: Entity_Id
:= Empty
;
9715 -- Set if the current scope has an operation that is type-conformant
9716 -- with S, and becomes hidden by S.
9718 Is_Primitive_Subp
: Boolean;
9719 -- Set to True if the new subprogram is primitive
9722 -- Entity that S overrides
9724 Prev_Vis
: Entity_Id
:= Empty
;
9725 -- Predecessor of E in Homonym chain
9727 procedure Check_For_Primitive_Subprogram
9728 (Is_Primitive
: out Boolean;
9729 Is_Overriding
: Boolean := False);
9730 -- If the subprogram being analyzed is a primitive operation of the type
9731 -- of a formal or result, set the Has_Primitive_Operations flag on the
9732 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9733 -- corresponding flag on the entity itself for later use.
9735 procedure Check_Synchronized_Overriding
9736 (Def_Id
: Entity_Id
;
9737 Overridden_Subp
: out Entity_Id
);
9738 -- First determine if Def_Id is an entry or a subprogram either defined
9739 -- in the scope of a task or protected type, or is a primitive of such
9740 -- a type. Check whether Def_Id overrides a subprogram of an interface
9741 -- implemented by the synchronized type, return the overridden entity
9744 function Is_Private_Declaration
(E
: Entity_Id
) return Boolean;
9745 -- Check that E is declared in the private part of the current package,
9746 -- or in the package body, where it may hide a previous declaration.
9747 -- We can't use In_Private_Part by itself because this flag is also
9748 -- set when freezing entities, so we must examine the place of the
9749 -- declaration in the tree, and recognize wrapper packages as well.
9751 function Is_Overriding_Alias
9753 New_E
: Entity_Id
) return Boolean;
9754 -- Check whether new subprogram and old subprogram are both inherited
9755 -- from subprograms that have distinct dispatch table entries. This can
9756 -- occur with derivations from instances with accidental homonyms. The
9757 -- function is conservative given that the converse is only true within
9758 -- instances that contain accidental overloadings.
9760 ------------------------------------
9761 -- Check_For_Primitive_Subprogram --
9762 ------------------------------------
9764 procedure Check_For_Primitive_Subprogram
9765 (Is_Primitive
: out Boolean;
9766 Is_Overriding
: Boolean := False)
9772 function Visible_Part_Type
(T
: Entity_Id
) return Boolean;
9773 -- Returns true if T is declared in the visible part of the current
9774 -- package scope; otherwise returns false. Assumes that T is declared
9777 procedure Check_Private_Overriding
(T
: Entity_Id
);
9778 -- Checks that if a primitive abstract subprogram of a visible
9779 -- abstract type is declared in a private part, then it must override
9780 -- an abstract subprogram declared in the visible part. Also checks
9781 -- that if a primitive function with a controlling result is declared
9782 -- in a private part, then it must override a function declared in
9783 -- the visible part.
9785 ------------------------------
9786 -- Check_Private_Overriding --
9787 ------------------------------
9789 procedure Check_Private_Overriding
(T
: Entity_Id
) is
9791 if Is_Package_Or_Generic_Package
(Current_Scope
)
9792 and then In_Private_Part
(Current_Scope
)
9793 and then Visible_Part_Type
(T
)
9794 and then not In_Instance
9796 if Is_Abstract_Type
(T
)
9797 and then Is_Abstract_Subprogram
(S
)
9798 and then (not Is_Overriding
9799 or else not Is_Abstract_Subprogram
(E
))
9802 ("abstract subprograms must be visible "
9803 & "(RM 3.9.3(10))!", S
);
9805 elsif Ekind
(S
) = E_Function
and then not Is_Overriding
then
9806 if Is_Tagged_Type
(T
) and then T
= Base_Type
(Etype
(S
)) then
9808 ("private function with tagged result must"
9809 & " override visible-part function", S
);
9811 ("\move subprogram to the visible part"
9812 & " (RM 3.9.3(10))", S
);
9814 -- AI05-0073: extend this test to the case of a function
9815 -- with a controlling access result.
9817 elsif Ekind
(Etype
(S
)) = E_Anonymous_Access_Type
9818 and then Is_Tagged_Type
(Designated_Type
(Etype
(S
)))
9820 not Is_Class_Wide_Type
(Designated_Type
(Etype
(S
)))
9821 and then Ada_Version
>= Ada_2012
9824 ("private function with controlling access result "
9825 & "must override visible-part function", S
);
9827 ("\move subprogram to the visible part"
9828 & " (RM 3.9.3(10))", S
);
9832 end Check_Private_Overriding
;
9834 -----------------------
9835 -- Visible_Part_Type --
9836 -----------------------
9838 function Visible_Part_Type
(T
: Entity_Id
) return Boolean is
9839 P
: constant Node_Id
:= Unit_Declaration_Node
(Scope
(T
));
9843 -- If the entity is a private type, then it must be declared in a
9846 if Ekind
(T
) in Private_Kind
then
9850 -- Otherwise, we traverse the visible part looking for its
9851 -- corresponding declaration. We cannot use the declaration
9852 -- node directly because in the private part the entity of a
9853 -- private type is the one in the full view, which does not
9854 -- indicate that it is the completion of something visible.
9856 N
:= First
(Visible_Declarations
(Specification
(P
)));
9857 while Present
(N
) loop
9858 if Nkind
(N
) = N_Full_Type_Declaration
9859 and then Present
(Defining_Identifier
(N
))
9860 and then T
= Defining_Identifier
(N
)
9864 elsif Nkind_In
(N
, N_Private_Type_Declaration
,
9865 N_Private_Extension_Declaration
)
9866 and then Present
(Defining_Identifier
(N
))
9867 and then T
= Full_View
(Defining_Identifier
(N
))
9876 end Visible_Part_Type
;
9878 -- Start of processing for Check_For_Primitive_Subprogram
9881 Is_Primitive
:= False;
9883 if not Comes_From_Source
(S
) then
9886 -- If subprogram is at library level, it is not primitive operation
9888 elsif Current_Scope
= Standard_Standard
then
9891 elsif (Is_Package_Or_Generic_Package
(Current_Scope
)
9892 and then not In_Package_Body
(Current_Scope
))
9893 or else Is_Overriding
9895 -- For function, check return type
9897 if Ekind
(S
) = E_Function
then
9898 if Ekind
(Etype
(S
)) = E_Anonymous_Access_Type
then
9899 F_Typ
:= Designated_Type
(Etype
(S
));
9904 B_Typ
:= Base_Type
(F_Typ
);
9906 if Scope
(B_Typ
) = Current_Scope
9907 and then not Is_Class_Wide_Type
(B_Typ
)
9908 and then not Is_Generic_Type
(B_Typ
)
9910 Is_Primitive
:= True;
9911 Set_Has_Primitive_Operations
(B_Typ
);
9912 Set_Is_Primitive
(S
);
9913 Check_Private_Overriding
(B_Typ
);
9917 -- For all subprograms, check formals
9919 Formal
:= First_Formal
(S
);
9920 while Present
(Formal
) loop
9921 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
9922 F_Typ
:= Designated_Type
(Etype
(Formal
));
9924 F_Typ
:= Etype
(Formal
);
9927 B_Typ
:= Base_Type
(F_Typ
);
9929 if Ekind
(B_Typ
) = E_Access_Subtype
then
9930 B_Typ
:= Base_Type
(B_Typ
);
9933 if Scope
(B_Typ
) = Current_Scope
9934 and then not Is_Class_Wide_Type
(B_Typ
)
9935 and then not Is_Generic_Type
(B_Typ
)
9937 Is_Primitive
:= True;
9938 Set_Is_Primitive
(S
);
9939 Set_Has_Primitive_Operations
(B_Typ
);
9940 Check_Private_Overriding
(B_Typ
);
9943 Next_Formal
(Formal
);
9946 -- Special case: An equality function can be redefined for a type
9947 -- occurring in a declarative part, and won't otherwise be treated as
9948 -- a primitive because it doesn't occur in a package spec and doesn't
9949 -- override an inherited subprogram. It's important that we mark it
9950 -- primitive so it can be returned by Collect_Primitive_Operations
9951 -- and be used in composing the equality operation of later types
9952 -- that have a component of the type.
9954 elsif Chars
(S
) = Name_Op_Eq
9955 and then Etype
(S
) = Standard_Boolean
9957 B_Typ
:= Base_Type
(Etype
(First_Formal
(S
)));
9959 if Scope
(B_Typ
) = Current_Scope
9961 Base_Type
(Etype
(Next_Formal
(First_Formal
(S
)))) = B_Typ
9962 and then not Is_Limited_Type
(B_Typ
)
9964 Is_Primitive
:= True;
9965 Set_Is_Primitive
(S
);
9966 Set_Has_Primitive_Operations
(B_Typ
);
9967 Check_Private_Overriding
(B_Typ
);
9970 end Check_For_Primitive_Subprogram
;
9972 -----------------------------------
9973 -- Check_Synchronized_Overriding --
9974 -----------------------------------
9976 procedure Check_Synchronized_Overriding
9977 (Def_Id
: Entity_Id
;
9978 Overridden_Subp
: out Entity_Id
)
9980 Ifaces_List
: Elist_Id
;
9984 function Matches_Prefixed_View_Profile
9985 (Prim_Params
: List_Id
;
9986 Iface_Params
: List_Id
) return Boolean;
9987 -- Determine whether a subprogram's parameter profile Prim_Params
9988 -- matches that of a potentially overridden interface subprogram
9989 -- Iface_Params. Also determine if the type of first parameter of
9990 -- Iface_Params is an implemented interface.
9992 -----------------------------------
9993 -- Matches_Prefixed_View_Profile --
9994 -----------------------------------
9996 function Matches_Prefixed_View_Profile
9997 (Prim_Params
: List_Id
;
9998 Iface_Params
: List_Id
) return Boolean
10000 Iface_Id
: Entity_Id
;
10001 Iface_Param
: Node_Id
;
10002 Iface_Typ
: Entity_Id
;
10003 Prim_Id
: Entity_Id
;
10004 Prim_Param
: Node_Id
;
10005 Prim_Typ
: Entity_Id
;
10007 function Is_Implemented
10008 (Ifaces_List
: Elist_Id
;
10009 Iface
: Entity_Id
) return Boolean;
10010 -- Determine if Iface is implemented by the current task or
10013 --------------------
10014 -- Is_Implemented --
10015 --------------------
10017 function Is_Implemented
10018 (Ifaces_List
: Elist_Id
;
10019 Iface
: Entity_Id
) return Boolean
10021 Iface_Elmt
: Elmt_Id
;
10024 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
10025 while Present
(Iface_Elmt
) loop
10026 if Node
(Iface_Elmt
) = Iface
then
10030 Next_Elmt
(Iface_Elmt
);
10034 end Is_Implemented
;
10036 -- Start of processing for Matches_Prefixed_View_Profile
10039 Iface_Param
:= First
(Iface_Params
);
10040 Iface_Typ
:= Etype
(Defining_Identifier
(Iface_Param
));
10042 if Is_Access_Type
(Iface_Typ
) then
10043 Iface_Typ
:= Designated_Type
(Iface_Typ
);
10046 Prim_Param
:= First
(Prim_Params
);
10048 -- The first parameter of the potentially overridden subprogram
10049 -- must be an interface implemented by Prim.
10051 if not Is_Interface
(Iface_Typ
)
10052 or else not Is_Implemented
(Ifaces_List
, Iface_Typ
)
10057 -- The checks on the object parameters are done, move onto the
10058 -- rest of the parameters.
10060 if not In_Scope
then
10061 Prim_Param
:= Next
(Prim_Param
);
10064 Iface_Param
:= Next
(Iface_Param
);
10065 while Present
(Iface_Param
) and then Present
(Prim_Param
) loop
10066 Iface_Id
:= Defining_Identifier
(Iface_Param
);
10067 Iface_Typ
:= Find_Parameter_Type
(Iface_Param
);
10069 Prim_Id
:= Defining_Identifier
(Prim_Param
);
10070 Prim_Typ
:= Find_Parameter_Type
(Prim_Param
);
10072 if Ekind
(Iface_Typ
) = E_Anonymous_Access_Type
10073 and then Ekind
(Prim_Typ
) = E_Anonymous_Access_Type
10074 and then Is_Concurrent_Type
(Designated_Type
(Prim_Typ
))
10076 Iface_Typ
:= Designated_Type
(Iface_Typ
);
10077 Prim_Typ
:= Designated_Type
(Prim_Typ
);
10080 -- Case of multiple interface types inside a parameter profile
10082 -- (Obj_Param : in out Iface; ...; Param : Iface)
10084 -- If the interface type is implemented, then the matching type
10085 -- in the primitive should be the implementing record type.
10087 if Ekind
(Iface_Typ
) = E_Record_Type
10088 and then Is_Interface
(Iface_Typ
)
10089 and then Is_Implemented
(Ifaces_List
, Iface_Typ
)
10091 if Prim_Typ
/= Typ
then
10095 -- The two parameters must be both mode and subtype conformant
10097 elsif Ekind
(Iface_Id
) /= Ekind
(Prim_Id
)
10099 Conforming_Types
(Iface_Typ
, Prim_Typ
, Subtype_Conformant
)
10104 Next
(Iface_Param
);
10108 -- One of the two lists contains more parameters than the other
10110 if Present
(Iface_Param
) or else Present
(Prim_Param
) then
10115 end Matches_Prefixed_View_Profile
;
10117 -- Start of processing for Check_Synchronized_Overriding
10120 Overridden_Subp
:= Empty
;
10122 -- Def_Id must be an entry or a subprogram. We should skip predefined
10123 -- primitives internally generated by the frontend; however at this
10124 -- stage predefined primitives are still not fully decorated. As a
10125 -- minor optimization we skip here internally generated subprograms.
10127 if (Ekind
(Def_Id
) /= E_Entry
10128 and then Ekind
(Def_Id
) /= E_Function
10129 and then Ekind
(Def_Id
) /= E_Procedure
)
10130 or else not Comes_From_Source
(Def_Id
)
10135 -- Search for the concurrent declaration since it contains the list
10136 -- of all implemented interfaces. In this case, the subprogram is
10137 -- declared within the scope of a protected or a task type.
10139 if Present
(Scope
(Def_Id
))
10140 and then Is_Concurrent_Type
(Scope
(Def_Id
))
10141 and then not Is_Generic_Actual_Type
(Scope
(Def_Id
))
10143 Typ
:= Scope
(Def_Id
);
10146 -- The enclosing scope is not a synchronized type and the subprogram
10149 elsif No
(First_Formal
(Def_Id
)) then
10152 -- The subprogram has formals and hence it may be a primitive of a
10153 -- concurrent type.
10156 Typ
:= Etype
(First_Formal
(Def_Id
));
10158 if Is_Access_Type
(Typ
) then
10159 Typ
:= Directly_Designated_Type
(Typ
);
10162 if Is_Concurrent_Type
(Typ
)
10163 and then not Is_Generic_Actual_Type
(Typ
)
10167 -- This case occurs when the concurrent type is declared within
10168 -- a generic unit. As a result the corresponding record has been
10169 -- built and used as the type of the first formal, we just have
10170 -- to retrieve the corresponding concurrent type.
10172 elsif Is_Concurrent_Record_Type
(Typ
)
10173 and then not Is_Class_Wide_Type
(Typ
)
10174 and then Present
(Corresponding_Concurrent_Type
(Typ
))
10176 Typ
:= Corresponding_Concurrent_Type
(Typ
);
10184 -- There is no overriding to check if is an inherited operation in a
10185 -- type derivation on for a generic actual.
10187 Collect_Interfaces
(Typ
, Ifaces_List
);
10189 if Is_Empty_Elmt_List
(Ifaces_List
) then
10193 -- Determine whether entry or subprogram Def_Id overrides a primitive
10194 -- operation that belongs to one of the interfaces in Ifaces_List.
10197 Candidate
: Entity_Id
:= Empty
;
10198 Hom
: Entity_Id
:= Empty
;
10199 Iface_Typ
: Entity_Id
;
10200 Subp
: Entity_Id
:= Empty
;
10203 -- Traverse the homonym chain, looking for a potentially
10204 -- overridden subprogram that belongs to an implemented
10207 Hom
:= Current_Entity_In_Scope
(Def_Id
);
10208 while Present
(Hom
) loop
10212 or else not Is_Overloadable
(Subp
)
10213 or else not Is_Primitive
(Subp
)
10214 or else not Is_Dispatching_Operation
(Subp
)
10215 or else not Present
(Find_Dispatching_Type
(Subp
))
10216 or else not Is_Interface
(Find_Dispatching_Type
(Subp
))
10220 -- Entries and procedures can override abstract or null
10221 -- interface procedures.
10223 elsif (Ekind
(Def_Id
) = E_Procedure
10224 or else Ekind
(Def_Id
) = E_Entry
)
10225 and then Ekind
(Subp
) = E_Procedure
10226 and then Matches_Prefixed_View_Profile
10227 (Parameter_Specifications
(Parent
(Def_Id
)),
10228 Parameter_Specifications
(Parent
(Subp
)))
10232 -- For an overridden subprogram Subp, check whether the mode
10233 -- of its first parameter is correct depending on the kind
10234 -- of synchronized type.
10237 Formal
: constant Node_Id
:= First_Formal
(Candidate
);
10240 -- In order for an entry or a protected procedure to
10241 -- override, the first parameter of the overridden
10242 -- routine must be of mode "out", "in out" or
10243 -- access-to-variable.
10245 if Ekind_In
(Candidate
, E_Entry
, E_Procedure
)
10246 and then Is_Protected_Type
(Typ
)
10247 and then Ekind
(Formal
) /= E_In_Out_Parameter
10248 and then Ekind
(Formal
) /= E_Out_Parameter
10249 and then Nkind
(Parameter_Type
(Parent
(Formal
))) /=
10250 N_Access_Definition
10254 -- All other cases are OK since a task entry or routine
10255 -- does not have a restriction on the mode of the first
10256 -- parameter of the overridden interface routine.
10259 Overridden_Subp
:= Candidate
;
10264 -- Functions can override abstract interface functions
10266 elsif Ekind
(Def_Id
) = E_Function
10267 and then Ekind
(Subp
) = E_Function
10268 and then Matches_Prefixed_View_Profile
10269 (Parameter_Specifications
(Parent
(Def_Id
)),
10270 Parameter_Specifications
(Parent
(Subp
)))
10271 and then Etype
(Result_Definition
(Parent
(Def_Id
))) =
10272 Etype
(Result_Definition
(Parent
(Subp
)))
10274 Overridden_Subp
:= Subp
;
10278 Hom
:= Homonym
(Hom
);
10281 -- After examining all candidates for overriding, we are left with
10282 -- the best match which is a mode incompatible interface routine.
10283 -- Do not emit an error if the Expander is active since this error
10284 -- will be detected later on after all concurrent types are
10285 -- expanded and all wrappers are built. This check is meant for
10286 -- spec-only compilations.
10288 if Present
(Candidate
) and then not Expander_Active
then
10290 Find_Parameter_Type
(Parent
(First_Formal
(Candidate
)));
10292 -- Def_Id is primitive of a protected type, declared inside the
10293 -- type, and the candidate is primitive of a limited or
10294 -- synchronized interface.
10297 and then Is_Protected_Type
(Typ
)
10299 (Is_Limited_Interface
(Iface_Typ
)
10300 or else Is_Protected_Interface
(Iface_Typ
)
10301 or else Is_Synchronized_Interface
(Iface_Typ
)
10302 or else Is_Task_Interface
(Iface_Typ
))
10304 Error_Msg_PT
(Parent
(Typ
), Candidate
);
10308 Overridden_Subp
:= Candidate
;
10311 end Check_Synchronized_Overriding
;
10313 ----------------------------
10314 -- Is_Private_Declaration --
10315 ----------------------------
10317 function Is_Private_Declaration
(E
: Entity_Id
) return Boolean is
10318 Priv_Decls
: List_Id
;
10319 Decl
: constant Node_Id
:= Unit_Declaration_Node
(E
);
10322 if Is_Package_Or_Generic_Package
(Current_Scope
)
10323 and then In_Private_Part
(Current_Scope
)
10326 Private_Declarations
(Package_Specification
(Current_Scope
));
10328 return In_Package_Body
(Current_Scope
)
10330 (Is_List_Member
(Decl
)
10331 and then List_Containing
(Decl
) = Priv_Decls
)
10332 or else (Nkind
(Parent
(Decl
)) = N_Package_Specification
10334 Is_Compilation_Unit
10335 (Defining_Entity
(Parent
(Decl
)))
10336 and then List_Containing
(Parent
(Parent
(Decl
))) =
10341 end Is_Private_Declaration
;
10343 --------------------------
10344 -- Is_Overriding_Alias --
10345 --------------------------
10347 function Is_Overriding_Alias
10348 (Old_E
: Entity_Id
;
10349 New_E
: Entity_Id
) return Boolean
10351 AO
: constant Entity_Id
:= Alias
(Old_E
);
10352 AN
: constant Entity_Id
:= Alias
(New_E
);
10355 return Scope
(AO
) /= Scope
(AN
)
10356 or else No
(DTC_Entity
(AO
))
10357 or else No
(DTC_Entity
(AN
))
10358 or else DT_Position
(AO
) = DT_Position
(AN
);
10359 end Is_Overriding_Alias
;
10361 -- Start of processing for New_Overloaded_Entity
10364 -- We need to look for an entity that S may override. This must be a
10365 -- homonym in the current scope, so we look for the first homonym of
10366 -- S in the current scope as the starting point for the search.
10368 E
:= Current_Entity_In_Scope
(S
);
10370 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10371 -- They are directly added to the list of primitive operations of
10372 -- Derived_Type, unless this is a rederivation in the private part
10373 -- of an operation that was already derived in the visible part of
10374 -- the current package.
10376 if Ada_Version
>= Ada_2005
10377 and then Present
(Derived_Type
)
10378 and then Present
(Alias
(S
))
10379 and then Is_Dispatching_Operation
(Alias
(S
))
10380 and then Present
(Find_Dispatching_Type
(Alias
(S
)))
10381 and then Is_Interface
(Find_Dispatching_Type
(Alias
(S
)))
10383 -- For private types, when the full-view is processed we propagate to
10384 -- the full view the non-overridden entities whose attribute "alias"
10385 -- references an interface primitive. These entities were added by
10386 -- Derive_Subprograms to ensure that interface primitives are
10389 -- Inside_Freeze_Actions is non zero when S corresponds with an
10390 -- internal entity that links an interface primitive with its
10391 -- covering primitive through attribute Interface_Alias (see
10392 -- Add_Internal_Interface_Entities).
10394 if Inside_Freezing_Actions
= 0
10395 and then Is_Package_Or_Generic_Package
(Current_Scope
)
10396 and then In_Private_Part
(Current_Scope
)
10397 and then Nkind
(Parent
(E
)) = N_Private_Extension_Declaration
10398 and then Nkind
(Parent
(S
)) = N_Full_Type_Declaration
10399 and then Full_View
(Defining_Identifier
(Parent
(E
)))
10400 = Defining_Identifier
(Parent
(S
))
10401 and then Alias
(E
) = Alias
(S
)
10403 Check_Operation_From_Private_View
(S
, E
);
10404 Set_Is_Dispatching_Operation
(S
);
10409 Enter_Overloaded_Entity
(S
);
10410 Check_Dispatching_Operation
(S
, Empty
);
10411 Check_For_Primitive_Subprogram
(Is_Primitive_Subp
);
10417 -- If there is no homonym then this is definitely not overriding
10420 Enter_Overloaded_Entity
(S
);
10421 Check_Dispatching_Operation
(S
, Empty
);
10422 Check_For_Primitive_Subprogram
(Is_Primitive_Subp
);
10424 -- If subprogram has an explicit declaration, check whether it has an
10425 -- overriding indicator.
10427 if Comes_From_Source
(S
) then
10428 Check_Synchronized_Overriding
(S
, Overridden_Subp
);
10430 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10431 -- it may have overridden some hidden inherited primitive. Update
10432 -- Overridden_Subp to avoid spurious errors when checking the
10433 -- overriding indicator.
10435 if Ada_Version
>= Ada_2012
10436 and then No
(Overridden_Subp
)
10437 and then Is_Dispatching_Operation
(S
)
10438 and then Present
(Overridden_Operation
(S
))
10440 Overridden_Subp
:= Overridden_Operation
(S
);
10443 Check_Overriding_Indicator
10444 (S
, Overridden_Subp
, Is_Primitive
=> Is_Primitive_Subp
);
10447 -- If there is a homonym that is not overloadable, then we have an
10448 -- error, except for the special cases checked explicitly below.
10450 elsif not Is_Overloadable
(E
) then
10452 -- Check for spurious conflict produced by a subprogram that has the
10453 -- same name as that of the enclosing generic package. The conflict
10454 -- occurs within an instance, between the subprogram and the renaming
10455 -- declaration for the package. After the subprogram, the package
10456 -- renaming declaration becomes hidden.
10458 if Ekind
(E
) = E_Package
10459 and then Present
(Renamed_Object
(E
))
10460 and then Renamed_Object
(E
) = Current_Scope
10461 and then Nkind
(Parent
(Renamed_Object
(E
))) =
10462 N_Package_Specification
10463 and then Present
(Generic_Parent
(Parent
(Renamed_Object
(E
))))
10466 Set_Is_Immediately_Visible
(E
, False);
10467 Enter_Overloaded_Entity
(S
);
10468 Set_Homonym
(S
, Homonym
(E
));
10469 Check_Dispatching_Operation
(S
, Empty
);
10470 Check_Overriding_Indicator
(S
, Empty
, Is_Primitive
=> False);
10472 -- If the subprogram is implicit it is hidden by the previous
10473 -- declaration. However if it is dispatching, it must appear in the
10474 -- dispatch table anyway, because it can be dispatched to even if it
10475 -- cannot be called directly.
10477 elsif Present
(Alias
(S
)) and then not Comes_From_Source
(S
) then
10478 Set_Scope
(S
, Current_Scope
);
10480 if Is_Dispatching_Operation
(Alias
(S
)) then
10481 Check_Dispatching_Operation
(S
, Empty
);
10487 Error_Msg_Sloc
:= Sloc
(E
);
10489 -- Generate message, with useful additional warning if in generic
10491 if Is_Generic_Unit
(E
) then
10492 Error_Msg_N
("previous generic unit cannot be overloaded", S
);
10493 Error_Msg_N
("\& conflicts with declaration#", S
);
10495 Error_Msg_N
("& conflicts with declaration#", S
);
10501 -- E exists and is overloadable
10504 Check_Synchronized_Overriding
(S
, Overridden_Subp
);
10506 -- Loop through E and its homonyms to determine if any of them is
10507 -- the candidate for overriding by S.
10509 while Present
(E
) loop
10511 -- Definitely not interesting if not in the current scope
10513 if Scope
(E
) /= Current_Scope
then
10516 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10517 -- procedures locate the internally generated spec. We enforce
10518 -- mode conformance since a tagged type may inherit from
10519 -- interfaces several null primitives which differ only in
10520 -- the mode of the formals.
10522 elsif not Comes_From_Source
(S
)
10523 and then Is_Null_Procedure
(S
)
10524 and then not Mode_Conformant
(E
, S
)
10528 -- Check if we have type conformance
10530 elsif Type_Conformant
(E
, S
) then
10532 -- If the old and new entities have the same profile and one
10533 -- is not the body of the other, then this is an error, unless
10534 -- one of them is implicitly declared.
10536 -- There are some cases when both can be implicit, for example
10537 -- when both a literal and a function that overrides it are
10538 -- inherited in a derivation, or when an inherited operation
10539 -- of a tagged full type overrides the inherited operation of
10540 -- a private extension. Ada 83 had a special rule for the
10541 -- literal case. In Ada 95, the later implicit operation hides
10542 -- the former, and the literal is always the former. In the
10543 -- odd case where both are derived operations declared at the
10544 -- same point, both operations should be declared, and in that
10545 -- case we bypass the following test and proceed to the next
10546 -- part. This can only occur for certain obscure cases in
10547 -- instances, when an operation on a type derived from a formal
10548 -- private type does not override a homograph inherited from
10549 -- the actual. In subsequent derivations of such a type, the
10550 -- DT positions of these operations remain distinct, if they
10553 if Present
(Alias
(S
))
10554 and then (No
(Alias
(E
))
10555 or else Comes_From_Source
(E
)
10556 or else Is_Abstract_Subprogram
(S
)
10558 (Is_Dispatching_Operation
(E
)
10559 and then Is_Overriding_Alias
(E
, S
)))
10560 and then Ekind
(E
) /= E_Enumeration_Literal
10562 -- When an derived operation is overloaded it may be due to
10563 -- the fact that the full view of a private extension
10564 -- re-inherits. It has to be dealt with.
10566 if Is_Package_Or_Generic_Package
(Current_Scope
)
10567 and then In_Private_Part
(Current_Scope
)
10569 Check_Operation_From_Private_View
(S
, E
);
10572 -- In any case the implicit operation remains hidden by the
10573 -- existing declaration, which is overriding. Indicate that
10574 -- E overrides the operation from which S is inherited.
10576 if Present
(Alias
(S
)) then
10577 Set_Overridden_Operation
(E
, Alias
(S
));
10579 Set_Overridden_Operation
(E
, S
);
10582 if Comes_From_Source
(E
) then
10583 Check_Overriding_Indicator
(E
, S
, Is_Primitive
=> False);
10588 -- Within an instance, the renaming declarations for actual
10589 -- subprograms may become ambiguous, but they do not hide each
10592 elsif Ekind
(E
) /= E_Entry
10593 and then not Comes_From_Source
(E
)
10594 and then not Is_Generic_Instance
(E
)
10595 and then (Present
(Alias
(E
))
10596 or else Is_Intrinsic_Subprogram
(E
))
10597 and then (not In_Instance
10598 or else No
(Parent
(E
))
10599 or else Nkind
(Unit_Declaration_Node
(E
)) /=
10600 N_Subprogram_Renaming_Declaration
)
10602 -- A subprogram child unit is not allowed to override an
10603 -- inherited subprogram (10.1.1(20)).
10605 if Is_Child_Unit
(S
) then
10607 ("child unit overrides inherited subprogram in parent",
10612 if Is_Non_Overriding_Operation
(E
, S
) then
10613 Enter_Overloaded_Entity
(S
);
10615 if No
(Derived_Type
)
10616 or else Is_Tagged_Type
(Derived_Type
)
10618 Check_Dispatching_Operation
(S
, Empty
);
10624 -- E is a derived operation or an internal operator which
10625 -- is being overridden. Remove E from further visibility.
10626 -- Furthermore, if E is a dispatching operation, it must be
10627 -- replaced in the list of primitive operations of its type
10628 -- (see Override_Dispatching_Operation).
10630 Overridden_Subp
:= E
;
10636 Prev
:= First_Entity
(Current_Scope
);
10637 while Present
(Prev
) and then Next_Entity
(Prev
) /= E
loop
10638 Next_Entity
(Prev
);
10641 -- It is possible for E to be in the current scope and
10642 -- yet not in the entity chain. This can only occur in a
10643 -- generic context where E is an implicit concatenation
10644 -- in the formal part, because in a generic body the
10645 -- entity chain starts with the formals.
10648 (Present
(Prev
) or else Chars
(E
) = Name_Op_Concat
);
10650 -- E must be removed both from the entity_list of the
10651 -- current scope, and from the visibility chain
10653 if Debug_Flag_E
then
10654 Write_Str
("Override implicit operation ");
10655 Write_Int
(Int
(E
));
10659 -- If E is a predefined concatenation, it stands for four
10660 -- different operations. As a result, a single explicit
10661 -- declaration does not hide it. In a possible ambiguous
10662 -- situation, Disambiguate chooses the user-defined op,
10663 -- so it is correct to retain the previous internal one.
10665 if Chars
(E
) /= Name_Op_Concat
10666 or else Ekind
(E
) /= E_Operator
10668 -- For nondispatching derived operations that are
10669 -- overridden by a subprogram declared in the private
10670 -- part of a package, we retain the derived subprogram
10671 -- but mark it as not immediately visible. If the
10672 -- derived operation was declared in the visible part
10673 -- then this ensures that it will still be visible
10674 -- outside the package with the proper signature
10675 -- (calls from outside must also be directed to this
10676 -- version rather than the overriding one, unlike the
10677 -- dispatching case). Calls from inside the package
10678 -- will still resolve to the overriding subprogram
10679 -- since the derived one is marked as not visible
10680 -- within the package.
10682 -- If the private operation is dispatching, we achieve
10683 -- the overriding by keeping the implicit operation
10684 -- but setting its alias to be the overriding one. In
10685 -- this fashion the proper body is executed in all
10686 -- cases, but the original signature is used outside
10689 -- If the overriding is not in the private part, we
10690 -- remove the implicit operation altogether.
10692 if Is_Private_Declaration
(S
) then
10693 if not Is_Dispatching_Operation
(E
) then
10694 Set_Is_Immediately_Visible
(E
, False);
10696 -- Work done in Override_Dispatching_Operation,
10697 -- so nothing else needs to be done here.
10703 -- Find predecessor of E in Homonym chain
10705 if E
= Current_Entity
(E
) then
10708 Prev_Vis
:= Current_Entity
(E
);
10709 while Homonym
(Prev_Vis
) /= E
loop
10710 Prev_Vis
:= Homonym
(Prev_Vis
);
10714 if Prev_Vis
/= Empty
then
10716 -- Skip E in the visibility chain
10718 Set_Homonym
(Prev_Vis
, Homonym
(E
));
10721 Set_Name_Entity_Id
(Chars
(E
), Homonym
(E
));
10724 Set_Next_Entity
(Prev
, Next_Entity
(E
));
10726 if No
(Next_Entity
(Prev
)) then
10727 Set_Last_Entity
(Current_Scope
, Prev
);
10732 Enter_Overloaded_Entity
(S
);
10734 -- For entities generated by Derive_Subprograms the
10735 -- overridden operation is the inherited primitive
10736 -- (which is available through the attribute alias).
10738 if not (Comes_From_Source
(E
))
10739 and then Is_Dispatching_Operation
(E
)
10740 and then Find_Dispatching_Type
(E
) =
10741 Find_Dispatching_Type
(S
)
10742 and then Present
(Alias
(E
))
10743 and then Comes_From_Source
(Alias
(E
))
10745 Set_Overridden_Operation
(S
, Alias
(E
));
10747 -- Normal case of setting entity as overridden
10749 -- Note: Static_Initialization and Overridden_Operation
10750 -- attributes use the same field in subprogram entities.
10751 -- Static_Initialization is only defined for internal
10752 -- initialization procedures, where Overridden_Operation
10753 -- is irrelevant. Therefore the setting of this attribute
10754 -- must check whether the target is an init_proc.
10756 elsif not Is_Init_Proc
(S
) then
10757 Set_Overridden_Operation
(S
, E
);
10760 Check_Overriding_Indicator
(S
, E
, Is_Primitive
=> True);
10762 -- If S is a user-defined subprogram or a null procedure
10763 -- expanded to override an inherited null procedure, or a
10764 -- predefined dispatching primitive then indicate that E
10765 -- overrides the operation from which S is inherited.
10767 if Comes_From_Source
(S
)
10769 (Present
(Parent
(S
))
10771 Nkind
(Parent
(S
)) = N_Procedure_Specification
10773 Null_Present
(Parent
(S
)))
10775 (Present
(Alias
(E
))
10777 Is_Predefined_Dispatching_Operation
(Alias
(E
)))
10779 if Present
(Alias
(E
)) then
10780 Set_Overridden_Operation
(S
, Alias
(E
));
10784 if Is_Dispatching_Operation
(E
) then
10786 -- An overriding dispatching subprogram inherits the
10787 -- convention of the overridden subprogram (AI-117).
10789 Set_Convention
(S
, Convention
(E
));
10790 Check_Dispatching_Operation
(S
, E
);
10793 Check_Dispatching_Operation
(S
, Empty
);
10796 Check_For_Primitive_Subprogram
10797 (Is_Primitive_Subp
, Is_Overriding
=> True);
10798 goto Check_Inequality
;
10801 -- Apparent redeclarations in instances can occur when two
10802 -- formal types get the same actual type. The subprograms in
10803 -- in the instance are legal, even if not callable from the
10804 -- outside. Calls from within are disambiguated elsewhere.
10805 -- For dispatching operations in the visible part, the usual
10806 -- rules apply, and operations with the same profile are not
10807 -- legal (B830001).
10809 elsif (In_Instance_Visible_Part
10810 and then not Is_Dispatching_Operation
(E
))
10811 or else In_Instance_Not_Visible
10815 -- Here we have a real error (identical profile)
10818 Error_Msg_Sloc
:= Sloc
(E
);
10820 -- Avoid cascaded errors if the entity appears in
10821 -- subsequent calls.
10823 Set_Scope
(S
, Current_Scope
);
10825 -- Generate error, with extra useful warning for the case
10826 -- of a generic instance with no completion.
10828 if Is_Generic_Instance
(S
)
10829 and then not Has_Completion
(E
)
10832 ("instantiation cannot provide body for&", S
);
10833 Error_Msg_N
("\& conflicts with declaration#", S
);
10835 Error_Msg_N
("& conflicts with declaration#", S
);
10842 -- If one subprogram has an access parameter and the other
10843 -- a parameter of an access type, calls to either might be
10844 -- ambiguous. Verify that parameters match except for the
10845 -- access parameter.
10847 if May_Hide_Profile
then
10853 F1
:= First_Formal
(S
);
10854 F2
:= First_Formal
(E
);
10855 while Present
(F1
) and then Present
(F2
) loop
10856 if Is_Access_Type
(Etype
(F1
)) then
10857 if not Is_Access_Type
(Etype
(F2
))
10858 or else not Conforming_Types
10859 (Designated_Type
(Etype
(F1
)),
10860 Designated_Type
(Etype
(F2
)),
10863 May_Hide_Profile
:= False;
10867 not Conforming_Types
10868 (Etype
(F1
), Etype
(F2
), Type_Conformant
)
10870 May_Hide_Profile
:= False;
10877 if May_Hide_Profile
10881 Error_Msg_NE
("calls to& may be ambiguous??", S
, S
);
10890 -- On exit, we know that S is a new entity
10892 Enter_Overloaded_Entity
(S
);
10893 Check_For_Primitive_Subprogram
(Is_Primitive_Subp
);
10894 Check_Overriding_Indicator
10895 (S
, Overridden_Subp
, Is_Primitive
=> Is_Primitive_Subp
);
10897 -- Overloading is not allowed in SPARK, except for operators
10899 if Nkind
(S
) /= N_Defining_Operator_Symbol
then
10900 Error_Msg_Sloc
:= Sloc
(Homonym
(S
));
10901 Check_SPARK_Restriction
10902 ("overloading not allowed with entity#", S
);
10905 -- If S is a derived operation for an untagged type then by
10906 -- definition it's not a dispatching operation (even if the parent
10907 -- operation was dispatching), so Check_Dispatching_Operation is not
10908 -- called in that case.
10910 if No
(Derived_Type
)
10911 or else Is_Tagged_Type
(Derived_Type
)
10913 Check_Dispatching_Operation
(S
, Empty
);
10917 -- If this is a user-defined equality operator that is not a derived
10918 -- subprogram, create the corresponding inequality. If the operation is
10919 -- dispatching, the expansion is done elsewhere, and we do not create
10920 -- an explicit inequality operation.
10922 <<Check_Inequality
>>
10923 if Chars
(S
) = Name_Op_Eq
10924 and then Etype
(S
) = Standard_Boolean
10925 and then Present
(Parent
(S
))
10926 and then not Is_Dispatching_Operation
(S
)
10928 Make_Inequality_Operator
(S
);
10930 if Ada_Version
>= Ada_2012
then
10931 Check_Untagged_Equality
(S
);
10934 end New_Overloaded_Entity
;
10936 ---------------------
10937 -- Process_Formals --
10938 ---------------------
10940 procedure Process_Formals
10942 Related_Nod
: Node_Id
)
10944 Param_Spec
: Node_Id
;
10945 Formal
: Entity_Id
;
10946 Formal_Type
: Entity_Id
;
10950 Num_Out_Params
: Nat
:= 0;
10951 First_Out_Param
: Entity_Id
:= Empty
;
10952 -- Used for setting Is_Only_Out_Parameter
10954 function Designates_From_Limited_With
(Typ
: Entity_Id
) return Boolean;
10955 -- Determine whether an access type designates a type coming from a
10958 function Is_Class_Wide_Default
(D
: Node_Id
) return Boolean;
10959 -- Check whether the default has a class-wide type. After analysis the
10960 -- default has the type of the formal, so we must also check explicitly
10961 -- for an access attribute.
10963 ----------------------------------
10964 -- Designates_From_Limited_With --
10965 ----------------------------------
10967 function Designates_From_Limited_With
(Typ
: Entity_Id
) return Boolean is
10968 Desig
: Entity_Id
:= Typ
;
10971 if Is_Access_Type
(Desig
) then
10972 Desig
:= Directly_Designated_Type
(Desig
);
10975 if Is_Class_Wide_Type
(Desig
) then
10976 Desig
:= Root_Type
(Desig
);
10980 Ekind
(Desig
) = E_Incomplete_Type
10981 and then From_Limited_With
(Desig
);
10982 end Designates_From_Limited_With
;
10984 ---------------------------
10985 -- Is_Class_Wide_Default --
10986 ---------------------------
10988 function Is_Class_Wide_Default
(D
: Node_Id
) return Boolean is
10990 return Is_Class_Wide_Type
(Designated_Type
(Etype
(D
)))
10991 or else (Nkind
(D
) = N_Attribute_Reference
10992 and then Attribute_Name
(D
) = Name_Access
10993 and then Is_Class_Wide_Type
(Etype
(Prefix
(D
))));
10994 end Is_Class_Wide_Default
;
10996 -- Start of processing for Process_Formals
10999 -- In order to prevent premature use of the formals in the same formal
11000 -- part, the Ekind is left undefined until all default expressions are
11001 -- analyzed. The Ekind is established in a separate loop at the end.
11003 Param_Spec
:= First
(T
);
11004 while Present
(Param_Spec
) loop
11005 Formal
:= Defining_Identifier
(Param_Spec
);
11006 Set_Never_Set_In_Source
(Formal
, True);
11007 Enter_Name
(Formal
);
11009 -- Case of ordinary parameters
11011 if Nkind
(Parameter_Type
(Param_Spec
)) /= N_Access_Definition
then
11012 Find_Type
(Parameter_Type
(Param_Spec
));
11013 Ptype
:= Parameter_Type
(Param_Spec
);
11015 if Ptype
= Error
then
11019 Formal_Type
:= Entity
(Ptype
);
11021 if Is_Incomplete_Type
(Formal_Type
)
11023 (Is_Class_Wide_Type
(Formal_Type
)
11024 and then Is_Incomplete_Type
(Root_Type
(Formal_Type
)))
11026 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
11027 -- primitive operations, as long as their completion is
11028 -- in the same declarative part. If in the private part
11029 -- this means that the type cannot be a Taft-amendment type.
11030 -- Check is done on package exit. For access to subprograms,
11031 -- the use is legal for Taft-amendment types.
11033 -- Ada 2012: tagged incomplete types are allowed as generic
11034 -- formal types. They do not introduce dependencies and the
11035 -- corresponding generic subprogram does not have a delayed
11036 -- freeze, because it does not need a freeze node.
11038 if Is_Tagged_Type
(Formal_Type
) then
11039 if Ekind
(Scope
(Current_Scope
)) = E_Package
11040 and then not From_Limited_With
(Formal_Type
)
11041 and then not Is_Generic_Type
(Formal_Type
)
11042 and then not Is_Class_Wide_Type
(Formal_Type
)
11045 (Parent
(T
), N_Access_Function_Definition
,
11046 N_Access_Procedure_Definition
)
11050 Private_Dependents
(Base_Type
(Formal_Type
)));
11052 -- Freezing is delayed to ensure that Register_Prim
11053 -- will get called for this operation, which is needed
11054 -- in cases where static dispatch tables aren't built.
11055 -- (Note that the same is done for controlling access
11056 -- parameter cases in function Access_Definition.)
11058 Set_Has_Delayed_Freeze
(Current_Scope
);
11062 -- Special handling of Value_Type for CIL case
11064 elsif Is_Value_Type
(Formal_Type
) then
11067 elsif not Nkind_In
(Parent
(T
), N_Access_Function_Definition
,
11068 N_Access_Procedure_Definition
)
11070 -- AI05-0151: Tagged incomplete types are allowed in all
11071 -- formal parts. Untagged incomplete types are not allowed
11074 if Ada_Version
>= Ada_2012
then
11075 if Is_Tagged_Type
(Formal_Type
) then
11078 elsif Nkind_In
(Parent
(Parent
(T
)), N_Accept_Statement
,
11083 ("invalid use of untagged incomplete type&",
11084 Ptype
, Formal_Type
);
11089 ("invalid use of incomplete type&",
11090 Param_Spec
, Formal_Type
);
11092 -- Further checks on the legality of incomplete types
11093 -- in formal parts are delayed until the freeze point
11094 -- of the enclosing subprogram or access to subprogram.
11098 elsif Ekind
(Formal_Type
) = E_Void
then
11100 ("premature use of&",
11101 Parameter_Type
(Param_Spec
), Formal_Type
);
11104 -- Ada 2012 (AI-142): Handle aliased parameters
11106 if Ada_Version
>= Ada_2012
11107 and then Aliased_Present
(Param_Spec
)
11109 Set_Is_Aliased
(Formal
);
11112 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11113 -- declaration corresponding to the null-excluding type of the
11114 -- formal in the enclosing scope. Finally, replace the parameter
11115 -- type of the formal with the internal subtype.
11117 if Ada_Version
>= Ada_2005
11118 and then Null_Exclusion_Present
(Param_Spec
)
11120 if not Is_Access_Type
(Formal_Type
) then
11122 ("`NOT NULL` allowed only for an access type", Param_Spec
);
11125 if Can_Never_Be_Null
(Formal_Type
)
11126 and then Comes_From_Source
(Related_Nod
)
11129 ("`NOT NULL` not allowed (& already excludes null)",
11130 Param_Spec
, Formal_Type
);
11134 Create_Null_Excluding_Itype
11136 Related_Nod
=> Related_Nod
,
11137 Scope_Id
=> Scope
(Current_Scope
));
11139 -- If the designated type of the itype is an itype that is
11140 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11141 -- on the access subtype, to prevent order-of-elaboration
11142 -- issues in the backend.
11145 -- type T is access procedure;
11146 -- procedure Op (O : not null T);
11148 if Is_Itype
(Directly_Designated_Type
(Formal_Type
))
11150 not Is_Frozen
(Directly_Designated_Type
(Formal_Type
))
11152 Set_Has_Delayed_Freeze
(Formal_Type
);
11157 -- An access formal type
11161 Access_Definition
(Related_Nod
, Parameter_Type
(Param_Spec
));
11163 -- No need to continue if we already notified errors
11165 if not Present
(Formal_Type
) then
11169 -- Ada 2005 (AI-254)
11172 AD
: constant Node_Id
:=
11173 Access_To_Subprogram_Definition
11174 (Parameter_Type
(Param_Spec
));
11176 if Present
(AD
) and then Protected_Present
(AD
) then
11178 Replace_Anonymous_Access_To_Protected_Subprogram
11184 Set_Etype
(Formal
, Formal_Type
);
11186 -- Deal with default expression if present
11188 Default
:= Expression
(Param_Spec
);
11190 if Present
(Default
) then
11191 Check_SPARK_Restriction
11192 ("default expression is not allowed", Default
);
11194 if Out_Present
(Param_Spec
) then
11196 ("default initialization only allowed for IN parameters",
11200 -- Do the special preanalysis of the expression (see section on
11201 -- "Handling of Default Expressions" in the spec of package Sem).
11203 Preanalyze_Spec_Expression
(Default
, Formal_Type
);
11205 -- An access to constant cannot be the default for
11206 -- an access parameter that is an access to variable.
11208 if Ekind
(Formal_Type
) = E_Anonymous_Access_Type
11209 and then not Is_Access_Constant
(Formal_Type
)
11210 and then Is_Access_Type
(Etype
(Default
))
11211 and then Is_Access_Constant
(Etype
(Default
))
11214 ("formal that is access to variable cannot be initialized " &
11215 "with an access-to-constant expression", Default
);
11218 -- Check that the designated type of an access parameter's default
11219 -- is not a class-wide type unless the parameter's designated type
11220 -- is also class-wide.
11222 if Ekind
(Formal_Type
) = E_Anonymous_Access_Type
11223 and then not Designates_From_Limited_With
(Formal_Type
)
11224 and then Is_Class_Wide_Default
(Default
)
11225 and then not Is_Class_Wide_Type
(Designated_Type
(Formal_Type
))
11228 ("access to class-wide expression not allowed here", Default
);
11231 -- Check incorrect use of dynamically tagged expressions
11233 if Is_Tagged_Type
(Formal_Type
) then
11234 Check_Dynamically_Tagged_Expression
11236 Typ
=> Formal_Type
,
11237 Related_Nod
=> Default
);
11241 -- Ada 2005 (AI-231): Static checks
11243 if Ada_Version
>= Ada_2005
11244 and then Is_Access_Type
(Etype
(Formal
))
11245 and then Can_Never_Be_Null
(Etype
(Formal
))
11247 Null_Exclusion_Static_Checks
(Param_Spec
);
11254 -- If this is the formal part of a function specification, analyze the
11255 -- subtype mark in the context where the formals are visible but not
11256 -- yet usable, and may hide outer homographs.
11258 if Nkind
(Related_Nod
) = N_Function_Specification
then
11259 Analyze_Return_Type
(Related_Nod
);
11262 -- Now set the kind (mode) of each formal
11264 Param_Spec
:= First
(T
);
11265 while Present
(Param_Spec
) loop
11266 Formal
:= Defining_Identifier
(Param_Spec
);
11267 Set_Formal_Mode
(Formal
);
11269 if Ekind
(Formal
) = E_In_Parameter
then
11270 Set_Default_Value
(Formal
, Expression
(Param_Spec
));
11272 if Present
(Expression
(Param_Spec
)) then
11273 Default
:= Expression
(Param_Spec
);
11275 if Is_Scalar_Type
(Etype
(Default
)) then
11276 if Nkind
(Parameter_Type
(Param_Spec
)) /=
11277 N_Access_Definition
11279 Formal_Type
:= Entity
(Parameter_Type
(Param_Spec
));
11283 (Related_Nod
, Parameter_Type
(Param_Spec
));
11286 Apply_Scalar_Range_Check
(Default
, Formal_Type
);
11290 elsif Ekind
(Formal
) = E_Out_Parameter
then
11291 Num_Out_Params
:= Num_Out_Params
+ 1;
11293 if Num_Out_Params
= 1 then
11294 First_Out_Param
:= Formal
;
11297 elsif Ekind
(Formal
) = E_In_Out_Parameter
then
11298 Num_Out_Params
:= Num_Out_Params
+ 1;
11301 -- Skip remaining processing if formal type was in error
11303 if Etype
(Formal
) = Any_Type
or else Error_Posted
(Formal
) then
11304 goto Next_Parameter
;
11307 -- Force call by reference if aliased
11309 if Is_Aliased
(Formal
) then
11310 Set_Mechanism
(Formal
, By_Reference
);
11312 -- Warn if user asked this to be passed by copy
11314 if Convention
(Formal_Type
) = Convention_Ada_Pass_By_Copy
then
11316 ("cannot pass aliased parameter & by copy?", Formal
);
11319 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11321 elsif Convention
(Formal_Type
) = Convention_Ada_Pass_By_Copy
then
11322 Set_Mechanism
(Formal
, By_Copy
);
11324 elsif Convention
(Formal_Type
) = Convention_Ada_Pass_By_Reference
then
11325 Set_Mechanism
(Formal
, By_Reference
);
11332 if Present
(First_Out_Param
) and then Num_Out_Params
= 1 then
11333 Set_Is_Only_Out_Parameter
(First_Out_Param
);
11335 end Process_Formals
;
11337 ----------------------------
11338 -- Reference_Body_Formals --
11339 ----------------------------
11341 procedure Reference_Body_Formals
(Spec
: Entity_Id
; Bod
: Entity_Id
) is
11346 if Error_Posted
(Spec
) then
11350 -- Iterate over both lists. They may be of different lengths if the two
11351 -- specs are not conformant.
11353 Fs
:= First_Formal
(Spec
);
11354 Fb
:= First_Formal
(Bod
);
11355 while Present
(Fs
) and then Present
(Fb
) loop
11356 Generate_Reference
(Fs
, Fb
, 'b');
11358 if Style_Check
then
11359 Style
.Check_Identifier
(Fb
, Fs
);
11362 Set_Spec_Entity
(Fb
, Fs
);
11363 Set_Referenced
(Fs
, False);
11367 end Reference_Body_Formals
;
11369 -------------------------
11370 -- Set_Actual_Subtypes --
11371 -------------------------
11373 procedure Set_Actual_Subtypes
(N
: Node_Id
; Subp
: Entity_Id
) is
11375 Formal
: Entity_Id
;
11377 First_Stmt
: Node_Id
:= Empty
;
11378 AS_Needed
: Boolean;
11381 -- If this is an empty initialization procedure, no need to create
11382 -- actual subtypes (small optimization).
11384 if Ekind
(Subp
) = E_Procedure
and then Is_Null_Init_Proc
(Subp
) then
11388 Formal
:= First_Formal
(Subp
);
11389 while Present
(Formal
) loop
11390 T
:= Etype
(Formal
);
11392 -- We never need an actual subtype for a constrained formal
11394 if Is_Constrained
(T
) then
11395 AS_Needed
:= False;
11397 -- If we have unknown discriminants, then we do not need an actual
11398 -- subtype, or more accurately we cannot figure it out! Note that
11399 -- all class-wide types have unknown discriminants.
11401 elsif Has_Unknown_Discriminants
(T
) then
11402 AS_Needed
:= False;
11404 -- At this stage we have an unconstrained type that may need an
11405 -- actual subtype. For sure the actual subtype is needed if we have
11406 -- an unconstrained array type.
11408 elsif Is_Array_Type
(T
) then
11411 -- The only other case needing an actual subtype is an unconstrained
11412 -- record type which is an IN parameter (we cannot generate actual
11413 -- subtypes for the OUT or IN OUT case, since an assignment can
11414 -- change the discriminant values. However we exclude the case of
11415 -- initialization procedures, since discriminants are handled very
11416 -- specially in this context, see the section entitled "Handling of
11417 -- Discriminants" in Einfo.
11419 -- We also exclude the case of Discrim_SO_Functions (functions used
11420 -- in front end layout mode for size/offset values), since in such
11421 -- functions only discriminants are referenced, and not only are such
11422 -- subtypes not needed, but they cannot always be generated, because
11423 -- of order of elaboration issues.
11425 elsif Is_Record_Type
(T
)
11426 and then Ekind
(Formal
) = E_In_Parameter
11427 and then Chars
(Formal
) /= Name_uInit
11428 and then not Is_Unchecked_Union
(T
)
11429 and then not Is_Discrim_SO_Function
(Subp
)
11433 -- All other cases do not need an actual subtype
11436 AS_Needed
:= False;
11439 -- Generate actual subtypes for unconstrained arrays and
11440 -- unconstrained discriminated records.
11443 if Nkind
(N
) = N_Accept_Statement
then
11445 -- If expansion is active, the formal is replaced by a local
11446 -- variable that renames the corresponding entry of the
11447 -- parameter block, and it is this local variable that may
11448 -- require an actual subtype.
11450 if Full_Expander_Active
then
11451 Decl
:= Build_Actual_Subtype
(T
, Renamed_Object
(Formal
));
11453 Decl
:= Build_Actual_Subtype
(T
, Formal
);
11456 if Present
(Handled_Statement_Sequence
(N
)) then
11458 First
(Statements
(Handled_Statement_Sequence
(N
)));
11459 Prepend
(Decl
, Statements
(Handled_Statement_Sequence
(N
)));
11460 Mark_Rewrite_Insertion
(Decl
);
11462 -- If the accept statement has no body, there will be no
11463 -- reference to the actuals, so no need to compute actual
11470 Decl
:= Build_Actual_Subtype
(T
, Formal
);
11471 Prepend
(Decl
, Declarations
(N
));
11472 Mark_Rewrite_Insertion
(Decl
);
11475 -- The declaration uses the bounds of an existing object, and
11476 -- therefore needs no constraint checks.
11478 Analyze
(Decl
, Suppress
=> All_Checks
);
11480 -- We need to freeze manually the generated type when it is
11481 -- inserted anywhere else than in a declarative part.
11483 if Present
(First_Stmt
) then
11484 Insert_List_Before_And_Analyze
(First_Stmt
,
11485 Freeze_Entity
(Defining_Identifier
(Decl
), N
));
11488 if Nkind
(N
) = N_Accept_Statement
11489 and then Full_Expander_Active
11491 Set_Actual_Subtype
(Renamed_Object
(Formal
),
11492 Defining_Identifier
(Decl
));
11494 Set_Actual_Subtype
(Formal
, Defining_Identifier
(Decl
));
11498 Next_Formal
(Formal
);
11500 end Set_Actual_Subtypes
;
11502 ---------------------
11503 -- Set_Formal_Mode --
11504 ---------------------
11506 procedure Set_Formal_Mode
(Formal_Id
: Entity_Id
) is
11507 Spec
: constant Node_Id
:= Parent
(Formal_Id
);
11510 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11511 -- since we ensure that corresponding actuals are always valid at the
11512 -- point of the call.
11514 if Out_Present
(Spec
) then
11515 if Ekind
(Scope
(Formal_Id
)) = E_Function
11516 or else Ekind
(Scope
(Formal_Id
)) = E_Generic_Function
11518 -- [IN] OUT parameters allowed for functions in Ada 2012
11520 if Ada_Version
>= Ada_2012
then
11522 -- Even in Ada 2012 operators can only have IN parameters
11524 if Is_Operator_Symbol_Name
(Chars
(Scope
(Formal_Id
))) then
11525 Error_Msg_N
("operators can only have IN parameters", Spec
);
11528 if In_Present
(Spec
) then
11529 Set_Ekind
(Formal_Id
, E_In_Out_Parameter
);
11531 Set_Ekind
(Formal_Id
, E_Out_Parameter
);
11534 -- But not in earlier versions of Ada
11537 Error_Msg_N
("functions can only have IN parameters", Spec
);
11538 Set_Ekind
(Formal_Id
, E_In_Parameter
);
11541 elsif In_Present
(Spec
) then
11542 Set_Ekind
(Formal_Id
, E_In_Out_Parameter
);
11545 Set_Ekind
(Formal_Id
, E_Out_Parameter
);
11546 Set_Never_Set_In_Source
(Formal_Id
, True);
11547 Set_Is_True_Constant
(Formal_Id
, False);
11548 Set_Current_Value
(Formal_Id
, Empty
);
11552 Set_Ekind
(Formal_Id
, E_In_Parameter
);
11555 -- Set Is_Known_Non_Null for access parameters since the language
11556 -- guarantees that access parameters are always non-null. We also set
11557 -- Can_Never_Be_Null, since there is no way to change the value.
11559 if Nkind
(Parameter_Type
(Spec
)) = N_Access_Definition
then
11561 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
11562 -- null; In Ada 2005, only if then null_exclusion is explicit.
11564 if Ada_Version
< Ada_2005
11565 or else Can_Never_Be_Null
(Etype
(Formal_Id
))
11567 Set_Is_Known_Non_Null
(Formal_Id
);
11568 Set_Can_Never_Be_Null
(Formal_Id
);
11571 -- Ada 2005 (AI-231): Null-exclusion access subtype
11573 elsif Is_Access_Type
(Etype
(Formal_Id
))
11574 and then Can_Never_Be_Null
(Etype
(Formal_Id
))
11576 Set_Is_Known_Non_Null
(Formal_Id
);
11578 -- We can also set Can_Never_Be_Null (thus preventing some junk
11579 -- access checks) for the case of an IN parameter, which cannot
11580 -- be changed, or for an IN OUT parameter, which can be changed but
11581 -- not to a null value. But for an OUT parameter, the initial value
11582 -- passed in can be null, so we can't set this flag in that case.
11584 if Ekind
(Formal_Id
) /= E_Out_Parameter
then
11585 Set_Can_Never_Be_Null
(Formal_Id
);
11589 Set_Mechanism
(Formal_Id
, Default_Mechanism
);
11590 Set_Formal_Validity
(Formal_Id
);
11591 end Set_Formal_Mode
;
11593 -------------------------
11594 -- Set_Formal_Validity --
11595 -------------------------
11597 procedure Set_Formal_Validity
(Formal_Id
: Entity_Id
) is
11599 -- If no validity checking, then we cannot assume anything about the
11600 -- validity of parameters, since we do not know there is any checking
11601 -- of the validity on the call side.
11603 if not Validity_Checks_On
then
11606 -- If validity checking for parameters is enabled, this means we are
11607 -- not supposed to make any assumptions about argument values.
11609 elsif Validity_Check_Parameters
then
11612 -- If we are checking in parameters, we will assume that the caller is
11613 -- also checking parameters, so we can assume the parameter is valid.
11615 elsif Ekind
(Formal_Id
) = E_In_Parameter
11616 and then Validity_Check_In_Params
11618 Set_Is_Known_Valid
(Formal_Id
, True);
11620 -- Similar treatment for IN OUT parameters
11622 elsif Ekind
(Formal_Id
) = E_In_Out_Parameter
11623 and then Validity_Check_In_Out_Params
11625 Set_Is_Known_Valid
(Formal_Id
, True);
11627 end Set_Formal_Validity
;
11629 ------------------------
11630 -- Subtype_Conformant --
11631 ------------------------
11633 function Subtype_Conformant
11634 (New_Id
: Entity_Id
;
11635 Old_Id
: Entity_Id
;
11636 Skip_Controlling_Formals
: Boolean := False) return Boolean
11640 Check_Conformance
(New_Id
, Old_Id
, Subtype_Conformant
, False, Result
,
11641 Skip_Controlling_Formals
=> Skip_Controlling_Formals
);
11643 end Subtype_Conformant
;
11645 ---------------------
11646 -- Type_Conformant --
11647 ---------------------
11649 function Type_Conformant
11650 (New_Id
: Entity_Id
;
11651 Old_Id
: Entity_Id
;
11652 Skip_Controlling_Formals
: Boolean := False) return Boolean
11656 May_Hide_Profile
:= False;
11659 (New_Id
, Old_Id
, Type_Conformant
, False, Result
,
11660 Skip_Controlling_Formals
=> Skip_Controlling_Formals
);
11662 end Type_Conformant
;
11664 -------------------------------
11665 -- Valid_Operator_Definition --
11666 -------------------------------
11668 procedure Valid_Operator_Definition
(Designator
: Entity_Id
) is
11671 Id
: constant Name_Id
:= Chars
(Designator
);
11675 F
:= First_Formal
(Designator
);
11676 while Present
(F
) loop
11679 if Present
(Default_Value
(F
)) then
11681 ("default values not allowed for operator parameters",
11688 -- Verify that user-defined operators have proper number of arguments
11689 -- First case of operators which can only be unary
11691 if Nam_In
(Id
, Name_Op_Not
, Name_Op_Abs
) then
11694 -- Case of operators which can be unary or binary
11696 elsif Nam_In
(Id
, Name_Op_Add
, Name_Op_Subtract
) then
11697 N_OK
:= (N
in 1 .. 2);
11699 -- All other operators can only be binary
11707 ("incorrect number of arguments for operator", Designator
);
11711 and then Base_Type
(Etype
(Designator
)) = Standard_Boolean
11712 and then not Is_Intrinsic_Subprogram
(Designator
)
11715 ("explicit definition of inequality not allowed", Designator
);
11717 end Valid_Operator_Definition
;