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 Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Debug
; use Debug
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
31 with Errout
; use Errout
;
32 with Expander
; use Expander
;
33 with Exp_Ch6
; use Exp_Ch6
;
34 with Exp_Ch7
; use Exp_Ch7
;
35 with Exp_Ch9
; use Exp_Ch9
;
36 with Exp_Dbug
; use Exp_Dbug
;
37 with Exp_Disp
; use Exp_Disp
;
38 with Exp_Tss
; use Exp_Tss
;
39 with Exp_Util
; use Exp_Util
;
40 with Fname
; use Fname
;
41 with Freeze
; use Freeze
;
42 with Itypes
; use Itypes
;
43 with Lib
.Xref
; use Lib
.Xref
;
44 with Layout
; use Layout
;
45 with Namet
; use Namet
;
47 with Nlists
; use Nlists
;
48 with Nmake
; use Nmake
;
50 with Output
; use Output
;
51 with Restrict
; use Restrict
;
52 with Rident
; use Rident
;
53 with Rtsfind
; use Rtsfind
;
55 with Sem_Aux
; use Sem_Aux
;
56 with Sem_Cat
; use Sem_Cat
;
57 with Sem_Ch3
; use Sem_Ch3
;
58 with Sem_Ch4
; use Sem_Ch4
;
59 with Sem_Ch5
; use Sem_Ch5
;
60 with Sem_Ch8
; use Sem_Ch8
;
61 with Sem_Ch10
; use Sem_Ch10
;
62 with Sem_Ch12
; use Sem_Ch12
;
63 with Sem_Ch13
; use Sem_Ch13
;
64 with Sem_Dim
; use Sem_Dim
;
65 with Sem_Disp
; use Sem_Disp
;
66 with Sem_Dist
; use Sem_Dist
;
67 with Sem_Elim
; use Sem_Elim
;
68 with Sem_Eval
; use Sem_Eval
;
69 with Sem_Mech
; use Sem_Mech
;
70 with Sem_Prag
; use Sem_Prag
;
71 with Sem_Res
; use Sem_Res
;
72 with Sem_Util
; use Sem_Util
;
73 with Sem_Type
; use Sem_Type
;
74 with Sem_Warn
; use Sem_Warn
;
75 with Sinput
; use Sinput
;
76 with Stand
; use Stand
;
77 with Sinfo
; use Sinfo
;
78 with Sinfo
.CN
; use Sinfo
.CN
;
79 with Snames
; use Snames
;
80 with Stringt
; use Stringt
;
82 with Stylesw
; use Stylesw
;
83 with Targparm
; use Targparm
;
84 with Tbuild
; use Tbuild
;
85 with Uintp
; use Uintp
;
86 with Urealp
; use Urealp
;
87 with Validsw
; use Validsw
;
89 package body Sem_Ch6
is
91 May_Hide_Profile
: Boolean := False;
92 -- This flag is used to indicate that two formals in two subprograms being
93 -- checked for conformance differ only in that one is an access parameter
94 -- while the other is of a general access type with the same designated
95 -- type. In this case, if the rest of the signatures match, a call to
96 -- either subprogram may be ambiguous, which is worth a warning. The flag
97 -- is set in Compatible_Types, and the warning emitted in
98 -- New_Overloaded_Entity.
100 -----------------------
101 -- Local Subprograms --
102 -----------------------
104 procedure Analyze_Null_Procedure
106 Is_Completion
: out Boolean);
107 -- A null procedure can be a declaration or (Ada 2012) a completion.
109 procedure Analyze_Return_Statement
(N
: Node_Id
);
110 -- Common processing for simple and extended return statements
112 procedure Analyze_Function_Return
(N
: Node_Id
);
113 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
114 -- applies to a [generic] function.
116 procedure Analyze_Return_Type
(N
: Node_Id
);
117 -- Subsidiary to Process_Formals: analyze subtype mark in function
118 -- specification in a context where the formals are visible and hide
121 procedure Analyze_Subprogram_Body_Helper
(N
: Node_Id
);
122 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
123 -- that we can use RETURN but not skip the debug output at the end.
125 procedure Analyze_Generic_Subprogram_Body
(N
: Node_Id
; Gen_Id
: Entity_Id
);
126 -- Analyze a generic subprogram body. N is the body to be analyzed, and
127 -- Gen_Id is the defining entity Id for the corresponding spec.
129 procedure Build_Body_To_Inline
(N
: Node_Id
; Subp
: Entity_Id
);
130 -- If a subprogram has pragma Inline and inlining is active, use generic
131 -- machinery to build an unexpanded body for the subprogram. This body is
132 -- subsequently used for inline expansions at call sites. If subprogram can
133 -- be inlined (depending on size and nature of local declarations) this
134 -- function returns true. Otherwise subprogram body is treated normally.
135 -- If proper warnings are enabled and the subprogram contains a construct
136 -- that cannot be inlined, the offending construct is flagged accordingly.
138 function Can_Override_Operator
(Subp
: Entity_Id
) return Boolean;
139 -- Returns true if Subp can override a predefined operator.
141 procedure Check_And_Build_Body_To_Inline
144 Body_Id
: Entity_Id
);
145 -- Spec_Id and Body_Id are the entities of the specification and body of
146 -- the subprogram body N. If N can be inlined by the frontend (supported
147 -- cases documented in Check_Body_To_Inline) then build the body-to-inline
148 -- associated with N and attach it to the declaration node of Spec_Id.
150 procedure Check_Conformance
153 Ctype
: Conformance_Type
;
155 Conforms
: out Boolean;
156 Err_Loc
: Node_Id
:= Empty
;
157 Get_Inst
: Boolean := False;
158 Skip_Controlling_Formals
: Boolean := False);
159 -- Given two entities, this procedure checks that the profiles associated
160 -- with these entities meet the conformance criterion given by the third
161 -- parameter. If they conform, Conforms is set True and control returns
162 -- to the caller. If they do not conform, Conforms is set to False, and
163 -- in addition, if Errmsg is True on the call, proper messages are output
164 -- to complain about the conformance failure. If Err_Loc is non_Empty
165 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
166 -- error messages are placed on the appropriate part of the construct
167 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
168 -- against a formal access-to-subprogram type so Get_Instance_Of must
171 procedure Check_Subprogram_Order
(N
: Node_Id
);
172 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
173 -- the alpha ordering rule for N if this ordering requirement applicable.
175 procedure Check_Returns
179 Proc
: Entity_Id
:= Empty
);
180 -- Called to check for missing return statements in a function body, or for
181 -- returns present in a procedure body which has No_Return set. HSS is the
182 -- handled statement sequence for the subprogram body. This procedure
183 -- checks all flow paths to make sure they either have return (Mode = 'F',
184 -- used for functions) or do not have a return (Mode = 'P', used for
185 -- No_Return procedures). The flag Err is set if there are any control
186 -- paths not explicitly terminated by a return in the function case, and is
187 -- True otherwise. Proc is the entity for the procedure case and is used
188 -- in posting the warning message.
190 procedure Check_Untagged_Equality
(Eq_Op
: Entity_Id
);
191 -- In Ada 2012, a primitive equality operator on an untagged record type
192 -- must appear before the type is frozen, and have the same visibility as
193 -- that of the type. This procedure checks that this rule is met, and
194 -- otherwise emits an error on the subprogram declaration and a warning
195 -- on the earlier freeze point if it is easy to locate.
197 procedure Enter_Overloaded_Entity
(S
: Entity_Id
);
198 -- This procedure makes S, a new overloaded entity, into the first visible
199 -- entity with that name.
201 function Is_Non_Overriding_Operation
203 New_E
: Entity_Id
) return Boolean;
204 -- Enforce the rule given in 12.3(18): a private operation in an instance
205 -- overrides an inherited operation only if the corresponding operation
206 -- was overriding in the generic. This needs to be checked for primitive
207 -- operations of types derived (in the generic unit) from formal private
208 -- or formal derived types.
210 procedure Make_Inequality_Operator
(S
: Entity_Id
);
211 -- Create the declaration for an inequality operator that is implicitly
212 -- created by a user-defined equality operator that yields a boolean.
214 procedure May_Need_Actuals
(Fun
: Entity_Id
);
215 -- Flag functions that can be called without parameters, i.e. those that
216 -- have no parameters, or those for which defaults exist for all parameters
218 procedure Process_PPCs
221 Body_Id
: Entity_Id
);
222 -- Called from Analyze[_Generic]_Subprogram_Body to deal with scanning post
223 -- conditions for the body and assembling and inserting the _postconditions
224 -- procedure. N is the node for the subprogram body and Body_Id/Spec_Id are
225 -- the entities for the body and separate spec (if there is no separate
226 -- spec, Spec_Id is Empty). Note that invariants and predicates may also
227 -- provide postconditions, and are also handled in this procedure.
229 procedure Set_Formal_Validity
(Formal_Id
: Entity_Id
);
230 -- Formal_Id is an formal parameter entity. This procedure deals with
231 -- setting the proper validity status for this entity, which depends on
232 -- the kind of parameter and the validity checking mode.
234 ---------------------------------------------
235 -- Analyze_Abstract_Subprogram_Declaration --
236 ---------------------------------------------
238 procedure Analyze_Abstract_Subprogram_Declaration
(N
: Node_Id
) is
239 Designator
: constant Entity_Id
:=
240 Analyze_Subprogram_Specification
(Specification
(N
));
241 Scop
: constant Entity_Id
:= Current_Scope
;
244 Check_SPARK_Restriction
("abstract subprogram is not allowed", N
);
246 Generate_Definition
(Designator
);
247 Set_Contract
(Designator
, Make_Contract
(Sloc
(Designator
)));
248 Set_Is_Abstract_Subprogram
(Designator
);
249 New_Overloaded_Entity
(Designator
);
250 Check_Delayed_Subprogram
(Designator
);
252 Set_Categorization_From_Scope
(Designator
, Scop
);
254 if Ekind
(Scope
(Designator
)) = E_Protected_Type
then
256 ("abstract subprogram not allowed in protected type", N
);
258 -- Issue a warning if the abstract subprogram is neither a dispatching
259 -- operation nor an operation that overrides an inherited subprogram or
260 -- predefined operator, since this most likely indicates a mistake.
262 elsif Warn_On_Redundant_Constructs
263 and then not Is_Dispatching_Operation
(Designator
)
264 and then not Present
(Overridden_Operation
(Designator
))
265 and then (not Is_Operator_Symbol_Name
(Chars
(Designator
))
266 or else Scop
/= Scope
(Etype
(First_Formal
(Designator
))))
269 ("abstract subprogram is not dispatching or overriding?r?", N
);
272 Generate_Reference_To_Formals
(Designator
);
273 Check_Eliminated
(Designator
);
275 if Has_Aspects
(N
) then
276 Analyze_Aspect_Specifications
(N
, Designator
);
278 end Analyze_Abstract_Subprogram_Declaration
;
280 ---------------------------------
281 -- Analyze_Expression_Function --
282 ---------------------------------
284 procedure Analyze_Expression_Function
(N
: Node_Id
) is
285 Loc
: constant Source_Ptr
:= Sloc
(N
);
286 LocX
: constant Source_Ptr
:= Sloc
(Expression
(N
));
287 Expr
: constant Node_Id
:= Expression
(N
);
288 Spec
: constant Node_Id
:= Specification
(N
);
293 -- If the expression is a completion, Prev is the entity whose
294 -- declaration is completed. Def_Id is needed to analyze the spec.
302 -- This is one of the occasions on which we transform the tree during
303 -- semantic analysis. If this is a completion, transform the expression
304 -- function into an equivalent subprogram body, and analyze it.
306 -- Expression functions are inlined unconditionally. The back-end will
307 -- determine whether this is possible.
309 Inline_Processing_Required
:= True;
311 -- Create a specification for the generated body. Types and defauts in
312 -- the profile are copies of the spec, but new entities must be created
313 -- for the unit name and the formals.
315 New_Spec
:= New_Copy_Tree
(Spec
);
316 Set_Defining_Unit_Name
(New_Spec
,
317 Make_Defining_Identifier
(Sloc
(Defining_Unit_Name
(Spec
)),
318 Chars
(Defining_Unit_Name
(Spec
))));
320 if Present
(Parameter_Specifications
(New_Spec
)) then
322 Formal_Spec
: Node_Id
;
324 Formal_Spec
:= First
(Parameter_Specifications
(New_Spec
));
325 while Present
(Formal_Spec
) loop
326 Set_Defining_Identifier
328 Make_Defining_Identifier
(Sloc
(Formal_Spec
),
329 Chars
=> Chars
(Defining_Identifier
(Formal_Spec
))));
335 Prev
:= Current_Entity_In_Scope
(Defining_Entity
(Spec
));
337 -- If there are previous overloadable entities with the same name,
338 -- check whether any of them is completed by the expression function.
340 if Present
(Prev
) and then Is_Overloadable
(Prev
) then
341 Def_Id
:= Analyze_Subprogram_Specification
(Spec
);
342 Prev
:= Find_Corresponding_Spec
(N
);
345 Ret
:= Make_Simple_Return_Statement
(LocX
, Expression
(N
));
348 Make_Subprogram_Body
(Loc
,
349 Specification
=> New_Spec
,
350 Declarations
=> Empty_List
,
351 Handled_Statement_Sequence
=>
352 Make_Handled_Sequence_Of_Statements
(LocX
,
353 Statements
=> New_List
(Ret
)));
355 if Present
(Prev
) and then Ekind
(Prev
) = E_Generic_Function
then
357 -- If the expression completes a generic subprogram, we must create a
358 -- separate node for the body, because at instantiation the original
359 -- node of the generic copy must be a generic subprogram body, and
360 -- cannot be a expression function. Otherwise we just rewrite the
361 -- expression with the non-generic body.
363 Insert_After
(N
, New_Body
);
364 Rewrite
(N
, Make_Null_Statement
(Loc
));
365 Set_Has_Completion
(Prev
, False);
368 Set_Is_Inlined
(Prev
);
370 elsif Present
(Prev
) and then Comes_From_Source
(Prev
) then
371 Set_Has_Completion
(Prev
, False);
373 -- For navigation purposes, indicate that the function is a body
375 Generate_Reference
(Prev
, Defining_Entity
(N
), 'b', Force
=> True);
376 Rewrite
(N
, New_Body
);
379 -- Prev is the previous entity with the same name, but it is can
380 -- be an unrelated spec that is not completed by the expression
381 -- function. In that case the relevant entity is the one in the body.
382 -- Not clear that the backend can inline it in this case ???
384 if Has_Completion
(Prev
) then
385 Set_Is_Inlined
(Prev
);
387 -- The formals of the expression function are body formals,
388 -- and do not appear in the ali file, which will only contain
389 -- references to the formals of the original subprogram spec.
396 F1
:= First_Formal
(Def_Id
);
397 F2
:= First_Formal
(Prev
);
399 while Present
(F1
) loop
400 Set_Spec_Entity
(F1
, F2
);
407 Set_Is_Inlined
(Defining_Entity
(New_Body
));
410 -- If this is not a completion, create both a declaration and a body, so
411 -- that the expression can be inlined whenever possible.
414 -- An expression function that is not a completion is not a
415 -- subprogram declaration, and thus cannot appear in a protected
418 if Nkind
(Parent
(N
)) = N_Protected_Definition
then
420 ("an expression function is not a legal protected operation", N
);
424 Make_Subprogram_Declaration
(Loc
, Specification
=> Spec
);
426 Rewrite
(N
, New_Decl
);
428 Set_Is_Inlined
(Defining_Entity
(New_Decl
));
430 -- To prevent premature freeze action, insert the new body at the end
431 -- of the current declarations, or at the end of the package spec.
432 -- However, resolve usage names now, to prevent spurious visibility
433 -- on later entities.
436 Decls
: List_Id
:= List_Containing
(N
);
437 Par
: constant Node_Id
:= Parent
(Decls
);
438 Id
: constant Entity_Id
:= Defining_Entity
(New_Decl
);
441 if Nkind
(Par
) = N_Package_Specification
442 and then Decls
= Visible_Declarations
(Par
)
443 and then Present
(Private_Declarations
(Par
))
444 and then not Is_Empty_List
(Private_Declarations
(Par
))
446 Decls
:= Private_Declarations
(Par
);
449 Insert_After
(Last
(Decls
), New_Body
);
451 Install_Formals
(Id
);
453 -- Do a preanalysis of the expression on a separate copy, to
454 -- prevent visibility issues later with operators in instances.
455 -- Attach copy to tree so that parent links are available.
458 Expr
: constant Node_Id
:= New_Copy_Tree
(Expression
(Ret
));
460 Set_Parent
(Expr
, Ret
);
461 Preanalyze_Spec_Expression
(Expr
, Etype
(Id
));
468 -- If the return expression is a static constant, we suppress warning
469 -- messages on unused formals, which in most cases will be noise.
471 Set_Is_Trivial_Subprogram
(Defining_Entity
(New_Body
),
472 Is_OK_Static_Expression
(Expr
));
473 end Analyze_Expression_Function
;
475 ----------------------------------------
476 -- Analyze_Extended_Return_Statement --
477 ----------------------------------------
479 procedure Analyze_Extended_Return_Statement
(N
: Node_Id
) is
481 Analyze_Return_Statement
(N
);
482 end Analyze_Extended_Return_Statement
;
484 ----------------------------
485 -- Analyze_Function_Call --
486 ----------------------------
488 procedure Analyze_Function_Call
(N
: Node_Id
) is
489 Actuals
: constant List_Id
:= Parameter_Associations
(N
);
490 Func_Nam
: constant Node_Id
:= Name
(N
);
496 -- A call of the form A.B (X) may be an Ada 2005 call, which is
497 -- rewritten as B (A, X). If the rewriting is successful, the call
498 -- has been analyzed and we just return.
500 if Nkind
(Func_Nam
) = N_Selected_Component
501 and then Name
(N
) /= Func_Nam
502 and then Is_Rewrite_Substitution
(N
)
503 and then Present
(Etype
(N
))
508 -- If error analyzing name, then set Any_Type as result type and return
510 if Etype
(Func_Nam
) = Any_Type
then
511 Set_Etype
(N
, Any_Type
);
515 -- Otherwise analyze the parameters
517 if Present
(Actuals
) then
518 Actual
:= First
(Actuals
);
519 while Present
(Actual
) loop
521 Check_Parameterless_Call
(Actual
);
527 end Analyze_Function_Call
;
529 -----------------------------
530 -- Analyze_Function_Return --
531 -----------------------------
533 procedure Analyze_Function_Return
(N
: Node_Id
) is
534 Loc
: constant Source_Ptr
:= Sloc
(N
);
535 Stm_Entity
: constant Entity_Id
:= Return_Statement_Entity
(N
);
536 Scope_Id
: constant Entity_Id
:= Return_Applies_To
(Stm_Entity
);
538 R_Type
: constant Entity_Id
:= Etype
(Scope_Id
);
539 -- Function result subtype
541 procedure Check_Limited_Return
(Expr
: Node_Id
);
542 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
543 -- limited types. Used only for simple return statements.
544 -- Expr is the expression returned.
546 procedure Check_Return_Subtype_Indication
(Obj_Decl
: Node_Id
);
547 -- Check that the return_subtype_indication properly matches the result
548 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
550 --------------------------
551 -- Check_Limited_Return --
552 --------------------------
554 procedure Check_Limited_Return
(Expr
: Node_Id
) is
556 -- Ada 2005 (AI-318-02): Return-by-reference types have been
557 -- removed and replaced by anonymous access results. This is an
558 -- incompatibility with Ada 95. Not clear whether this should be
559 -- enforced yet or perhaps controllable with special switch. ???
561 -- A limited interface that is not immutably limited is OK.
563 if Is_Limited_Interface
(R_Type
)
565 not (Is_Task_Interface
(R_Type
)
566 or else Is_Protected_Interface
(R_Type
)
567 or else Is_Synchronized_Interface
(R_Type
))
571 elsif Is_Limited_Type
(R_Type
)
572 and then not Is_Interface
(R_Type
)
573 and then Comes_From_Source
(N
)
574 and then not In_Instance_Body
575 and then not OK_For_Limited_Init_In_05
(R_Type
, Expr
)
579 if Ada_Version
>= Ada_2005
580 and then not Debug_Flag_Dot_L
581 and then not GNAT_Mode
584 ("(Ada 2005) cannot copy object of a limited type " &
585 "(RM-2005 6.5(5.5/2))", Expr
);
587 if Is_Immutably_Limited_Type
(R_Type
) then
589 ("\return by reference not permitted in Ada 2005", Expr
);
592 -- Warn in Ada 95 mode, to give folks a heads up about this
595 -- In GNAT mode, this is just a warning, to allow it to be
596 -- evilly turned off. Otherwise it is a real error.
598 -- In a generic context, simplify the warning because it makes
599 -- no sense to discuss pass-by-reference or copy.
601 elsif Warn_On_Ada_2005_Compatibility
or GNAT_Mode
then
602 if Inside_A_Generic
then
604 ("return of limited object not permitted in Ada 2005 "
605 & "(RM-2005 6.5(5.5/2))?y?", Expr
);
607 elsif Is_Immutably_Limited_Type
(R_Type
) then
609 ("return by reference not permitted in Ada 2005 "
610 & "(RM-2005 6.5(5.5/2))?y?", Expr
);
613 ("cannot copy object of a limited type in Ada 2005 "
614 & "(RM-2005 6.5(5.5/2))?y?", Expr
);
617 -- Ada 95 mode, compatibility warnings disabled
620 return; -- skip continuation messages below
623 if not Inside_A_Generic
then
625 ("\consider switching to return of access type", Expr
);
626 Explain_Limited_Type
(R_Type
, Expr
);
629 end Check_Limited_Return
;
631 -------------------------------------
632 -- Check_Return_Subtype_Indication --
633 -------------------------------------
635 procedure Check_Return_Subtype_Indication
(Obj_Decl
: Node_Id
) is
636 Return_Obj
: constant Node_Id
:= Defining_Identifier
(Obj_Decl
);
638 R_Stm_Type
: constant Entity_Id
:= Etype
(Return_Obj
);
639 -- Subtype given in the extended return statement (must match R_Type)
641 Subtype_Ind
: constant Node_Id
:=
642 Object_Definition
(Original_Node
(Obj_Decl
));
644 R_Type_Is_Anon_Access
:
646 Ekind
(R_Type
) = E_Anonymous_Access_Subprogram_Type
648 Ekind
(R_Type
) = E_Anonymous_Access_Protected_Subprogram_Type
650 Ekind
(R_Type
) = E_Anonymous_Access_Type
;
651 -- True if return type of the function is an anonymous access type
652 -- Can't we make Is_Anonymous_Access_Type in einfo ???
654 R_Stm_Type_Is_Anon_Access
:
656 Ekind
(R_Stm_Type
) = E_Anonymous_Access_Subprogram_Type
658 Ekind
(R_Stm_Type
) = E_Anonymous_Access_Protected_Subprogram_Type
660 Ekind
(R_Stm_Type
) = E_Anonymous_Access_Type
;
661 -- True if type of the return object is an anonymous access type
664 -- First, avoid cascaded errors
666 if Error_Posted
(Obj_Decl
) or else Error_Posted
(Subtype_Ind
) then
670 -- "return access T" case; check that the return statement also has
671 -- "access T", and that the subtypes statically match:
672 -- if this is an access to subprogram the signatures must match.
674 if R_Type_Is_Anon_Access
then
675 if R_Stm_Type_Is_Anon_Access
then
677 Ekind
(Designated_Type
(R_Stm_Type
)) /= E_Subprogram_Type
679 if Base_Type
(Designated_Type
(R_Stm_Type
)) /=
680 Base_Type
(Designated_Type
(R_Type
))
681 or else not Subtypes_Statically_Match
(R_Stm_Type
, R_Type
)
684 ("subtype must statically match function result subtype",
685 Subtype_Mark
(Subtype_Ind
));
689 -- For two anonymous access to subprogram types, the
690 -- types themselves must be type conformant.
692 if not Conforming_Types
693 (R_Stm_Type
, R_Type
, Fully_Conformant
)
696 ("subtype must statically match function result subtype",
702 Error_Msg_N
("must use anonymous access type", Subtype_Ind
);
705 -- If the return object is of an anonymous access type, then report
706 -- an error if the function's result type is not also anonymous.
708 elsif R_Stm_Type_Is_Anon_Access
709 and then not R_Type_Is_Anon_Access
711 Error_Msg_N
("anonymous access not allowed for function with " &
712 "named access result", Subtype_Ind
);
714 -- Subtype indication case: check that the return object's type is
715 -- covered by the result type, and that the subtypes statically match
716 -- when the result subtype is constrained. Also handle record types
717 -- with unknown discriminants for which we have built the underlying
718 -- record view. Coverage is needed to allow specific-type return
719 -- objects when the result type is class-wide (see AI05-32).
721 elsif Covers
(Base_Type
(R_Type
), Base_Type
(R_Stm_Type
))
722 or else (Is_Underlying_Record_View
(Base_Type
(R_Stm_Type
))
726 Underlying_Record_View
(Base_Type
(R_Stm_Type
))))
728 -- A null exclusion may be present on the return type, on the
729 -- function specification, on the object declaration or on the
732 if Is_Access_Type
(R_Type
)
734 (Can_Never_Be_Null
(R_Type
)
735 or else Null_Exclusion_Present
(Parent
(Scope_Id
))) /=
736 Can_Never_Be_Null
(R_Stm_Type
)
739 ("subtype must statically match function result subtype",
743 -- AI05-103: for elementary types, subtypes must statically match
745 if Is_Constrained
(R_Type
)
746 or else Is_Access_Type
(R_Type
)
748 if not Subtypes_Statically_Match
(R_Stm_Type
, R_Type
) then
750 ("subtype must statically match function result subtype",
755 elsif Etype
(Base_Type
(R_Type
)) = R_Stm_Type
756 and then Is_Null_Extension
(Base_Type
(R_Type
))
762 ("wrong type for return_subtype_indication", Subtype_Ind
);
764 end Check_Return_Subtype_Indication
;
766 ---------------------
767 -- Local Variables --
768 ---------------------
772 -- Start of processing for Analyze_Function_Return
775 Set_Return_Present
(Scope_Id
);
777 if Nkind
(N
) = N_Simple_Return_Statement
then
778 Expr
:= Expression
(N
);
780 -- Guard against a malformed expression. The parser may have tried to
781 -- recover but the node is not analyzable.
783 if Nkind
(Expr
) = N_Error
then
784 Set_Etype
(Expr
, Any_Type
);
785 Expander_Mode_Save_And_Set
(False);
789 -- The resolution of a controlled [extension] aggregate associated
790 -- with a return statement creates a temporary which needs to be
791 -- finalized on function exit. Wrap the return statement inside a
792 -- block so that the finalization machinery can detect this case.
793 -- This early expansion is done only when the return statement is
794 -- not part of a handled sequence of statements.
796 if Nkind_In
(Expr
, N_Aggregate
,
797 N_Extension_Aggregate
)
798 and then Needs_Finalization
(R_Type
)
799 and then Nkind
(Parent
(N
)) /= N_Handled_Sequence_Of_Statements
802 Make_Block_Statement
(Loc
,
803 Handled_Statement_Sequence
=>
804 Make_Handled_Sequence_Of_Statements
(Loc
,
805 Statements
=> New_List
(Relocate_Node
(N
)))));
811 Analyze_And_Resolve
(Expr
, R_Type
);
812 Check_Limited_Return
(Expr
);
815 -- RETURN only allowed in SPARK as the last statement in function
817 if Nkind
(Parent
(N
)) /= N_Handled_Sequence_Of_Statements
819 (Nkind
(Parent
(Parent
(N
))) /= N_Subprogram_Body
820 or else Present
(Next
(N
)))
822 Check_SPARK_Restriction
823 ("RETURN should be the last statement in function", N
);
827 Check_SPARK_Restriction
("extended RETURN is not allowed", N
);
829 -- Analyze parts specific to extended_return_statement:
832 Obj_Decl
: constant Node_Id
:=
833 Last
(Return_Object_Declarations
(N
));
834 Has_Aliased
: constant Boolean := Aliased_Present
(Obj_Decl
);
835 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
838 Expr
:= Expression
(Obj_Decl
);
840 -- Note: The check for OK_For_Limited_Init will happen in
841 -- Analyze_Object_Declaration; we treat it as a normal
842 -- object declaration.
844 Set_Is_Return_Object
(Defining_Identifier
(Obj_Decl
));
847 Check_Return_Subtype_Indication
(Obj_Decl
);
849 if Present
(HSS
) then
852 if Present
(Exception_Handlers
(HSS
)) then
854 -- ???Has_Nested_Block_With_Handler needs to be set.
855 -- Probably by creating an actual N_Block_Statement.
856 -- Probably in Expand.
862 -- Mark the return object as referenced, since the return is an
863 -- implicit reference of the object.
865 Set_Referenced
(Defining_Identifier
(Obj_Decl
));
867 Check_References
(Stm_Entity
);
869 -- Check RM 6.5 (5.9/3)
872 if Ada_Version
< Ada_2012
then
874 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
875 -- Can it really happen (extended return???)
878 ("aliased only allowed for limited"
879 & " return objects in Ada 2012?", N
);
881 elsif not Is_Immutably_Limited_Type
(R_Type
) then
882 Error_Msg_N
("aliased only allowed for limited"
883 & " return objects", N
);
889 -- Case of Expr present
893 -- Defend against previous errors
895 and then Nkind
(Expr
) /= N_Empty
896 and then Present
(Etype
(Expr
))
898 -- Apply constraint check. Note that this is done before the implicit
899 -- conversion of the expression done for anonymous access types to
900 -- ensure correct generation of the null-excluding check associated
901 -- with null-excluding expressions found in return statements.
903 Apply_Constraint_Check
(Expr
, R_Type
);
905 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
906 -- type, apply an implicit conversion of the expression to that type
907 -- to force appropriate static and run-time accessibility checks.
909 if Ada_Version
>= Ada_2005
910 and then Ekind
(R_Type
) = E_Anonymous_Access_Type
912 Rewrite
(Expr
, Convert_To
(R_Type
, Relocate_Node
(Expr
)));
913 Analyze_And_Resolve
(Expr
, R_Type
);
915 -- If this is a local anonymous access to subprogram, the
916 -- accessibility check can be applied statically. The return is
917 -- illegal if the access type of the return expression is declared
918 -- inside of the subprogram (except if it is the subtype indication
919 -- of an extended return statement).
921 elsif Ekind
(R_Type
) = E_Anonymous_Access_Subprogram_Type
then
922 if not Comes_From_Source
(Current_Scope
)
923 or else Ekind
(Current_Scope
) = E_Return_Statement
928 Scope_Depth
(Scope
(Etype
(Expr
))) >= Scope_Depth
(Scope_Id
)
930 Error_Msg_N
("cannot return local access to subprogram", N
);
934 -- If the result type is class-wide, then check that the return
935 -- expression's type is not declared at a deeper level than the
936 -- function (RM05-6.5(5.6/2)).
938 if Ada_Version
>= Ada_2005
939 and then Is_Class_Wide_Type
(R_Type
)
941 if Type_Access_Level
(Etype
(Expr
)) >
942 Subprogram_Access_Level
(Scope_Id
)
945 ("level of return expression type is deeper than " &
946 "class-wide function!", Expr
);
950 -- Check incorrect use of dynamically tagged expression
952 if Is_Tagged_Type
(R_Type
) then
953 Check_Dynamically_Tagged_Expression
959 -- ??? A real run-time accessibility check is needed in cases
960 -- involving dereferences of access parameters. For now we just
961 -- check the static cases.
963 if (Ada_Version
< Ada_2005
or else Debug_Flag_Dot_L
)
964 and then Is_Immutably_Limited_Type
(Etype
(Scope_Id
))
965 and then Object_Access_Level
(Expr
) >
966 Subprogram_Access_Level
(Scope_Id
)
968 -- Suppress the message in a generic, where the rewriting
971 if Inside_A_Generic
then
976 Make_Raise_Program_Error
(Loc
,
977 Reason
=> PE_Accessibility_Check_Failed
));
981 ("cannot return a local value by reference??", N
);
983 ("\& will be raised at run time??",
984 N
, Standard_Program_Error
);
989 and then Nkind
(Parent
(Scope_Id
)) = N_Function_Specification
990 and then Null_Exclusion_Present
(Parent
(Scope_Id
))
992 Apply_Compile_Time_Constraint_Error
994 Msg
=> "(Ada 2005) null not allowed for "
995 & "null-excluding return??",
996 Reason
=> CE_Null_Not_Allowed
);
999 end Analyze_Function_Return
;
1001 -------------------------------------
1002 -- Analyze_Generic_Subprogram_Body --
1003 -------------------------------------
1005 procedure Analyze_Generic_Subprogram_Body
1009 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Id
);
1010 Kind
: constant Entity_Kind
:= Ekind
(Gen_Id
);
1011 Body_Id
: Entity_Id
;
1016 -- Copy body and disable expansion while analyzing the generic For a
1017 -- stub, do not copy the stub (which would load the proper body), this
1018 -- will be done when the proper body is analyzed.
1020 if Nkind
(N
) /= N_Subprogram_Body_Stub
then
1021 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
1026 Spec
:= Specification
(N
);
1028 -- Within the body of the generic, the subprogram is callable, and
1029 -- behaves like the corresponding non-generic unit.
1031 Body_Id
:= Defining_Entity
(Spec
);
1033 if Kind
= E_Generic_Procedure
1034 and then Nkind
(Spec
) /= N_Procedure_Specification
1036 Error_Msg_N
("invalid body for generic procedure ", Body_Id
);
1039 elsif Kind
= E_Generic_Function
1040 and then Nkind
(Spec
) /= N_Function_Specification
1042 Error_Msg_N
("invalid body for generic function ", Body_Id
);
1046 Set_Corresponding_Body
(Gen_Decl
, Body_Id
);
1048 if Has_Completion
(Gen_Id
)
1049 and then Nkind
(Parent
(N
)) /= N_Subunit
1051 Error_Msg_N
("duplicate generic body", N
);
1054 Set_Has_Completion
(Gen_Id
);
1057 if Nkind
(N
) = N_Subprogram_Body_Stub
then
1058 Set_Ekind
(Defining_Entity
(Specification
(N
)), Kind
);
1060 Set_Corresponding_Spec
(N
, Gen_Id
);
1063 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
1064 Set_Cunit_Entity
(Current_Sem_Unit
, Defining_Entity
(N
));
1067 -- Make generic parameters immediately visible in the body. They are
1068 -- needed to process the formals declarations. Then make the formals
1069 -- visible in a separate step.
1071 Push_Scope
(Gen_Id
);
1075 First_Ent
: Entity_Id
;
1078 First_Ent
:= First_Entity
(Gen_Id
);
1081 while Present
(E
) and then not Is_Formal
(E
) loop
1086 Set_Use
(Generic_Formal_Declarations
(Gen_Decl
));
1088 -- Now generic formals are visible, and the specification can be
1089 -- analyzed, for subsequent conformance check.
1091 Body_Id
:= Analyze_Subprogram_Specification
(Spec
);
1093 -- Make formal parameters visible
1097 -- E is the first formal parameter, we loop through the formals
1098 -- installing them so that they will be visible.
1100 Set_First_Entity
(Gen_Id
, E
);
1101 while Present
(E
) loop
1107 -- Visible generic entity is callable within its own body
1109 Set_Ekind
(Gen_Id
, Ekind
(Body_Id
));
1110 Set_Contract
(Body_Id
, Empty
);
1111 Set_Ekind
(Body_Id
, E_Subprogram_Body
);
1112 Set_Convention
(Body_Id
, Convention
(Gen_Id
));
1113 Set_Is_Obsolescent
(Body_Id
, Is_Obsolescent
(Gen_Id
));
1114 Set_Scope
(Body_Id
, Scope
(Gen_Id
));
1115 Check_Fully_Conformant
(Body_Id
, Gen_Id
, Body_Id
);
1117 if Nkind
(N
) = N_Subprogram_Body_Stub
then
1119 -- No body to analyze, so restore state of generic unit
1121 Set_Ekind
(Gen_Id
, Kind
);
1122 Set_Ekind
(Body_Id
, Kind
);
1124 if Present
(First_Ent
) then
1125 Set_First_Entity
(Gen_Id
, First_Ent
);
1132 -- If this is a compilation unit, it must be made visible explicitly,
1133 -- because the compilation of the declaration, unlike other library
1134 -- unit declarations, does not. If it is not a unit, the following
1135 -- is redundant but harmless.
1137 Set_Is_Immediately_Visible
(Gen_Id
);
1138 Reference_Body_Formals
(Gen_Id
, Body_Id
);
1140 if Is_Child_Unit
(Gen_Id
) then
1141 Generate_Reference
(Gen_Id
, Scope
(Gen_Id
), 'k', False);
1144 Set_Actual_Subtypes
(N
, Current_Scope
);
1146 -- Deal with preconditions and postconditions. In formal verification
1147 -- mode, we keep pre- and postconditions attached to entities rather
1148 -- than inserted in the code, in order to facilitate a distinct
1149 -- treatment for them.
1151 if not SPARK_Mode
then
1152 Process_PPCs
(N
, Gen_Id
, Body_Id
);
1155 -- If the generic unit carries pre- or post-conditions, copy them
1156 -- to the original generic tree, so that they are properly added
1157 -- to any instantiation.
1160 Orig
: constant Node_Id
:= Original_Node
(N
);
1164 Cond
:= First
(Declarations
(N
));
1165 while Present
(Cond
) loop
1166 if Nkind
(Cond
) = N_Pragma
1167 and then Pragma_Name
(Cond
) = Name_Check
1169 Prepend
(New_Copy_Tree
(Cond
), Declarations
(Orig
));
1171 elsif Nkind
(Cond
) = N_Pragma
1172 and then Pragma_Name
(Cond
) = Name_Postcondition
1174 Set_Ekind
(Defining_Entity
(Orig
), Ekind
(Gen_Id
));
1175 Prepend
(New_Copy_Tree
(Cond
), Declarations
(Orig
));
1184 Analyze_Declarations
(Declarations
(N
));
1186 Analyze
(Handled_Statement_Sequence
(N
));
1188 Save_Global_References
(Original_Node
(N
));
1190 -- Prior to exiting the scope, include generic formals again (if any
1191 -- are present) in the set of local entities.
1193 if Present
(First_Ent
) then
1194 Set_First_Entity
(Gen_Id
, First_Ent
);
1197 Check_References
(Gen_Id
);
1200 Process_End_Label
(Handled_Statement_Sequence
(N
), 't', Current_Scope
);
1202 Check_Subprogram_Order
(N
);
1204 -- Outside of its body, unit is generic again
1206 Set_Ekind
(Gen_Id
, Kind
);
1207 Generate_Reference
(Gen_Id
, Body_Id
, 'b', Set_Ref
=> False);
1210 Style
.Check_Identifier
(Body_Id
, Gen_Id
);
1214 end Analyze_Generic_Subprogram_Body
;
1216 ----------------------------
1217 -- Analyze_Null_Procedure --
1218 ----------------------------
1220 procedure Analyze_Null_Procedure
1222 Is_Completion
: out Boolean)
1224 Loc
: constant Source_Ptr
:= Sloc
(N
);
1225 Spec
: constant Node_Id
:= Specification
(N
);
1226 Designator
: Entity_Id
;
1228 Null_Body
: Node_Id
:= Empty
;
1232 -- Capture the profile of the null procedure before analysis, for
1233 -- expansion at the freeze point and at each point of call. The body is
1234 -- used if the procedure has preconditions, or if it is a completion. In
1235 -- the first case the body is analyzed at the freeze point, in the other
1236 -- it replaces the null procedure declaration.
1239 Make_Subprogram_Body
(Loc
,
1240 Specification
=> New_Copy_Tree
(Spec
),
1241 Declarations
=> New_List
,
1242 Handled_Statement_Sequence
=>
1243 Make_Handled_Sequence_Of_Statements
(Loc
,
1244 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
1246 -- Create new entities for body and formals
1248 Set_Defining_Unit_Name
(Specification
(Null_Body
),
1249 Make_Defining_Identifier
(Loc
, Chars
(Defining_Entity
(N
))));
1251 Form
:= First
(Parameter_Specifications
(Specification
(Null_Body
)));
1252 while Present
(Form
) loop
1253 Set_Defining_Identifier
(Form
,
1254 Make_Defining_Identifier
(Loc
, Chars
(Defining_Identifier
(Form
))));
1258 -- Determine whether the null procedure may be a completion of a generic
1259 -- suprogram, in which case we use the new null body as the completion
1260 -- and set minimal semantic information on the original declaration,
1261 -- which is rewritten as a null statement.
1263 Prev
:= Current_Entity_In_Scope
(Defining_Entity
(Spec
));
1265 if Present
(Prev
) and then Is_Generic_Subprogram
(Prev
) then
1266 Insert_Before
(N
, Null_Body
);
1267 Set_Ekind
(Defining_Entity
(N
), Ekind
(Prev
));
1268 Set_Contract
(Defining_Entity
(N
), Make_Contract
(Loc
));
1270 Rewrite
(N
, Make_Null_Statement
(Loc
));
1271 Analyze_Generic_Subprogram_Body
(Null_Body
, Prev
);
1272 Is_Completion
:= True;
1277 -- Resolve the types of the formals now, because the freeze point
1278 -- may appear in a different context, e.g. an instantiation.
1280 Form
:= First
(Parameter_Specifications
(Specification
(Null_Body
)));
1281 while Present
(Form
) loop
1282 if Nkind
(Parameter_Type
(Form
)) /= N_Access_Definition
then
1283 Find_Type
(Parameter_Type
(Form
));
1286 No
(Access_To_Subprogram_Definition
(Parameter_Type
(Form
)))
1288 Find_Type
(Subtype_Mark
(Parameter_Type
(Form
)));
1291 -- The case of a null procedure with a formal that is an
1292 -- access_to_subprogram type, and that is used as an actual
1293 -- in an instantiation is left to the enthusiastic reader.
1302 -- If there are previous overloadable entities with the same name,
1303 -- check whether any of them is completed by the null procedure.
1305 if Present
(Prev
) and then Is_Overloadable
(Prev
) then
1306 Designator
:= Analyze_Subprogram_Specification
(Spec
);
1307 Prev
:= Find_Corresponding_Spec
(N
);
1310 if No
(Prev
) or else not Comes_From_Source
(Prev
) then
1311 Designator
:= Analyze_Subprogram_Specification
(Spec
);
1312 Set_Has_Completion
(Designator
);
1314 -- Signal to caller that this is a procedure declaration
1316 Is_Completion
:= False;
1318 -- Null procedures are always inlined, but generic formal subprograms
1319 -- which appear as such in the internal instance of formal packages,
1320 -- need no completion and are not marked Inline.
1323 and then Nkind
(N
) /= N_Formal_Concrete_Subprogram_Declaration
1325 Set_Corresponding_Body
(N
, Defining_Entity
(Null_Body
));
1326 Set_Body_To_Inline
(N
, Null_Body
);
1327 Set_Is_Inlined
(Designator
);
1331 -- The null procedure is a completion
1333 Is_Completion
:= True;
1335 if Expander_Active
then
1336 Rewrite
(N
, Null_Body
);
1340 Designator
:= Analyze_Subprogram_Specification
(Spec
);
1341 Set_Has_Completion
(Designator
);
1342 Set_Has_Completion
(Prev
);
1345 end Analyze_Null_Procedure
;
1347 -----------------------------
1348 -- Analyze_Operator_Symbol --
1349 -----------------------------
1351 -- An operator symbol such as "+" or "and" may appear in context where the
1352 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1353 -- is just a string, as in (conjunction = "or"). In these cases the parser
1354 -- generates this node, and the semantics does the disambiguation. Other
1355 -- such case are actuals in an instantiation, the generic unit in an
1356 -- instantiation, and pragma arguments.
1358 procedure Analyze_Operator_Symbol
(N
: Node_Id
) is
1359 Par
: constant Node_Id
:= Parent
(N
);
1362 if (Nkind
(Par
) = N_Function_Call
1363 and then N
= Name
(Par
))
1364 or else Nkind
(Par
) = N_Function_Instantiation
1365 or else (Nkind
(Par
) = N_Indexed_Component
1366 and then N
= Prefix
(Par
))
1367 or else (Nkind
(Par
) = N_Pragma_Argument_Association
1368 and then not Is_Pragma_String_Literal
(Par
))
1369 or else Nkind
(Par
) = N_Subprogram_Renaming_Declaration
1370 or else (Nkind
(Par
) = N_Attribute_Reference
1371 and then Attribute_Name
(Par
) /= Name_Value
)
1373 Find_Direct_Name
(N
);
1376 Change_Operator_Symbol_To_String_Literal
(N
);
1379 end Analyze_Operator_Symbol
;
1381 -----------------------------------
1382 -- Analyze_Parameter_Association --
1383 -----------------------------------
1385 procedure Analyze_Parameter_Association
(N
: Node_Id
) is
1387 Analyze
(Explicit_Actual_Parameter
(N
));
1388 end Analyze_Parameter_Association
;
1390 ----------------------------
1391 -- Analyze_Procedure_Call --
1392 ----------------------------
1394 procedure Analyze_Procedure_Call
(N
: Node_Id
) is
1395 Loc
: constant Source_Ptr
:= Sloc
(N
);
1396 P
: constant Node_Id
:= Name
(N
);
1397 Actuals
: constant List_Id
:= Parameter_Associations
(N
);
1401 procedure Analyze_Call_And_Resolve
;
1402 -- Do Analyze and Resolve calls for procedure call
1403 -- At end, check illegal order dependence.
1405 ------------------------------
1406 -- Analyze_Call_And_Resolve --
1407 ------------------------------
1409 procedure Analyze_Call_And_Resolve
is
1411 if Nkind
(N
) = N_Procedure_Call_Statement
then
1413 Resolve
(N
, Standard_Void_Type
);
1417 end Analyze_Call_And_Resolve
;
1419 -- Start of processing for Analyze_Procedure_Call
1422 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1423 -- a procedure call or an entry call. The prefix may denote an access
1424 -- to subprogram type, in which case an implicit dereference applies.
1425 -- If the prefix is an indexed component (without implicit dereference)
1426 -- then the construct denotes a call to a member of an entire family.
1427 -- If the prefix is a simple name, it may still denote a call to a
1428 -- parameterless member of an entry family. Resolution of these various
1429 -- interpretations is delicate.
1433 -- If this is a call of the form Obj.Op, the call may have been
1434 -- analyzed and possibly rewritten into a block, in which case
1437 if Analyzed
(N
) then
1441 -- If there is an error analyzing the name (which may have been
1442 -- rewritten if the original call was in prefix notation) then error
1443 -- has been emitted already, mark node and return.
1445 if Error_Posted
(N
) or else Etype
(Name
(N
)) = Any_Type
then
1446 Set_Etype
(N
, Any_Type
);
1450 -- Otherwise analyze the parameters
1452 if Present
(Actuals
) then
1453 Actual
:= First
(Actuals
);
1455 while Present
(Actual
) loop
1457 Check_Parameterless_Call
(Actual
);
1462 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1464 if Nkind
(P
) = N_Attribute_Reference
1465 and then Nam_In
(Attribute_Name
(P
), Name_Elab_Spec
,
1467 Name_Elab_Subp_Body
)
1469 if Present
(Actuals
) then
1471 ("no parameters allowed for this call", First
(Actuals
));
1475 Set_Etype
(N
, Standard_Void_Type
);
1478 elsif Is_Entity_Name
(P
)
1479 and then Is_Record_Type
(Etype
(Entity
(P
)))
1480 and then Remote_AST_I_Dereference
(P
)
1484 elsif Is_Entity_Name
(P
)
1485 and then Ekind
(Entity
(P
)) /= E_Entry_Family
1487 if Is_Access_Type
(Etype
(P
))
1488 and then Ekind
(Designated_Type
(Etype
(P
))) = E_Subprogram_Type
1489 and then No
(Actuals
)
1490 and then Comes_From_Source
(N
)
1492 Error_Msg_N
("missing explicit dereference in call", N
);
1495 Analyze_Call_And_Resolve
;
1497 -- If the prefix is the simple name of an entry family, this is
1498 -- a parameterless call from within the task body itself.
1500 elsif Is_Entity_Name
(P
)
1501 and then Nkind
(P
) = N_Identifier
1502 and then Ekind
(Entity
(P
)) = E_Entry_Family
1503 and then Present
(Actuals
)
1504 and then No
(Next
(First
(Actuals
)))
1506 -- Can be call to parameterless entry family. What appears to be the
1507 -- sole argument is in fact the entry index. Rewrite prefix of node
1508 -- accordingly. Source representation is unchanged by this
1512 Make_Indexed_Component
(Loc
,
1514 Make_Selected_Component
(Loc
,
1515 Prefix
=> New_Occurrence_Of
(Scope
(Entity
(P
)), Loc
),
1516 Selector_Name
=> New_Occurrence_Of
(Entity
(P
), Loc
)),
1517 Expressions
=> Actuals
);
1518 Set_Name
(N
, New_N
);
1519 Set_Etype
(New_N
, Standard_Void_Type
);
1520 Set_Parameter_Associations
(N
, No_List
);
1521 Analyze_Call_And_Resolve
;
1523 elsif Nkind
(P
) = N_Explicit_Dereference
then
1524 if Ekind
(Etype
(P
)) = E_Subprogram_Type
then
1525 Analyze_Call_And_Resolve
;
1527 Error_Msg_N
("expect access to procedure in call", P
);
1530 -- The name can be a selected component or an indexed component that
1531 -- yields an access to subprogram. Such a prefix is legal if the call
1532 -- has parameter associations.
1534 elsif Is_Access_Type
(Etype
(P
))
1535 and then Ekind
(Designated_Type
(Etype
(P
))) = E_Subprogram_Type
1537 if Present
(Actuals
) then
1538 Analyze_Call_And_Resolve
;
1540 Error_Msg_N
("missing explicit dereference in call ", N
);
1543 -- If not an access to subprogram, then the prefix must resolve to the
1544 -- name of an entry, entry family, or protected operation.
1546 -- For the case of a simple entry call, P is a selected component where
1547 -- the prefix is the task and the selector name is the entry. A call to
1548 -- a protected procedure will have the same syntax. If the protected
1549 -- object contains overloaded operations, the entity may appear as a
1550 -- function, the context will select the operation whose type is Void.
1552 elsif Nkind
(P
) = N_Selected_Component
1553 and then Ekind_In
(Entity
(Selector_Name
(P
)), E_Entry
,
1557 Analyze_Call_And_Resolve
;
1559 elsif Nkind
(P
) = N_Selected_Component
1560 and then Ekind
(Entity
(Selector_Name
(P
))) = E_Entry_Family
1561 and then Present
(Actuals
)
1562 and then No
(Next
(First
(Actuals
)))
1564 -- Can be call to parameterless entry family. What appears to be the
1565 -- sole argument is in fact the entry index. Rewrite prefix of node
1566 -- accordingly. Source representation is unchanged by this
1570 Make_Indexed_Component
(Loc
,
1571 Prefix
=> New_Copy
(P
),
1572 Expressions
=> Actuals
);
1573 Set_Name
(N
, New_N
);
1574 Set_Etype
(New_N
, Standard_Void_Type
);
1575 Set_Parameter_Associations
(N
, No_List
);
1576 Analyze_Call_And_Resolve
;
1578 -- For the case of a reference to an element of an entry family, P is
1579 -- an indexed component whose prefix is a selected component (task and
1580 -- entry family), and whose index is the entry family index.
1582 elsif Nkind
(P
) = N_Indexed_Component
1583 and then Nkind
(Prefix
(P
)) = N_Selected_Component
1584 and then Ekind
(Entity
(Selector_Name
(Prefix
(P
)))) = E_Entry_Family
1586 Analyze_Call_And_Resolve
;
1588 -- If the prefix is the name of an entry family, it is a call from
1589 -- within the task body itself.
1591 elsif Nkind
(P
) = N_Indexed_Component
1592 and then Nkind
(Prefix
(P
)) = N_Identifier
1593 and then Ekind
(Entity
(Prefix
(P
))) = E_Entry_Family
1596 Make_Selected_Component
(Loc
,
1597 Prefix
=> New_Occurrence_Of
(Scope
(Entity
(Prefix
(P
))), Loc
),
1598 Selector_Name
=> New_Occurrence_Of
(Entity
(Prefix
(P
)), Loc
));
1599 Rewrite
(Prefix
(P
), New_N
);
1601 Analyze_Call_And_Resolve
;
1603 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1604 -- procedure name, so the construct can only be a qualified expression.
1606 elsif Nkind
(P
) = N_Qualified_Expression
1607 and then Ada_Version
>= Ada_2012
1609 Rewrite
(N
, Make_Code_Statement
(Loc
, Expression
=> P
));
1612 -- Anything else is an error
1615 Error_Msg_N
("invalid procedure or entry call", N
);
1617 end Analyze_Procedure_Call
;
1619 ------------------------------
1620 -- Analyze_Return_Statement --
1621 ------------------------------
1623 procedure Analyze_Return_Statement
(N
: Node_Id
) is
1625 pragma Assert
(Nkind_In
(N
, N_Simple_Return_Statement
,
1626 N_Extended_Return_Statement
));
1628 Returns_Object
: constant Boolean :=
1629 Nkind
(N
) = N_Extended_Return_Statement
1631 (Nkind
(N
) = N_Simple_Return_Statement
1632 and then Present
(Expression
(N
)));
1633 -- True if we're returning something; that is, "return <expression>;"
1634 -- or "return Result : T [:= ...]". False for "return;". Used for error
1635 -- checking: If Returns_Object is True, N should apply to a function
1636 -- body; otherwise N should apply to a procedure body, entry body,
1637 -- accept statement, or extended return statement.
1639 function Find_What_It_Applies_To
return Entity_Id
;
1640 -- Find the entity representing the innermost enclosing body, accept
1641 -- statement, or extended return statement. If the result is a callable
1642 -- construct or extended return statement, then this will be the value
1643 -- of the Return_Applies_To attribute. Otherwise, the program is
1644 -- illegal. See RM-6.5(4/2).
1646 -----------------------------
1647 -- Find_What_It_Applies_To --
1648 -----------------------------
1650 function Find_What_It_Applies_To
return Entity_Id
is
1651 Result
: Entity_Id
:= Empty
;
1654 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1655 -- and postconditions.
1657 for J
in reverse 0 .. Scope_Stack
.Last
loop
1658 Result
:= Scope_Stack
.Table
(J
).Entity
;
1659 exit when not Ekind_In
(Result
, E_Block
, E_Loop
)
1660 and then Chars
(Result
) /= Name_uPostconditions
;
1663 pragma Assert
(Present
(Result
));
1665 end Find_What_It_Applies_To
;
1667 -- Local declarations
1669 Scope_Id
: constant Entity_Id
:= Find_What_It_Applies_To
;
1670 Kind
: constant Entity_Kind
:= Ekind
(Scope_Id
);
1671 Loc
: constant Source_Ptr
:= Sloc
(N
);
1672 Stm_Entity
: constant Entity_Id
:=
1674 (E_Return_Statement
, Current_Scope
, Loc
, 'R');
1676 -- Start of processing for Analyze_Return_Statement
1679 Set_Return_Statement_Entity
(N
, Stm_Entity
);
1681 Set_Etype
(Stm_Entity
, Standard_Void_Type
);
1682 Set_Return_Applies_To
(Stm_Entity
, Scope_Id
);
1684 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1685 -- (4/2): an inner return statement will apply to this extended return.
1687 if Nkind
(N
) = N_Extended_Return_Statement
then
1688 Push_Scope
(Stm_Entity
);
1691 -- Check that pragma No_Return is obeyed. Don't complain about the
1692 -- implicitly-generated return that is placed at the end.
1694 if No_Return
(Scope_Id
) and then Comes_From_Source
(N
) then
1695 Error_Msg_N
("RETURN statement not allowed (No_Return)", N
);
1698 -- Warn on any unassigned OUT parameters if in procedure
1700 if Ekind
(Scope_Id
) = E_Procedure
then
1701 Warn_On_Unassigned_Out_Parameter
(N
, Scope_Id
);
1704 -- Check that functions return objects, and other things do not
1706 if Kind
= E_Function
or else Kind
= E_Generic_Function
then
1707 if not Returns_Object
then
1708 Error_Msg_N
("missing expression in return from function", N
);
1711 elsif Kind
= E_Procedure
or else Kind
= E_Generic_Procedure
then
1712 if Returns_Object
then
1713 Error_Msg_N
("procedure cannot return value (use function)", N
);
1716 elsif Kind
= E_Entry
or else Kind
= E_Entry_Family
then
1717 if Returns_Object
then
1718 if Is_Protected_Type
(Scope
(Scope_Id
)) then
1719 Error_Msg_N
("entry body cannot return value", N
);
1721 Error_Msg_N
("accept statement cannot return value", N
);
1725 elsif Kind
= E_Return_Statement
then
1727 -- We are nested within another return statement, which must be an
1728 -- extended_return_statement.
1730 if Returns_Object
then
1731 if Nkind
(N
) = N_Extended_Return_Statement
then
1733 ("extended return statement cannot be nested (use `RETURN;`)",
1736 -- Case of a simple return statement with a value inside extended
1737 -- return statement.
1741 ("return nested in extended return statement cannot return " &
1742 "value (use `RETURN;`)", N
);
1747 Error_Msg_N
("illegal context for return statement", N
);
1750 if Ekind_In
(Kind
, E_Function
, E_Generic_Function
) then
1751 Analyze_Function_Return
(N
);
1753 elsif Ekind_In
(Kind
, E_Procedure
, E_Generic_Procedure
) then
1754 Set_Return_Present
(Scope_Id
);
1757 if Nkind
(N
) = N_Extended_Return_Statement
then
1761 Kill_Current_Values
(Last_Assignment_Only
=> True);
1762 Check_Unreachable_Code
(N
);
1764 Analyze_Dimension
(N
);
1765 end Analyze_Return_Statement
;
1767 -------------------------------------
1768 -- Analyze_Simple_Return_Statement --
1769 -------------------------------------
1771 procedure Analyze_Simple_Return_Statement
(N
: Node_Id
) is
1773 if Present
(Expression
(N
)) then
1774 Mark_Coextensions
(N
, Expression
(N
));
1777 Analyze_Return_Statement
(N
);
1778 end Analyze_Simple_Return_Statement
;
1780 -------------------------
1781 -- Analyze_Return_Type --
1782 -------------------------
1784 procedure Analyze_Return_Type
(N
: Node_Id
) is
1785 Designator
: constant Entity_Id
:= Defining_Entity
(N
);
1786 Typ
: Entity_Id
:= Empty
;
1789 -- Normal case where result definition does not indicate an error
1791 if Result_Definition
(N
) /= Error
then
1792 if Nkind
(Result_Definition
(N
)) = N_Access_Definition
then
1793 Check_SPARK_Restriction
1794 ("access result is not allowed", Result_Definition
(N
));
1796 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1799 AD
: constant Node_Id
:=
1800 Access_To_Subprogram_Definition
(Result_Definition
(N
));
1802 if Present
(AD
) and then Protected_Present
(AD
) then
1803 Typ
:= Replace_Anonymous_Access_To_Protected_Subprogram
(N
);
1805 Typ
:= Access_Definition
(N
, Result_Definition
(N
));
1809 Set_Parent
(Typ
, Result_Definition
(N
));
1810 Set_Is_Local_Anonymous_Access
(Typ
);
1811 Set_Etype
(Designator
, Typ
);
1813 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1815 Null_Exclusion_Static_Checks
(N
);
1817 -- Subtype_Mark case
1820 Find_Type
(Result_Definition
(N
));
1821 Typ
:= Entity
(Result_Definition
(N
));
1822 Set_Etype
(Designator
, Typ
);
1824 -- Unconstrained array as result is not allowed in SPARK
1826 if Is_Array_Type
(Typ
) and then not Is_Constrained
(Typ
) then
1827 Check_SPARK_Restriction
1828 ("returning an unconstrained array is not allowed",
1829 Result_Definition
(N
));
1832 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1834 Null_Exclusion_Static_Checks
(N
);
1836 -- If a null exclusion is imposed on the result type, then create
1837 -- a null-excluding itype (an access subtype) and use it as the
1838 -- function's Etype. Note that the null exclusion checks are done
1839 -- right before this, because they don't get applied to types that
1840 -- do not come from source.
1842 if Is_Access_Type
(Typ
) and then Null_Exclusion_Present
(N
) then
1843 Set_Etype
(Designator
,
1844 Create_Null_Excluding_Itype
1847 Scope_Id
=> Scope
(Current_Scope
)));
1849 -- The new subtype must be elaborated before use because
1850 -- it is visible outside of the function. However its base
1851 -- type may not be frozen yet, so the reference that will
1852 -- force elaboration must be attached to the freezing of
1855 -- If the return specification appears on a proper body,
1856 -- the subtype will have been created already on the spec.
1858 if Is_Frozen
(Typ
) then
1859 if Nkind
(Parent
(N
)) = N_Subprogram_Body
1860 and then Nkind
(Parent
(Parent
(N
))) = N_Subunit
1864 Build_Itype_Reference
(Etype
(Designator
), Parent
(N
));
1868 Ensure_Freeze_Node
(Typ
);
1871 IR
: constant Node_Id
:= Make_Itype_Reference
(Sloc
(N
));
1873 Set_Itype
(IR
, Etype
(Designator
));
1874 Append_Freeze_Actions
(Typ
, New_List
(IR
));
1879 Set_Etype
(Designator
, Typ
);
1882 if Ekind
(Typ
) = E_Incomplete_Type
1883 and then Is_Value_Type
(Typ
)
1887 elsif Ekind
(Typ
) = E_Incomplete_Type
1888 or else (Is_Class_Wide_Type
(Typ
)
1889 and then Ekind
(Root_Type
(Typ
)) = E_Incomplete_Type
)
1891 -- AI05-0151: Tagged incomplete types are allowed in all formal
1892 -- parts. Untagged incomplete types are not allowed in bodies.
1894 if Ada_Version
>= Ada_2012
then
1895 if Is_Tagged_Type
(Typ
) then
1898 elsif Nkind_In
(Parent
(Parent
(N
)),
1904 ("invalid use of untagged incomplete type&",
1908 -- The type must be completed in the current package. This
1909 -- is checked at the end of the package declaraton, when
1910 -- Taft-amendment types are identified. If the return type
1911 -- is class-wide, there is no required check, the type can
1912 -- be a bona fide TAT.
1914 if Ekind
(Scope
(Current_Scope
)) = E_Package
1915 and then In_Private_Part
(Scope
(Current_Scope
))
1916 and then not Is_Class_Wide_Type
(Typ
)
1918 Append_Elmt
(Designator
, Private_Dependents
(Typ
));
1923 ("invalid use of incomplete type&", Designator
, Typ
);
1928 -- Case where result definition does indicate an error
1931 Set_Etype
(Designator
, Any_Type
);
1933 end Analyze_Return_Type
;
1935 -----------------------------
1936 -- Analyze_Subprogram_Body --
1937 -----------------------------
1939 procedure Analyze_Subprogram_Body
(N
: Node_Id
) is
1940 Loc
: constant Source_Ptr
:= Sloc
(N
);
1941 Body_Spec
: constant Node_Id
:= Specification
(N
);
1942 Body_Id
: constant Entity_Id
:= Defining_Entity
(Body_Spec
);
1945 if Debug_Flag_C
then
1946 Write_Str
("==> subprogram body ");
1947 Write_Name
(Chars
(Body_Id
));
1948 Write_Str
(" from ");
1949 Write_Location
(Loc
);
1954 Trace_Scope
(N
, Body_Id
, " Analyze subprogram: ");
1956 -- The real work is split out into the helper, so it can do "return;"
1957 -- without skipping the debug output:
1959 Analyze_Subprogram_Body_Helper
(N
);
1961 if Debug_Flag_C
then
1963 Write_Str
("<== subprogram body ");
1964 Write_Name
(Chars
(Body_Id
));
1965 Write_Str
(" from ");
1966 Write_Location
(Loc
);
1969 end Analyze_Subprogram_Body
;
1971 ------------------------------------
1972 -- Analyze_Subprogram_Body_Helper --
1973 ------------------------------------
1975 -- This procedure is called for regular subprogram bodies, generic bodies,
1976 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1977 -- specification matters, and is used to create a proper declaration for
1978 -- the subprogram, or to perform conformance checks.
1980 procedure Analyze_Subprogram_Body_Helper
(N
: Node_Id
) is
1981 Loc
: constant Source_Ptr
:= Sloc
(N
);
1982 Body_Spec
: constant Node_Id
:= Specification
(N
);
1983 Body_Id
: Entity_Id
:= Defining_Entity
(Body_Spec
);
1984 Prev_Id
: constant Entity_Id
:= Current_Entity_In_Scope
(Body_Id
);
1985 Conformant
: Boolean;
1987 Prot_Typ
: Entity_Id
:= Empty
;
1988 Spec_Id
: Entity_Id
;
1989 Spec_Decl
: Node_Id
:= Empty
;
1991 Last_Real_Spec_Entity
: Entity_Id
:= Empty
;
1992 -- When we analyze a separate spec, the entity chain ends up containing
1993 -- the formals, as well as any itypes generated during analysis of the
1994 -- default expressions for parameters, or the arguments of associated
1995 -- precondition/postcondition pragmas (which are analyzed in the context
1996 -- of the spec since they have visibility on formals).
1998 -- These entities belong with the spec and not the body. However we do
1999 -- the analysis of the body in the context of the spec (again to obtain
2000 -- visibility to the formals), and all the entities generated during
2001 -- this analysis end up also chained to the entity chain of the spec.
2002 -- But they really belong to the body, and there is circuitry to move
2003 -- them from the spec to the body.
2005 -- However, when we do this move, we don't want to move the real spec
2006 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2007 -- variable points to the last real spec entity, so we only move those
2008 -- chained beyond that point. It is initialized to Empty to deal with
2009 -- the case where there is no separate spec.
2011 procedure Check_Anonymous_Return
;
2012 -- Ada 2005: if a function returns an access type that denotes a task,
2013 -- or a type that contains tasks, we must create a master entity for
2014 -- the anonymous type, which typically will be used in an allocator
2015 -- in the body of the function.
2017 procedure Check_Inline_Pragma
(Spec
: in out Node_Id
);
2018 -- Look ahead to recognize a pragma that may appear after the body.
2019 -- If there is a previous spec, check that it appears in the same
2020 -- declarative part. If the pragma is Inline_Always, perform inlining
2021 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2022 -- If the body acts as a spec, and inlining is required, we create a
2023 -- subprogram declaration for it, in order to attach the body to inline.
2024 -- If pragma does not appear after the body, check whether there is
2025 -- an inline pragma before any local declarations.
2027 procedure Check_Missing_Return
;
2028 -- Checks for a function with a no return statements, and also performs
2029 -- the warning checks implemented by Check_Returns. In formal mode, also
2030 -- verify that a function ends with a RETURN and that a procedure does
2031 -- not contain any RETURN.
2033 function Disambiguate_Spec
return Entity_Id
;
2034 -- When a primitive is declared between the private view and the full
2035 -- view of a concurrent type which implements an interface, a special
2036 -- mechanism is used to find the corresponding spec of the primitive
2039 procedure Exchange_Limited_Views
(Subp_Id
: Entity_Id
);
2040 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2041 -- incomplete types coming from a limited context and swap their limited
2042 -- views with the non-limited ones.
2044 function Is_Private_Concurrent_Primitive
2045 (Subp_Id
: Entity_Id
) return Boolean;
2046 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2047 -- type that implements an interface and has a private view.
2049 procedure Set_Trivial_Subprogram
(N
: Node_Id
);
2050 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2051 -- subprogram whose body is being analyzed. N is the statement node
2052 -- causing the flag to be set, if the following statement is a return
2053 -- of an entity, we mark the entity as set in source to suppress any
2054 -- warning on the stylized use of function stubs with a dummy return.
2056 procedure Verify_Overriding_Indicator
;
2057 -- If there was a previous spec, the entity has been entered in the
2058 -- current scope previously. If the body itself carries an overriding
2059 -- indicator, check that it is consistent with the known status of the
2062 ----------------------------
2063 -- Check_Anonymous_Return --
2064 ----------------------------
2066 procedure Check_Anonymous_Return
is
2072 if Present
(Spec_Id
) then
2078 if Ekind
(Scop
) = E_Function
2079 and then Ekind
(Etype
(Scop
)) = E_Anonymous_Access_Type
2080 and then not Is_Thunk
(Scop
)
2081 and then (Has_Task
(Designated_Type
(Etype
(Scop
)))
2083 (Is_Class_Wide_Type
(Designated_Type
(Etype
(Scop
)))
2085 Is_Limited_Record
(Designated_Type
(Etype
(Scop
)))))
2086 and then Expander_Active
2088 -- Avoid cases with no tasking support
2090 and then RTE_Available
(RE_Current_Master
)
2091 and then not Restriction_Active
(No_Task_Hierarchy
)
2094 Make_Object_Declaration
(Loc
,
2095 Defining_Identifier
=>
2096 Make_Defining_Identifier
(Loc
, Name_uMaster
),
2097 Constant_Present
=> True,
2098 Object_Definition
=>
2099 New_Reference_To
(RTE
(RE_Master_Id
), Loc
),
2101 Make_Explicit_Dereference
(Loc
,
2102 New_Reference_To
(RTE
(RE_Current_Master
), Loc
)));
2104 if Present
(Declarations
(N
)) then
2105 Prepend
(Decl
, Declarations
(N
));
2107 Set_Declarations
(N
, New_List
(Decl
));
2110 Set_Master_Id
(Etype
(Scop
), Defining_Identifier
(Decl
));
2111 Set_Has_Master_Entity
(Scop
);
2113 -- Now mark the containing scope as a task master
2116 while Nkind
(Par
) /= N_Compilation_Unit
loop
2117 Par
:= Parent
(Par
);
2118 pragma Assert
(Present
(Par
));
2120 -- If we fall off the top, we are at the outer level, and
2121 -- the environment task is our effective master, so nothing
2125 (Par
, N_Task_Body
, N_Block_Statement
, N_Subprogram_Body
)
2127 Set_Is_Task_Master
(Par
, True);
2132 end Check_Anonymous_Return
;
2134 -------------------------
2135 -- Check_Inline_Pragma --
2136 -------------------------
2138 procedure Check_Inline_Pragma
(Spec
: in out Node_Id
) is
2142 function Is_Inline_Pragma
(N
: Node_Id
) return Boolean;
2143 -- True when N is a pragma Inline or Inline_Always that applies
2144 -- to this subprogram.
2146 -----------------------
2147 -- Is_Inline_Pragma --
2148 -----------------------
2150 function Is_Inline_Pragma
(N
: Node_Id
) return Boolean is
2153 Nkind
(N
) = N_Pragma
2155 (Pragma_Name
(N
) = Name_Inline_Always
2158 and then Pragma_Name
(N
) = Name_Inline
))
2161 (Expression
(First
(Pragma_Argument_Associations
(N
)))) =
2163 end Is_Inline_Pragma
;
2165 -- Start of processing for Check_Inline_Pragma
2168 if not Expander_Active
then
2172 if Is_List_Member
(N
)
2173 and then Present
(Next
(N
))
2174 and then Is_Inline_Pragma
(Next
(N
))
2178 elsif Nkind
(N
) /= N_Subprogram_Body_Stub
2179 and then Present
(Declarations
(N
))
2180 and then Is_Inline_Pragma
(First
(Declarations
(N
)))
2182 Prag
:= First
(Declarations
(N
));
2188 if Present
(Prag
) then
2189 if Present
(Spec_Id
) then
2190 if In_Same_List
(N
, Unit_Declaration_Node
(Spec_Id
)) then
2195 -- Create a subprogram declaration, to make treatment uniform
2198 Subp
: constant Entity_Id
:=
2199 Make_Defining_Identifier
(Loc
, Chars
(Body_Id
));
2200 Decl
: constant Node_Id
:=
2201 Make_Subprogram_Declaration
(Loc
,
2203 New_Copy_Tree
(Specification
(N
)));
2206 Set_Defining_Unit_Name
(Specification
(Decl
), Subp
);
2208 if Present
(First_Formal
(Body_Id
)) then
2209 Plist
:= Copy_Parameter_List
(Body_Id
);
2210 Set_Parameter_Specifications
2211 (Specification
(Decl
), Plist
);
2214 Insert_Before
(N
, Decl
);
2217 Set_Has_Pragma_Inline
(Subp
);
2219 if Pragma_Name
(Prag
) = Name_Inline_Always
then
2220 Set_Is_Inlined
(Subp
);
2221 Set_Has_Pragma_Inline_Always
(Subp
);
2228 end Check_Inline_Pragma
;
2230 --------------------------
2231 -- Check_Missing_Return --
2232 --------------------------
2234 procedure Check_Missing_Return
is
2236 Missing_Ret
: Boolean;
2239 if Nkind
(Body_Spec
) = N_Function_Specification
then
2240 if Present
(Spec_Id
) then
2246 if Return_Present
(Id
) then
2247 Check_Returns
(HSS
, 'F', Missing_Ret
);
2250 Set_Has_Missing_Return
(Id
);
2253 elsif Is_Generic_Subprogram
(Id
)
2254 or else not Is_Machine_Code_Subprogram
(Id
)
2256 Error_Msg_N
("missing RETURN statement in function body", N
);
2259 -- If procedure with No_Return, check returns
2261 elsif Nkind
(Body_Spec
) = N_Procedure_Specification
2262 and then Present
(Spec_Id
)
2263 and then No_Return
(Spec_Id
)
2265 Check_Returns
(HSS
, 'P', Missing_Ret
, Spec_Id
);
2268 -- Special checks in SPARK mode
2270 if Nkind
(Body_Spec
) = N_Function_Specification
then
2272 -- In SPARK mode, last statement of a function should be a return
2275 Stat
: constant Node_Id
:= Last_Source_Statement
(HSS
);
2278 and then not Nkind_In
(Stat
, N_Simple_Return_Statement
,
2279 N_Extended_Return_Statement
)
2281 Check_SPARK_Restriction
2282 ("last statement in function should be RETURN", Stat
);
2286 -- In SPARK mode, verify that a procedure has no return
2288 elsif Nkind
(Body_Spec
) = N_Procedure_Specification
then
2289 if Present
(Spec_Id
) then
2295 -- Would be nice to point to return statement here, can we
2296 -- borrow the Check_Returns procedure here ???
2298 if Return_Present
(Id
) then
2299 Check_SPARK_Restriction
2300 ("procedure should not have RETURN", N
);
2303 end Check_Missing_Return
;
2305 -----------------------
2306 -- Disambiguate_Spec --
2307 -----------------------
2309 function Disambiguate_Spec
return Entity_Id
is
2310 Priv_Spec
: Entity_Id
;
2313 procedure Replace_Types
(To_Corresponding
: Boolean);
2314 -- Depending on the flag, replace the type of formal parameters of
2315 -- Body_Id if it is a concurrent type implementing interfaces with
2316 -- the corresponding record type or the other way around.
2318 procedure Replace_Types
(To_Corresponding
: Boolean) is
2320 Formal_Typ
: Entity_Id
;
2323 Formal
:= First_Formal
(Body_Id
);
2324 while Present
(Formal
) loop
2325 Formal_Typ
:= Etype
(Formal
);
2327 if Is_Class_Wide_Type
(Formal_Typ
) then
2328 Formal_Typ
:= Root_Type
(Formal_Typ
);
2331 -- From concurrent type to corresponding record
2333 if To_Corresponding
then
2334 if Is_Concurrent_Type
(Formal_Typ
)
2335 and then Present
(Corresponding_Record_Type
(Formal_Typ
))
2336 and then Present
(Interfaces
(
2337 Corresponding_Record_Type
(Formal_Typ
)))
2340 Corresponding_Record_Type
(Formal_Typ
));
2343 -- From corresponding record to concurrent type
2346 if Is_Concurrent_Record_Type
(Formal_Typ
)
2347 and then Present
(Interfaces
(Formal_Typ
))
2350 Corresponding_Concurrent_Type
(Formal_Typ
));
2354 Next_Formal
(Formal
);
2358 -- Start of processing for Disambiguate_Spec
2361 -- Try to retrieve the specification of the body as is. All error
2362 -- messages are suppressed because the body may not have a spec in
2363 -- its current state.
2365 Spec_N
:= Find_Corresponding_Spec
(N
, False);
2367 -- It is possible that this is the body of a primitive declared
2368 -- between a private and a full view of a concurrent type. The
2369 -- controlling parameter of the spec carries the concurrent type,
2370 -- not the corresponding record type as transformed by Analyze_
2371 -- Subprogram_Specification. In such cases, we undo the change
2372 -- made by the analysis of the specification and try to find the
2375 -- Note that wrappers already have their corresponding specs and
2376 -- bodies set during their creation, so if the candidate spec is
2377 -- a wrapper, then we definitely need to swap all types to their
2378 -- original concurrent status.
2381 or else Is_Primitive_Wrapper
(Spec_N
)
2383 -- Restore all references of corresponding record types to the
2384 -- original concurrent types.
2386 Replace_Types
(To_Corresponding
=> False);
2387 Priv_Spec
:= Find_Corresponding_Spec
(N
, False);
2389 -- The current body truly belongs to a primitive declared between
2390 -- a private and a full view. We leave the modified body as is,
2391 -- and return the true spec.
2393 if Present
(Priv_Spec
)
2394 and then Is_Private_Primitive
(Priv_Spec
)
2399 -- In case that this is some sort of error, restore the original
2400 -- state of the body.
2402 Replace_Types
(To_Corresponding
=> True);
2406 end Disambiguate_Spec
;
2408 ----------------------------
2409 -- Exchange_Limited_Views --
2410 ----------------------------
2412 procedure Exchange_Limited_Views
(Subp_Id
: Entity_Id
) is
2413 procedure Detect_And_Exchange
(Id
: Entity_Id
);
2414 -- Determine whether Id's type denotes an incomplete type associated
2415 -- with a limited with clause and exchange the limited view with the
2418 -------------------------
2419 -- Detect_And_Exchange --
2420 -------------------------
2422 procedure Detect_And_Exchange
(Id
: Entity_Id
) is
2423 Typ
: constant Entity_Id
:= Etype
(Id
);
2426 if Ekind
(Typ
) = E_Incomplete_Type
2427 and then From_With_Type
(Typ
)
2428 and then Present
(Non_Limited_View
(Typ
))
2430 Set_Etype
(Id
, Non_Limited_View
(Typ
));
2432 end Detect_And_Exchange
;
2438 -- Start of processing for Exchange_Limited_Views
2441 if No
(Subp_Id
) then
2444 -- Do not process subprogram bodies as they already use the non-
2445 -- limited view of types.
2447 elsif not Ekind_In
(Subp_Id
, E_Function
, E_Procedure
) then
2451 -- Examine all formals and swap views when applicable
2453 Formal
:= First_Formal
(Subp_Id
);
2454 while Present
(Formal
) loop
2455 Detect_And_Exchange
(Formal
);
2457 Next_Formal
(Formal
);
2460 -- Process the return type of a function
2462 if Ekind
(Subp_Id
) = E_Function
then
2463 Detect_And_Exchange
(Subp_Id
);
2465 end Exchange_Limited_Views
;
2467 -------------------------------------
2468 -- Is_Private_Concurrent_Primitive --
2469 -------------------------------------
2471 function Is_Private_Concurrent_Primitive
2472 (Subp_Id
: Entity_Id
) return Boolean
2474 Formal_Typ
: Entity_Id
;
2477 if Present
(First_Formal
(Subp_Id
)) then
2478 Formal_Typ
:= Etype
(First_Formal
(Subp_Id
));
2480 if Is_Concurrent_Record_Type
(Formal_Typ
) then
2481 if Is_Class_Wide_Type
(Formal_Typ
) then
2482 Formal_Typ
:= Root_Type
(Formal_Typ
);
2485 Formal_Typ
:= Corresponding_Concurrent_Type
(Formal_Typ
);
2488 -- The type of the first formal is a concurrent tagged type with
2492 Is_Concurrent_Type
(Formal_Typ
)
2493 and then Is_Tagged_Type
(Formal_Typ
)
2494 and then Has_Private_Declaration
(Formal_Typ
);
2498 end Is_Private_Concurrent_Primitive
;
2500 ----------------------------
2501 -- Set_Trivial_Subprogram --
2502 ----------------------------
2504 procedure Set_Trivial_Subprogram
(N
: Node_Id
) is
2505 Nxt
: constant Node_Id
:= Next
(N
);
2508 Set_Is_Trivial_Subprogram
(Body_Id
);
2510 if Present
(Spec_Id
) then
2511 Set_Is_Trivial_Subprogram
(Spec_Id
);
2515 and then Nkind
(Nxt
) = N_Simple_Return_Statement
2516 and then No
(Next
(Nxt
))
2517 and then Present
(Expression
(Nxt
))
2518 and then Is_Entity_Name
(Expression
(Nxt
))
2520 Set_Never_Set_In_Source
(Entity
(Expression
(Nxt
)), False);
2522 end Set_Trivial_Subprogram
;
2524 ---------------------------------
2525 -- Verify_Overriding_Indicator --
2526 ---------------------------------
2528 procedure Verify_Overriding_Indicator
is
2530 if Must_Override
(Body_Spec
) then
2531 if Nkind
(Spec_Id
) = N_Defining_Operator_Symbol
2532 and then Operator_Matches_Spec
(Spec_Id
, Spec_Id
)
2536 elsif not Present
(Overridden_Operation
(Spec_Id
)) then
2538 ("subprogram& is not overriding", Body_Spec
, Spec_Id
);
2541 elsif Must_Not_Override
(Body_Spec
) then
2542 if Present
(Overridden_Operation
(Spec_Id
)) then
2544 ("subprogram& overrides inherited operation",
2545 Body_Spec
, Spec_Id
);
2547 elsif Nkind
(Spec_Id
) = N_Defining_Operator_Symbol
2548 and then Operator_Matches_Spec
(Spec_Id
, Spec_Id
)
2551 ("subprogram & overrides predefined operator ",
2552 Body_Spec
, Spec_Id
);
2554 -- If this is not a primitive operation or protected subprogram,
2555 -- then the overriding indicator is altogether illegal.
2557 elsif not Is_Primitive
(Spec_Id
)
2558 and then Ekind
(Scope
(Spec_Id
)) /= E_Protected_Type
2561 ("overriding indicator only allowed " &
2562 "if subprogram is primitive",
2567 and then Present
(Overridden_Operation
(Spec_Id
))
2569 pragma Assert
(Unit_Declaration_Node
(Body_Id
) = N
);
2570 Style
.Missing_Overriding
(N
, Body_Id
);
2573 and then Can_Override_Operator
(Spec_Id
)
2574 and then not Is_Predefined_File_Name
2575 (Unit_File_Name
(Get_Source_Unit
(Spec_Id
)))
2577 pragma Assert
(Unit_Declaration_Node
(Body_Id
) = N
);
2578 Style
.Missing_Overriding
(N
, Body_Id
);
2580 end Verify_Overriding_Indicator
;
2582 -- Start of processing for Analyze_Subprogram_Body_Helper
2585 -- Generic subprograms are handled separately. They always have a
2586 -- generic specification. Determine whether current scope has a
2587 -- previous declaration.
2589 -- If the subprogram body is defined within an instance of the same
2590 -- name, the instance appears as a package renaming, and will be hidden
2591 -- within the subprogram.
2593 if Present
(Prev_Id
)
2594 and then not Is_Overloadable
(Prev_Id
)
2595 and then (Nkind
(Parent
(Prev_Id
)) /= N_Package_Renaming_Declaration
2596 or else Comes_From_Source
(Prev_Id
))
2598 if Is_Generic_Subprogram
(Prev_Id
) then
2600 Set_Is_Compilation_Unit
(Body_Id
, Is_Compilation_Unit
(Spec_Id
));
2601 Set_Is_Child_Unit
(Body_Id
, Is_Child_Unit
(Spec_Id
));
2603 Analyze_Generic_Subprogram_Body
(N
, Spec_Id
);
2605 if Nkind
(N
) = N_Subprogram_Body
then
2606 HSS
:= Handled_Statement_Sequence
(N
);
2607 Check_Missing_Return
;
2613 -- Previous entity conflicts with subprogram name. Attempting to
2614 -- enter name will post error.
2616 Enter_Name
(Body_Id
);
2620 -- Non-generic case, find the subprogram declaration, if one was seen,
2621 -- or enter new overloaded entity in the current scope. If the
2622 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2623 -- part of the context of one of its subunits. No need to redo the
2626 elsif Prev_Id
= Body_Id
and then Has_Completion
(Body_Id
) then
2630 Body_Id
:= Analyze_Subprogram_Specification
(Body_Spec
);
2632 if Nkind
(N
) = N_Subprogram_Body_Stub
2633 or else No
(Corresponding_Spec
(N
))
2635 if Is_Private_Concurrent_Primitive
(Body_Id
) then
2636 Spec_Id
:= Disambiguate_Spec
;
2638 Spec_Id
:= Find_Corresponding_Spec
(N
);
2641 -- If this is a duplicate body, no point in analyzing it
2643 if Error_Posted
(N
) then
2647 -- A subprogram body should cause freezing of its own declaration,
2648 -- but if there was no previous explicit declaration, then the
2649 -- subprogram will get frozen too late (there may be code within
2650 -- the body that depends on the subprogram having been frozen,
2651 -- such as uses of extra formals), so we force it to be frozen
2652 -- here. Same holds if the body and spec are compilation units.
2653 -- Finally, if the return type is an anonymous access to protected
2654 -- subprogram, it must be frozen before the body because its
2655 -- expansion has generated an equivalent type that is used when
2656 -- elaborating the body.
2658 -- An exception in the case of Ada 2012, AI05-177: The bodies
2659 -- created for expression functions do not freeze.
2662 and then Nkind
(Original_Node
(N
)) /= N_Expression_Function
2664 Freeze_Before
(N
, Body_Id
);
2666 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
2667 Freeze_Before
(N
, Spec_Id
);
2669 elsif Is_Access_Subprogram_Type
(Etype
(Body_Id
)) then
2670 Freeze_Before
(N
, Etype
(Body_Id
));
2674 Spec_Id
:= Corresponding_Spec
(N
);
2678 -- Ada 2012 aspects may appear in a subprogram body, but only if there
2679 -- is no previous spec. Ditto for a subprogram stub that does not have
2680 -- a corresponding spec, but for which there may also be a spec_id.
2682 if Has_Aspects
(N
) then
2683 if Present
(Spec_Id
) then
2685 ("aspect specifications must appear in subprogram declaration",
2688 Analyze_Aspect_Specifications
(N
, Body_Id
);
2692 -- Previously we scanned the body to look for nested subprograms, and
2693 -- rejected an inline directive if nested subprograms were present,
2694 -- because the back-end would generate conflicting symbols for the
2695 -- nested bodies. This is now unnecessary.
2697 -- Look ahead to recognize a pragma Inline that appears after the body
2699 Check_Inline_Pragma
(Spec_Id
);
2701 -- Deal with special case of a fully private operation in the body of
2702 -- the protected type. We must create a declaration for the subprogram,
2703 -- in order to attach the protected subprogram that will be used in
2704 -- internal calls. We exclude compiler generated bodies from the
2705 -- expander since the issue does not arise for those cases.
2708 and then Comes_From_Source
(N
)
2709 and then Is_Protected_Type
(Current_Scope
)
2711 Spec_Id
:= Build_Private_Protected_Declaration
(N
);
2714 -- If a separate spec is present, then deal with freezing issues
2716 if Present
(Spec_Id
) then
2717 Spec_Decl
:= Unit_Declaration_Node
(Spec_Id
);
2718 Verify_Overriding_Indicator
;
2720 -- In general, the spec will be frozen when we start analyzing the
2721 -- body. However, for internally generated operations, such as
2722 -- wrapper functions for inherited operations with controlling
2723 -- results, the spec may not have been frozen by the time we expand
2724 -- the freeze actions that include the bodies. In particular, extra
2725 -- formals for accessibility or for return-in-place may need to be
2726 -- generated. Freeze nodes, if any, are inserted before the current
2727 -- body. These freeze actions are also needed in ASIS mode to enable
2728 -- the proper back-annotations.
2730 if not Is_Frozen
(Spec_Id
)
2731 and then (Expander_Active
or ASIS_Mode
)
2733 -- Force the generation of its freezing node to ensure proper
2734 -- management of access types in the backend.
2736 -- This is definitely needed for some cases, but it is not clear
2737 -- why, to be investigated further???
2739 Set_Has_Delayed_Freeze
(Spec_Id
);
2740 Freeze_Before
(N
, Spec_Id
);
2744 -- Mark presence of postcondition procedure in current scope and mark
2745 -- the procedure itself as needing debug info. The latter is important
2746 -- when analyzing decision coverage (for example, for MC/DC coverage).
2748 if Chars
(Body_Id
) = Name_uPostconditions
then
2749 Set_Has_Postconditions
(Current_Scope
);
2750 Set_Debug_Info_Needed
(Body_Id
);
2753 -- Place subprogram on scope stack, and make formals visible. If there
2754 -- is a spec, the visible entity remains that of the spec.
2756 if Present
(Spec_Id
) then
2757 Generate_Reference
(Spec_Id
, Body_Id
, 'b', Set_Ref
=> False);
2759 if Is_Child_Unit
(Spec_Id
) then
2760 Generate_Reference
(Spec_Id
, Scope
(Spec_Id
), 'k', False);
2764 Style
.Check_Identifier
(Body_Id
, Spec_Id
);
2767 Set_Is_Compilation_Unit
(Body_Id
, Is_Compilation_Unit
(Spec_Id
));
2768 Set_Is_Child_Unit
(Body_Id
, Is_Child_Unit
(Spec_Id
));
2770 if Is_Abstract_Subprogram
(Spec_Id
) then
2771 Error_Msg_N
("an abstract subprogram cannot have a body", N
);
2775 Set_Convention
(Body_Id
, Convention
(Spec_Id
));
2776 Set_Has_Completion
(Spec_Id
);
2778 if Is_Protected_Type
(Scope
(Spec_Id
)) then
2779 Prot_Typ
:= Scope
(Spec_Id
);
2782 -- If this is a body generated for a renaming, do not check for
2783 -- full conformance. The check is redundant, because the spec of
2784 -- the body is a copy of the spec in the renaming declaration,
2785 -- and the test can lead to spurious errors on nested defaults.
2787 if Present
(Spec_Decl
)
2788 and then not Comes_From_Source
(N
)
2790 (Nkind
(Original_Node
(Spec_Decl
)) =
2791 N_Subprogram_Renaming_Declaration
2792 or else (Present
(Corresponding_Body
(Spec_Decl
))
2794 Nkind
(Unit_Declaration_Node
2795 (Corresponding_Body
(Spec_Decl
))) =
2796 N_Subprogram_Renaming_Declaration
))
2800 -- Conversely, the spec may have been generated for specless body
2801 -- with an inline pragma.
2803 elsif Comes_From_Source
(N
)
2804 and then not Comes_From_Source
(Spec_Id
)
2805 and then Has_Pragma_Inline
(Spec_Id
)
2812 Fully_Conformant
, True, Conformant
, Body_Id
);
2815 -- If the body is not fully conformant, we have to decide if we
2816 -- should analyze it or not. If it has a really messed up profile
2817 -- then we probably should not analyze it, since we will get too
2818 -- many bogus messages.
2820 -- Our decision is to go ahead in the non-fully conformant case
2821 -- only if it is at least mode conformant with the spec. Note
2822 -- that the call to Check_Fully_Conformant has issued the proper
2823 -- error messages to complain about the lack of conformance.
2826 and then not Mode_Conformant
(Body_Id
, Spec_Id
)
2832 if Spec_Id
/= Body_Id
then
2833 Reference_Body_Formals
(Spec_Id
, Body_Id
);
2836 if Nkind
(N
) /= N_Subprogram_Body_Stub
then
2837 Set_Corresponding_Spec
(N
, Spec_Id
);
2839 -- Ada 2005 (AI-345): If the operation is a primitive operation
2840 -- of a concurrent type, the type of the first parameter has been
2841 -- replaced with the corresponding record, which is the proper
2842 -- run-time structure to use. However, within the body there may
2843 -- be uses of the formals that depend on primitive operations
2844 -- of the type (in particular calls in prefixed form) for which
2845 -- we need the original concurrent type. The operation may have
2846 -- several controlling formals, so the replacement must be done
2849 if Comes_From_Source
(Spec_Id
)
2850 and then Present
(First_Entity
(Spec_Id
))
2851 and then Ekind
(Etype
(First_Entity
(Spec_Id
))) = E_Record_Type
2852 and then Is_Tagged_Type
(Etype
(First_Entity
(Spec_Id
)))
2854 Present
(Interfaces
(Etype
(First_Entity
(Spec_Id
))))
2857 (Corresponding_Concurrent_Type
2858 (Etype
(First_Entity
(Spec_Id
))))
2861 Typ
: constant Entity_Id
:= Etype
(First_Entity
(Spec_Id
));
2865 Form
:= First_Formal
(Spec_Id
);
2866 while Present
(Form
) loop
2867 if Etype
(Form
) = Typ
then
2868 Set_Etype
(Form
, Corresponding_Concurrent_Type
(Typ
));
2876 -- Make the formals visible, and place subprogram on scope stack.
2877 -- This is also the point at which we set Last_Real_Spec_Entity
2878 -- to mark the entities which will not be moved to the body.
2880 Install_Formals
(Spec_Id
);
2881 Last_Real_Spec_Entity
:= Last_Entity
(Spec_Id
);
2883 -- Within an instance, add local renaming declarations so that
2884 -- gdb can retrieve the values of actuals more easily. This is
2885 -- only relevant if generating code (and indeed we definitely
2886 -- do not want these definitions -gnatc mode, because that would
2889 if Is_Generic_Instance
(Spec_Id
)
2890 and then Is_Wrapper_Package
(Current_Scope
)
2891 and then Expander_Active
2893 Build_Subprogram_Instance_Renamings
(N
, Current_Scope
);
2896 Push_Scope
(Spec_Id
);
2898 -- Make sure that the subprogram is immediately visible. For
2899 -- child units that have no separate spec this is indispensable.
2900 -- Otherwise it is safe albeit redundant.
2902 Set_Is_Immediately_Visible
(Spec_Id
);
2905 Set_Corresponding_Body
(Unit_Declaration_Node
(Spec_Id
), Body_Id
);
2906 Set_Contract
(Body_Id
, Empty
);
2907 Set_Ekind
(Body_Id
, E_Subprogram_Body
);
2908 Set_Scope
(Body_Id
, Scope
(Spec_Id
));
2909 Set_Is_Obsolescent
(Body_Id
, Is_Obsolescent
(Spec_Id
));
2911 -- Case of subprogram body with no previous spec
2914 -- Check for style warning required
2918 -- Only apply check for source level subprograms for which checks
2919 -- have not been suppressed.
2921 and then Comes_From_Source
(Body_Id
)
2922 and then not Suppress_Style_Checks
(Body_Id
)
2924 -- No warnings within an instance
2926 and then not In_Instance
2928 -- No warnings for expression functions
2930 and then Nkind
(Original_Node
(N
)) /= N_Expression_Function
2932 Style
.Body_With_No_Spec
(N
);
2935 New_Overloaded_Entity
(Body_Id
);
2937 if Nkind
(N
) /= N_Subprogram_Body_Stub
then
2938 Set_Acts_As_Spec
(N
);
2939 Generate_Definition
(Body_Id
);
2940 Set_Contract
(Body_Id
, Make_Contract
(Sloc
(Body_Id
)));
2942 (Body_Id
, Body_Id
, 'b', Set_Ref
=> False, Force
=> True);
2943 Install_Formals
(Body_Id
);
2944 Push_Scope
(Body_Id
);
2947 -- For stubs and bodies with no previous spec, generate references to
2950 Generate_Reference_To_Formals
(Body_Id
);
2953 -- If the return type is an anonymous access type whose designated type
2954 -- is the limited view of a class-wide type and the non-limited view is
2955 -- available, update the return type accordingly.
2957 if Ada_Version
>= Ada_2005
and then Comes_From_Source
(N
) then
2963 Rtyp
:= Etype
(Current_Scope
);
2965 if Ekind
(Rtyp
) = E_Anonymous_Access_Type
then
2966 Etyp
:= Directly_Designated_Type
(Rtyp
);
2968 if Is_Class_Wide_Type
(Etyp
) and then From_With_Type
(Etyp
) then
2969 Set_Directly_Designated_Type
2970 (Etype
(Current_Scope
), Available_View
(Etyp
));
2976 -- If this is the proper body of a stub, we must verify that the stub
2977 -- conforms to the body, and to the previous spec if one was present.
2978 -- We know already that the body conforms to that spec. This test is
2979 -- only required for subprograms that come from source.
2981 if Nkind
(Parent
(N
)) = N_Subunit
2982 and then Comes_From_Source
(N
)
2983 and then not Error_Posted
(Body_Id
)
2984 and then Nkind
(Corresponding_Stub
(Parent
(N
))) =
2985 N_Subprogram_Body_Stub
2988 Old_Id
: constant Entity_Id
:=
2990 (Specification
(Corresponding_Stub
(Parent
(N
))));
2992 Conformant
: Boolean := False;
2995 if No
(Spec_Id
) then
2996 Check_Fully_Conformant
(Body_Id
, Old_Id
);
3000 (Body_Id
, Old_Id
, Fully_Conformant
, False, Conformant
);
3002 if not Conformant
then
3004 -- The stub was taken to be a new declaration. Indicate that
3007 Set_Has_Completion
(Old_Id
, False);
3013 Set_Has_Completion
(Body_Id
);
3014 Check_Eliminated
(Body_Id
);
3016 if Nkind
(N
) = N_Subprogram_Body_Stub
then
3020 -- Handle frontend inlining. There is no need to prepare us for inlining
3021 -- if we will not generate the code.
3025 if not Debug_Flag_Dot_K
then
3026 if Present
(Spec_Id
)
3027 and then Expander_Active
3029 (Has_Pragma_Inline_Always
(Spec_Id
)
3030 or else (Has_Pragma_Inline
(Spec_Id
) and Front_End_Inlining
))
3032 Build_Body_To_Inline
(N
, Spec_Id
);
3037 elsif Expander_Active
3038 and then Serious_Errors_Detected
= 0
3039 and then Present
(Spec_Id
)
3040 and then Has_Pragma_Inline
(Spec_Id
)
3042 Check_And_Build_Body_To_Inline
(N
, Spec_Id
, Body_Id
);
3045 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3046 -- of the specification we have to install the private withed units.
3047 -- This holds for child units as well.
3049 if Is_Compilation_Unit
(Body_Id
)
3050 or else Nkind
(Parent
(N
)) = N_Compilation_Unit
3052 Install_Private_With_Clauses
(Body_Id
);
3055 Check_Anonymous_Return
;
3057 -- Set the Protected_Formal field of each extra formal of the protected
3058 -- subprogram to reference the corresponding extra formal of the
3059 -- subprogram that implements it. For regular formals this occurs when
3060 -- the protected subprogram's declaration is expanded, but the extra
3061 -- formals don't get created until the subprogram is frozen. We need to
3062 -- do this before analyzing the protected subprogram's body so that any
3063 -- references to the original subprogram's extra formals will be changed
3064 -- refer to the implementing subprogram's formals (see Expand_Formal).
3066 if Present
(Spec_Id
)
3067 and then Is_Protected_Type
(Scope
(Spec_Id
))
3068 and then Present
(Protected_Body_Subprogram
(Spec_Id
))
3071 Impl_Subp
: constant Entity_Id
:=
3072 Protected_Body_Subprogram
(Spec_Id
);
3073 Prot_Ext_Formal
: Entity_Id
:= Extra_Formals
(Spec_Id
);
3074 Impl_Ext_Formal
: Entity_Id
:= Extra_Formals
(Impl_Subp
);
3076 while Present
(Prot_Ext_Formal
) loop
3077 pragma Assert
(Present
(Impl_Ext_Formal
));
3078 Set_Protected_Formal
(Prot_Ext_Formal
, Impl_Ext_Formal
);
3079 Next_Formal_With_Extras
(Prot_Ext_Formal
);
3080 Next_Formal_With_Extras
(Impl_Ext_Formal
);
3085 -- Now we can go on to analyze the body
3087 HSS
:= Handled_Statement_Sequence
(N
);
3088 Set_Actual_Subtypes
(N
, Current_Scope
);
3090 -- Deal with preconditions and postconditions. In formal verification
3091 -- mode, we keep pre- and postconditions attached to entities rather
3092 -- than inserted in the code, in order to facilitate a distinct
3093 -- treatment for them.
3095 if not SPARK_Mode
then
3096 Process_PPCs
(N
, Spec_Id
, Body_Id
);
3099 -- Add a declaration for the Protection object, renaming declarations
3100 -- for discriminals and privals and finally a declaration for the entry
3101 -- family index (if applicable). This form of early expansion is done
3102 -- when the Expander is active because Install_Private_Data_Declarations
3103 -- references entities which were created during regular expansion. The
3104 -- body may be the rewritting of an expression function, and we need to
3105 -- verify that the original node is in the source.
3107 if Full_Expander_Active
3108 and then Comes_From_Source
(Original_Node
(N
))
3109 and then Present
(Prot_Typ
)
3110 and then Present
(Spec_Id
)
3111 and then not Is_Eliminated
(Spec_Id
)
3113 Install_Private_Data_Declarations
3114 (Sloc
(N
), Spec_Id
, Prot_Typ
, N
, Declarations
(N
));
3117 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3118 -- may now appear in parameter and result profiles. Since the analysis
3119 -- of a subprogram body may use the parameter and result profile of the
3120 -- spec, swap any limited views with their non-limited counterpart.
3122 if Ada_Version
>= Ada_2012
then
3123 Exchange_Limited_Views
(Spec_Id
);
3126 -- Analyze the declarations (this call will analyze the precondition
3127 -- Check pragmas we prepended to the list, as well as the declaration
3128 -- of the _Postconditions procedure).
3130 Analyze_Declarations
(Declarations
(N
));
3132 -- Check completion, and analyze the statements
3135 Inspect_Deferred_Constant_Completion
(Declarations
(N
));
3138 -- Deal with end of scope processing for the body
3140 Process_End_Label
(HSS
, 't', Current_Scope
);
3142 Check_Subprogram_Order
(N
);
3143 Set_Analyzed
(Body_Id
);
3145 -- If we have a separate spec, then the analysis of the declarations
3146 -- caused the entities in the body to be chained to the spec id, but
3147 -- we want them chained to the body id. Only the formal parameters
3148 -- end up chained to the spec id in this case.
3150 if Present
(Spec_Id
) then
3152 -- We must conform to the categorization of our spec
3154 Validate_Categorization_Dependency
(N
, Spec_Id
);
3156 -- And if this is a child unit, the parent units must conform
3158 if Is_Child_Unit
(Spec_Id
) then
3159 Validate_Categorization_Dependency
3160 (Unit_Declaration_Node
(Spec_Id
), Spec_Id
);
3163 -- Here is where we move entities from the spec to the body
3165 -- Case where there are entities that stay with the spec
3167 if Present
(Last_Real_Spec_Entity
) then
3169 -- No body entities (happens when the only real spec entities come
3170 -- from precondition and postcondition pragmas).
3172 if No
(Last_Entity
(Body_Id
)) then
3174 (Body_Id
, Next_Entity
(Last_Real_Spec_Entity
));
3176 -- Body entities present (formals), so chain stuff past them
3180 (Last_Entity
(Body_Id
), Next_Entity
(Last_Real_Spec_Entity
));
3183 Set_Next_Entity
(Last_Real_Spec_Entity
, Empty
);
3184 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
3185 Set_Last_Entity
(Spec_Id
, Last_Real_Spec_Entity
);
3187 -- Case where there are no spec entities, in this case there can be
3188 -- no body entities either, so just move everything.
3191 pragma Assert
(No
(Last_Entity
(Body_Id
)));
3192 Set_First_Entity
(Body_Id
, First_Entity
(Spec_Id
));
3193 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
3194 Set_First_Entity
(Spec_Id
, Empty
);
3195 Set_Last_Entity
(Spec_Id
, Empty
);
3199 Check_Missing_Return
;
3201 -- Now we are going to check for variables that are never modified in
3202 -- the body of the procedure. But first we deal with a special case
3203 -- where we want to modify this check. If the body of the subprogram
3204 -- starts with a raise statement or its equivalent, or if the body
3205 -- consists entirely of a null statement, then it is pretty obvious
3206 -- that it is OK to not reference the parameters. For example, this
3207 -- might be the following common idiom for a stubbed function:
3208 -- statement of the procedure raises an exception. In particular this
3209 -- deals with the common idiom of a stubbed function, which might
3210 -- appear as something like:
3212 -- function F (A : Integer) return Some_Type;
3215 -- raise Program_Error;
3219 -- Here the purpose of X is simply to satisfy the annoying requirement
3220 -- in Ada that there be at least one return, and we certainly do not
3221 -- want to go posting warnings on X that it is not initialized! On
3222 -- the other hand, if X is entirely unreferenced that should still
3225 -- What we do is to detect these cases, and if we find them, flag the
3226 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3227 -- suppress unwanted warnings. For the case of the function stub above
3228 -- we have a special test to set X as apparently assigned to suppress
3235 -- Skip initial labels (for one thing this occurs when we are in
3236 -- front end ZCX mode, but in any case it is irrelevant), and also
3237 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3239 Stm
:= First
(Statements
(HSS
));
3240 while Nkind
(Stm
) = N_Label
3241 or else Nkind
(Stm
) in N_Push_xxx_Label
3246 -- Do the test on the original statement before expansion
3249 Ostm
: constant Node_Id
:= Original_Node
(Stm
);
3252 -- If explicit raise statement, turn on flag
3254 if Nkind
(Ostm
) = N_Raise_Statement
then
3255 Set_Trivial_Subprogram
(Stm
);
3257 -- If null statement, and no following statements, turn on flag
3259 elsif Nkind
(Stm
) = N_Null_Statement
3260 and then Comes_From_Source
(Stm
)
3261 and then No
(Next
(Stm
))
3263 Set_Trivial_Subprogram
(Stm
);
3265 -- Check for explicit call cases which likely raise an exception
3267 elsif Nkind
(Ostm
) = N_Procedure_Call_Statement
then
3268 if Is_Entity_Name
(Name
(Ostm
)) then
3270 Ent
: constant Entity_Id
:= Entity
(Name
(Ostm
));
3273 -- If the procedure is marked No_Return, then likely it
3274 -- raises an exception, but in any case it is not coming
3275 -- back here, so turn on the flag.
3278 and then Ekind
(Ent
) = E_Procedure
3279 and then No_Return
(Ent
)
3281 Set_Trivial_Subprogram
(Stm
);
3289 -- Check for variables that are never modified
3295 -- If there is a separate spec, then transfer Never_Set_In_Source
3296 -- flags from out parameters to the corresponding entities in the
3297 -- body. The reason we do that is we want to post error flags on
3298 -- the body entities, not the spec entities.
3300 if Present
(Spec_Id
) then
3301 E1
:= First_Entity
(Spec_Id
);
3302 while Present
(E1
) loop
3303 if Ekind
(E1
) = E_Out_Parameter
then
3304 E2
:= First_Entity
(Body_Id
);
3305 while Present
(E2
) loop
3306 exit when Chars
(E1
) = Chars
(E2
);
3310 if Present
(E2
) then
3311 Set_Never_Set_In_Source
(E2
, Never_Set_In_Source
(E1
));
3319 -- Check references in body
3321 Check_References
(Body_Id
);
3323 end Analyze_Subprogram_Body_Helper
;
3325 ---------------------------------
3326 -- Analyze_Subprogram_Contract --
3327 ---------------------------------
3329 procedure Analyze_Subprogram_Contract
(Subp
: Entity_Id
) is
3330 Result_Seen
: Boolean := False;
3331 -- A flag which keeps track of whether at least one postcondition or
3332 -- contract-case mentions attribute 'Result (set True if so).
3334 procedure Check_Result_And_Post_State
3336 Error_Nod
: in out Node_Id
);
3337 -- Determine whether pragma Prag mentions attribute 'Result and whether
3338 -- the pragma contains an expression that evaluates differently in pre-
3339 -- and post-state. Prag is a postcondition or a contract-cases pragma.
3340 -- Error_Nod denotes the proper error node.
3342 ---------------------------------
3343 -- Check_Result_And_Post_State --
3344 ---------------------------------
3346 procedure Check_Result_And_Post_State
3348 Error_Nod
: in out Node_Id
)
3350 procedure Check_Expression
(Expr
: Node_Id
);
3351 -- Perform the 'Result and post-state checks on a given expression
3353 function Is_Function_Result
(N
: Node_Id
) return Traverse_Result
;
3354 -- Attempt to find attribute 'Result in a subtree denoted by N
3356 function Is_Trivial_Boolean
(N
: Node_Id
) return Boolean;
3357 -- Determine whether source node N denotes "True" or "False"
3359 function Mentions_Post_State
(N
: Node_Id
) return Boolean;
3360 -- Determine whether a subtree denoted by N mentions any construct
3361 -- that denotes a post-state.
3363 procedure Check_Function_Result
is
3364 new Traverse_Proc
(Is_Function_Result
);
3366 ----------------------
3367 -- Check_Expression --
3368 ----------------------
3370 procedure Check_Expression
(Expr
: Node_Id
) is
3372 if not Is_Trivial_Boolean
(Expr
) then
3373 Check_Function_Result
(Expr
);
3375 if not Mentions_Post_State
(Expr
) then
3376 if Pragma_Name
(Prag
) = Name_Contract_Cases
then
3378 ("contract case refers only to pre-state?T?", Expr
);
3381 ("postcondition refers only to pre-state?T?", Prag
);
3385 end Check_Expression
;
3387 ------------------------
3388 -- Is_Function_Result --
3389 ------------------------
3391 function Is_Function_Result
(N
: Node_Id
) return Traverse_Result
is
3393 if Nkind
(N
) = N_Attribute_Reference
3394 and then Attribute_Name
(N
) = Name_Result
3396 Result_Seen
:= True;
3399 -- Continue the traversal
3404 end Is_Function_Result
;
3406 ------------------------
3407 -- Is_Trivial_Boolean --
3408 ------------------------
3410 function Is_Trivial_Boolean
(N
: Node_Id
) return Boolean is
3413 Comes_From_Source
(N
)
3414 and then Is_Entity_Name
(N
)
3415 and then (Entity
(N
) = Standard_True
3416 or else Entity
(N
) = Standard_False
);
3417 end Is_Trivial_Boolean
;
3419 -------------------------
3420 -- Mentions_Post_State --
3421 -------------------------
3423 function Mentions_Post_State
(N
: Node_Id
) return Boolean is
3424 Post_State_Seen
: Boolean := False;
3426 function Is_Post_State
(N
: Node_Id
) return Traverse_Result
;
3427 -- Attempt to find a construct that denotes a post-state. If this
3428 -- is the case, set flag Post_State_Seen.
3434 function Is_Post_State
(N
: Node_Id
) return Traverse_Result
is
3438 if Nkind_In
(N
, N_Explicit_Dereference
, N_Function_Call
) then
3439 Post_State_Seen
:= True;
3442 elsif Nkind_In
(N
, N_Expanded_Name
, N_Identifier
) then
3445 if No
(Ent
) or else Ekind
(Ent
) in Assignable_Kind
then
3446 Post_State_Seen
:= True;
3450 elsif Nkind
(N
) = N_Attribute_Reference
then
3451 if Attribute_Name
(N
) = Name_Old
then
3453 elsif Attribute_Name
(N
) = Name_Result
then
3454 Post_State_Seen
:= True;
3462 procedure Find_Post_State
is new Traverse_Proc
(Is_Post_State
);
3464 -- Start of processing for Mentions_Post_State
3467 Find_Post_State
(N
);
3468 return Post_State_Seen
;
3469 end Mentions_Post_State
;
3473 Expr
: constant Node_Id
:=
3474 Expression
(First
(Pragma_Argument_Associations
(Prag
)));
3475 Nam
: constant Name_Id
:= Pragma_Name
(Prag
);
3478 -- Start of processing for Check_Result_And_Post_State
3481 if No
(Error_Nod
) then
3485 -- Examine all consequences
3487 if Nam
= Name_Contract_Cases
then
3488 CCase
:= First
(Component_Associations
(Expr
));
3489 while Present
(CCase
) loop
3490 Check_Expression
(Expression
(CCase
));
3495 -- Examine the expression of a postcondition
3498 pragma Assert
(Nam
= Name_Postcondition
);
3499 Check_Expression
(Expr
);
3501 end Check_Result_And_Post_State
;
3505 Items
: constant Node_Id
:= Contract
(Subp
);
3506 Error_CCase
: Node_Id
;
3507 Error_Post
: Node_Id
;
3510 -- Start of processing for Analyze_Subprogram_Contract
3513 Error_CCase
:= Empty
;
3514 Error_Post
:= Empty
;
3516 if Present
(Items
) then
3518 -- Analyze pre- and postconditions
3520 Prag
:= Pre_Post_Conditions
(Items
);
3521 while Present
(Prag
) loop
3522 Analyze_PPC_In_Decl_Part
(Prag
, Subp
);
3524 -- Verify whether a postcondition mentions attribute 'Result and
3525 -- its expression introduces a post-state.
3527 if Warn_On_Suspicious_Contract
3528 and then Pragma_Name
(Prag
) = Name_Postcondition
3530 Check_Result_And_Post_State
(Prag
, Error_Post
);
3533 Prag
:= Next_Pragma
(Prag
);
3536 -- Analyze contract-cases and test-cases
3538 Prag
:= Contract_Test_Cases
(Items
);
3539 while Present
(Prag
) loop
3540 if Pragma_Name
(Prag
) = Name_Contract_Cases
then
3541 Analyze_Contract_Cases_In_Decl_Part
(Prag
);
3543 -- Verify whether contract-cases mention attribute 'Result and
3544 -- its expression introduces a post-state. Perform the check
3545 -- only when the pragma is legal.
3547 if Warn_On_Suspicious_Contract
3548 and then not Error_Posted
(Prag
)
3550 Check_Result_And_Post_State
(Prag
, Error_CCase
);
3554 pragma Assert
(Pragma_Name
(Prag
) = Name_Test_Case
);
3555 Analyze_Test_Case_In_Decl_Part
(Prag
, Subp
);
3558 Prag
:= Next_Pragma
(Prag
);
3561 -- Analyze classification pragmas
3563 Prag
:= Classifications
(Contract
(Subp
));
3564 while Present
(Prag
) loop
3565 if Pragma_Name
(Prag
) = Name_Depends
then
3566 Analyze_Depends_In_Decl_Part
(Prag
);
3568 pragma Assert
(Pragma_Name
(Prag
) = Name_Global
);
3569 Analyze_Global_In_Decl_Part
(Prag
);
3572 Prag
:= Next_Pragma
(Prag
);
3576 -- Emit an error when none of the postconditions or contract-cases
3577 -- mention attribute 'Result in the context of a function.
3579 if Warn_On_Suspicious_Contract
3580 and then Ekind_In
(Subp
, E_Function
, E_Generic_Function
)
3581 and then not Result_Seen
3583 if Present
(Error_Post
) and then Present
(Error_CCase
) then
3585 ("neither function postcondition nor contract cases mention "
3586 & "result?T?", Error_Post
);
3588 elsif Present
(Error_Post
) then
3590 ("function postcondition does not mention result?T?",
3593 elsif Present
(Error_CCase
) then
3595 ("contract cases do not mention result?T?", Error_CCase
);
3598 end Analyze_Subprogram_Contract
;
3600 ------------------------------------
3601 -- Analyze_Subprogram_Declaration --
3602 ------------------------------------
3604 procedure Analyze_Subprogram_Declaration
(N
: Node_Id
) is
3605 Scop
: constant Entity_Id
:= Current_Scope
;
3606 Designator
: Entity_Id
;
3607 Is_Completion
: Boolean;
3608 -- Indicates whether a null procedure declaration is a completion
3611 -- Null procedures are not allowed in SPARK
3613 if Nkind
(Specification
(N
)) = N_Procedure_Specification
3614 and then Null_Present
(Specification
(N
))
3616 Check_SPARK_Restriction
("null procedure is not allowed", N
);
3618 if Is_Protected_Type
(Current_Scope
) then
3619 Error_Msg_N
("protected operation cannot be a null procedure", N
);
3622 Analyze_Null_Procedure
(N
, Is_Completion
);
3624 if Is_Completion
then
3626 -- The null procedure acts as a body, nothing further is needed.
3632 Designator
:= Analyze_Subprogram_Specification
(Specification
(N
));
3634 -- A reference may already have been generated for the unit name, in
3635 -- which case the following call is redundant. However it is needed for
3636 -- declarations that are the rewriting of an expression function.
3638 Generate_Definition
(Designator
);
3640 if Debug_Flag_C
then
3641 Write_Str
("==> subprogram spec ");
3642 Write_Name
(Chars
(Designator
));
3643 Write_Str
(" from ");
3644 Write_Location
(Sloc
(N
));
3649 Validate_RCI_Subprogram_Declaration
(N
);
3650 New_Overloaded_Entity
(Designator
);
3651 Check_Delayed_Subprogram
(Designator
);
3653 -- If the type of the first formal of the current subprogram is a
3654 -- non-generic tagged private type, mark the subprogram as being a
3655 -- private primitive. Ditto if this is a function with controlling
3656 -- result, and the return type is currently private. In both cases,
3657 -- the type of the controlling argument or result must be in the
3658 -- current scope for the operation to be primitive.
3660 if Has_Controlling_Result
(Designator
)
3661 and then Is_Private_Type
(Etype
(Designator
))
3662 and then Scope
(Etype
(Designator
)) = Current_Scope
3663 and then not Is_Generic_Actual_Type
(Etype
(Designator
))
3665 Set_Is_Private_Primitive
(Designator
);
3667 elsif Present
(First_Formal
(Designator
)) then
3669 Formal_Typ
: constant Entity_Id
:=
3670 Etype
(First_Formal
(Designator
));
3672 Set_Is_Private_Primitive
(Designator
,
3673 Is_Tagged_Type
(Formal_Typ
)
3674 and then Scope
(Formal_Typ
) = Current_Scope
3675 and then Is_Private_Type
(Formal_Typ
)
3676 and then not Is_Generic_Actual_Type
(Formal_Typ
));
3680 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
3683 if Ada_Version
>= Ada_2005
3684 and then Comes_From_Source
(N
)
3685 and then Is_Dispatching_Operation
(Designator
)
3692 if Has_Controlling_Result
(Designator
) then
3693 Etyp
:= Etype
(Designator
);
3696 E
:= First_Entity
(Designator
);
3698 and then Is_Formal
(E
)
3699 and then not Is_Controlling_Formal
(E
)
3707 if Is_Access_Type
(Etyp
) then
3708 Etyp
:= Directly_Designated_Type
(Etyp
);
3711 if Is_Interface
(Etyp
)
3712 and then not Is_Abstract_Subprogram
(Designator
)
3713 and then not (Ekind
(Designator
) = E_Procedure
3714 and then Null_Present
(Specification
(N
)))
3716 Error_Msg_Name_1
:= Chars
(Defining_Entity
(N
));
3718 -- Specialize error message based on procedures vs. functions,
3719 -- since functions can't be null subprograms.
3721 if Ekind
(Designator
) = E_Procedure
then
3723 ("interface procedure % must be abstract or null", N
);
3725 Error_Msg_N
("interface function % must be abstract", N
);
3731 -- What is the following code for, it used to be
3733 -- ??? Set_Suppress_Elaboration_Checks
3734 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3736 -- The following seems equivalent, but a bit dubious
3738 if Elaboration_Checks_Suppressed
(Designator
) then
3739 Set_Kill_Elaboration_Checks
(Designator
);
3742 if Scop
/= Standard_Standard
and then not Is_Child_Unit
(Designator
) then
3743 Set_Categorization_From_Scope
(Designator
, Scop
);
3746 -- For a compilation unit, check for library-unit pragmas
3748 Push_Scope
(Designator
);
3749 Set_Categorization_From_Pragmas
(N
);
3750 Validate_Categorization_Dependency
(N
, Designator
);
3754 -- For a compilation unit, set body required. This flag will only be
3755 -- reset if a valid Import or Interface pragma is processed later on.
3757 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3758 Set_Body_Required
(Parent
(N
), True);
3760 if Ada_Version
>= Ada_2005
3761 and then Nkind
(Specification
(N
)) = N_Procedure_Specification
3762 and then Null_Present
(Specification
(N
))
3765 ("null procedure cannot be declared at library level", N
);
3769 Generate_Reference_To_Formals
(Designator
);
3770 Check_Eliminated
(Designator
);
3772 if Debug_Flag_C
then
3774 Write_Str
("<== subprogram spec ");
3775 Write_Name
(Chars
(Designator
));
3776 Write_Str
(" from ");
3777 Write_Location
(Sloc
(N
));
3781 if Is_Protected_Type
(Current_Scope
) then
3783 -- Indicate that this is a protected operation, because it may be
3784 -- used in subsequent declarations within the protected type.
3786 Set_Convention
(Designator
, Convention_Protected
);
3789 List_Inherited_Pre_Post_Aspects
(Designator
);
3791 if Has_Aspects
(N
) then
3792 Analyze_Aspect_Specifications
(N
, Designator
);
3794 end Analyze_Subprogram_Declaration
;
3796 --------------------------------------
3797 -- Analyze_Subprogram_Specification --
3798 --------------------------------------
3800 -- Reminder: N here really is a subprogram specification (not a subprogram
3801 -- declaration). This procedure is called to analyze the specification in
3802 -- both subprogram bodies and subprogram declarations (specs).
3804 function Analyze_Subprogram_Specification
(N
: Node_Id
) return Entity_Id
is
3805 Designator
: constant Entity_Id
:= Defining_Entity
(N
);
3806 Formals
: constant List_Id
:= Parameter_Specifications
(N
);
3808 -- Start of processing for Analyze_Subprogram_Specification
3811 -- User-defined operator is not allowed in SPARK, except as a renaming
3813 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Operator_Symbol
3814 and then Nkind
(Parent
(N
)) /= N_Subprogram_Renaming_Declaration
3816 Check_SPARK_Restriction
("user-defined operator is not allowed", N
);
3819 -- Proceed with analysis. Do not emit a cross-reference entry if the
3820 -- specification comes from an expression function, because it may be
3821 -- the completion of a previous declaration. It is is not, the cross-
3822 -- reference entry will be emitted for the new subprogram declaration.
3824 if Nkind
(Parent
(N
)) /= N_Expression_Function
then
3825 Generate_Definition
(Designator
);
3828 Set_Contract
(Designator
, Make_Contract
(Sloc
(Designator
)));
3830 if Nkind
(N
) = N_Function_Specification
then
3831 Set_Ekind
(Designator
, E_Function
);
3832 Set_Mechanism
(Designator
, Default_Mechanism
);
3834 Set_Ekind
(Designator
, E_Procedure
);
3835 Set_Etype
(Designator
, Standard_Void_Type
);
3838 -- Introduce new scope for analysis of the formals and the return type
3840 Set_Scope
(Designator
, Current_Scope
);
3842 if Present
(Formals
) then
3843 Push_Scope
(Designator
);
3844 Process_Formals
(Formals
, N
);
3846 -- Check dimensions in N for formals with default expression
3848 Analyze_Dimension_Formals
(N
, Formals
);
3850 -- Ada 2005 (AI-345): If this is an overriding operation of an
3851 -- inherited interface operation, and the controlling type is
3852 -- a synchronized type, replace the type with its corresponding
3853 -- record, to match the proper signature of an overriding operation.
3854 -- Same processing for an access parameter whose designated type is
3855 -- derived from a synchronized interface.
3857 if Ada_Version
>= Ada_2005
then
3860 Formal_Typ
: Entity_Id
;
3861 Rec_Typ
: Entity_Id
;
3862 Desig_Typ
: Entity_Id
;
3865 Formal
:= First_Formal
(Designator
);
3866 while Present
(Formal
) loop
3867 Formal_Typ
:= Etype
(Formal
);
3869 if Is_Concurrent_Type
(Formal_Typ
)
3870 and then Present
(Corresponding_Record_Type
(Formal_Typ
))
3872 Rec_Typ
:= Corresponding_Record_Type
(Formal_Typ
);
3874 if Present
(Interfaces
(Rec_Typ
)) then
3875 Set_Etype
(Formal
, Rec_Typ
);
3878 elsif Ekind
(Formal_Typ
) = E_Anonymous_Access_Type
then
3879 Desig_Typ
:= Designated_Type
(Formal_Typ
);
3881 if Is_Concurrent_Type
(Desig_Typ
)
3882 and then Present
(Corresponding_Record_Type
(Desig_Typ
))
3884 Rec_Typ
:= Corresponding_Record_Type
(Desig_Typ
);
3886 if Present
(Interfaces
(Rec_Typ
)) then
3887 Set_Directly_Designated_Type
(Formal_Typ
, Rec_Typ
);
3892 Next_Formal
(Formal
);
3899 -- The subprogram scope is pushed and popped around the processing of
3900 -- the return type for consistency with call above to Process_Formals
3901 -- (which itself can call Analyze_Return_Type), and to ensure that any
3902 -- itype created for the return type will be associated with the proper
3905 elsif Nkind
(N
) = N_Function_Specification
then
3906 Push_Scope
(Designator
);
3907 Analyze_Return_Type
(N
);
3913 if Nkind
(N
) = N_Function_Specification
then
3915 -- Deal with operator symbol case
3917 if Nkind
(Designator
) = N_Defining_Operator_Symbol
then
3918 Valid_Operator_Definition
(Designator
);
3921 May_Need_Actuals
(Designator
);
3923 -- Ada 2005 (AI-251): If the return type is abstract, verify that
3924 -- the subprogram is abstract also. This does not apply to renaming
3925 -- declarations, where abstractness is inherited, and to subprogram
3926 -- bodies generated for stream operations, which become renamings as
3929 -- In case of primitives associated with abstract interface types
3930 -- the check is applied later (see Analyze_Subprogram_Declaration).
3932 if not Nkind_In
(Original_Node
(Parent
(N
)),
3933 N_Subprogram_Renaming_Declaration
,
3934 N_Abstract_Subprogram_Declaration
,
3935 N_Formal_Abstract_Subprogram_Declaration
)
3937 if Is_Abstract_Type
(Etype
(Designator
))
3938 and then not Is_Interface
(Etype
(Designator
))
3941 ("function that returns abstract type must be abstract", N
);
3943 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
3944 -- access result whose designated type is abstract.
3946 elsif Nkind
(Result_Definition
(N
)) = N_Access_Definition
3948 not Is_Class_Wide_Type
(Designated_Type
(Etype
(Designator
)))
3949 and then Is_Abstract_Type
(Designated_Type
(Etype
(Designator
)))
3950 and then Ada_Version
>= Ada_2012
3952 Error_Msg_N
("function whose access result designates "
3953 & "abstract type must be abstract", N
);
3959 end Analyze_Subprogram_Specification
;
3961 --------------------------
3962 -- Build_Body_To_Inline --
3963 --------------------------
3965 procedure Build_Body_To_Inline
(N
: Node_Id
; Subp
: Entity_Id
) is
3966 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
3967 Original_Body
: Node_Id
;
3968 Body_To_Analyze
: Node_Id
;
3969 Max_Size
: constant := 10;
3970 Stat_Count
: Integer := 0;
3972 function Has_Excluded_Declaration
(Decls
: List_Id
) return Boolean;
3973 -- Check for declarations that make inlining not worthwhile
3975 function Has_Excluded_Statement
(Stats
: List_Id
) return Boolean;
3976 -- Check for statements that make inlining not worthwhile: any tasking
3977 -- statement, nested at any level. Keep track of total number of
3978 -- elementary statements, as a measure of acceptable size.
3980 function Has_Pending_Instantiation
return Boolean;
3981 -- If some enclosing body contains instantiations that appear before the
3982 -- corresponding generic body, the enclosing body has a freeze node so
3983 -- that it can be elaborated after the generic itself. This might
3984 -- conflict with subsequent inlinings, so that it is unsafe to try to
3985 -- inline in such a case.
3987 function Has_Single_Return
return Boolean;
3988 -- In general we cannot inline functions that return unconstrained type.
3989 -- However, we can handle such functions if all return statements return
3990 -- a local variable that is the only declaration in the body of the
3991 -- function. In that case the call can be replaced by that local
3992 -- variable as is done for other inlined calls.
3994 procedure Remove_Pragmas
;
3995 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
3996 -- parameter has no meaning when the body is inlined and the formals
3997 -- are rewritten. Remove it from body to inline. The analysis of the
3998 -- non-inlined body will handle the pragma properly.
4000 function Uses_Secondary_Stack
(Bod
: Node_Id
) return Boolean;
4001 -- If the body of the subprogram includes a call that returns an
4002 -- unconstrained type, the secondary stack is involved, and it
4003 -- is not worth inlining.
4005 ------------------------------
4006 -- Has_Excluded_Declaration --
4007 ------------------------------
4009 function Has_Excluded_Declaration
(Decls
: List_Id
) return Boolean is
4012 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean;
4013 -- Nested subprograms make a given body ineligible for inlining, but
4014 -- we make an exception for instantiations of unchecked conversion.
4015 -- The body has not been analyzed yet, so check the name, and verify
4016 -- that the visible entity with that name is the predefined unit.
4018 -----------------------------
4019 -- Is_Unchecked_Conversion --
4020 -----------------------------
4022 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean is
4023 Id
: constant Node_Id
:= Name
(D
);
4027 if Nkind
(Id
) = N_Identifier
4028 and then Chars
(Id
) = Name_Unchecked_Conversion
4030 Conv
:= Current_Entity
(Id
);
4032 elsif Nkind_In
(Id
, N_Selected_Component
, N_Expanded_Name
)
4033 and then Chars
(Selector_Name
(Id
)) = Name_Unchecked_Conversion
4035 Conv
:= Current_Entity
(Selector_Name
(Id
));
4040 return Present
(Conv
)
4041 and then Is_Predefined_File_Name
4042 (Unit_File_Name
(Get_Source_Unit
(Conv
)))
4043 and then Is_Intrinsic_Subprogram
(Conv
);
4044 end Is_Unchecked_Conversion
;
4046 -- Start of processing for Has_Excluded_Declaration
4050 while Present
(D
) loop
4051 if (Nkind
(D
) = N_Function_Instantiation
4052 and then not Is_Unchecked_Conversion
(D
))
4053 or else Nkind_In
(D
, N_Protected_Type_Declaration
,
4054 N_Package_Declaration
,
4055 N_Package_Instantiation
,
4057 N_Procedure_Instantiation
,
4058 N_Task_Type_Declaration
)
4061 ("cannot inline & (non-allowed declaration)?", D
, Subp
);
4069 end Has_Excluded_Declaration
;
4071 ----------------------------
4072 -- Has_Excluded_Statement --
4073 ----------------------------
4075 function Has_Excluded_Statement
(Stats
: List_Id
) return Boolean is
4081 while Present
(S
) loop
4082 Stat_Count
:= Stat_Count
+ 1;
4084 if Nkind_In
(S
, N_Abort_Statement
,
4085 N_Asynchronous_Select
,
4086 N_Conditional_Entry_Call
,
4087 N_Delay_Relative_Statement
,
4088 N_Delay_Until_Statement
,
4093 ("cannot inline & (non-allowed statement)?", S
, Subp
);
4096 elsif Nkind
(S
) = N_Block_Statement
then
4097 if Present
(Declarations
(S
))
4098 and then Has_Excluded_Declaration
(Declarations
(S
))
4102 elsif Present
(Handled_Statement_Sequence
(S
))
4105 (Exception_Handlers
(Handled_Statement_Sequence
(S
)))
4107 Has_Excluded_Statement
4108 (Statements
(Handled_Statement_Sequence
(S
))))
4113 elsif Nkind
(S
) = N_Case_Statement
then
4114 E
:= First
(Alternatives
(S
));
4115 while Present
(E
) loop
4116 if Has_Excluded_Statement
(Statements
(E
)) then
4123 elsif Nkind
(S
) = N_If_Statement
then
4124 if Has_Excluded_Statement
(Then_Statements
(S
)) then
4128 if Present
(Elsif_Parts
(S
)) then
4129 E
:= First
(Elsif_Parts
(S
));
4130 while Present
(E
) loop
4131 if Has_Excluded_Statement
(Then_Statements
(E
)) then
4139 if Present
(Else_Statements
(S
))
4140 and then Has_Excluded_Statement
(Else_Statements
(S
))
4145 elsif Nkind
(S
) = N_Loop_Statement
4146 and then Has_Excluded_Statement
(Statements
(S
))
4150 elsif Nkind
(S
) = N_Extended_Return_Statement
then
4151 if Has_Excluded_Statement
4152 (Statements
(Handled_Statement_Sequence
(S
)))
4154 (Exception_Handlers
(Handled_Statement_Sequence
(S
)))
4164 end Has_Excluded_Statement
;
4166 -------------------------------
4167 -- Has_Pending_Instantiation --
4168 -------------------------------
4170 function Has_Pending_Instantiation
return Boolean is
4175 while Present
(S
) loop
4176 if Is_Compilation_Unit
(S
)
4177 or else Is_Child_Unit
(S
)
4181 elsif Ekind
(S
) = E_Package
4182 and then Has_Forward_Instantiation
(S
)
4191 end Has_Pending_Instantiation
;
4193 ------------------------
4194 -- Has_Single_Return --
4195 ------------------------
4197 function Has_Single_Return
return Boolean is
4198 Return_Statement
: Node_Id
:= Empty
;
4200 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
4206 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
4208 if Nkind
(N
) = N_Simple_Return_Statement
then
4209 if Present
(Expression
(N
))
4210 and then Is_Entity_Name
(Expression
(N
))
4212 if No
(Return_Statement
) then
4213 Return_Statement
:= N
;
4216 elsif Chars
(Expression
(N
)) =
4217 Chars
(Expression
(Return_Statement
))
4225 -- A return statement within an extended return is a noop
4228 elsif No
(Expression
(N
))
4229 and then Nkind
(Parent
(Parent
(N
))) =
4230 N_Extended_Return_Statement
4235 -- Expression has wrong form
4240 -- We can only inline a build-in-place function if
4241 -- it has a single extended return.
4243 elsif Nkind
(N
) = N_Extended_Return_Statement
then
4244 if No
(Return_Statement
) then
4245 Return_Statement
:= N
;
4257 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
4259 -- Start of processing for Has_Single_Return
4262 if Check_All_Returns
(N
) /= OK
then
4265 elsif Nkind
(Return_Statement
) = N_Extended_Return_Statement
then
4269 return Present
(Declarations
(N
))
4270 and then Present
(First
(Declarations
(N
)))
4271 and then Chars
(Expression
(Return_Statement
)) =
4272 Chars
(Defining_Identifier
(First
(Declarations
(N
))));
4274 end Has_Single_Return
;
4276 --------------------
4277 -- Remove_Pragmas --
4278 --------------------
4280 procedure Remove_Pragmas
is
4285 Decl
:= First
(Declarations
(Body_To_Analyze
));
4286 while Present
(Decl
) loop
4289 if Nkind
(Decl
) = N_Pragma
4290 and then Nam_In
(Pragma_Name
(Decl
), Name_Unreferenced
,
4300 --------------------------
4301 -- Uses_Secondary_Stack --
4302 --------------------------
4304 function Uses_Secondary_Stack
(Bod
: Node_Id
) return Boolean is
4305 function Check_Call
(N
: Node_Id
) return Traverse_Result
;
4306 -- Look for function calls that return an unconstrained type
4312 function Check_Call
(N
: Node_Id
) return Traverse_Result
is
4314 if Nkind
(N
) = N_Function_Call
4315 and then Is_Entity_Name
(Name
(N
))
4316 and then Is_Composite_Type
(Etype
(Entity
(Name
(N
))))
4317 and then not Is_Constrained
(Etype
(Entity
(Name
(N
))))
4320 ("cannot inline & (call returns unconstrained type)?",
4328 function Check_Calls
is new Traverse_Func
(Check_Call
);
4331 return Check_Calls
(Bod
) = Abandon
;
4332 end Uses_Secondary_Stack
;
4334 -- Start of processing for Build_Body_To_Inline
4337 -- Return immediately if done already
4339 if Nkind
(Decl
) = N_Subprogram_Declaration
4340 and then Present
(Body_To_Inline
(Decl
))
4344 -- Functions that return unconstrained composite types require
4345 -- secondary stack handling, and cannot currently be inlined, unless
4346 -- all return statements return a local variable that is the first
4347 -- local declaration in the body.
4349 elsif Ekind
(Subp
) = E_Function
4350 and then not Is_Scalar_Type
(Etype
(Subp
))
4351 and then not Is_Access_Type
(Etype
(Subp
))
4352 and then not Is_Constrained
(Etype
(Subp
))
4354 if not Has_Single_Return
then
4356 ("cannot inline & (unconstrained return type)?", N
, Subp
);
4360 -- Ditto for functions that return controlled types, where controlled
4361 -- actions interfere in complex ways with inlining.
4363 elsif Ekind
(Subp
) = E_Function
4364 and then Needs_Finalization
(Etype
(Subp
))
4367 ("cannot inline & (controlled return type)?", N
, Subp
);
4371 if Present
(Declarations
(N
))
4372 and then Has_Excluded_Declaration
(Declarations
(N
))
4377 if Present
(Handled_Statement_Sequence
(N
)) then
4378 if Present
(Exception_Handlers
(Handled_Statement_Sequence
(N
))) then
4380 ("cannot inline& (exception handler)?",
4381 First
(Exception_Handlers
(Handled_Statement_Sequence
(N
))),
4385 Has_Excluded_Statement
4386 (Statements
(Handled_Statement_Sequence
(N
)))
4392 -- We do not inline a subprogram that is too large, unless it is
4393 -- marked Inline_Always. This pragma does not suppress the other
4394 -- checks on inlining (forbidden declarations, handlers, etc).
4396 if Stat_Count
> Max_Size
4397 and then not Has_Pragma_Inline_Always
(Subp
)
4399 Cannot_Inline
("cannot inline& (body too large)?", N
, Subp
);
4403 if Has_Pending_Instantiation
then
4405 ("cannot inline& (forward instance within enclosing body)?",
4410 -- Within an instance, the body to inline must be treated as a nested
4411 -- generic, so that the proper global references are preserved.
4413 -- Note that we do not do this at the library level, because it is not
4414 -- needed, and furthermore this causes trouble if front end inlining
4415 -- is activated (-gnatN).
4417 if In_Instance
and then Scope
(Current_Scope
) /= Standard_Standard
then
4418 Save_Env
(Scope
(Current_Scope
), Scope
(Current_Scope
));
4419 Original_Body
:= Copy_Generic_Node
(N
, Empty
, True);
4421 Original_Body
:= Copy_Separate_Tree
(N
);
4424 -- We need to capture references to the formals in order to substitute
4425 -- the actuals at the point of inlining, i.e. instantiation. To treat
4426 -- the formals as globals to the body to inline, we nest it within
4427 -- a dummy parameterless subprogram, declared within the real one.
4428 -- To avoid generating an internal name (which is never public, and
4429 -- which affects serial numbers of other generated names), we use
4430 -- an internal symbol that cannot conflict with user declarations.
4432 Set_Parameter_Specifications
(Specification
(Original_Body
), No_List
);
4433 Set_Defining_Unit_Name
4434 (Specification
(Original_Body
),
4435 Make_Defining_Identifier
(Sloc
(N
), Name_uParent
));
4436 Set_Corresponding_Spec
(Original_Body
, Empty
);
4438 Body_To_Analyze
:= Copy_Generic_Node
(Original_Body
, Empty
, False);
4440 -- Set return type of function, which is also global and does not need
4443 if Ekind
(Subp
) = E_Function
then
4444 Set_Result_Definition
(Specification
(Body_To_Analyze
),
4445 New_Occurrence_Of
(Etype
(Subp
), Sloc
(N
)));
4448 if No
(Declarations
(N
)) then
4449 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
4451 Append
(Body_To_Analyze
, Declarations
(N
));
4454 Expander_Mode_Save_And_Set
(False);
4457 Analyze
(Body_To_Analyze
);
4458 Push_Scope
(Defining_Entity
(Body_To_Analyze
));
4459 Save_Global_References
(Original_Body
);
4461 Remove
(Body_To_Analyze
);
4463 Expander_Mode_Restore
;
4465 -- Restore environment if previously saved
4467 if In_Instance
and then Scope
(Current_Scope
) /= Standard_Standard
then
4471 -- If secondary stk used there is no point in inlining. We have
4472 -- already issued the warning in this case, so nothing to do.
4474 if Uses_Secondary_Stack
(Body_To_Analyze
) then
4478 Set_Body_To_Inline
(Decl
, Original_Body
);
4479 Set_Ekind
(Defining_Entity
(Original_Body
), Ekind
(Subp
));
4480 Set_Is_Inlined
(Subp
);
4481 end Build_Body_To_Inline
;
4487 procedure Cannot_Inline
4491 Is_Serious
: Boolean := False)
4494 pragma Assert
(Msg
(Msg
'Last) = '?');
4498 if not Debug_Flag_Dot_K
then
4500 -- Do not emit warning if this is a predefined unit which is not
4501 -- the main unit. With validity checks enabled, some predefined
4502 -- subprograms may contain nested subprograms and become ineligible
4505 if Is_Predefined_File_Name
(Unit_File_Name
(Get_Source_Unit
(Subp
)))
4506 and then not In_Extended_Main_Source_Unit
(Subp
)
4510 elsif Has_Pragma_Inline_Always
(Subp
) then
4512 -- Remove last character (question mark) to make this into an
4513 -- error, because the Inline_Always pragma cannot be obeyed.
4515 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
4517 elsif Ineffective_Inline_Warnings
then
4518 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
4525 elsif Is_Serious
then
4527 -- Remove last character (question mark) to make this into an error.
4529 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
4531 elsif Optimization_Level
= 0 then
4533 -- Do not emit warning if this is a predefined unit which is not
4534 -- the main unit. This behavior is currently provided for backward
4535 -- compatibility but it will be removed when we enforce the
4536 -- strictness of the new rules.
4538 if Is_Predefined_File_Name
(Unit_File_Name
(Get_Source_Unit
(Subp
)))
4539 and then not In_Extended_Main_Source_Unit
(Subp
)
4543 elsif Has_Pragma_Inline_Always
(Subp
) then
4545 -- Emit a warning if this is a call to a runtime subprogram
4546 -- which is located inside a generic. Previously this call
4547 -- was silently skipped!
4549 if Is_Generic_Instance
(Subp
) then
4551 Gen_P
: constant Entity_Id
:= Generic_Parent
(Parent
(Subp
));
4553 if Is_Predefined_File_Name
4554 (Unit_File_Name
(Get_Source_Unit
(Gen_P
)))
4556 Set_Is_Inlined
(Subp
, False);
4557 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
4563 -- Remove last character (question mark) to make this into an
4564 -- error, because the Inline_Always pragma cannot be obeyed.
4566 Error_Msg_NE
(Msg
(Msg
'First .. Msg
'Last - 1), N
, Subp
);
4568 else pragma Assert
(Front_End_Inlining
);
4569 Set_Is_Inlined
(Subp
, False);
4571 -- When inlining cannot take place we must issue an error.
4572 -- For backward compatibility we still report a warning.
4574 if Ineffective_Inline_Warnings
then
4575 Error_Msg_NE
(Msg
& "p?", N
, Subp
);
4579 -- Compiling with optimizations enabled it is too early to report
4580 -- problems since the backend may still perform inlining. In order
4581 -- to report unhandled inlinings the program must be compiled with
4582 -- -Winline and the error is reported by the backend.
4589 ------------------------------------
4590 -- Check_And_Build_Body_To_Inline --
4591 ------------------------------------
4593 procedure Check_And_Build_Body_To_Inline
4595 Spec_Id
: Entity_Id
;
4596 Body_Id
: Entity_Id
)
4598 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
);
4599 -- Use generic machinery to build an unexpanded body for the subprogram.
4600 -- This body is subsequently used for inline expansions at call sites.
4602 function Can_Split_Unconstrained_Function
(N
: Node_Id
) return Boolean;
4603 -- Return true if we generate code for the function body N, the function
4604 -- body N has no local declarations and its unique statement is a single
4605 -- extended return statement with a handled statements sequence.
4607 function Check_Body_To_Inline
4609 Subp
: Entity_Id
) return Boolean;
4610 -- N is the N_Subprogram_Body of Subp. Return true if Subp can be
4611 -- inlined by the frontend. These are the rules:
4612 -- * At -O0 use fe inlining when inline_always is specified except if
4613 -- the function returns a controlled type.
4614 -- * At other optimization levels use the fe inlining for both inline
4615 -- and inline_always in the following cases:
4616 -- - function returning a known at compile time constant
4617 -- - function returning a call to an intrinsic function
4618 -- - function returning an unconstrained type (see Can_Split
4619 -- Unconstrained_Function).
4620 -- - function returning a call to a frontend-inlined function
4621 -- Use the back-end mechanism otherwise
4623 -- In addition, in the following cases the function cannot be inlined by
4625 -- - functions that uses the secondary stack
4626 -- - functions that have declarations of:
4627 -- - Concurrent types
4631 -- - functions that have some of the following statements:
4633 -- - asynchronous-select
4634 -- - conditional-entry-call
4637 -- - selective-accept
4638 -- - timed-entry-call
4639 -- - functions that have exception handlers
4640 -- - functions that have some enclosing body containing instantiations
4641 -- that appear before the corresponding generic body.
4643 procedure Generate_Body_To_Inline
4645 Body_To_Inline
: out Node_Id
);
4646 -- Generate a parameterless duplicate of subprogram body N. Occurrences
4647 -- of pragmas referencing the formals are removed since they have no
4648 -- meaning when the body is inlined and the formals are rewritten (the
4649 -- analysis of the non-inlined body will handle these pragmas properly).
4650 -- A new internal name is associated with Body_To_Inline.
4652 procedure Split_Unconstrained_Function
4654 Spec_Id
: Entity_Id
);
4655 -- N is an inlined function body that returns an unconstrained type and
4656 -- has a single extended return statement. Split N in two subprograms:
4657 -- a procedure P' and a function F'. The formals of P' duplicate the
4658 -- formals of N plus an extra formal which is used return a value;
4659 -- its body is composed by the declarations and list of statements
4660 -- of the extended return statement of N.
4662 --------------------------
4663 -- Build_Body_To_Inline --
4664 --------------------------
4666 procedure Build_Body_To_Inline
(N
: Node_Id
; Spec_Id
: Entity_Id
) is
4667 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
4668 Original_Body
: Node_Id
;
4669 Body_To_Analyze
: Node_Id
;
4672 pragma Assert
(Current_Scope
= Spec_Id
);
4674 -- Within an instance, the body to inline must be treated as a nested
4675 -- generic, so that the proper global references are preserved. We
4676 -- do not do this at the library level, because it is not needed, and
4677 -- furthermore this causes trouble if front end inlining is activated
4681 and then Scope
(Current_Scope
) /= Standard_Standard
4683 Save_Env
(Scope
(Current_Scope
), Scope
(Current_Scope
));
4686 -- We need to capture references to the formals in order
4687 -- to substitute the actuals at the point of inlining, i.e.
4688 -- instantiation. To treat the formals as globals to the body to
4689 -- inline, we nest it within a dummy parameterless subprogram,
4690 -- declared within the real one.
4692 Generate_Body_To_Inline
(N
, Original_Body
);
4693 Body_To_Analyze
:= Copy_Generic_Node
(Original_Body
, Empty
, False);
4695 -- Set return type of function, which is also global and does not
4696 -- need to be resolved.
4698 if Ekind
(Spec_Id
) = E_Function
then
4699 Set_Result_Definition
(Specification
(Body_To_Analyze
),
4700 New_Occurrence_Of
(Etype
(Spec_Id
), Sloc
(N
)));
4703 if No
(Declarations
(N
)) then
4704 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
4706 Append_To
(Declarations
(N
), Body_To_Analyze
);
4709 Preanalyze
(Body_To_Analyze
);
4711 Push_Scope
(Defining_Entity
(Body_To_Analyze
));
4712 Save_Global_References
(Original_Body
);
4714 Remove
(Body_To_Analyze
);
4716 -- Restore environment if previously saved
4719 and then Scope
(Current_Scope
) /= Standard_Standard
4724 pragma Assert
(No
(Body_To_Inline
(Decl
)));
4725 Set_Body_To_Inline
(Decl
, Original_Body
);
4726 Set_Ekind
(Defining_Entity
(Original_Body
), Ekind
(Spec_Id
));
4727 end Build_Body_To_Inline
;
4729 --------------------------
4730 -- Check_Body_To_Inline --
4731 --------------------------
4733 function Check_Body_To_Inline
4735 Subp
: Entity_Id
) return Boolean
4737 Max_Size
: constant := 10;
4738 Stat_Count
: Integer := 0;
4740 function Has_Excluded_Declaration
(Decls
: List_Id
) return Boolean;
4741 -- Check for declarations that make inlining not worthwhile
4743 function Has_Excluded_Statement
(Stats
: List_Id
) return Boolean;
4744 -- Check for statements that make inlining not worthwhile: any
4745 -- tasking statement, nested at any level. Keep track of total
4746 -- number of elementary statements, as a measure of acceptable size.
4748 function Has_Pending_Instantiation
return Boolean;
4749 -- Return True if some enclosing body contains instantiations that
4750 -- appear before the corresponding generic body.
4752 function Returns_Compile_Time_Constant
(N
: Node_Id
) return Boolean;
4753 -- Return True if all the return statements of the function body N
4754 -- are simple return statements and return a compile time constant
4756 function Returns_Intrinsic_Function_Call
(N
: Node_Id
) return Boolean;
4757 -- Return True if all the return statements of the function body N
4758 -- are simple return statements and return an intrinsic function call
4760 function Uses_Secondary_Stack
(N
: Node_Id
) return Boolean;
4761 -- If the body of the subprogram includes a call that returns an
4762 -- unconstrained type, the secondary stack is involved, and it
4763 -- is not worth inlining.
4765 ------------------------------
4766 -- Has_Excluded_Declaration --
4767 ------------------------------
4769 function Has_Excluded_Declaration
(Decls
: List_Id
) return Boolean is
4772 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean;
4773 -- Nested subprograms make a given body ineligible for inlining,
4774 -- but we make an exception for instantiations of unchecked
4775 -- conversion. The body has not been analyzed yet, so check the
4776 -- name, and verify that the visible entity with that name is the
4779 -----------------------------
4780 -- Is_Unchecked_Conversion --
4781 -----------------------------
4783 function Is_Unchecked_Conversion
(D
: Node_Id
) return Boolean is
4784 Id
: constant Node_Id
:= Name
(D
);
4788 if Nkind
(Id
) = N_Identifier
4789 and then Chars
(Id
) = Name_Unchecked_Conversion
4791 Conv
:= Current_Entity
(Id
);
4793 elsif Nkind_In
(Id
, N_Selected_Component
, N_Expanded_Name
)
4795 Chars
(Selector_Name
(Id
)) = Name_Unchecked_Conversion
4797 Conv
:= Current_Entity
(Selector_Name
(Id
));
4802 return Present
(Conv
)
4803 and then Is_Predefined_File_Name
4804 (Unit_File_Name
(Get_Source_Unit
(Conv
)))
4805 and then Is_Intrinsic_Subprogram
(Conv
);
4806 end Is_Unchecked_Conversion
;
4808 -- Start of processing for Has_Excluded_Declaration
4812 while Present
(D
) loop
4813 if (Nkind
(D
) = N_Function_Instantiation
4814 and then not Is_Unchecked_Conversion
(D
))
4815 or else Nkind_In
(D
, N_Protected_Type_Declaration
,
4816 N_Package_Declaration
,
4817 N_Package_Instantiation
,
4819 N_Procedure_Instantiation
,
4820 N_Task_Type_Declaration
)
4823 ("cannot inline & (non-allowed declaration)?", D
, Subp
);
4832 end Has_Excluded_Declaration
;
4834 ----------------------------
4835 -- Has_Excluded_Statement --
4836 ----------------------------
4838 function Has_Excluded_Statement
(Stats
: List_Id
) return Boolean is
4844 while Present
(S
) loop
4845 Stat_Count
:= Stat_Count
+ 1;
4847 if Nkind_In
(S
, N_Abort_Statement
,
4848 N_Asynchronous_Select
,
4849 N_Conditional_Entry_Call
,
4850 N_Delay_Relative_Statement
,
4851 N_Delay_Until_Statement
,
4856 ("cannot inline & (non-allowed statement)?", S
, Subp
);
4859 elsif Nkind
(S
) = N_Block_Statement
then
4860 if Present
(Declarations
(S
))
4861 and then Has_Excluded_Declaration
(Declarations
(S
))
4865 elsif Present
(Handled_Statement_Sequence
(S
)) then
4867 (Exception_Handlers
(Handled_Statement_Sequence
(S
)))
4870 ("cannot inline& (exception handler)?",
4871 First
(Exception_Handlers
4872 (Handled_Statement_Sequence
(S
))),
4876 elsif Has_Excluded_Statement
4877 (Statements
(Handled_Statement_Sequence
(S
)))
4883 elsif Nkind
(S
) = N_Case_Statement
then
4884 E
:= First
(Alternatives
(S
));
4885 while Present
(E
) loop
4886 if Has_Excluded_Statement
(Statements
(E
)) then
4893 elsif Nkind
(S
) = N_If_Statement
then
4894 if Has_Excluded_Statement
(Then_Statements
(S
)) then
4898 if Present
(Elsif_Parts
(S
)) then
4899 E
:= First
(Elsif_Parts
(S
));
4900 while Present
(E
) loop
4901 if Has_Excluded_Statement
(Then_Statements
(E
)) then
4908 if Present
(Else_Statements
(S
))
4909 and then Has_Excluded_Statement
(Else_Statements
(S
))
4914 elsif Nkind
(S
) = N_Loop_Statement
4915 and then Has_Excluded_Statement
(Statements
(S
))
4919 elsif Nkind
(S
) = N_Extended_Return_Statement
then
4920 if Present
(Handled_Statement_Sequence
(S
))
4922 Has_Excluded_Statement
4923 (Statements
(Handled_Statement_Sequence
(S
)))
4927 elsif Present
(Handled_Statement_Sequence
(S
))
4929 Present
(Exception_Handlers
4930 (Handled_Statement_Sequence
(S
)))
4933 ("cannot inline& (exception handler)?",
4934 First
(Exception_Handlers
4935 (Handled_Statement_Sequence
(S
))),
4945 end Has_Excluded_Statement
;
4947 -------------------------------
4948 -- Has_Pending_Instantiation --
4949 -------------------------------
4951 function Has_Pending_Instantiation
return Boolean is
4956 while Present
(S
) loop
4957 if Is_Compilation_Unit
(S
)
4958 or else Is_Child_Unit
(S
)
4962 elsif Ekind
(S
) = E_Package
4963 and then Has_Forward_Instantiation
(S
)
4972 end Has_Pending_Instantiation
;
4974 ------------------------------------
4975 -- Returns_Compile_Time_Constant --
4976 ------------------------------------
4978 function Returns_Compile_Time_Constant
(N
: Node_Id
) return Boolean is
4980 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
4986 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
4988 if Nkind
(N
) = N_Extended_Return_Statement
then
4991 elsif Nkind
(N
) = N_Simple_Return_Statement
then
4992 if Present
(Expression
(N
)) then
4994 Orig_Expr
: constant Node_Id
:=
4995 Original_Node
(Expression
(N
));
4998 if Nkind_In
(Orig_Expr
, N_Integer_Literal
,
5000 N_Character_Literal
)
5004 elsif Is_Entity_Name
(Orig_Expr
)
5005 and then Ekind
(Entity
(Orig_Expr
)) = E_Constant
5006 and then Is_Static_Expression
(Orig_Expr
)
5014 -- Expression has wrong form
5020 -- Continue analyzing statements
5027 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
5029 -- Start of processing for Returns_Compile_Time_Constant
5032 return Check_All_Returns
(N
) = OK
;
5033 end Returns_Compile_Time_Constant
;
5035 --------------------------------------
5036 -- Returns_Intrinsic_Function_Call --
5037 --------------------------------------
5039 function Returns_Intrinsic_Function_Call
5040 (N
: Node_Id
) return Boolean
5042 function Check_Return
(N
: Node_Id
) return Traverse_Result
;
5048 function Check_Return
(N
: Node_Id
) return Traverse_Result
is
5050 if Nkind
(N
) = N_Extended_Return_Statement
then
5053 elsif Nkind
(N
) = N_Simple_Return_Statement
then
5054 if Present
(Expression
(N
)) then
5056 Orig_Expr
: constant Node_Id
:=
5057 Original_Node
(Expression
(N
));
5060 if Nkind
(Orig_Expr
) in N_Op
5061 and then Is_Intrinsic_Subprogram
(Entity
(Orig_Expr
))
5065 elsif Nkind
(Orig_Expr
) in N_Has_Entity
5066 and then Present
(Entity
(Orig_Expr
))
5067 and then Ekind
(Entity
(Orig_Expr
)) = E_Function
5068 and then Is_Inlined
(Entity
(Orig_Expr
))
5072 elsif Nkind
(Orig_Expr
) in N_Has_Entity
5073 and then Present
(Entity
(Orig_Expr
))
5074 and then Is_Intrinsic_Subprogram
(Entity
(Orig_Expr
))
5083 -- Expression has wrong form
5089 -- Continue analyzing statements
5096 function Check_All_Returns
is new Traverse_Func
(Check_Return
);
5098 -- Start of processing for Returns_Intrinsic_Function_Call
5101 return Check_All_Returns
(N
) = OK
;
5102 end Returns_Intrinsic_Function_Call
;
5104 --------------------------
5105 -- Uses_Secondary_Stack --
5106 --------------------------
5108 function Uses_Secondary_Stack
(N
: Node_Id
) return Boolean is
5110 function Check_Call
(N
: Node_Id
) return Traverse_Result
;
5111 -- Look for function calls that return an unconstrained type
5117 function Check_Call
(N
: Node_Id
) return Traverse_Result
is
5119 if Nkind
(N
) = N_Function_Call
5120 and then Is_Entity_Name
(Name
(N
))
5121 and then Is_Composite_Type
(Etype
(Entity
(Name
(N
))))
5122 and then not Is_Constrained
(Etype
(Entity
(Name
(N
))))
5125 ("cannot inline & (call returns unconstrained type)?",
5134 function Check_Calls
is new Traverse_Func
(Check_Call
);
5136 -- Start of processing for Uses_Secondary_Stack
5139 return Check_Calls
(N
) = Abandon
;
5140 end Uses_Secondary_Stack
;
5144 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Spec_Id
);
5145 May_Inline
: constant Boolean :=
5146 Has_Pragma_Inline_Always
(Spec_Id
)
5147 or else (Has_Pragma_Inline
(Spec_Id
)
5148 and then ((Optimization_Level
> 0
5149 and then Ekind
(Spec_Id
)
5151 or else Front_End_Inlining
));
5152 Body_To_Analyze
: Node_Id
;
5154 -- Start of processing for Check_Body_To_Inline
5157 -- No action needed in stubs since the attribute Body_To_Inline
5160 if Nkind
(Decl
) = N_Subprogram_Body_Stub
then
5163 -- Cannot build the body to inline if the attribute is already set.
5164 -- This attribute may have been set if this is a subprogram renaming
5165 -- declarations (see Freeze.Build_Renamed_Body).
5167 elsif Present
(Body_To_Inline
(Decl
)) then
5170 -- No action needed if the subprogram does not fulfill the minimum
5171 -- conditions to be inlined by the frontend
5173 elsif not May_Inline
then
5177 -- Check excluded declarations
5179 if Present
(Declarations
(N
))
5180 and then Has_Excluded_Declaration
(Declarations
(N
))
5185 -- Check excluded statements
5187 if Present
(Handled_Statement_Sequence
(N
)) then
5189 (Exception_Handlers
(Handled_Statement_Sequence
(N
)))
5192 ("cannot inline& (exception handler)?",
5194 (Exception_Handlers
(Handled_Statement_Sequence
(N
))),
5199 elsif Has_Excluded_Statement
5200 (Statements
(Handled_Statement_Sequence
(N
)))
5206 -- For backward compatibility, compiling under -gnatN we do not
5207 -- inline a subprogram that is too large, unless it is marked
5208 -- Inline_Always. This pragma does not suppress the other checks
5209 -- on inlining (forbidden declarations, handlers, etc).
5211 if Front_End_Inlining
5212 and then not Has_Pragma_Inline_Always
(Subp
)
5213 and then Stat_Count
> Max_Size
5215 Cannot_Inline
("cannot inline& (body too large)?", N
, Subp
);
5219 -- If some enclosing body contains instantiations that appear before
5220 -- the corresponding generic body, the enclosing body has a freeze
5221 -- node so that it can be elaborated after the generic itself. This
5222 -- might conflict with subsequent inlinings, so that it is unsafe to
5223 -- try to inline in such a case.
5225 if Has_Pending_Instantiation
then
5227 ("cannot inline& (forward instance within enclosing body)?",
5233 -- Generate and preanalyze the body to inline (needed to perform
5234 -- the rest of the checks)
5236 Generate_Body_To_Inline
(N
, Body_To_Analyze
);
5238 if Ekind
(Subp
) = E_Function
then
5239 Set_Result_Definition
(Specification
(Body_To_Analyze
),
5240 New_Occurrence_Of
(Etype
(Subp
), Sloc
(N
)));
5243 -- Nest the body to analyze within the real one
5245 if No
(Declarations
(N
)) then
5246 Set_Declarations
(N
, New_List
(Body_To_Analyze
));
5248 Append_To
(Declarations
(N
), Body_To_Analyze
);
5251 Preanalyze
(Body_To_Analyze
);
5252 Remove
(Body_To_Analyze
);
5254 -- Keep separate checks needed when compiling without optimizations
5256 if Optimization_Level
= 0
5258 -- AAMP and VM targets have no support for inlining in the backend
5259 -- and hence we use frontend inlining at all optimization levels.
5261 or else AAMP_On_Target
5262 or else VM_Target
/= No_VM
5264 -- Cannot inline functions whose body has a call that returns an
5265 -- unconstrained type since the secondary stack is involved, and
5266 -- it is not worth inlining.
5268 if Uses_Secondary_Stack
(Body_To_Analyze
) then
5271 -- Cannot inline functions that return controlled types since
5272 -- controlled actions interfere in complex ways with inlining.
5274 elsif Ekind
(Subp
) = E_Function
5275 and then Needs_Finalization
(Etype
(Subp
))
5278 ("cannot inline & (controlled return type)?", N
, Subp
);
5281 elsif Returns_Unconstrained_Type
(Subp
) then
5283 ("cannot inline & (unconstrained return type)?", N
, Subp
);
5287 -- Compiling with optimizations enabled
5290 -- Procedures are never frontend inlined in this case!
5292 if Ekind
(Subp
) /= E_Function
then
5295 -- Functions returning unconstrained types are tested
5296 -- separately (see Can_Split_Unconstrained_Function).
5298 elsif Returns_Unconstrained_Type
(Subp
) then
5301 -- Check supported cases
5303 elsif not Returns_Compile_Time_Constant
(Body_To_Analyze
)
5304 and then Convention
(Subp
) /= Convention_Intrinsic
5305 and then not Returns_Intrinsic_Function_Call
(Body_To_Analyze
)
5312 end Check_Body_To_Inline
;
5314 --------------------------------------
5315 -- Can_Split_Unconstrained_Function --
5316 --------------------------------------
5318 function Can_Split_Unconstrained_Function
(N
: Node_Id
) return Boolean
5320 Ret_Node
: constant Node_Id
:=
5321 First
(Statements
(Handled_Statement_Sequence
(N
)));
5325 -- No user defined declarations allowed in the function except inside
5326 -- the unique return statement; implicit labels are the only allowed
5329 if not Is_Empty_List
(Declarations
(N
)) then
5330 D
:= First
(Declarations
(N
));
5331 while Present
(D
) loop
5332 if Nkind
(D
) /= N_Implicit_Label_Declaration
then
5340 -- We only split the inlined function when we are generating the code
5341 -- of its body; otherwise we leave duplicated split subprograms in
5342 -- the tree which (if referenced) generate wrong references at link
5345 return In_Extended_Main_Code_Unit
(N
)
5346 and then Present
(Ret_Node
)
5347 and then Nkind
(Ret_Node
) = N_Extended_Return_Statement
5348 and then No
(Next
(Ret_Node
))
5349 and then Present
(Handled_Statement_Sequence
(Ret_Node
));
5350 end Can_Split_Unconstrained_Function
;
5352 -----------------------------
5353 -- Generate_Body_To_Inline --
5354 -----------------------------
5356 procedure Generate_Body_To_Inline
5358 Body_To_Inline
: out Node_Id
)
5360 procedure Remove_Pragmas
(N
: Node_Id
);
5361 -- Remove occurrences of pragmas that may reference the formals of
5362 -- N. The analysis of the non-inlined body will handle these pragmas
5365 --------------------
5366 -- Remove_Pragmas --
5367 --------------------
5369 procedure Remove_Pragmas
(N
: Node_Id
) is
5374 Decl
:= First
(Declarations
(N
));
5375 while Present
(Decl
) loop
5378 if Nkind
(Decl
) = N_Pragma
5379 and then Nam_In
(Pragma_Name
(Decl
), Name_Unreferenced
,
5389 -- Start of processing for Generate_Body_To_Inline
5392 -- Within an instance, the body to inline must be treated as a nested
5393 -- generic, so that the proper global references are preserved.
5395 -- Note that we do not do this at the library level, because it
5396 -- is not needed, and furthermore this causes trouble if front
5397 -- end inlining is activated (-gnatN).
5400 and then Scope
(Current_Scope
) /= Standard_Standard
5402 Body_To_Inline
:= Copy_Generic_Node
(N
, Empty
, True);
5404 Body_To_Inline
:= Copy_Separate_Tree
(N
);
5407 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
5408 -- parameter has no meaning when the body is inlined and the formals
5409 -- are rewritten. Remove it from body to inline. The analysis of the
5410 -- non-inlined body will handle the pragma properly.
5412 Remove_Pragmas
(Body_To_Inline
);
5414 -- We need to capture references to the formals in order
5415 -- to substitute the actuals at the point of inlining, i.e.
5416 -- instantiation. To treat the formals as globals to the body to
5417 -- inline, we nest it within a dummy parameterless subprogram,
5418 -- declared within the real one.
5420 Set_Parameter_Specifications
5421 (Specification
(Body_To_Inline
), No_List
);
5423 -- A new internal name is associated with Body_To_Inline to avoid
5424 -- conflicts when the non-inlined body N is analyzed.
5426 Set_Defining_Unit_Name
(Specification
(Body_To_Inline
),
5427 Make_Defining_Identifier
(Sloc
(N
), New_Internal_Name
('P')));
5428 Set_Corresponding_Spec
(Body_To_Inline
, Empty
);
5429 end Generate_Body_To_Inline
;
5431 ----------------------------------
5432 -- Split_Unconstrained_Function --
5433 ----------------------------------
5435 procedure Split_Unconstrained_Function
5437 Spec_Id
: Entity_Id
)
5439 Loc
: constant Source_Ptr
:= Sloc
(N
);
5440 Ret_Node
: constant Node_Id
:=
5441 First
(Statements
(Handled_Statement_Sequence
(N
)));
5442 Ret_Obj
: constant Node_Id
:=
5443 First
(Return_Object_Declarations
(Ret_Node
));
5445 procedure Build_Procedure
5446 (Proc_Id
: out Entity_Id
;
5447 Decl_List
: out List_Id
);
5448 -- Build a procedure containing the statements found in the extended
5449 -- return statement of the unconstrained function body N.
5451 procedure Build_Procedure
5452 (Proc_Id
: out Entity_Id
;
5453 Decl_List
: out List_Id
)
5456 Formal_List
: constant List_Id
:= New_List
;
5457 Proc_Spec
: Node_Id
;
5458 Proc_Body
: Node_Id
;
5459 Subp_Name
: constant Name_Id
:= New_Internal_Name
('F');
5460 Body_Decl_List
: List_Id
:= No_List
;
5461 Param_Type
: Node_Id
;
5464 if Nkind
(Object_Definition
(Ret_Obj
)) = N_Identifier
then
5465 Param_Type
:= New_Copy
(Object_Definition
(Ret_Obj
));
5468 New_Copy
(Subtype_Mark
(Object_Definition
(Ret_Obj
)));
5471 Append_To
(Formal_List
,
5472 Make_Parameter_Specification
(Loc
,
5473 Defining_Identifier
=>
5474 Make_Defining_Identifier
(Loc
,
5475 Chars
=> Chars
(Defining_Identifier
(Ret_Obj
))),
5476 In_Present
=> False,
5477 Out_Present
=> True,
5478 Null_Exclusion_Present
=> False,
5479 Parameter_Type
=> Param_Type
));
5481 Formal
:= First_Formal
(Spec_Id
);
5482 while Present
(Formal
) loop
5483 Append_To
(Formal_List
,
5484 Make_Parameter_Specification
(Loc
,
5485 Defining_Identifier
=>
5486 Make_Defining_Identifier
(Sloc
(Formal
),
5487 Chars
=> Chars
(Formal
)),
5488 In_Present
=> In_Present
(Parent
(Formal
)),
5489 Out_Present
=> Out_Present
(Parent
(Formal
)),
5490 Null_Exclusion_Present
=>
5491 Null_Exclusion_Present
(Parent
(Formal
)),
5493 New_Reference_To
(Etype
(Formal
), Loc
),
5495 Copy_Separate_Tree
(Expression
(Parent
(Formal
)))));
5497 Next_Formal
(Formal
);
5501 Make_Defining_Identifier
(Loc
, Chars
=> Subp_Name
);
5504 Make_Procedure_Specification
(Loc
,
5505 Defining_Unit_Name
=> Proc_Id
,
5506 Parameter_Specifications
=> Formal_List
);
5508 Decl_List
:= New_List
;
5510 Append_To
(Decl_List
,
5511 Make_Subprogram_Declaration
(Loc
, Proc_Spec
));
5513 -- Can_Convert_Unconstrained_Function checked that the function
5514 -- has no local declarations except implicit label declarations.
5515 -- Copy these declarations to the built procedure.
5517 if Present
(Declarations
(N
)) then
5518 Body_Decl_List
:= New_List
;
5525 D
:= First
(Declarations
(N
));
5526 while Present
(D
) loop
5527 pragma Assert
(Nkind
(D
) = N_Implicit_Label_Declaration
);
5530 Make_Implicit_Label_Declaration
(Loc
,
5531 Make_Defining_Identifier
(Loc
,
5532 Chars
=> Chars
(Defining_Identifier
(D
))),
5533 Label_Construct
=> Empty
);
5534 Append_To
(Body_Decl_List
, New_D
);
5541 pragma Assert
(Present
(Handled_Statement_Sequence
(Ret_Node
)));
5544 Make_Subprogram_Body
(Loc
,
5545 Specification
=> Copy_Separate_Tree
(Proc_Spec
),
5546 Declarations
=> Body_Decl_List
,
5547 Handled_Statement_Sequence
=>
5548 Copy_Separate_Tree
(Handled_Statement_Sequence
(Ret_Node
)));
5550 Set_Defining_Unit_Name
(Specification
(Proc_Body
),
5551 Make_Defining_Identifier
(Loc
, Subp_Name
));
5553 Append_To
(Decl_List
, Proc_Body
);
5554 end Build_Procedure
;
5558 New_Obj
: constant Node_Id
:= Copy_Separate_Tree
(Ret_Obj
);
5560 Proc_Id
: Entity_Id
;
5561 Proc_Call
: Node_Id
;
5563 -- Start of processing for Split_Unconstrained_Function
5566 -- Build the associated procedure, analyze it and insert it before
5567 -- the function body N
5570 Scope
: constant Entity_Id
:= Current_Scope
;
5571 Decl_List
: List_Id
;
5574 Build_Procedure
(Proc_Id
, Decl_List
);
5575 Insert_Actions
(N
, Decl_List
);
5579 -- Build the call to the generated procedure
5582 Actual_List
: constant List_Id
:= New_List
;
5586 Append_To
(Actual_List
,
5587 New_Reference_To
(Defining_Identifier
(New_Obj
), Loc
));
5589 Formal
:= First_Formal
(Spec_Id
);
5590 while Present
(Formal
) loop
5591 Append_To
(Actual_List
, New_Reference_To
(Formal
, Loc
));
5593 -- Avoid spurious warning on unreferenced formals
5595 Set_Referenced
(Formal
);
5596 Next_Formal
(Formal
);
5600 Make_Procedure_Call_Statement
(Loc
,
5601 Name
=> New_Reference_To
(Proc_Id
, Loc
),
5602 Parameter_Associations
=> Actual_List
);
5610 -- main_1__F1b (New_Obj, ...);
5615 Make_Block_Statement
(Loc
,
5616 Declarations
=> New_List
(New_Obj
),
5617 Handled_Statement_Sequence
=>
5618 Make_Handled_Sequence_Of_Statements
(Loc
,
5619 Statements
=> New_List
(
5623 Make_Simple_Return_Statement
(Loc
,
5626 (Defining_Identifier
(New_Obj
), Loc
)))));
5628 Rewrite
(Ret_Node
, Blk_Stmt
);
5629 end Split_Unconstrained_Function
;
5631 -- Start of processing for Check_And_Build_Body_To_Inline
5634 -- Do not inline any subprogram that contains nested subprograms, since
5635 -- the backend inlining circuit seems to generate uninitialized
5636 -- references in this case. We know this happens in the case of front
5637 -- end ZCX support, but it also appears it can happen in other cases as
5638 -- well. The backend often rejects attempts to inline in the case of
5639 -- nested procedures anyway, so little if anything is lost by this.
5640 -- Note that this is test is for the benefit of the back-end. There is
5641 -- a separate test for front-end inlining that also rejects nested
5644 -- Do not do this test if errors have been detected, because in some
5645 -- error cases, this code blows up, and we don't need it anyway if
5646 -- there have been errors, since we won't get to the linker anyway.
5648 if Comes_From_Source
(Body_Id
)
5649 and then (Has_Pragma_Inline_Always
(Spec_Id
)
5650 or else Optimization_Level
> 0)
5651 and then Serious_Errors_Detected
= 0
5659 P_Ent
:= Scope
(P_Ent
);
5660 exit when No
(P_Ent
) or else P_Ent
= Standard_Standard
;
5662 if Is_Subprogram
(P_Ent
) then
5663 Set_Is_Inlined
(P_Ent
, False);
5665 if Comes_From_Source
(P_Ent
)
5666 and then Has_Pragma_Inline
(P_Ent
)
5669 ("cannot inline& (nested subprogram)?", N
, P_Ent
,
5670 Is_Serious
=> True);
5677 -- Build the body to inline only if really needed!
5679 if Check_Body_To_Inline
(N
, Spec_Id
)
5680 and then Serious_Errors_Detected
= 0
5682 if Returns_Unconstrained_Type
(Spec_Id
) then
5683 if Can_Split_Unconstrained_Function
(N
) then
5684 Split_Unconstrained_Function
(N
, Spec_Id
);
5685 Build_Body_To_Inline
(N
, Spec_Id
);
5686 Set_Is_Inlined
(Spec_Id
);
5689 Build_Body_To_Inline
(N
, Spec_Id
);
5690 Set_Is_Inlined
(Spec_Id
);
5693 end Check_And_Build_Body_To_Inline
;
5695 -----------------------
5696 -- Check_Conformance --
5697 -----------------------
5699 procedure Check_Conformance
5700 (New_Id
: Entity_Id
;
5702 Ctype
: Conformance_Type
;
5704 Conforms
: out Boolean;
5705 Err_Loc
: Node_Id
:= Empty
;
5706 Get_Inst
: Boolean := False;
5707 Skip_Controlling_Formals
: Boolean := False)
5709 procedure Conformance_Error
(Msg
: String; N
: Node_Id
:= New_Id
);
5710 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5711 -- If Errmsg is True, then processing continues to post an error message
5712 -- for conformance error on given node. Two messages are output. The
5713 -- first message points to the previous declaration with a general "no
5714 -- conformance" message. The second is the detailed reason, supplied as
5715 -- Msg. The parameter N provide information for a possible & insertion
5716 -- in the message, and also provides the location for posting the
5717 -- message in the absence of a specified Err_Loc location.
5719 -----------------------
5720 -- Conformance_Error --
5721 -----------------------
5723 procedure Conformance_Error
(Msg
: String; N
: Node_Id
:= New_Id
) is
5730 if No
(Err_Loc
) then
5736 Error_Msg_Sloc
:= Sloc
(Old_Id
);
5739 when Type_Conformant
=>
5740 Error_Msg_N
-- CODEFIX
5741 ("not type conformant with declaration#!", Enode
);
5743 when Mode_Conformant
=>
5744 if Nkind
(Parent
(Old_Id
)) = N_Full_Type_Declaration
then
5746 ("not mode conformant with operation inherited#!",
5750 ("not mode conformant with declaration#!", Enode
);
5753 when Subtype_Conformant
=>
5754 if Nkind
(Parent
(Old_Id
)) = N_Full_Type_Declaration
then
5756 ("not subtype conformant with operation inherited#!",
5760 ("not subtype conformant with declaration#!", Enode
);
5763 when Fully_Conformant
=>
5764 if Nkind
(Parent
(Old_Id
)) = N_Full_Type_Declaration
then
5765 Error_Msg_N
-- CODEFIX
5766 ("not fully conformant with operation inherited#!",
5769 Error_Msg_N
-- CODEFIX
5770 ("not fully conformant with declaration#!", Enode
);
5774 Error_Msg_NE
(Msg
, Enode
, N
);
5776 end Conformance_Error
;
5780 Old_Type
: constant Entity_Id
:= Etype
(Old_Id
);
5781 New_Type
: constant Entity_Id
:= Etype
(New_Id
);
5782 Old_Formal
: Entity_Id
;
5783 New_Formal
: Entity_Id
;
5784 Access_Types_Match
: Boolean;
5785 Old_Formal_Base
: Entity_Id
;
5786 New_Formal_Base
: Entity_Id
;
5788 -- Start of processing for Check_Conformance
5793 -- We need a special case for operators, since they don't appear
5796 if Ctype
= Type_Conformant
then
5797 if Ekind
(New_Id
) = E_Operator
5798 and then Operator_Matches_Spec
(New_Id
, Old_Id
)
5804 -- If both are functions/operators, check return types conform
5806 if Old_Type
/= Standard_Void_Type
5807 and then New_Type
/= Standard_Void_Type
5810 -- If we are checking interface conformance we omit controlling
5811 -- arguments and result, because we are only checking the conformance
5812 -- of the remaining parameters.
5814 if Has_Controlling_Result
(Old_Id
)
5815 and then Has_Controlling_Result
(New_Id
)
5816 and then Skip_Controlling_Formals
5820 elsif not Conforming_Types
(Old_Type
, New_Type
, Ctype
, Get_Inst
) then
5821 Conformance_Error
("\return type does not match!", New_Id
);
5825 -- Ada 2005 (AI-231): In case of anonymous access types check the
5826 -- null-exclusion and access-to-constant attributes match.
5828 if Ada_Version
>= Ada_2005
5829 and then Ekind
(Etype
(Old_Type
)) = E_Anonymous_Access_Type
5831 (Can_Never_Be_Null
(Old_Type
) /= Can_Never_Be_Null
(New_Type
)
5832 or else Is_Access_Constant
(Etype
(Old_Type
)) /=
5833 Is_Access_Constant
(Etype
(New_Type
)))
5835 Conformance_Error
("\return type does not match!", New_Id
);
5839 -- If either is a function/operator and the other isn't, error
5841 elsif Old_Type
/= Standard_Void_Type
5842 or else New_Type
/= Standard_Void_Type
5844 Conformance_Error
("\functions can only match functions!", New_Id
);
5848 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5849 -- If this is a renaming as body, refine error message to indicate that
5850 -- the conflict is with the original declaration. If the entity is not
5851 -- frozen, the conventions don't have to match, the one of the renamed
5852 -- entity is inherited.
5854 if Ctype
>= Subtype_Conformant
then
5855 if Convention
(Old_Id
) /= Convention
(New_Id
) then
5856 if not Is_Frozen
(New_Id
) then
5859 elsif Present
(Err_Loc
)
5860 and then Nkind
(Err_Loc
) = N_Subprogram_Renaming_Declaration
5861 and then Present
(Corresponding_Spec
(Err_Loc
))
5863 Error_Msg_Name_1
:= Chars
(New_Id
);
5865 Name_Ada
+ Convention_Id
'Pos (Convention
(New_Id
));
5866 Conformance_Error
("\prior declaration for% has convention %!");
5869 Conformance_Error
("\calling conventions do not match!");
5874 elsif Is_Formal_Subprogram
(Old_Id
)
5875 or else Is_Formal_Subprogram
(New_Id
)
5877 Conformance_Error
("\formal subprograms not allowed!");
5882 -- Deal with parameters
5884 -- Note: we use the entity information, rather than going directly
5885 -- to the specification in the tree. This is not only simpler, but
5886 -- absolutely necessary for some cases of conformance tests between
5887 -- operators, where the declaration tree simply does not exist!
5889 Old_Formal
:= First_Formal
(Old_Id
);
5890 New_Formal
:= First_Formal
(New_Id
);
5891 while Present
(Old_Formal
) and then Present
(New_Formal
) loop
5892 if Is_Controlling_Formal
(Old_Formal
)
5893 and then Is_Controlling_Formal
(New_Formal
)
5894 and then Skip_Controlling_Formals
5896 -- The controlling formals will have different types when
5897 -- comparing an interface operation with its match, but both
5898 -- or neither must be access parameters.
5900 if Is_Access_Type
(Etype
(Old_Formal
))
5902 Is_Access_Type
(Etype
(New_Formal
))
5904 goto Skip_Controlling_Formal
;
5907 ("\access parameter does not match!", New_Formal
);
5911 -- Ada 2012: Mode conformance also requires that formal parameters
5912 -- be both aliased, or neither.
5914 if Ctype
>= Mode_Conformant
and then Ada_Version
>= Ada_2012
then
5915 if Is_Aliased
(Old_Formal
) /= Is_Aliased
(New_Formal
) then
5917 ("\aliased parameter mismatch!", New_Formal
);
5921 if Ctype
= Fully_Conformant
then
5923 -- Names must match. Error message is more accurate if we do
5924 -- this before checking that the types of the formals match.
5926 if Chars
(Old_Formal
) /= Chars
(New_Formal
) then
5927 Conformance_Error
("\name & does not match!", New_Formal
);
5929 -- Set error posted flag on new formal as well to stop
5930 -- junk cascaded messages in some cases.
5932 Set_Error_Posted
(New_Formal
);
5936 -- Null exclusion must match
5938 if Null_Exclusion_Present
(Parent
(Old_Formal
))
5940 Null_Exclusion_Present
(Parent
(New_Formal
))
5942 -- Only give error if both come from source. This should be
5943 -- investigated some time, since it should not be needed ???
5945 if Comes_From_Source
(Old_Formal
)
5947 Comes_From_Source
(New_Formal
)
5950 ("\null exclusion for & does not match", New_Formal
);
5952 -- Mark error posted on the new formal to avoid duplicated
5953 -- complaint about types not matching.
5955 Set_Error_Posted
(New_Formal
);
5960 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5961 -- case occurs whenever a subprogram is being renamed and one of its
5962 -- parameters imposes a null exclusion. For example:
5964 -- type T is null record;
5965 -- type Acc_T is access T;
5966 -- subtype Acc_T_Sub is Acc_T;
5968 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5969 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5972 Old_Formal_Base
:= Etype
(Old_Formal
);
5973 New_Formal_Base
:= Etype
(New_Formal
);
5976 Old_Formal_Base
:= Get_Instance_Of
(Old_Formal_Base
);
5977 New_Formal_Base
:= Get_Instance_Of
(New_Formal_Base
);
5980 Access_Types_Match
:= Ada_Version
>= Ada_2005
5982 -- Ensure that this rule is only applied when New_Id is a
5983 -- renaming of Old_Id.
5985 and then Nkind
(Parent
(Parent
(New_Id
))) =
5986 N_Subprogram_Renaming_Declaration
5987 and then Nkind
(Name
(Parent
(Parent
(New_Id
)))) in N_Has_Entity
5988 and then Present
(Entity
(Name
(Parent
(Parent
(New_Id
)))))
5989 and then Entity
(Name
(Parent
(Parent
(New_Id
)))) = Old_Id
5991 -- Now handle the allowed access-type case
5993 and then Is_Access_Type
(Old_Formal_Base
)
5994 and then Is_Access_Type
(New_Formal_Base
)
5996 -- The type kinds must match. The only exception occurs with
5997 -- multiple generics of the form:
6000 -- type F is private; type A is private;
6001 -- type F_Ptr is access F; type A_Ptr is access A;
6002 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
6003 -- package F_Pack is ... package A_Pack is
6004 -- package F_Inst is
6005 -- new F_Pack (A, A_Ptr, A_P);
6007 -- When checking for conformance between the parameters of A_P
6008 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
6009 -- because the compiler has transformed A_Ptr into a subtype of
6010 -- F_Ptr. We catch this case in the code below.
6012 and then (Ekind
(Old_Formal_Base
) = Ekind
(New_Formal_Base
)
6014 (Is_Generic_Type
(Old_Formal_Base
)
6015 and then Is_Generic_Type
(New_Formal_Base
)
6016 and then Is_Internal
(New_Formal_Base
)
6017 and then Etype
(Etype
(New_Formal_Base
)) =
6019 and then Directly_Designated_Type
(Old_Formal_Base
) =
6020 Directly_Designated_Type
(New_Formal_Base
)
6021 and then ((Is_Itype
(Old_Formal_Base
)
6022 and then Can_Never_Be_Null
(Old_Formal_Base
))
6024 (Is_Itype
(New_Formal_Base
)
6025 and then Can_Never_Be_Null
(New_Formal_Base
)));
6027 -- Types must always match. In the visible part of an instance,
6028 -- usual overloading rules for dispatching operations apply, and
6029 -- we check base types (not the actual subtypes).
6031 if In_Instance_Visible_Part
6032 and then Is_Dispatching_Operation
(New_Id
)
6034 if not Conforming_Types
6035 (T1
=> Base_Type
(Etype
(Old_Formal
)),
6036 T2
=> Base_Type
(Etype
(New_Formal
)),
6038 Get_Inst
=> Get_Inst
)
6039 and then not Access_Types_Match
6041 Conformance_Error
("\type of & does not match!", New_Formal
);
6045 elsif not Conforming_Types
6046 (T1
=> Old_Formal_Base
,
6047 T2
=> New_Formal_Base
,
6049 Get_Inst
=> Get_Inst
)
6050 and then not Access_Types_Match
6052 -- Don't give error message if old type is Any_Type. This test
6053 -- avoids some cascaded errors, e.g. in case of a bad spec.
6055 if Errmsg
and then Old_Formal_Base
= Any_Type
then
6058 Conformance_Error
("\type of & does not match!", New_Formal
);
6064 -- For mode conformance, mode must match
6066 if Ctype
>= Mode_Conformant
then
6067 if Parameter_Mode
(Old_Formal
) /= Parameter_Mode
(New_Formal
) then
6068 if not Ekind_In
(New_Id
, E_Function
, E_Procedure
)
6069 or else not Is_Primitive_Wrapper
(New_Id
)
6071 Conformance_Error
("\mode of & does not match!", New_Formal
);
6075 T
: constant Entity_Id
:= Find_Dispatching_Type
(New_Id
);
6077 if Is_Protected_Type
6078 (Corresponding_Concurrent_Type
(T
))
6080 Error_Msg_PT
(T
, New_Id
);
6083 ("\mode of & does not match!", New_Formal
);
6090 -- Part of mode conformance for access types is having the same
6091 -- constant modifier.
6093 elsif Access_Types_Match
6094 and then Is_Access_Constant
(Old_Formal_Base
) /=
6095 Is_Access_Constant
(New_Formal_Base
)
6098 ("\constant modifier does not match!", New_Formal
);
6103 if Ctype
>= Subtype_Conformant
then
6105 -- Ada 2005 (AI-231): In case of anonymous access types check
6106 -- the null-exclusion and access-to-constant attributes must
6107 -- match. For null exclusion, we test the types rather than the
6108 -- formals themselves, since the attribute is only set reliably
6109 -- on the formals in the Ada 95 case, and we exclude the case
6110 -- where Old_Formal is marked as controlling, to avoid errors
6111 -- when matching completing bodies with dispatching declarations
6112 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6114 if Ada_Version
>= Ada_2005
6115 and then Ekind
(Etype
(Old_Formal
)) = E_Anonymous_Access_Type
6116 and then Ekind
(Etype
(New_Formal
)) = E_Anonymous_Access_Type
6118 ((Can_Never_Be_Null
(Etype
(Old_Formal
)) /=
6119 Can_Never_Be_Null
(Etype
(New_Formal
))
6121 not Is_Controlling_Formal
(Old_Formal
))
6123 Is_Access_Constant
(Etype
(Old_Formal
)) /=
6124 Is_Access_Constant
(Etype
(New_Formal
)))
6126 -- Do not complain if error already posted on New_Formal. This
6127 -- avoids some redundant error messages.
6129 and then not Error_Posted
(New_Formal
)
6131 -- It is allowed to omit the null-exclusion in case of stream
6132 -- attribute subprograms. We recognize stream subprograms
6133 -- through their TSS-generated suffix.
6136 TSS_Name
: constant TSS_Name_Type
:= Get_TSS_Name
(New_Id
);
6139 if TSS_Name
/= TSS_Stream_Read
6140 and then TSS_Name
/= TSS_Stream_Write
6141 and then TSS_Name
/= TSS_Stream_Input
6142 and then TSS_Name
/= TSS_Stream_Output
6144 -- Here we have a definite conformance error. It is worth
6145 -- special casing the error message for the case of a
6146 -- controlling formal (which excludes null).
6148 if Is_Controlling_Formal
(New_Formal
) then
6149 Error_Msg_Node_2
:= Scope
(New_Formal
);
6151 ("\controlling formal& of& excludes null, "
6152 & "declaration must exclude null as well",
6155 -- Normal case (couldn't we give more detail here???)
6159 ("\type of & does not match!", New_Formal
);
6168 -- Full conformance checks
6170 if Ctype
= Fully_Conformant
then
6172 -- We have checked already that names match
6174 if Parameter_Mode
(Old_Formal
) = E_In_Parameter
then
6176 -- Check default expressions for in parameters
6179 NewD
: constant Boolean :=
6180 Present
(Default_Value
(New_Formal
));
6181 OldD
: constant Boolean :=
6182 Present
(Default_Value
(Old_Formal
));
6184 if NewD
or OldD
then
6186 -- The old default value has been analyzed because the
6187 -- current full declaration will have frozen everything
6188 -- before. The new default value has not been analyzed,
6189 -- so analyze it now before we check for conformance.
6192 Push_Scope
(New_Id
);
6193 Preanalyze_Spec_Expression
6194 (Default_Value
(New_Formal
), Etype
(New_Formal
));
6198 if not (NewD
and OldD
)
6199 or else not Fully_Conformant_Expressions
6200 (Default_Value
(Old_Formal
),
6201 Default_Value
(New_Formal
))
6204 ("\default expression for & does not match!",
6213 -- A couple of special checks for Ada 83 mode. These checks are
6214 -- skipped if either entity is an operator in package Standard,
6215 -- or if either old or new instance is not from the source program.
6217 if Ada_Version
= Ada_83
6218 and then Sloc
(Old_Id
) > Standard_Location
6219 and then Sloc
(New_Id
) > Standard_Location
6220 and then Comes_From_Source
(Old_Id
)
6221 and then Comes_From_Source
(New_Id
)
6224 Old_Param
: constant Node_Id
:= Declaration_Node
(Old_Formal
);
6225 New_Param
: constant Node_Id
:= Declaration_Node
(New_Formal
);
6228 -- Explicit IN must be present or absent in both cases. This
6229 -- test is required only in the full conformance case.
6231 if In_Present
(Old_Param
) /= In_Present
(New_Param
)
6232 and then Ctype
= Fully_Conformant
6235 ("\(Ada 83) IN must appear in both declarations",
6240 -- Grouping (use of comma in param lists) must be the same
6241 -- This is where we catch a misconformance like:
6244 -- A : Integer; B : Integer
6246 -- which are represented identically in the tree except
6247 -- for the setting of the flags More_Ids and Prev_Ids.
6249 if More_Ids
(Old_Param
) /= More_Ids
(New_Param
)
6250 or else Prev_Ids
(Old_Param
) /= Prev_Ids
(New_Param
)
6253 ("\grouping of & does not match!", New_Formal
);
6259 -- This label is required when skipping controlling formals
6261 <<Skip_Controlling_Formal
>>
6263 Next_Formal
(Old_Formal
);
6264 Next_Formal
(New_Formal
);
6267 if Present
(Old_Formal
) then
6268 Conformance_Error
("\too few parameters!");
6271 elsif Present
(New_Formal
) then
6272 Conformance_Error
("\too many parameters!", New_Formal
);
6275 end Check_Conformance
;
6277 -----------------------
6278 -- Check_Conventions --
6279 -----------------------
6281 procedure Check_Conventions
(Typ
: Entity_Id
) is
6282 Ifaces_List
: Elist_Id
;
6284 procedure Check_Convention
(Op
: Entity_Id
);
6285 -- Verify that the convention of inherited dispatching operation Op is
6286 -- consistent among all subprograms it overrides. In order to minimize
6287 -- the search, Search_From is utilized to designate a specific point in
6288 -- the list rather than iterating over the whole list once more.
6290 ----------------------
6291 -- Check_Convention --
6292 ----------------------
6294 procedure Check_Convention
(Op
: Entity_Id
) is
6295 function Convention_Of
(Id
: Entity_Id
) return Convention_Id
;
6296 -- Given an entity, return its convention. The function treats Ghost
6297 -- as convention Ada because the two have the same dynamic semantics.
6303 function Convention_Of
(Id
: Entity_Id
) return Convention_Id
is
6304 Conv
: constant Convention_Id
:= Convention
(Id
);
6306 if Conv
= Convention_Ghost
then
6307 return Convention_Ada
;
6315 Op_Conv
: constant Convention_Id
:= Convention_Of
(Op
);
6316 Iface_Conv
: Convention_Id
;
6317 Iface_Elmt
: Elmt_Id
;
6318 Iface_Prim_Elmt
: Elmt_Id
;
6319 Iface_Prim
: Entity_Id
;
6321 -- Start of processing for Check_Convention
6324 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
6325 while Present
(Iface_Elmt
) loop
6327 First_Elmt
(Primitive_Operations
(Node
(Iface_Elmt
)));
6328 while Present
(Iface_Prim_Elmt
) loop
6329 Iface_Prim
:= Node
(Iface_Prim_Elmt
);
6330 Iface_Conv
:= Convention_Of
(Iface_Prim
);
6332 if Is_Interface_Conformant
(Typ
, Iface_Prim
, Op
)
6333 and then Iface_Conv
/= Op_Conv
6336 ("inconsistent conventions in primitive operations", Typ
);
6338 Error_Msg_Name_1
:= Chars
(Op
);
6339 Error_Msg_Name_2
:= Get_Convention_Name
(Op_Conv
);
6340 Error_Msg_Sloc
:= Sloc
(Op
);
6342 if Comes_From_Source
(Op
) or else No
(Alias
(Op
)) then
6343 if not Present
(Overridden_Operation
(Op
)) then
6344 Error_Msg_N
("\\primitive % defined #", Typ
);
6347 ("\\overriding operation % with " &
6348 "convention % defined #", Typ
);
6351 else pragma Assert
(Present
(Alias
(Op
)));
6352 Error_Msg_Sloc
:= Sloc
(Alias
(Op
));
6354 ("\\inherited operation % with " &
6355 "convention % defined #", Typ
);
6358 Error_Msg_Name_1
:= Chars
(Op
);
6359 Error_Msg_Name_2
:= Get_Convention_Name
(Iface_Conv
);
6360 Error_Msg_Sloc
:= Sloc
(Iface_Prim
);
6362 ("\\overridden operation % with " &
6363 "convention % defined #", Typ
);
6365 -- Avoid cascading errors
6370 Next_Elmt
(Iface_Prim_Elmt
);
6373 Next_Elmt
(Iface_Elmt
);
6375 end Check_Convention
;
6379 Prim_Op
: Entity_Id
;
6380 Prim_Op_Elmt
: Elmt_Id
;
6382 -- Start of processing for Check_Conventions
6385 if not Has_Interfaces
(Typ
) then
6389 Collect_Interfaces
(Typ
, Ifaces_List
);
6391 -- The algorithm checks every overriding dispatching operation against
6392 -- all the corresponding overridden dispatching operations, detecting
6393 -- differences in conventions.
6395 Prim_Op_Elmt
:= First_Elmt
(Primitive_Operations
(Typ
));
6396 while Present
(Prim_Op_Elmt
) loop
6397 Prim_Op
:= Node
(Prim_Op_Elmt
);
6399 -- A small optimization: skip the predefined dispatching operations
6400 -- since they always have the same convention.
6402 if not Is_Predefined_Dispatching_Operation
(Prim_Op
) then
6403 Check_Convention
(Prim_Op
);
6406 Next_Elmt
(Prim_Op_Elmt
);
6408 end Check_Conventions
;
6410 ------------------------------
6411 -- Check_Delayed_Subprogram --
6412 ------------------------------
6414 procedure Check_Delayed_Subprogram
(Designator
: Entity_Id
) is
6417 procedure Possible_Freeze
(T
: Entity_Id
);
6418 -- T is the type of either a formal parameter or of the return type.
6419 -- If T is not yet frozen and needs a delayed freeze, then the
6420 -- subprogram itself must be delayed. If T is the limited view of an
6421 -- incomplete type the subprogram must be frozen as well, because
6422 -- T may depend on local types that have not been frozen yet.
6424 ---------------------
6425 -- Possible_Freeze --
6426 ---------------------
6428 procedure Possible_Freeze
(T
: Entity_Id
) is
6430 if Has_Delayed_Freeze
(T
) and then not Is_Frozen
(T
) then
6431 Set_Has_Delayed_Freeze
(Designator
);
6433 elsif Is_Access_Type
(T
)
6434 and then Has_Delayed_Freeze
(Designated_Type
(T
))
6435 and then not Is_Frozen
(Designated_Type
(T
))
6437 Set_Has_Delayed_Freeze
(Designator
);
6439 elsif Ekind
(T
) = E_Incomplete_Type
and then From_With_Type
(T
) then
6440 Set_Has_Delayed_Freeze
(Designator
);
6442 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
6443 -- of a subprogram or entry declaration.
6445 elsif Ekind
(T
) = E_Incomplete_Type
6446 and then Ada_Version
>= Ada_2012
6448 Set_Has_Delayed_Freeze
(Designator
);
6451 end Possible_Freeze
;
6453 -- Start of processing for Check_Delayed_Subprogram
6456 -- All subprograms, including abstract subprograms, may need a freeze
6457 -- node if some formal type or the return type needs one.
6459 Possible_Freeze
(Etype
(Designator
));
6460 Possible_Freeze
(Base_Type
(Etype
(Designator
))); -- needed ???
6462 -- Need delayed freeze if any of the formal types themselves need
6463 -- a delayed freeze and are not yet frozen.
6465 F
:= First_Formal
(Designator
);
6466 while Present
(F
) loop
6467 Possible_Freeze
(Etype
(F
));
6468 Possible_Freeze
(Base_Type
(Etype
(F
))); -- needed ???
6472 -- Mark functions that return by reference. Note that it cannot be
6473 -- done for delayed_freeze subprograms because the underlying
6474 -- returned type may not be known yet (for private types)
6476 if not Has_Delayed_Freeze
(Designator
) and then Expander_Active
then
6478 Typ
: constant Entity_Id
:= Etype
(Designator
);
6479 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
6481 if Is_Immutably_Limited_Type
(Typ
) then
6482 Set_Returns_By_Ref
(Designator
);
6483 elsif Present
(Utyp
) and then CW_Or_Has_Controlled_Part
(Utyp
) then
6484 Set_Returns_By_Ref
(Designator
);
6488 end Check_Delayed_Subprogram
;
6490 ------------------------------------
6491 -- Check_Discriminant_Conformance --
6492 ------------------------------------
6494 procedure Check_Discriminant_Conformance
6499 Old_Discr
: Entity_Id
:= First_Discriminant
(Prev
);
6500 New_Discr
: Node_Id
:= First
(Discriminant_Specifications
(N
));
6501 New_Discr_Id
: Entity_Id
;
6502 New_Discr_Type
: Entity_Id
;
6504 procedure Conformance_Error
(Msg
: String; N
: Node_Id
);
6505 -- Post error message for conformance error on given node. Two messages
6506 -- are output. The first points to the previous declaration with a
6507 -- general "no conformance" message. The second is the detailed reason,
6508 -- supplied as Msg. The parameter N provide information for a possible
6509 -- & insertion in the message.
6511 -----------------------
6512 -- Conformance_Error --
6513 -----------------------
6515 procedure Conformance_Error
(Msg
: String; N
: Node_Id
) is
6517 Error_Msg_Sloc
:= Sloc
(Prev_Loc
);
6518 Error_Msg_N
-- CODEFIX
6519 ("not fully conformant with declaration#!", N
);
6520 Error_Msg_NE
(Msg
, N
, N
);
6521 end Conformance_Error
;
6523 -- Start of processing for Check_Discriminant_Conformance
6526 while Present
(Old_Discr
) and then Present
(New_Discr
) loop
6527 New_Discr_Id
:= Defining_Identifier
(New_Discr
);
6529 -- The subtype mark of the discriminant on the full type has not
6530 -- been analyzed so we do it here. For an access discriminant a new
6533 if Nkind
(Discriminant_Type
(New_Discr
)) = N_Access_Definition
then
6535 Access_Definition
(N
, Discriminant_Type
(New_Discr
));
6538 Analyze
(Discriminant_Type
(New_Discr
));
6539 New_Discr_Type
:= Etype
(Discriminant_Type
(New_Discr
));
6541 -- Ada 2005: if the discriminant definition carries a null
6542 -- exclusion, create an itype to check properly for consistency
6543 -- with partial declaration.
6545 if Is_Access_Type
(New_Discr_Type
)
6546 and then Null_Exclusion_Present
(New_Discr
)
6549 Create_Null_Excluding_Itype
6550 (T
=> New_Discr_Type
,
6551 Related_Nod
=> New_Discr
,
6552 Scope_Id
=> Current_Scope
);
6556 if not Conforming_Types
6557 (Etype
(Old_Discr
), New_Discr_Type
, Fully_Conformant
)
6559 Conformance_Error
("type of & does not match!", New_Discr_Id
);
6562 -- Treat the new discriminant as an occurrence of the old one,
6563 -- for navigation purposes, and fill in some semantic
6564 -- information, for completeness.
6566 Generate_Reference
(Old_Discr
, New_Discr_Id
, 'r');
6567 Set_Etype
(New_Discr_Id
, Etype
(Old_Discr
));
6568 Set_Scope
(New_Discr_Id
, Scope
(Old_Discr
));
6573 if Chars
(Old_Discr
) /= Chars
(Defining_Identifier
(New_Discr
)) then
6574 Conformance_Error
("name & does not match!", New_Discr_Id
);
6578 -- Default expressions must match
6581 NewD
: constant Boolean :=
6582 Present
(Expression
(New_Discr
));
6583 OldD
: constant Boolean :=
6584 Present
(Expression
(Parent
(Old_Discr
)));
6587 if NewD
or OldD
then
6589 -- The old default value has been analyzed and expanded,
6590 -- because the current full declaration will have frozen
6591 -- everything before. The new default values have not been
6592 -- expanded, so expand now to check conformance.
6595 Preanalyze_Spec_Expression
6596 (Expression
(New_Discr
), New_Discr_Type
);
6599 if not (NewD
and OldD
)
6600 or else not Fully_Conformant_Expressions
6601 (Expression
(Parent
(Old_Discr
)),
6602 Expression
(New_Discr
))
6606 ("default expression for & does not match!",
6613 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6615 if Ada_Version
= Ada_83
then
6617 Old_Disc
: constant Node_Id
:= Declaration_Node
(Old_Discr
);
6620 -- Grouping (use of comma in param lists) must be the same
6621 -- This is where we catch a misconformance like:
6624 -- A : Integer; B : Integer
6626 -- which are represented identically in the tree except
6627 -- for the setting of the flags More_Ids and Prev_Ids.
6629 if More_Ids
(Old_Disc
) /= More_Ids
(New_Discr
)
6630 or else Prev_Ids
(Old_Disc
) /= Prev_Ids
(New_Discr
)
6633 ("grouping of & does not match!", New_Discr_Id
);
6639 Next_Discriminant
(Old_Discr
);
6643 if Present
(Old_Discr
) then
6644 Conformance_Error
("too few discriminants!", Defining_Identifier
(N
));
6647 elsif Present
(New_Discr
) then
6649 ("too many discriminants!", Defining_Identifier
(New_Discr
));
6652 end Check_Discriminant_Conformance
;
6654 ----------------------------
6655 -- Check_Fully_Conformant --
6656 ----------------------------
6658 procedure Check_Fully_Conformant
6659 (New_Id
: Entity_Id
;
6661 Err_Loc
: Node_Id
:= Empty
)
6664 pragma Warnings
(Off
, Result
);
6667 (New_Id
, Old_Id
, Fully_Conformant
, True, Result
, Err_Loc
);
6668 end Check_Fully_Conformant
;
6670 ---------------------------
6671 -- Check_Mode_Conformant --
6672 ---------------------------
6674 procedure Check_Mode_Conformant
6675 (New_Id
: Entity_Id
;
6677 Err_Loc
: Node_Id
:= Empty
;
6678 Get_Inst
: Boolean := False)
6681 pragma Warnings
(Off
, Result
);
6684 (New_Id
, Old_Id
, Mode_Conformant
, True, Result
, Err_Loc
, Get_Inst
);
6685 end Check_Mode_Conformant
;
6687 --------------------------------
6688 -- Check_Overriding_Indicator --
6689 --------------------------------
6691 procedure Check_Overriding_Indicator
6693 Overridden_Subp
: Entity_Id
;
6694 Is_Primitive
: Boolean)
6700 -- No overriding indicator for literals
6702 if Ekind
(Subp
) = E_Enumeration_Literal
then
6705 elsif Ekind
(Subp
) = E_Entry
then
6706 Decl
:= Parent
(Subp
);
6708 -- No point in analyzing a malformed operator
6710 elsif Nkind
(Subp
) = N_Defining_Operator_Symbol
6711 and then Error_Posted
(Subp
)
6716 Decl
:= Unit_Declaration_Node
(Subp
);
6719 if Nkind_In
(Decl
, N_Subprogram_Body
,
6720 N_Subprogram_Body_Stub
,
6721 N_Subprogram_Declaration
,
6722 N_Abstract_Subprogram_Declaration
,
6723 N_Subprogram_Renaming_Declaration
)
6725 Spec
:= Specification
(Decl
);
6727 elsif Nkind
(Decl
) = N_Entry_Declaration
then
6734 -- The overriding operation is type conformant with the overridden one,
6735 -- but the names of the formals are not required to match. If the names
6736 -- appear permuted in the overriding operation, this is a possible
6737 -- source of confusion that is worth diagnosing. Controlling formals
6738 -- often carry names that reflect the type, and it is not worthwhile
6739 -- requiring that their names match.
6741 if Present
(Overridden_Subp
)
6742 and then Nkind
(Subp
) /= N_Defining_Operator_Symbol
6749 Form1
:= First_Formal
(Subp
);
6750 Form2
:= First_Formal
(Overridden_Subp
);
6752 -- If the overriding operation is a synchronized operation, skip
6753 -- the first parameter of the overridden operation, which is
6754 -- implicit in the new one. If the operation is declared in the
6755 -- body it is not primitive and all formals must match.
6757 if Is_Concurrent_Type
(Scope
(Subp
))
6758 and then Is_Tagged_Type
(Scope
(Subp
))
6759 and then not Has_Completion
(Scope
(Subp
))
6761 Form2
:= Next_Formal
(Form2
);
6764 if Present
(Form1
) then
6765 Form1
:= Next_Formal
(Form1
);
6766 Form2
:= Next_Formal
(Form2
);
6769 while Present
(Form1
) loop
6770 if not Is_Controlling_Formal
(Form1
)
6771 and then Present
(Next_Formal
(Form2
))
6772 and then Chars
(Form1
) = Chars
(Next_Formal
(Form2
))
6774 Error_Msg_Node_2
:= Alias
(Overridden_Subp
);
6775 Error_Msg_Sloc
:= Sloc
(Error_Msg_Node_2
);
6777 ("& does not match corresponding formal of&#",
6782 Next_Formal
(Form1
);
6783 Next_Formal
(Form2
);
6788 -- If there is an overridden subprogram, then check that there is no
6789 -- "not overriding" indicator, and mark the subprogram as overriding.
6790 -- This is not done if the overridden subprogram is marked as hidden,
6791 -- which can occur for the case of inherited controlled operations
6792 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6793 -- subprogram is not itself hidden. (Note: This condition could probably
6794 -- be simplified, leaving out the testing for the specific controlled
6795 -- cases, but it seems safer and clearer this way, and echoes similar
6796 -- special-case tests of this kind in other places.)
6798 if Present
(Overridden_Subp
)
6799 and then (not Is_Hidden
(Overridden_Subp
)
6801 (Nam_In
(Chars
(Overridden_Subp
), Name_Initialize
,
6804 and then Present
(Alias
(Overridden_Subp
))
6805 and then not Is_Hidden
(Alias
(Overridden_Subp
))))
6807 if Must_Not_Override
(Spec
) then
6808 Error_Msg_Sloc
:= Sloc
(Overridden_Subp
);
6810 if Ekind
(Subp
) = E_Entry
then
6812 ("entry & overrides inherited operation #", Spec
, Subp
);
6815 ("subprogram & overrides inherited operation #", Spec
, Subp
);
6818 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6819 -- as an extension of Root_Controlled, and thus has a useless Adjust
6820 -- operation. This operation should not be inherited by other limited
6821 -- controlled types. An explicit Adjust for them is not overriding.
6823 elsif Must_Override
(Spec
)
6824 and then Chars
(Overridden_Subp
) = Name_Adjust
6825 and then Is_Limited_Type
(Etype
(First_Formal
(Subp
)))
6826 and then Present
(Alias
(Overridden_Subp
))
6828 Is_Predefined_File_Name
6829 (Unit_File_Name
(Get_Source_Unit
(Alias
(Overridden_Subp
))))
6831 Error_Msg_NE
("subprogram & is not overriding", Spec
, Subp
);
6833 elsif Is_Subprogram
(Subp
) then
6834 if Is_Init_Proc
(Subp
) then
6837 elsif No
(Overridden_Operation
(Subp
)) then
6839 -- For entities generated by Derive_Subprograms the overridden
6840 -- operation is the inherited primitive (which is available
6841 -- through the attribute alias)
6843 if (Is_Dispatching_Operation
(Subp
)
6844 or else Is_Dispatching_Operation
(Overridden_Subp
))
6845 and then not Comes_From_Source
(Overridden_Subp
)
6846 and then Find_Dispatching_Type
(Overridden_Subp
) =
6847 Find_Dispatching_Type
(Subp
)
6848 and then Present
(Alias
(Overridden_Subp
))
6849 and then Comes_From_Source
(Alias
(Overridden_Subp
))
6851 Set_Overridden_Operation
(Subp
, Alias
(Overridden_Subp
));
6854 Set_Overridden_Operation
(Subp
, Overridden_Subp
);
6859 -- If primitive flag is set or this is a protected operation, then
6860 -- the operation is overriding at the point of its declaration, so
6861 -- warn if necessary. Otherwise it may have been declared before the
6862 -- operation it overrides and no check is required.
6865 and then not Must_Override
(Spec
)
6866 and then (Is_Primitive
6867 or else Ekind
(Scope
(Subp
)) = E_Protected_Type
)
6869 Style
.Missing_Overriding
(Decl
, Subp
);
6872 -- If Subp is an operator, it may override a predefined operation, if
6873 -- it is defined in the same scope as the type to which it applies.
6874 -- In that case Overridden_Subp is empty because of our implicit
6875 -- representation for predefined operators. We have to check whether the
6876 -- signature of Subp matches that of a predefined operator. Note that
6877 -- first argument provides the name of the operator, and the second
6878 -- argument the signature that may match that of a standard operation.
6879 -- If the indicator is overriding, then the operator must match a
6880 -- predefined signature, because we know already that there is no
6881 -- explicit overridden operation.
6883 elsif Nkind
(Subp
) = N_Defining_Operator_Symbol
then
6884 if Must_Not_Override
(Spec
) then
6886 -- If this is not a primitive or a protected subprogram, then
6887 -- "not overriding" is illegal.
6890 and then Ekind
(Scope
(Subp
)) /= E_Protected_Type
6893 ("overriding indicator only allowed "
6894 & "if subprogram is primitive", Subp
);
6896 elsif Can_Override_Operator
(Subp
) then
6898 ("subprogram& overrides predefined operator ", Spec
, Subp
);
6901 elsif Must_Override
(Spec
) then
6902 if No
(Overridden_Operation
(Subp
))
6903 and then not Can_Override_Operator
(Subp
)
6905 Error_Msg_NE
("subprogram & is not overriding", Spec
, Subp
);
6908 elsif not Error_Posted
(Subp
)
6909 and then Style_Check
6910 and then Can_Override_Operator
(Subp
)
6912 not Is_Predefined_File_Name
6913 (Unit_File_Name
(Get_Source_Unit
(Subp
)))
6915 -- If style checks are enabled, indicate that the indicator is
6916 -- missing. However, at the point of declaration, the type of
6917 -- which this is a primitive operation may be private, in which
6918 -- case the indicator would be premature.
6920 if Has_Private_Declaration
(Etype
(Subp
))
6921 or else Has_Private_Declaration
(Etype
(First_Formal
(Subp
)))
6925 Style
.Missing_Overriding
(Decl
, Subp
);
6929 elsif Must_Override
(Spec
) then
6930 if Ekind
(Subp
) = E_Entry
then
6931 Error_Msg_NE
("entry & is not overriding", Spec
, Subp
);
6933 Error_Msg_NE
("subprogram & is not overriding", Spec
, Subp
);
6936 -- If the operation is marked "not overriding" and it's not primitive
6937 -- then an error is issued, unless this is an operation of a task or
6938 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6939 -- has been specified have already been checked above.
6941 elsif Must_Not_Override
(Spec
)
6942 and then not Is_Primitive
6943 and then Ekind
(Subp
) /= E_Entry
6944 and then Ekind
(Scope
(Subp
)) /= E_Protected_Type
6947 ("overriding indicator only allowed if subprogram is primitive",
6951 end Check_Overriding_Indicator
;
6957 -- Note: this procedure needs to know far too much about how the expander
6958 -- messes with exceptions. The use of the flag Exception_Junk and the
6959 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6960 -- works, but is not very clean. It would be better if the expansion
6961 -- routines would leave Original_Node working nicely, and we could use
6962 -- Original_Node here to ignore all the peculiar expander messing ???
6964 procedure Check_Returns
6968 Proc
: Entity_Id
:= Empty
)
6972 procedure Check_Statement_Sequence
(L
: List_Id
);
6973 -- Internal recursive procedure to check a list of statements for proper
6974 -- termination by a return statement (or a transfer of control or a
6975 -- compound statement that is itself internally properly terminated).
6977 ------------------------------
6978 -- Check_Statement_Sequence --
6979 ------------------------------
6981 procedure Check_Statement_Sequence
(L
: List_Id
) is
6986 Raise_Exception_Call
: Boolean;
6987 -- Set True if statement sequence terminated by Raise_Exception call
6988 -- or a Reraise_Occurrence call.
6991 Raise_Exception_Call
:= False;
6993 -- Get last real statement
6995 Last_Stm
:= Last
(L
);
6997 -- Deal with digging out exception handler statement sequences that
6998 -- have been transformed by the local raise to goto optimization.
6999 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7000 -- optimization has occurred, we are looking at something like:
7003 -- original stmts in block
7007 -- goto L1; | omitted if No_Exception_Propagation
7012 -- goto L3; -- skip handler when exception not raised
7014 -- <<L1>> -- target label for local exception
7028 -- and what we have to do is to dig out the estmts1 and estmts2
7029 -- sequences (which were the original sequences of statements in
7030 -- the exception handlers) and check them.
7032 if Nkind
(Last_Stm
) = N_Label
and then Exception_Junk
(Last_Stm
) then
7037 exit when Nkind
(Stm
) /= N_Block_Statement
;
7038 exit when not Exception_Junk
(Stm
);
7041 exit when Nkind
(Stm
) /= N_Label
;
7042 exit when not Exception_Junk
(Stm
);
7043 Check_Statement_Sequence
7044 (Statements
(Handled_Statement_Sequence
(Next
(Stm
))));
7049 exit when Nkind
(Stm
) /= N_Goto_Statement
;
7050 exit when not Exception_Junk
(Stm
);
7054 -- Don't count pragmas
7056 while Nkind
(Last_Stm
) = N_Pragma
7058 -- Don't count call to SS_Release (can happen after Raise_Exception)
7061 (Nkind
(Last_Stm
) = N_Procedure_Call_Statement
7063 Nkind
(Name
(Last_Stm
)) = N_Identifier
7065 Is_RTE
(Entity
(Name
(Last_Stm
)), RE_SS_Release
))
7067 -- Don't count exception junk
7070 (Nkind_In
(Last_Stm
, N_Goto_Statement
,
7072 N_Object_Declaration
)
7073 and then Exception_Junk
(Last_Stm
))
7074 or else Nkind
(Last_Stm
) in N_Push_xxx_Label
7075 or else Nkind
(Last_Stm
) in N_Pop_xxx_Label
7077 -- Inserted code, such as finalization calls, is irrelevant: we only
7078 -- need to check original source.
7080 or else Is_Rewrite_Insertion
(Last_Stm
)
7085 -- Here we have the "real" last statement
7087 Kind
:= Nkind
(Last_Stm
);
7089 -- Transfer of control, OK. Note that in the No_Return procedure
7090 -- case, we already diagnosed any explicit return statements, so
7091 -- we can treat them as OK in this context.
7093 if Is_Transfer
(Last_Stm
) then
7096 -- Check cases of explicit non-indirect procedure calls
7098 elsif Kind
= N_Procedure_Call_Statement
7099 and then Is_Entity_Name
(Name
(Last_Stm
))
7101 -- Check call to Raise_Exception procedure which is treated
7102 -- specially, as is a call to Reraise_Occurrence.
7104 -- We suppress the warning in these cases since it is likely that
7105 -- the programmer really does not expect to deal with the case
7106 -- of Null_Occurrence, and thus would find a warning about a
7107 -- missing return curious, and raising Program_Error does not
7108 -- seem such a bad behavior if this does occur.
7110 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7111 -- behavior will be to raise Constraint_Error (see AI-329).
7113 if Is_RTE
(Entity
(Name
(Last_Stm
)), RE_Raise_Exception
)
7115 Is_RTE
(Entity
(Name
(Last_Stm
)), RE_Reraise_Occurrence
)
7117 Raise_Exception_Call
:= True;
7119 -- For Raise_Exception call, test first argument, if it is
7120 -- an attribute reference for a 'Identity call, then we know
7121 -- that the call cannot possibly return.
7124 Arg
: constant Node_Id
:=
7125 Original_Node
(First_Actual
(Last_Stm
));
7127 if Nkind
(Arg
) = N_Attribute_Reference
7128 and then Attribute_Name
(Arg
) = Name_Identity
7135 -- If statement, need to look inside if there is an else and check
7136 -- each constituent statement sequence for proper termination.
7138 elsif Kind
= N_If_Statement
7139 and then Present
(Else_Statements
(Last_Stm
))
7141 Check_Statement_Sequence
(Then_Statements
(Last_Stm
));
7142 Check_Statement_Sequence
(Else_Statements
(Last_Stm
));
7144 if Present
(Elsif_Parts
(Last_Stm
)) then
7146 Elsif_Part
: Node_Id
:= First
(Elsif_Parts
(Last_Stm
));
7149 while Present
(Elsif_Part
) loop
7150 Check_Statement_Sequence
(Then_Statements
(Elsif_Part
));
7158 -- Case statement, check each case for proper termination
7160 elsif Kind
= N_Case_Statement
then
7164 Case_Alt
:= First_Non_Pragma
(Alternatives
(Last_Stm
));
7165 while Present
(Case_Alt
) loop
7166 Check_Statement_Sequence
(Statements
(Case_Alt
));
7167 Next_Non_Pragma
(Case_Alt
);
7173 -- Block statement, check its handled sequence of statements
7175 elsif Kind
= N_Block_Statement
then
7181 (Handled_Statement_Sequence
(Last_Stm
), Mode
, Err1
);
7190 -- Loop statement. If there is an iteration scheme, we can definitely
7191 -- fall out of the loop. Similarly if there is an exit statement, we
7192 -- can fall out. In either case we need a following return.
7194 elsif Kind
= N_Loop_Statement
then
7195 if Present
(Iteration_Scheme
(Last_Stm
))
7196 or else Has_Exit
(Entity
(Identifier
(Last_Stm
)))
7200 -- A loop with no exit statement or iteration scheme is either
7201 -- an infinite loop, or it has some other exit (raise/return).
7202 -- In either case, no warning is required.
7208 -- Timed entry call, check entry call and delay alternatives
7210 -- Note: in expanded code, the timed entry call has been converted
7211 -- to a set of expanded statements on which the check will work
7212 -- correctly in any case.
7214 elsif Kind
= N_Timed_Entry_Call
then
7216 ECA
: constant Node_Id
:= Entry_Call_Alternative
(Last_Stm
);
7217 DCA
: constant Node_Id
:= Delay_Alternative
(Last_Stm
);
7220 -- If statement sequence of entry call alternative is missing,
7221 -- then we can definitely fall through, and we post the error
7222 -- message on the entry call alternative itself.
7224 if No
(Statements
(ECA
)) then
7227 -- If statement sequence of delay alternative is missing, then
7228 -- we can definitely fall through, and we post the error
7229 -- message on the delay alternative itself.
7231 -- Note: if both ECA and DCA are missing the return, then we
7232 -- post only one message, should be enough to fix the bugs.
7233 -- If not we will get a message next time on the DCA when the
7236 elsif No
(Statements
(DCA
)) then
7239 -- Else check both statement sequences
7242 Check_Statement_Sequence
(Statements
(ECA
));
7243 Check_Statement_Sequence
(Statements
(DCA
));
7248 -- Conditional entry call, check entry call and else part
7250 -- Note: in expanded code, the conditional entry call has been
7251 -- converted to a set of expanded statements on which the check
7252 -- will work correctly in any case.
7254 elsif Kind
= N_Conditional_Entry_Call
then
7256 ECA
: constant Node_Id
:= Entry_Call_Alternative
(Last_Stm
);
7259 -- If statement sequence of entry call alternative is missing,
7260 -- then we can definitely fall through, and we post the error
7261 -- message on the entry call alternative itself.
7263 if No
(Statements
(ECA
)) then
7266 -- Else check statement sequence and else part
7269 Check_Statement_Sequence
(Statements
(ECA
));
7270 Check_Statement_Sequence
(Else_Statements
(Last_Stm
));
7276 -- If we fall through, issue appropriate message
7279 if not Raise_Exception_Call
then
7281 ("RETURN statement missing following this statement??!",
7284 ("\Program_Error may be raised at run time??!",
7288 -- Note: we set Err even though we have not issued a warning
7289 -- because we still have a case of a missing return. This is
7290 -- an extremely marginal case, probably will never be noticed
7291 -- but we might as well get it right.
7295 -- Otherwise we have the case of a procedure marked No_Return
7298 if not Raise_Exception_Call
then
7300 ("implied return after this statement " &
7301 "will raise Program_Error??",
7304 ("\procedure & is marked as No_Return??!",
7309 RE
: constant Node_Id
:=
7310 Make_Raise_Program_Error
(Sloc
(Last_Stm
),
7311 Reason
=> PE_Implicit_Return
);
7313 Insert_After
(Last_Stm
, RE
);
7317 end Check_Statement_Sequence
;
7319 -- Start of processing for Check_Returns
7323 Check_Statement_Sequence
(Statements
(HSS
));
7325 if Present
(Exception_Handlers
(HSS
)) then
7326 Handler
:= First_Non_Pragma
(Exception_Handlers
(HSS
));
7327 while Present
(Handler
) loop
7328 Check_Statement_Sequence
(Statements
(Handler
));
7329 Next_Non_Pragma
(Handler
);
7334 ----------------------------
7335 -- Check_Subprogram_Order --
7336 ----------------------------
7338 procedure Check_Subprogram_Order
(N
: Node_Id
) is
7340 function Subprogram_Name_Greater
(S1
, S2
: String) return Boolean;
7341 -- This is used to check if S1 > S2 in the sense required by this test,
7342 -- for example nameab < namec, but name2 < name10.
7344 -----------------------------
7345 -- Subprogram_Name_Greater --
7346 -----------------------------
7348 function Subprogram_Name_Greater
(S1
, S2
: String) return Boolean is
7353 -- Deal with special case where names are identical except for a
7354 -- numerical suffix. These are handled specially, taking the numeric
7355 -- ordering from the suffix into account.
7358 while S1
(L1
) in '0' .. '9' loop
7363 while S2
(L2
) in '0' .. '9' loop
7367 -- If non-numeric parts non-equal, do straight compare
7369 if S1
(S1
'First .. L1
) /= S2
(S2
'First .. L2
) then
7372 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7373 -- that a missing suffix is treated as numeric zero in this test.
7377 while L1
< S1
'Last loop
7379 N1
:= N1
* 10 + Character'Pos (S1
(L1
)) - Character'Pos ('0');
7383 while L2
< S2
'Last loop
7385 N2
:= N2
* 10 + Character'Pos (S2
(L2
)) - Character'Pos ('0');
7390 end Subprogram_Name_Greater
;
7392 -- Start of processing for Check_Subprogram_Order
7395 -- Check body in alpha order if this is option
7398 and then Style_Check_Order_Subprograms
7399 and then Nkind
(N
) = N_Subprogram_Body
7400 and then Comes_From_Source
(N
)
7401 and then In_Extended_Main_Source_Unit
(N
)
7405 renames Scope_Stack
.Table
7406 (Scope_Stack
.Last
).Last_Subprogram_Name
;
7408 Body_Id
: constant Entity_Id
:=
7409 Defining_Entity
(Specification
(N
));
7412 Get_Decoded_Name_String
(Chars
(Body_Id
));
7415 if Subprogram_Name_Greater
7416 (LSN
.all, Name_Buffer
(1 .. Name_Len
))
7418 Style
.Subprogram_Not_In_Alpha_Order
(Body_Id
);
7424 LSN
:= new String'(Name_Buffer (1 .. Name_Len));
7427 end Check_Subprogram_Order;
7429 ------------------------------
7430 -- Check_Subtype_Conformant --
7431 ------------------------------
7433 procedure Check_Subtype_Conformant
7434 (New_Id : Entity_Id;
7436 Err_Loc : Node_Id := Empty;
7437 Skip_Controlling_Formals : Boolean := False;
7438 Get_Inst : Boolean := False)
7441 pragma Warnings (Off, Result);
7444 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7445 Skip_Controlling_Formals => Skip_Controlling_Formals,
7446 Get_Inst => Get_Inst);
7447 end Check_Subtype_Conformant;
7449 ---------------------------
7450 -- Check_Type_Conformant --
7451 ---------------------------
7453 procedure Check_Type_Conformant
7454 (New_Id : Entity_Id;
7456 Err_Loc : Node_Id := Empty)
7459 pragma Warnings (Off, Result);
7462 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7463 end Check_Type_Conformant;
7465 ---------------------------
7466 -- Can_Override_Operator --
7467 ---------------------------
7469 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7473 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7477 Typ := Base_Type (Etype (First_Formal (Subp)));
7479 -- Check explicitly that the operation is a primitive of the type
7481 return Operator_Matches_Spec (Subp, Subp)
7482 and then not Is_Generic_Type (Typ)
7483 and then Scope (Subp) = Scope (Typ)
7484 and then not Is_Class_Wide_Type (Typ);
7486 end Can_Override_Operator;
7488 ----------------------
7489 -- Conforming_Types --
7490 ----------------------
7492 function Conforming_Types
7495 Ctype : Conformance_Type;
7496 Get_Inst : Boolean := False) return Boolean
7498 Type_1 : Entity_Id := T1;
7499 Type_2 : Entity_Id := T2;
7500 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7502 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
7503 -- If neither T1 nor T2 are generic actual types, or if they are in
7504 -- different scopes (e.g. parent and child instances), then verify that
7505 -- the base types are equal. Otherwise T1 and T2 must be on the same
7506 -- subtype chain. The whole purpose of this procedure is to prevent
7507 -- spurious ambiguities in an instantiation that may arise if two
7508 -- distinct generic types are instantiated with the same actual.
7510 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7511 -- An access parameter can designate an incomplete type. If the
7512 -- incomplete type is the limited view of a type from a limited_
7513 -- with_clause, check whether the non-limited view is available. If
7514 -- it is a (non-limited) incomplete type, get the full view.
7516 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7517 -- Returns True if and only if either T1 denotes a limited view of T2
7518 -- or T2 denotes a limited view of T1. This can arise when the limited
7519 -- with view of a type is used in a subprogram declaration and the
7520 -- subprogram body is in the scope of a regular with clause for the
7521 -- same unit. In such a case, the two type entities can be considered
7522 -- identical for purposes of conformance checking.
7524 ----------------------
7525 -- Base_Types_Match --
7526 ----------------------
7528 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7529 BT1 : constant Entity_Id := Base_Type (T1);
7530 BT2 : constant Entity_Id := Base_Type (T2);
7536 elsif BT1 = BT2 then
7538 -- The following is too permissive. A more precise test should
7539 -- check that the generic actual is an ancestor subtype of the
7542 -- See code in Find_Corresponding_Spec that applies an additional
7543 -- filter to handle accidental amiguities in instances.
7545 return not Is_Generic_Actual_Type (T1)
7546 or else not Is_Generic_Actual_Type (T2)
7547 or else Scope (T1) /= Scope (T2);
7549 -- If T2 is a generic actual type it is declared as the subtype of
7550 -- the actual. If that actual is itself a subtype we need to use its
7551 -- own base type to check for compatibility.
7553 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
7556 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
7562 end Base_Types_Match;
7564 --------------------------
7565 -- Find_Designated_Type --
7566 --------------------------
7568 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7572 Desig := Directly_Designated_Type (T);
7574 if Ekind (Desig) = E_Incomplete_Type then
7576 -- If regular incomplete type, get full view if available
7578 if Present (Full_View (Desig)) then
7579 Desig := Full_View (Desig);
7581 -- If limited view of a type, get non-limited view if available,
7582 -- and check again for a regular incomplete type.
7584 elsif Present (Non_Limited_View (Desig)) then
7585 Desig := Get_Full_View (Non_Limited_View (Desig));
7590 end Find_Designated_Type;
7592 -------------------------------
7593 -- Matches_Limited_With_View --
7594 -------------------------------
7596 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7598 -- In some cases a type imported through a limited_with clause, and
7599 -- its nonlimited view are both visible, for example in an anonymous
7600 -- access-to-class-wide type in a formal. Both entities designate the
7603 if From_With_Type (T1) and then T2 = Available_View (T1) then
7606 elsif From_With_Type (T2) and then T1 = Available_View (T2) then
7609 elsif From_With_Type (T1)
7610 and then From_With_Type (T2)
7611 and then Available_View (T1) = Available_View (T2)
7618 end Matches_Limited_With_View;
7620 -- Start of processing for Conforming_Types
7623 -- The context is an instance association for a formal access-to-
7624 -- subprogram type; the formal parameter types require mapping because
7625 -- they may denote other formal parameters of the generic unit.
7628 Type_1 := Get_Instance_Of (T1);
7629 Type_2 := Get_Instance_Of (T2);
7632 -- If one of the types is a view of the other introduced by a limited
7633 -- with clause, treat these as conforming for all purposes.
7635 if Matches_Limited_With_View (T1, T2) then
7638 elsif Base_Types_Match (Type_1, Type_2) then
7639 return Ctype <= Mode_Conformant
7640 or else Subtypes_Statically_Match (Type_1, Type_2);
7642 elsif Is_Incomplete_Or_Private_Type (Type_1)
7643 and then Present (Full_View (Type_1))
7644 and then Base_Types_Match (Full_View (Type_1), Type_2)
7646 return Ctype <= Mode_Conformant
7647 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7649 elsif Ekind (Type_2) = E_Incomplete_Type
7650 and then Present (Full_View (Type_2))
7651 and then Base_Types_Match (Type_1, Full_View (Type_2))
7653 return Ctype <= Mode_Conformant
7654 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7656 elsif Is_Private_Type (Type_2)
7657 and then In_Instance
7658 and then Present (Full_View (Type_2))
7659 and then Base_Types_Match (Type_1, Full_View (Type_2))
7661 return Ctype <= Mode_Conformant
7662 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7665 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7666 -- treated recursively because they carry a signature. As far as
7667 -- conformance is concerned, convention plays no role, and either
7668 -- or both could be access to protected subprograms.
7670 Are_Anonymous_Access_To_Subprogram_Types :=
7671 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7672 E_Anonymous_Access_Protected_Subprogram_Type)
7674 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7675 E_Anonymous_Access_Protected_Subprogram_Type);
7677 -- Test anonymous access type case. For this case, static subtype
7678 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7679 -- the base types because we may have built internal subtype entities
7680 -- to handle null-excluding types (see Process_Formals).
7682 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7684 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7686 -- Ada 2005 (AI-254)
7688 or else Are_Anonymous_Access_To_Subprogram_Types
7691 Desig_1 : Entity_Id;
7692 Desig_2 : Entity_Id;
7695 -- In Ada 2005, access constant indicators must match for
7696 -- subtype conformance.
7698 if Ada_Version >= Ada_2005
7699 and then Ctype >= Subtype_Conformant
7701 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7706 Desig_1 := Find_Designated_Type (Type_1);
7707 Desig_2 := Find_Designated_Type (Type_2);
7709 -- If the context is an instance association for a formal
7710 -- access-to-subprogram type; formal access parameter designated
7711 -- types require mapping because they may denote other formal
7712 -- parameters of the generic unit.
7715 Desig_1 := Get_Instance_Of (Desig_1);
7716 Desig_2 := Get_Instance_Of (Desig_2);
7719 -- It is possible for a Class_Wide_Type to be introduced for an
7720 -- incomplete type, in which case there is a separate class_ wide
7721 -- type for the full view. The types conform if their Etypes
7722 -- conform, i.e. one may be the full view of the other. This can
7723 -- only happen in the context of an access parameter, other uses
7724 -- of an incomplete Class_Wide_Type are illegal.
7726 if Is_Class_Wide_Type (Desig_1)
7728 Is_Class_Wide_Type (Desig_2)
7732 (Etype (Base_Type (Desig_1)),
7733 Etype (Base_Type (Desig_2)), Ctype);
7735 elsif Are_Anonymous_Access_To_Subprogram_Types then
7736 if Ada_Version < Ada_2005 then
7737 return Ctype = Type_Conformant
7739 Subtypes_Statically_Match (Desig_1, Desig_2);
7741 -- We must check the conformance of the signatures themselves
7745 Conformant : Boolean;
7748 (Desig_1, Desig_2, Ctype, False, Conformant);
7754 return Base_Type (Desig_1) = Base_Type (Desig_2)
7755 and then (Ctype = Type_Conformant
7757 Subtypes_Statically_Match (Desig_1, Desig_2));
7761 -- Otherwise definitely no match
7764 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7765 and then Is_Access_Type (Type_2))
7766 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7767 and then Is_Access_Type (Type_1)))
7770 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7772 May_Hide_Profile := True;
7777 end Conforming_Types;
7779 --------------------------
7780 -- Create_Extra_Formals --
7781 --------------------------
7783 procedure Create_Extra_Formals (E : Entity_Id) is
7785 First_Extra : Entity_Id := Empty;
7786 Last_Extra : Entity_Id;
7787 Formal_Type : Entity_Id;
7788 P_Formal : Entity_Id := Empty;
7790 function Add_Extra_Formal
7791 (Assoc_Entity : Entity_Id;
7794 Suffix : String) return Entity_Id;
7795 -- Add an extra formal to the current list of formals and extra formals.
7796 -- The extra formal is added to the end of the list of extra formals,
7797 -- and also returned as the result. These formals are always of mode IN.
7798 -- The new formal has the type Typ, is declared in Scope, and its name
7799 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7800 -- The following suffixes are currently used. They should not be changed
7801 -- without coordinating with CodePeer, which makes use of these to
7802 -- provide better messages.
7804 -- O denotes the Constrained bit.
7805 -- L denotes the accessibility level.
7806 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7807 -- the full list in exp_ch6.BIP_Formal_Kind.
7809 ----------------------
7810 -- Add_Extra_Formal --
7811 ----------------------
7813 function Add_Extra_Formal
7814 (Assoc_Entity : Entity_Id;
7817 Suffix : String) return Entity_Id
7819 EF : constant Entity_Id :=
7820 Make_Defining_Identifier (Sloc (Assoc_Entity),
7821 Chars => New_External_Name (Chars (Assoc_Entity),
7825 -- A little optimization. Never generate an extra formal for the
7826 -- _init operand of an initialization procedure, since it could
7829 if Chars (Formal) = Name_uInit then
7833 Set_Ekind (EF, E_In_Parameter);
7834 Set_Actual_Subtype (EF, Typ);
7835 Set_Etype (EF, Typ);
7836 Set_Scope (EF, Scope);
7837 Set_Mechanism (EF, Default_Mechanism);
7838 Set_Formal_Validity (EF);
7840 if No (First_Extra) then
7842 Set_Extra_Formals (Scope, First_Extra);
7845 if Present (Last_Extra) then
7846 Set_Extra_Formal (Last_Extra, EF);
7852 end Add_Extra_Formal;
7854 -- Start of processing for Create_Extra_Formals
7857 -- We never generate extra formals if expansion is not active because we
7858 -- don't need them unless we are generating code.
7860 if not Expander_Active then
7864 -- No need to generate extra formals in interface thunks whose target
7865 -- primitive has no extra formals.
7867 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7871 -- If this is a derived subprogram then the subtypes of the parent
7872 -- subprogram's formal parameters will be used to determine the need
7873 -- for extra formals.
7875 if Is_Overloadable (E) and then Present (Alias (E)) then
7876 P_Formal := First_Formal (Alias (E));
7879 Last_Extra := Empty;
7880 Formal := First_Formal (E);
7881 while Present (Formal) loop
7882 Last_Extra := Formal;
7883 Next_Formal (Formal);
7886 -- If Extra_formals were already created, don't do it again. This
7887 -- situation may arise for subprogram types created as part of
7888 -- dispatching calls (see Expand_Dispatching_Call)
7890 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7894 -- If the subprogram is a predefined dispatching subprogram then don't
7895 -- generate any extra constrained or accessibility level formals. In
7896 -- general we suppress these for internal subprograms (by not calling
7897 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7898 -- generated stream attributes do get passed through because extra
7899 -- build-in-place formals are needed in some cases (limited 'Input
).
7901 if Is_Predefined_Internal_Operation
(E
) then
7902 goto Test_For_Func_Result_Extras
;
7905 Formal
:= First_Formal
(E
);
7906 while Present
(Formal
) loop
7908 -- Create extra formal for supporting the attribute 'Constrained.
7909 -- The case of a private type view without discriminants also
7910 -- requires the extra formal if the underlying type has defaulted
7913 if Ekind
(Formal
) /= E_In_Parameter
then
7914 if Present
(P_Formal
) then
7915 Formal_Type
:= Etype
(P_Formal
);
7917 Formal_Type
:= Etype
(Formal
);
7920 -- Do not produce extra formals for Unchecked_Union parameters.
7921 -- Jump directly to the end of the loop.
7923 if Is_Unchecked_Union
(Base_Type
(Formal_Type
)) then
7924 goto Skip_Extra_Formal_Generation
;
7927 if not Has_Discriminants
(Formal_Type
)
7928 and then Ekind
(Formal_Type
) in Private_Kind
7929 and then Present
(Underlying_Type
(Formal_Type
))
7931 Formal_Type
:= Underlying_Type
(Formal_Type
);
7934 -- Suppress the extra formal if formal's subtype is constrained or
7935 -- indefinite, or we're compiling for Ada 2012 and the underlying
7936 -- type is tagged and limited. In Ada 2012, a limited tagged type
7937 -- can have defaulted discriminants, but 'Constrained is required
7938 -- to return True, so the formal is never needed (see AI05-0214).
7939 -- Note that this ensures consistency of calling sequences for
7940 -- dispatching operations when some types in a class have defaults
7941 -- on discriminants and others do not (and requiring the extra
7942 -- formal would introduce distributed overhead).
7944 -- If the type does not have a completion yet, treat as prior to
7945 -- Ada 2012 for consistency.
7947 if Has_Discriminants
(Formal_Type
)
7948 and then not Is_Constrained
(Formal_Type
)
7949 and then not Is_Indefinite_Subtype
(Formal_Type
)
7950 and then (Ada_Version
< Ada_2012
7951 or else No
(Underlying_Type
(Formal_Type
))
7953 (Is_Limited_Type
(Formal_Type
)
7956 (Underlying_Type
(Formal_Type
)))))
7958 Set_Extra_Constrained
7959 (Formal
, Add_Extra_Formal
(Formal
, Standard_Boolean
, E
, "O"));
7963 -- Create extra formal for supporting accessibility checking. This
7964 -- is done for both anonymous access formals and formals of named
7965 -- access types that are marked as controlling formals. The latter
7966 -- case can occur when Expand_Dispatching_Call creates a subprogram
7967 -- type and substitutes the types of access-to-class-wide actuals
7968 -- for the anonymous access-to-specific-type of controlling formals.
7969 -- Base_Type is applied because in cases where there is a null
7970 -- exclusion the formal may have an access subtype.
7972 -- This is suppressed if we specifically suppress accessibility
7973 -- checks at the package level for either the subprogram, or the
7974 -- package in which it resides. However, we do not suppress it
7975 -- simply if the scope has accessibility checks suppressed, since
7976 -- this could cause trouble when clients are compiled with a
7977 -- different suppression setting. The explicit checks at the
7978 -- package level are safe from this point of view.
7980 if (Ekind
(Base_Type
(Etype
(Formal
))) = E_Anonymous_Access_Type
7981 or else (Is_Controlling_Formal
(Formal
)
7982 and then Is_Access_Type
(Base_Type
(Etype
(Formal
)))))
7984 (Explicit_Suppress
(E
, Accessibility_Check
)
7986 Explicit_Suppress
(Scope
(E
), Accessibility_Check
))
7989 or else Present
(Extra_Accessibility
(P_Formal
)))
7991 Set_Extra_Accessibility
7992 (Formal
, Add_Extra_Formal
(Formal
, Standard_Natural
, E
, "L"));
7995 -- This label is required when skipping extra formal generation for
7996 -- Unchecked_Union parameters.
7998 <<Skip_Extra_Formal_Generation
>>
8000 if Present
(P_Formal
) then
8001 Next_Formal
(P_Formal
);
8004 Next_Formal
(Formal
);
8007 <<Test_For_Func_Result_Extras
>>
8009 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8010 -- function call is ... determined by the point of call ...".
8012 if Needs_Result_Accessibility_Level
(E
) then
8013 Set_Extra_Accessibility_Of_Result
8014 (E
, Add_Extra_Formal
(E
, Standard_Natural
, E
, "L"));
8017 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8018 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8020 if Ada_Version
>= Ada_2005
and then Is_Build_In_Place_Function
(E
) then
8022 Result_Subt
: constant Entity_Id
:= Etype
(E
);
8023 Full_Subt
: constant Entity_Id
:= Available_View
(Result_Subt
);
8024 Formal_Typ
: Entity_Id
;
8026 Discard
: Entity_Id
;
8027 pragma Warnings
(Off
, Discard
);
8030 -- In the case of functions with unconstrained result subtypes,
8031 -- add a 4-state formal indicating whether the return object is
8032 -- allocated by the caller (1), or should be allocated by the
8033 -- callee on the secondary stack (2), in the global heap (3), or
8034 -- in a user-defined storage pool (4). For the moment we just use
8035 -- Natural for the type of this formal. Note that this formal
8036 -- isn't usually needed in the case where the result subtype is
8037 -- constrained, but it is needed when the function has a tagged
8038 -- result, because generally such functions can be called in a
8039 -- dispatching context and such calls must be handled like calls
8040 -- to a class-wide function.
8042 if Needs_BIP_Alloc_Form
(E
) then
8045 (E
, Standard_Natural
,
8046 E
, BIP_Formal_Suffix
(BIP_Alloc_Form
));
8048 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8049 -- use a user-defined pool. This formal is not added on
8050 -- .NET/JVM/ZFP as those targets do not support pools.
8052 if VM_Target
= No_VM
8053 and then RTE_Available
(RE_Root_Storage_Pool_Ptr
)
8057 (E
, RTE
(RE_Root_Storage_Pool_Ptr
),
8058 E
, BIP_Formal_Suffix
(BIP_Storage_Pool
));
8062 -- In the case of functions whose result type needs finalization,
8063 -- add an extra formal which represents the finalization master.
8065 if Needs_BIP_Finalization_Master
(E
) then
8068 (E
, RTE
(RE_Finalization_Master_Ptr
),
8069 E
, BIP_Formal_Suffix
(BIP_Finalization_Master
));
8072 -- When the result type contains tasks, add two extra formals: the
8073 -- master of the tasks to be created, and the caller's activation
8076 if Has_Task
(Full_Subt
) then
8079 (E
, RTE
(RE_Master_Id
),
8080 E
, BIP_Formal_Suffix
(BIP_Task_Master
));
8083 (E
, RTE
(RE_Activation_Chain_Access
),
8084 E
, BIP_Formal_Suffix
(BIP_Activation_Chain
));
8087 -- All build-in-place functions get an extra formal that will be
8088 -- passed the address of the return object within the caller.
8091 Create_Itype
(E_Anonymous_Access_Type
, E
, Scope_Id
=> Scope
(E
));
8093 Set_Directly_Designated_Type
(Formal_Typ
, Result_Subt
);
8094 Set_Etype
(Formal_Typ
, Formal_Typ
);
8095 Set_Depends_On_Private
8096 (Formal_Typ
, Has_Private_Component
(Formal_Typ
));
8097 Set_Is_Public
(Formal_Typ
, Is_Public
(Scope
(Formal_Typ
)));
8098 Set_Is_Access_Constant
(Formal_Typ
, False);
8100 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8101 -- the designated type comes from the limited view (for back-end
8104 Set_From_With_Type
(Formal_Typ
, From_With_Type
(Result_Subt
));
8106 Layout_Type
(Formal_Typ
);
8110 (E
, Formal_Typ
, E
, BIP_Formal_Suffix
(BIP_Object_Access
));
8113 end Create_Extra_Formals
;
8115 -----------------------------
8116 -- Enter_Overloaded_Entity --
8117 -----------------------------
8119 procedure Enter_Overloaded_Entity
(S
: Entity_Id
) is
8120 E
: Entity_Id
:= Current_Entity_In_Scope
(S
);
8121 C_E
: Entity_Id
:= Current_Entity
(S
);
8125 Set_Has_Homonym
(E
);
8126 Set_Has_Homonym
(S
);
8129 Set_Is_Immediately_Visible
(S
);
8130 Set_Scope
(S
, Current_Scope
);
8132 -- Chain new entity if front of homonym in current scope, so that
8133 -- homonyms are contiguous.
8135 if Present
(E
) and then E
/= C_E
then
8136 while Homonym
(C_E
) /= E
loop
8137 C_E
:= Homonym
(C_E
);
8140 Set_Homonym
(C_E
, S
);
8144 Set_Current_Entity
(S
);
8149 if Is_Inherited_Operation
(S
) then
8150 Append_Inherited_Subprogram
(S
);
8152 Append_Entity
(S
, Current_Scope
);
8155 Set_Public_Status
(S
);
8157 if Debug_Flag_E
then
8158 Write_Str
("New overloaded entity chain: ");
8159 Write_Name
(Chars
(S
));
8162 while Present
(E
) loop
8163 Write_Str
(" "); Write_Int
(Int
(E
));
8170 -- Generate warning for hiding
8173 and then Comes_From_Source
(S
)
8174 and then In_Extended_Main_Source_Unit
(S
)
8181 -- Warn unless genuine overloading. Do not emit warning on
8182 -- hiding predefined operators in Standard (these are either an
8183 -- (artifact of our implicit declarations, or simple noise) but
8184 -- keep warning on a operator defined on a local subtype, because
8185 -- of the real danger that different operators may be applied in
8186 -- various parts of the program.
8188 -- Note that if E and S have the same scope, there is never any
8189 -- hiding. Either the two conflict, and the program is illegal,
8190 -- or S is overriding an implicit inherited subprogram.
8192 if Scope
(E
) /= Scope
(S
)
8193 and then (not Is_Overloadable
(E
)
8194 or else Subtype_Conformant
(E
, S
))
8195 and then (Is_Immediately_Visible
(E
)
8197 Is_Potentially_Use_Visible
(S
))
8199 if Scope
(E
) /= Standard_Standard
then
8200 Error_Msg_Sloc
:= Sloc
(E
);
8201 Error_Msg_N
("declaration of & hides one#?h?", S
);
8203 elsif Nkind
(S
) = N_Defining_Operator_Symbol
8205 Scope
(Base_Type
(Etype
(First_Formal
(S
)))) /= Scope
(S
)
8208 ("declaration of & hides predefined operator?h?", S
);
8213 end Enter_Overloaded_Entity
;
8215 -----------------------------
8216 -- Check_Untagged_Equality --
8217 -----------------------------
8219 procedure Check_Untagged_Equality
(Eq_Op
: Entity_Id
) is
8220 Typ
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
8221 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Eq_Op
);
8225 if Nkind
(Decl
) = N_Subprogram_Declaration
8226 and then Is_Record_Type
(Typ
)
8227 and then not Is_Tagged_Type
(Typ
)
8229 -- If the type is not declared in a package, or if we are in the
8230 -- body of the package or in some other scope, the new operation is
8231 -- not primitive, and therefore legal, though suspicious. If the
8232 -- type is a generic actual (sub)type, the operation is not primitive
8233 -- either because the base type is declared elsewhere.
8235 if Is_Frozen
(Typ
) then
8236 if Ekind
(Scope
(Typ
)) /= E_Package
8237 or else Scope
(Typ
) /= Current_Scope
8241 elsif Is_Generic_Actual_Type
(Typ
) then
8244 elsif In_Package_Body
(Scope
(Typ
)) then
8246 ("equality operator must be declared "
8247 & "before type& is frozen", Eq_Op
, Typ
);
8249 ("\move declaration to package spec", Eq_Op
);
8253 ("equality operator must be declared "
8254 & "before type& is frozen", Eq_Op
, Typ
);
8256 Obj_Decl
:= Next
(Parent
(Typ
));
8257 while Present
(Obj_Decl
) and then Obj_Decl
/= Decl
loop
8258 if Nkind
(Obj_Decl
) = N_Object_Declaration
8259 and then Etype
(Defining_Identifier
(Obj_Decl
)) = Typ
8262 ("type& is frozen by declaration??", Obj_Decl
, Typ
);
8264 ("\an equality operator cannot be declared after this "
8265 & "point (RM 4.5.2 (9.8)) (Ada 2012))??", Obj_Decl
);
8273 elsif not In_Same_List
(Parent
(Typ
), Decl
)
8274 and then not Is_Limited_Type
(Typ
)
8277 -- This makes it illegal to have a primitive equality declared in
8278 -- the private part if the type is visible.
8280 Error_Msg_N
("equality operator appears too late", Eq_Op
);
8283 end Check_Untagged_Equality
;
8285 -----------------------------
8286 -- Find_Corresponding_Spec --
8287 -----------------------------
8289 function Find_Corresponding_Spec
8291 Post_Error
: Boolean := True) return Entity_Id
8293 Spec
: constant Node_Id
:= Specification
(N
);
8294 Designator
: constant Entity_Id
:= Defining_Entity
(Spec
);
8298 function Different_Generic_Profile
(E
: Entity_Id
) return Boolean;
8299 -- Even if fully conformant, a body may depend on a generic actual when
8300 -- the spec does not, or vice versa, in which case they were distinct
8301 -- entities in the generic.
8303 -------------------------------
8304 -- Different_Generic_Profile --
8305 -------------------------------
8307 function Different_Generic_Profile
(E
: Entity_Id
) return Boolean is
8310 function Same_Generic_Actual
(T1
, T2
: Entity_Id
) return Boolean;
8311 -- Check that the types of corresponding formals have the same
8312 -- generic actual if any. We have to account for subtypes of a
8313 -- generic formal, declared between a spec and a body, which may
8314 -- appear distinct in an instance but matched in the generic.
8316 -------------------------
8317 -- Same_Generic_Actual --
8318 -------------------------
8320 function Same_Generic_Actual
(T1
, T2
: Entity_Id
) return Boolean is
8322 return Is_Generic_Actual_Type
(T1
) = Is_Generic_Actual_Type
(T2
)
8324 (Present
(Parent
(T1
))
8325 and then Comes_From_Source
(Parent
(T1
))
8326 and then Nkind
(Parent
(T1
)) = N_Subtype_Declaration
8327 and then Is_Entity_Name
(Subtype_Indication
(Parent
(T1
)))
8328 and then Entity
(Subtype_Indication
(Parent
(T1
))) = T2
);
8329 end Same_Generic_Actual
;
8331 -- Start of processing for Different_Generic_Profile
8334 if not In_Instance
then
8337 elsif Ekind
(E
) = E_Function
8338 and then not Same_Generic_Actual
(Etype
(E
), Etype
(Designator
))
8343 F1
:= First_Formal
(Designator
);
8344 F2
:= First_Formal
(E
);
8345 while Present
(F1
) loop
8346 if not Same_Generic_Actual
(Etype
(F1
), Etype
(F2
)) then
8355 end Different_Generic_Profile
;
8357 -- Start of processing for Find_Corresponding_Spec
8360 E
:= Current_Entity
(Designator
);
8361 while Present
(E
) loop
8363 -- We are looking for a matching spec. It must have the same scope,
8364 -- and the same name, and either be type conformant, or be the case
8365 -- of a library procedure spec and its body (which belong to one
8366 -- another regardless of whether they are type conformant or not).
8368 if Scope
(E
) = Current_Scope
then
8369 if Current_Scope
= Standard_Standard
8370 or else (Ekind
(E
) = Ekind
(Designator
)
8371 and then Type_Conformant
(E
, Designator
))
8373 -- Within an instantiation, we know that spec and body are
8374 -- subtype conformant, because they were subtype conformant in
8375 -- the generic. We choose the subtype-conformant entity here as
8376 -- well, to resolve spurious ambiguities in the instance that
8377 -- were not present in the generic (i.e. when two different
8378 -- types are given the same actual). If we are looking for a
8379 -- spec to match a body, full conformance is expected.
8382 Set_Convention
(Designator
, Convention
(E
));
8384 -- Skip past subprogram bodies and subprogram renamings that
8385 -- may appear to have a matching spec, but that aren't fully
8386 -- conformant with it. That can occur in cases where an
8387 -- actual type causes unrelated homographs in the instance.
8389 if Nkind_In
(N
, N_Subprogram_Body
,
8390 N_Subprogram_Renaming_Declaration
)
8391 and then Present
(Homonym
(E
))
8392 and then not Fully_Conformant
(Designator
, E
)
8396 elsif not Subtype_Conformant
(Designator
, E
) then
8399 elsif Different_Generic_Profile
(E
) then
8404 -- Ada 2012 (AI05-0165): For internally generated bodies of
8405 -- null procedures locate the internally generated spec. We
8406 -- enforce mode conformance since a tagged type may inherit
8407 -- from interfaces several null primitives which differ only
8408 -- in the mode of the formals.
8410 if not (Comes_From_Source
(E
))
8411 and then Is_Null_Procedure
(E
)
8412 and then not Mode_Conformant
(Designator
, E
)
8416 -- For null procedures coming from source that are completions,
8417 -- analysis of the generated body will establish the link.
8419 elsif Comes_From_Source
(E
)
8420 and then Nkind
(Spec
) = N_Procedure_Specification
8421 and then Null_Present
(Spec
)
8425 elsif not Has_Completion
(E
) then
8426 if Nkind
(N
) /= N_Subprogram_Body_Stub
then
8427 Set_Corresponding_Spec
(N
, E
);
8430 Set_Has_Completion
(E
);
8433 elsif Nkind
(Parent
(N
)) = N_Subunit
then
8435 -- If this is the proper body of a subunit, the completion
8436 -- flag is set when analyzing the stub.
8440 -- If E is an internal function with a controlling result that
8441 -- was created for an operation inherited by a null extension,
8442 -- it may be overridden by a body without a previous spec (one
8443 -- more reason why these should be shunned). In that case we
8444 -- remove the generated body if present, because the current
8445 -- one is the explicit overriding.
8447 elsif Ekind
(E
) = E_Function
8448 and then Ada_Version
>= Ada_2005
8449 and then not Comes_From_Source
(E
)
8450 and then Has_Controlling_Result
(E
)
8451 and then Is_Null_Extension
(Etype
(E
))
8452 and then Comes_From_Source
(Spec
)
8454 Set_Has_Completion
(E
, False);
8457 and then Nkind
(Parent
(E
)) = N_Function_Specification
8460 (Unit_Declaration_Node
8461 (Corresponding_Body
(Unit_Declaration_Node
(E
))));
8465 -- If expansion is disabled, or if the wrapper function has
8466 -- not been generated yet, this a late body overriding an
8467 -- inherited operation, or it is an overriding by some other
8468 -- declaration before the controlling result is frozen. In
8469 -- either case this is a declaration of a new entity.
8475 -- If the body already exists, then this is an error unless
8476 -- the previous declaration is the implicit declaration of a
8477 -- derived subprogram. It is also legal for an instance to
8478 -- contain type conformant overloadable declarations (but the
8479 -- generic declaration may not), per 8.3(26/2).
8481 elsif No
(Alias
(E
))
8482 and then not Is_Intrinsic_Subprogram
(E
)
8483 and then not In_Instance
8486 Error_Msg_Sloc
:= Sloc
(E
);
8488 if Is_Imported
(E
) then
8490 ("body not allowed for imported subprogram & declared#",
8493 Error_Msg_NE
("duplicate body for & declared#", N
, E
);
8497 -- Child units cannot be overloaded, so a conformance mismatch
8498 -- between body and a previous spec is an error.
8500 elsif Is_Child_Unit
(E
)
8502 Nkind
(Unit_Declaration_Node
(Designator
)) = N_Subprogram_Body
8504 Nkind
(Parent
(Unit_Declaration_Node
(Designator
))) =
8509 ("body of child unit does not match previous declaration", N
);
8517 -- On exit, we know that no previous declaration of subprogram exists
8520 end Find_Corresponding_Spec
;
8522 ----------------------
8523 -- Fully_Conformant --
8524 ----------------------
8526 function Fully_Conformant
(New_Id
, Old_Id
: Entity_Id
) return Boolean is
8529 Check_Conformance
(New_Id
, Old_Id
, Fully_Conformant
, False, Result
);
8531 end Fully_Conformant
;
8533 ----------------------------------
8534 -- Fully_Conformant_Expressions --
8535 ----------------------------------
8537 function Fully_Conformant_Expressions
8538 (Given_E1
: Node_Id
;
8539 Given_E2
: Node_Id
) return Boolean
8541 E1
: constant Node_Id
:= Original_Node
(Given_E1
);
8542 E2
: constant Node_Id
:= Original_Node
(Given_E2
);
8543 -- We always test conformance on original nodes, since it is possible
8544 -- for analysis and/or expansion to make things look as though they
8545 -- conform when they do not, e.g. by converting 1+2 into 3.
8547 function FCE
(Given_E1
, Given_E2
: Node_Id
) return Boolean
8548 renames Fully_Conformant_Expressions
;
8550 function FCL
(L1
, L2
: List_Id
) return Boolean;
8551 -- Compare elements of two lists for conformance. Elements have to be
8552 -- conformant, and actuals inserted as default parameters do not match
8553 -- explicit actuals with the same value.
8555 function FCO
(Op_Node
, Call_Node
: Node_Id
) return Boolean;
8556 -- Compare an operator node with a function call
8562 function FCL
(L1
, L2
: List_Id
) return Boolean is
8566 if L1
= No_List
then
8572 if L2
= No_List
then
8578 -- Compare two lists, skipping rewrite insertions (we want to compare
8579 -- the original trees, not the expanded versions!)
8582 if Is_Rewrite_Insertion
(N1
) then
8584 elsif Is_Rewrite_Insertion
(N2
) then
8590 elsif not FCE
(N1
, N2
) then
8603 function FCO
(Op_Node
, Call_Node
: Node_Id
) return Boolean is
8604 Actuals
: constant List_Id
:= Parameter_Associations
(Call_Node
);
8609 or else Entity
(Op_Node
) /= Entity
(Name
(Call_Node
))
8614 Act
:= First
(Actuals
);
8616 if Nkind
(Op_Node
) in N_Binary_Op
then
8617 if not FCE
(Left_Opnd
(Op_Node
), Act
) then
8624 return Present
(Act
)
8625 and then FCE
(Right_Opnd
(Op_Node
), Act
)
8626 and then No
(Next
(Act
));
8630 -- Start of processing for Fully_Conformant_Expressions
8633 -- Non-conformant if paren count does not match. Note: if some idiot
8634 -- complains that we don't do this right for more than 3 levels of
8635 -- parentheses, they will be treated with the respect they deserve!
8637 if Paren_Count
(E1
) /= Paren_Count
(E2
) then
8640 -- If same entities are referenced, then they are conformant even if
8641 -- they have different forms (RM 8.3.1(19-20)).
8643 elsif Is_Entity_Name
(E1
) and then Is_Entity_Name
(E2
) then
8644 if Present
(Entity
(E1
)) then
8645 return Entity
(E1
) = Entity
(E2
)
8646 or else (Chars
(Entity
(E1
)) = Chars
(Entity
(E2
))
8647 and then Ekind
(Entity
(E1
)) = E_Discriminant
8648 and then Ekind
(Entity
(E2
)) = E_In_Parameter
);
8650 elsif Nkind
(E1
) = N_Expanded_Name
8651 and then Nkind
(E2
) = N_Expanded_Name
8652 and then Nkind
(Selector_Name
(E1
)) = N_Character_Literal
8653 and then Nkind
(Selector_Name
(E2
)) = N_Character_Literal
8655 return Chars
(Selector_Name
(E1
)) = Chars
(Selector_Name
(E2
));
8658 -- Identifiers in component associations don't always have
8659 -- entities, but their names must conform.
8661 return Nkind
(E1
) = N_Identifier
8662 and then Nkind
(E2
) = N_Identifier
8663 and then Chars
(E1
) = Chars
(E2
);
8666 elsif Nkind
(E1
) = N_Character_Literal
8667 and then Nkind
(E2
) = N_Expanded_Name
8669 return Nkind
(Selector_Name
(E2
)) = N_Character_Literal
8670 and then Chars
(E1
) = Chars
(Selector_Name
(E2
));
8672 elsif Nkind
(E2
) = N_Character_Literal
8673 and then Nkind
(E1
) = N_Expanded_Name
8675 return Nkind
(Selector_Name
(E1
)) = N_Character_Literal
8676 and then Chars
(E2
) = Chars
(Selector_Name
(E1
));
8678 elsif Nkind
(E1
) in N_Op
and then Nkind
(E2
) = N_Function_Call
then
8679 return FCO
(E1
, E2
);
8681 elsif Nkind
(E2
) in N_Op
and then Nkind
(E1
) = N_Function_Call
then
8682 return FCO
(E2
, E1
);
8684 -- Otherwise we must have the same syntactic entity
8686 elsif Nkind
(E1
) /= Nkind
(E2
) then
8689 -- At this point, we specialize by node type
8696 FCL
(Expressions
(E1
), Expressions
(E2
))
8698 FCL
(Component_Associations
(E1
),
8699 Component_Associations
(E2
));
8702 if Nkind
(Expression
(E1
)) = N_Qualified_Expression
8704 Nkind
(Expression
(E2
)) = N_Qualified_Expression
8706 return FCE
(Expression
(E1
), Expression
(E2
));
8708 -- Check that the subtype marks and any constraints
8713 Indic1
: constant Node_Id
:= Expression
(E1
);
8714 Indic2
: constant Node_Id
:= Expression
(E2
);
8719 if Nkind
(Indic1
) /= N_Subtype_Indication
then
8721 Nkind
(Indic2
) /= N_Subtype_Indication
8722 and then Entity
(Indic1
) = Entity
(Indic2
);
8724 elsif Nkind
(Indic2
) /= N_Subtype_Indication
then
8726 Nkind
(Indic1
) /= N_Subtype_Indication
8727 and then Entity
(Indic1
) = Entity
(Indic2
);
8730 if Entity
(Subtype_Mark
(Indic1
)) /=
8731 Entity
(Subtype_Mark
(Indic2
))
8736 Elt1
:= First
(Constraints
(Constraint
(Indic1
)));
8737 Elt2
:= First
(Constraints
(Constraint
(Indic2
)));
8738 while Present
(Elt1
) and then Present
(Elt2
) loop
8739 if not FCE
(Elt1
, Elt2
) then
8752 when N_Attribute_Reference
=>
8754 Attribute_Name
(E1
) = Attribute_Name
(E2
)
8755 and then FCL
(Expressions
(E1
), Expressions
(E2
));
8759 Entity
(E1
) = Entity
(E2
)
8760 and then FCE
(Left_Opnd
(E1
), Left_Opnd
(E2
))
8761 and then FCE
(Right_Opnd
(E1
), Right_Opnd
(E2
));
8763 when N_Short_Circuit | N_Membership_Test
=>
8765 FCE
(Left_Opnd
(E1
), Left_Opnd
(E2
))
8767 FCE
(Right_Opnd
(E1
), Right_Opnd
(E2
));
8769 when N_Case_Expression
=>
8775 if not FCE
(Expression
(E1
), Expression
(E2
)) then
8779 Alt1
:= First
(Alternatives
(E1
));
8780 Alt2
:= First
(Alternatives
(E2
));
8782 if Present
(Alt1
) /= Present
(Alt2
) then
8784 elsif No
(Alt1
) then
8788 if not FCE
(Expression
(Alt1
), Expression
(Alt2
))
8789 or else not FCL
(Discrete_Choices
(Alt1
),
8790 Discrete_Choices
(Alt2
))
8801 when N_Character_Literal
=>
8803 Char_Literal_Value
(E1
) = Char_Literal_Value
(E2
);
8805 when N_Component_Association
=>
8807 FCL
(Choices
(E1
), Choices
(E2
))
8809 FCE
(Expression
(E1
), Expression
(E2
));
8811 when N_Explicit_Dereference
=>
8813 FCE
(Prefix
(E1
), Prefix
(E2
));
8815 when N_Extension_Aggregate
=>
8817 FCL
(Expressions
(E1
), Expressions
(E2
))
8818 and then Null_Record_Present
(E1
) =
8819 Null_Record_Present
(E2
)
8820 and then FCL
(Component_Associations
(E1
),
8821 Component_Associations
(E2
));
8823 when N_Function_Call
=>
8825 FCE
(Name
(E1
), Name
(E2
))
8827 FCL
(Parameter_Associations
(E1
),
8828 Parameter_Associations
(E2
));
8830 when N_If_Expression
=>
8832 FCL
(Expressions
(E1
), Expressions
(E2
));
8834 when N_Indexed_Component
=>
8836 FCE
(Prefix
(E1
), Prefix
(E2
))
8838 FCL
(Expressions
(E1
), Expressions
(E2
));
8840 when N_Integer_Literal
=>
8841 return (Intval
(E1
) = Intval
(E2
));
8846 when N_Operator_Symbol
=>
8848 Chars
(E1
) = Chars
(E2
);
8850 when N_Others_Choice
=>
8853 when N_Parameter_Association
=>
8855 Chars
(Selector_Name
(E1
)) = Chars
(Selector_Name
(E2
))
8856 and then FCE
(Explicit_Actual_Parameter
(E1
),
8857 Explicit_Actual_Parameter
(E2
));
8859 when N_Qualified_Expression
=>
8861 FCE
(Subtype_Mark
(E1
), Subtype_Mark
(E2
))
8863 FCE
(Expression
(E1
), Expression
(E2
));
8865 when N_Quantified_Expression
=>
8866 if not FCE
(Condition
(E1
), Condition
(E2
)) then
8870 if Present
(Loop_Parameter_Specification
(E1
))
8871 and then Present
(Loop_Parameter_Specification
(E2
))
8874 L1
: constant Node_Id
:=
8875 Loop_Parameter_Specification
(E1
);
8876 L2
: constant Node_Id
:=
8877 Loop_Parameter_Specification
(E2
);
8881 Reverse_Present
(L1
) = Reverse_Present
(L2
)
8883 FCE
(Defining_Identifier
(L1
),
8884 Defining_Identifier
(L2
))
8886 FCE
(Discrete_Subtype_Definition
(L1
),
8887 Discrete_Subtype_Definition
(L2
));
8890 elsif Present
(Iterator_Specification
(E1
))
8891 and then Present
(Iterator_Specification
(E2
))
8894 I1
: constant Node_Id
:= Iterator_Specification
(E1
);
8895 I2
: constant Node_Id
:= Iterator_Specification
(E2
);
8899 FCE
(Defining_Identifier
(I1
),
8900 Defining_Identifier
(I2
))
8902 Of_Present
(I1
) = Of_Present
(I2
)
8904 Reverse_Present
(I1
) = Reverse_Present
(I2
)
8905 and then FCE
(Name
(I1
), Name
(I2
))
8906 and then FCE
(Subtype_Indication
(I1
),
8907 Subtype_Indication
(I2
));
8910 -- The quantified expressions used different specifications to
8911 -- walk their respective ranges.
8919 FCE
(Low_Bound
(E1
), Low_Bound
(E2
))
8921 FCE
(High_Bound
(E1
), High_Bound
(E2
));
8923 when N_Real_Literal
=>
8924 return (Realval
(E1
) = Realval
(E2
));
8926 when N_Selected_Component
=>
8928 FCE
(Prefix
(E1
), Prefix
(E2
))
8930 FCE
(Selector_Name
(E1
), Selector_Name
(E2
));
8934 FCE
(Prefix
(E1
), Prefix
(E2
))
8936 FCE
(Discrete_Range
(E1
), Discrete_Range
(E2
));
8938 when N_String_Literal
=>
8940 S1
: constant String_Id
:= Strval
(E1
);
8941 S2
: constant String_Id
:= Strval
(E2
);
8942 L1
: constant Nat
:= String_Length
(S1
);
8943 L2
: constant Nat
:= String_Length
(S2
);
8950 for J
in 1 .. L1
loop
8951 if Get_String_Char
(S1
, J
) /=
8952 Get_String_Char
(S2
, J
)
8962 when N_Type_Conversion
=>
8964 FCE
(Subtype_Mark
(E1
), Subtype_Mark
(E2
))
8966 FCE
(Expression
(E1
), Expression
(E2
));
8970 Entity
(E1
) = Entity
(E2
)
8972 FCE
(Right_Opnd
(E1
), Right_Opnd
(E2
));
8974 when N_Unchecked_Type_Conversion
=>
8976 FCE
(Subtype_Mark
(E1
), Subtype_Mark
(E2
))
8978 FCE
(Expression
(E1
), Expression
(E2
));
8980 -- All other node types cannot appear in this context. Strictly
8981 -- we should raise a fatal internal error. Instead we just ignore
8982 -- the nodes. This means that if anyone makes a mistake in the
8983 -- expander and mucks an expression tree irretrievably, the result
8984 -- will be a failure to detect a (probably very obscure) case
8985 -- of non-conformance, which is better than bombing on some
8986 -- case where two expressions do in fact conform.
8993 end Fully_Conformant_Expressions
;
8995 ----------------------------------------
8996 -- Fully_Conformant_Discrete_Subtypes --
8997 ----------------------------------------
8999 function Fully_Conformant_Discrete_Subtypes
9000 (Given_S1
: Node_Id
;
9001 Given_S2
: Node_Id
) return Boolean
9003 S1
: constant Node_Id
:= Original_Node
(Given_S1
);
9004 S2
: constant Node_Id
:= Original_Node
(Given_S2
);
9006 function Conforming_Bounds
(B1
, B2
: Node_Id
) return Boolean;
9007 -- Special-case for a bound given by a discriminant, which in the body
9008 -- is replaced with the discriminal of the enclosing type.
9010 function Conforming_Ranges
(R1
, R2
: Node_Id
) return Boolean;
9011 -- Check both bounds
9013 -----------------------
9014 -- Conforming_Bounds --
9015 -----------------------
9017 function Conforming_Bounds
(B1
, B2
: Node_Id
) return Boolean is
9019 if Is_Entity_Name
(B1
)
9020 and then Is_Entity_Name
(B2
)
9021 and then Ekind
(Entity
(B1
)) = E_Discriminant
9023 return Chars
(B1
) = Chars
(B2
);
9026 return Fully_Conformant_Expressions
(B1
, B2
);
9028 end Conforming_Bounds
;
9030 -----------------------
9031 -- Conforming_Ranges --
9032 -----------------------
9034 function Conforming_Ranges
(R1
, R2
: Node_Id
) return Boolean is
9037 Conforming_Bounds
(Low_Bound
(R1
), Low_Bound
(R2
))
9039 Conforming_Bounds
(High_Bound
(R1
), High_Bound
(R2
));
9040 end Conforming_Ranges
;
9042 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9045 if Nkind
(S1
) /= Nkind
(S2
) then
9048 elsif Is_Entity_Name
(S1
) then
9049 return Entity
(S1
) = Entity
(S2
);
9051 elsif Nkind
(S1
) = N_Range
then
9052 return Conforming_Ranges
(S1
, S2
);
9054 elsif Nkind
(S1
) = N_Subtype_Indication
then
9056 Entity
(Subtype_Mark
(S1
)) = Entity
(Subtype_Mark
(S2
))
9059 (Range_Expression
(Constraint
(S1
)),
9060 Range_Expression
(Constraint
(S2
)));
9064 end Fully_Conformant_Discrete_Subtypes
;
9066 --------------------
9067 -- Install_Entity --
9068 --------------------
9070 procedure Install_Entity
(E
: Entity_Id
) is
9071 Prev
: constant Entity_Id
:= Current_Entity
(E
);
9073 Set_Is_Immediately_Visible
(E
);
9074 Set_Current_Entity
(E
);
9075 Set_Homonym
(E
, Prev
);
9078 ---------------------
9079 -- Install_Formals --
9080 ---------------------
9082 procedure Install_Formals
(Id
: Entity_Id
) is
9085 F
:= First_Formal
(Id
);
9086 while Present
(F
) loop
9090 end Install_Formals
;
9092 -----------------------------
9093 -- Is_Interface_Conformant --
9094 -----------------------------
9096 function Is_Interface_Conformant
9097 (Tagged_Type
: Entity_Id
;
9098 Iface_Prim
: Entity_Id
;
9099 Prim
: Entity_Id
) return Boolean
9101 Iface
: constant Entity_Id
:= Find_Dispatching_Type
(Iface_Prim
);
9102 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Prim
);
9104 function Controlling_Formal
(Prim
: Entity_Id
) return Entity_Id
;
9105 -- Return the controlling formal of Prim
9107 ------------------------
9108 -- Controlling_Formal --
9109 ------------------------
9111 function Controlling_Formal
(Prim
: Entity_Id
) return Entity_Id
is
9112 E
: Entity_Id
:= First_Entity
(Prim
);
9115 while Present
(E
) loop
9116 if Is_Formal
(E
) and then Is_Controlling_Formal
(E
) then
9124 end Controlling_Formal
;
9128 Iface_Ctrl_F
: constant Entity_Id
:= Controlling_Formal
(Iface_Prim
);
9129 Prim_Ctrl_F
: constant Entity_Id
:= Controlling_Formal
(Prim
);
9131 -- Start of processing for Is_Interface_Conformant
9134 pragma Assert
(Is_Subprogram
(Iface_Prim
)
9135 and then Is_Subprogram
(Prim
)
9136 and then Is_Dispatching_Operation
(Iface_Prim
)
9137 and then Is_Dispatching_Operation
(Prim
));
9139 pragma Assert
(Is_Interface
(Iface
)
9140 or else (Present
(Alias
(Iface_Prim
))
9143 (Find_Dispatching_Type
(Ultimate_Alias
(Iface_Prim
)))));
9145 if Prim
= Iface_Prim
9146 or else not Is_Subprogram
(Prim
)
9147 or else Ekind
(Prim
) /= Ekind
(Iface_Prim
)
9148 or else not Is_Dispatching_Operation
(Prim
)
9149 or else Scope
(Prim
) /= Scope
(Tagged_Type
)
9151 or else Base_Type
(Typ
) /= Base_Type
(Tagged_Type
)
9152 or else not Primitive_Names_Match
(Iface_Prim
, Prim
)
9156 -- The mode of the controlling formals must match
9158 elsif Present
(Iface_Ctrl_F
)
9159 and then Present
(Prim_Ctrl_F
)
9160 and then Ekind
(Iface_Ctrl_F
) /= Ekind
(Prim_Ctrl_F
)
9164 -- Case of a procedure, or a function whose result type matches the
9165 -- result type of the interface primitive, or a function that has no
9166 -- controlling result (I or access I).
9168 elsif Ekind
(Iface_Prim
) = E_Procedure
9169 or else Etype
(Prim
) = Etype
(Iface_Prim
)
9170 or else not Has_Controlling_Result
(Prim
)
9172 return Type_Conformant
9173 (Iface_Prim
, Prim
, Skip_Controlling_Formals
=> True);
9175 -- Case of a function returning an interface, or an access to one. Check
9176 -- that the return types correspond.
9178 elsif Implements_Interface
(Typ
, Iface
) then
9179 if (Ekind
(Etype
(Prim
)) = E_Anonymous_Access_Type
)
9181 (Ekind
(Etype
(Iface_Prim
)) = E_Anonymous_Access_Type
)
9186 Type_Conformant
(Prim
, Iface_Prim
,
9187 Skip_Controlling_Formals
=> True);
9193 end Is_Interface_Conformant
;
9195 ---------------------------------
9196 -- Is_Non_Overriding_Operation --
9197 ---------------------------------
9199 function Is_Non_Overriding_Operation
9200 (Prev_E
: Entity_Id
;
9201 New_E
: Entity_Id
) return Boolean
9205 G_Typ
: Entity_Id
:= Empty
;
9207 function Get_Generic_Parent_Type
(F_Typ
: Entity_Id
) return Entity_Id
;
9208 -- If F_Type is a derived type associated with a generic actual subtype,
9209 -- then return its Generic_Parent_Type attribute, else return Empty.
9211 function Types_Correspond
9212 (P_Type
: Entity_Id
;
9213 N_Type
: Entity_Id
) return Boolean;
9214 -- Returns true if and only if the types (or designated types in the
9215 -- case of anonymous access types) are the same or N_Type is derived
9216 -- directly or indirectly from P_Type.
9218 -----------------------------
9219 -- Get_Generic_Parent_Type --
9220 -----------------------------
9222 function Get_Generic_Parent_Type
(F_Typ
: Entity_Id
) return Entity_Id
is
9228 if Is_Derived_Type
(F_Typ
)
9229 and then Nkind
(Parent
(F_Typ
)) = N_Full_Type_Declaration
9231 -- The tree must be traversed to determine the parent subtype in
9232 -- the generic unit, which unfortunately isn't always available
9233 -- via semantic attributes. ??? (Note: The use of Original_Node
9234 -- is needed for cases where a full derived type has been
9237 Defn
:= Type_Definition
(Original_Node
(Parent
(F_Typ
)));
9238 if Nkind
(Defn
) = N_Derived_Type_Definition
then
9239 Indic
:= Subtype_Indication
(Defn
);
9241 if Nkind
(Indic
) = N_Subtype_Indication
then
9242 G_Typ
:= Entity
(Subtype_Mark
(Indic
));
9244 G_Typ
:= Entity
(Indic
);
9247 if Nkind
(Parent
(G_Typ
)) = N_Subtype_Declaration
9248 and then Present
(Generic_Parent_Type
(Parent
(G_Typ
)))
9250 return Generic_Parent_Type
(Parent
(G_Typ
));
9256 end Get_Generic_Parent_Type
;
9258 ----------------------
9259 -- Types_Correspond --
9260 ----------------------
9262 function Types_Correspond
9263 (P_Type
: Entity_Id
;
9264 N_Type
: Entity_Id
) return Boolean
9266 Prev_Type
: Entity_Id
:= Base_Type
(P_Type
);
9267 New_Type
: Entity_Id
:= Base_Type
(N_Type
);
9270 if Ekind
(Prev_Type
) = E_Anonymous_Access_Type
then
9271 Prev_Type
:= Designated_Type
(Prev_Type
);
9274 if Ekind
(New_Type
) = E_Anonymous_Access_Type
then
9275 New_Type
:= Designated_Type
(New_Type
);
9278 if Prev_Type
= New_Type
then
9281 elsif not Is_Class_Wide_Type
(New_Type
) then
9282 while Etype
(New_Type
) /= New_Type
loop
9283 New_Type
:= Etype
(New_Type
);
9284 if New_Type
= Prev_Type
then
9290 end Types_Correspond
;
9292 -- Start of processing for Is_Non_Overriding_Operation
9295 -- In the case where both operations are implicit derived subprograms
9296 -- then neither overrides the other. This can only occur in certain
9297 -- obscure cases (e.g., derivation from homographs created in a generic
9300 if Present
(Alias
(Prev_E
)) and then Present
(Alias
(New_E
)) then
9303 elsif Ekind
(Current_Scope
) = E_Package
9304 and then Is_Generic_Instance
(Current_Scope
)
9305 and then In_Private_Part
(Current_Scope
)
9306 and then Comes_From_Source
(New_E
)
9308 -- We examine the formals and result type of the inherited operation,
9309 -- to determine whether their type is derived from (the instance of)
9310 -- a generic type. The first such formal or result type is the one
9313 Formal
:= First_Formal
(Prev_E
);
9314 while Present
(Formal
) loop
9315 F_Typ
:= Base_Type
(Etype
(Formal
));
9317 if Ekind
(F_Typ
) = E_Anonymous_Access_Type
then
9318 F_Typ
:= Designated_Type
(F_Typ
);
9321 G_Typ
:= Get_Generic_Parent_Type
(F_Typ
);
9322 exit when Present
(G_Typ
);
9324 Next_Formal
(Formal
);
9327 if No
(G_Typ
) and then Ekind
(Prev_E
) = E_Function
then
9328 G_Typ
:= Get_Generic_Parent_Type
(Base_Type
(Etype
(Prev_E
)));
9335 -- If the generic type is a private type, then the original operation
9336 -- was not overriding in the generic, because there was no primitive
9337 -- operation to override.
9339 if Nkind
(Parent
(G_Typ
)) = N_Formal_Type_Declaration
9340 and then Nkind
(Formal_Type_Definition
(Parent
(G_Typ
))) =
9341 N_Formal_Private_Type_Definition
9345 -- The generic parent type is the ancestor of a formal derived
9346 -- type declaration. We need to check whether it has a primitive
9347 -- operation that should be overridden by New_E in the generic.
9351 P_Formal
: Entity_Id
;
9352 N_Formal
: Entity_Id
;
9356 Prim_Elt
: Elmt_Id
:= First_Elmt
(Primitive_Operations
(G_Typ
));
9359 while Present
(Prim_Elt
) loop
9360 P_Prim
:= Node
(Prim_Elt
);
9362 if Chars
(P_Prim
) = Chars
(New_E
)
9363 and then Ekind
(P_Prim
) = Ekind
(New_E
)
9365 P_Formal
:= First_Formal
(P_Prim
);
9366 N_Formal
:= First_Formal
(New_E
);
9367 while Present
(P_Formal
) and then Present
(N_Formal
) loop
9368 P_Typ
:= Etype
(P_Formal
);
9369 N_Typ
:= Etype
(N_Formal
);
9371 if not Types_Correspond
(P_Typ
, N_Typ
) then
9375 Next_Entity
(P_Formal
);
9376 Next_Entity
(N_Formal
);
9379 -- Found a matching primitive operation belonging to the
9380 -- formal ancestor type, so the new subprogram is
9384 and then No
(N_Formal
)
9385 and then (Ekind
(New_E
) /= E_Function
9388 (Etype
(P_Prim
), Etype
(New_E
)))
9394 Next_Elmt
(Prim_Elt
);
9397 -- If no match found, then the new subprogram does not override
9398 -- in the generic (nor in the instance).
9400 -- If the type in question is not abstract, and the subprogram
9401 -- is, this will be an error if the new operation is in the
9402 -- private part of the instance. Emit a warning now, which will
9403 -- make the subsequent error message easier to understand.
9405 if not Is_Abstract_Type
(F_Typ
)
9406 and then Is_Abstract_Subprogram
(Prev_E
)
9407 and then In_Private_Part
(Current_Scope
)
9409 Error_Msg_Node_2
:= F_Typ
;
9411 ("private operation& in generic unit does not override " &
9412 "any primitive operation of& (RM 12.3 (18))??",
9422 end Is_Non_Overriding_Operation
;
9424 -------------------------------------
9425 -- List_Inherited_Pre_Post_Aspects --
9426 -------------------------------------
9428 procedure List_Inherited_Pre_Post_Aspects
(E
: Entity_Id
) is
9430 if Opt
.List_Inherited_Aspects
9431 and then (Is_Subprogram
(E
) or else Is_Generic_Subprogram
(E
))
9434 Inherited
: constant Subprogram_List
:= Inherited_Subprograms
(E
);
9438 for J
in Inherited
'Range loop
9439 P
:= Pre_Post_Conditions
(Contract
(Inherited
(J
)));
9440 while Present
(P
) loop
9441 Error_Msg_Sloc
:= Sloc
(P
);
9443 if Class_Present
(P
) and then not Split_PPC
(P
) then
9444 if Pragma_Name
(P
) = Name_Precondition
then
9446 ("info: & inherits `Pre''Class` aspect from #?L?",
9450 ("info: & inherits `Post''Class` aspect from #?L?",
9455 P
:= Next_Pragma
(P
);
9460 end List_Inherited_Pre_Post_Aspects
;
9462 ------------------------------
9463 -- Make_Inequality_Operator --
9464 ------------------------------
9466 -- S is the defining identifier of an equality operator. We build a
9467 -- subprogram declaration with the right signature. This operation is
9468 -- intrinsic, because it is always expanded as the negation of the
9469 -- call to the equality function.
9471 procedure Make_Inequality_Operator
(S
: Entity_Id
) is
9472 Loc
: constant Source_Ptr
:= Sloc
(S
);
9475 Op_Name
: Entity_Id
;
9477 FF
: constant Entity_Id
:= First_Formal
(S
);
9478 NF
: constant Entity_Id
:= Next_Formal
(FF
);
9481 -- Check that equality was properly defined, ignore call if not
9488 A
: constant Entity_Id
:=
9489 Make_Defining_Identifier
(Sloc
(FF
),
9490 Chars
=> Chars
(FF
));
9492 B
: constant Entity_Id
:=
9493 Make_Defining_Identifier
(Sloc
(NF
),
9494 Chars
=> Chars
(NF
));
9497 Op_Name
:= Make_Defining_Operator_Symbol
(Loc
, Name_Op_Ne
);
9499 Formals
:= New_List
(
9500 Make_Parameter_Specification
(Loc
,
9501 Defining_Identifier
=> A
,
9503 New_Reference_To
(Etype
(First_Formal
(S
)),
9504 Sloc
(Etype
(First_Formal
(S
))))),
9506 Make_Parameter_Specification
(Loc
,
9507 Defining_Identifier
=> B
,
9509 New_Reference_To
(Etype
(Next_Formal
(First_Formal
(S
))),
9510 Sloc
(Etype
(Next_Formal
(First_Formal
(S
)))))));
9513 Make_Subprogram_Declaration
(Loc
,
9515 Make_Function_Specification
(Loc
,
9516 Defining_Unit_Name
=> Op_Name
,
9517 Parameter_Specifications
=> Formals
,
9518 Result_Definition
=>
9519 New_Reference_To
(Standard_Boolean
, Loc
)));
9521 -- Insert inequality right after equality if it is explicit or after
9522 -- the derived type when implicit. These entities are created only
9523 -- for visibility purposes, and eventually replaced in the course
9524 -- of expansion, so they do not need to be attached to the tree and
9525 -- seen by the back-end. Keeping them internal also avoids spurious
9526 -- freezing problems. The declaration is inserted in the tree for
9527 -- analysis, and removed afterwards. If the equality operator comes
9528 -- from an explicit declaration, attach the inequality immediately
9529 -- after. Else the equality is inherited from a derived type
9530 -- declaration, so insert inequality after that declaration.
9532 if No
(Alias
(S
)) then
9533 Insert_After
(Unit_Declaration_Node
(S
), Decl
);
9534 elsif Is_List_Member
(Parent
(S
)) then
9535 Insert_After
(Parent
(S
), Decl
);
9537 Insert_After
(Parent
(Etype
(First_Formal
(S
))), Decl
);
9540 Mark_Rewrite_Insertion
(Decl
);
9541 Set_Is_Intrinsic_Subprogram
(Op_Name
);
9544 Set_Has_Completion
(Op_Name
);
9545 Set_Corresponding_Equality
(Op_Name
, S
);
9546 Set_Is_Abstract_Subprogram
(Op_Name
, Is_Abstract_Subprogram
(S
));
9548 end Make_Inequality_Operator
;
9550 ----------------------
9551 -- May_Need_Actuals --
9552 ----------------------
9554 procedure May_Need_Actuals
(Fun
: Entity_Id
) is
9559 F
:= First_Formal
(Fun
);
9561 while Present
(F
) loop
9562 if No
(Default_Value
(F
)) then
9570 Set_Needs_No_Actuals
(Fun
, B
);
9571 end May_Need_Actuals
;
9573 ---------------------
9574 -- Mode_Conformant --
9575 ---------------------
9577 function Mode_Conformant
(New_Id
, Old_Id
: Entity_Id
) return Boolean is
9580 Check_Conformance
(New_Id
, Old_Id
, Mode_Conformant
, False, Result
);
9582 end Mode_Conformant
;
9584 ---------------------------
9585 -- New_Overloaded_Entity --
9586 ---------------------------
9588 procedure New_Overloaded_Entity
9590 Derived_Type
: Entity_Id
:= Empty
)
9592 Overridden_Subp
: Entity_Id
:= Empty
;
9593 -- Set if the current scope has an operation that is type-conformant
9594 -- with S, and becomes hidden by S.
9596 Is_Primitive_Subp
: Boolean;
9597 -- Set to True if the new subprogram is primitive
9600 -- Entity that S overrides
9602 Prev_Vis
: Entity_Id
:= Empty
;
9603 -- Predecessor of E in Homonym chain
9605 procedure Check_For_Primitive_Subprogram
9606 (Is_Primitive
: out Boolean;
9607 Is_Overriding
: Boolean := False);
9608 -- If the subprogram being analyzed is a primitive operation of the type
9609 -- of a formal or result, set the Has_Primitive_Operations flag on the
9610 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9611 -- corresponding flag on the entity itself for later use.
9613 procedure Check_Synchronized_Overriding
9614 (Def_Id
: Entity_Id
;
9615 Overridden_Subp
: out Entity_Id
);
9616 -- First determine if Def_Id is an entry or a subprogram either defined
9617 -- in the scope of a task or protected type, or is a primitive of such
9618 -- a type. Check whether Def_Id overrides a subprogram of an interface
9619 -- implemented by the synchronized type, return the overridden entity
9622 function Is_Private_Declaration
(E
: Entity_Id
) return Boolean;
9623 -- Check that E is declared in the private part of the current package,
9624 -- or in the package body, where it may hide a previous declaration.
9625 -- We can't use In_Private_Part by itself because this flag is also
9626 -- set when freezing entities, so we must examine the place of the
9627 -- declaration in the tree, and recognize wrapper packages as well.
9629 function Is_Overriding_Alias
9631 New_E
: Entity_Id
) return Boolean;
9632 -- Check whether new subprogram and old subprogram are both inherited
9633 -- from subprograms that have distinct dispatch table entries. This can
9634 -- occur with derivations from instances with accidental homonyms. The
9635 -- function is conservative given that the converse is only true within
9636 -- instances that contain accidental overloadings.
9638 ------------------------------------
9639 -- Check_For_Primitive_Subprogram --
9640 ------------------------------------
9642 procedure Check_For_Primitive_Subprogram
9643 (Is_Primitive
: out Boolean;
9644 Is_Overriding
: Boolean := False)
9650 function Visible_Part_Type
(T
: Entity_Id
) return Boolean;
9651 -- Returns true if T is declared in the visible part of the current
9652 -- package scope; otherwise returns false. Assumes that T is declared
9655 procedure Check_Private_Overriding
(T
: Entity_Id
);
9656 -- Checks that if a primitive abstract subprogram of a visible
9657 -- abstract type is declared in a private part, then it must override
9658 -- an abstract subprogram declared in the visible part. Also checks
9659 -- that if a primitive function with a controlling result is declared
9660 -- in a private part, then it must override a function declared in
9661 -- the visible part.
9663 ------------------------------
9664 -- Check_Private_Overriding --
9665 ------------------------------
9667 procedure Check_Private_Overriding
(T
: Entity_Id
) is
9669 if Is_Package_Or_Generic_Package
(Current_Scope
)
9670 and then In_Private_Part
(Current_Scope
)
9671 and then Visible_Part_Type
(T
)
9672 and then not In_Instance
9674 if Is_Abstract_Type
(T
)
9675 and then Is_Abstract_Subprogram
(S
)
9676 and then (not Is_Overriding
9677 or else not Is_Abstract_Subprogram
(E
))
9680 ("abstract subprograms must be visible "
9681 & "(RM 3.9.3(10))!", S
);
9683 elsif Ekind
(S
) = E_Function
and then not Is_Overriding
then
9684 if Is_Tagged_Type
(T
) and then T
= Base_Type
(Etype
(S
)) then
9686 ("private function with tagged result must"
9687 & " override visible-part function", S
);
9689 ("\move subprogram to the visible part"
9690 & " (RM 3.9.3(10))", S
);
9692 -- AI05-0073: extend this test to the case of a function
9693 -- with a controlling access result.
9695 elsif Ekind
(Etype
(S
)) = E_Anonymous_Access_Type
9696 and then Is_Tagged_Type
(Designated_Type
(Etype
(S
)))
9698 not Is_Class_Wide_Type
(Designated_Type
(Etype
(S
)))
9699 and then Ada_Version
>= Ada_2012
9702 ("private function with controlling access result "
9703 & "must override visible-part function", S
);
9705 ("\move subprogram to the visible part"
9706 & " (RM 3.9.3(10))", S
);
9710 end Check_Private_Overriding
;
9712 -----------------------
9713 -- Visible_Part_Type --
9714 -----------------------
9716 function Visible_Part_Type
(T
: Entity_Id
) return Boolean is
9717 P
: constant Node_Id
:= Unit_Declaration_Node
(Scope
(T
));
9721 -- If the entity is a private type, then it must be declared in a
9724 if Ekind
(T
) in Private_Kind
then
9728 -- Otherwise, we traverse the visible part looking for its
9729 -- corresponding declaration. We cannot use the declaration
9730 -- node directly because in the private part the entity of a
9731 -- private type is the one in the full view, which does not
9732 -- indicate that it is the completion of something visible.
9734 N
:= First
(Visible_Declarations
(Specification
(P
)));
9735 while Present
(N
) loop
9736 if Nkind
(N
) = N_Full_Type_Declaration
9737 and then Present
(Defining_Identifier
(N
))
9738 and then T
= Defining_Identifier
(N
)
9742 elsif Nkind_In
(N
, N_Private_Type_Declaration
,
9743 N_Private_Extension_Declaration
)
9744 and then Present
(Defining_Identifier
(N
))
9745 and then T
= Full_View
(Defining_Identifier
(N
))
9754 end Visible_Part_Type
;
9756 -- Start of processing for Check_For_Primitive_Subprogram
9759 Is_Primitive
:= False;
9761 if not Comes_From_Source
(S
) then
9764 -- If subprogram is at library level, it is not primitive operation
9766 elsif Current_Scope
= Standard_Standard
then
9769 elsif (Is_Package_Or_Generic_Package
(Current_Scope
)
9770 and then not In_Package_Body
(Current_Scope
))
9771 or else Is_Overriding
9773 -- For function, check return type
9775 if Ekind
(S
) = E_Function
then
9776 if Ekind
(Etype
(S
)) = E_Anonymous_Access_Type
then
9777 F_Typ
:= Designated_Type
(Etype
(S
));
9782 B_Typ
:= Base_Type
(F_Typ
);
9784 if Scope
(B_Typ
) = Current_Scope
9785 and then not Is_Class_Wide_Type
(B_Typ
)
9786 and then not Is_Generic_Type
(B_Typ
)
9788 Is_Primitive
:= True;
9789 Set_Has_Primitive_Operations
(B_Typ
);
9790 Set_Is_Primitive
(S
);
9791 Check_Private_Overriding
(B_Typ
);
9795 -- For all subprograms, check formals
9797 Formal
:= First_Formal
(S
);
9798 while Present
(Formal
) loop
9799 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
9800 F_Typ
:= Designated_Type
(Etype
(Formal
));
9802 F_Typ
:= Etype
(Formal
);
9805 B_Typ
:= Base_Type
(F_Typ
);
9807 if Ekind
(B_Typ
) = E_Access_Subtype
then
9808 B_Typ
:= Base_Type
(B_Typ
);
9811 if Scope
(B_Typ
) = Current_Scope
9812 and then not Is_Class_Wide_Type
(B_Typ
)
9813 and then not Is_Generic_Type
(B_Typ
)
9815 Is_Primitive
:= True;
9816 Set_Is_Primitive
(S
);
9817 Set_Has_Primitive_Operations
(B_Typ
);
9818 Check_Private_Overriding
(B_Typ
);
9821 Next_Formal
(Formal
);
9824 -- Special case: An equality function can be redefined for a type
9825 -- occurring in a declarative part, and won't otherwise be treated as
9826 -- a primitive because it doesn't occur in a package spec and doesn't
9827 -- override an inherited subprogram. It's important that we mark it
9828 -- primitive so it can be returned by Collect_Primitive_Operations
9829 -- and be used in composing the equality operation of later types
9830 -- that have a component of the type.
9832 elsif Chars
(S
) = Name_Op_Eq
9833 and then Etype
(S
) = Standard_Boolean
9835 B_Typ
:= Base_Type
(Etype
(First_Formal
(S
)));
9837 if Scope
(B_Typ
) = Current_Scope
9839 Base_Type
(Etype
(Next_Formal
(First_Formal
(S
)))) = B_Typ
9840 and then not Is_Limited_Type
(B_Typ
)
9842 Is_Primitive
:= True;
9843 Set_Is_Primitive
(S
);
9844 Set_Has_Primitive_Operations
(B_Typ
);
9845 Check_Private_Overriding
(B_Typ
);
9848 end Check_For_Primitive_Subprogram
;
9850 -----------------------------------
9851 -- Check_Synchronized_Overriding --
9852 -----------------------------------
9854 procedure Check_Synchronized_Overriding
9855 (Def_Id
: Entity_Id
;
9856 Overridden_Subp
: out Entity_Id
)
9858 Ifaces_List
: Elist_Id
;
9862 function Matches_Prefixed_View_Profile
9863 (Prim_Params
: List_Id
;
9864 Iface_Params
: List_Id
) return Boolean;
9865 -- Determine whether a subprogram's parameter profile Prim_Params
9866 -- matches that of a potentially overridden interface subprogram
9867 -- Iface_Params. Also determine if the type of first parameter of
9868 -- Iface_Params is an implemented interface.
9870 -----------------------------------
9871 -- Matches_Prefixed_View_Profile --
9872 -----------------------------------
9874 function Matches_Prefixed_View_Profile
9875 (Prim_Params
: List_Id
;
9876 Iface_Params
: List_Id
) return Boolean
9878 Iface_Id
: Entity_Id
;
9879 Iface_Param
: Node_Id
;
9880 Iface_Typ
: Entity_Id
;
9881 Prim_Id
: Entity_Id
;
9882 Prim_Param
: Node_Id
;
9883 Prim_Typ
: Entity_Id
;
9885 function Is_Implemented
9886 (Ifaces_List
: Elist_Id
;
9887 Iface
: Entity_Id
) return Boolean;
9888 -- Determine if Iface is implemented by the current task or
9891 --------------------
9892 -- Is_Implemented --
9893 --------------------
9895 function Is_Implemented
9896 (Ifaces_List
: Elist_Id
;
9897 Iface
: Entity_Id
) return Boolean
9899 Iface_Elmt
: Elmt_Id
;
9902 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
9903 while Present
(Iface_Elmt
) loop
9904 if Node
(Iface_Elmt
) = Iface
then
9908 Next_Elmt
(Iface_Elmt
);
9914 -- Start of processing for Matches_Prefixed_View_Profile
9917 Iface_Param
:= First
(Iface_Params
);
9918 Iface_Typ
:= Etype
(Defining_Identifier
(Iface_Param
));
9920 if Is_Access_Type
(Iface_Typ
) then
9921 Iface_Typ
:= Designated_Type
(Iface_Typ
);
9924 Prim_Param
:= First
(Prim_Params
);
9926 -- The first parameter of the potentially overridden subprogram
9927 -- must be an interface implemented by Prim.
9929 if not Is_Interface
(Iface_Typ
)
9930 or else not Is_Implemented
(Ifaces_List
, Iface_Typ
)
9935 -- The checks on the object parameters are done, move onto the
9936 -- rest of the parameters.
9938 if not In_Scope
then
9939 Prim_Param
:= Next
(Prim_Param
);
9942 Iface_Param
:= Next
(Iface_Param
);
9943 while Present
(Iface_Param
) and then Present
(Prim_Param
) loop
9944 Iface_Id
:= Defining_Identifier
(Iface_Param
);
9945 Iface_Typ
:= Find_Parameter_Type
(Iface_Param
);
9947 Prim_Id
:= Defining_Identifier
(Prim_Param
);
9948 Prim_Typ
:= Find_Parameter_Type
(Prim_Param
);
9950 if Ekind
(Iface_Typ
) = E_Anonymous_Access_Type
9951 and then Ekind
(Prim_Typ
) = E_Anonymous_Access_Type
9952 and then Is_Concurrent_Type
(Designated_Type
(Prim_Typ
))
9954 Iface_Typ
:= Designated_Type
(Iface_Typ
);
9955 Prim_Typ
:= Designated_Type
(Prim_Typ
);
9958 -- Case of multiple interface types inside a parameter profile
9960 -- (Obj_Param : in out Iface; ...; Param : Iface)
9962 -- If the interface type is implemented, then the matching type
9963 -- in the primitive should be the implementing record type.
9965 if Ekind
(Iface_Typ
) = E_Record_Type
9966 and then Is_Interface
(Iface_Typ
)
9967 and then Is_Implemented
(Ifaces_List
, Iface_Typ
)
9969 if Prim_Typ
/= Typ
then
9973 -- The two parameters must be both mode and subtype conformant
9975 elsif Ekind
(Iface_Id
) /= Ekind
(Prim_Id
)
9977 Conforming_Types
(Iface_Typ
, Prim_Typ
, Subtype_Conformant
)
9986 -- One of the two lists contains more parameters than the other
9988 if Present
(Iface_Param
) or else Present
(Prim_Param
) then
9993 end Matches_Prefixed_View_Profile
;
9995 -- Start of processing for Check_Synchronized_Overriding
9998 Overridden_Subp
:= Empty
;
10000 -- Def_Id must be an entry or a subprogram. We should skip predefined
10001 -- primitives internally generated by the frontend; however at this
10002 -- stage predefined primitives are still not fully decorated. As a
10003 -- minor optimization we skip here internally generated subprograms.
10005 if (Ekind
(Def_Id
) /= E_Entry
10006 and then Ekind
(Def_Id
) /= E_Function
10007 and then Ekind
(Def_Id
) /= E_Procedure
)
10008 or else not Comes_From_Source
(Def_Id
)
10013 -- Search for the concurrent declaration since it contains the list
10014 -- of all implemented interfaces. In this case, the subprogram is
10015 -- declared within the scope of a protected or a task type.
10017 if Present
(Scope
(Def_Id
))
10018 and then Is_Concurrent_Type
(Scope
(Def_Id
))
10019 and then not Is_Generic_Actual_Type
(Scope
(Def_Id
))
10021 Typ
:= Scope
(Def_Id
);
10024 -- The enclosing scope is not a synchronized type and the subprogram
10027 elsif No
(First_Formal
(Def_Id
)) then
10030 -- The subprogram has formals and hence it may be a primitive of a
10031 -- concurrent type.
10034 Typ
:= Etype
(First_Formal
(Def_Id
));
10036 if Is_Access_Type
(Typ
) then
10037 Typ
:= Directly_Designated_Type
(Typ
);
10040 if Is_Concurrent_Type
(Typ
)
10041 and then not Is_Generic_Actual_Type
(Typ
)
10045 -- This case occurs when the concurrent type is declared within
10046 -- a generic unit. As a result the corresponding record has been
10047 -- built and used as the type of the first formal, we just have
10048 -- to retrieve the corresponding concurrent type.
10050 elsif Is_Concurrent_Record_Type
(Typ
)
10051 and then not Is_Class_Wide_Type
(Typ
)
10052 and then Present
(Corresponding_Concurrent_Type
(Typ
))
10054 Typ
:= Corresponding_Concurrent_Type
(Typ
);
10062 -- There is no overriding to check if is an inherited operation in a
10063 -- type derivation on for a generic actual.
10065 Collect_Interfaces
(Typ
, Ifaces_List
);
10067 if Is_Empty_Elmt_List
(Ifaces_List
) then
10071 -- Determine whether entry or subprogram Def_Id overrides a primitive
10072 -- operation that belongs to one of the interfaces in Ifaces_List.
10075 Candidate
: Entity_Id
:= Empty
;
10076 Hom
: Entity_Id
:= Empty
;
10077 Iface_Typ
: Entity_Id
;
10078 Subp
: Entity_Id
:= Empty
;
10081 -- Traverse the homonym chain, looking for a potentially
10082 -- overridden subprogram that belongs to an implemented
10085 Hom
:= Current_Entity_In_Scope
(Def_Id
);
10086 while Present
(Hom
) loop
10090 or else not Is_Overloadable
(Subp
)
10091 or else not Is_Primitive
(Subp
)
10092 or else not Is_Dispatching_Operation
(Subp
)
10093 or else not Present
(Find_Dispatching_Type
(Subp
))
10094 or else not Is_Interface
(Find_Dispatching_Type
(Subp
))
10098 -- Entries and procedures can override abstract or null
10099 -- interface procedures.
10101 elsif (Ekind
(Def_Id
) = E_Procedure
10102 or else Ekind
(Def_Id
) = E_Entry
)
10103 and then Ekind
(Subp
) = E_Procedure
10104 and then Matches_Prefixed_View_Profile
10105 (Parameter_Specifications
(Parent
(Def_Id
)),
10106 Parameter_Specifications
(Parent
(Subp
)))
10110 -- For an overridden subprogram Subp, check whether the mode
10111 -- of its first parameter is correct depending on the kind
10112 -- of synchronized type.
10115 Formal
: constant Node_Id
:= First_Formal
(Candidate
);
10118 -- In order for an entry or a protected procedure to
10119 -- override, the first parameter of the overridden
10120 -- routine must be of mode "out", "in out" or
10121 -- access-to-variable.
10123 if Ekind_In
(Candidate
, E_Entry
, E_Procedure
)
10124 and then Is_Protected_Type
(Typ
)
10125 and then Ekind
(Formal
) /= E_In_Out_Parameter
10126 and then Ekind
(Formal
) /= E_Out_Parameter
10127 and then Nkind
(Parameter_Type
(Parent
(Formal
))) /=
10128 N_Access_Definition
10132 -- All other cases are OK since a task entry or routine
10133 -- does not have a restriction on the mode of the first
10134 -- parameter of the overridden interface routine.
10137 Overridden_Subp
:= Candidate
;
10142 -- Functions can override abstract interface functions
10144 elsif Ekind
(Def_Id
) = E_Function
10145 and then Ekind
(Subp
) = E_Function
10146 and then Matches_Prefixed_View_Profile
10147 (Parameter_Specifications
(Parent
(Def_Id
)),
10148 Parameter_Specifications
(Parent
(Subp
)))
10149 and then Etype
(Result_Definition
(Parent
(Def_Id
))) =
10150 Etype
(Result_Definition
(Parent
(Subp
)))
10152 Overridden_Subp
:= Subp
;
10156 Hom
:= Homonym
(Hom
);
10159 -- After examining all candidates for overriding, we are left with
10160 -- the best match which is a mode incompatible interface routine.
10161 -- Do not emit an error if the Expander is active since this error
10162 -- will be detected later on after all concurrent types are
10163 -- expanded and all wrappers are built. This check is meant for
10164 -- spec-only compilations.
10166 if Present
(Candidate
) and then not Expander_Active
then
10168 Find_Parameter_Type
(Parent
(First_Formal
(Candidate
)));
10170 -- Def_Id is primitive of a protected type, declared inside the
10171 -- type, and the candidate is primitive of a limited or
10172 -- synchronized interface.
10175 and then Is_Protected_Type
(Typ
)
10177 (Is_Limited_Interface
(Iface_Typ
)
10178 or else Is_Protected_Interface
(Iface_Typ
)
10179 or else Is_Synchronized_Interface
(Iface_Typ
)
10180 or else Is_Task_Interface
(Iface_Typ
))
10182 Error_Msg_PT
(Parent
(Typ
), Candidate
);
10186 Overridden_Subp
:= Candidate
;
10189 end Check_Synchronized_Overriding
;
10191 ----------------------------
10192 -- Is_Private_Declaration --
10193 ----------------------------
10195 function Is_Private_Declaration
(E
: Entity_Id
) return Boolean is
10196 Priv_Decls
: List_Id
;
10197 Decl
: constant Node_Id
:= Unit_Declaration_Node
(E
);
10200 if Is_Package_Or_Generic_Package
(Current_Scope
)
10201 and then In_Private_Part
(Current_Scope
)
10204 Private_Declarations
10205 (Specification
(Unit_Declaration_Node
(Current_Scope
)));
10207 return In_Package_Body
(Current_Scope
)
10209 (Is_List_Member
(Decl
)
10210 and then List_Containing
(Decl
) = Priv_Decls
)
10211 or else (Nkind
(Parent
(Decl
)) = N_Package_Specification
10213 Is_Compilation_Unit
10214 (Defining_Entity
(Parent
(Decl
)))
10215 and then List_Containing
(Parent
(Parent
(Decl
))) =
10220 end Is_Private_Declaration
;
10222 --------------------------
10223 -- Is_Overriding_Alias --
10224 --------------------------
10226 function Is_Overriding_Alias
10227 (Old_E
: Entity_Id
;
10228 New_E
: Entity_Id
) return Boolean
10230 AO
: constant Entity_Id
:= Alias
(Old_E
);
10231 AN
: constant Entity_Id
:= Alias
(New_E
);
10234 return Scope
(AO
) /= Scope
(AN
)
10235 or else No
(DTC_Entity
(AO
))
10236 or else No
(DTC_Entity
(AN
))
10237 or else DT_Position
(AO
) = DT_Position
(AN
);
10238 end Is_Overriding_Alias
;
10240 -- Start of processing for New_Overloaded_Entity
10243 -- We need to look for an entity that S may override. This must be a
10244 -- homonym in the current scope, so we look for the first homonym of
10245 -- S in the current scope as the starting point for the search.
10247 E
:= Current_Entity_In_Scope
(S
);
10249 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10250 -- They are directly added to the list of primitive operations of
10251 -- Derived_Type, unless this is a rederivation in the private part
10252 -- of an operation that was already derived in the visible part of
10253 -- the current package.
10255 if Ada_Version
>= Ada_2005
10256 and then Present
(Derived_Type
)
10257 and then Present
(Alias
(S
))
10258 and then Is_Dispatching_Operation
(Alias
(S
))
10259 and then Present
(Find_Dispatching_Type
(Alias
(S
)))
10260 and then Is_Interface
(Find_Dispatching_Type
(Alias
(S
)))
10262 -- For private types, when the full-view is processed we propagate to
10263 -- the full view the non-overridden entities whose attribute "alias"
10264 -- references an interface primitive. These entities were added by
10265 -- Derive_Subprograms to ensure that interface primitives are
10268 -- Inside_Freeze_Actions is non zero when S corresponds with an
10269 -- internal entity that links an interface primitive with its
10270 -- covering primitive through attribute Interface_Alias (see
10271 -- Add_Internal_Interface_Entities).
10273 if Inside_Freezing_Actions
= 0
10274 and then Is_Package_Or_Generic_Package
(Current_Scope
)
10275 and then In_Private_Part
(Current_Scope
)
10276 and then Nkind
(Parent
(E
)) = N_Private_Extension_Declaration
10277 and then Nkind
(Parent
(S
)) = N_Full_Type_Declaration
10278 and then Full_View
(Defining_Identifier
(Parent
(E
)))
10279 = Defining_Identifier
(Parent
(S
))
10280 and then Alias
(E
) = Alias
(S
)
10282 Check_Operation_From_Private_View
(S
, E
);
10283 Set_Is_Dispatching_Operation
(S
);
10288 Enter_Overloaded_Entity
(S
);
10289 Check_Dispatching_Operation
(S
, Empty
);
10290 Check_For_Primitive_Subprogram
(Is_Primitive_Subp
);
10296 -- If there is no homonym then this is definitely not overriding
10299 Enter_Overloaded_Entity
(S
);
10300 Check_Dispatching_Operation
(S
, Empty
);
10301 Check_For_Primitive_Subprogram
(Is_Primitive_Subp
);
10303 -- If subprogram has an explicit declaration, check whether it has an
10304 -- overriding indicator.
10306 if Comes_From_Source
(S
) then
10307 Check_Synchronized_Overriding
(S
, Overridden_Subp
);
10309 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10310 -- it may have overridden some hidden inherited primitive. Update
10311 -- Overridden_Subp to avoid spurious errors when checking the
10312 -- overriding indicator.
10314 if Ada_Version
>= Ada_2012
10315 and then No
(Overridden_Subp
)
10316 and then Is_Dispatching_Operation
(S
)
10317 and then Present
(Overridden_Operation
(S
))
10319 Overridden_Subp
:= Overridden_Operation
(S
);
10322 Check_Overriding_Indicator
10323 (S
, Overridden_Subp
, Is_Primitive
=> Is_Primitive_Subp
);
10326 -- If there is a homonym that is not overloadable, then we have an
10327 -- error, except for the special cases checked explicitly below.
10329 elsif not Is_Overloadable
(E
) then
10331 -- Check for spurious conflict produced by a subprogram that has the
10332 -- same name as that of the enclosing generic package. The conflict
10333 -- occurs within an instance, between the subprogram and the renaming
10334 -- declaration for the package. After the subprogram, the package
10335 -- renaming declaration becomes hidden.
10337 if Ekind
(E
) = E_Package
10338 and then Present
(Renamed_Object
(E
))
10339 and then Renamed_Object
(E
) = Current_Scope
10340 and then Nkind
(Parent
(Renamed_Object
(E
))) =
10341 N_Package_Specification
10342 and then Present
(Generic_Parent
(Parent
(Renamed_Object
(E
))))
10345 Set_Is_Immediately_Visible
(E
, False);
10346 Enter_Overloaded_Entity
(S
);
10347 Set_Homonym
(S
, Homonym
(E
));
10348 Check_Dispatching_Operation
(S
, Empty
);
10349 Check_Overriding_Indicator
(S
, Empty
, Is_Primitive
=> False);
10351 -- If the subprogram is implicit it is hidden by the previous
10352 -- declaration. However if it is dispatching, it must appear in the
10353 -- dispatch table anyway, because it can be dispatched to even if it
10354 -- cannot be called directly.
10356 elsif Present
(Alias
(S
)) and then not Comes_From_Source
(S
) then
10357 Set_Scope
(S
, Current_Scope
);
10359 if Is_Dispatching_Operation
(Alias
(S
)) then
10360 Check_Dispatching_Operation
(S
, Empty
);
10366 Error_Msg_Sloc
:= Sloc
(E
);
10368 -- Generate message, with useful additional warning if in generic
10370 if Is_Generic_Unit
(E
) then
10371 Error_Msg_N
("previous generic unit cannot be overloaded", S
);
10372 Error_Msg_N
("\& conflicts with declaration#", S
);
10374 Error_Msg_N
("& conflicts with declaration#", S
);
10380 -- E exists and is overloadable
10383 Check_Synchronized_Overriding
(S
, Overridden_Subp
);
10385 -- Loop through E and its homonyms to determine if any of them is
10386 -- the candidate for overriding by S.
10388 while Present
(E
) loop
10390 -- Definitely not interesting if not in the current scope
10392 if Scope
(E
) /= Current_Scope
then
10395 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10396 -- procedures locate the internally generated spec. We enforce
10397 -- mode conformance since a tagged type may inherit from
10398 -- interfaces several null primitives which differ only in
10399 -- the mode of the formals.
10401 elsif not Comes_From_Source
(S
)
10402 and then Is_Null_Procedure
(S
)
10403 and then not Mode_Conformant
(E
, S
)
10407 -- Check if we have type conformance
10409 elsif Type_Conformant
(E
, S
) then
10411 -- If the old and new entities have the same profile and one
10412 -- is not the body of the other, then this is an error, unless
10413 -- one of them is implicitly declared.
10415 -- There are some cases when both can be implicit, for example
10416 -- when both a literal and a function that overrides it are
10417 -- inherited in a derivation, or when an inherited operation
10418 -- of a tagged full type overrides the inherited operation of
10419 -- a private extension. Ada 83 had a special rule for the
10420 -- literal case. In Ada 95, the later implicit operation hides
10421 -- the former, and the literal is always the former. In the
10422 -- odd case where both are derived operations declared at the
10423 -- same point, both operations should be declared, and in that
10424 -- case we bypass the following test and proceed to the next
10425 -- part. This can only occur for certain obscure cases in
10426 -- instances, when an operation on a type derived from a formal
10427 -- private type does not override a homograph inherited from
10428 -- the actual. In subsequent derivations of such a type, the
10429 -- DT positions of these operations remain distinct, if they
10432 if Present
(Alias
(S
))
10433 and then (No
(Alias
(E
))
10434 or else Comes_From_Source
(E
)
10435 or else Is_Abstract_Subprogram
(S
)
10437 (Is_Dispatching_Operation
(E
)
10438 and then Is_Overriding_Alias
(E
, S
)))
10439 and then Ekind
(E
) /= E_Enumeration_Literal
10441 -- When an derived operation is overloaded it may be due to
10442 -- the fact that the full view of a private extension
10443 -- re-inherits. It has to be dealt with.
10445 if Is_Package_Or_Generic_Package
(Current_Scope
)
10446 and then In_Private_Part
(Current_Scope
)
10448 Check_Operation_From_Private_View
(S
, E
);
10451 -- In any case the implicit operation remains hidden by the
10452 -- existing declaration, which is overriding. Indicate that
10453 -- E overrides the operation from which S is inherited.
10455 if Present
(Alias
(S
)) then
10456 Set_Overridden_Operation
(E
, Alias
(S
));
10458 Set_Overridden_Operation
(E
, S
);
10461 if Comes_From_Source
(E
) then
10462 Check_Overriding_Indicator
(E
, S
, Is_Primitive
=> False);
10467 -- Within an instance, the renaming declarations for actual
10468 -- subprograms may become ambiguous, but they do not hide each
10471 elsif Ekind
(E
) /= E_Entry
10472 and then not Comes_From_Source
(E
)
10473 and then not Is_Generic_Instance
(E
)
10474 and then (Present
(Alias
(E
))
10475 or else Is_Intrinsic_Subprogram
(E
))
10476 and then (not In_Instance
10477 or else No
(Parent
(E
))
10478 or else Nkind
(Unit_Declaration_Node
(E
)) /=
10479 N_Subprogram_Renaming_Declaration
)
10481 -- A subprogram child unit is not allowed to override an
10482 -- inherited subprogram (10.1.1(20)).
10484 if Is_Child_Unit
(S
) then
10486 ("child unit overrides inherited subprogram in parent",
10491 if Is_Non_Overriding_Operation
(E
, S
) then
10492 Enter_Overloaded_Entity
(S
);
10494 if No
(Derived_Type
)
10495 or else Is_Tagged_Type
(Derived_Type
)
10497 Check_Dispatching_Operation
(S
, Empty
);
10503 -- E is a derived operation or an internal operator which
10504 -- is being overridden. Remove E from further visibility.
10505 -- Furthermore, if E is a dispatching operation, it must be
10506 -- replaced in the list of primitive operations of its type
10507 -- (see Override_Dispatching_Operation).
10509 Overridden_Subp
:= E
;
10515 Prev
:= First_Entity
(Current_Scope
);
10516 while Present
(Prev
) and then Next_Entity
(Prev
) /= E
loop
10517 Next_Entity
(Prev
);
10520 -- It is possible for E to be in the current scope and
10521 -- yet not in the entity chain. This can only occur in a
10522 -- generic context where E is an implicit concatenation
10523 -- in the formal part, because in a generic body the
10524 -- entity chain starts with the formals.
10527 (Present
(Prev
) or else Chars
(E
) = Name_Op_Concat
);
10529 -- E must be removed both from the entity_list of the
10530 -- current scope, and from the visibility chain
10532 if Debug_Flag_E
then
10533 Write_Str
("Override implicit operation ");
10534 Write_Int
(Int
(E
));
10538 -- If E is a predefined concatenation, it stands for four
10539 -- different operations. As a result, a single explicit
10540 -- declaration does not hide it. In a possible ambiguous
10541 -- situation, Disambiguate chooses the user-defined op,
10542 -- so it is correct to retain the previous internal one.
10544 if Chars
(E
) /= Name_Op_Concat
10545 or else Ekind
(E
) /= E_Operator
10547 -- For nondispatching derived operations that are
10548 -- overridden by a subprogram declared in the private
10549 -- part of a package, we retain the derived subprogram
10550 -- but mark it as not immediately visible. If the
10551 -- derived operation was declared in the visible part
10552 -- then this ensures that it will still be visible
10553 -- outside the package with the proper signature
10554 -- (calls from outside must also be directed to this
10555 -- version rather than the overriding one, unlike the
10556 -- dispatching case). Calls from inside the package
10557 -- will still resolve to the overriding subprogram
10558 -- since the derived one is marked as not visible
10559 -- within the package.
10561 -- If the private operation is dispatching, we achieve
10562 -- the overriding by keeping the implicit operation
10563 -- but setting its alias to be the overriding one. In
10564 -- this fashion the proper body is executed in all
10565 -- cases, but the original signature is used outside
10568 -- If the overriding is not in the private part, we
10569 -- remove the implicit operation altogether.
10571 if Is_Private_Declaration
(S
) then
10572 if not Is_Dispatching_Operation
(E
) then
10573 Set_Is_Immediately_Visible
(E
, False);
10575 -- Work done in Override_Dispatching_Operation,
10576 -- so nothing else needs to be done here.
10582 -- Find predecessor of E in Homonym chain
10584 if E
= Current_Entity
(E
) then
10587 Prev_Vis
:= Current_Entity
(E
);
10588 while Homonym
(Prev_Vis
) /= E
loop
10589 Prev_Vis
:= Homonym
(Prev_Vis
);
10593 if Prev_Vis
/= Empty
then
10595 -- Skip E in the visibility chain
10597 Set_Homonym
(Prev_Vis
, Homonym
(E
));
10600 Set_Name_Entity_Id
(Chars
(E
), Homonym
(E
));
10603 Set_Next_Entity
(Prev
, Next_Entity
(E
));
10605 if No
(Next_Entity
(Prev
)) then
10606 Set_Last_Entity
(Current_Scope
, Prev
);
10611 Enter_Overloaded_Entity
(S
);
10613 -- For entities generated by Derive_Subprograms the
10614 -- overridden operation is the inherited primitive
10615 -- (which is available through the attribute alias).
10617 if not (Comes_From_Source
(E
))
10618 and then Is_Dispatching_Operation
(E
)
10619 and then Find_Dispatching_Type
(E
) =
10620 Find_Dispatching_Type
(S
)
10621 and then Present
(Alias
(E
))
10622 and then Comes_From_Source
(Alias
(E
))
10624 Set_Overridden_Operation
(S
, Alias
(E
));
10626 -- Normal case of setting entity as overridden
10628 -- Note: Static_Initialization and Overridden_Operation
10629 -- attributes use the same field in subprogram entities.
10630 -- Static_Initialization is only defined for internal
10631 -- initialization procedures, where Overridden_Operation
10632 -- is irrelevant. Therefore the setting of this attribute
10633 -- must check whether the target is an init_proc.
10635 elsif not Is_Init_Proc
(S
) then
10636 Set_Overridden_Operation
(S
, E
);
10639 Check_Overriding_Indicator
(S
, E
, Is_Primitive
=> True);
10641 -- If S is a user-defined subprogram or a null procedure
10642 -- expanded to override an inherited null procedure, or a
10643 -- predefined dispatching primitive then indicate that E
10644 -- overrides the operation from which S is inherited.
10646 if Comes_From_Source
(S
)
10648 (Present
(Parent
(S
))
10650 Nkind
(Parent
(S
)) = N_Procedure_Specification
10652 Null_Present
(Parent
(S
)))
10654 (Present
(Alias
(E
))
10656 Is_Predefined_Dispatching_Operation
(Alias
(E
)))
10658 if Present
(Alias
(E
)) then
10659 Set_Overridden_Operation
(S
, Alias
(E
));
10663 if Is_Dispatching_Operation
(E
) then
10665 -- An overriding dispatching subprogram inherits the
10666 -- convention of the overridden subprogram (AI-117).
10668 Set_Convention
(S
, Convention
(E
));
10669 Check_Dispatching_Operation
(S
, E
);
10672 Check_Dispatching_Operation
(S
, Empty
);
10675 Check_For_Primitive_Subprogram
10676 (Is_Primitive_Subp
, Is_Overriding
=> True);
10677 goto Check_Inequality
;
10680 -- Apparent redeclarations in instances can occur when two
10681 -- formal types get the same actual type. The subprograms in
10682 -- in the instance are legal, even if not callable from the
10683 -- outside. Calls from within are disambiguated elsewhere.
10684 -- For dispatching operations in the visible part, the usual
10685 -- rules apply, and operations with the same profile are not
10686 -- legal (B830001).
10688 elsif (In_Instance_Visible_Part
10689 and then not Is_Dispatching_Operation
(E
))
10690 or else In_Instance_Not_Visible
10694 -- Here we have a real error (identical profile)
10697 Error_Msg_Sloc
:= Sloc
(E
);
10699 -- Avoid cascaded errors if the entity appears in
10700 -- subsequent calls.
10702 Set_Scope
(S
, Current_Scope
);
10704 -- Generate error, with extra useful warning for the case
10705 -- of a generic instance with no completion.
10707 if Is_Generic_Instance
(S
)
10708 and then not Has_Completion
(E
)
10711 ("instantiation cannot provide body for&", S
);
10712 Error_Msg_N
("\& conflicts with declaration#", S
);
10714 Error_Msg_N
("& conflicts with declaration#", S
);
10721 -- If one subprogram has an access parameter and the other
10722 -- a parameter of an access type, calls to either might be
10723 -- ambiguous. Verify that parameters match except for the
10724 -- access parameter.
10726 if May_Hide_Profile
then
10732 F1
:= First_Formal
(S
);
10733 F2
:= First_Formal
(E
);
10734 while Present
(F1
) and then Present
(F2
) loop
10735 if Is_Access_Type
(Etype
(F1
)) then
10736 if not Is_Access_Type
(Etype
(F2
))
10737 or else not Conforming_Types
10738 (Designated_Type
(Etype
(F1
)),
10739 Designated_Type
(Etype
(F2
)),
10742 May_Hide_Profile
:= False;
10746 not Conforming_Types
10747 (Etype
(F1
), Etype
(F2
), Type_Conformant
)
10749 May_Hide_Profile
:= False;
10756 if May_Hide_Profile
10760 Error_Msg_NE
("calls to& may be ambiguous??", S
, S
);
10769 -- On exit, we know that S is a new entity
10771 Enter_Overloaded_Entity
(S
);
10772 Check_For_Primitive_Subprogram
(Is_Primitive_Subp
);
10773 Check_Overriding_Indicator
10774 (S
, Overridden_Subp
, Is_Primitive
=> Is_Primitive_Subp
);
10776 -- Overloading is not allowed in SPARK, except for operators
10778 if Nkind
(S
) /= N_Defining_Operator_Symbol
then
10779 Error_Msg_Sloc
:= Sloc
(Homonym
(S
));
10780 Check_SPARK_Restriction
10781 ("overloading not allowed with entity#", S
);
10784 -- If S is a derived operation for an untagged type then by
10785 -- definition it's not a dispatching operation (even if the parent
10786 -- operation was dispatching), so Check_Dispatching_Operation is not
10787 -- called in that case.
10789 if No
(Derived_Type
)
10790 or else Is_Tagged_Type
(Derived_Type
)
10792 Check_Dispatching_Operation
(S
, Empty
);
10796 -- If this is a user-defined equality operator that is not a derived
10797 -- subprogram, create the corresponding inequality. If the operation is
10798 -- dispatching, the expansion is done elsewhere, and we do not create
10799 -- an explicit inequality operation.
10801 <<Check_Inequality
>>
10802 if Chars
(S
) = Name_Op_Eq
10803 and then Etype
(S
) = Standard_Boolean
10804 and then Present
(Parent
(S
))
10805 and then not Is_Dispatching_Operation
(S
)
10807 Make_Inequality_Operator
(S
);
10809 if Ada_Version
>= Ada_2012
then
10810 Check_Untagged_Equality
(S
);
10813 end New_Overloaded_Entity
;
10815 ---------------------
10816 -- Process_Formals --
10817 ---------------------
10819 procedure Process_Formals
10821 Related_Nod
: Node_Id
)
10823 Param_Spec
: Node_Id
;
10824 Formal
: Entity_Id
;
10825 Formal_Type
: Entity_Id
;
10829 Num_Out_Params
: Nat
:= 0;
10830 First_Out_Param
: Entity_Id
:= Empty
;
10831 -- Used for setting Is_Only_Out_Parameter
10833 function Designates_From_With_Type
(Typ
: Entity_Id
) return Boolean;
10834 -- Determine whether an access type designates a type coming from a
10837 function Is_Class_Wide_Default
(D
: Node_Id
) return Boolean;
10838 -- Check whether the default has a class-wide type. After analysis the
10839 -- default has the type of the formal, so we must also check explicitly
10840 -- for an access attribute.
10842 -------------------------------
10843 -- Designates_From_With_Type --
10844 -------------------------------
10846 function Designates_From_With_Type
(Typ
: Entity_Id
) return Boolean is
10847 Desig
: Entity_Id
:= Typ
;
10850 if Is_Access_Type
(Desig
) then
10851 Desig
:= Directly_Designated_Type
(Desig
);
10854 if Is_Class_Wide_Type
(Desig
) then
10855 Desig
:= Root_Type
(Desig
);
10859 Ekind
(Desig
) = E_Incomplete_Type
and then From_With_Type
(Desig
);
10860 end Designates_From_With_Type
;
10862 ---------------------------
10863 -- Is_Class_Wide_Default --
10864 ---------------------------
10866 function Is_Class_Wide_Default
(D
: Node_Id
) return Boolean is
10868 return Is_Class_Wide_Type
(Designated_Type
(Etype
(D
)))
10869 or else (Nkind
(D
) = N_Attribute_Reference
10870 and then Attribute_Name
(D
) = Name_Access
10871 and then Is_Class_Wide_Type
(Etype
(Prefix
(D
))));
10872 end Is_Class_Wide_Default
;
10874 -- Start of processing for Process_Formals
10877 -- In order to prevent premature use of the formals in the same formal
10878 -- part, the Ekind is left undefined until all default expressions are
10879 -- analyzed. The Ekind is established in a separate loop at the end.
10881 Param_Spec
:= First
(T
);
10882 while Present
(Param_Spec
) loop
10883 Formal
:= Defining_Identifier
(Param_Spec
);
10884 Set_Never_Set_In_Source
(Formal
, True);
10885 Enter_Name
(Formal
);
10887 -- Case of ordinary parameters
10889 if Nkind
(Parameter_Type
(Param_Spec
)) /= N_Access_Definition
then
10890 Find_Type
(Parameter_Type
(Param_Spec
));
10891 Ptype
:= Parameter_Type
(Param_Spec
);
10893 if Ptype
= Error
then
10897 Formal_Type
:= Entity
(Ptype
);
10899 if Is_Incomplete_Type
(Formal_Type
)
10901 (Is_Class_Wide_Type
(Formal_Type
)
10902 and then Is_Incomplete_Type
(Root_Type
(Formal_Type
)))
10904 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10905 -- primitive operations, as long as their completion is
10906 -- in the same declarative part. If in the private part
10907 -- this means that the type cannot be a Taft-amendment type.
10908 -- Check is done on package exit. For access to subprograms,
10909 -- the use is legal for Taft-amendment types.
10911 -- Ada 2012: tagged incomplete types are allowed as generic
10912 -- formal types. They do not introduce dependencies and the
10913 -- corresponding generic subprogram does not have a delayed
10914 -- freeze, because it does not need a freeze node.
10916 if Is_Tagged_Type
(Formal_Type
) then
10917 if Ekind
(Scope
(Current_Scope
)) = E_Package
10918 and then not From_With_Type
(Formal_Type
)
10919 and then not Is_Generic_Type
(Formal_Type
)
10920 and then not Is_Class_Wide_Type
(Formal_Type
)
10923 (Parent
(T
), N_Access_Function_Definition
,
10924 N_Access_Procedure_Definition
)
10928 Private_Dependents
(Base_Type
(Formal_Type
)));
10930 -- Freezing is delayed to ensure that Register_Prim
10931 -- will get called for this operation, which is needed
10932 -- in cases where static dispatch tables aren't built.
10933 -- (Note that the same is done for controlling access
10934 -- parameter cases in function Access_Definition.)
10936 Set_Has_Delayed_Freeze
(Current_Scope
);
10940 -- Special handling of Value_Type for CIL case
10942 elsif Is_Value_Type
(Formal_Type
) then
10945 elsif not Nkind_In
(Parent
(T
), N_Access_Function_Definition
,
10946 N_Access_Procedure_Definition
)
10948 -- AI05-0151: Tagged incomplete types are allowed in all
10949 -- formal parts. Untagged incomplete types are not allowed
10952 if Ada_Version
>= Ada_2012
then
10953 if Is_Tagged_Type
(Formal_Type
) then
10956 elsif Nkind_In
(Parent
(Parent
(T
)), N_Accept_Statement
,
10961 ("invalid use of untagged incomplete type&",
10962 Ptype
, Formal_Type
);
10967 ("invalid use of incomplete type&",
10968 Param_Spec
, Formal_Type
);
10970 -- Further checks on the legality of incomplete types
10971 -- in formal parts are delayed until the freeze point
10972 -- of the enclosing subprogram or access to subprogram.
10976 elsif Ekind
(Formal_Type
) = E_Void
then
10978 ("premature use of&",
10979 Parameter_Type
(Param_Spec
), Formal_Type
);
10982 -- Ada 2012 (AI-142): Handle aliased parameters
10984 if Ada_Version
>= Ada_2012
10985 and then Aliased_Present
(Param_Spec
)
10987 Set_Is_Aliased
(Formal
);
10990 -- Ada 2005 (AI-231): Create and decorate an internal subtype
10991 -- declaration corresponding to the null-excluding type of the
10992 -- formal in the enclosing scope. Finally, replace the parameter
10993 -- type of the formal with the internal subtype.
10995 if Ada_Version
>= Ada_2005
10996 and then Null_Exclusion_Present
(Param_Spec
)
10998 if not Is_Access_Type
(Formal_Type
) then
11000 ("`NOT NULL` allowed only for an access type", Param_Spec
);
11003 if Can_Never_Be_Null
(Formal_Type
)
11004 and then Comes_From_Source
(Related_Nod
)
11007 ("`NOT NULL` not allowed (& already excludes null)",
11008 Param_Spec
, Formal_Type
);
11012 Create_Null_Excluding_Itype
11014 Related_Nod
=> Related_Nod
,
11015 Scope_Id
=> Scope
(Current_Scope
));
11017 -- If the designated type of the itype is an itype that is
11018 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11019 -- on the access subtype, to prevent order-of-elaboration
11020 -- issues in the backend.
11023 -- type T is access procedure;
11024 -- procedure Op (O : not null T);
11026 if Is_Itype
(Directly_Designated_Type
(Formal_Type
))
11028 not Is_Frozen
(Directly_Designated_Type
(Formal_Type
))
11030 Set_Has_Delayed_Freeze
(Formal_Type
);
11035 -- An access formal type
11039 Access_Definition
(Related_Nod
, Parameter_Type
(Param_Spec
));
11041 -- No need to continue if we already notified errors
11043 if not Present
(Formal_Type
) then
11047 -- Ada 2005 (AI-254)
11050 AD
: constant Node_Id
:=
11051 Access_To_Subprogram_Definition
11052 (Parameter_Type
(Param_Spec
));
11054 if Present
(AD
) and then Protected_Present
(AD
) then
11056 Replace_Anonymous_Access_To_Protected_Subprogram
11062 Set_Etype
(Formal
, Formal_Type
);
11064 -- Deal with default expression if present
11066 Default
:= Expression
(Param_Spec
);
11068 if Present
(Default
) then
11069 Check_SPARK_Restriction
11070 ("default expression is not allowed", Default
);
11072 if Out_Present
(Param_Spec
) then
11074 ("default initialization only allowed for IN parameters",
11078 -- Do the special preanalysis of the expression (see section on
11079 -- "Handling of Default Expressions" in the spec of package Sem).
11081 Preanalyze_Spec_Expression
(Default
, Formal_Type
);
11083 -- An access to constant cannot be the default for
11084 -- an access parameter that is an access to variable.
11086 if Ekind
(Formal_Type
) = E_Anonymous_Access_Type
11087 and then not Is_Access_Constant
(Formal_Type
)
11088 and then Is_Access_Type
(Etype
(Default
))
11089 and then Is_Access_Constant
(Etype
(Default
))
11092 ("formal that is access to variable cannot be initialized " &
11093 "with an access-to-constant expression", Default
);
11096 -- Check that the designated type of an access parameter's default
11097 -- is not a class-wide type unless the parameter's designated type
11098 -- is also class-wide.
11100 if Ekind
(Formal_Type
) = E_Anonymous_Access_Type
11101 and then not Designates_From_With_Type
(Formal_Type
)
11102 and then Is_Class_Wide_Default
(Default
)
11103 and then not Is_Class_Wide_Type
(Designated_Type
(Formal_Type
))
11106 ("access to class-wide expression not allowed here", Default
);
11109 -- Check incorrect use of dynamically tagged expressions
11111 if Is_Tagged_Type
(Formal_Type
) then
11112 Check_Dynamically_Tagged_Expression
11114 Typ
=> Formal_Type
,
11115 Related_Nod
=> Default
);
11119 -- Ada 2005 (AI-231): Static checks
11121 if Ada_Version
>= Ada_2005
11122 and then Is_Access_Type
(Etype
(Formal
))
11123 and then Can_Never_Be_Null
(Etype
(Formal
))
11125 Null_Exclusion_Static_Checks
(Param_Spec
);
11132 -- If this is the formal part of a function specification, analyze the
11133 -- subtype mark in the context where the formals are visible but not
11134 -- yet usable, and may hide outer homographs.
11136 if Nkind
(Related_Nod
) = N_Function_Specification
then
11137 Analyze_Return_Type
(Related_Nod
);
11140 -- Now set the kind (mode) of each formal
11142 Param_Spec
:= First
(T
);
11143 while Present
(Param_Spec
) loop
11144 Formal
:= Defining_Identifier
(Param_Spec
);
11145 Set_Formal_Mode
(Formal
);
11147 if Ekind
(Formal
) = E_In_Parameter
then
11148 Set_Default_Value
(Formal
, Expression
(Param_Spec
));
11150 if Present
(Expression
(Param_Spec
)) then
11151 Default
:= Expression
(Param_Spec
);
11153 if Is_Scalar_Type
(Etype
(Default
)) then
11154 if Nkind
(Parameter_Type
(Param_Spec
)) /=
11155 N_Access_Definition
11157 Formal_Type
:= Entity
(Parameter_Type
(Param_Spec
));
11161 (Related_Nod
, Parameter_Type
(Param_Spec
));
11164 Apply_Scalar_Range_Check
(Default
, Formal_Type
);
11168 elsif Ekind
(Formal
) = E_Out_Parameter
then
11169 Num_Out_Params
:= Num_Out_Params
+ 1;
11171 if Num_Out_Params
= 1 then
11172 First_Out_Param
:= Formal
;
11175 elsif Ekind
(Formal
) = E_In_Out_Parameter
then
11176 Num_Out_Params
:= Num_Out_Params
+ 1;
11179 -- Skip remaining processing if formal type was in error
11181 if Etype
(Formal
) = Any_Type
or else Error_Posted
(Formal
) then
11182 goto Next_Parameter
;
11185 -- Force call by reference if aliased
11187 if Is_Aliased
(Formal
) then
11188 Set_Mechanism
(Formal
, By_Reference
);
11190 -- Warn if user asked this to be passed by copy
11192 if Convention
(Formal_Type
) = Convention_Ada_Pass_By_Copy
then
11194 ("cannot pass aliased parameter & by copy?", Formal
);
11197 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11199 elsif Convention
(Formal_Type
) = Convention_Ada_Pass_By_Copy
then
11200 Set_Mechanism
(Formal
, By_Copy
);
11202 elsif Convention
(Formal_Type
) = Convention_Ada_Pass_By_Reference
then
11203 Set_Mechanism
(Formal
, By_Reference
);
11210 if Present
(First_Out_Param
) and then Num_Out_Params
= 1 then
11211 Set_Is_Only_Out_Parameter
(First_Out_Param
);
11213 end Process_Formals
;
11219 procedure Process_PPCs
11221 Spec_Id
: Entity_Id
;
11222 Body_Id
: Entity_Id
)
11224 Loc
: constant Source_Ptr
:= Sloc
(N
);
11228 Designator
: Entity_Id
;
11229 -- Subprogram designator, set from Spec_Id if present, else Body_Id
11231 Precond
: Node_Id
:= Empty
;
11232 -- Set non-Empty if we prepend precondition to the declarations. This
11233 -- is used to hook up inherited preconditions (adding the condition
11234 -- expression with OR ELSE, and adding the message).
11236 Inherited_Precond
: Node_Id
;
11237 -- Precondition inherited from parent subprogram
11239 Inherited
: constant Subprogram_List
:=
11240 Inherited_Subprograms
(Spec_Id
);
11241 -- List of subprograms inherited by this subprogram
11243 Plist
: List_Id
:= No_List
;
11244 -- List of generated postconditions
11246 procedure Append_Enabled_Item
(Item
: Node_Id
; List
: in out List_Id
);
11247 -- Append a node to a list. If there is no list, create a new one. When
11248 -- the item denotes a pragma, it is added to the list only when it is
11251 procedure Check_Access_Invariants
(E
: Entity_Id
);
11252 -- If the subprogram returns an access to a type with invariants, or
11253 -- has access parameters whose designated type has an invariant, then
11254 -- under the same visibility conditions as for other invariant checks,
11255 -- the type invariant must be applied to the returned value.
11257 function Grab_PPC
(Pspec
: Entity_Id
:= Empty
) return Node_Id
;
11258 -- Prag contains an analyzed precondition or postcondition pragma. This
11259 -- function copies the pragma, changes it to the corresponding Check
11260 -- pragma and returns the Check pragma as the result. If Pspec is non-
11261 -- empty, this is the case of inheriting a PPC, where we must change
11262 -- references to parameters of the inherited subprogram to point to the
11263 -- corresponding parameters of the current subprogram.
11265 function Has_Checked_Predicate
(Typ
: Entity_Id
) return Boolean;
11266 -- Determine whether type Typ has or inherits at least one predicate
11267 -- aspect or pragma, for which the applicable policy is Checked.
11269 function Has_Null_Body
(Proc_Id
: Entity_Id
) return Boolean;
11270 -- Determine whether the body of procedure Proc_Id contains a sole null
11271 -- statement, possibly followed by an optional return.
11273 procedure Insert_After_Last_Declaration
(Nod
: Node_Id
);
11274 -- Insert node Nod after the last declaration of the context
11276 function Is_Public_Subprogram_For
(T
: Entity_Id
) return Boolean;
11277 -- T is the entity for a private type for which invariants are defined.
11278 -- This function returns True if the procedure corresponding to the
11279 -- value of Designator is a public procedure from the point of view of
11280 -- this type (i.e. its spec is in the visible part of the package that
11281 -- contains the declaration of the private type). A True value means
11282 -- that an invariant check is required (for an IN OUT parameter, or
11283 -- the returned value of a function.
11285 -------------------------
11286 -- Append_Enabled_Item --
11287 -------------------------
11289 procedure Append_Enabled_Item
(Item
: Node_Id
; List
: in out List_Id
) is
11291 -- Do not chain ignored or disabled pragmas
11293 if Nkind
(Item
) = N_Pragma
11294 and then (Is_Ignored
(Item
) or else Is_Disabled
(Item
))
11305 Append
(Item
, List
);
11307 end Append_Enabled_Item
;
11309 -----------------------------
11310 -- Check_Access_Invariants --
11311 -----------------------------
11313 procedure Check_Access_Invariants
(E
: Entity_Id
) is
11319 if Is_Access_Type
(Etype
(E
))
11320 and then not Is_Access_Constant
(Etype
(E
))
11322 Typ
:= Designated_Type
(Etype
(E
));
11324 if Has_Invariants
(Typ
)
11325 and then Present
(Invariant_Procedure
(Typ
))
11326 and then not Has_Null_Body
(Invariant_Procedure
(Typ
))
11327 and then Is_Public_Subprogram_For
(Typ
)
11330 Make_Explicit_Dereference
(Loc
,
11331 Prefix
=> New_Occurrence_Of
(E
, Loc
));
11332 Set_Etype
(Obj
, Typ
);
11334 Call
:= Make_Invariant_Call
(Obj
);
11336 Append_Enabled_Item
11337 (Make_If_Statement
(Loc
,
11340 Left_Opnd
=> Make_Null
(Loc
),
11341 Right_Opnd
=> New_Occurrence_Of
(E
, Loc
)),
11342 Then_Statements
=> New_List
(Call
)),
11346 end Check_Access_Invariants
;
11352 function Grab_PPC
(Pspec
: Entity_Id
:= Empty
) return Node_Id
is
11353 Nam
: constant Name_Id
:= Pragma_Name
(Prag
);
11358 -- Effective name of pragma (maybe Pre/Post rather than Precondition/
11359 -- Postcodition if the pragma came from a Pre/Post aspect). We need
11360 -- the name right when we generate the Check pragma, since we want
11361 -- the right set of check policies to apply.
11364 -- Prepare map if this is the case where we have to map entities of
11365 -- arguments in the overridden subprogram to corresponding entities
11366 -- of the current subprogram.
11377 Map
:= New_Elmt_List
;
11378 PF
:= First_Formal
(Pspec
);
11379 CF
:= First_Formal
(Designator
);
11380 while Present
(PF
) loop
11381 Append_Elmt
(PF
, Map
);
11382 Append_Elmt
(CF
, Map
);
11389 -- Now we can copy the tree, doing any required substitutions
11391 CP
:= New_Copy_Tree
(Prag
, Map
=> Map
, New_Scope
=> Current_Scope
);
11393 -- Set Analyzed to false, since we want to reanalyze the check
11394 -- procedure. Note that it is only at the outer level that we
11395 -- do this fiddling, for the spec cases, the already preanalyzed
11396 -- parameters are not affected.
11398 Set_Analyzed
(CP
, False);
11400 -- We also make sure Comes_From_Source is False for the copy
11402 Set_Comes_From_Source
(CP
, False);
11404 -- For a postcondition pragma within a generic, preserve the pragma
11405 -- for later expansion. This is also used when an error was detected,
11406 -- thus setting Expander_Active to False.
11408 if Nam
= Name_Postcondition
11409 and then not Expander_Active
11414 -- Get effective name of aspect
11416 if Present
(Corresponding_Aspect
(Prag
)) then
11417 Ename
:= Chars
(Identifier
(Corresponding_Aspect
(Prag
)));
11422 -- Change copy of pragma into corresponding pragma Check
11424 Prepend_To
(Pragma_Argument_Associations
(CP
),
11425 Make_Pragma_Argument_Association
(Sloc
(Prag
),
11426 Expression
=> Make_Identifier
(Loc
, Ename
)));
11427 Set_Pragma_Identifier
(CP
, Make_Identifier
(Sloc
(Prag
), Name_Check
));
11429 -- If this is inherited case and the current message starts with
11430 -- "failed p", we change it to "failed inherited p...".
11432 if Present
(Pspec
) then
11434 Msg
: constant Node_Id
:=
11435 Last
(Pragma_Argument_Associations
(CP
));
11438 if Chars
(Msg
) = Name_Message
then
11439 String_To_Name_Buffer
(Strval
(Expression
(Msg
)));
11441 if Name_Buffer
(1 .. 8) = "failed p" then
11442 Insert_Str_In_Name_Buffer
("inherited ", 8);
11444 (Expression
(Last
(Pragma_Argument_Associations
(CP
))),
11445 String_From_Name_Buffer
);
11451 -- Return the check pragma
11456 ---------------------------
11457 -- Has_Checked_Predicate --
11458 ---------------------------
11460 function Has_Checked_Predicate
(Typ
: Entity_Id
) return Boolean is
11465 -- Climb the ancestor type chain staring from the input. This is done
11466 -- because the input type may lack aspect/pragma predicate and simply
11467 -- inherit those from its ancestor.
11469 -- Note that predicate pragmas include all three cases of predicate
11470 -- aspects (Predicate, Dynamic_Predicate, Static_Predicate), so this
11471 -- routine checks for all three cases.
11474 while Present
(Anc
) loop
11475 Pred
:= Get_Pragma
(Anc
, Pragma_Predicate
);
11477 if Present
(Pred
) and then not Is_Ignored
(Pred
) then
11481 Anc
:= Nearest_Ancestor
(Anc
);
11485 end Has_Checked_Predicate
;
11487 -------------------
11488 -- Has_Null_Body --
11489 -------------------
11491 function Has_Null_Body
(Proc_Id
: Entity_Id
) return Boolean is
11492 Body_Id
: Entity_Id
;
11499 Spec
:= Parent
(Proc_Id
);
11500 Decl
:= Parent
(Spec
);
11502 -- Retrieve the entity of the invariant procedure body
11504 if Nkind
(Spec
) = N_Procedure_Specification
11505 and then Nkind
(Decl
) = N_Subprogram_Declaration
11507 Body_Id
:= Corresponding_Body
(Decl
);
11509 -- The body acts as a spec
11512 Body_Id
:= Proc_Id
;
11515 -- The body will be generated later
11517 if No
(Body_Id
) then
11521 Spec
:= Parent
(Body_Id
);
11522 Decl
:= Parent
(Spec
);
11525 (Nkind
(Spec
) = N_Procedure_Specification
11526 and then Nkind
(Decl
) = N_Subprogram_Body
);
11528 Stmt1
:= First
(Statements
(Handled_Statement_Sequence
(Decl
)));
11530 -- Look for a null statement followed by an optional return statement
11532 if Nkind
(Stmt1
) = N_Null_Statement
then
11533 Stmt2
:= Next
(Stmt1
);
11535 if Present
(Stmt2
) then
11536 return Nkind
(Stmt2
) = N_Simple_Return_Statement
;
11545 -----------------------------------
11546 -- Insert_After_Last_Declaration --
11547 -----------------------------------
11549 procedure Insert_After_Last_Declaration
(Nod
: Node_Id
) is
11550 Decls
: constant List_Id
:= Declarations
(N
);
11554 Set_Declarations
(N
, New_List
(Nod
));
11556 Append_To
(Decls
, Nod
);
11558 end Insert_After_Last_Declaration
;
11560 ------------------------------
11561 -- Is_Public_Subprogram_For --
11562 ------------------------------
11564 -- The type T is a private type, its declaration is therefore in
11565 -- the list of public declarations of some package. The test for a
11566 -- public subprogram is that its declaration is in this same list
11567 -- of declarations for the same package (note that all the public
11568 -- declarations are in one list, and all the private declarations
11569 -- in another, so this deals with the public/private distinction).
11571 function Is_Public_Subprogram_For
(T
: Entity_Id
) return Boolean is
11572 DD
: constant Node_Id
:= Unit_Declaration_Node
(Designator
);
11573 -- The subprogram declaration for the subprogram in question
11575 TL
: constant List_Id
:=
11576 Visible_Declarations
11577 (Specification
(Unit_Declaration_Node
(Scope
(T
))));
11578 -- The list of declarations containing the private declaration of
11579 -- the type. We know it is a private type, so we know its scope is
11580 -- the package in question, and we know it must be in the visible
11581 -- declarations of this package.
11584 -- If the subprogram declaration is not a list member, it must be
11585 -- an Init_Proc, in which case we want to consider it to be a
11586 -- public subprogram, since we do get initializations to deal with.
11587 -- Other internally generated subprograms are not public.
11589 if not Is_List_Member
(DD
)
11590 and then Is_Init_Proc
(Defining_Entity
(DD
))
11594 -- The declaration may have been generated for an expression function
11595 -- so check whether that function comes from source.
11597 elsif not Comes_From_Source
(DD
)
11599 (Nkind
(Original_Node
(DD
)) /= N_Expression_Function
11600 or else not Comes_From_Source
(Defining_Entity
(DD
)))
11604 -- Otherwise we test whether the subprogram is declared in the
11605 -- visible declarations of the package containing the type.
11608 return TL
= List_Containing
(DD
);
11610 end Is_Public_Subprogram_For
;
11615 Formal_Typ
: Entity_Id
;
11616 Func_Typ
: Entity_Id
;
11617 Post_Proc
: Entity_Id
;
11620 -- Start of processing for Process_PPCs
11623 -- Capture designator from spec if present, else from body
11625 if Present
(Spec_Id
) then
11626 Designator
:= Spec_Id
;
11628 Designator
:= Body_Id
;
11631 -- Do not process a predicate function as its body will contain a
11632 -- recursive call to itself and blow up the stack.
11634 if Ekind
(Designator
) = E_Function
11635 and then Is_Predicate_Function
(Designator
)
11639 -- Internally generated subprograms, such as type-specific functions,
11640 -- don't get assertion checks.
11642 elsif Get_TSS_Name
(Designator
) /= TSS_Null
then
11646 -- Grab preconditions from spec
11648 if Present
(Spec_Id
) then
11650 -- Loop through PPC pragmas from spec. Note that preconditions from
11651 -- the body will be analyzed and converted when we scan the body
11652 -- declarations below.
11654 Prag
:= Pre_Post_Conditions
(Contract
(Spec_Id
));
11655 while Present
(Prag
) loop
11656 if Pragma_Name
(Prag
) = Name_Precondition
then
11658 -- For Pre (or Precondition pragma), we simply prepend the
11659 -- pragma to the list of declarations right away so that it
11660 -- will be executed at the start of the procedure. Note that
11661 -- this processing reverses the order of the list, which is
11662 -- what we want since new entries were chained to the head of
11663 -- the list. There can be more than one precondition when we
11664 -- use pragma Precondition.
11666 if not Class_Present
(Prag
) then
11667 Prepend
(Grab_PPC
, Declarations
(N
));
11669 -- For Pre'Class there can only be one pragma, and we save
11670 -- it in Precond for now. We will add inherited Pre'Class
11671 -- stuff before inserting this pragma in the declarations.
11673 Precond
:= Grab_PPC
;
11677 Prag
:= Next_Pragma
(Prag
);
11680 -- Now deal with inherited preconditions
11682 for J
in Inherited
'Range loop
11683 Prag
:= Pre_Post_Conditions
(Contract
(Inherited
(J
)));
11685 while Present
(Prag
) loop
11686 if Pragma_Name
(Prag
) = Name_Precondition
11687 and then Class_Present
(Prag
)
11689 Inherited_Precond
:= Grab_PPC
(Inherited
(J
));
11691 -- No precondition so far, so establish this as the first
11693 if No
(Precond
) then
11694 Precond
:= Inherited_Precond
;
11696 -- Here we already have a precondition, add inherited one
11699 -- Add new precondition to old one using OR ELSE
11702 New_Expr
: constant Node_Id
:=
11704 (Next
(First
(Pragma_Argument_Associations
11705 (Inherited_Precond
))));
11706 Old_Expr
: constant Node_Id
:=
11708 (Next
(First
(Pragma_Argument_Associations
11712 if Paren_Count
(Old_Expr
) = 0 then
11713 Set_Paren_Count
(Old_Expr
, 1);
11716 if Paren_Count
(New_Expr
) = 0 then
11717 Set_Paren_Count
(New_Expr
, 1);
11721 Make_Or_Else
(Sloc
(Old_Expr
),
11722 Left_Opnd
=> Relocate_Node
(Old_Expr
),
11723 Right_Opnd
=> New_Expr
));
11726 -- Add new message in the form:
11728 -- failed precondition from bla
11729 -- also failed inherited precondition from bla
11732 -- Skip this if exception locations are suppressed
11734 if not Exception_Locations_Suppressed
then
11736 New_Msg
: constant Node_Id
:=
11739 (Pragma_Argument_Associations
11740 (Inherited_Precond
)));
11741 Old_Msg
: constant Node_Id
:=
11744 (Pragma_Argument_Associations
11747 Start_String
(Strval
(Old_Msg
));
11748 Store_String_Chars
(ASCII
.LF
& " also ");
11749 Store_String_Chars
(Strval
(New_Msg
));
11750 Set_Strval
(Old_Msg
, End_String
);
11756 Prag
:= Next_Pragma
(Prag
);
11760 -- If we have built a precondition for Pre'Class (including any
11761 -- Pre'Class aspects inherited from parent subprograms), then we
11762 -- insert this composite precondition at this stage.
11764 if Present
(Precond
) then
11765 Prepend
(Precond
, Declarations
(N
));
11769 -- Build postconditions procedure if needed and prepend the following
11770 -- declaration to the start of the declarations for the subprogram.
11772 -- procedure _postconditions [(_Result : resulttype)] is
11774 -- pragma Check (Postcondition, condition [,message]);
11775 -- pragma Check (Postcondition, condition [,message]);
11777 -- Invariant_Procedure (_Result) ...
11778 -- Invariant_Procedure (Arg1)
11782 -- First we deal with the postconditions in the body
11784 if Is_Non_Empty_List
(Declarations
(N
)) then
11786 -- Loop through declarations
11788 Prag
:= First
(Declarations
(N
));
11789 while Present
(Prag
) loop
11790 if Nkind
(Prag
) = N_Pragma
then
11792 -- Capture postcondition pragmas
11794 if Pragma_Name
(Prag
) = Name_Postcondition
then
11797 -- If expansion is disabled, as in a generic unit, save
11798 -- pragma for later expansion.
11800 if not Expander_Active
then
11801 Prepend
(Grab_PPC
, Declarations
(N
));
11803 Append_Enabled_Item
(Grab_PPC
, Plist
);
11809 -- Not a pragma, if comes from source, then end scan
11811 elsif Comes_From_Source
(Prag
) then
11814 -- Skip stuff not coming from source
11822 -- Now deal with any postconditions from the spec
11824 if Present
(Spec_Id
) then
11825 Spec_Postconditions
: declare
11826 procedure Process_Contract_Cases
(Spec
: Node_Id
);
11827 -- This processes the Contract_Test_Cases from Spec, processing
11828 -- any contract-cases from the list. The caller has checked that
11829 -- Contract_Test_Cases is non-Empty.
11831 procedure Process_Post_Conditions
11834 -- This processes the Pre_Post_Conditions from Spec, processing
11835 -- any postconditions from the list. If Class is True, then only
11836 -- postconditions marked with Class_Present are considered. The
11837 -- caller has checked that Pre_Post_Conditions is non-Empty.
11839 ----------------------------
11840 -- Process_Contract_Cases --
11841 ----------------------------
11843 procedure Process_Contract_Cases
(Spec
: Node_Id
) is
11845 -- Loop through Contract_Cases pragmas from spec
11847 Prag
:= Contract_Test_Cases
(Contract
(Spec
));
11849 if Pragma_Name
(Prag
) = Name_Contract_Cases
then
11850 Expand_Contract_Cases
11852 Subp_Id
=> Spec_Id
,
11853 Decls
=> Declarations
(N
),
11857 Prag
:= Next_Pragma
(Prag
);
11858 exit when No
(Prag
);
11860 end Process_Contract_Cases
;
11862 -----------------------------
11863 -- Process_Post_Conditions --
11864 -----------------------------
11866 procedure Process_Post_Conditions
11879 -- Loop through PPC pragmas from spec
11881 Prag
:= Pre_Post_Conditions
(Contract
(Spec
));
11883 if Pragma_Name
(Prag
) = Name_Postcondition
11884 and then (not Class
or else Class_Present
(Prag
))
11886 if not Expander_Active
then
11887 Prepend
(Grab_PPC
(Pspec
), Declarations
(N
));
11889 Append_Enabled_Item
(Grab_PPC
(Pspec
), Plist
);
11893 Prag
:= Next_Pragma
(Prag
);
11894 exit when No
(Prag
);
11896 end Process_Post_Conditions
;
11898 -- Start of processing for Spec_Postconditions
11901 -- Process postconditions expressed as contract-cases
11903 if Present
(Contract_Test_Cases
(Contract
(Spec_Id
))) then
11904 Process_Contract_Cases
(Spec_Id
);
11907 -- Process spec postconditions
11909 if Present
(Pre_Post_Conditions
(Contract
(Spec_Id
))) then
11910 Process_Post_Conditions
(Spec_Id
, Class
=> False);
11913 -- Process inherited postconditions
11915 for J
in Inherited
'Range loop
11916 if Present
(Pre_Post_Conditions
(Contract
(Inherited
(J
)))) then
11917 Process_Post_Conditions
(Inherited
(J
), Class
=> True);
11920 end Spec_Postconditions
;
11923 -- Add an invariant call to check the result of a function
11925 if Ekind
(Designator
) /= E_Procedure
and then Expander_Active
then
11926 Func_Typ
:= Etype
(Designator
);
11927 Result
:= Make_Defining_Identifier
(Loc
, Name_uResult
);
11929 Set_Etype
(Result
, Func_Typ
);
11931 -- Add argument for return
11933 Parms
:= New_List
(
11934 Make_Parameter_Specification
(Loc
,
11935 Defining_Identifier
=> Result
,
11936 Parameter_Type
=> New_Occurrence_Of
(Func_Typ
, Loc
)));
11938 -- Add invariant call if returning type with invariants and this is a
11939 -- public function, i.e. a function declared in the visible part of
11940 -- the package defining the private type.
11942 if Has_Invariants
(Func_Typ
)
11943 and then Present
(Invariant_Procedure
(Func_Typ
))
11944 and then not Has_Null_Body
(Invariant_Procedure
(Func_Typ
))
11945 and then Is_Public_Subprogram_For
(Func_Typ
)
11947 Append_Enabled_Item
11948 (Make_Invariant_Call
(New_Occurrence_Of
(Result
, Loc
)), Plist
);
11951 -- Same if return value is an access to type with invariants
11953 Check_Access_Invariants
(Result
);
11961 -- Add invariant calls and predicate calls for parameters. Note that
11962 -- this is done for functions as well, since in Ada 2012 they can have
11965 if Expander_Active
then
11966 Formal
:= First_Formal
(Designator
);
11967 while Present
(Formal
) loop
11968 if Ekind
(Formal
) /= E_In_Parameter
11969 or else Is_Access_Type
(Etype
(Formal
))
11971 Formal_Typ
:= Etype
(Formal
);
11973 if Has_Invariants
(Formal_Typ
)
11974 and then Present
(Invariant_Procedure
(Formal_Typ
))
11975 and then not Has_Null_Body
(Invariant_Procedure
(Formal_Typ
))
11976 and then Is_Public_Subprogram_For
(Formal_Typ
)
11978 Append_Enabled_Item
11979 (Make_Invariant_Call
(New_Occurrence_Of
(Formal
, Loc
)),
11983 Check_Access_Invariants
(Formal
);
11985 if Has_Predicates
(Formal_Typ
)
11986 and then Present
(Predicate_Function
(Formal_Typ
))
11987 and then Has_Checked_Predicate
(Formal_Typ
)
11989 Append_Enabled_Item
11990 (Make_Predicate_Check
11991 (Formal_Typ
, New_Occurrence_Of
(Formal
, Loc
)),
11996 Next_Formal
(Formal
);
12000 -- Build and insert postcondition procedure
12002 if Expander_Active
and then Present
(Plist
) then
12004 Make_Defining_Identifier
(Loc
, Chars
=> Name_uPostconditions
);
12006 -- Insert the corresponding body of a post condition pragma after the
12007 -- last declaration of the context. This ensures that the body will
12008 -- not cause any premature freezing as it may mention types:
12010 -- procedure Proc (Obj : Array_Typ) is
12011 -- procedure _postconditions is
12014 -- end _postconditions;
12016 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
12019 -- In the example above, Obj is of type T but the incorrect placement
12020 -- of _postconditions will cause a crash in gigi due to an out of
12021 -- order reference. The body of _postconditions must be placed after
12022 -- the declaration of Temp to preserve correct visibility.
12024 Insert_After_Last_Declaration
(
12025 Make_Subprogram_Body
(Loc
,
12027 Make_Procedure_Specification
(Loc
,
12028 Defining_Unit_Name
=> Post_Proc
,
12029 Parameter_Specifications
=> Parms
),
12031 Declarations
=> Empty_List
,
12033 Handled_Statement_Sequence
=>
12034 Make_Handled_Sequence_Of_Statements
(Loc
,
12035 Statements
=> Plist
)));
12037 Set_Ekind
(Post_Proc
, E_Procedure
);
12039 -- If this is a procedure, set the Postcondition_Proc attribute on
12040 -- the proper defining entity for the subprogram.
12042 if Ekind
(Designator
) = E_Procedure
then
12043 Set_Postcondition_Proc
(Designator
, Post_Proc
);
12046 Set_Has_Postconditions
(Designator
);
12050 ----------------------------
12051 -- Reference_Body_Formals --
12052 ----------------------------
12054 procedure Reference_Body_Formals
(Spec
: Entity_Id
; Bod
: Entity_Id
) is
12059 if Error_Posted
(Spec
) then
12063 -- Iterate over both lists. They may be of different lengths if the two
12064 -- specs are not conformant.
12066 Fs
:= First_Formal
(Spec
);
12067 Fb
:= First_Formal
(Bod
);
12068 while Present
(Fs
) and then Present
(Fb
) loop
12069 Generate_Reference
(Fs
, Fb
, 'b');
12071 if Style_Check
then
12072 Style
.Check_Identifier
(Fb
, Fs
);
12075 Set_Spec_Entity
(Fb
, Fs
);
12076 Set_Referenced
(Fs
, False);
12080 end Reference_Body_Formals
;
12082 -------------------------
12083 -- Set_Actual_Subtypes --
12084 -------------------------
12086 procedure Set_Actual_Subtypes
(N
: Node_Id
; Subp
: Entity_Id
) is
12088 Formal
: Entity_Id
;
12090 First_Stmt
: Node_Id
:= Empty
;
12091 AS_Needed
: Boolean;
12094 -- If this is an empty initialization procedure, no need to create
12095 -- actual subtypes (small optimization).
12097 if Ekind
(Subp
) = E_Procedure
and then Is_Null_Init_Proc
(Subp
) then
12101 Formal
:= First_Formal
(Subp
);
12102 while Present
(Formal
) loop
12103 T
:= Etype
(Formal
);
12105 -- We never need an actual subtype for a constrained formal
12107 if Is_Constrained
(T
) then
12108 AS_Needed
:= False;
12110 -- If we have unknown discriminants, then we do not need an actual
12111 -- subtype, or more accurately we cannot figure it out! Note that
12112 -- all class-wide types have unknown discriminants.
12114 elsif Has_Unknown_Discriminants
(T
) then
12115 AS_Needed
:= False;
12117 -- At this stage we have an unconstrained type that may need an
12118 -- actual subtype. For sure the actual subtype is needed if we have
12119 -- an unconstrained array type.
12121 elsif Is_Array_Type
(T
) then
12124 -- The only other case needing an actual subtype is an unconstrained
12125 -- record type which is an IN parameter (we cannot generate actual
12126 -- subtypes for the OUT or IN OUT case, since an assignment can
12127 -- change the discriminant values. However we exclude the case of
12128 -- initialization procedures, since discriminants are handled very
12129 -- specially in this context, see the section entitled "Handling of
12130 -- Discriminants" in Einfo.
12132 -- We also exclude the case of Discrim_SO_Functions (functions used
12133 -- in front end layout mode for size/offset values), since in such
12134 -- functions only discriminants are referenced, and not only are such
12135 -- subtypes not needed, but they cannot always be generated, because
12136 -- of order of elaboration issues.
12138 elsif Is_Record_Type
(T
)
12139 and then Ekind
(Formal
) = E_In_Parameter
12140 and then Chars
(Formal
) /= Name_uInit
12141 and then not Is_Unchecked_Union
(T
)
12142 and then not Is_Discrim_SO_Function
(Subp
)
12146 -- All other cases do not need an actual subtype
12149 AS_Needed
:= False;
12152 -- Generate actual subtypes for unconstrained arrays and
12153 -- unconstrained discriminated records.
12156 if Nkind
(N
) = N_Accept_Statement
then
12158 -- If expansion is active, the formal is replaced by a local
12159 -- variable that renames the corresponding entry of the
12160 -- parameter block, and it is this local variable that may
12161 -- require an actual subtype.
12163 if Full_Expander_Active
then
12164 Decl
:= Build_Actual_Subtype
(T
, Renamed_Object
(Formal
));
12166 Decl
:= Build_Actual_Subtype
(T
, Formal
);
12169 if Present
(Handled_Statement_Sequence
(N
)) then
12171 First
(Statements
(Handled_Statement_Sequence
(N
)));
12172 Prepend
(Decl
, Statements
(Handled_Statement_Sequence
(N
)));
12173 Mark_Rewrite_Insertion
(Decl
);
12175 -- If the accept statement has no body, there will be no
12176 -- reference to the actuals, so no need to compute actual
12183 Decl
:= Build_Actual_Subtype
(T
, Formal
);
12184 Prepend
(Decl
, Declarations
(N
));
12185 Mark_Rewrite_Insertion
(Decl
);
12188 -- The declaration uses the bounds of an existing object, and
12189 -- therefore needs no constraint checks.
12191 Analyze
(Decl
, Suppress
=> All_Checks
);
12193 -- We need to freeze manually the generated type when it is
12194 -- inserted anywhere else than in a declarative part.
12196 if Present
(First_Stmt
) then
12197 Insert_List_Before_And_Analyze
(First_Stmt
,
12198 Freeze_Entity
(Defining_Identifier
(Decl
), N
));
12201 if Nkind
(N
) = N_Accept_Statement
12202 and then Full_Expander_Active
12204 Set_Actual_Subtype
(Renamed_Object
(Formal
),
12205 Defining_Identifier
(Decl
));
12207 Set_Actual_Subtype
(Formal
, Defining_Identifier
(Decl
));
12211 Next_Formal
(Formal
);
12213 end Set_Actual_Subtypes
;
12215 ---------------------
12216 -- Set_Formal_Mode --
12217 ---------------------
12219 procedure Set_Formal_Mode
(Formal_Id
: Entity_Id
) is
12220 Spec
: constant Node_Id
:= Parent
(Formal_Id
);
12223 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
12224 -- since we ensure that corresponding actuals are always valid at the
12225 -- point of the call.
12227 if Out_Present
(Spec
) then
12228 if Ekind
(Scope
(Formal_Id
)) = E_Function
12229 or else Ekind
(Scope
(Formal_Id
)) = E_Generic_Function
12231 -- [IN] OUT parameters allowed for functions in Ada 2012
12233 if Ada_Version
>= Ada_2012
then
12235 -- Even in Ada 2012 operators can only have IN parameters
12237 if Is_Operator_Symbol_Name
(Chars
(Scope
(Formal_Id
))) then
12238 Error_Msg_N
("operators can only have IN parameters", Spec
);
12241 if In_Present
(Spec
) then
12242 Set_Ekind
(Formal_Id
, E_In_Out_Parameter
);
12244 Set_Ekind
(Formal_Id
, E_Out_Parameter
);
12247 -- But not in earlier versions of Ada
12250 Error_Msg_N
("functions can only have IN parameters", Spec
);
12251 Set_Ekind
(Formal_Id
, E_In_Parameter
);
12254 elsif In_Present
(Spec
) then
12255 Set_Ekind
(Formal_Id
, E_In_Out_Parameter
);
12258 Set_Ekind
(Formal_Id
, E_Out_Parameter
);
12259 Set_Never_Set_In_Source
(Formal_Id
, True);
12260 Set_Is_True_Constant
(Formal_Id
, False);
12261 Set_Current_Value
(Formal_Id
, Empty
);
12265 Set_Ekind
(Formal_Id
, E_In_Parameter
);
12268 -- Set Is_Known_Non_Null for access parameters since the language
12269 -- guarantees that access parameters are always non-null. We also set
12270 -- Can_Never_Be_Null, since there is no way to change the value.
12272 if Nkind
(Parameter_Type
(Spec
)) = N_Access_Definition
then
12274 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12275 -- null; In Ada 2005, only if then null_exclusion is explicit.
12277 if Ada_Version
< Ada_2005
12278 or else Can_Never_Be_Null
(Etype
(Formal_Id
))
12280 Set_Is_Known_Non_Null
(Formal_Id
);
12281 Set_Can_Never_Be_Null
(Formal_Id
);
12284 -- Ada 2005 (AI-231): Null-exclusion access subtype
12286 elsif Is_Access_Type
(Etype
(Formal_Id
))
12287 and then Can_Never_Be_Null
(Etype
(Formal_Id
))
12289 Set_Is_Known_Non_Null
(Formal_Id
);
12291 -- We can also set Can_Never_Be_Null (thus preventing some junk
12292 -- access checks) for the case of an IN parameter, which cannot
12293 -- be changed, or for an IN OUT parameter, which can be changed but
12294 -- not to a null value. But for an OUT parameter, the initial value
12295 -- passed in can be null, so we can't set this flag in that case.
12297 if Ekind
(Formal_Id
) /= E_Out_Parameter
then
12298 Set_Can_Never_Be_Null
(Formal_Id
);
12302 Set_Mechanism
(Formal_Id
, Default_Mechanism
);
12303 Set_Formal_Validity
(Formal_Id
);
12304 end Set_Formal_Mode
;
12306 -------------------------
12307 -- Set_Formal_Validity --
12308 -------------------------
12310 procedure Set_Formal_Validity
(Formal_Id
: Entity_Id
) is
12312 -- If no validity checking, then we cannot assume anything about the
12313 -- validity of parameters, since we do not know there is any checking
12314 -- of the validity on the call side.
12316 if not Validity_Checks_On
then
12319 -- If validity checking for parameters is enabled, this means we are
12320 -- not supposed to make any assumptions about argument values.
12322 elsif Validity_Check_Parameters
then
12325 -- If we are checking in parameters, we will assume that the caller is
12326 -- also checking parameters, so we can assume the parameter is valid.
12328 elsif Ekind
(Formal_Id
) = E_In_Parameter
12329 and then Validity_Check_In_Params
12331 Set_Is_Known_Valid
(Formal_Id
, True);
12333 -- Similar treatment for IN OUT parameters
12335 elsif Ekind
(Formal_Id
) = E_In_Out_Parameter
12336 and then Validity_Check_In_Out_Params
12338 Set_Is_Known_Valid
(Formal_Id
, True);
12340 end Set_Formal_Validity
;
12342 ------------------------
12343 -- Subtype_Conformant --
12344 ------------------------
12346 function Subtype_Conformant
12347 (New_Id
: Entity_Id
;
12348 Old_Id
: Entity_Id
;
12349 Skip_Controlling_Formals
: Boolean := False) return Boolean
12353 Check_Conformance
(New_Id
, Old_Id
, Subtype_Conformant
, False, Result
,
12354 Skip_Controlling_Formals
=> Skip_Controlling_Formals
);
12356 end Subtype_Conformant
;
12358 ---------------------
12359 -- Type_Conformant --
12360 ---------------------
12362 function Type_Conformant
12363 (New_Id
: Entity_Id
;
12364 Old_Id
: Entity_Id
;
12365 Skip_Controlling_Formals
: Boolean := False) return Boolean
12369 May_Hide_Profile
:= False;
12372 (New_Id
, Old_Id
, Type_Conformant
, False, Result
,
12373 Skip_Controlling_Formals
=> Skip_Controlling_Formals
);
12375 end Type_Conformant
;
12377 -------------------------------
12378 -- Valid_Operator_Definition --
12379 -------------------------------
12381 procedure Valid_Operator_Definition
(Designator
: Entity_Id
) is
12384 Id
: constant Name_Id
:= Chars
(Designator
);
12388 F
:= First_Formal
(Designator
);
12389 while Present
(F
) loop
12392 if Present
(Default_Value
(F
)) then
12394 ("default values not allowed for operator parameters",
12401 -- Verify that user-defined operators have proper number of arguments
12402 -- First case of operators which can only be unary
12404 if Nam_In
(Id
, Name_Op_Not
, Name_Op_Abs
) then
12407 -- Case of operators which can be unary or binary
12409 elsif Nam_In
(Id
, Name_Op_Add
, Name_Op_Subtract
) then
12410 N_OK
:= (N
in 1 .. 2);
12412 -- All other operators can only be binary
12420 ("incorrect number of arguments for operator", Designator
);
12424 and then Base_Type
(Etype
(Designator
)) = Standard_Boolean
12425 and then not Is_Intrinsic_Subprogram
(Designator
)
12428 ("explicit definition of inequality not allowed", Designator
);
12430 end Valid_Operator_Definition
;