]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch6.adb
configure.ac: Update comment.
[thirdparty/gcc.git] / gcc / ada / sem_ch6.adb
CommitLineData
996ae0b0
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ C H 6 --
6-- --
7-- B o d y --
996ae0b0 8-- --
77a40ec1 9-- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
996ae0b0
RK
10-- --
11-- GNAT is free software; you can redistribute it and/or modify it under --
12-- terms of the GNU General Public License as published by the Free Soft- --
b5c84c3c 13-- ware Foundation; either version 3, or (at your option) any later ver- --
996ae0b0
RK
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 --
b5c84c3c
RD
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. --
996ae0b0
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
996ae0b0
RK
23-- --
24------------------------------------------------------------------------------
25
80e59506 26with Aspects; use Aspects;
996ae0b0
RK
27with Atree; use Atree;
28with Checks; use Checks;
29with Debug; use Debug;
30with Einfo; use Einfo;
31with Elists; use Elists;
32with Errout; use Errout;
33with Expander; use Expander;
ec4867fa 34with Exp_Ch6; use Exp_Ch6;
996ae0b0 35with Exp_Ch7; use Exp_Ch7;
21d27997 36with Exp_Ch9; use Exp_Ch9;
616547fa 37with Exp_Dbug; use Exp_Dbug;
ce2b6ba5 38with Exp_Disp; use Exp_Disp;
e660dbf7 39with Exp_Tss; use Exp_Tss;
ec4867fa 40with Exp_Util; use Exp_Util;
fbf5a39b 41with Fname; use Fname;
996ae0b0 42with Freeze; use Freeze;
540d8610 43with Inline; use Inline;
41251c60 44with Itypes; use Itypes;
996ae0b0 45with Lib.Xref; use Lib.Xref;
ec4867fa 46with Layout; use Layout;
996ae0b0
RK
47with Namet; use Namet;
48with Lib; use Lib;
49with Nlists; use Nlists;
50with Nmake; use Nmake;
51with Opt; use Opt;
52with Output; use Output;
b20de9b9
AC
53with Restrict; use Restrict;
54with Rident; use Rident;
996ae0b0
RK
55with Rtsfind; use Rtsfind;
56with Sem; use Sem;
a4100e55 57with Sem_Aux; use Sem_Aux;
996ae0b0
RK
58with Sem_Cat; use Sem_Cat;
59with Sem_Ch3; use Sem_Ch3;
60with Sem_Ch4; use Sem_Ch4;
61with Sem_Ch5; use Sem_Ch5;
62with Sem_Ch8; use Sem_Ch8;
9bc856dd 63with Sem_Ch10; use Sem_Ch10;
996ae0b0 64with Sem_Ch12; use Sem_Ch12;
0f1a6a0b 65with Sem_Ch13; use Sem_Ch13;
dec6faf1 66with Sem_Dim; use Sem_Dim;
996ae0b0
RK
67with Sem_Disp; use Sem_Disp;
68with Sem_Dist; use Sem_Dist;
69with Sem_Elim; use Sem_Elim;
70with Sem_Eval; use Sem_Eval;
71with Sem_Mech; use Sem_Mech;
72with Sem_Prag; use Sem_Prag;
73with Sem_Res; use Sem_Res;
74with Sem_Util; use Sem_Util;
75with Sem_Type; use Sem_Type;
76with Sem_Warn; use Sem_Warn;
77with Sinput; use Sinput;
78with Stand; use Stand;
79with Sinfo; use Sinfo;
80with Sinfo.CN; use Sinfo.CN;
81with Snames; use Snames;
82with Stringt; use Stringt;
83with Style;
84with Stylesw; use Stylesw;
8417f4b2 85with Targparm; use Targparm;
996ae0b0
RK
86with Tbuild; use Tbuild;
87with Uintp; use Uintp;
88with Urealp; use Urealp;
89with Validsw; use Validsw;
90
91package body Sem_Ch6 is
92
c8ef728f 93 May_Hide_Profile : Boolean := False;
ec4867fa
ES
94 -- This flag is used to indicate that two formals in two subprograms being
95 -- checked for conformance differ only in that one is an access parameter
96 -- while the other is of a general access type with the same designated
97 -- type. In this case, if the rest of the signatures match, a call to
98 -- either subprogram may be ambiguous, which is worth a warning. The flag
99 -- is set in Compatible_Types, and the warning emitted in
100 -- New_Overloaded_Entity.
c8ef728f 101
996ae0b0
RK
102 -----------------------
103 -- Local Subprograms --
104 -----------------------
105
4d8f3296
ES
106 procedure Analyze_Null_Procedure
107 (N : Node_Id;
108 Is_Completion : out Boolean);
9d2a2071 109 -- A null procedure can be a declaration or (Ada 2012) a completion
4d8f3296 110
5d37ba92 111 procedure Analyze_Return_Statement (N : Node_Id);
5b9c3fc4 112 -- Common processing for simple and extended return statements
ec4867fa
ES
113
114 procedure Analyze_Function_Return (N : Node_Id);
81db9d77
ES
115 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
116 -- applies to a [generic] function.
ec4867fa 117
82c80734
RD
118 procedure Analyze_Return_Type (N : Node_Id);
119 -- Subsidiary to Process_Formals: analyze subtype mark in function
5b9c3fc4 120 -- specification in a context where the formals are visible and hide
82c80734
RD
121 -- outer homographs.
122
b1b543d2 123 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
13d923cc
RD
124 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
125 -- that we can use RETURN but not skip the debug output at the end.
b1b543d2 126
996ae0b0 127 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
82c80734
RD
128 -- Analyze a generic subprogram body. N is the body to be analyzed, and
129 -- Gen_Id is the defining entity Id for the corresponding spec.
996ae0b0 130
806f6d37
AC
131 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
132 -- Returns true if Subp can override a predefined operator.
133
996ae0b0 134 procedure Check_Conformance
41251c60
JM
135 (New_Id : Entity_Id;
136 Old_Id : Entity_Id;
137 Ctype : Conformance_Type;
138 Errmsg : Boolean;
139 Conforms : out Boolean;
140 Err_Loc : Node_Id := Empty;
141 Get_Inst : Boolean := False;
142 Skip_Controlling_Formals : Boolean := False);
996ae0b0
RK
143 -- Given two entities, this procedure checks that the profiles associated
144 -- with these entities meet the conformance criterion given by the third
145 -- parameter. If they conform, Conforms is set True and control returns
146 -- to the caller. If they do not conform, Conforms is set to False, and
147 -- in addition, if Errmsg is True on the call, proper messages are output
148 -- to complain about the conformance failure. If Err_Loc is non_Empty
149 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
150 -- error messages are placed on the appropriate part of the construct
151 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
152 -- against a formal access-to-subprogram type so Get_Instance_Of must
153 -- be called.
154
155 procedure Check_Subprogram_Order (N : Node_Id);
156 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
157 -- the alpha ordering rule for N if this ordering requirement applicable.
158
996ae0b0
RK
159 procedure Check_Returns
160 (HSS : Node_Id;
161 Mode : Character;
c8ef728f
ES
162 Err : out Boolean;
163 Proc : Entity_Id := Empty);
164 -- Called to check for missing return statements in a function body, or for
0a36105d 165 -- returns present in a procedure body which has No_Return set. HSS is the
c8ef728f
ES
166 -- handled statement sequence for the subprogram body. This procedure
167 -- checks all flow paths to make sure they either have return (Mode = 'F',
168 -- used for functions) or do not have a return (Mode = 'P', used for
169 -- No_Return procedures). The flag Err is set if there are any control
170 -- paths not explicitly terminated by a return in the function case, and is
171 -- True otherwise. Proc is the entity for the procedure case and is used
172 -- in posting the warning message.
996ae0b0 173
e5a58fac
AC
174 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
175 -- In Ada 2012, a primitive equality operator on an untagged record type
176 -- must appear before the type is frozen, and have the same visibility as
177 -- that of the type. This procedure checks that this rule is met, and
178 -- otherwise emits an error on the subprogram declaration and a warning
b2834fbd
AC
179 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
180 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
181 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
182 -- is set, otherwise the call has no effect.
e5a58fac 183
996ae0b0 184 procedure Enter_Overloaded_Entity (S : Entity_Id);
82c80734
RD
185 -- This procedure makes S, a new overloaded entity, into the first visible
186 -- entity with that name.
996ae0b0 187
a5b62485
AC
188 function Is_Non_Overriding_Operation
189 (Prev_E : Entity_Id;
190 New_E : Entity_Id) return Boolean;
191 -- Enforce the rule given in 12.3(18): a private operation in an instance
192 -- overrides an inherited operation only if the corresponding operation
260359e3
AC
193 -- was overriding in the generic. This needs to be checked for primitive
194 -- operations of types derived (in the generic unit) from formal private
195 -- or formal derived types.
a5b62485 196
996ae0b0
RK
197 procedure Make_Inequality_Operator (S : Entity_Id);
198 -- Create the declaration for an inequality operator that is implicitly
199 -- created by a user-defined equality operator that yields a boolean.
200
996ae0b0
RK
201 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
202 -- Formal_Id is an formal parameter entity. This procedure deals with
e358346d
AC
203 -- setting the proper validity status for this entity, which depends on
204 -- the kind of parameter and the validity checking mode.
996ae0b0
RK
205
206 ---------------------------------------------
207 -- Analyze_Abstract_Subprogram_Declaration --
208 ---------------------------------------------
209
210 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
fbf5a39b
AC
211 Designator : constant Entity_Id :=
212 Analyze_Subprogram_Specification (Specification (N));
996ae0b0
RK
213 Scop : constant Entity_Id := Current_Scope;
214
215 begin
ce5ba43a 216 Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N);
38171f43 217
996ae0b0 218 Generate_Definition (Designator);
dac3bede 219 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
f937473f 220 Set_Is_Abstract_Subprogram (Designator);
996ae0b0
RK
221 New_Overloaded_Entity (Designator);
222 Check_Delayed_Subprogram (Designator);
223
fbf5a39b 224 Set_Categorization_From_Scope (Designator, Scop);
996ae0b0
RK
225
226 if Ekind (Scope (Designator)) = E_Protected_Type then
227 Error_Msg_N
228 ("abstract subprogram not allowed in protected type", N);
5d37ba92
ES
229
230 -- Issue a warning if the abstract subprogram is neither a dispatching
231 -- operation nor an operation that overrides an inherited subprogram or
232 -- predefined operator, since this most likely indicates a mistake.
233
234 elsif Warn_On_Redundant_Constructs
235 and then not Is_Dispatching_Operation (Designator)
038140ed 236 and then not Present (Overridden_Operation (Designator))
5d37ba92
ES
237 and then (not Is_Operator_Symbol_Name (Chars (Designator))
238 or else Scop /= Scope (Etype (First_Formal (Designator))))
239 then
240 Error_Msg_N
dbfeb4fa 241 ("abstract subprogram is not dispatching or overriding?r?", N);
996ae0b0 242 end if;
fbf5a39b
AC
243
244 Generate_Reference_To_Formals (Designator);
361effb1 245 Check_Eliminated (Designator);
eaba57fb
RD
246
247 if Has_Aspects (N) then
248 Analyze_Aspect_Specifications (N, Designator);
249 end if;
996ae0b0
RK
250 end Analyze_Abstract_Subprogram_Declaration;
251
b0186f71
AC
252 ---------------------------------
253 -- Analyze_Expression_Function --
254 ---------------------------------
255
256 procedure Analyze_Expression_Function (N : Node_Id) is
257 Loc : constant Source_Ptr := Sloc (N);
258 LocX : constant Source_Ptr := Sloc (Expression (N));
0b5b2bbc 259 Expr : constant Node_Id := Expression (N);
d2d4b355
AC
260 Spec : constant Node_Id := Specification (N);
261
8a06151a 262 Def_Id : Entity_Id;
b0186f71 263
8a06151a 264 Prev : Entity_Id;
b0186f71 265 -- If the expression is a completion, Prev is the entity whose
d2d4b355
AC
266 -- declaration is completed. Def_Id is needed to analyze the spec.
267
268 New_Body : Node_Id;
d2d4b355 269 New_Spec : Node_Id;
b913199e 270 Ret : Node_Id;
b0186f71
AC
271
272 begin
273 -- This is one of the occasions on which we transform the tree during
afc8324d 274 -- semantic analysis. If this is a completion, transform the expression
d2b10647
ES
275 -- function into an equivalent subprogram body, and analyze it.
276
277 -- Expression functions are inlined unconditionally. The back-end will
278 -- determine whether this is possible.
279
280 Inline_Processing_Required := True;
b727a82b
AC
281
282 -- Create a specification for the generated body. Types and defauts in
283 -- the profile are copies of the spec, but new entities must be created
284 -- for the unit name and the formals.
285
286 New_Spec := New_Copy_Tree (Spec);
287 Set_Defining_Unit_Name (New_Spec,
288 Make_Defining_Identifier (Sloc (Defining_Unit_Name (Spec)),
289 Chars (Defining_Unit_Name (Spec))));
290
291 if Present (Parameter_Specifications (New_Spec)) then
292 declare
293 Formal_Spec : Node_Id;
a6abfd78
AC
294 Def : Entity_Id;
295
b727a82b
AC
296 begin
297 Formal_Spec := First (Parameter_Specifications (New_Spec));
b2afe274
AC
298
299 -- Create a new formal parameter at the same source position
300
b727a82b 301 while Present (Formal_Spec) loop
a6abfd78
AC
302 Def := Defining_Identifier (Formal_Spec);
303 Set_Defining_Identifier (Formal_Spec,
304 Make_Defining_Identifier (Sloc (Def),
305 Chars => Chars (Def)));
b727a82b
AC
306 Next (Formal_Spec);
307 end loop;
308 end;
309 end if;
310
51597c23 311 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
d2d4b355
AC
312
313 -- If there are previous overloadable entities with the same name,
314 -- check whether any of them is completed by the expression function.
b04d926e 315 -- In a generic context a formal subprogram has no completion.
d2d4b355 316
b04d926e
AC
317 if Present (Prev) and then Is_Overloadable (Prev)
318 and then not Is_Formal_Subprogram (Prev)
319 then
51597c23
AC
320 Def_Id := Analyze_Subprogram_Specification (Spec);
321 Prev := Find_Corresponding_Spec (N);
d2d4b355 322 end if;
b0186f71 323
b913199e
AC
324 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
325
b0186f71
AC
326 New_Body :=
327 Make_Subprogram_Body (Loc,
d2d4b355 328 Specification => New_Spec,
b0186f71
AC
329 Declarations => Empty_List,
330 Handled_Statement_Sequence =>
331 Make_Handled_Sequence_Of_Statements (LocX,
b913199e 332 Statements => New_List (Ret)));
b0186f71 333
e7f23f06
AC
334 -- If the expression completes a generic subprogram, we must create a
335 -- separate node for the body, because at instantiation the original
336 -- node of the generic copy must be a generic subprogram body, and
337 -- cannot be a expression function. Otherwise we just rewrite the
338 -- expression with the non-generic body.
339
6d7e5c54 340 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
e7f23f06 341 Insert_After (N, New_Body);
6d7e5c54 342
e7f23f06
AC
343 -- Propagate any aspects or pragmas that apply to the expression
344 -- function to the proper body when the expression function acts
345 -- as a completion.
346
347 if Has_Aspects (N) then
348 Move_Aspects (N, To => New_Body);
349 end if;
350
351 Relocate_Pragmas_To_Body (New_Body);
b0186f71 352
b0186f71 353 Rewrite (N, Make_Null_Statement (Loc));
d2d4b355 354 Set_Has_Completion (Prev, False);
b0186f71
AC
355 Analyze (N);
356 Analyze (New_Body);
d2b10647 357 Set_Is_Inlined (Prev);
b0186f71 358
e5c4e2bc
AC
359 -- If the expression function is a completion, the previous declaration
360 -- must come from source. We know already that appears in the current
361 -- scope. The entity itself may be internally created if within a body
362 -- to be inlined.
363
b04d926e
AC
364 elsif Present (Prev) and then Comes_From_Source (Parent (Prev))
365 and then not Is_Formal_Subprogram (Prev)
366 then
d2d4b355 367 Set_Has_Completion (Prev, False);
76264f60 368
c6d2191a
AC
369 -- An expression function that is a completion freezes the
370 -- expression. This means freezing the return type, and if it is
371 -- an access type, freezing its designated type as well.
1b31321b 372
c6d2191a 373 -- Note that we cannot defer this freezing to the analysis of the
1b31321b
AC
374 -- expression itself, because a freeze node might appear in a nested
375 -- scope, leading to an elaboration order issue in gigi.
c6d2191a
AC
376
377 Freeze_Before (N, Etype (Prev));
1b31321b 378
c6d2191a
AC
379 if Is_Access_Type (Etype (Prev)) then
380 Freeze_Before (N, Designated_Type (Etype (Prev)));
381 end if;
382
76264f60
AC
383 -- For navigation purposes, indicate that the function is a body
384
385 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
b0186f71 386 Rewrite (N, New_Body);
e7f23f06 387
c0cdbd39
AC
388 -- Correct the parent pointer of the aspect specification list to
389 -- reference the rewritten node.
390
391 if Has_Aspects (N) then
392 Set_Parent (Aspect_Specifications (N), N);
393 end if;
394
e7f23f06
AC
395 -- Propagate any pragmas that apply to the expression function to the
396 -- proper body when the expression function acts as a completion.
397 -- Aspects are automatically transfered because of node rewriting.
398
399 Relocate_Pragmas_To_Body (N);
d2b10647
ES
400 Analyze (N);
401
6d7e5c54
AC
402 -- Prev is the previous entity with the same name, but it is can
403 -- be an unrelated spec that is not completed by the expression
404 -- function. In that case the relevant entity is the one in the body.
405 -- Not clear that the backend can inline it in this case ???
406
407 if Has_Completion (Prev) then
408 Set_Is_Inlined (Prev);
31af8899
AC
409
410 -- The formals of the expression function are body formals,
411 -- and do not appear in the ali file, which will only contain
412 -- references to the formals of the original subprogram spec.
413
414 declare
415 F1 : Entity_Id;
416 F2 : Entity_Id;
417
418 begin
419 F1 := First_Formal (Def_Id);
420 F2 := First_Formal (Prev);
421
422 while Present (F1) loop
423 Set_Spec_Entity (F1, F2);
424 Next_Formal (F1);
425 Next_Formal (F2);
426 end loop;
427 end;
428
6d7e5c54
AC
429 else
430 Set_Is_Inlined (Defining_Entity (New_Body));
431 end if;
432
0b5b2bbc 433 -- If this is not a completion, create both a declaration and a body, so
6d7e5c54 434 -- that the expression can be inlined whenever possible.
d2b10647
ES
435
436 else
a52e6d7e
AC
437 -- An expression function that is not a completion is not a
438 -- subprogram declaration, and thus cannot appear in a protected
439 -- definition.
440
441 if Nkind (Parent (N)) = N_Protected_Definition then
442 Error_Msg_N
443 ("an expression function is not a legal protected operation", N);
444 end if;
445
b8e6830b 446 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
c0cdbd39
AC
447
448 -- Correct the parent pointer of the aspect specification list to
449 -- reference the rewritten node.
450
451 if Has_Aspects (N) then
452 Set_Parent (Aspect_Specifications (N), N);
453 end if;
454
b0186f71 455 Analyze (N);
b04d926e 456
e699b76e
AC
457 -- Within a generic pre-analyze the original expression for name
458 -- capture. The body is also generated but plays no role in
459 -- this because it is not part of the original source.
b04d926e
AC
460
461 if Inside_A_Generic then
462 declare
463 Id : constant Entity_Id := Defining_Entity (N);
b04d926e
AC
464
465 begin
466 Set_Has_Completion (Id);
b04d926e
AC
467 Push_Scope (Id);
468 Install_Formals (Id);
e699b76e 469 Preanalyze_Spec_Expression (Expr, Etype (Id));
b04d926e 470 End_Scope;
b04d926e
AC
471 end;
472 end if;
473
b8e6830b
AC
474 Set_Is_Inlined (Defining_Entity (N));
475
476 -- Establish the linkages between the spec and the body. These are
477 -- used when the expression function acts as the prefix of attribute
478 -- 'Access in order to freeze the original expression which has been
479 -- moved to the generated body.
480
481 Set_Corresponding_Body (N, Defining_Entity (New_Body));
482 Set_Corresponding_Spec (New_Body, Defining_Entity (N));
d2b10647 483
6d7e5c54
AC
484 -- To prevent premature freeze action, insert the new body at the end
485 -- of the current declarations, or at the end of the package spec.
b913199e 486 -- However, resolve usage names now, to prevent spurious visibility
ad4e3362
ES
487 -- on later entities. Note that the function can now be called in
488 -- the current declarative part, which will appear to be prior to
489 -- the presence of the body in the code. There are nevertheless no
490 -- order of elaboration issues because all name resolution has taken
491 -- place at the point of declaration.
6d7e5c54
AC
492
493 declare
e876c43a
AC
494 Decls : List_Id := List_Containing (N);
495 Par : constant Node_Id := Parent (Decls);
b8e6830b 496 Id : constant Entity_Id := Defining_Entity (N);
6d7e5c54
AC
497
498 begin
fce54763
AC
499 -- If this is a wrapper created for in an instance for a formal
500 -- subprogram, insert body after declaration, to be analyzed when
501 -- the enclosing instance is analyzed.
502
503 if GNATprove_Mode
504 and then Is_Generic_Actual_Subprogram (Defining_Entity (N))
6d7e5c54 505 then
fce54763
AC
506 Insert_After (N, New_Body);
507
508 else
509 if Nkind (Par) = N_Package_Specification
510 and then Decls = Visible_Declarations (Par)
511 and then Present (Private_Declarations (Par))
512 and then not Is_Empty_List (Private_Declarations (Par))
513 then
514 Decls := Private_Declarations (Par);
515 end if;
6d7e5c54 516
fce54763
AC
517 Insert_After (Last (Decls), New_Body);
518 Push_Scope (Id);
519 Install_Formals (Id);
3a8e3f63 520
fce54763
AC
521 -- Preanalyze the expression for name capture, except in an
522 -- instance, where this has been done during generic analysis,
523 -- and will be redone when analyzing the body.
845f06e2 524
fce54763
AC
525 declare
526 Expr : constant Node_Id := Expression (Ret);
4058ddcc 527
fce54763
AC
528 begin
529 Set_Parent (Expr, Ret);
4058ddcc 530
fce54763
AC
531 if not In_Instance then
532 Preanalyze_Spec_Expression (Expr, Etype (Id));
533 end if;
534 end;
3a8e3f63 535
fce54763
AC
536 End_Scope;
537 end if;
6d7e5c54 538 end;
b0186f71 539 end if;
0b5b2bbc
AC
540
541 -- If the return expression is a static constant, we suppress warning
542 -- messages on unused formals, which in most cases will be noise.
543
544 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
545 Is_OK_Static_Expression (Expr));
b0186f71
AC
546 end Analyze_Expression_Function;
547
ec4867fa
ES
548 ----------------------------------------
549 -- Analyze_Extended_Return_Statement --
550 ----------------------------------------
551
552 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
553 begin
c86cf714 554 Check_Compiler_Unit ("extended return statement", N);
5d37ba92 555 Analyze_Return_Statement (N);
ec4867fa
ES
556 end Analyze_Extended_Return_Statement;
557
996ae0b0
RK
558 ----------------------------
559 -- Analyze_Function_Call --
560 ----------------------------
561
562 procedure Analyze_Function_Call (N : Node_Id) is
a7e68e7f
HK
563 Actuals : constant List_Id := Parameter_Associations (N);
564 Func_Nam : constant Node_Id := Name (N);
565 Actual : Node_Id;
566
996ae0b0 567 begin
a7e68e7f 568 Analyze (Func_Nam);
996ae0b0 569
3e7302c3
AC
570 -- A call of the form A.B (X) may be an Ada 2005 call, which is
571 -- rewritten as B (A, X). If the rewriting is successful, the call
572 -- has been analyzed and we just return.
82c80734 573
a7e68e7f
HK
574 if Nkind (Func_Nam) = N_Selected_Component
575 and then Name (N) /= Func_Nam
82c80734
RD
576 and then Is_Rewrite_Substitution (N)
577 and then Present (Etype (N))
578 then
579 return;
580 end if;
581
996ae0b0
RK
582 -- If error analyzing name, then set Any_Type as result type and return
583
a7e68e7f 584 if Etype (Func_Nam) = Any_Type then
996ae0b0
RK
585 Set_Etype (N, Any_Type);
586 return;
587 end if;
588
589 -- Otherwise analyze the parameters
590
e24329cd
YM
591 if Present (Actuals) then
592 Actual := First (Actuals);
996ae0b0
RK
593 while Present (Actual) loop
594 Analyze (Actual);
595 Check_Parameterless_Call (Actual);
596 Next (Actual);
597 end loop;
598 end if;
599
600 Analyze_Call (N);
996ae0b0
RK
601 end Analyze_Function_Call;
602
ec4867fa
ES
603 -----------------------------
604 -- Analyze_Function_Return --
605 -----------------------------
606
607 procedure Analyze_Function_Return (N : Node_Id) is
a7e68e7f
HK
608 Loc : constant Source_Ptr := Sloc (N);
609 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
610 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
ec4867fa 611
5d37ba92 612 R_Type : constant Entity_Id := Etype (Scope_Id);
ec4867fa
ES
613 -- Function result subtype
614
615 procedure Check_Limited_Return (Expr : Node_Id);
616 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
617 -- limited types. Used only for simple return statements.
618 -- Expr is the expression returned.
619
620 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
621 -- Check that the return_subtype_indication properly matches the result
622 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
623
624 --------------------------
625 -- Check_Limited_Return --
626 --------------------------
627
628 procedure Check_Limited_Return (Expr : Node_Id) is
629 begin
630 -- Ada 2005 (AI-318-02): Return-by-reference types have been
631 -- removed and replaced by anonymous access results. This is an
632 -- incompatibility with Ada 95. Not clear whether this should be
633 -- enforced yet or perhaps controllable with special switch. ???
634
ce72a9a3
AC
635 -- A limited interface that is not immutably limited is OK.
636
637 if Is_Limited_Interface (R_Type)
638 and then
639 not (Is_Task_Interface (R_Type)
640 or else Is_Protected_Interface (R_Type)
641 or else Is_Synchronized_Interface (R_Type))
642 then
643 null;
644
645 elsif Is_Limited_Type (R_Type)
646 and then not Is_Interface (R_Type)
ec4867fa
ES
647 and then Comes_From_Source (N)
648 and then not In_Instance_Body
2a31c32b 649 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
ec4867fa
ES
650 then
651 -- Error in Ada 2005
652
0791fbe9 653 if Ada_Version >= Ada_2005
ec4867fa
ES
654 and then not Debug_Flag_Dot_L
655 and then not GNAT_Mode
656 then
657 Error_Msg_N
3ccedacc
AC
658 ("(Ada 2005) cannot copy object of a limited type "
659 & "(RM-2005 6.5(5.5/2))", Expr);
e0ae93e2 660
51245e2d 661 if Is_Limited_View (R_Type) then
ec4867fa
ES
662 Error_Msg_N
663 ("\return by reference not permitted in Ada 2005", Expr);
664 end if;
665
666 -- Warn in Ada 95 mode, to give folks a heads up about this
667 -- incompatibility.
668
669 -- In GNAT mode, this is just a warning, to allow it to be
670 -- evilly turned off. Otherwise it is a real error.
671
9694c039
AC
672 -- In a generic context, simplify the warning because it makes
673 -- no sense to discuss pass-by-reference or copy.
674
ec4867fa 675 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
9694c039
AC
676 if Inside_A_Generic then
677 Error_Msg_N
885c4871 678 ("return of limited object not permitted in Ada 2005 "
dbfeb4fa 679 & "(RM-2005 6.5(5.5/2))?y?", Expr);
9694c039 680
51245e2d 681 elsif Is_Limited_View (R_Type) then
ec4867fa 682 Error_Msg_N
20261dc1 683 ("return by reference not permitted in Ada 2005 "
dbfeb4fa 684 & "(RM-2005 6.5(5.5/2))?y?", Expr);
ec4867fa
ES
685 else
686 Error_Msg_N
20261dc1 687 ("cannot copy object of a limited type in Ada 2005 "
dbfeb4fa 688 & "(RM-2005 6.5(5.5/2))?y?", Expr);
ec4867fa
ES
689 end if;
690
691 -- Ada 95 mode, compatibility warnings disabled
692
693 else
694 return; -- skip continuation messages below
695 end if;
696
9694c039
AC
697 if not Inside_A_Generic then
698 Error_Msg_N
699 ("\consider switching to return of access type", Expr);
700 Explain_Limited_Type (R_Type, Expr);
701 end if;
ec4867fa
ES
702 end if;
703 end Check_Limited_Return;
704
705 -------------------------------------
706 -- Check_Return_Subtype_Indication --
707 -------------------------------------
708
709 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
7665e4bd
AC
710 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
711
712 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
713 -- Subtype given in the extended return statement (must match R_Type)
ec4867fa
ES
714
715 Subtype_Ind : constant Node_Id :=
716 Object_Definition (Original_Node (Obj_Decl));
717
7f568bfa
AC
718 R_Type_Is_Anon_Access : constant Boolean :=
719 Ekind_In (R_Type,
720 E_Anonymous_Access_Subprogram_Type,
721 E_Anonymous_Access_Protected_Subprogram_Type,
722 E_Anonymous_Access_Type);
ec4867fa
ES
723 -- True if return type of the function is an anonymous access type
724 -- Can't we make Is_Anonymous_Access_Type in einfo ???
725
7f568bfa
AC
726 R_Stm_Type_Is_Anon_Access : constant Boolean :=
727 Ekind_In (R_Stm_Type,
728 E_Anonymous_Access_Subprogram_Type,
729 E_Anonymous_Access_Protected_Subprogram_Type,
730 E_Anonymous_Access_Type);
ec4867fa
ES
731 -- True if type of the return object is an anonymous access type
732
7f568bfa
AC
733 procedure Error_No_Match (N : Node_Id);
734 -- Output error messages for case where types do not statically
735 -- match. N is the location for the messages.
736
737 --------------------
738 -- Error_No_Match --
739 --------------------
740
741 procedure Error_No_Match (N : Node_Id) is
742 begin
743 Error_Msg_N
744 ("subtype must statically match function result subtype", N);
745
746 if not Predicates_Match (R_Stm_Type, R_Type) then
747 Error_Msg_Node_2 := R_Type;
748 Error_Msg_NE
3ccedacc 749 ("\predicate of& does not match predicate of&",
7f568bfa
AC
750 N, R_Stm_Type);
751 end if;
752 end Error_No_Match;
753
754 -- Start of processing for Check_Return_Subtype_Indication
755
ec4867fa 756 begin
7665e4bd 757 -- First, avoid cascaded errors
ec4867fa
ES
758
759 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
760 return;
761 end if;
762
763 -- "return access T" case; check that the return statement also has
764 -- "access T", and that the subtypes statically match:
53cf4600 765 -- if this is an access to subprogram the signatures must match.
ec4867fa
ES
766
767 if R_Type_Is_Anon_Access then
768 if R_Stm_Type_Is_Anon_Access then
53cf4600
ES
769 if
770 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
0a36105d 771 then
53cf4600
ES
772 if Base_Type (Designated_Type (R_Stm_Type)) /=
773 Base_Type (Designated_Type (R_Type))
774 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
775 then
7f568bfa 776 Error_No_Match (Subtype_Mark (Subtype_Ind));
53cf4600
ES
777 end if;
778
779 else
780 -- For two anonymous access to subprogram types, the
781 -- types themselves must be type conformant.
782
783 if not Conforming_Types
784 (R_Stm_Type, R_Type, Fully_Conformant)
785 then
7f568bfa 786 Error_No_Match (Subtype_Ind);
53cf4600 787 end if;
ec4867fa 788 end if;
0a36105d 789
ec4867fa
ES
790 else
791 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
792 end if;
793
6cce2156
GD
794 -- If the return object is of an anonymous access type, then report
795 -- an error if the function's result type is not also anonymous.
796
797 elsif R_Stm_Type_Is_Anon_Access
798 and then not R_Type_Is_Anon_Access
799 then
3ccedacc
AC
800 Error_Msg_N ("anonymous access not allowed for function with "
801 & "named access result", Subtype_Ind);
6cce2156 802
81d93365
AC
803 -- Subtype indication case: check that the return object's type is
804 -- covered by the result type, and that the subtypes statically match
805 -- when the result subtype is constrained. Also handle record types
806 -- with unknown discriminants for which we have built the underlying
807 -- record view. Coverage is needed to allow specific-type return
808 -- objects when the result type is class-wide (see AI05-32).
809
810 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
9013065b 811 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
212863c0
AC
812 and then
813 Covers
814 (Base_Type (R_Type),
815 Underlying_Record_View (Base_Type (R_Stm_Type))))
9013065b
AC
816 then
817 -- A null exclusion may be present on the return type, on the
818 -- function specification, on the object declaration or on the
819 -- subtype itself.
ec4867fa 820
21d27997
RD
821 if Is_Access_Type (R_Type)
822 and then
823 (Can_Never_Be_Null (R_Type)
824 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
825 Can_Never_Be_Null (R_Stm_Type)
826 then
7f568bfa 827 Error_No_Match (Subtype_Ind);
21d27997
RD
828 end if;
829
105b5e65 830 -- AI05-103: for elementary types, subtypes must statically match
8779dffa
AC
831
832 if Is_Constrained (R_Type)
833 or else Is_Access_Type (R_Type)
834 then
ec4867fa 835 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
7f568bfa 836 Error_No_Match (Subtype_Ind);
ec4867fa
ES
837 end if;
838 end if;
839
a8b346d2
RD
840 -- All remaining cases are illegal
841
842 -- Note: previous versions of this subprogram allowed the return
843 -- value to be the ancestor of the return type if the return type
844 -- was a null extension. This was plainly incorrect.
ff7139c3 845
ec4867fa
ES
846 else
847 Error_Msg_N
848 ("wrong type for return_subtype_indication", Subtype_Ind);
849 end if;
850 end Check_Return_Subtype_Indication;
851
852 ---------------------
853 -- Local Variables --
854 ---------------------
855
856 Expr : Node_Id;
857
858 -- Start of processing for Analyze_Function_Return
859
860 begin
861 Set_Return_Present (Scope_Id);
862
5d37ba92 863 if Nkind (N) = N_Simple_Return_Statement then
ec4867fa 864 Expr := Expression (N);
4ee646da 865
e917aec2
RD
866 -- Guard against a malformed expression. The parser may have tried to
867 -- recover but the node is not analyzable.
4ee646da
AC
868
869 if Nkind (Expr) = N_Error then
870 Set_Etype (Expr, Any_Type);
871 Expander_Mode_Save_And_Set (False);
872 return;
873
874 else
0180fd26
AC
875 -- The resolution of a controlled [extension] aggregate associated
876 -- with a return statement creates a temporary which needs to be
877 -- finalized on function exit. Wrap the return statement inside a
878 -- block so that the finalization machinery can detect this case.
879 -- This early expansion is done only when the return statement is
880 -- not part of a handled sequence of statements.
881
882 if Nkind_In (Expr, N_Aggregate,
883 N_Extension_Aggregate)
884 and then Needs_Finalization (R_Type)
885 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
886 then
887 Rewrite (N,
888 Make_Block_Statement (Loc,
889 Handled_Statement_Sequence =>
890 Make_Handled_Sequence_Of_Statements (Loc,
891 Statements => New_List (Relocate_Node (N)))));
892
893 Analyze (N);
894 return;
895 end if;
896
4ee646da
AC
897 Analyze_And_Resolve (Expr, R_Type);
898 Check_Limited_Return (Expr);
899 end if;
ec4867fa 900
ad05f2e9 901 -- RETURN only allowed in SPARK as the last statement in function
607d0635 902
fe5d3068 903 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
607d0635
AC
904 and then
905 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
8d606a78 906 or else Present (Next (N)))
607d0635 907 then
ce5ba43a 908 Check_SPARK_05_Restriction
fe5d3068 909 ("RETURN should be the last statement in function", N);
607d0635
AC
910 end if;
911
ec4867fa 912 else
ce5ba43a 913 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
607d0635 914
ec4867fa
ES
915 -- Analyze parts specific to extended_return_statement:
916
917 declare
de6cad7c 918 Obj_Decl : constant Node_Id :=
b9daa96e 919 Last (Return_Object_Declarations (N));
de6cad7c 920 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
b9daa96e 921 HSS : constant Node_Id := Handled_Statement_Sequence (N);
ec4867fa
ES
922
923 begin
924 Expr := Expression (Obj_Decl);
925
926 -- Note: The check for OK_For_Limited_Init will happen in
927 -- Analyze_Object_Declaration; we treat it as a normal
928 -- object declaration.
929
cd1c668b 930 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
ec4867fa
ES
931 Analyze (Obj_Decl);
932
ec4867fa
ES
933 Check_Return_Subtype_Indication (Obj_Decl);
934
935 if Present (HSS) then
936 Analyze (HSS);
937
938 if Present (Exception_Handlers (HSS)) then
939
940 -- ???Has_Nested_Block_With_Handler needs to be set.
941 -- Probably by creating an actual N_Block_Statement.
942 -- Probably in Expand.
943
944 null;
945 end if;
946 end if;
947
9337aa0a
AC
948 -- Mark the return object as referenced, since the return is an
949 -- implicit reference of the object.
950
951 Set_Referenced (Defining_Identifier (Obj_Decl));
952
ec4867fa 953 Check_References (Stm_Entity);
de6cad7c
AC
954
955 -- Check RM 6.5 (5.9/3)
956
957 if Has_Aliased then
958 if Ada_Version < Ada_2012 then
dbfeb4fa
RD
959
960 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
961 -- Can it really happen (extended return???)
962
963 Error_Msg_N
b785e0b8
AC
964 ("aliased only allowed for limited return objects "
965 & "in Ada 2012??", N);
de6cad7c 966
51245e2d 967 elsif not Is_Limited_View (R_Type) then
3ccedacc
AC
968 Error_Msg_N
969 ("aliased only allowed for limited return objects", N);
de6cad7c
AC
970 end if;
971 end if;
ec4867fa
ES
972 end;
973 end if;
974
21d27997 975 -- Case of Expr present
5d37ba92 976
ec4867fa 977 if Present (Expr)
21d27997 978
8fde064e 979 -- Defend against previous errors
21d27997
RD
980
981 and then Nkind (Expr) /= N_Empty
5d37ba92 982 and then Present (Etype (Expr))
ec4867fa 983 then
5d37ba92
ES
984 -- Apply constraint check. Note that this is done before the implicit
985 -- conversion of the expression done for anonymous access types to
f3d57416 986 -- ensure correct generation of the null-excluding check associated
5d37ba92
ES
987 -- with null-excluding expressions found in return statements.
988
989 Apply_Constraint_Check (Expr, R_Type);
990
991 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
992 -- type, apply an implicit conversion of the expression to that type
993 -- to force appropriate static and run-time accessibility checks.
ec4867fa 994
0791fbe9 995 if Ada_Version >= Ada_2005
ec4867fa
ES
996 and then Ekind (R_Type) = E_Anonymous_Access_Type
997 then
998 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
999 Analyze_And_Resolve (Expr, R_Type);
b6b5cca8
AC
1000
1001 -- If this is a local anonymous access to subprogram, the
1002 -- accessibility check can be applied statically. The return is
1003 -- illegal if the access type of the return expression is declared
1004 -- inside of the subprogram (except if it is the subtype indication
1005 -- of an extended return statement).
1006
1007 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1008 if not Comes_From_Source (Current_Scope)
1009 or else Ekind (Current_Scope) = E_Return_Statement
1010 then
1011 null;
1012
1013 elsif
1014 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1015 then
1016 Error_Msg_N ("cannot return local access to subprogram", N);
1017 end if;
1ebc2612
AC
1018
1019 -- The expression cannot be of a formal incomplete type
1020
1021 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1022 and then Is_Generic_Type (Etype (Expr))
1023 then
1024 Error_Msg_N
1025 ("cannot return expression of a formal incomplete type", N);
ec4867fa
ES
1026 end if;
1027
21d27997
RD
1028 -- If the result type is class-wide, then check that the return
1029 -- expression's type is not declared at a deeper level than the
1030 -- function (RM05-6.5(5.6/2)).
1031
0791fbe9 1032 if Ada_Version >= Ada_2005
21d27997
RD
1033 and then Is_Class_Wide_Type (R_Type)
1034 then
1035 if Type_Access_Level (Etype (Expr)) >
1036 Subprogram_Access_Level (Scope_Id)
1037 then
1038 Error_Msg_N
3ccedacc
AC
1039 ("level of return expression type is deeper than "
1040 & "class-wide function!", Expr);
21d27997
RD
1041 end if;
1042 end if;
1043
4755cce9
JM
1044 -- Check incorrect use of dynamically tagged expression
1045
1046 if Is_Tagged_Type (R_Type) then
1047 Check_Dynamically_Tagged_Expression
1048 (Expr => Expr,
1049 Typ => R_Type,
1050 Related_Nod => N);
ec4867fa
ES
1051 end if;
1052
ec4867fa
ES
1053 -- ??? A real run-time accessibility check is needed in cases
1054 -- involving dereferences of access parameters. For now we just
1055 -- check the static cases.
1056
0791fbe9 1057 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
51245e2d 1058 and then Is_Limited_View (Etype (Scope_Id))
ec4867fa
ES
1059 and then Object_Access_Level (Expr) >
1060 Subprogram_Access_Level (Scope_Id)
1061 then
9694c039
AC
1062 -- Suppress the message in a generic, where the rewriting
1063 -- is irrelevant.
1064
1065 if Inside_A_Generic then
1066 null;
1067
1068 else
1069 Rewrite (N,
1070 Make_Raise_Program_Error (Loc,
1071 Reason => PE_Accessibility_Check_Failed));
1072 Analyze (N);
1073
43417b90 1074 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
1075 Error_Msg_N ("cannot return a local value by reference<<", N);
1076 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
9694c039 1077 end if;
ec4867fa 1078 end if;
5d37ba92
ES
1079
1080 if Known_Null (Expr)
1081 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1082 and then Null_Exclusion_Present (Parent (Scope_Id))
1083 then
1084 Apply_Compile_Time_Constraint_Error
1085 (N => Expr,
1086 Msg => "(Ada 2005) null not allowed for "
dbfeb4fa 1087 & "null-excluding return??",
5d37ba92
ES
1088 Reason => CE_Null_Not_Allowed);
1089 end if;
ec4867fa
ES
1090 end if;
1091 end Analyze_Function_Return;
1092
996ae0b0
RK
1093 -------------------------------------
1094 -- Analyze_Generic_Subprogram_Body --
1095 -------------------------------------
1096
1097 procedure Analyze_Generic_Subprogram_Body
1098 (N : Node_Id;
1099 Gen_Id : Entity_Id)
1100 is
fbf5a39b 1101 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
996ae0b0 1102 Kind : constant Entity_Kind := Ekind (Gen_Id);
fbf5a39b 1103 Body_Id : Entity_Id;
996ae0b0 1104 New_N : Node_Id;
fbf5a39b 1105 Spec : Node_Id;
996ae0b0
RK
1106
1107 begin
82c80734
RD
1108 -- Copy body and disable expansion while analyzing the generic For a
1109 -- stub, do not copy the stub (which would load the proper body), this
1110 -- will be done when the proper body is analyzed.
996ae0b0
RK
1111
1112 if Nkind (N) /= N_Subprogram_Body_Stub then
1113 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1114 Rewrite (N, New_N);
1115 Start_Generic;
1116 end if;
1117
1118 Spec := Specification (N);
1119
1120 -- Within the body of the generic, the subprogram is callable, and
1121 -- behaves like the corresponding non-generic unit.
1122
fbf5a39b 1123 Body_Id := Defining_Entity (Spec);
996ae0b0
RK
1124
1125 if Kind = E_Generic_Procedure
1126 and then Nkind (Spec) /= N_Procedure_Specification
1127 then
fbf5a39b 1128 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
996ae0b0
RK
1129 return;
1130
1131 elsif Kind = E_Generic_Function
1132 and then Nkind (Spec) /= N_Function_Specification
1133 then
fbf5a39b 1134 Error_Msg_N ("invalid body for generic function ", Body_Id);
996ae0b0
RK
1135 return;
1136 end if;
1137
fbf5a39b 1138 Set_Corresponding_Body (Gen_Decl, Body_Id);
996ae0b0
RK
1139
1140 if Has_Completion (Gen_Id)
1141 and then Nkind (Parent (N)) /= N_Subunit
1142 then
1143 Error_Msg_N ("duplicate generic body", N);
1144 return;
1145 else
1146 Set_Has_Completion (Gen_Id);
1147 end if;
1148
1149 if Nkind (N) = N_Subprogram_Body_Stub then
1150 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1151 else
1152 Set_Corresponding_Spec (N, Gen_Id);
1153 end if;
1154
1155 if Nkind (Parent (N)) = N_Compilation_Unit then
1156 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1157 end if;
1158
1159 -- Make generic parameters immediately visible in the body. They are
1160 -- needed to process the formals declarations. Then make the formals
1161 -- visible in a separate step.
1162
0a36105d 1163 Push_Scope (Gen_Id);
996ae0b0
RK
1164
1165 declare
1166 E : Entity_Id;
1167 First_Ent : Entity_Id;
1168
1169 begin
1170 First_Ent := First_Entity (Gen_Id);
1171
1172 E := First_Ent;
1173 while Present (E) and then not Is_Formal (E) loop
1174 Install_Entity (E);
1175 Next_Entity (E);
1176 end loop;
1177
1178 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1179
1180 -- Now generic formals are visible, and the specification can be
1181 -- analyzed, for subsequent conformance check.
1182
fbf5a39b 1183 Body_Id := Analyze_Subprogram_Specification (Spec);
996ae0b0 1184
fbf5a39b 1185 -- Make formal parameters visible
996ae0b0
RK
1186
1187 if Present (E) then
1188
fbf5a39b
AC
1189 -- E is the first formal parameter, we loop through the formals
1190 -- installing them so that they will be visible.
996ae0b0
RK
1191
1192 Set_First_Entity (Gen_Id, E);
996ae0b0
RK
1193 while Present (E) loop
1194 Install_Entity (E);
1195 Next_Formal (E);
1196 end loop;
1197 end if;
1198
e895b435 1199 -- Visible generic entity is callable within its own body
996ae0b0 1200
ec4867fa 1201 Set_Ekind (Gen_Id, Ekind (Body_Id));
ea3c0651 1202 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
ec4867fa
ES
1203 Set_Ekind (Body_Id, E_Subprogram_Body);
1204 Set_Convention (Body_Id, Convention (Gen_Id));
1205 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1206 Set_Scope (Body_Id, Scope (Gen_Id));
fbf5a39b
AC
1207 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1208
1209 if Nkind (N) = N_Subprogram_Body_Stub then
1210
e895b435 1211 -- No body to analyze, so restore state of generic unit
fbf5a39b
AC
1212
1213 Set_Ekind (Gen_Id, Kind);
1214 Set_Ekind (Body_Id, Kind);
1215
1216 if Present (First_Ent) then
1217 Set_First_Entity (Gen_Id, First_Ent);
1218 end if;
1219
1220 End_Scope;
1221 return;
1222 end if;
996ae0b0 1223
82c80734
RD
1224 -- If this is a compilation unit, it must be made visible explicitly,
1225 -- because the compilation of the declaration, unlike other library
1226 -- unit declarations, does not. If it is not a unit, the following
1227 -- is redundant but harmless.
996ae0b0
RK
1228
1229 Set_Is_Immediately_Visible (Gen_Id);
fbf5a39b 1230 Reference_Body_Formals (Gen_Id, Body_Id);
996ae0b0 1231
ec4867fa
ES
1232 if Is_Child_Unit (Gen_Id) then
1233 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1234 end if;
1235
996ae0b0 1236 Set_Actual_Subtypes (N, Current_Scope);
483361a6 1237
ea3c0651
AC
1238 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
1239 -- invariants and predicates associated with the body and its spec.
1240 -- Note that this is not pure expansion as Expand_Subprogram_Contract
1241 -- prepares the contract assertions for generic subprograms or for
1242 -- ASIS. Do not generate contract checks in SPARK mode.
483361a6 1243
f5da7a97 1244 if not GNATprove_Mode then
ea3c0651 1245 Expand_Subprogram_Contract (N, Gen_Id, Body_Id);
483361a6 1246 end if;
0dabde3a
ES
1247
1248 -- If the generic unit carries pre- or post-conditions, copy them
1249 -- to the original generic tree, so that they are properly added
1250 -- to any instantiation.
1251
1252 declare
1253 Orig : constant Node_Id := Original_Node (N);
1254 Cond : Node_Id;
1255
1256 begin
1257 Cond := First (Declarations (N));
1258 while Present (Cond) loop
1259 if Nkind (Cond) = N_Pragma
1260 and then Pragma_Name (Cond) = Name_Check
1261 then
1262 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1263
1264 elsif Nkind (Cond) = N_Pragma
1265 and then Pragma_Name (Cond) = Name_Postcondition
1266 then
1267 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
1268 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1269 else
1270 exit;
1271 end if;
1272
1273 Next (Cond);
1274 end loop;
1275 end;
1276
579847c2
AC
1277 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1278 Set_SPARK_Pragma_Inherited (Body_Id, True);
1279
996ae0b0
RK
1280 Analyze_Declarations (Declarations (N));
1281 Check_Completion;
1282 Analyze (Handled_Statement_Sequence (N));
1283
1284 Save_Global_References (Original_Node (N));
1285
82c80734
RD
1286 -- Prior to exiting the scope, include generic formals again (if any
1287 -- are present) in the set of local entities.
996ae0b0
RK
1288
1289 if Present (First_Ent) then
1290 Set_First_Entity (Gen_Id, First_Ent);
1291 end if;
1292
fbf5a39b 1293 Check_References (Gen_Id);
996ae0b0
RK
1294 end;
1295
e6f69614 1296 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
996ae0b0
RK
1297 End_Scope;
1298 Check_Subprogram_Order (N);
1299
e895b435 1300 -- Outside of its body, unit is generic again
996ae0b0
RK
1301
1302 Set_Ekind (Gen_Id, Kind);
fbf5a39b 1303 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
5d37ba92
ES
1304
1305 if Style_Check then
1306 Style.Check_Identifier (Body_Id, Gen_Id);
1307 end if;
13d923cc 1308
996ae0b0 1309 End_Generic;
996ae0b0
RK
1310 end Analyze_Generic_Subprogram_Body;
1311
4d8f3296
ES
1312 ----------------------------
1313 -- Analyze_Null_Procedure --
1314 ----------------------------
1315
1316 procedure Analyze_Null_Procedure
1317 (N : Node_Id;
1318 Is_Completion : out Boolean)
1319 is
1320 Loc : constant Source_Ptr := Sloc (N);
1321 Spec : constant Node_Id := Specification (N);
1322 Designator : Entity_Id;
1323 Form : Node_Id;
1324 Null_Body : Node_Id := Empty;
1325 Prev : Entity_Id;
1326
1327 begin
1328 -- Capture the profile of the null procedure before analysis, for
1329 -- expansion at the freeze point and at each point of call. The body is
1330 -- used if the procedure has preconditions, or if it is a completion. In
1331 -- the first case the body is analyzed at the freeze point, in the other
1332 -- it replaces the null procedure declaration.
1333
1334 Null_Body :=
1335 Make_Subprogram_Body (Loc,
1336 Specification => New_Copy_Tree (Spec),
8c35b40a 1337 Declarations => New_List,
4d8f3296
ES
1338 Handled_Statement_Sequence =>
1339 Make_Handled_Sequence_Of_Statements (Loc,
1340 Statements => New_List (Make_Null_Statement (Loc))));
1341
1342 -- Create new entities for body and formals
1343
1344 Set_Defining_Unit_Name (Specification (Null_Body),
9d2a2071
AC
1345 Make_Defining_Identifier
1346 (Sloc (Defining_Entity (N)),
1347 Chars (Defining_Entity (N))));
4d8f3296
ES
1348
1349 Form := First (Parameter_Specifications (Specification (Null_Body)));
1350 while Present (Form) loop
1351 Set_Defining_Identifier (Form,
9d2a2071
AC
1352 Make_Defining_Identifier
1353 (Sloc (Defining_Identifier (Form)),
1354 Chars (Defining_Identifier (Form))));
4d8f3296
ES
1355 Next (Form);
1356 end loop;
1357
1358 -- Determine whether the null procedure may be a completion of a generic
1359 -- suprogram, in which case we use the new null body as the completion
1360 -- and set minimal semantic information on the original declaration,
1361 -- which is rewritten as a null statement.
1362
1363 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1364
1365 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1366 Insert_Before (N, Null_Body);
1367 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1368 Set_Contract (Defining_Entity (N), Make_Contract (Loc));
1369
1370 Rewrite (N, Make_Null_Statement (Loc));
1371 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1372 Is_Completion := True;
1373 return;
1374
1375 else
4d8f3296
ES
1376 -- Resolve the types of the formals now, because the freeze point
1377 -- may appear in a different context, e.g. an instantiation.
1378
1379 Form := First (Parameter_Specifications (Specification (Null_Body)));
1380 while Present (Form) loop
1381 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1382 Find_Type (Parameter_Type (Form));
1383
1384 elsif
1385 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1386 then
1387 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1388
1389 else
1390 -- The case of a null procedure with a formal that is an
1391 -- access_to_subprogram type, and that is used as an actual
1392 -- in an instantiation is left to the enthusiastic reader.
1393
1394 null;
1395 end if;
1396
1397 Next (Form);
1398 end loop;
1399 end if;
1400
1401 -- If there are previous overloadable entities with the same name,
1402 -- check whether any of them is completed by the null procedure.
1403
1404 if Present (Prev) and then Is_Overloadable (Prev) then
1405 Designator := Analyze_Subprogram_Specification (Spec);
1406 Prev := Find_Corresponding_Spec (N);
1407 end if;
1408
1409 if No (Prev) or else not Comes_From_Source (Prev) then
1410 Designator := Analyze_Subprogram_Specification (Spec);
1411 Set_Has_Completion (Designator);
1412
1413 -- Signal to caller that this is a procedure declaration
1414
1415 Is_Completion := False;
1416
1417 -- Null procedures are always inlined, but generic formal subprograms
1418 -- which appear as such in the internal instance of formal packages,
1419 -- need no completion and are not marked Inline.
1420
1421 if Expander_Active
1422 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1423 then
1424 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1425 Set_Body_To_Inline (N, Null_Body);
1426 Set_Is_Inlined (Designator);
1427 end if;
1428
1429 else
2fe258bf
AC
1430 -- The null procedure is a completion. We unconditionally rewrite
1431 -- this as a null body (even if expansion is not active), because
1432 -- there are various error checks that are applied on this body
1433 -- when it is analyzed (e.g. correct aspect placement).
4d8f3296
ES
1434
1435 Is_Completion := True;
2fe258bf
AC
1436 Rewrite (N, Null_Body);
1437 Analyze (N);
4d8f3296
ES
1438 end if;
1439 end Analyze_Null_Procedure;
1440
996ae0b0
RK
1441 -----------------------------
1442 -- Analyze_Operator_Symbol --
1443 -----------------------------
1444
82c80734
RD
1445 -- An operator symbol such as "+" or "and" may appear in context where the
1446 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1447 -- is just a string, as in (conjunction = "or"). In these cases the parser
1448 -- generates this node, and the semantics does the disambiguation. Other
1449 -- such case are actuals in an instantiation, the generic unit in an
1450 -- instantiation, and pragma arguments.
996ae0b0
RK
1451
1452 procedure Analyze_Operator_Symbol (N : Node_Id) is
1453 Par : constant Node_Id := Parent (N);
1454
1455 begin
1f0b1e48 1456 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
996ae0b0 1457 or else Nkind (Par) = N_Function_Instantiation
1f0b1e48 1458 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
996ae0b0 1459 or else (Nkind (Par) = N_Pragma_Argument_Association
1f0b1e48 1460 and then not Is_Pragma_String_Literal (Par))
996ae0b0 1461 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
800621e0
RD
1462 or else (Nkind (Par) = N_Attribute_Reference
1463 and then Attribute_Name (Par) /= Name_Value)
996ae0b0
RK
1464 then
1465 Find_Direct_Name (N);
1466
1467 else
1468 Change_Operator_Symbol_To_String_Literal (N);
1469 Analyze (N);
1470 end if;
1471 end Analyze_Operator_Symbol;
1472
1473 -----------------------------------
1474 -- Analyze_Parameter_Association --
1475 -----------------------------------
1476
1477 procedure Analyze_Parameter_Association (N : Node_Id) is
1478 begin
1479 Analyze (Explicit_Actual_Parameter (N));
1480 end Analyze_Parameter_Association;
1481
1482 ----------------------------
1483 -- Analyze_Procedure_Call --
1484 ----------------------------
1485
1486 procedure Analyze_Procedure_Call (N : Node_Id) is
1487 Loc : constant Source_Ptr := Sloc (N);
1488 P : constant Node_Id := Name (N);
1489 Actuals : constant List_Id := Parameter_Associations (N);
1490 Actual : Node_Id;
1491 New_N : Node_Id;
1492
1493 procedure Analyze_Call_And_Resolve;
1494 -- Do Analyze and Resolve calls for procedure call
cd5a9750 1495 -- At end, check illegal order dependence.
996ae0b0 1496
fbf5a39b
AC
1497 ------------------------------
1498 -- Analyze_Call_And_Resolve --
1499 ------------------------------
1500
996ae0b0
RK
1501 procedure Analyze_Call_And_Resolve is
1502 begin
1503 if Nkind (N) = N_Procedure_Call_Statement then
1504 Analyze_Call (N);
1505 Resolve (N, Standard_Void_Type);
1506 else
1507 Analyze (N);
1508 end if;
1509 end Analyze_Call_And_Resolve;
1510
1511 -- Start of processing for Analyze_Procedure_Call
1512
1513 begin
1514 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1515 -- a procedure call or an entry call. The prefix may denote an access
1516 -- to subprogram type, in which case an implicit dereference applies.
f3d57416 1517 -- If the prefix is an indexed component (without implicit dereference)
996ae0b0
RK
1518 -- then the construct denotes a call to a member of an entire family.
1519 -- If the prefix is a simple name, it may still denote a call to a
1520 -- parameterless member of an entry family. Resolution of these various
1521 -- interpretations is delicate.
1522
1523 Analyze (P);
1524
758c442c
GD
1525 -- If this is a call of the form Obj.Op, the call may have been
1526 -- analyzed and possibly rewritten into a block, in which case
1527 -- we are done.
1528
1529 if Analyzed (N) then
1530 return;
1531 end if;
1532
7415029d
AC
1533 -- If there is an error analyzing the name (which may have been
1534 -- rewritten if the original call was in prefix notation) then error
1535 -- has been emitted already, mark node and return.
996ae0b0 1536
21791d97 1537 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
996ae0b0
RK
1538 Set_Etype (N, Any_Type);
1539 return;
1540 end if;
1541
1542 -- Otherwise analyze the parameters
1543
1544 if Present (Actuals) then
1545 Actual := First (Actuals);
1546
1547 while Present (Actual) loop
1548 Analyze (Actual);
1549 Check_Parameterless_Call (Actual);
1550 Next (Actual);
1551 end loop;
1552 end if;
1553
0bfc9a64 1554 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
996ae0b0
RK
1555
1556 if Nkind (P) = N_Attribute_Reference
b69cd36a
AC
1557 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1558 Name_Elab_Body,
1559 Name_Elab_Subp_Body)
996ae0b0
RK
1560 then
1561 if Present (Actuals) then
1562 Error_Msg_N
1563 ("no parameters allowed for this call", First (Actuals));
1564 return;
1565 end if;
1566
1567 Set_Etype (N, Standard_Void_Type);
1568 Set_Analyzed (N);
1569
1570 elsif Is_Entity_Name (P)
1571 and then Is_Record_Type (Etype (Entity (P)))
1572 and then Remote_AST_I_Dereference (P)
1573 then
1574 return;
1575
1576 elsif Is_Entity_Name (P)
1577 and then Ekind (Entity (P)) /= E_Entry_Family
1578 then
1579 if Is_Access_Type (Etype (P))
1580 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1581 and then No (Actuals)
1582 and then Comes_From_Source (N)
1583 then
ed2233dc 1584 Error_Msg_N ("missing explicit dereference in call", N);
996ae0b0
RK
1585 end if;
1586
1587 Analyze_Call_And_Resolve;
1588
1589 -- If the prefix is the simple name of an entry family, this is
1590 -- a parameterless call from within the task body itself.
1591
1592 elsif Is_Entity_Name (P)
1593 and then Nkind (P) = N_Identifier
1594 and then Ekind (Entity (P)) = E_Entry_Family
1595 and then Present (Actuals)
1596 and then No (Next (First (Actuals)))
1597 then
82c80734
RD
1598 -- Can be call to parameterless entry family. What appears to be the
1599 -- sole argument is in fact the entry index. Rewrite prefix of node
1600 -- accordingly. Source representation is unchanged by this
996ae0b0
RK
1601 -- transformation.
1602
1603 New_N :=
1604 Make_Indexed_Component (Loc,
1605 Prefix =>
1606 Make_Selected_Component (Loc,
1607 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1608 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1609 Expressions => Actuals);
1610 Set_Name (N, New_N);
1611 Set_Etype (New_N, Standard_Void_Type);
1612 Set_Parameter_Associations (N, No_List);
1613 Analyze_Call_And_Resolve;
1614
1615 elsif Nkind (P) = N_Explicit_Dereference then
1616 if Ekind (Etype (P)) = E_Subprogram_Type then
1617 Analyze_Call_And_Resolve;
1618 else
1619 Error_Msg_N ("expect access to procedure in call", P);
1620 end if;
1621
82c80734
RD
1622 -- The name can be a selected component or an indexed component that
1623 -- yields an access to subprogram. Such a prefix is legal if the call
1624 -- has parameter associations.
996ae0b0
RK
1625
1626 elsif Is_Access_Type (Etype (P))
1627 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1628 then
1629 if Present (Actuals) then
1630 Analyze_Call_And_Resolve;
1631 else
ed2233dc 1632 Error_Msg_N ("missing explicit dereference in call ", N);
996ae0b0
RK
1633 end if;
1634
82c80734
RD
1635 -- If not an access to subprogram, then the prefix must resolve to the
1636 -- name of an entry, entry family, or protected operation.
996ae0b0 1637
82c80734
RD
1638 -- For the case of a simple entry call, P is a selected component where
1639 -- the prefix is the task and the selector name is the entry. A call to
1640 -- a protected procedure will have the same syntax. If the protected
1641 -- object contains overloaded operations, the entity may appear as a
1642 -- function, the context will select the operation whose type is Void.
996ae0b0
RK
1643
1644 elsif Nkind (P) = N_Selected_Component
8fde064e
AC
1645 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1646 E_Procedure,
1647 E_Function)
996ae0b0
RK
1648 then
1649 Analyze_Call_And_Resolve;
1650
1651 elsif Nkind (P) = N_Selected_Component
1652 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1653 and then Present (Actuals)
1654 and then No (Next (First (Actuals)))
1655 then
82c80734
RD
1656 -- Can be call to parameterless entry family. What appears to be the
1657 -- sole argument is in fact the entry index. Rewrite prefix of node
1658 -- accordingly. Source representation is unchanged by this
996ae0b0
RK
1659 -- transformation.
1660
1661 New_N :=
1662 Make_Indexed_Component (Loc,
1663 Prefix => New_Copy (P),
1664 Expressions => Actuals);
1665 Set_Name (N, New_N);
1666 Set_Etype (New_N, Standard_Void_Type);
1667 Set_Parameter_Associations (N, No_List);
1668 Analyze_Call_And_Resolve;
1669
1670 -- For the case of a reference to an element of an entry family, P is
1671 -- an indexed component whose prefix is a selected component (task and
1672 -- entry family), and whose index is the entry family index.
1673
1674 elsif Nkind (P) = N_Indexed_Component
1675 and then Nkind (Prefix (P)) = N_Selected_Component
1676 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1677 then
1678 Analyze_Call_And_Resolve;
1679
1680 -- If the prefix is the name of an entry family, it is a call from
1681 -- within the task body itself.
1682
1683 elsif Nkind (P) = N_Indexed_Component
1684 and then Nkind (Prefix (P)) = N_Identifier
1685 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1686 then
1687 New_N :=
1688 Make_Selected_Component (Loc,
1689 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1690 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1691 Rewrite (Prefix (P), New_N);
1692 Analyze (P);
1693 Analyze_Call_And_Resolve;
1694
9f8d1e5c
AC
1695 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1696 -- procedure name, so the construct can only be a qualified expression.
1697
1698 elsif Nkind (P) = N_Qualified_Expression
1699 and then Ada_Version >= Ada_2012
1700 then
1701 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1702 Analyze (N);
1703
e895b435 1704 -- Anything else is an error
996ae0b0
RK
1705
1706 else
758c442c 1707 Error_Msg_N ("invalid procedure or entry call", N);
996ae0b0
RK
1708 end if;
1709 end Analyze_Procedure_Call;
1710
b0186f71
AC
1711 ------------------------------
1712 -- Analyze_Return_Statement --
1713 ------------------------------
1714
1715 procedure Analyze_Return_Statement (N : Node_Id) is
1716
1717 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1718 N_Extended_Return_Statement));
1719
1720 Returns_Object : constant Boolean :=
1721 Nkind (N) = N_Extended_Return_Statement
1722 or else
8fde064e
AC
1723 (Nkind (N) = N_Simple_Return_Statement
1724 and then Present (Expression (N)));
b0186f71
AC
1725 -- True if we're returning something; that is, "return <expression>;"
1726 -- or "return Result : T [:= ...]". False for "return;". Used for error
1727 -- checking: If Returns_Object is True, N should apply to a function
1728 -- body; otherwise N should apply to a procedure body, entry body,
1729 -- accept statement, or extended return statement.
1730
1731 function Find_What_It_Applies_To return Entity_Id;
1732 -- Find the entity representing the innermost enclosing body, accept
1733 -- statement, or extended return statement. If the result is a callable
1734 -- construct or extended return statement, then this will be the value
1735 -- of the Return_Applies_To attribute. Otherwise, the program is
1736 -- illegal. See RM-6.5(4/2).
1737
1738 -----------------------------
1739 -- Find_What_It_Applies_To --
1740 -----------------------------
1741
1742 function Find_What_It_Applies_To return Entity_Id is
1743 Result : Entity_Id := Empty;
1744
1745 begin
36b8f95f
AC
1746 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1747 -- and postconditions.
b0186f71
AC
1748
1749 for J in reverse 0 .. Scope_Stack.Last loop
1750 Result := Scope_Stack.Table (J).Entity;
11bc76df
AC
1751 exit when not Ekind_In (Result, E_Block, E_Loop)
1752 and then Chars (Result) /= Name_uPostconditions;
b0186f71
AC
1753 end loop;
1754
1755 pragma Assert (Present (Result));
1756 return Result;
1757 end Find_What_It_Applies_To;
1758
1759 -- Local declarations
1760
1761 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1762 Kind : constant Entity_Kind := Ekind (Scope_Id);
1763 Loc : constant Source_Ptr := Sloc (N);
1764 Stm_Entity : constant Entity_Id :=
1765 New_Internal_Entity
1766 (E_Return_Statement, Current_Scope, Loc, 'R');
1767
1768 -- Start of processing for Analyze_Return_Statement
1769
1770 begin
1771 Set_Return_Statement_Entity (N, Stm_Entity);
1772
1773 Set_Etype (Stm_Entity, Standard_Void_Type);
1774 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1775
1776 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1777 -- (4/2): an inner return statement will apply to this extended return.
1778
1779 if Nkind (N) = N_Extended_Return_Statement then
1780 Push_Scope (Stm_Entity);
1781 end if;
1782
1783 -- Check that pragma No_Return is obeyed. Don't complain about the
1784 -- implicitly-generated return that is placed at the end.
1785
1786 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1787 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1788 end if;
1789
1790 -- Warn on any unassigned OUT parameters if in procedure
1791
1792 if Ekind (Scope_Id) = E_Procedure then
1793 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1794 end if;
1795
1796 -- Check that functions return objects, and other things do not
1797
1798 if Kind = E_Function or else Kind = E_Generic_Function then
1799 if not Returns_Object then
1800 Error_Msg_N ("missing expression in return from function", N);
1801 end if;
1802
1803 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1804 if Returns_Object then
1805 Error_Msg_N ("procedure cannot return value (use function)", N);
1806 end if;
1807
1808 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1809 if Returns_Object then
1810 if Is_Protected_Type (Scope (Scope_Id)) then
1811 Error_Msg_N ("entry body cannot return value", N);
1812 else
1813 Error_Msg_N ("accept statement cannot return value", N);
1814 end if;
1815 end if;
1816
1817 elsif Kind = E_Return_Statement then
1818
1819 -- We are nested within another return statement, which must be an
1820 -- extended_return_statement.
1821
1822 if Returns_Object then
d0dcb2b1
AC
1823 if Nkind (N) = N_Extended_Return_Statement then
1824 Error_Msg_N
cc96a1b8 1825 ("extended return statement cannot be nested (use `RETURN;`)",
d0dcb2b1
AC
1826 N);
1827
1828 -- Case of a simple return statement with a value inside extended
1829 -- return statement.
1830
1831 else
1832 Error_Msg_N
3ccedacc
AC
1833 ("return nested in extended return statement cannot return "
1834 & "value (use `RETURN;`)", N);
d0dcb2b1 1835 end if;
b0186f71
AC
1836 end if;
1837
1838 else
1839 Error_Msg_N ("illegal context for return statement", N);
1840 end if;
1841
1842 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1843 Analyze_Function_Return (N);
1844
1845 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1846 Set_Return_Present (Scope_Id);
1847 end if;
1848
1849 if Nkind (N) = N_Extended_Return_Statement then
1850 End_Scope;
1851 end if;
1852
1853 Kill_Current_Values (Last_Assignment_Only => True);
1854 Check_Unreachable_Code (N);
dec6faf1
AC
1855
1856 Analyze_Dimension (N);
b0186f71
AC
1857 end Analyze_Return_Statement;
1858
5d37ba92
ES
1859 -------------------------------------
1860 -- Analyze_Simple_Return_Statement --
1861 -------------------------------------
ec4867fa 1862
5d37ba92 1863 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
996ae0b0 1864 begin
5d37ba92
ES
1865 if Present (Expression (N)) then
1866 Mark_Coextensions (N, Expression (N));
996ae0b0
RK
1867 end if;
1868
5d37ba92
ES
1869 Analyze_Return_Statement (N);
1870 end Analyze_Simple_Return_Statement;
996ae0b0 1871
82c80734
RD
1872 -------------------------
1873 -- Analyze_Return_Type --
1874 -------------------------
1875
1876 procedure Analyze_Return_Type (N : Node_Id) is
1877 Designator : constant Entity_Id := Defining_Entity (N);
1878 Typ : Entity_Id := Empty;
1879
1880 begin
ec4867fa
ES
1881 -- Normal case where result definition does not indicate an error
1882
41251c60
JM
1883 if Result_Definition (N) /= Error then
1884 if Nkind (Result_Definition (N)) = N_Access_Definition then
ce5ba43a 1885 Check_SPARK_05_Restriction
fe5d3068 1886 ("access result is not allowed", Result_Definition (N));
daec8eeb 1887
b1c11e0e
JM
1888 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1889
1890 declare
1891 AD : constant Node_Id :=
1892 Access_To_Subprogram_Definition (Result_Definition (N));
1893 begin
1894 if Present (AD) and then Protected_Present (AD) then
1895 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1896 else
1897 Typ := Access_Definition (N, Result_Definition (N));
1898 end if;
1899 end;
1900
41251c60
JM
1901 Set_Parent (Typ, Result_Definition (N));
1902 Set_Is_Local_Anonymous_Access (Typ);
1903 Set_Etype (Designator, Typ);
1904
b66c3ff4
AC
1905 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1906
1907 Null_Exclusion_Static_Checks (N);
1908
41251c60
JM
1909 -- Subtype_Mark case
1910
1911 else
1912 Find_Type (Result_Definition (N));
1913 Typ := Entity (Result_Definition (N));
1914 Set_Etype (Designator, Typ);
1915
2ba431e5 1916 -- Unconstrained array as result is not allowed in SPARK
daec8eeb 1917
8fde064e 1918 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
ce5ba43a 1919 Check_SPARK_05_Restriction
fe5d3068 1920 ("returning an unconstrained array is not allowed",
7394c8cc 1921 Result_Definition (N));
daec8eeb
YM
1922 end if;
1923
b66c3ff4
AC
1924 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1925
1926 Null_Exclusion_Static_Checks (N);
1927
1928 -- If a null exclusion is imposed on the result type, then create
1929 -- a null-excluding itype (an access subtype) and use it as the
1930 -- function's Etype. Note that the null exclusion checks are done
1931 -- right before this, because they don't get applied to types that
1932 -- do not come from source.
1933
8fde064e 1934 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
b66c3ff4
AC
1935 Set_Etype (Designator,
1936 Create_Null_Excluding_Itype
ff7139c3
AC
1937 (T => Typ,
1938 Related_Nod => N,
1939 Scope_Id => Scope (Current_Scope)));
1940
1941 -- The new subtype must be elaborated before use because
1942 -- it is visible outside of the function. However its base
1943 -- type may not be frozen yet, so the reference that will
1944 -- force elaboration must be attached to the freezing of
1945 -- the base type.
1946
212863c0
AC
1947 -- If the return specification appears on a proper body,
1948 -- the subtype will have been created already on the spec.
1949
ff7139c3 1950 if Is_Frozen (Typ) then
212863c0
AC
1951 if Nkind (Parent (N)) = N_Subprogram_Body
1952 and then Nkind (Parent (Parent (N))) = N_Subunit
1953 then
1954 null;
1955 else
1956 Build_Itype_Reference (Etype (Designator), Parent (N));
1957 end if;
1958
ff7139c3
AC
1959 else
1960 Ensure_Freeze_Node (Typ);
1961
1962 declare
212863c0 1963 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
ff7139c3
AC
1964 begin
1965 Set_Itype (IR, Etype (Designator));
1966 Append_Freeze_Actions (Typ, New_List (IR));
1967 end;
1968 end if;
1969
b66c3ff4
AC
1970 else
1971 Set_Etype (Designator, Typ);
1972 end if;
1973
41251c60 1974 if Ekind (Typ) = E_Incomplete_Type
0a36105d
JM
1975 and then Is_Value_Type (Typ)
1976 then
1977 null;
1978
1979 elsif Ekind (Typ) = E_Incomplete_Type
41251c60 1980 or else (Is_Class_Wide_Type (Typ)
8fde064e 1981 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
41251c60 1982 then
dd386db0
AC
1983 -- AI05-0151: Tagged incomplete types are allowed in all formal
1984 -- parts. Untagged incomplete types are not allowed in bodies.
b973629e
AC
1985 -- As a consequence, limited views cannot appear in a basic
1986 -- declaration that is itself within a body, because there is
1987 -- no point at which the non-limited view will become visible.
dd386db0
AC
1988
1989 if Ada_Version >= Ada_2012 then
b973629e
AC
1990 if From_Limited_With (Typ) and then In_Package_Body then
1991 Error_Msg_NE
1992 ("invalid use of incomplete type&",
3f80a182 1993 Result_Definition (N), Typ);
b973629e 1994
1ebc2612
AC
1995 -- The return type of a subprogram body cannot be of a
1996 -- formal incomplete type.
1997
1998 elsif Is_Generic_Type (Typ)
1999 and then Nkind (Parent (N)) = N_Subprogram_Body
2000 then
2001 Error_Msg_N
2002 ("return type cannot be a formal incomplete type",
2003 Result_Definition (N));
2004
2005 elsif Is_Class_Wide_Type (Typ)
2006 and then Is_Generic_Type (Root_Type (Typ))
2007 and then Nkind (Parent (N)) = N_Subprogram_Body
2008 then
2009 Error_Msg_N
2010 ("return type cannot be a formal incomplete type",
2011 Result_Definition (N));
2012
b973629e 2013 elsif Is_Tagged_Type (Typ) then
dd386db0
AC
2014 null;
2015
5b6f12c7 2016 elsif Nkind (Parent (N)) = N_Subprogram_Body
31d922e3
AC
2017 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2018 N_Entry_Body)
dd386db0
AC
2019 then
2020 Error_Msg_NE
2021 ("invalid use of untagged incomplete type&",
2022 Designator, Typ);
2023 end if;
2024
63be2a5a 2025 -- The type must be completed in the current package. This
31d922e3 2026 -- is checked at the end of the package declaration when
7b7a0c2b
AC
2027 -- Taft-amendment types are identified. If the return type
2028 -- is class-wide, there is no required check, the type can
2029 -- be a bona fide TAT.
63be2a5a
AC
2030
2031 if Ekind (Scope (Current_Scope)) = E_Package
c199ccf7 2032 and then In_Private_Part (Scope (Current_Scope))
7b7a0c2b 2033 and then not Is_Class_Wide_Type (Typ)
63be2a5a
AC
2034 then
2035 Append_Elmt (Designator, Private_Dependents (Typ));
2036 end if;
2037
dd386db0
AC
2038 else
2039 Error_Msg_NE
2040 ("invalid use of incomplete type&", Designator, Typ);
2041 end if;
41251c60 2042 end if;
82c80734
RD
2043 end if;
2044
ec4867fa
ES
2045 -- Case where result definition does indicate an error
2046
82c80734
RD
2047 else
2048 Set_Etype (Designator, Any_Type);
2049 end if;
2050 end Analyze_Return_Type;
2051
996ae0b0
RK
2052 -----------------------------
2053 -- Analyze_Subprogram_Body --
2054 -----------------------------
2055
b1b543d2
BD
2056 procedure Analyze_Subprogram_Body (N : Node_Id) is
2057 Loc : constant Source_Ptr := Sloc (N);
2058 Body_Spec : constant Node_Id := Specification (N);
2059 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2060
2061 begin
2062 if Debug_Flag_C then
2063 Write_Str ("==> subprogram body ");
2064 Write_Name (Chars (Body_Id));
2065 Write_Str (" from ");
2066 Write_Location (Loc);
2067 Write_Eol;
2068 Indent;
2069 end if;
2070
2071 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2072
2073 -- The real work is split out into the helper, so it can do "return;"
2074 -- without skipping the debug output:
2075
2076 Analyze_Subprogram_Body_Helper (N);
2077
2078 if Debug_Flag_C then
2079 Outdent;
2080 Write_Str ("<== subprogram body ");
2081 Write_Name (Chars (Body_Id));
2082 Write_Str (" from ");
2083 Write_Location (Loc);
2084 Write_Eol;
2085 end if;
2086 end Analyze_Subprogram_Body;
2087
ea3c0651
AC
2088 --------------------------------------
2089 -- Analyze_Subprogram_Body_Contract --
2090 --------------------------------------
2091
ab8843fa 2092 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
2c8d828a 2093 Body_Decl : constant Node_Id := Parent (Parent (Body_Id));
c61ef416 2094 Mode : SPARK_Mode_Type;
54e28df2
HK
2095 Prag : Node_Id;
2096 Ref_Depends : Node_Id := Empty;
2097 Ref_Global : Node_Id := Empty;
2c8d828a 2098 Spec_Id : Entity_Id;
ea3c0651 2099
ea3c0651 2100 begin
fc999c5d
RD
2101 -- Due to the timing of contract analysis, delayed pragmas may be
2102 -- subject to the wrong SPARK_Mode, usually that of the enclosing
2103 -- context. To remedy this, restore the original SPARK_Mode of the
2104 -- related subprogram body.
2105
c61ef416
AC
2106 Save_SPARK_Mode_And_Set (Body_Id, Mode);
2107
77a40ec1
AC
2108 -- When a subprogram body declaration is illegal, its defining entity is
2109 -- left unanalyzed. There is nothing left to do in this case because the
dda38714 2110 -- body lacks a contract, or even a proper Ekind.
ab8843fa 2111
dda38714 2112 if Ekind (Body_Id) = E_Void then
ab8843fa
HK
2113 return;
2114 end if;
2115
2c8d828a
AC
2116 if Nkind (Body_Decl) = N_Subprogram_Body_Stub then
2117 Spec_Id := Corresponding_Spec_Of_Stub (Body_Decl);
2118 else
2119 Spec_Id := Corresponding_Spec (Body_Decl);
2120 end if;
2121
54e28df2
HK
2122 -- Locate and store pragmas Refined_Depends and Refined_Global since
2123 -- their order of analysis matters.
2124
ab8843fa
HK
2125 Prag := Classifications (Contract (Body_Id));
2126 while Present (Prag) loop
2127 if Pragma_Name (Prag) = Name_Refined_Depends then
54e28df2 2128 Ref_Depends := Prag;
ab8843fa 2129 elsif Pragma_Name (Prag) = Name_Refined_Global then
54e28df2 2130 Ref_Global := Prag;
ab8843fa
HK
2131 end if;
2132
2133 Prag := Next_Pragma (Prag);
2134 end loop;
2135
54e28df2
HK
2136 -- Analyze Refined_Global first as Refined_Depends may mention items
2137 -- classified in the global refinement.
2138
2139 if Present (Ref_Global) then
2140 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
2141
ab8843fa 2142 -- When the corresponding Global aspect/pragma references a state with
2dade097
HK
2143 -- visible refinement, the body requires Refined_Global. Refinement is
2144 -- not required when SPARK checks are suppressed.
ab8843fa 2145
54e28df2 2146 elsif Present (Spec_Id) then
ab8843fa
HK
2147 Prag := Get_Pragma (Spec_Id, Pragma_Global);
2148
2dade097
HK
2149 if SPARK_Mode /= Off
2150 and then Present (Prag)
2151 and then Contains_Refined_State (Prag)
2152 then
ab8843fa 2153 Error_Msg_NE
3ccedacc 2154 ("body of subprogram& requires global refinement",
ab8843fa
HK
2155 Body_Decl, Spec_Id);
2156 end if;
2157 end if;
54e28df2
HK
2158
2159 -- Refined_Depends must be analyzed after Refined_Global in order to see
2160 -- the modes of all global refinements.
2161
2162 if Present (Ref_Depends) then
2163 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
39d3009f
AC
2164
2165 -- When the corresponding Depends aspect/pragma references a state with
2dade097
HK
2166 -- visible refinement, the body requires Refined_Depends. Refinement is
2167 -- not required when SPARK checks are suppressed.
39d3009f
AC
2168
2169 elsif Present (Spec_Id) then
2170 Prag := Get_Pragma (Spec_Id, Pragma_Depends);
2171
2dade097
HK
2172 if SPARK_Mode /= Off
2173 and then Present (Prag)
2174 and then Contains_Refined_State (Prag)
2175 then
39d3009f 2176 Error_Msg_NE
3ccedacc 2177 ("body of subprogram& requires dependance refinement",
39d3009f
AC
2178 Body_Decl, Spec_Id);
2179 end if;
54e28df2 2180 end if;
c61ef416 2181
fc999c5d
RD
2182 -- Restore the SPARK_Mode of the enclosing context after all delayed
2183 -- pragmas have been analyzed.
2184
c61ef416 2185 Restore_SPARK_Mode (Mode);
ea3c0651
AC
2186 end Analyze_Subprogram_Body_Contract;
2187
b1b543d2
BD
2188 ------------------------------------
2189 -- Analyze_Subprogram_Body_Helper --
2190 ------------------------------------
2191
996ae0b0
RK
2192 -- This procedure is called for regular subprogram bodies, generic bodies,
2193 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2194 -- specification matters, and is used to create a proper declaration for
2195 -- the subprogram, or to perform conformance checks.
2196
b1b543d2 2197 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
fbf5a39b
AC
2198 Loc : constant Source_Ptr := Sloc (N);
2199 Body_Spec : constant Node_Id := Specification (N);
2200 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2201 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
0868e09c 2202 Conformant : Boolean;
21d27997 2203 HSS : Node_Id;
21d27997
RD
2204 Prot_Typ : Entity_Id := Empty;
2205 Spec_Id : Entity_Id;
2206 Spec_Decl : Node_Id := Empty;
2207
2208 Last_Real_Spec_Entity : Entity_Id := Empty;
2209 -- When we analyze a separate spec, the entity chain ends up containing
2210 -- the formals, as well as any itypes generated during analysis of the
2211 -- default expressions for parameters, or the arguments of associated
2212 -- precondition/postcondition pragmas (which are analyzed in the context
2213 -- of the spec since they have visibility on formals).
2214 --
2215 -- These entities belong with the spec and not the body. However we do
2216 -- the analysis of the body in the context of the spec (again to obtain
2217 -- visibility to the formals), and all the entities generated during
2218 -- this analysis end up also chained to the entity chain of the spec.
2219 -- But they really belong to the body, and there is circuitry to move
2220 -- them from the spec to the body.
2221 --
2222 -- However, when we do this move, we don't want to move the real spec
2223 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2224 -- variable points to the last real spec entity, so we only move those
2225 -- chained beyond that point. It is initialized to Empty to deal with
2226 -- the case where there is no separate spec.
996ae0b0 2227
c8a3028c
AC
2228 procedure Analyze_Aspects_On_Body_Or_Stub;
2229 -- Analyze the aspect specifications of a subprogram body [stub]. It is
2230 -- assumed that N has aspects.
2231
ac072cb2
AC
2232 function Body_Has_Contract return Boolean;
2233 -- Check whether unanalyzed body has an aspect or pragma that may
1399d355 2234 -- generate a SPARK contract.
ac072cb2 2235
ec4867fa 2236 procedure Check_Anonymous_Return;
e50e1c5e 2237 -- Ada 2005: if a function returns an access type that denotes a task,
ec4867fa
ES
2238 -- or a type that contains tasks, we must create a master entity for
2239 -- the anonymous type, which typically will be used in an allocator
2240 -- in the body of the function.
2241
e660dbf7
JM
2242 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2243 -- Look ahead to recognize a pragma that may appear after the body.
2244 -- If there is a previous spec, check that it appears in the same
2245 -- declarative part. If the pragma is Inline_Always, perform inlining
2246 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2247 -- If the body acts as a spec, and inlining is required, we create a
2248 -- subprogram declaration for it, in order to attach the body to inline.
21d27997
RD
2249 -- If pragma does not appear after the body, check whether there is
2250 -- an inline pragma before any local declarations.
c37bb106 2251
7665e4bd
AC
2252 procedure Check_Missing_Return;
2253 -- Checks for a function with a no return statements, and also performs
8d606a78
RD
2254 -- the warning checks implemented by Check_Returns. In formal mode, also
2255 -- verify that a function ends with a RETURN and that a procedure does
2256 -- not contain any RETURN.
7665e4bd 2257
d44202ba
HK
2258 function Disambiguate_Spec return Entity_Id;
2259 -- When a primitive is declared between the private view and the full
2260 -- view of a concurrent type which implements an interface, a special
2261 -- mechanism is used to find the corresponding spec of the primitive
2262 -- body.
2263
5dcab3ca
AC
2264 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2265 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2266 -- incomplete types coming from a limited context and swap their limited
2267 -- views with the non-limited ones.
2268
d44202ba
HK
2269 function Is_Private_Concurrent_Primitive
2270 (Subp_Id : Entity_Id) return Boolean;
2271 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2272 -- type that implements an interface and has a private view.
2273
76a69663
ES
2274 procedure Set_Trivial_Subprogram (N : Node_Id);
2275 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2276 -- subprogram whose body is being analyzed. N is the statement node
2277 -- causing the flag to be set, if the following statement is a return
2278 -- of an entity, we mark the entity as set in source to suppress any
2279 -- warning on the stylized use of function stubs with a dummy return.
2280
758c442c
GD
2281 procedure Verify_Overriding_Indicator;
2282 -- If there was a previous spec, the entity has been entered in the
2283 -- current scope previously. If the body itself carries an overriding
2284 -- indicator, check that it is consistent with the known status of the
2285 -- entity.
2286
c8a3028c
AC
2287 -------------------------------------
2288 -- Analyze_Aspects_On_Body_Or_Stub --
2289 -------------------------------------
2290
2291 procedure Analyze_Aspects_On_Body_Or_Stub is
2292 procedure Diagnose_Misplaced_Aspects;
2293 -- Subprogram body [stub] N has aspects, but they are not properly
2294 -- placed. Provide precise diagnostics depending on the aspects
2295 -- involved.
2296
2297 --------------------------------
2298 -- Diagnose_Misplaced_Aspects --
2299 --------------------------------
2300
2301 procedure Diagnose_Misplaced_Aspects is
2302 Asp : Node_Id;
2303 Asp_Nam : Name_Id;
2304 Asp_Id : Aspect_Id;
2305 -- The current aspect along with its name and id
2306
2307 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id);
2308 -- Emit an error message concerning SPARK aspect Asp. Ref_Nam is
2309 -- the name of the refined version of the aspect.
2310
2311 ------------------------
2312 -- SPARK_Aspect_Error --
2313 ------------------------
2314
2315 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id) is
2316 begin
2317 -- The corresponding spec already contains the aspect in
2318 -- question and the one appearing on the body must be the
2319 -- refined form:
2320
2321 -- procedure P with Global ...;
2322 -- procedure P with Global ... is ... end P;
2323 -- ^
2324 -- Refined_Global
2325
2326 if Has_Aspect (Spec_Id, Asp_Id) then
2327 Error_Msg_Name_1 := Asp_Nam;
2328
2329 -- Subunits cannot carry aspects that apply to a subprogram
2330 -- declaration.
2331
2332 if Nkind (Parent (N)) = N_Subunit then
2333 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
2334
2335 else
2336 Error_Msg_Name_2 := Ref_Nam;
2337 Error_Msg_N ("aspect % should be %", Asp);
2338 end if;
2339
2340 -- Otherwise the aspect must appear in the spec, not in the
2341 -- body:
2342
2343 -- procedure P;
2344 -- procedure P with Global ... is ... end P;
2345
2346 else
2347 Error_Msg_N
2348 ("aspect specification must appear in subprogram "
2349 & "declaration", Asp);
2350 end if;
2351 end SPARK_Aspect_Error;
2352
2353 -- Start of processing for Diagnose_Misplaced_Aspects
2354
2355 begin
2356 -- Iterate over the aspect specifications and emit specific errors
2357 -- where applicable.
2358
2359 Asp := First (Aspect_Specifications (N));
2360 while Present (Asp) loop
2361 Asp_Nam := Chars (Identifier (Asp));
2362 Asp_Id := Get_Aspect_Id (Asp_Nam);
2363
2364 -- Do not emit errors on aspects that can appear on a
2365 -- subprogram body. This scenario occurs when the aspect
2366 -- specification list contains both misplaced and properly
2367 -- placed aspects.
2368
2369 if Aspect_On_Body_Or_Stub_OK (Asp_Id) then
2370 null;
2371
2372 -- Special diagnostics for SPARK aspects
2373
2374 elsif Asp_Nam = Name_Depends then
2375 SPARK_Aspect_Error (Name_Refined_Depends);
2376
2377 elsif Asp_Nam = Name_Global then
2378 SPARK_Aspect_Error (Name_Refined_Global);
2379
2380 elsif Asp_Nam = Name_Post then
2381 SPARK_Aspect_Error (Name_Refined_Post);
2382
2383 else
2384 Error_Msg_N
2385 ("aspect specification must appear in subprogram "
2386 & "declaration", Asp);
2387 end if;
2388
2389 Next (Asp);
2390 end loop;
2391 end Diagnose_Misplaced_Aspects;
2392
2393 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
2394
2395 begin
2396 -- Language-defined aspects cannot be associated with a subprogram
2397 -- body [stub] if the subprogram has a spec. Certain implementation
2398 -- defined aspects are allowed to break this rule (for list, see
2399 -- table Aspect_On_Body_Or_Stub_OK).
2400
2401 if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then
2402 Diagnose_Misplaced_Aspects;
2403 else
2404 Analyze_Aspect_Specifications (N, Body_Id);
2405 end if;
2406 end Analyze_Aspects_On_Body_Or_Stub;
2407
ac072cb2
AC
2408 -----------------------
2409 -- Body_Has_Contract --
2410 -----------------------
2411
2412 function Body_Has_Contract return Boolean is
2413 Decls : constant List_Id := Declarations (N);
2414 A_Spec : Node_Id;
2415 A : Aspect_Id;
2416 Decl : Node_Id;
2417 P_Id : Pragma_Id;
2418
2419 begin
2420 -- Check for unanalyzed aspects in the body that will
2421 -- generate a contract.
2422
2423 if Present (Aspect_Specifications (N)) then
2424 A_Spec := First (Aspect_Specifications (N));
2425 while Present (A_Spec) loop
2426 A := Get_Aspect_Id (Chars (Identifier (A_Spec)));
2427
1399d355
AC
2428 if A = Aspect_Contract_Cases or else
2429 A = Aspect_Depends or else
2430 A = Aspect_Global or else
2431 A = Aspect_Pre or else
2432 A = Aspect_Precondition or else
2433 A = Aspect_Post or else
2434 A = Aspect_Postcondition
ac072cb2
AC
2435 then
2436 return True;
2437 end if;
2438
2439 Next (A_Spec);
2440 end loop;
2441 end if;
2442
1399d355 2443 -- Check for pragmas that may generate a contract
ac072cb2
AC
2444
2445 if Present (Decls) then
2446 Decl := First (Decls);
2447 while Present (Decl) loop
2448 if Nkind (Decl) = N_Pragma then
2449 P_Id := Get_Pragma_Id (Pragma_Name (Decl));
2450
1399d355
AC
2451 if P_Id = Pragma_Contract_Cases or else
2452 P_Id = Pragma_Depends or else
2453 P_Id = Pragma_Global or else
2454 P_Id = Pragma_Pre or else
2455 P_Id = Pragma_Precondition or else
2456 P_Id = Pragma_Post or else
2457 P_Id = Pragma_Postcondition
ac072cb2
AC
2458 then
2459 return True;
2460 end if;
2461 end if;
2462
2463 Next (Decl);
2464 end loop;
2465 end if;
2466
2467 return False;
2468 end Body_Has_Contract;
2469
ec4867fa
ES
2470 ----------------------------
2471 -- Check_Anonymous_Return --
2472 ----------------------------
2473
2474 procedure Check_Anonymous_Return is
2475 Decl : Node_Id;
a523b302 2476 Par : Node_Id;
ec4867fa
ES
2477 Scop : Entity_Id;
2478
2479 begin
2480 if Present (Spec_Id) then
2481 Scop := Spec_Id;
2482 else
2483 Scop := Body_Id;
2484 end if;
2485
2486 if Ekind (Scop) = E_Function
2487 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
a523b302
JM
2488 and then not Is_Thunk (Scop)
2489 and then (Has_Task (Designated_Type (Etype (Scop)))
2490 or else
2491 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2492 and then
2493 Is_Limited_Record (Designated_Type (Etype (Scop)))))
ec4867fa 2494 and then Expander_Active
b20de9b9 2495
8fde064e 2496 -- Avoid cases with no tasking support
b20de9b9
AC
2497
2498 and then RTE_Available (RE_Current_Master)
2499 and then not Restriction_Active (No_Task_Hierarchy)
ec4867fa
ES
2500 then
2501 Decl :=
2502 Make_Object_Declaration (Loc,
2503 Defining_Identifier =>
2504 Make_Defining_Identifier (Loc, Name_uMaster),
2505 Constant_Present => True,
2506 Object_Definition =>
e4494292 2507 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
ec4867fa
ES
2508 Expression =>
2509 Make_Explicit_Dereference (Loc,
e4494292 2510 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
ec4867fa
ES
2511
2512 if Present (Declarations (N)) then
2513 Prepend (Decl, Declarations (N));
2514 else
2515 Set_Declarations (N, New_List (Decl));
2516 end if;
2517
2518 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2519 Set_Has_Master_Entity (Scop);
a523b302
JM
2520
2521 -- Now mark the containing scope as a task master
2522
2523 Par := N;
2524 while Nkind (Par) /= N_Compilation_Unit loop
2525 Par := Parent (Par);
2526 pragma Assert (Present (Par));
2527
2528 -- If we fall off the top, we are at the outer level, and
2529 -- the environment task is our effective master, so nothing
2530 -- to mark.
2531
2532 if Nkind_In
2533 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2534 then
2535 Set_Is_Task_Master (Par, True);
2536 exit;
2537 end if;
2538 end loop;
ec4867fa
ES
2539 end if;
2540 end Check_Anonymous_Return;
2541
e660dbf7
JM
2542 -------------------------
2543 -- Check_Inline_Pragma --
2544 -------------------------
758c442c 2545
e660dbf7
JM
2546 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2547 Prag : Node_Id;
2548 Plist : List_Id;
0fb2ea01 2549
21d27997 2550 function Is_Inline_Pragma (N : Node_Id) return Boolean;
30783513 2551 -- True when N is a pragma Inline or Inline_Always that applies
33931112 2552 -- to this subprogram.
21d27997
RD
2553
2554 -----------------------
2555 -- Is_Inline_Pragma --
2556 -----------------------
2557
2558 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2559 begin
2560 return
2561 Nkind (N) = N_Pragma
2562 and then
8fde064e
AC
2563 (Pragma_Name (N) = Name_Inline_Always
2564 or else
21d27997
RD
2565 (Front_End_Inlining
2566 and then Pragma_Name (N) = Name_Inline))
2567 and then
8fde064e
AC
2568 Chars
2569 (Expression (First (Pragma_Argument_Associations (N)))) =
2570 Chars (Body_Id);
21d27997
RD
2571 end Is_Inline_Pragma;
2572
2573 -- Start of processing for Check_Inline_Pragma
2574
c37bb106 2575 begin
e660dbf7
JM
2576 if not Expander_Active then
2577 return;
2578 end if;
2579
2580 if Is_List_Member (N)
2581 and then Present (Next (N))
21d27997 2582 and then Is_Inline_Pragma (Next (N))
c37bb106
AC
2583 then
2584 Prag := Next (N);
2585
21d27997
RD
2586 elsif Nkind (N) /= N_Subprogram_Body_Stub
2587 and then Present (Declarations (N))
2588 and then Is_Inline_Pragma (First (Declarations (N)))
2589 then
2590 Prag := First (Declarations (N));
2591
e660dbf7
JM
2592 else
2593 Prag := Empty;
c37bb106 2594 end if;
e660dbf7
JM
2595
2596 if Present (Prag) then
2597 if Present (Spec_Id) then
30196a76 2598 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
e660dbf7
JM
2599 Analyze (Prag);
2600 end if;
2601
2602 else
d39d6bb8 2603 -- Create a subprogram declaration, to make treatment uniform
e660dbf7
JM
2604
2605 declare
2606 Subp : constant Entity_Id :=
30196a76 2607 Make_Defining_Identifier (Loc, Chars (Body_Id));
e660dbf7 2608 Decl : constant Node_Id :=
30196a76
RD
2609 Make_Subprogram_Declaration (Loc,
2610 Specification =>
2611 New_Copy_Tree (Specification (N)));
2612
e660dbf7
JM
2613 begin
2614 Set_Defining_Unit_Name (Specification (Decl), Subp);
2615
2616 if Present (First_Formal (Body_Id)) then
21d27997 2617 Plist := Copy_Parameter_List (Body_Id);
e660dbf7
JM
2618 Set_Parameter_Specifications
2619 (Specification (Decl), Plist);
2620 end if;
2621
2622 Insert_Before (N, Decl);
2623 Analyze (Decl);
2624 Analyze (Prag);
2625 Set_Has_Pragma_Inline (Subp);
2626
76a69663 2627 if Pragma_Name (Prag) = Name_Inline_Always then
e660dbf7 2628 Set_Is_Inlined (Subp);
21d27997 2629 Set_Has_Pragma_Inline_Always (Subp);
e660dbf7
JM
2630 end if;
2631
158d55fa
AC
2632 -- Prior to copying the subprogram body to create a template
2633 -- for it for subsequent inlining, remove the pragma from
2634 -- the current body so that the copy that will produce the
2635 -- new body will start from a completely unanalyzed tree.
2636
2637 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2638 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2639 end if;
2640
e660dbf7
JM
2641 Spec := Subp;
2642 end;
2643 end if;
2644 end if;
2645 end Check_Inline_Pragma;
2646
7665e4bd
AC
2647 --------------------------
2648 -- Check_Missing_Return --
2649 --------------------------
2650
2651 procedure Check_Missing_Return is
2652 Id : Entity_Id;
2653 Missing_Ret : Boolean;
2654
2655 begin
2656 if Nkind (Body_Spec) = N_Function_Specification then
2657 if Present (Spec_Id) then
2658 Id := Spec_Id;
2659 else
2660 Id := Body_Id;
2661 end if;
2662
fe5d3068 2663 if Return_Present (Id) then
7665e4bd
AC
2664 Check_Returns (HSS, 'F', Missing_Ret);
2665
2666 if Missing_Ret then
2667 Set_Has_Missing_Return (Id);
2668 end if;
2669
2aca76d6
AC
2670 elsif Is_Generic_Subprogram (Id)
2671 or else not Is_Machine_Code_Subprogram (Id)
7665e4bd
AC
2672 then
2673 Error_Msg_N ("missing RETURN statement in function body", N);
2674 end if;
2675
fe5d3068 2676 -- If procedure with No_Return, check returns
607d0635 2677
fe5d3068
YM
2678 elsif Nkind (Body_Spec) = N_Procedure_Specification
2679 and then Present (Spec_Id)
2680 and then No_Return (Spec_Id)
607d0635 2681 then
fe5d3068
YM
2682 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2683 end if;
2684
ad05f2e9 2685 -- Special checks in SPARK mode
fe5d3068
YM
2686
2687 if Nkind (Body_Spec) = N_Function_Specification then
7394c8cc 2688
ad05f2e9 2689 -- In SPARK mode, last statement of a function should be a return
fe5d3068
YM
2690
2691 declare
2692 Stat : constant Node_Id := Last_Source_Statement (HSS);
2693 begin
2694 if Present (Stat)
7394c8cc
AC
2695 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2696 N_Extended_Return_Statement)
fe5d3068 2697 then
ce5ba43a 2698 Check_SPARK_05_Restriction
fe5d3068
YM
2699 ("last statement in function should be RETURN", Stat);
2700 end if;
2701 end;
2702
ad05f2e9 2703 -- In SPARK mode, verify that a procedure has no return
fe5d3068
YM
2704
2705 elsif Nkind (Body_Spec) = N_Procedure_Specification then
607d0635
AC
2706 if Present (Spec_Id) then
2707 Id := Spec_Id;
2708 else
2709 Id := Body_Id;
2710 end if;
2711
8d606a78
RD
2712 -- Would be nice to point to return statement here, can we
2713 -- borrow the Check_Returns procedure here ???
2714
607d0635 2715 if Return_Present (Id) then
ce5ba43a 2716 Check_SPARK_05_Restriction
fe5d3068 2717 ("procedure should not have RETURN", N);
607d0635 2718 end if;
7665e4bd
AC
2719 end if;
2720 end Check_Missing_Return;
2721
d44202ba
HK
2722 -----------------------
2723 -- Disambiguate_Spec --
2724 -----------------------
2725
2726 function Disambiguate_Spec return Entity_Id is
2727 Priv_Spec : Entity_Id;
2728 Spec_N : Entity_Id;
2729
2730 procedure Replace_Types (To_Corresponding : Boolean);
2731 -- Depending on the flag, replace the type of formal parameters of
2732 -- Body_Id if it is a concurrent type implementing interfaces with
2733 -- the corresponding record type or the other way around.
2734
2735 procedure Replace_Types (To_Corresponding : Boolean) is
2736 Formal : Entity_Id;
2737 Formal_Typ : Entity_Id;
2738
2739 begin
2740 Formal := First_Formal (Body_Id);
2741 while Present (Formal) loop
2742 Formal_Typ := Etype (Formal);
2743
df3e68b1
HK
2744 if Is_Class_Wide_Type (Formal_Typ) then
2745 Formal_Typ := Root_Type (Formal_Typ);
2746 end if;
2747
d44202ba
HK
2748 -- From concurrent type to corresponding record
2749
2750 if To_Corresponding then
2751 if Is_Concurrent_Type (Formal_Typ)
2752 and then Present (Corresponding_Record_Type (Formal_Typ))
2753 and then Present (Interfaces (
2754 Corresponding_Record_Type (Formal_Typ)))
2755 then
2756 Set_Etype (Formal,
2757 Corresponding_Record_Type (Formal_Typ));
2758 end if;
2759
2760 -- From corresponding record to concurrent type
2761
2762 else
2763 if Is_Concurrent_Record_Type (Formal_Typ)
2764 and then Present (Interfaces (Formal_Typ))
2765 then
2766 Set_Etype (Formal,
2767 Corresponding_Concurrent_Type (Formal_Typ));
2768 end if;
2769 end if;
2770
2771 Next_Formal (Formal);
2772 end loop;
2773 end Replace_Types;
2774
2775 -- Start of processing for Disambiguate_Spec
2776
2777 begin
2778 -- Try to retrieve the specification of the body as is. All error
2779 -- messages are suppressed because the body may not have a spec in
2780 -- its current state.
2781
2782 Spec_N := Find_Corresponding_Spec (N, False);
2783
2784 -- It is possible that this is the body of a primitive declared
2785 -- between a private and a full view of a concurrent type. The
2786 -- controlling parameter of the spec carries the concurrent type,
2787 -- not the corresponding record type as transformed by Analyze_
2788 -- Subprogram_Specification. In such cases, we undo the change
2789 -- made by the analysis of the specification and try to find the
2790 -- spec again.
766d7add 2791
8198b93d
HK
2792 -- Note that wrappers already have their corresponding specs and
2793 -- bodies set during their creation, so if the candidate spec is
16b05213 2794 -- a wrapper, then we definitely need to swap all types to their
8198b93d 2795 -- original concurrent status.
d44202ba 2796
8198b93d
HK
2797 if No (Spec_N)
2798 or else Is_Primitive_Wrapper (Spec_N)
2799 then
d44202ba
HK
2800 -- Restore all references of corresponding record types to the
2801 -- original concurrent types.
2802
2803 Replace_Types (To_Corresponding => False);
2804 Priv_Spec := Find_Corresponding_Spec (N, False);
2805
2806 -- The current body truly belongs to a primitive declared between
2807 -- a private and a full view. We leave the modified body as is,
2808 -- and return the true spec.
2809
2810 if Present (Priv_Spec)
2811 and then Is_Private_Primitive (Priv_Spec)
2812 then
2813 return Priv_Spec;
2814 end if;
2815
2816 -- In case that this is some sort of error, restore the original
2817 -- state of the body.
2818
2819 Replace_Types (To_Corresponding => True);
2820 end if;
2821
2822 return Spec_N;
2823 end Disambiguate_Spec;
2824
5dcab3ca
AC
2825 ----------------------------
2826 -- Exchange_Limited_Views --
2827 ----------------------------
2828
2829 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2830 procedure Detect_And_Exchange (Id : Entity_Id);
2831 -- Determine whether Id's type denotes an incomplete type associated
2832 -- with a limited with clause and exchange the limited view with the
2833 -- non-limited one.
2834
2835 -------------------------
2836 -- Detect_And_Exchange --
2837 -------------------------
2838
2839 procedure Detect_And_Exchange (Id : Entity_Id) is
2840 Typ : constant Entity_Id := Etype (Id);
2841
2842 begin
2843 if Ekind (Typ) = E_Incomplete_Type
7b56a91b 2844 and then From_Limited_With (Typ)
5dcab3ca
AC
2845 and then Present (Non_Limited_View (Typ))
2846 then
2847 Set_Etype (Id, Non_Limited_View (Typ));
2848 end if;
2849 end Detect_And_Exchange;
2850
2851 -- Local variables
2852
2853 Formal : Entity_Id;
2854
2855 -- Start of processing for Exchange_Limited_Views
2856
2857 begin
2858 if No (Subp_Id) then
2859 return;
2860
2861 -- Do not process subprogram bodies as they already use the non-
2862 -- limited view of types.
2863
2864 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2865 return;
2866 end if;
2867
2868 -- Examine all formals and swap views when applicable
2869
2870 Formal := First_Formal (Subp_Id);
2871 while Present (Formal) loop
2872 Detect_And_Exchange (Formal);
2873
2874 Next_Formal (Formal);
2875 end loop;
2876
2877 -- Process the return type of a function
2878
2879 if Ekind (Subp_Id) = E_Function then
2880 Detect_And_Exchange (Subp_Id);
2881 end if;
2882 end Exchange_Limited_Views;
2883
d44202ba
HK
2884 -------------------------------------
2885 -- Is_Private_Concurrent_Primitive --
2886 -------------------------------------
2887
2888 function Is_Private_Concurrent_Primitive
2889 (Subp_Id : Entity_Id) return Boolean
2890 is
2891 Formal_Typ : Entity_Id;
2892
2893 begin
2894 if Present (First_Formal (Subp_Id)) then
2895 Formal_Typ := Etype (First_Formal (Subp_Id));
2896
2897 if Is_Concurrent_Record_Type (Formal_Typ) then
df3e68b1
HK
2898 if Is_Class_Wide_Type (Formal_Typ) then
2899 Formal_Typ := Root_Type (Formal_Typ);
2900 end if;
2901
d44202ba
HK
2902 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2903 end if;
2904
2905 -- The type of the first formal is a concurrent tagged type with
2906 -- a private view.
2907
2908 return
2909 Is_Concurrent_Type (Formal_Typ)
2910 and then Is_Tagged_Type (Formal_Typ)
2911 and then Has_Private_Declaration (Formal_Typ);
2912 end if;
2913
2914 return False;
2915 end Is_Private_Concurrent_Primitive;
2916
76a69663
ES
2917 ----------------------------
2918 -- Set_Trivial_Subprogram --
2919 ----------------------------
2920
2921 procedure Set_Trivial_Subprogram (N : Node_Id) is
2922 Nxt : constant Node_Id := Next (N);
2923
2924 begin
2925 Set_Is_Trivial_Subprogram (Body_Id);
2926
2927 if Present (Spec_Id) then
2928 Set_Is_Trivial_Subprogram (Spec_Id);
2929 end if;
2930
2931 if Present (Nxt)
2932 and then Nkind (Nxt) = N_Simple_Return_Statement
2933 and then No (Next (Nxt))
2934 and then Present (Expression (Nxt))
2935 and then Is_Entity_Name (Expression (Nxt))
2936 then
2937 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2938 end if;
2939 end Set_Trivial_Subprogram;
2940
758c442c
GD
2941 ---------------------------------
2942 -- Verify_Overriding_Indicator --
2943 ---------------------------------
2944
2945 procedure Verify_Overriding_Indicator is
2946 begin
21d27997
RD
2947 if Must_Override (Body_Spec) then
2948 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2949 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2950 then
2951 null;
2952
038140ed 2953 elsif not Present (Overridden_Operation (Spec_Id)) then
ed2233dc 2954 Error_Msg_NE
21d27997 2955 ("subprogram& is not overriding", Body_Spec, Spec_Id);
23e28b42
AC
2956
2957 -- Overriding indicators aren't allowed for protected subprogram
2958 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2959 -- this to a warning if -gnatd.E is enabled.
2960
2961 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2962 Error_Msg_Warn := Error_To_Warning;
2963 Error_Msg_N
b785e0b8 2964 ("<<overriding indicator not allowed for protected "
23e28b42 2965 & "subprogram body", Body_Spec);
21d27997 2966 end if;
758c442c 2967
5d37ba92 2968 elsif Must_Not_Override (Body_Spec) then
038140ed 2969 if Present (Overridden_Operation (Spec_Id)) then
ed2233dc 2970 Error_Msg_NE
5d37ba92 2971 ("subprogram& overrides inherited operation",
76a69663 2972 Body_Spec, Spec_Id);
5d37ba92 2973
21d27997
RD
2974 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2975 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2976 then
ed2233dc 2977 Error_Msg_NE
3ccedacc 2978 ("subprogram& overrides predefined operator ",
21d27997
RD
2979 Body_Spec, Spec_Id);
2980
23e28b42
AC
2981 -- Overriding indicators aren't allowed for protected subprogram
2982 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2983 -- this to a warning if -gnatd.E is enabled.
2984
2985 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2986 Error_Msg_Warn := Error_To_Warning;
5d37ba92 2987
23e28b42 2988 Error_Msg_N
3ccedacc
AC
2989 ("<<overriding indicator not allowed "
2990 & "for protected subprogram body", Body_Spec);
23e28b42
AC
2991
2992 -- If this is not a primitive operation, then the overriding
2993 -- indicator is altogether illegal.
2994
2995 elsif not Is_Primitive (Spec_Id) then
ed2233dc 2996 Error_Msg_N
3ccedacc
AC
2997 ("overriding indicator only allowed "
2998 & "if subprogram is primitive", Body_Spec);
5d37ba92 2999 end if;
235f4375 3000
23e28b42
AC
3001 -- If checking the style rule and the operation overrides, then
3002 -- issue a warning about a missing overriding_indicator. Protected
3003 -- subprogram bodies are excluded from this style checking, since
3004 -- they aren't primitives (even though their declarations can
3005 -- override) and aren't allowed to have an overriding_indicator.
3006
806f6d37 3007 elsif Style_Check
038140ed 3008 and then Present (Overridden_Operation (Spec_Id))
23e28b42 3009 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
235f4375
AC
3010 then
3011 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3012 Style.Missing_Overriding (N, Body_Id);
806f6d37
AC
3013
3014 elsif Style_Check
3015 and then Can_Override_Operator (Spec_Id)
3016 and then not Is_Predefined_File_Name
3017 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
3018 then
3019 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3020 Style.Missing_Overriding (N, Body_Id);
758c442c
GD
3021 end if;
3022 end Verify_Overriding_Indicator;
3023
b1b543d2 3024 -- Start of processing for Analyze_Subprogram_Body_Helper
0fb2ea01 3025
996ae0b0 3026 begin
82c80734
RD
3027 -- Generic subprograms are handled separately. They always have a
3028 -- generic specification. Determine whether current scope has a
3029 -- previous declaration.
996ae0b0 3030
82c80734
RD
3031 -- If the subprogram body is defined within an instance of the same
3032 -- name, the instance appears as a package renaming, and will be hidden
3033 -- within the subprogram.
996ae0b0
RK
3034
3035 if Present (Prev_Id)
3036 and then not Is_Overloadable (Prev_Id)
3037 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3038 or else Comes_From_Source (Prev_Id))
3039 then
fbf5a39b 3040 if Is_Generic_Subprogram (Prev_Id) then
996ae0b0
RK
3041 Spec_Id := Prev_Id;
3042 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3043 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3044
3045 Analyze_Generic_Subprogram_Body (N, Spec_Id);
7665e4bd
AC
3046
3047 if Nkind (N) = N_Subprogram_Body then
3048 HSS := Handled_Statement_Sequence (N);
3049 Check_Missing_Return;
3050 end if;
3051
996ae0b0
RK
3052 return;
3053
3054 else
82c80734
RD
3055 -- Previous entity conflicts with subprogram name. Attempting to
3056 -- enter name will post error.
996ae0b0
RK
3057
3058 Enter_Name (Body_Id);
3059 return;
3060 end if;
3061
82c80734
RD
3062 -- Non-generic case, find the subprogram declaration, if one was seen,
3063 -- or enter new overloaded entity in the current scope. If the
3064 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3065 -- part of the context of one of its subunits. No need to redo the
3066 -- analysis.
996ae0b0 3067
8fde064e 3068 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
996ae0b0
RK
3069 return;
3070
3071 else
fbf5a39b 3072 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
996ae0b0
RK
3073
3074 if Nkind (N) = N_Subprogram_Body_Stub
3075 or else No (Corresponding_Spec (N))
3076 then
d44202ba
HK
3077 if Is_Private_Concurrent_Primitive (Body_Id) then
3078 Spec_Id := Disambiguate_Spec;
3079 else
3080 Spec_Id := Find_Corresponding_Spec (N);
b6c8e5be
AC
3081
3082 -- In GNATprove mode, if the body has no previous spec, create
3083 -- one so that the inlining machinery can operate properly.
3084 -- Transfer aspects, if any, to the new spec, so that they
3085 -- are legal and can be processed ahead of the body.
3086 -- We make two copies of the given spec, one for the new
3087 -- declaration, and one for the body.
3088
b6c8e5be
AC
3089 if No (Spec_Id)
3090 and then GNATprove_Mode
480156b2 3091
480156b2
AC
3092 -- Inlining does not apply during pre-analysis of code
3093
b6c8e5be 3094 and then Full_Analysis
480156b2
AC
3095
3096 -- Inlining only applies to full bodies, not stubs
3097
7b2888e6 3098 and then Nkind (N) /= N_Subprogram_Body_Stub
480156b2
AC
3099
3100 -- Inlining only applies to bodies in the source code, not to
3101 -- those generated by the compiler. In particular, expression
3102 -- functions, whose body is generated by the compiler, are
3103 -- treated specially by GNATprove.
3104
b6c8e5be 3105 and then Comes_From_Source (Body_Id)
480156b2
AC
3106
3107 -- This cannot be done for a compilation unit, which is not
3108 -- in a context where we can insert a new spec.
3109
b6c8e5be 3110 and then Is_List_Member (N)
480156b2
AC
3111
3112 -- Inlining only applies to subprograms without contracts,
3113 -- as a contract is a sign that GNATprove should perform a
3114 -- modular analysis of the subprogram instead of a contextual
3115 -- analysis at each call site. The same test is performed in
3116 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3117 -- here in another form (because the contract has not
3118 -- been attached to the body) to avoid frontend errors in
3119 -- case pragmas are used instead of aspects, because the
3120 -- corresponding pragmas in the body would not be transferred
3121 -- to the spec, leading to legality errors.
3122
ac072cb2 3123 and then not Body_Has_Contract
b6c8e5be
AC
3124 then
3125 declare
3126 Body_Spec : constant Node_Id :=
662c2ad4 3127 Copy_Separate_Tree (Specification (N));
eb1ee757
AC
3128 New_Decl : constant Node_Id :=
3129 Make_Subprogram_Declaration (Loc,
3130 Copy_Separate_Tree (Specification (N)));
3131
2178830b 3132 SPARK_Mode_Aspect : Node_Id;
eb1ee757
AC
3133 Aspects : List_Id;
3134 Prag, Aspect : Node_Id;
b6c8e5be
AC
3135
3136 begin
3137 Insert_Before (N, New_Decl);
3138 Move_Aspects (From => N, To => New_Decl);
2178830b
AC
3139
3140 -- Mark the newly moved aspects as not analyzed, so that
3141 -- their effect on New_Decl is properly analyzed.
3142
3143 Aspect := First (Aspect_Specifications (New_Decl));
3144 while Present (Aspect) loop
3145 Set_Analyzed (Aspect, False);
3146 Next (Aspect);
3147 end loop;
3148
b6c8e5be 3149 Analyze (New_Decl);
2178830b
AC
3150
3151 -- The analysis of the generated subprogram declaration
eb1ee757 3152 -- may have introduced pragmas that need to be analyzed.
2178830b
AC
3153
3154 Prag := Next (New_Decl);
3155 while Prag /= N loop
3156 Analyze (Prag);
3157 Next (Prag);
3158 end loop;
3159
b6c8e5be
AC
3160 Spec_Id := Defining_Entity (New_Decl);
3161
793c5f05
AC
3162 -- As Body_Id originally comes from source, mark the new
3163 -- Spec_Id as such, which is required so that calls to
3164 -- this subprogram are registered in the local effects
3165 -- stored in ALI files for GNATprove.
3166
3167 Set_Comes_From_Source (Spec_Id, True);
3168
2178830b
AC
3169 -- If aspect SPARK_Mode was specified on the body, it
3170 -- needs to be repeated on the generated decl and the
3171 -- body. Since the original aspect was moved to the
3172 -- generated decl, copy it for the body.
3173
3174 if Has_Aspect (Spec_Id, Aspect_SPARK_Mode) then
3175 SPARK_Mode_Aspect :=
3176 New_Copy (Find_Aspect (Spec_Id, Aspect_SPARK_Mode));
3177 Set_Analyzed (SPARK_Mode_Aspect, False);
eb1ee757 3178 Aspects := New_List (SPARK_Mode_Aspect);
2178830b
AC
3179 Set_Aspect_Specifications (N, Aspects);
3180 end if;
3181
b6c8e5be
AC
3182 Set_Specification (N, Body_Spec);
3183 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3184 Set_Corresponding_Spec (N, Spec_Id);
3185 end;
3186 end if;
d44202ba 3187 end if;
996ae0b0
RK
3188
3189 -- If this is a duplicate body, no point in analyzing it
3190
3191 if Error_Posted (N) then
3192 return;
3193 end if;
3194
82c80734
RD
3195 -- A subprogram body should cause freezing of its own declaration,
3196 -- but if there was no previous explicit declaration, then the
3197 -- subprogram will get frozen too late (there may be code within
3198 -- the body that depends on the subprogram having been frozen,
3199 -- such as uses of extra formals), so we force it to be frozen
76a69663 3200 -- here. Same holds if the body and spec are compilation units.
cd1c668b
ES
3201 -- Finally, if the return type is an anonymous access to protected
3202 -- subprogram, it must be frozen before the body because its
3203 -- expansion has generated an equivalent type that is used when
3204 -- elaborating the body.
996ae0b0 3205
885c4871 3206 -- An exception in the case of Ada 2012, AI05-177: The bodies
ebb6faaa
AC
3207 -- created for expression functions do not freeze.
3208
3209 if No (Spec_Id)
3210 and then Nkind (Original_Node (N)) /= N_Expression_Function
3211 then
996ae0b0
RK
3212 Freeze_Before (N, Body_Id);
3213
3214 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3215 Freeze_Before (N, Spec_Id);
cd1c668b
ES
3216
3217 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3218 Freeze_Before (N, Etype (Body_Id));
996ae0b0 3219 end if;
a38ff9b1 3220
996ae0b0
RK
3221 else
3222 Spec_Id := Corresponding_Spec (N);
3223 end if;
3224 end if;
3225
799d0e05
AC
3226 -- Previously we scanned the body to look for nested subprograms, and
3227 -- rejected an inline directive if nested subprograms were present,
3228 -- because the back-end would generate conflicting symbols for the
c8957aae 3229 -- nested bodies. This is now unnecessary.
07fc65c4 3230
c8957aae 3231 -- Look ahead to recognize a pragma Inline that appears after the body
84f4072a 3232
e660dbf7
JM
3233 Check_Inline_Pragma (Spec_Id);
3234
701b7fbb
RD
3235 -- Deal with special case of a fully private operation in the body of
3236 -- the protected type. We must create a declaration for the subprogram,
3237 -- in order to attach the protected subprogram that will be used in
3238 -- internal calls. We exclude compiler generated bodies from the
3239 -- expander since the issue does not arise for those cases.
07fc65c4 3240
996ae0b0
RK
3241 if No (Spec_Id)
3242 and then Comes_From_Source (N)
3243 and then Is_Protected_Type (Current_Scope)
3244 then
47bfea3a 3245 Spec_Id := Build_Private_Protected_Declaration (N);
701b7fbb 3246 end if;
996ae0b0 3247
5334d18f 3248 -- If a separate spec is present, then deal with freezing issues
7ca78bba 3249
701b7fbb 3250 if Present (Spec_Id) then
996ae0b0 3251 Spec_Decl := Unit_Declaration_Node (Spec_Id);
758c442c 3252 Verify_Overriding_Indicator;
5d37ba92
ES
3253
3254 -- In general, the spec will be frozen when we start analyzing the
3255 -- body. However, for internally generated operations, such as
3256 -- wrapper functions for inherited operations with controlling
164e06c6
AC
3257 -- results, the spec may not have been frozen by the time we expand
3258 -- the freeze actions that include the bodies. In particular, extra
3259 -- formals for accessibility or for return-in-place may need to be
3260 -- generated. Freeze nodes, if any, are inserted before the current
3261 -- body. These freeze actions are also needed in ASIS mode to enable
3262 -- the proper back-annotations.
5d37ba92
ES
3263
3264 if not Is_Frozen (Spec_Id)
7134062a 3265 and then (Expander_Active or ASIS_Mode)
5d37ba92
ES
3266 then
3267 -- Force the generation of its freezing node to ensure proper
3268 -- management of access types in the backend.
3269
3270 -- This is definitely needed for some cases, but it is not clear
3271 -- why, to be investigated further???
3272
3273 Set_Has_Delayed_Freeze (Spec_Id);
6b958cec 3274 Freeze_Before (N, Spec_Id);
5d37ba92 3275 end if;
996ae0b0
RK
3276 end if;
3277
a5d83d61
AC
3278 -- Mark presence of postcondition procedure in current scope and mark
3279 -- the procedure itself as needing debug info. The latter is important
3280 -- when analyzing decision coverage (for example, for MC/DC coverage).
7ca78bba 3281
0dabde3a
ES
3282 if Chars (Body_Id) = Name_uPostconditions then
3283 Set_Has_Postconditions (Current_Scope);
a5d83d61 3284 Set_Debug_Info_Needed (Body_Id);
0dabde3a
ES
3285 end if;
3286
996ae0b0
RK
3287 -- Place subprogram on scope stack, and make formals visible. If there
3288 -- is a spec, the visible entity remains that of the spec.
3289
3290 if Present (Spec_Id) then
07fc65c4 3291 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
758c442c
GD
3292
3293 if Is_Child_Unit (Spec_Id) then
3294 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3295 end if;
3296
fbf5a39b
AC
3297 if Style_Check then
3298 Style.Check_Identifier (Body_Id, Spec_Id);
3299 end if;
996ae0b0
RK
3300
3301 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3302 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3303
f937473f 3304 if Is_Abstract_Subprogram (Spec_Id) then
ed2233dc 3305 Error_Msg_N ("an abstract subprogram cannot have a body", N);
996ae0b0 3306 return;
21d27997 3307
996ae0b0
RK
3308 else
3309 Set_Convention (Body_Id, Convention (Spec_Id));
3310 Set_Has_Completion (Spec_Id);
3311
3312 if Is_Protected_Type (Scope (Spec_Id)) then
21d27997 3313 Prot_Typ := Scope (Spec_Id);
996ae0b0
RK
3314 end if;
3315
3316 -- If this is a body generated for a renaming, do not check for
3317 -- full conformance. The check is redundant, because the spec of
3318 -- the body is a copy of the spec in the renaming declaration,
3319 -- and the test can lead to spurious errors on nested defaults.
3320
3321 if Present (Spec_Decl)
996ae0b0 3322 and then not Comes_From_Source (N)
93a81b02
GB
3323 and then
3324 (Nkind (Original_Node (Spec_Decl)) =
d2f97d3e 3325 N_Subprogram_Renaming_Declaration
466c2127
AC
3326 or else (Present (Corresponding_Body (Spec_Decl))
3327 and then
3328 Nkind (Unit_Declaration_Node
3329 (Corresponding_Body (Spec_Decl))) =
3330 N_Subprogram_Renaming_Declaration))
996ae0b0
RK
3331 then
3332 Conformant := True;
cabe9abc
AC
3333
3334 -- Conversely, the spec may have been generated for specless body
3335 -- with an inline pragma.
3336
3337 elsif Comes_From_Source (N)
3338 and then not Comes_From_Source (Spec_Id)
3339 and then Has_Pragma_Inline (Spec_Id)
3340 then
3341 Conformant := True;
76a69663 3342
996ae0b0
RK
3343 else
3344 Check_Conformance
3345 (Body_Id, Spec_Id,
76a69663 3346 Fully_Conformant, True, Conformant, Body_Id);
996ae0b0
RK
3347 end if;
3348
3349 -- If the body is not fully conformant, we have to decide if we
3350 -- should analyze it or not. If it has a really messed up profile
3351 -- then we probably should not analyze it, since we will get too
3352 -- many bogus messages.
3353
3354 -- Our decision is to go ahead in the non-fully conformant case
3355 -- only if it is at least mode conformant with the spec. Note
3356 -- that the call to Check_Fully_Conformant has issued the proper
3357 -- error messages to complain about the lack of conformance.
3358
3359 if not Conformant
3360 and then not Mode_Conformant (Body_Id, Spec_Id)
3361 then
3362 return;
3363 end if;
3364 end if;
3365
996ae0b0 3366 if Spec_Id /= Body_Id then
fbf5a39b 3367 Reference_Body_Formals (Spec_Id, Body_Id);
996ae0b0
RK
3368 end if;
3369
579847c2
AC
3370 Set_Ekind (Body_Id, E_Subprogram_Body);
3371
e28072cd
AC
3372 if Nkind (N) = N_Subprogram_Body_Stub then
3373 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3374
3375 -- Regular body
3376
3377 else
996ae0b0 3378 Set_Corresponding_Spec (N, Spec_Id);
758c442c 3379
5d37ba92
ES
3380 -- Ada 2005 (AI-345): If the operation is a primitive operation
3381 -- of a concurrent type, the type of the first parameter has been
3382 -- replaced with the corresponding record, which is the proper
3383 -- run-time structure to use. However, within the body there may
3384 -- be uses of the formals that depend on primitive operations
3385 -- of the type (in particular calls in prefixed form) for which
3386 -- we need the original concurrent type. The operation may have
3387 -- several controlling formals, so the replacement must be done
3388 -- for all of them.
758c442c
GD
3389
3390 if Comes_From_Source (Spec_Id)
3391 and then Present (First_Entity (Spec_Id))
3392 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3393 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
15918371
AC
3394 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3395 and then Present (Corresponding_Concurrent_Type
3396 (Etype (First_Entity (Spec_Id))))
758c442c 3397 then
5d37ba92
ES
3398 declare
3399 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3400 Form : Entity_Id;
3401
3402 begin
3403 Form := First_Formal (Spec_Id);
3404 while Present (Form) loop
3405 if Etype (Form) = Typ then
3406 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3407 end if;
3408
3409 Next_Formal (Form);
3410 end loop;
3411 end;
758c442c
GD
3412 end if;
3413
21d27997
RD
3414 -- Make the formals visible, and place subprogram on scope stack.
3415 -- This is also the point at which we set Last_Real_Spec_Entity
3416 -- to mark the entities which will not be moved to the body.
758c442c 3417
996ae0b0 3418 Install_Formals (Spec_Id);
21d27997 3419 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
616547fa
AC
3420
3421 -- Within an instance, add local renaming declarations so that
a5a809b2
AC
3422 -- gdb can retrieve the values of actuals more easily. This is
3423 -- only relevant if generating code (and indeed we definitely
3424 -- do not want these definitions -gnatc mode, because that would
3425 -- confuse ASIS).
616547fa
AC
3426
3427 if Is_Generic_Instance (Spec_Id)
3428 and then Is_Wrapper_Package (Current_Scope)
a5a809b2 3429 and then Expander_Active
616547fa
AC
3430 then
3431 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3432 end if;
3433
0a36105d 3434 Push_Scope (Spec_Id);
996ae0b0
RK
3435
3436 -- Make sure that the subprogram is immediately visible. For
3437 -- child units that have no separate spec this is indispensable.
3438 -- Otherwise it is safe albeit redundant.
3439
3440 Set_Is_Immediately_Visible (Spec_Id);
3441 end if;
3442
3443 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
ea3c0651 3444 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
996ae0b0 3445 Set_Scope (Body_Id, Scope (Spec_Id));
ec4867fa 3446 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
996ae0b0
RK
3447
3448 -- Case of subprogram body with no previous spec
3449
3450 else
3e5daac4
AC
3451 -- Check for style warning required
3452
996ae0b0 3453 if Style_Check
3e5daac4
AC
3454
3455 -- Only apply check for source level subprograms for which checks
3456 -- have not been suppressed.
3457
996ae0b0
RK
3458 and then Comes_From_Source (Body_Id)
3459 and then not Suppress_Style_Checks (Body_Id)
3e5daac4
AC
3460
3461 -- No warnings within an instance
3462
996ae0b0 3463 and then not In_Instance
3e5daac4 3464
b0186f71 3465 -- No warnings for expression functions
3e5daac4 3466
b0186f71 3467 and then Nkind (Original_Node (N)) /= N_Expression_Function
996ae0b0
RK
3468 then
3469 Style.Body_With_No_Spec (N);
3470 end if;
3471
3472 New_Overloaded_Entity (Body_Id);
3473
3474 if Nkind (N) /= N_Subprogram_Body_Stub then
3475 Set_Acts_As_Spec (N);
3476 Generate_Definition (Body_Id);
dac3bede 3477 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
fbf5a39b
AC
3478 Generate_Reference
3479 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
996ae0b0 3480 Install_Formals (Body_Id);
e949ee22 3481
4a854847 3482 Push_Scope (Body_Id);
996ae0b0 3483 end if;
dbe36d67
AC
3484
3485 -- For stubs and bodies with no previous spec, generate references to
3486 -- formals.
3487
3488 Generate_Reference_To_Formals (Body_Id);
996ae0b0
RK
3489 end if;
3490
5216b599
AC
3491 -- Set SPARK_Mode from context
3492
3493 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3494 Set_SPARK_Pragma_Inherited (Body_Id, True);
3495
76a69663
ES
3496 -- If the return type is an anonymous access type whose designated type
3497 -- is the limited view of a class-wide type and the non-limited view is
3498 -- available, update the return type accordingly.
ec4867fa 3499
8fde064e 3500 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
ec4867fa 3501 declare
ec4867fa 3502 Etyp : Entity_Id;
0a36105d 3503 Rtyp : Entity_Id;
ec4867fa
ES
3504
3505 begin
0a36105d
JM
3506 Rtyp := Etype (Current_Scope);
3507
3508 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3509 Etyp := Directly_Designated_Type (Rtyp);
3510
7b56a91b
AC
3511 if Is_Class_Wide_Type (Etyp)
3512 and then From_Limited_With (Etyp)
3513 then
0a36105d
JM
3514 Set_Directly_Designated_Type
3515 (Etype (Current_Scope), Available_View (Etyp));
3516 end if;
3517 end if;
ec4867fa
ES
3518 end;
3519 end if;
3520
996ae0b0
RK
3521 -- If this is the proper body of a stub, we must verify that the stub
3522 -- conforms to the body, and to the previous spec if one was present.
dbe36d67 3523 -- We know already that the body conforms to that spec. This test is
996ae0b0
RK
3524 -- only required for subprograms that come from source.
3525
3526 if Nkind (Parent (N)) = N_Subunit
3527 and then Comes_From_Source (N)
3528 and then not Error_Posted (Body_Id)
e895b435
ES
3529 and then Nkind (Corresponding_Stub (Parent (N))) =
3530 N_Subprogram_Body_Stub
996ae0b0
RK
3531 then
3532 declare
fbf5a39b
AC
3533 Old_Id : constant Entity_Id :=
3534 Defining_Entity
3535 (Specification (Corresponding_Stub (Parent (N))));
3536
996ae0b0 3537 Conformant : Boolean := False;
996ae0b0
RK
3538
3539 begin
3540 if No (Spec_Id) then
3541 Check_Fully_Conformant (Body_Id, Old_Id);
3542
3543 else
3544 Check_Conformance
3545 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3546
3547 if not Conformant then
3548
dbe36d67
AC
3549 -- The stub was taken to be a new declaration. Indicate that
3550 -- it lacks a body.
996ae0b0
RK
3551
3552 Set_Has_Completion (Old_Id, False);
3553 end if;
3554 end if;
3555 end;
3556 end if;
3557
3558 Set_Has_Completion (Body_Id);
3559 Check_Eliminated (Body_Id);
3560
3561 if Nkind (N) = N_Subprogram_Body_Stub then
c8a3028c
AC
3562
3563 -- Analyze any aspect specifications that appear on the subprogram
3564 -- body stub.
3565
3566 if Has_Aspects (N) then
3567 Analyze_Aspects_On_Body_Or_Stub;
3568 end if;
3569
3570 -- Stop the analysis now as the stub cannot be inlined, plus it does
3571 -- not have declarative or statement lists.
3572
996ae0b0 3573 return;
84f4072a 3574 end if;
996ae0b0 3575
b94b6c56 3576 -- Handle frontend inlining
84f4072a 3577
b94b6c56
RD
3578 -- Note: Normally we don't do any inlining if expansion is off, since
3579 -- we won't generate code in any case. An exception arises in GNATprove
2d180af1
YM
3580 -- mode where we want to expand some calls in place, even with expansion
3581 -- disabled, since the inlining eases formal verification.
ecad37f3 3582
6c26bac2
AC
3583 if not GNATprove_Mode
3584 and then Expander_Active
3585 and then Serious_Errors_Detected = 0
3586 and then Present (Spec_Id)
3587 and then Has_Pragma_Inline (Spec_Id)
3588 then
3589 -- Legacy implementation (relying on frontend inlining)
84f4072a 3590
6c26bac2
AC
3591 if not Back_End_Inlining then
3592 if Has_Pragma_Inline_Always (Spec_Id)
ea0c8cfb 3593 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining)
6c26bac2
AC
3594 then
3595 Build_Body_To_Inline (N, Spec_Id);
3596 end if;
f087ea44 3597
6c26bac2
AC
3598 -- New implementation (relying on backend inlining). Enabled by
3599 -- debug flag gnatd.z for testing
f087ea44 3600
6c26bac2
AC
3601 else
3602 if Has_Pragma_Inline_Always (Spec_Id)
3603 or else Optimization_Level > 0
3604 then
3605 -- Handle function returning an unconstrained type
f087ea44 3606
6c26bac2
AC
3607 if Comes_From_Source (Body_Id)
3608 and then Ekind (Spec_Id) = E_Function
3609 and then Returns_Unconstrained_Type (Spec_Id)
3610 then
16b10ccc 3611 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
84f4072a 3612
6c26bac2
AC
3613 else
3614 declare
b80a2b4b
AC
3615 Subp_Body : constant Node_Id :=
3616 Unit_Declaration_Node (Body_Id);
6c26bac2 3617 Subp_Decl : constant List_Id := Declarations (Subp_Body);
84f4072a 3618
6c26bac2
AC
3619 begin
3620 -- Do not pass inlining to the backend if the subprogram
3621 -- has declarations or statements which cannot be inlined
3622 -- by the backend. This check is done here to emit an
3623 -- error instead of the generic warning message reported
3624 -- by the GCC backend (ie. "function might not be
3625 -- inlinable").
3626
3627 if Present (Subp_Decl)
3628 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
3629 then
3630 null;
3631
3632 elsif Has_Excluded_Statement
3633 (Spec_Id,
3634 Statements
3635 (Handled_Statement_Sequence (Subp_Body)))
3636 then
3637 null;
3638
3639 -- If the backend inlining is available then at this
3640 -- stage we only have to mark the subprogram as inlined.
3641 -- The expander will take care of registering it in the
3642 -- table of subprograms inlined by the backend a part of
3643 -- processing calls to it (cf. Expand_Call)
3644
3645 else
3646 Set_Is_Inlined (Spec_Id);
3647 end if;
3648 end;
3649 end if;
3650 end if;
3651 end if;
2d180af1
YM
3652
3653 -- In GNATprove mode, inline only when there is a separate subprogram
3654 -- declaration for now, as inlining of subprogram bodies acting as
3655 -- declarations, or subprogram stubs, are not supported by frontend
3656 -- inlining. This inlining should occur after analysis of the body, so
3657 -- that it is known whether the value of SPARK_Mode applicable to the
3658 -- body, which can be defined by a pragma inside the body.
3659
3660 elsif GNATprove_Mode
2d180af1
YM
3661 and then Full_Analysis
3662 and then not Inside_A_Generic
3663 and then Present (Spec_Id)
3664 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3665 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
ac072cb2 3666 and then not Body_Has_Contract
2d180af1 3667 then
6c26bac2 3668 Build_Body_To_Inline (N, Spec_Id);
996ae0b0
RK
3669 end if;
3670
0ab80019 3671 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
dbe36d67 3672 -- of the specification we have to install the private withed units.
21d27997 3673 -- This holds for child units as well.
9bc856dd
AC
3674
3675 if Is_Compilation_Unit (Body_Id)
21d27997 3676 or else Nkind (Parent (N)) = N_Compilation_Unit
9bc856dd
AC
3677 then
3678 Install_Private_With_Clauses (Body_Id);
3679 end if;
3680
ec4867fa
ES
3681 Check_Anonymous_Return;
3682
fdce4bb7
JM
3683 -- Set the Protected_Formal field of each extra formal of the protected
3684 -- subprogram to reference the corresponding extra formal of the
3685 -- subprogram that implements it. For regular formals this occurs when
3686 -- the protected subprogram's declaration is expanded, but the extra
3687 -- formals don't get created until the subprogram is frozen. We need to
3688 -- do this before analyzing the protected subprogram's body so that any
3689 -- references to the original subprogram's extra formals will be changed
3690 -- refer to the implementing subprogram's formals (see Expand_Formal).
3691
3692 if Present (Spec_Id)
3693 and then Is_Protected_Type (Scope (Spec_Id))
3694 and then Present (Protected_Body_Subprogram (Spec_Id))
3695 then
3696 declare
3697 Impl_Subp : constant Entity_Id :=
3698 Protected_Body_Subprogram (Spec_Id);
3699 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3700 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
fdce4bb7
JM
3701 begin
3702 while Present (Prot_Ext_Formal) loop
3703 pragma Assert (Present (Impl_Ext_Formal));
fdce4bb7 3704 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
fdce4bb7
JM
3705 Next_Formal_With_Extras (Prot_Ext_Formal);
3706 Next_Formal_With_Extras (Impl_Ext_Formal);
3707 end loop;
3708 end;
3709 end if;
3710
0868e09c 3711 -- Now we can go on to analyze the body
996ae0b0
RK
3712
3713 HSS := Handled_Statement_Sequence (N);
3714 Set_Actual_Subtypes (N, Current_Scope);
21d27997 3715
f3d0f304 3716 -- Add a declaration for the Protection object, renaming declarations
21d27997
RD
3717 -- for discriminals and privals and finally a declaration for the entry
3718 -- family index (if applicable). This form of early expansion is done
3719 -- when the Expander is active because Install_Private_Data_Declarations
81bf2382 3720 -- references entities which were created during regular expansion. The
3b8056a5
AC
3721 -- subprogram entity must come from source, and not be an internally
3722 -- generated subprogram.
21d27997 3723
4460a9bc 3724 if Expander_Active
21d27997
RD
3725 and then Present (Prot_Typ)
3726 and then Present (Spec_Id)
3b8056a5 3727 and then Comes_From_Source (Spec_Id)
21d27997
RD
3728 and then not Is_Eliminated (Spec_Id)
3729 then
3730 Install_Private_Data_Declarations
3731 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3732 end if;
3733
5dcab3ca
AC
3734 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3735 -- may now appear in parameter and result profiles. Since the analysis
3736 -- of a subprogram body may use the parameter and result profile of the
3737 -- spec, swap any limited views with their non-limited counterpart.
3738
3739 if Ada_Version >= Ada_2012 then
3740 Exchange_Limited_Views (Spec_Id);
3741 end if;
3742
c8a3028c
AC
3743 -- Analyze any aspect specifications that appear on the subprogram body
3744
3745 if Has_Aspects (N) then
3746 Analyze_Aspects_On_Body_Or_Stub;
3747 end if;
3748
3749 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
3750 -- invariants and predicates associated with the body and its spec.
3751 -- Note that this is not pure expansion as Expand_Subprogram_Contract
3752 -- prepares the contract assertions for generic subprograms or for ASIS.
3753 -- Do not generate contract checks in SPARK mode.
3754
3755 if not GNATprove_Mode then
3756 Expand_Subprogram_Contract (N, Spec_Id, Body_Id);
3757 end if;
3758
21d27997
RD
3759 -- Analyze the declarations (this call will analyze the precondition
3760 -- Check pragmas we prepended to the list, as well as the declaration
3761 -- of the _Postconditions procedure).
3762
996ae0b0 3763 Analyze_Declarations (Declarations (N));
21d27997 3764
f3124d8f 3765 -- Verify that the SPARK_Mode of the body agrees with that of its spec
f90d14ac 3766
f1c7be38 3767 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
f90d14ac
AC
3768 if Present (SPARK_Pragma (Spec_Id)) then
3769 if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off
3770 and then
3771 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
3772 then
3773 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3774 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3775 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3776 Error_Msg_NE
f3124d8f 3777 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
f90d14ac
AC
3778 end if;
3779
3780 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3781 null;
3782
3783 else
3784 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3ccedacc 3785 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
f90d14ac 3786 Error_Msg_Sloc := Sloc (Spec_Id);
f3124d8f
HK
3787 Error_Msg_NE
3788 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
f90d14ac
AC
3789 end if;
3790 end if;
3791
ac43e11e
AC
3792 -- If SPARK_Mode for body is not On, disable frontend inlining for this
3793 -- subprogram in GNATprove mode, as its body should not be analyzed.
3794
3795 if SPARK_Mode /= On
3796 and then GNATprove_Mode
ac43e11e
AC
3797 and then Present (Spec_Id)
3798 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3799 then
3800 Set_Body_To_Inline (Parent (Parent (Spec_Id)), Empty);
4bd4bb7f 3801 Set_Is_Inlined_Always (Spec_Id, False);
ac43e11e
AC
3802 end if;
3803
21d27997
RD
3804 -- Check completion, and analyze the statements
3805
996ae0b0 3806 Check_Completion;
33931112 3807 Inspect_Deferred_Constant_Completion (Declarations (N));
996ae0b0 3808 Analyze (HSS);
21d27997
RD
3809
3810 -- Deal with end of scope processing for the body
3811
07fc65c4 3812 Process_End_Label (HSS, 't', Current_Scope);
996ae0b0
RK
3813 End_Scope;
3814 Check_Subprogram_Order (N);
c37bb106 3815 Set_Analyzed (Body_Id);
996ae0b0
RK
3816
3817 -- If we have a separate spec, then the analysis of the declarations
3818 -- caused the entities in the body to be chained to the spec id, but
3819 -- we want them chained to the body id. Only the formal parameters
3820 -- end up chained to the spec id in this case.
3821
3822 if Present (Spec_Id) then
3823
d39d6bb8 3824 -- We must conform to the categorization of our spec
996ae0b0 3825
d39d6bb8 3826 Validate_Categorization_Dependency (N, Spec_Id);
996ae0b0 3827
d39d6bb8
RD
3828 -- And if this is a child unit, the parent units must conform
3829
3830 if Is_Child_Unit (Spec_Id) then
996ae0b0
RK
3831 Validate_Categorization_Dependency
3832 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3833 end if;
3834
21d27997
RD
3835 -- Here is where we move entities from the spec to the body
3836
3837 -- Case where there are entities that stay with the spec
3838
3839 if Present (Last_Real_Spec_Entity) then
3840
dbe36d67
AC
3841 -- No body entities (happens when the only real spec entities come
3842 -- from precondition and postcondition pragmas).
21d27997
RD
3843
3844 if No (Last_Entity (Body_Id)) then
3845 Set_First_Entity
3846 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3847
3848 -- Body entities present (formals), so chain stuff past them
3849
3850 else
3851 Set_Next_Entity
3852 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3853 end if;
3854
3855 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
996ae0b0 3856 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
21d27997
RD
3857 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3858
dbe36d67
AC
3859 -- Case where there are no spec entities, in this case there can be
3860 -- no body entities either, so just move everything.
996ae0b0
RK
3861
3862 else
21d27997 3863 pragma Assert (No (Last_Entity (Body_Id)));
996ae0b0
RK
3864 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3865 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3866 Set_First_Entity (Spec_Id, Empty);
3867 Set_Last_Entity (Spec_Id, Empty);
3868 end if;
3869 end if;
3870
7665e4bd 3871 Check_Missing_Return;
996ae0b0 3872
82c80734 3873 -- Now we are going to check for variables that are never modified in
76a69663
ES
3874 -- the body of the procedure. But first we deal with a special case
3875 -- where we want to modify this check. If the body of the subprogram
3876 -- starts with a raise statement or its equivalent, or if the body
cf3e6845
AC
3877 -- consists entirely of a null statement, then it is pretty obvious that
3878 -- it is OK to not reference the parameters. For example, this might be
3879 -- the following common idiom for a stubbed function: statement of the
3880 -- procedure raises an exception. In particular this deals with the
3881 -- common idiom of a stubbed function, which appears something like:
fbf5a39b
AC
3882
3883 -- function F (A : Integer) return Some_Type;
3884 -- X : Some_Type;
3885 -- begin
3886 -- raise Program_Error;
3887 -- return X;
3888 -- end F;
3889
76a69663
ES
3890 -- Here the purpose of X is simply to satisfy the annoying requirement
3891 -- in Ada that there be at least one return, and we certainly do not
a90bd866 3892 -- want to go posting warnings on X that it is not initialized. On
76a69663
ES
3893 -- the other hand, if X is entirely unreferenced that should still
3894 -- get a warning.
3895
3896 -- What we do is to detect these cases, and if we find them, flag the
3897 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3898 -- suppress unwanted warnings. For the case of the function stub above
3899 -- we have a special test to set X as apparently assigned to suppress
3900 -- the warning.
996ae0b0
RK
3901
3902 declare
800621e0 3903 Stm : Node_Id;
996ae0b0
RK
3904
3905 begin
0a36105d
JM
3906 -- Skip initial labels (for one thing this occurs when we are in
3907 -- front end ZCX mode, but in any case it is irrelevant), and also
3908 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
fbf5a39b 3909
800621e0 3910 Stm := First (Statements (HSS));
0a36105d
JM
3911 while Nkind (Stm) = N_Label
3912 or else Nkind (Stm) in N_Push_xxx_Label
3913 loop
996ae0b0 3914 Next (Stm);
0a36105d 3915 end loop;
996ae0b0 3916
fbf5a39b
AC
3917 -- Do the test on the original statement before expansion
3918
3919 declare
3920 Ostm : constant Node_Id := Original_Node (Stm);
3921
3922 begin
76a69663 3923 -- If explicit raise statement, turn on flag
fbf5a39b
AC
3924
3925 if Nkind (Ostm) = N_Raise_Statement then
76a69663
ES
3926 Set_Trivial_Subprogram (Stm);
3927
f3d57416 3928 -- If null statement, and no following statements, turn on flag
76a69663
ES
3929
3930 elsif Nkind (Stm) = N_Null_Statement
3931 and then Comes_From_Source (Stm)
3932 and then No (Next (Stm))
3933 then
3934 Set_Trivial_Subprogram (Stm);
fbf5a39b
AC
3935
3936 -- Check for explicit call cases which likely raise an exception
3937
3938 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3939 if Is_Entity_Name (Name (Ostm)) then
3940 declare
3941 Ent : constant Entity_Id := Entity (Name (Ostm));
3942
3943 begin
3944 -- If the procedure is marked No_Return, then likely it
3945 -- raises an exception, but in any case it is not coming
76a69663 3946 -- back here, so turn on the flag.
fbf5a39b 3947
f46faa08
AC
3948 if Present (Ent)
3949 and then Ekind (Ent) = E_Procedure
fbf5a39b
AC
3950 and then No_Return (Ent)
3951 then
76a69663 3952 Set_Trivial_Subprogram (Stm);
fbf5a39b
AC
3953 end if;
3954 end;
3955 end if;
3956 end if;
3957 end;
996ae0b0
RK
3958 end;
3959
3960 -- Check for variables that are never modified
3961
3962 declare
3963 E1, E2 : Entity_Id;
3964
3965 begin
fbf5a39b 3966 -- If there is a separate spec, then transfer Never_Set_In_Source
996ae0b0
RK
3967 -- flags from out parameters to the corresponding entities in the
3968 -- body. The reason we do that is we want to post error flags on
3969 -- the body entities, not the spec entities.
3970
3971 if Present (Spec_Id) then
3972 E1 := First_Entity (Spec_Id);
996ae0b0
RK
3973 while Present (E1) loop
3974 if Ekind (E1) = E_Out_Parameter then
3975 E2 := First_Entity (Body_Id);
fbf5a39b 3976 while Present (E2) loop
996ae0b0
RK
3977 exit when Chars (E1) = Chars (E2);
3978 Next_Entity (E2);
3979 end loop;
3980
fbf5a39b
AC
3981 if Present (E2) then
3982 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
3983 end if;
996ae0b0
RK
3984 end if;
3985
3986 Next_Entity (E1);
3987 end loop;
3988 end if;
3989
2aca76d6 3990 -- Check references in body
0868e09c 3991
2aca76d6 3992 Check_References (Body_Id);
996ae0b0 3993 end;
b1b543d2 3994 end Analyze_Subprogram_Body_Helper;
996ae0b0 3995
5afe5d2d
HK
3996 ---------------------------------
3997 -- Analyze_Subprogram_Contract --
3998 ---------------------------------
3999
4000 procedure Analyze_Subprogram_Contract (Subp : Entity_Id) is
36eef04a
AC
4001 Items : constant Node_Id := Contract (Subp);
4002 Case_Prag : Node_Id := Empty;
4003 Depends : Node_Id := Empty;
4004 Global : Node_Id := Empty;
c61ef416 4005 Mode : SPARK_Mode_Type;
36eef04a
AC
4006 Nam : Name_Id;
4007 Post_Prag : Node_Id := Empty;
4008 Prag : Node_Id;
4009 Seen_In_Case : Boolean := False;
4010 Seen_In_Post : Boolean := False;
5afe5d2d
HK
4011
4012 begin
fc999c5d
RD
4013 -- Due to the timing of contract analysis, delayed pragmas may be
4014 -- subject to the wrong SPARK_Mode, usually that of the enclosing
4015 -- context. To remedy this, restore the original SPARK_Mode of the
4016 -- related subprogram body.
4017
c61ef416
AC
4018 Save_SPARK_Mode_And_Set (Subp, Mode);
4019
5afe5d2d
HK
4020 if Present (Items) then
4021
4022 -- Analyze pre- and postconditions
4023
4024 Prag := Pre_Post_Conditions (Items);
4025 while Present (Prag) loop
ea3c0651 4026 Analyze_Pre_Post_Condition_In_Decl_Part (Prag, Subp);
5afe5d2d
HK
4027
4028 -- Verify whether a postcondition mentions attribute 'Result and
4029 -- its expression introduces a post-state.
4030
4031 if Warn_On_Suspicious_Contract
4032 and then Pragma_Name (Prag) = Name_Postcondition
4033 then
36eef04a
AC
4034 Post_Prag := Prag;
4035 Check_Result_And_Post_State (Prag, Seen_In_Post);
5afe5d2d
HK
4036 end if;
4037
4038 Prag := Next_Pragma (Prag);
4039 end loop;
4040
4041 -- Analyze contract-cases and test-cases
4042
4043 Prag := Contract_Test_Cases (Items);
4044 while Present (Prag) loop
ea3c0651
AC
4045 Nam := Pragma_Name (Prag);
4046
4047 if Nam = Name_Contract_Cases then
5afe5d2d
HK
4048 Analyze_Contract_Cases_In_Decl_Part (Prag);
4049
4050 -- Verify whether contract-cases mention attribute 'Result and
4051 -- its expression introduces a post-state. Perform the check
4052 -- only when the pragma is legal.
4053
4054 if Warn_On_Suspicious_Contract
4055 and then not Error_Posted (Prag)
4056 then
36eef04a
AC
4057 Case_Prag := Prag;
4058 Check_Result_And_Post_State (Prag, Seen_In_Case);
5afe5d2d
HK
4059 end if;
4060
4061 else
ea3c0651 4062 pragma Assert (Nam = Name_Test_Case);
5afe5d2d
HK
4063 Analyze_Test_Case_In_Decl_Part (Prag, Subp);
4064 end if;
4065
4066 Prag := Next_Pragma (Prag);
4067 end loop;
4068
4069 -- Analyze classification pragmas
4070
6c3c671e 4071 Prag := Classifications (Items);
5afe5d2d 4072 while Present (Prag) loop
ea3c0651
AC
4073 Nam := Pragma_Name (Prag);
4074
4075 if Nam = Name_Depends then
54e28df2 4076 Depends := Prag;
039538bc
AC
4077
4078 elsif Nam = Name_Global then
54e28df2 4079 Global := Prag;
039538bc
AC
4080
4081 -- Note that pragma Extensions_Visible has already been analyzed
4082
5afe5d2d
HK
4083 end if;
4084
4085 Prag := Next_Pragma (Prag);
4086 end loop;
54e28df2
HK
4087
4088 -- Analyze Global first as Depends may mention items classified in
4089 -- the global categorization.
4090
4091 if Present (Global) then
4092 Analyze_Global_In_Decl_Part (Global);
4093 end if;
4094
4095 -- Depends must be analyzed after Global in order to see the modes of
4096 -- all global items.
4097
4098 if Present (Depends) then
4099 Analyze_Depends_In_Decl_Part (Depends);
4100 end if;
5afe5d2d
HK
4101 end if;
4102
36eef04a 4103 -- Emit an error when neither the postconditions nor the contract-cases
5afe5d2d
HK
4104 -- mention attribute 'Result in the context of a function.
4105
4106 if Warn_On_Suspicious_Contract
4107 and then Ekind_In (Subp, E_Function, E_Generic_Function)
5afe5d2d 4108 then
36eef04a
AC
4109 if Present (Case_Prag)
4110 and then not Seen_In_Case
4111 and then Present (Post_Prag)
4112 and then not Seen_In_Post
4113 then
5afe5d2d
HK
4114 Error_Msg_N
4115 ("neither function postcondition nor contract cases mention "
36eef04a 4116 & "result?T?", Post_Prag);
5afe5d2d 4117
36eef04a 4118 elsif Present (Case_Prag) and then not Seen_In_Case then
5afe5d2d 4119 Error_Msg_N
36eef04a 4120 ("contract cases do not mention result?T?", Case_Prag);
5afe5d2d 4121
f660fba6
AC
4122 -- OK if we have at least one IN OUT parameter
4123
36eef04a 4124 elsif Present (Post_Prag) and then not Seen_In_Post then
f660fba6
AC
4125 declare
4126 F : Entity_Id;
4127 begin
4128 F := First_Formal (Subp);
4129 while Present (F) loop
4130 if Ekind (F) = E_In_Out_Parameter then
4131 return;
4132 else
4133 Next_Formal (F);
4134 end if;
4135 end loop;
4136 end;
4137
4138 -- If no in-out parameters and no mention of Result, the contract
4139 -- is certainly suspicious.
4140
5afe5d2d 4141 Error_Msg_N
36eef04a 4142 ("function postcondition does not mention result?T?", Post_Prag);
5afe5d2d
HK
4143 end if;
4144 end if;
c61ef416 4145
fc999c5d
RD
4146 -- Restore the SPARK_Mode of the enclosing context after all delayed
4147 -- pragmas have been analyzed.
4148
c61ef416 4149 Restore_SPARK_Mode (Mode);
5afe5d2d
HK
4150 end Analyze_Subprogram_Contract;
4151
996ae0b0
RK
4152 ------------------------------------
4153 -- Analyze_Subprogram_Declaration --
4154 ------------------------------------
4155
4156 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
579847c2 4157 Scop : constant Entity_Id := Current_Scope;
5d5832bc 4158 Designator : Entity_Id;
579847c2 4159
4d8f3296
ES
4160 Is_Completion : Boolean;
4161 -- Indicates whether a null procedure declaration is a completion
996ae0b0
RK
4162
4163 begin
2ba431e5 4164 -- Null procedures are not allowed in SPARK
daec8eeb 4165
fe5d3068 4166 if Nkind (Specification (N)) = N_Procedure_Specification
daec8eeb
YM
4167 and then Null_Present (Specification (N))
4168 then
ce5ba43a 4169 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
718deaf1 4170
4d8f3296
ES
4171 if Is_Protected_Type (Current_Scope) then
4172 Error_Msg_N ("protected operation cannot be a null procedure", N);
4173 end if;
718deaf1 4174
4d8f3296 4175 Analyze_Null_Procedure (N, Is_Completion);
718deaf1 4176
4d8f3296 4177 if Is_Completion then
718deaf1 4178
4d8f3296 4179 -- The null procedure acts as a body, nothing further is needed.
5d5832bc 4180
4d8f3296 4181 return;
5d5832bc
AC
4182 end if;
4183 end if;
4184
beacce02 4185 Designator := Analyze_Subprogram_Specification (Specification (N));
31af8899
AC
4186
4187 -- A reference may already have been generated for the unit name, in
4188 -- which case the following call is redundant. However it is needed for
4189 -- declarations that are the rewriting of an expression function.
4190
5d5832bc
AC
4191 Generate_Definition (Designator);
4192
f90d14ac
AC
4193 -- Set SPARK mode from current context (may be overwritten later with
4194 -- explicit pragma).
4a854847 4195
f90d14ac
AC
4196 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4197 Set_SPARK_Pragma_Inherited (Designator, True);
579847c2 4198
b1b543d2
BD
4199 if Debug_Flag_C then
4200 Write_Str ("==> subprogram spec ");
4201 Write_Name (Chars (Designator));
4202 Write_Str (" from ");
4203 Write_Location (Sloc (N));
4204 Write_Eol;
4205 Indent;
4206 end if;
4207
996ae0b0 4208 Validate_RCI_Subprogram_Declaration (N);
996ae0b0
RK
4209 New_Overloaded_Entity (Designator);
4210 Check_Delayed_Subprogram (Designator);
fbf5a39b 4211
cf3e6845
AC
4212 -- If the type of the first formal of the current subprogram is a non-
4213 -- generic tagged private type, mark the subprogram as being a private
4214 -- primitive. Ditto if this is a function with controlling result, and
4215 -- the return type is currently private. In both cases, the type of the
4216 -- controlling argument or result must be in the current scope for the
4217 -- operation to be primitive.
6ca063eb
AC
4218
4219 if Has_Controlling_Result (Designator)
4220 and then Is_Private_Type (Etype (Designator))
b7d5e87b 4221 and then Scope (Etype (Designator)) = Current_Scope
6ca063eb
AC
4222 and then not Is_Generic_Actual_Type (Etype (Designator))
4223 then
4224 Set_Is_Private_Primitive (Designator);
d44202ba 4225
6ca063eb 4226 elsif Present (First_Formal (Designator)) then
d44202ba
HK
4227 declare
4228 Formal_Typ : constant Entity_Id :=
4229 Etype (First_Formal (Designator));
4230 begin
4231 Set_Is_Private_Primitive (Designator,
4232 Is_Tagged_Type (Formal_Typ)
b7d5e87b 4233 and then Scope (Formal_Typ) = Current_Scope
d44202ba
HK
4234 and then Is_Private_Type (Formal_Typ)
4235 and then not Is_Generic_Actual_Type (Formal_Typ));
4236 end;
4237 end if;
4238
ec4867fa
ES
4239 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4240 -- or null.
4241
0791fbe9 4242 if Ada_Version >= Ada_2005
ec4867fa
ES
4243 and then Comes_From_Source (N)
4244 and then Is_Dispatching_Operation (Designator)
4245 then
4246 declare
4247 E : Entity_Id;
4248 Etyp : Entity_Id;
4249
4250 begin
4251 if Has_Controlling_Result (Designator) then
4252 Etyp := Etype (Designator);
4253
4254 else
4255 E := First_Entity (Designator);
4256 while Present (E)
4257 and then Is_Formal (E)
4258 and then not Is_Controlling_Formal (E)
4259 loop
4260 Next_Entity (E);
4261 end loop;
4262
4263 Etyp := Etype (E);
4264 end if;
4265
4266 if Is_Access_Type (Etyp) then
4267 Etyp := Directly_Designated_Type (Etyp);
4268 end if;
4269
4270 if Is_Interface (Etyp)
f937473f 4271 and then not Is_Abstract_Subprogram (Designator)
ec4867fa 4272 and then not (Ekind (Designator) = E_Procedure
8fde064e 4273 and then Null_Present (Specification (N)))
ec4867fa
ES
4274 then
4275 Error_Msg_Name_1 := Chars (Defining_Entity (N));
033eaf85
AC
4276
4277 -- Specialize error message based on procedures vs. functions,
4278 -- since functions can't be null subprograms.
4279
4280 if Ekind (Designator) = E_Procedure then
4281 Error_Msg_N
4282 ("interface procedure % must be abstract or null", N);
4283 else
3f80a182
AC
4284 Error_Msg_N
4285 ("interface function % must be abstract", N);
033eaf85 4286 end if;
ec4867fa
ES
4287 end if;
4288 end;
4289 end if;
4290
fbf5a39b
AC
4291 -- What is the following code for, it used to be
4292
4293 -- ??? Set_Suppress_Elaboration_Checks
4294 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4295
4296 -- The following seems equivalent, but a bit dubious
4297
4298 if Elaboration_Checks_Suppressed (Designator) then
4299 Set_Kill_Elaboration_Checks (Designator);
4300 end if;
996ae0b0 4301
8fde064e 4302 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
fbf5a39b 4303 Set_Categorization_From_Scope (Designator, Scop);
8fde064e 4304
996ae0b0 4305 else
e895b435 4306 -- For a compilation unit, check for library-unit pragmas
996ae0b0 4307
0a36105d 4308 Push_Scope (Designator);
996ae0b0
RK
4309 Set_Categorization_From_Pragmas (N);
4310 Validate_Categorization_Dependency (N, Designator);
4311 Pop_Scope;
4312 end if;
4313
4314 -- For a compilation unit, set body required. This flag will only be
4315 -- reset if a valid Import or Interface pragma is processed later on.
4316
4317 if Nkind (Parent (N)) = N_Compilation_Unit then
4318 Set_Body_Required (Parent (N), True);
758c442c 4319
0791fbe9 4320 if Ada_Version >= Ada_2005
758c442c
GD
4321 and then Nkind (Specification (N)) = N_Procedure_Specification
4322 and then Null_Present (Specification (N))
4323 then
4324 Error_Msg_N
4325 ("null procedure cannot be declared at library level", N);
4326 end if;
996ae0b0
RK
4327 end if;
4328
fbf5a39b 4329 Generate_Reference_To_Formals (Designator);
996ae0b0 4330 Check_Eliminated (Designator);
fbf5a39b 4331
b1b543d2
BD
4332 if Debug_Flag_C then
4333 Outdent;
4334 Write_Str ("<== subprogram spec ");
4335 Write_Name (Chars (Designator));
4336 Write_Str (" from ");
4337 Write_Location (Sloc (N));
4338 Write_Eol;
4339 end if;
0f1a6a0b 4340
1a265e78
AC
4341 if Is_Protected_Type (Current_Scope) then
4342
4343 -- Indicate that this is a protected operation, because it may be
4344 -- used in subsequent declarations within the protected type.
4345
4346 Set_Convention (Designator, Convention_Protected);
4347 end if;
4348
beacce02 4349 List_Inherited_Pre_Post_Aspects (Designator);
eaba57fb
RD
4350
4351 if Has_Aspects (N) then
4352 Analyze_Aspect_Specifications (N, Designator);
4353 end if;
996ae0b0
RK
4354 end Analyze_Subprogram_Declaration;
4355
fbf5a39b
AC
4356 --------------------------------------
4357 -- Analyze_Subprogram_Specification --
4358 --------------------------------------
4359
4360 -- Reminder: N here really is a subprogram specification (not a subprogram
4361 -- declaration). This procedure is called to analyze the specification in
4362 -- both subprogram bodies and subprogram declarations (specs).
4363
4364 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4365 Designator : constant Entity_Id := Defining_Entity (N);
21d27997 4366 Formals : constant List_Id := Parameter_Specifications (N);
fbf5a39b 4367
758c442c
GD
4368 -- Start of processing for Analyze_Subprogram_Specification
4369
fbf5a39b 4370 begin
2ba431e5 4371 -- User-defined operator is not allowed in SPARK, except as a renaming
38171f43 4372
db72f10a
AC
4373 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4374 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4375 then
ce5ba43a
AC
4376 Check_SPARK_05_Restriction
4377 ("user-defined operator is not allowed", N);
38171f43
AC
4378 end if;
4379
31af8899
AC
4380 -- Proceed with analysis. Do not emit a cross-reference entry if the
4381 -- specification comes from an expression function, because it may be
4382 -- the completion of a previous declaration. It is is not, the cross-
4383 -- reference entry will be emitted for the new subprogram declaration.
4384
4385 if Nkind (Parent (N)) /= N_Expression_Function then
4386 Generate_Definition (Designator);
4387 end if;
38171f43 4388
dac3bede 4389 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
fbf5a39b
AC
4390
4391 if Nkind (N) = N_Function_Specification then
4392 Set_Ekind (Designator, E_Function);
4393 Set_Mechanism (Designator, Default_Mechanism);
fbf5a39b
AC
4394 else
4395 Set_Ekind (Designator, E_Procedure);
4396 Set_Etype (Designator, Standard_Void_Type);
4397 end if;
4398
4bd4bb7f
AC
4399 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4400 -- those subprograms which could be inlined in GNATprove mode (because
4401 -- Body_To_Inline is non-Empty) but cannot be inlined.
4402
4403 if GNATprove_Mode then
4404 Set_Is_Inlined_Always (Designator);
4405 end if;
4406
800621e0 4407 -- Introduce new scope for analysis of the formals and the return type
82c80734
RD
4408
4409 Set_Scope (Designator, Current_Scope);
4410
fbf5a39b 4411 if Present (Formals) then
0a36105d 4412 Push_Scope (Designator);
fbf5a39b 4413 Process_Formals (Formals, N);
758c442c 4414
0929eaeb
AC
4415 -- Check dimensions in N for formals with default expression
4416
4417 Analyze_Dimension_Formals (N, Formals);
4418
a38ff9b1
ES
4419 -- Ada 2005 (AI-345): If this is an overriding operation of an
4420 -- inherited interface operation, and the controlling type is
4421 -- a synchronized type, replace the type with its corresponding
4422 -- record, to match the proper signature of an overriding operation.
69cb258c
AC
4423 -- Same processing for an access parameter whose designated type is
4424 -- derived from a synchronized interface.
758c442c 4425
0791fbe9 4426 if Ada_Version >= Ada_2005 then
d44202ba
HK
4427 declare
4428 Formal : Entity_Id;
4429 Formal_Typ : Entity_Id;
4430 Rec_Typ : Entity_Id;
69cb258c 4431 Desig_Typ : Entity_Id;
0a36105d 4432
d44202ba
HK
4433 begin
4434 Formal := First_Formal (Designator);
4435 while Present (Formal) loop
4436 Formal_Typ := Etype (Formal);
0a36105d 4437
d44202ba
HK
4438 if Is_Concurrent_Type (Formal_Typ)
4439 and then Present (Corresponding_Record_Type (Formal_Typ))
4440 then
4441 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4442
4443 if Present (Interfaces (Rec_Typ)) then
4444 Set_Etype (Formal, Rec_Typ);
4445 end if;
69cb258c
AC
4446
4447 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4448 Desig_Typ := Designated_Type (Formal_Typ);
4449
4450 if Is_Concurrent_Type (Desig_Typ)
4451 and then Present (Corresponding_Record_Type (Desig_Typ))
4452 then
4453 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4454
4455 if Present (Interfaces (Rec_Typ)) then
4456 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4457 end if;
4458 end if;
d44202ba
HK
4459 end if;
4460
4461 Next_Formal (Formal);
4462 end loop;
4463 end;
758c442c
GD
4464 end if;
4465
fbf5a39b 4466 End_Scope;
82c80734 4467
b66c3ff4
AC
4468 -- The subprogram scope is pushed and popped around the processing of
4469 -- the return type for consistency with call above to Process_Formals
4470 -- (which itself can call Analyze_Return_Type), and to ensure that any
4471 -- itype created for the return type will be associated with the proper
4472 -- scope.
4473
82c80734 4474 elsif Nkind (N) = N_Function_Specification then
b66c3ff4 4475 Push_Scope (Designator);
82c80734 4476 Analyze_Return_Type (N);
b66c3ff4 4477 End_Scope;
fbf5a39b
AC
4478 end if;
4479
e606088a
AC
4480 -- Function case
4481
fbf5a39b 4482 if Nkind (N) = N_Function_Specification then
e606088a
AC
4483
4484 -- Deal with operator symbol case
4485
fbf5a39b
AC
4486 if Nkind (Designator) = N_Defining_Operator_Symbol then
4487 Valid_Operator_Definition (Designator);
4488 end if;
4489
4490 May_Need_Actuals (Designator);
4491
fe63b1b1
ES
4492 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4493 -- the subprogram is abstract also. This does not apply to renaming
1adaea16
AC
4494 -- declarations, where abstractness is inherited, and to subprogram
4495 -- bodies generated for stream operations, which become renamings as
4496 -- bodies.
2bfb1b72 4497
fe63b1b1
ES
4498 -- In case of primitives associated with abstract interface types
4499 -- the check is applied later (see Analyze_Subprogram_Declaration).
ec4867fa 4500
1adaea16 4501 if not Nkind_In (Original_Node (Parent (N)),
3f80a182
AC
4502 N_Subprogram_Renaming_Declaration,
4503 N_Abstract_Subprogram_Declaration,
4504 N_Formal_Abstract_Subprogram_Declaration)
fbf5a39b 4505 then
2e79de51
AC
4506 if Is_Abstract_Type (Etype (Designator))
4507 and then not Is_Interface (Etype (Designator))
4508 then
4509 Error_Msg_N
4510 ("function that returns abstract type must be abstract", N);
4511
e606088a 4512 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
2e79de51
AC
4513 -- access result whose designated type is abstract.
4514
4515 elsif Nkind (Result_Definition (N)) = N_Access_Definition
4516 and then
4517 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4518 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
dbe945f1 4519 and then Ada_Version >= Ada_2012
2e79de51
AC
4520 then
4521 Error_Msg_N ("function whose access result designates "
3f80a182 4522 & "abstract type must be abstract", N);
2e79de51 4523 end if;
fbf5a39b
AC
4524 end if;
4525 end if;
4526
4527 return Designator;
4528 end Analyze_Subprogram_Specification;
4529
996ae0b0
RK
4530 -----------------------
4531 -- Check_Conformance --
4532 -----------------------
4533
4534 procedure Check_Conformance
41251c60
JM
4535 (New_Id : Entity_Id;
4536 Old_Id : Entity_Id;
4537 Ctype : Conformance_Type;
4538 Errmsg : Boolean;
4539 Conforms : out Boolean;
4540 Err_Loc : Node_Id := Empty;
4541 Get_Inst : Boolean := False;
4542 Skip_Controlling_Formals : Boolean := False)
996ae0b0 4543 is
996ae0b0 4544 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
c27f2f15
RD
4545 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4546 -- If Errmsg is True, then processing continues to post an error message
4547 -- for conformance error on given node. Two messages are output. The
4548 -- first message points to the previous declaration with a general "no
4549 -- conformance" message. The second is the detailed reason, supplied as
4550 -- Msg. The parameter N provide information for a possible & insertion
4551 -- in the message, and also provides the location for posting the
4552 -- message in the absence of a specified Err_Loc location.
996ae0b0
RK
4553
4554 -----------------------
4555 -- Conformance_Error --
4556 -----------------------
4557
4558 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4559 Enode : Node_Id;
4560
4561 begin
4562 Conforms := False;
4563
4564 if Errmsg then
4565 if No (Err_Loc) then
4566 Enode := N;
4567 else
4568 Enode := Err_Loc;
4569 end if;
4570
4571 Error_Msg_Sloc := Sloc (Old_Id);
4572
4573 case Ctype is
4574 when Type_Conformant =>
483c78cb 4575 Error_Msg_N -- CODEFIX
996ae0b0
RK
4576 ("not type conformant with declaration#!", Enode);
4577
4578 when Mode_Conformant =>
19590d70 4579 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
ed2233dc 4580 Error_Msg_N
19590d70
GD
4581 ("not mode conformant with operation inherited#!",
4582 Enode);
4583 else
ed2233dc 4584 Error_Msg_N
19590d70
GD
4585 ("not mode conformant with declaration#!", Enode);
4586 end if;
996ae0b0
RK
4587
4588 when Subtype_Conformant =>
19590d70 4589 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
ed2233dc 4590 Error_Msg_N
19590d70
GD
4591 ("not subtype conformant with operation inherited#!",
4592 Enode);
4593 else
ed2233dc 4594 Error_Msg_N
19590d70
GD
4595 ("not subtype conformant with declaration#!", Enode);
4596 end if;
996ae0b0
RK
4597
4598 when Fully_Conformant =>
19590d70 4599 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
483c78cb 4600 Error_Msg_N -- CODEFIX
19590d70
GD
4601 ("not fully conformant with operation inherited#!",
4602 Enode);
4603 else
483c78cb 4604 Error_Msg_N -- CODEFIX
19590d70
GD
4605 ("not fully conformant with declaration#!", Enode);
4606 end if;
996ae0b0
RK
4607 end case;
4608
4609 Error_Msg_NE (Msg, Enode, N);
4610 end if;
4611 end Conformance_Error;
4612
ec4867fa
ES
4613 -- Local Variables
4614
4615 Old_Type : constant Entity_Id := Etype (Old_Id);
4616 New_Type : constant Entity_Id := Etype (New_Id);
4617 Old_Formal : Entity_Id;
4618 New_Formal : Entity_Id;
4619 Access_Types_Match : Boolean;
4620 Old_Formal_Base : Entity_Id;
4621 New_Formal_Base : Entity_Id;
4622
996ae0b0
RK
4623 -- Start of processing for Check_Conformance
4624
4625 begin
4626 Conforms := True;
4627
82c80734
RD
4628 -- We need a special case for operators, since they don't appear
4629 -- explicitly.
996ae0b0
RK
4630
4631 if Ctype = Type_Conformant then
4632 if Ekind (New_Id) = E_Operator
4633 and then Operator_Matches_Spec (New_Id, Old_Id)
4634 then
4635 return;
4636 end if;
4637 end if;
4638
4639 -- If both are functions/operators, check return types conform
4640
4641 if Old_Type /= Standard_Void_Type
4642 and then New_Type /= Standard_Void_Type
4643 then
fceeaab6
ES
4644
4645 -- If we are checking interface conformance we omit controlling
4646 -- arguments and result, because we are only checking the conformance
4647 -- of the remaining parameters.
4648
4649 if Has_Controlling_Result (Old_Id)
4650 and then Has_Controlling_Result (New_Id)
4651 and then Skip_Controlling_Formals
4652 then
4653 null;
4654
4655 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
7f568bfa
AC
4656 if Ctype >= Subtype_Conformant
4657 and then not Predicates_Match (Old_Type, New_Type)
4658 then
4659 Conformance_Error
4660 ("\predicate of return type does not match!", New_Id);
4661 else
4662 Conformance_Error
4663 ("\return type does not match!", New_Id);
4664 end if;
4665
996ae0b0
RK
4666 return;
4667 end if;
4668
41251c60 4669 -- Ada 2005 (AI-231): In case of anonymous access types check the
0a36105d 4670 -- null-exclusion and access-to-constant attributes match.
41251c60 4671
0791fbe9 4672 if Ada_Version >= Ada_2005
41251c60
JM
4673 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
4674 and then
8fde064e
AC
4675 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
4676 or else Is_Access_Constant (Etype (Old_Type)) /=
4677 Is_Access_Constant (Etype (New_Type)))
41251c60 4678 then
5d37ba92 4679 Conformance_Error ("\return type does not match!", New_Id);
41251c60
JM
4680 return;
4681 end if;
4682
996ae0b0
RK
4683 -- If either is a function/operator and the other isn't, error
4684
4685 elsif Old_Type /= Standard_Void_Type
4686 or else New_Type /= Standard_Void_Type
4687 then
5d37ba92 4688 Conformance_Error ("\functions can only match functions!", New_Id);
996ae0b0
RK
4689 return;
4690 end if;
4691
0a36105d 4692 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
996ae0b0
RK
4693 -- If this is a renaming as body, refine error message to indicate that
4694 -- the conflict is with the original declaration. If the entity is not
4695 -- frozen, the conventions don't have to match, the one of the renamed
4696 -- entity is inherited.
4697
4698 if Ctype >= Subtype_Conformant then
996ae0b0 4699 if Convention (Old_Id) /= Convention (New_Id) then
996ae0b0
RK
4700 if not Is_Frozen (New_Id) then
4701 null;
4702
4703 elsif Present (Err_Loc)
4704 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
4705 and then Present (Corresponding_Spec (Err_Loc))
4706 then
4707 Error_Msg_Name_1 := Chars (New_Id);
4708 Error_Msg_Name_2 :=
4709 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5d37ba92 4710 Conformance_Error ("\prior declaration for% has convention %!");
996ae0b0
RK
4711
4712 else
5d37ba92 4713 Conformance_Error ("\calling conventions do not match!");
996ae0b0
RK
4714 end if;
4715
4716 return;
4717
4718 elsif Is_Formal_Subprogram (Old_Id)
4719 or else Is_Formal_Subprogram (New_Id)
4720 then
5d37ba92 4721 Conformance_Error ("\formal subprograms not allowed!");
996ae0b0
RK
4722 return;
4723 end if;
4724 end if;
4725
4726 -- Deal with parameters
4727
4728 -- Note: we use the entity information, rather than going directly
4729 -- to the specification in the tree. This is not only simpler, but
4730 -- absolutely necessary for some cases of conformance tests between
a90bd866 4731 -- operators, where the declaration tree simply does not exist.
996ae0b0
RK
4732
4733 Old_Formal := First_Formal (Old_Id);
4734 New_Formal := First_Formal (New_Id);
996ae0b0 4735 while Present (Old_Formal) and then Present (New_Formal) loop
41251c60
JM
4736 if Is_Controlling_Formal (Old_Formal)
4737 and then Is_Controlling_Formal (New_Formal)
4738 and then Skip_Controlling_Formals
4739 then
a2dc5812
AC
4740 -- The controlling formals will have different types when
4741 -- comparing an interface operation with its match, but both
4742 -- or neither must be access parameters.
4743
4744 if Is_Access_Type (Etype (Old_Formal))
4745 =
4746 Is_Access_Type (Etype (New_Formal))
4747 then
4748 goto Skip_Controlling_Formal;
4749 else
4750 Conformance_Error
4751 ("\access parameter does not match!", New_Formal);
4752 end if;
41251c60
JM
4753 end if;
4754
21791d97 4755 -- Ada 2012: Mode conformance also requires that formal parameters
2a290fec
AC
4756 -- be both aliased, or neither.
4757
21791d97 4758 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
2a290fec
AC
4759 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
4760 Conformance_Error
4761 ("\aliased parameter mismatch!", New_Formal);
4762 end if;
4763 end if;
4764
fbf5a39b
AC
4765 if Ctype = Fully_Conformant then
4766
4767 -- Names must match. Error message is more accurate if we do
4768 -- this before checking that the types of the formals match.
4769
4770 if Chars (Old_Formal) /= Chars (New_Formal) then
3ccedacc 4771 Conformance_Error ("\name& does not match!", New_Formal);
fbf5a39b
AC
4772
4773 -- Set error posted flag on new formal as well to stop
4774 -- junk cascaded messages in some cases.
4775
4776 Set_Error_Posted (New_Formal);
4777 return;
4778 end if;
40b93859
RD
4779
4780 -- Null exclusion must match
4781
4782 if Null_Exclusion_Present (Parent (Old_Formal))
4783 /=
4784 Null_Exclusion_Present (Parent (New_Formal))
4785 then
4786 -- Only give error if both come from source. This should be
4787 -- investigated some time, since it should not be needed ???
4788
4789 if Comes_From_Source (Old_Formal)
4790 and then
4791 Comes_From_Source (New_Formal)
4792 then
4793 Conformance_Error
3ccedacc 4794 ("\null exclusion for& does not match", New_Formal);
40b93859
RD
4795
4796 -- Mark error posted on the new formal to avoid duplicated
4797 -- complaint about types not matching.
4798
4799 Set_Error_Posted (New_Formal);
4800 end if;
4801 end if;
fbf5a39b 4802 end if;
996ae0b0 4803
ec4867fa
ES
4804 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
4805 -- case occurs whenever a subprogram is being renamed and one of its
4806 -- parameters imposes a null exclusion. For example:
4807
4808 -- type T is null record;
4809 -- type Acc_T is access T;
4810 -- subtype Acc_T_Sub is Acc_T;
4811
4812 -- procedure P (Obj : not null Acc_T_Sub); -- itype
4813 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
4814 -- renames P;
4815
4816 Old_Formal_Base := Etype (Old_Formal);
4817 New_Formal_Base := Etype (New_Formal);
4818
4819 if Get_Inst then
4820 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
4821 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
4822 end if;
4823
0791fbe9 4824 Access_Types_Match := Ada_Version >= Ada_2005
ec4867fa 4825
8fde064e
AC
4826 -- Ensure that this rule is only applied when New_Id is a
4827 -- renaming of Old_Id.
ec4867fa 4828
5d37ba92
ES
4829 and then Nkind (Parent (Parent (New_Id))) =
4830 N_Subprogram_Renaming_Declaration
ec4867fa
ES
4831 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
4832 and then Present (Entity (Name (Parent (Parent (New_Id)))))
4833 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
4834
8fde064e 4835 -- Now handle the allowed access-type case
ec4867fa
ES
4836
4837 and then Is_Access_Type (Old_Formal_Base)
4838 and then Is_Access_Type (New_Formal_Base)
5d37ba92 4839
8fde064e
AC
4840 -- The type kinds must match. The only exception occurs with
4841 -- multiple generics of the form:
5d37ba92 4842
8fde064e
AC
4843 -- generic generic
4844 -- type F is private; type A is private;
4845 -- type F_Ptr is access F; type A_Ptr is access A;
4846 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
4847 -- package F_Pack is ... package A_Pack is
4848 -- package F_Inst is
4849 -- new F_Pack (A, A_Ptr, A_P);
5d37ba92 4850
8fde064e
AC
4851 -- When checking for conformance between the parameters of A_P
4852 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
4853 -- because the compiler has transformed A_Ptr into a subtype of
4854 -- F_Ptr. We catch this case in the code below.
5d37ba92
ES
4855
4856 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
4857 or else
4858 (Is_Generic_Type (Old_Formal_Base)
4859 and then Is_Generic_Type (New_Formal_Base)
4860 and then Is_Internal (New_Formal_Base)
4861 and then Etype (Etype (New_Formal_Base)) =
4862 Old_Formal_Base))
ec4867fa 4863 and then Directly_Designated_Type (Old_Formal_Base) =
8fde064e 4864 Directly_Designated_Type (New_Formal_Base)
ec4867fa
ES
4865 and then ((Is_Itype (Old_Formal_Base)
4866 and then Can_Never_Be_Null (Old_Formal_Base))
4867 or else
4868 (Is_Itype (New_Formal_Base)
4869 and then Can_Never_Be_Null (New_Formal_Base)));
4870
996ae0b0
RK
4871 -- Types must always match. In the visible part of an instance,
4872 -- usual overloading rules for dispatching operations apply, and
4873 -- we check base types (not the actual subtypes).
4874
4875 if In_Instance_Visible_Part
4876 and then Is_Dispatching_Operation (New_Id)
4877 then
4878 if not Conforming_Types
ec4867fa
ES
4879 (T1 => Base_Type (Etype (Old_Formal)),
4880 T2 => Base_Type (Etype (New_Formal)),
4881 Ctype => Ctype,
4882 Get_Inst => Get_Inst)
4883 and then not Access_Types_Match
996ae0b0 4884 then
5d37ba92 4885 Conformance_Error ("\type of & does not match!", New_Formal);
996ae0b0
RK
4886 return;
4887 end if;
4888
4889 elsif not Conforming_Types
5d37ba92
ES
4890 (T1 => Old_Formal_Base,
4891 T2 => New_Formal_Base,
ec4867fa
ES
4892 Ctype => Ctype,
4893 Get_Inst => Get_Inst)
4894 and then not Access_Types_Match
996ae0b0 4895 then
c27f2f15
RD
4896 -- Don't give error message if old type is Any_Type. This test
4897 -- avoids some cascaded errors, e.g. in case of a bad spec.
4898
4899 if Errmsg and then Old_Formal_Base = Any_Type then
4900 Conforms := False;
4901 else
7f568bfa
AC
4902 if Ctype >= Subtype_Conformant
4903 and then
4904 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
4905 then
4906 Conformance_Error
4907 ("\predicate of & does not match!", New_Formal);
4908 else
4909 Conformance_Error
4910 ("\type of & does not match!", New_Formal);
4911 end if;
c27f2f15
RD
4912 end if;
4913
996ae0b0
RK
4914 return;
4915 end if;
4916
4917 -- For mode conformance, mode must match
4918
5d37ba92
ES
4919 if Ctype >= Mode_Conformant then
4920 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
dd54644b
JM
4921 if not Ekind_In (New_Id, E_Function, E_Procedure)
4922 or else not Is_Primitive_Wrapper (New_Id)
4923 then
4924 Conformance_Error ("\mode of & does not match!", New_Formal);
c199ccf7 4925
dd54644b
JM
4926 else
4927 declare
c199ccf7 4928 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
dd54644b 4929 begin
3ccedacc 4930 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
dd54644b
JM
4931 then
4932 Error_Msg_PT (T, New_Id);
4933 else
4934 Conformance_Error
4935 ("\mode of & does not match!", New_Formal);
4936 end if;
4937 end;
4938 end if;
4939
5d37ba92
ES
4940 return;
4941
4942 -- Part of mode conformance for access types is having the same
4943 -- constant modifier.
4944
4945 elsif Access_Types_Match
4946 and then Is_Access_Constant (Old_Formal_Base) /=
4947 Is_Access_Constant (New_Formal_Base)
4948 then
4949 Conformance_Error
4950 ("\constant modifier does not match!", New_Formal);
4951 return;
4952 end if;
996ae0b0
RK
4953 end if;
4954
0a36105d 4955 if Ctype >= Subtype_Conformant then
996ae0b0 4956
0a36105d
JM
4957 -- Ada 2005 (AI-231): In case of anonymous access types check
4958 -- the null-exclusion and access-to-constant attributes must
c7b9d548
AC
4959 -- match. For null exclusion, we test the types rather than the
4960 -- formals themselves, since the attribute is only set reliably
4961 -- on the formals in the Ada 95 case, and we exclude the case
4962 -- where Old_Formal is marked as controlling, to avoid errors
4963 -- when matching completing bodies with dispatching declarations
4964 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
996ae0b0 4965
0791fbe9 4966 if Ada_Version >= Ada_2005
0a36105d
JM
4967 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
4968 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
4969 and then
c7b9d548
AC
4970 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
4971 Can_Never_Be_Null (Etype (New_Formal))
4972 and then
4973 not Is_Controlling_Formal (Old_Formal))
0a36105d
JM
4974 or else
4975 Is_Access_Constant (Etype (Old_Formal)) /=
4976 Is_Access_Constant (Etype (New_Formal)))
40b93859
RD
4977
4978 -- Do not complain if error already posted on New_Formal. This
4979 -- avoids some redundant error messages.
4980
4981 and then not Error_Posted (New_Formal)
0a36105d
JM
4982 then
4983 -- It is allowed to omit the null-exclusion in case of stream
4984 -- attribute subprograms. We recognize stream subprograms
4985 -- through their TSS-generated suffix.
996ae0b0 4986
0a36105d
JM
4987 declare
4988 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
3ada950b 4989
0a36105d
JM
4990 begin
4991 if TSS_Name /= TSS_Stream_Read
4992 and then TSS_Name /= TSS_Stream_Write
4993 and then TSS_Name /= TSS_Stream_Input
4994 and then TSS_Name /= TSS_Stream_Output
4995 then
3ada950b 4996 -- Here we have a definite conformance error. It is worth
71fb4dc8 4997 -- special casing the error message for the case of a
3ada950b
AC
4998 -- controlling formal (which excludes null).
4999
5000 if Is_Controlling_Formal (New_Formal) then
5001 Error_Msg_Node_2 := Scope (New_Formal);
5002 Conformance_Error
3ccedacc
AC
5003 ("\controlling formal & of & excludes null, "
5004 & "declaration must exclude null as well",
5005 New_Formal);
3ada950b
AC
5006
5007 -- Normal case (couldn't we give more detail here???)
5008
5009 else
5010 Conformance_Error
5011 ("\type of & does not match!", New_Formal);
5012 end if;
5013
0a36105d
JM
5014 return;
5015 end if;
5016 end;
5017 end if;
5018 end if;
41251c60 5019
0a36105d 5020 -- Full conformance checks
41251c60 5021
0a36105d 5022 if Ctype = Fully_Conformant then
e660dbf7 5023
0a36105d 5024 -- We have checked already that names match
e660dbf7 5025
0a36105d 5026 if Parameter_Mode (Old_Formal) = E_In_Parameter then
41251c60
JM
5027
5028 -- Check default expressions for in parameters
5029
996ae0b0
RK
5030 declare
5031 NewD : constant Boolean :=
5032 Present (Default_Value (New_Formal));
5033 OldD : constant Boolean :=
5034 Present (Default_Value (Old_Formal));
5035 begin
5036 if NewD or OldD then
5037
82c80734
RD
5038 -- The old default value has been analyzed because the
5039 -- current full declaration will have frozen everything
0a36105d
JM
5040 -- before. The new default value has not been analyzed,
5041 -- so analyze it now before we check for conformance.
996ae0b0
RK
5042
5043 if NewD then
0a36105d 5044 Push_Scope (New_Id);
21d27997 5045 Preanalyze_Spec_Expression
fbf5a39b 5046 (Default_Value (New_Formal), Etype (New_Formal));
996ae0b0
RK
5047 End_Scope;
5048 end if;
5049
5050 if not (NewD and OldD)
5051 or else not Fully_Conformant_Expressions
5052 (Default_Value (Old_Formal),
5053 Default_Value (New_Formal))
5054 then
5055 Conformance_Error
5d37ba92 5056 ("\default expression for & does not match!",
996ae0b0
RK
5057 New_Formal);
5058 return;
5059 end if;
5060 end if;
5061 end;
5062 end if;
5063 end if;
5064
5065 -- A couple of special checks for Ada 83 mode. These checks are
0a36105d 5066 -- skipped if either entity is an operator in package Standard,
996ae0b0
RK
5067 -- or if either old or new instance is not from the source program.
5068
0ab80019 5069 if Ada_Version = Ada_83
996ae0b0
RK
5070 and then Sloc (Old_Id) > Standard_Location
5071 and then Sloc (New_Id) > Standard_Location
5072 and then Comes_From_Source (Old_Id)
5073 and then Comes_From_Source (New_Id)
5074 then
5075 declare
5076 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5077 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5078
5079 begin
5080 -- Explicit IN must be present or absent in both cases. This
5081 -- test is required only in the full conformance case.
5082
5083 if In_Present (Old_Param) /= In_Present (New_Param)
5084 and then Ctype = Fully_Conformant
5085 then
5086 Conformance_Error
5d37ba92 5087 ("\(Ada 83) IN must appear in both declarations",
996ae0b0
RK
5088 New_Formal);
5089 return;
5090 end if;
5091
5092 -- Grouping (use of comma in param lists) must be the same
5093 -- This is where we catch a misconformance like:
5094
0a36105d 5095 -- A, B : Integer
996ae0b0
RK
5096 -- A : Integer; B : Integer
5097
5098 -- which are represented identically in the tree except
5099 -- for the setting of the flags More_Ids and Prev_Ids.
5100
5101 if More_Ids (Old_Param) /= More_Ids (New_Param)
5102 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5103 then
5104 Conformance_Error
5d37ba92 5105 ("\grouping of & does not match!", New_Formal);
996ae0b0
RK
5106 return;
5107 end if;
5108 end;
5109 end if;
5110
41251c60
JM
5111 -- This label is required when skipping controlling formals
5112
5113 <<Skip_Controlling_Formal>>
5114
996ae0b0
RK
5115 Next_Formal (Old_Formal);
5116 Next_Formal (New_Formal);
5117 end loop;
5118
5119 if Present (Old_Formal) then
5d37ba92 5120 Conformance_Error ("\too few parameters!");
996ae0b0
RK
5121 return;
5122
5123 elsif Present (New_Formal) then
5d37ba92 5124 Conformance_Error ("\too many parameters!", New_Formal);
996ae0b0
RK
5125 return;
5126 end if;
996ae0b0
RK
5127 end Check_Conformance;
5128
ec4867fa
ES
5129 -----------------------
5130 -- Check_Conventions --
5131 -----------------------
5132
5133 procedure Check_Conventions (Typ : Entity_Id) is
ce2b6ba5 5134 Ifaces_List : Elist_Id;
0a36105d 5135
ce2b6ba5 5136 procedure Check_Convention (Op : Entity_Id);
0a36105d
JM
5137 -- Verify that the convention of inherited dispatching operation Op is
5138 -- consistent among all subprograms it overrides. In order to minimize
5139 -- the search, Search_From is utilized to designate a specific point in
5140 -- the list rather than iterating over the whole list once more.
ec4867fa
ES
5141
5142 ----------------------
5143 -- Check_Convention --
5144 ----------------------
5145
ce2b6ba5 5146 procedure Check_Convention (Op : Entity_Id) is
9f6aaa5c
AC
5147 function Convention_Of (Id : Entity_Id) return Convention_Id;
5148 -- Given an entity, return its convention. The function treats Ghost
5149 -- as convention Ada because the two have the same dynamic semantics.
5150
5151 -------------------
5152 -- Convention_Of --
5153 -------------------
5154
5155 function Convention_Of (Id : Entity_Id) return Convention_Id is
5156 Conv : constant Convention_Id := Convention (Id);
5157 begin
5158 if Conv = Convention_Ghost then
5159 return Convention_Ada;
5160 else
5161 return Conv;
5162 end if;
5163 end Convention_Of;
5164
5165 -- Local variables
5166
5167 Op_Conv : constant Convention_Id := Convention_Of (Op);
5168 Iface_Conv : Convention_Id;
ce2b6ba5
JM
5169 Iface_Elmt : Elmt_Id;
5170 Iface_Prim_Elmt : Elmt_Id;
5171 Iface_Prim : Entity_Id;
ec4867fa 5172
9f6aaa5c
AC
5173 -- Start of processing for Check_Convention
5174
ce2b6ba5
JM
5175 begin
5176 Iface_Elmt := First_Elmt (Ifaces_List);
5177 while Present (Iface_Elmt) loop
5178 Iface_Prim_Elmt :=
9f6aaa5c 5179 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
ce2b6ba5
JM
5180 while Present (Iface_Prim_Elmt) loop
5181 Iface_Prim := Node (Iface_Prim_Elmt);
9f6aaa5c 5182 Iface_Conv := Convention_Of (Iface_Prim);
ce2b6ba5
JM
5183
5184 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
9f6aaa5c 5185 and then Iface_Conv /= Op_Conv
ce2b6ba5 5186 then
ed2233dc 5187 Error_Msg_N
ce2b6ba5 5188 ("inconsistent conventions in primitive operations", Typ);
ec4867fa 5189
ce2b6ba5 5190 Error_Msg_Name_1 := Chars (Op);
9f6aaa5c 5191 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
ce2b6ba5 5192 Error_Msg_Sloc := Sloc (Op);
ec4867fa 5193
7a963087 5194 if Comes_From_Source (Op) or else No (Alias (Op)) then
038140ed 5195 if not Present (Overridden_Operation (Op)) then
ed2233dc 5196 Error_Msg_N ("\\primitive % defined #", Typ);
ce2b6ba5 5197 else
ed2233dc 5198 Error_Msg_N
3ccedacc
AC
5199 ("\\overriding operation % with "
5200 & "convention % defined #", Typ);
ce2b6ba5 5201 end if;
ec4867fa 5202
ce2b6ba5
JM
5203 else pragma Assert (Present (Alias (Op)));
5204 Error_Msg_Sloc := Sloc (Alias (Op));
3ccedacc
AC
5205 Error_Msg_N ("\\inherited operation % with "
5206 & "convention % defined #", Typ);
ce2b6ba5 5207 end if;
ec4867fa 5208
ce2b6ba5 5209 Error_Msg_Name_1 := Chars (Op);
9f6aaa5c
AC
5210 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5211 Error_Msg_Sloc := Sloc (Iface_Prim);
3ccedacc
AC
5212 Error_Msg_N ("\\overridden operation % with "
5213 & "convention % defined #", Typ);
ec4867fa 5214
ce2b6ba5 5215 -- Avoid cascading errors
ec4867fa 5216
ce2b6ba5
JM
5217 return;
5218 end if;
ec4867fa 5219
ce2b6ba5
JM
5220 Next_Elmt (Iface_Prim_Elmt);
5221 end loop;
ec4867fa 5222
ce2b6ba5 5223 Next_Elmt (Iface_Elmt);
ec4867fa
ES
5224 end loop;
5225 end Check_Convention;
5226
5227 -- Local variables
5228
5229 Prim_Op : Entity_Id;
5230 Prim_Op_Elmt : Elmt_Id;
5231
5232 -- Start of processing for Check_Conventions
5233
5234 begin
ce2b6ba5
JM
5235 if not Has_Interfaces (Typ) then
5236 return;
5237 end if;
5238
5239 Collect_Interfaces (Typ, Ifaces_List);
5240
0a36105d
JM
5241 -- The algorithm checks every overriding dispatching operation against
5242 -- all the corresponding overridden dispatching operations, detecting
f3d57416 5243 -- differences in conventions.
ec4867fa
ES
5244
5245 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5246 while Present (Prim_Op_Elmt) loop
5247 Prim_Op := Node (Prim_Op_Elmt);
5248
0a36105d 5249 -- A small optimization: skip the predefined dispatching operations
ce2b6ba5 5250 -- since they always have the same convention.
ec4867fa 5251
ce2b6ba5
JM
5252 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5253 Check_Convention (Prim_Op);
ec4867fa
ES
5254 end if;
5255
5256 Next_Elmt (Prim_Op_Elmt);
5257 end loop;
5258 end Check_Conventions;
5259
996ae0b0
RK
5260 ------------------------------
5261 -- Check_Delayed_Subprogram --
5262 ------------------------------
5263
5264 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5265 F : Entity_Id;
5266
5267 procedure Possible_Freeze (T : Entity_Id);
5268 -- T is the type of either a formal parameter or of the return type.
5269 -- If T is not yet frozen and needs a delayed freeze, then the
4a13695c
AC
5270 -- subprogram itself must be delayed. If T is the limited view of an
5271 -- incomplete type the subprogram must be frozen as well, because
5272 -- T may depend on local types that have not been frozen yet.
996ae0b0 5273
82c80734
RD
5274 ---------------------
5275 -- Possible_Freeze --
5276 ---------------------
5277
996ae0b0
RK
5278 procedure Possible_Freeze (T : Entity_Id) is
5279 begin
4a13695c 5280 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
996ae0b0
RK
5281 Set_Has_Delayed_Freeze (Designator);
5282
5283 elsif Is_Access_Type (T)
5284 and then Has_Delayed_Freeze (Designated_Type (T))
5285 and then not Is_Frozen (Designated_Type (T))
5286 then
5287 Set_Has_Delayed_Freeze (Designator);
e358346d 5288
7b56a91b
AC
5289 elsif Ekind (T) = E_Incomplete_Type
5290 and then From_Limited_With (T)
5291 then
e358346d 5292 Set_Has_Delayed_Freeze (Designator);
406935b6 5293
9aff36e9
RD
5294 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
5295 -- of a subprogram or entry declaration.
406935b6
AC
5296
5297 elsif Ekind (T) = E_Incomplete_Type
5298 and then Ada_Version >= Ada_2012
5299 then
5300 Set_Has_Delayed_Freeze (Designator);
996ae0b0 5301 end if;
4a13695c 5302
996ae0b0
RK
5303 end Possible_Freeze;
5304
5305 -- Start of processing for Check_Delayed_Subprogram
5306
5307 begin
76e3504f
AC
5308 -- All subprograms, including abstract subprograms, may need a freeze
5309 -- node if some formal type or the return type needs one.
996ae0b0 5310
76e3504f
AC
5311 Possible_Freeze (Etype (Designator));
5312 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
996ae0b0 5313
76e3504f
AC
5314 -- Need delayed freeze if any of the formal types themselves need
5315 -- a delayed freeze and are not yet frozen.
996ae0b0 5316
76e3504f
AC
5317 F := First_Formal (Designator);
5318 while Present (F) loop
5319 Possible_Freeze (Etype (F));
5320 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5321 Next_Formal (F);
5322 end loop;
996ae0b0
RK
5323
5324 -- Mark functions that return by reference. Note that it cannot be
5325 -- done for delayed_freeze subprograms because the underlying
5326 -- returned type may not be known yet (for private types)
5327
8fde064e 5328 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
996ae0b0
RK
5329 declare
5330 Typ : constant Entity_Id := Etype (Designator);
5331 Utyp : constant Entity_Id := Underlying_Type (Typ);
996ae0b0 5332 begin
51245e2d 5333 if Is_Limited_View (Typ) then
996ae0b0 5334 Set_Returns_By_Ref (Designator);
048e5cef 5335 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
996ae0b0
RK
5336 Set_Returns_By_Ref (Designator);
5337 end if;
5338 end;
5339 end if;
5340 end Check_Delayed_Subprogram;
5341
5342 ------------------------------------
5343 -- Check_Discriminant_Conformance --
5344 ------------------------------------
5345
5346 procedure Check_Discriminant_Conformance
5347 (N : Node_Id;
5348 Prev : Entity_Id;
5349 Prev_Loc : Node_Id)
5350 is
5351 Old_Discr : Entity_Id := First_Discriminant (Prev);
5352 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5353 New_Discr_Id : Entity_Id;
5354 New_Discr_Type : Entity_Id;
5355
5356 procedure Conformance_Error (Msg : String; N : Node_Id);
82c80734
RD
5357 -- Post error message for conformance error on given node. Two messages
5358 -- are output. The first points to the previous declaration with a
5359 -- general "no conformance" message. The second is the detailed reason,
5360 -- supplied as Msg. The parameter N provide information for a possible
5361 -- & insertion in the message.
996ae0b0
RK
5362
5363 -----------------------
5364 -- Conformance_Error --
5365 -----------------------
5366
5367 procedure Conformance_Error (Msg : String; N : Node_Id) is
5368 begin
5369 Error_Msg_Sloc := Sloc (Prev_Loc);
483c78cb
RD
5370 Error_Msg_N -- CODEFIX
5371 ("not fully conformant with declaration#!", N);
996ae0b0
RK
5372 Error_Msg_NE (Msg, N, N);
5373 end Conformance_Error;
5374
5375 -- Start of processing for Check_Discriminant_Conformance
5376
5377 begin
5378 while Present (Old_Discr) and then Present (New_Discr) loop
996ae0b0
RK
5379 New_Discr_Id := Defining_Identifier (New_Discr);
5380
82c80734
RD
5381 -- The subtype mark of the discriminant on the full type has not
5382 -- been analyzed so we do it here. For an access discriminant a new
5383 -- type is created.
996ae0b0
RK
5384
5385 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5386 New_Discr_Type :=
5387 Access_Definition (N, Discriminant_Type (New_Discr));
5388
5389 else
5390 Analyze (Discriminant_Type (New_Discr));
5391 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
e50e1c5e
AC
5392
5393 -- Ada 2005: if the discriminant definition carries a null
5394 -- exclusion, create an itype to check properly for consistency
5395 -- with partial declaration.
5396
5397 if Is_Access_Type (New_Discr_Type)
8fde064e 5398 and then Null_Exclusion_Present (New_Discr)
e50e1c5e
AC
5399 then
5400 New_Discr_Type :=
5401 Create_Null_Excluding_Itype
5402 (T => New_Discr_Type,
5403 Related_Nod => New_Discr,
5404 Scope_Id => Current_Scope);
5405 end if;
996ae0b0
RK
5406 end if;
5407
5408 if not Conforming_Types
5409 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5410 then
5411 Conformance_Error ("type of & does not match!", New_Discr_Id);
5412 return;
fbf5a39b 5413 else
82c80734
RD
5414 -- Treat the new discriminant as an occurrence of the old one,
5415 -- for navigation purposes, and fill in some semantic
fbf5a39b
AC
5416 -- information, for completeness.
5417
5418 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5419 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5420 Set_Scope (New_Discr_Id, Scope (Old_Discr));
996ae0b0
RK
5421 end if;
5422
5423 -- Names must match
5424
5425 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5426 Conformance_Error ("name & does not match!", New_Discr_Id);
5427 return;
5428 end if;
5429
5430 -- Default expressions must match
5431
5432 declare
5433 NewD : constant Boolean :=
5434 Present (Expression (New_Discr));
5435 OldD : constant Boolean :=
5436 Present (Expression (Parent (Old_Discr)));
5437
5438 begin
5439 if NewD or OldD then
5440
5441 -- The old default value has been analyzed and expanded,
5442 -- because the current full declaration will have frozen
82c80734
RD
5443 -- everything before. The new default values have not been
5444 -- expanded, so expand now to check conformance.
996ae0b0
RK
5445
5446 if NewD then
21d27997 5447 Preanalyze_Spec_Expression
996ae0b0
RK
5448 (Expression (New_Discr), New_Discr_Type);
5449 end if;
5450
5451 if not (NewD and OldD)
5452 or else not Fully_Conformant_Expressions
5453 (Expression (Parent (Old_Discr)),
5454 Expression (New_Discr))
5455
5456 then
5457 Conformance_Error
5458 ("default expression for & does not match!",
5459 New_Discr_Id);
5460 return;
5461 end if;
5462 end if;
5463 end;
5464
5465 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5466
0ab80019 5467 if Ada_Version = Ada_83 then
996ae0b0
RK
5468 declare
5469 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5470
5471 begin
5472 -- Grouping (use of comma in param lists) must be the same
5473 -- This is where we catch a misconformance like:
5474
60370fb1 5475 -- A, B : Integer
996ae0b0
RK
5476 -- A : Integer; B : Integer
5477
5478 -- which are represented identically in the tree except
5479 -- for the setting of the flags More_Ids and Prev_Ids.
5480
5481 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
5482 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
5483 then
5484 Conformance_Error
5485 ("grouping of & does not match!", New_Discr_Id);
5486 return;
5487 end if;
5488 end;
5489 end if;
5490
5491 Next_Discriminant (Old_Discr);
5492 Next (New_Discr);
5493 end loop;
5494
5495 if Present (Old_Discr) then
5496 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
5497 return;
5498
5499 elsif Present (New_Discr) then
5500 Conformance_Error
5501 ("too many discriminants!", Defining_Identifier (New_Discr));
5502 return;
5503 end if;
5504 end Check_Discriminant_Conformance;
5505
5506 ----------------------------
5507 -- Check_Fully_Conformant --
5508 ----------------------------
5509
5510 procedure Check_Fully_Conformant
5511 (New_Id : Entity_Id;
5512 Old_Id : Entity_Id;
5513 Err_Loc : Node_Id := Empty)
5514 is
5515 Result : Boolean;
81db9d77 5516 pragma Warnings (Off, Result);
996ae0b0
RK
5517 begin
5518 Check_Conformance
5519 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
5520 end Check_Fully_Conformant;
5521
5522 ---------------------------
5523 -- Check_Mode_Conformant --
5524 ---------------------------
5525
5526 procedure Check_Mode_Conformant
5527 (New_Id : Entity_Id;
5528 Old_Id : Entity_Id;
5529 Err_Loc : Node_Id := Empty;
5530 Get_Inst : Boolean := False)
5531 is
5532 Result : Boolean;
81db9d77 5533 pragma Warnings (Off, Result);
996ae0b0
RK
5534 begin
5535 Check_Conformance
5536 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
5537 end Check_Mode_Conformant;
5538
fbf5a39b 5539 --------------------------------
758c442c 5540 -- Check_Overriding_Indicator --
fbf5a39b
AC
5541 --------------------------------
5542
758c442c 5543 procedure Check_Overriding_Indicator
ec4867fa 5544 (Subp : Entity_Id;
5d37ba92
ES
5545 Overridden_Subp : Entity_Id;
5546 Is_Primitive : Boolean)
fbf5a39b 5547 is
758c442c
GD
5548 Decl : Node_Id;
5549 Spec : Node_Id;
fbf5a39b
AC
5550
5551 begin
ec4867fa 5552 -- No overriding indicator for literals
fbf5a39b 5553
ec4867fa 5554 if Ekind (Subp) = E_Enumeration_Literal then
758c442c 5555 return;
fbf5a39b 5556
ec4867fa
ES
5557 elsif Ekind (Subp) = E_Entry then
5558 Decl := Parent (Subp);
5559
53b10ce9
AC
5560 -- No point in analyzing a malformed operator
5561
5562 elsif Nkind (Subp) = N_Defining_Operator_Symbol
5563 and then Error_Posted (Subp)
5564 then
5565 return;
5566
758c442c
GD
5567 else
5568 Decl := Unit_Declaration_Node (Subp);
5569 end if;
fbf5a39b 5570
800621e0
RD
5571 if Nkind_In (Decl, N_Subprogram_Body,
5572 N_Subprogram_Body_Stub,
5573 N_Subprogram_Declaration,
5574 N_Abstract_Subprogram_Declaration,
5575 N_Subprogram_Renaming_Declaration)
758c442c
GD
5576 then
5577 Spec := Specification (Decl);
ec4867fa
ES
5578
5579 elsif Nkind (Decl) = N_Entry_Declaration then
5580 Spec := Decl;
5581
758c442c
GD
5582 else
5583 return;
5584 end if;
fbf5a39b 5585
e7d72fb9
AC
5586 -- The overriding operation is type conformant with the overridden one,
5587 -- but the names of the formals are not required to match. If the names
6823270c 5588 -- appear permuted in the overriding operation, this is a possible
e7d72fb9
AC
5589 -- source of confusion that is worth diagnosing. Controlling formals
5590 -- often carry names that reflect the type, and it is not worthwhile
5591 -- requiring that their names match.
5592
c9e7bd8e 5593 if Present (Overridden_Subp)
e7d72fb9
AC
5594 and then Nkind (Subp) /= N_Defining_Operator_Symbol
5595 then
5596 declare
5597 Form1 : Entity_Id;
5598 Form2 : Entity_Id;
5599
5600 begin
5601 Form1 := First_Formal (Subp);
5602 Form2 := First_Formal (Overridden_Subp);
5603
c9e7bd8e
AC
5604 -- If the overriding operation is a synchronized operation, skip
5605 -- the first parameter of the overridden operation, which is
6823270c
AC
5606 -- implicit in the new one. If the operation is declared in the
5607 -- body it is not primitive and all formals must match.
c9e7bd8e 5608
6823270c
AC
5609 if Is_Concurrent_Type (Scope (Subp))
5610 and then Is_Tagged_Type (Scope (Subp))
5611 and then not Has_Completion (Scope (Subp))
5612 then
c9e7bd8e
AC
5613 Form2 := Next_Formal (Form2);
5614 end if;
5615
e7d72fb9
AC
5616 if Present (Form1) then
5617 Form1 := Next_Formal (Form1);
5618 Form2 := Next_Formal (Form2);
5619 end if;
5620
5621 while Present (Form1) loop
5622 if not Is_Controlling_Formal (Form1)
5623 and then Present (Next_Formal (Form2))
5624 and then Chars (Form1) = Chars (Next_Formal (Form2))
5625 then
5626 Error_Msg_Node_2 := Alias (Overridden_Subp);
5627 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
ed2233dc 5628 Error_Msg_NE
19d846a0 5629 ("& does not match corresponding formal of&#",
e7d72fb9
AC
5630 Form1, Form1);
5631 exit;
5632 end if;
5633
5634 Next_Formal (Form1);
5635 Next_Formal (Form2);
5636 end loop;
5637 end;
5638 end if;
5639
676e8420
AC
5640 -- If there is an overridden subprogram, then check that there is no
5641 -- "not overriding" indicator, and mark the subprogram as overriding.
51bf9bdf
AC
5642 -- This is not done if the overridden subprogram is marked as hidden,
5643 -- which can occur for the case of inherited controlled operations
5644 -- (see Derive_Subprogram), unless the inherited subprogram's parent
5645 -- subprogram is not itself hidden. (Note: This condition could probably
5646 -- be simplified, leaving out the testing for the specific controlled
5647 -- cases, but it seems safer and clearer this way, and echoes similar
5648 -- special-case tests of this kind in other places.)
5649
fd0d899b 5650 if Present (Overridden_Subp)
51bf9bdf
AC
5651 and then (not Is_Hidden (Overridden_Subp)
5652 or else
b69cd36a
AC
5653 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
5654 Name_Adjust,
5655 Name_Finalize)
f0709ca6
AC
5656 and then Present (Alias (Overridden_Subp))
5657 and then not Is_Hidden (Alias (Overridden_Subp))))
fd0d899b 5658 then
ec4867fa
ES
5659 if Must_Not_Override (Spec) then
5660 Error_Msg_Sloc := Sloc (Overridden_Subp);
fbf5a39b 5661
ec4867fa 5662 if Ekind (Subp) = E_Entry then
ed2233dc 5663 Error_Msg_NE
5d37ba92 5664 ("entry & overrides inherited operation #", Spec, Subp);
ec4867fa 5665 else
ed2233dc 5666 Error_Msg_NE
5d37ba92 5667 ("subprogram & overrides inherited operation #", Spec, Subp);
ec4867fa 5668 end if;
21d27997 5669
bd603506 5670 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
24a120ac
AC
5671 -- as an extension of Root_Controlled, and thus has a useless Adjust
5672 -- operation. This operation should not be inherited by other limited
5673 -- controlled types. An explicit Adjust for them is not overriding.
5674
5675 elsif Must_Override (Spec)
5676 and then Chars (Overridden_Subp) = Name_Adjust
5677 and then Is_Limited_Type (Etype (First_Formal (Subp)))
5678 and then Present (Alias (Overridden_Subp))
bd603506
RD
5679 and then
5680 Is_Predefined_File_Name
5681 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
24a120ac
AC
5682 then
5683 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5684
21d27997 5685 elsif Is_Subprogram (Subp) then
2fe829ae
ES
5686 if Is_Init_Proc (Subp) then
5687 null;
5688
5689 elsif No (Overridden_Operation (Subp)) then
1c1289e7
AC
5690
5691 -- For entities generated by Derive_Subprograms the overridden
5692 -- operation is the inherited primitive (which is available
5693 -- through the attribute alias)
5694
5695 if (Is_Dispatching_Operation (Subp)
f9673bb0 5696 or else Is_Dispatching_Operation (Overridden_Subp))
1c1289e7 5697 and then not Comes_From_Source (Overridden_Subp)
f9673bb0
AC
5698 and then Find_Dispatching_Type (Overridden_Subp) =
5699 Find_Dispatching_Type (Subp)
1c1289e7
AC
5700 and then Present (Alias (Overridden_Subp))
5701 and then Comes_From_Source (Alias (Overridden_Subp))
5702 then
039538bc
AC
5703 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
5704 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
2fe829ae 5705
1c1289e7 5706 else
039538bc
AC
5707 Set_Overridden_Operation (Subp, Overridden_Subp);
5708 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
1c1289e7
AC
5709 end if;
5710 end if;
ec4867fa 5711 end if;
f937473f 5712
618fb570
AC
5713 -- If primitive flag is set or this is a protected operation, then
5714 -- the operation is overriding at the point of its declaration, so
5715 -- warn if necessary. Otherwise it may have been declared before the
5716 -- operation it overrides and no check is required.
3c25856a
AC
5717
5718 if Style_Check
618fb570
AC
5719 and then not Must_Override (Spec)
5720 and then (Is_Primitive
5721 or else Ekind (Scope (Subp)) = E_Protected_Type)
3c25856a 5722 then
235f4375
AC
5723 Style.Missing_Overriding (Decl, Subp);
5724 end if;
5725
53b10ce9
AC
5726 -- If Subp is an operator, it may override a predefined operation, if
5727 -- it is defined in the same scope as the type to which it applies.
676e8420 5728 -- In that case Overridden_Subp is empty because of our implicit
5d37ba92
ES
5729 -- representation for predefined operators. We have to check whether the
5730 -- signature of Subp matches that of a predefined operator. Note that
5731 -- first argument provides the name of the operator, and the second
5732 -- argument the signature that may match that of a standard operation.
21d27997
RD
5733 -- If the indicator is overriding, then the operator must match a
5734 -- predefined signature, because we know already that there is no
5735 -- explicit overridden operation.
f937473f 5736
21d27997 5737 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
806f6d37 5738 if Must_Not_Override (Spec) then
f937473f 5739
806f6d37
AC
5740 -- If this is not a primitive or a protected subprogram, then
5741 -- "not overriding" is illegal.
618fb570 5742
806f6d37
AC
5743 if not Is_Primitive
5744 and then Ekind (Scope (Subp)) /= E_Protected_Type
5745 then
3ccedacc
AC
5746 Error_Msg_N ("overriding indicator only allowed "
5747 & "if subprogram is primitive", Subp);
618fb570 5748
806f6d37
AC
5749 elsif Can_Override_Operator (Subp) then
5750 Error_Msg_NE
5751 ("subprogram& overrides predefined operator ", Spec, Subp);
5752 end if;
f937473f 5753
806f6d37
AC
5754 elsif Must_Override (Spec) then
5755 if No (Overridden_Operation (Subp))
5756 and then not Can_Override_Operator (Subp)
5757 then
5758 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5759 end if;
5d37ba92 5760
806f6d37
AC
5761 elsif not Error_Posted (Subp)
5762 and then Style_Check
5763 and then Can_Override_Operator (Subp)
5764 and then
5765 not Is_Predefined_File_Name
5766 (Unit_File_Name (Get_Source_Unit (Subp)))
5767 then
5768 -- If style checks are enabled, indicate that the indicator is
5769 -- missing. However, at the point of declaration, the type of
5770 -- which this is a primitive operation may be private, in which
5771 -- case the indicator would be premature.
235f4375 5772
806f6d37
AC
5773 if Has_Private_Declaration (Etype (Subp))
5774 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
53b10ce9 5775 then
806f6d37
AC
5776 null;
5777 else
5778 Style.Missing_Overriding (Decl, Subp);
5d5832bc 5779 end if;
806f6d37 5780 end if;
21d27997
RD
5781
5782 elsif Must_Override (Spec) then
5783 if Ekind (Subp) = E_Entry then
ed2233dc 5784 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5d37ba92 5785 else
ed2233dc 5786 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
758c442c 5787 end if;
5d37ba92
ES
5788
5789 -- If the operation is marked "not overriding" and it's not primitive
5790 -- then an error is issued, unless this is an operation of a task or
5791 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
5792 -- has been specified have already been checked above.
5793
5794 elsif Must_Not_Override (Spec)
5795 and then not Is_Primitive
5796 and then Ekind (Subp) /= E_Entry
5797 and then Ekind (Scope (Subp)) /= E_Protected_Type
5798 then
ed2233dc 5799 Error_Msg_N
5d37ba92
ES
5800 ("overriding indicator only allowed if subprogram is primitive",
5801 Subp);
5d37ba92 5802 return;
fbf5a39b 5803 end if;
758c442c 5804 end Check_Overriding_Indicator;
fbf5a39b 5805
996ae0b0
RK
5806 -------------------
5807 -- Check_Returns --
5808 -------------------
5809
0a36105d
JM
5810 -- Note: this procedure needs to know far too much about how the expander
5811 -- messes with exceptions. The use of the flag Exception_Junk and the
5812 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
5813 -- works, but is not very clean. It would be better if the expansion
5814 -- routines would leave Original_Node working nicely, and we could use
5815 -- Original_Node here to ignore all the peculiar expander messing ???
5816
996ae0b0
RK
5817 procedure Check_Returns
5818 (HSS : Node_Id;
5819 Mode : Character;
c8ef728f
ES
5820 Err : out Boolean;
5821 Proc : Entity_Id := Empty)
996ae0b0
RK
5822 is
5823 Handler : Node_Id;
5824
5825 procedure Check_Statement_Sequence (L : List_Id);
5826 -- Internal recursive procedure to check a list of statements for proper
5827 -- termination by a return statement (or a transfer of control or a
5828 -- compound statement that is itself internally properly terminated).
5829
5830 ------------------------------
5831 -- Check_Statement_Sequence --
5832 ------------------------------
5833
5834 procedure Check_Statement_Sequence (L : List_Id) is
5835 Last_Stm : Node_Id;
0a36105d 5836 Stm : Node_Id;
996ae0b0
RK
5837 Kind : Node_Kind;
5838
7b27e183
AC
5839 function Assert_False return Boolean;
5840 -- Returns True if Last_Stm is a pragma Assert (False) that has been
5841 -- rewritten as a null statement when assertions are off. The assert
5842 -- is not active, but it is still enough to kill the warning.
5843
5844 ------------------
5845 -- Assert_False --
5846 ------------------
5847
5848 function Assert_False return Boolean is
5849 Orig : constant Node_Id := Original_Node (Last_Stm);
5850
5851 begin
5852 if Nkind (Orig) = N_Pragma
5853 and then Pragma_Name (Orig) = Name_Assert
5854 and then not Error_Posted (Orig)
5855 then
5856 declare
5857 Arg : constant Node_Id :=
5858 First (Pragma_Argument_Associations (Orig));
5859 Exp : constant Node_Id := Expression (Arg);
5860 begin
5861 return Nkind (Exp) = N_Identifier
5862 and then Chars (Exp) = Name_False;
5863 end;
5864
5865 else
5866 return False;
5867 end if;
5868 end Assert_False;
5869
5870 -- Local variables
5871
996ae0b0
RK
5872 Raise_Exception_Call : Boolean;
5873 -- Set True if statement sequence terminated by Raise_Exception call
5874 -- or a Reraise_Occurrence call.
5875
7b27e183
AC
5876 -- Start of processing for Check_Statement_Sequence
5877
996ae0b0
RK
5878 begin
5879 Raise_Exception_Call := False;
5880
5881 -- Get last real statement
5882
5883 Last_Stm := Last (L);
5884
0a36105d
JM
5885 -- Deal with digging out exception handler statement sequences that
5886 -- have been transformed by the local raise to goto optimization.
5887 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
5888 -- optimization has occurred, we are looking at something like:
5889
5890 -- begin
5891 -- original stmts in block
5892
5893 -- exception \
5894 -- when excep1 => |
5895 -- goto L1; | omitted if No_Exception_Propagation
5896 -- when excep2 => |
5897 -- goto L2; /
5898 -- end;
5899
5900 -- goto L3; -- skip handler when exception not raised
5901
5902 -- <<L1>> -- target label for local exception
5903 -- begin
5904 -- estmts1
5905 -- end;
5906
5907 -- goto L3;
5908
5909 -- <<L2>>
5910 -- begin
5911 -- estmts2
5912 -- end;
5913
5914 -- <<L3>>
5915
5916 -- and what we have to do is to dig out the estmts1 and estmts2
5917 -- sequences (which were the original sequences of statements in
5918 -- the exception handlers) and check them.
5919
8fde064e 5920 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
0a36105d
JM
5921 Stm := Last_Stm;
5922 loop
5923 Prev (Stm);
5924 exit when No (Stm);
5925 exit when Nkind (Stm) /= N_Block_Statement;
5926 exit when not Exception_Junk (Stm);
5927 Prev (Stm);
5928 exit when No (Stm);
5929 exit when Nkind (Stm) /= N_Label;
5930 exit when not Exception_Junk (Stm);
5931 Check_Statement_Sequence
5932 (Statements (Handled_Statement_Sequence (Next (Stm))));
5933
5934 Prev (Stm);
5935 Last_Stm := Stm;
5936 exit when No (Stm);
5937 exit when Nkind (Stm) /= N_Goto_Statement;
5938 exit when not Exception_Junk (Stm);
5939 end loop;
5940 end if;
5941
996ae0b0
RK
5942 -- Don't count pragmas
5943
5944 while Nkind (Last_Stm) = N_Pragma
5945
5946 -- Don't count call to SS_Release (can happen after Raise_Exception)
5947
5948 or else
5949 (Nkind (Last_Stm) = N_Procedure_Call_Statement
5950 and then
5951 Nkind (Name (Last_Stm)) = N_Identifier
5952 and then
5953 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
5954
5955 -- Don't count exception junk
5956
5957 or else
800621e0
RD
5958 (Nkind_In (Last_Stm, N_Goto_Statement,
5959 N_Label,
5960 N_Object_Declaration)
8fde064e 5961 and then Exception_Junk (Last_Stm))
0a36105d
JM
5962 or else Nkind (Last_Stm) in N_Push_xxx_Label
5963 or else Nkind (Last_Stm) in N_Pop_xxx_Label
e3b3266c
AC
5964
5965 -- Inserted code, such as finalization calls, is irrelevant: we only
5966 -- need to check original source.
5967
5968 or else Is_Rewrite_Insertion (Last_Stm)
996ae0b0
RK
5969 loop
5970 Prev (Last_Stm);
5971 end loop;
5972
5973 -- Here we have the "real" last statement
5974
5975 Kind := Nkind (Last_Stm);
5976
5977 -- Transfer of control, OK. Note that in the No_Return procedure
5978 -- case, we already diagnosed any explicit return statements, so
5979 -- we can treat them as OK in this context.
5980
5981 if Is_Transfer (Last_Stm) then
5982 return;
5983
5984 -- Check cases of explicit non-indirect procedure calls
5985
5986 elsif Kind = N_Procedure_Call_Statement
5987 and then Is_Entity_Name (Name (Last_Stm))
5988 then
5989 -- Check call to Raise_Exception procedure which is treated
5990 -- specially, as is a call to Reraise_Occurrence.
5991
5992 -- We suppress the warning in these cases since it is likely that
5993 -- the programmer really does not expect to deal with the case
5994 -- of Null_Occurrence, and thus would find a warning about a
5995 -- missing return curious, and raising Program_Error does not
5996 -- seem such a bad behavior if this does occur.
5997
c8ef728f
ES
5998 -- Note that in the Ada 2005 case for Raise_Exception, the actual
5999 -- behavior will be to raise Constraint_Error (see AI-329).
6000
996ae0b0
RK
6001 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6002 or else
6003 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6004 then
6005 Raise_Exception_Call := True;
6006
6007 -- For Raise_Exception call, test first argument, if it is
6008 -- an attribute reference for a 'Identity call, then we know
6009 -- that the call cannot possibly return.
6010
6011 declare
6012 Arg : constant Node_Id :=
6013 Original_Node (First_Actual (Last_Stm));
996ae0b0
RK
6014 begin
6015 if Nkind (Arg) = N_Attribute_Reference
6016 and then Attribute_Name (Arg) = Name_Identity
6017 then
6018 return;
6019 end if;
6020 end;
6021 end if;
6022
6023 -- If statement, need to look inside if there is an else and check
6024 -- each constituent statement sequence for proper termination.
6025
6026 elsif Kind = N_If_Statement
6027 and then Present (Else_Statements (Last_Stm))
6028 then
6029 Check_Statement_Sequence (Then_Statements (Last_Stm));
6030 Check_Statement_Sequence (Else_Statements (Last_Stm));
6031
6032 if Present (Elsif_Parts (Last_Stm)) then
6033 declare
6034 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6035
6036 begin
6037 while Present (Elsif_Part) loop
6038 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6039 Next (Elsif_Part);
6040 end loop;
6041 end;
6042 end if;
6043
6044 return;
6045
6046 -- Case statement, check each case for proper termination
6047
6048 elsif Kind = N_Case_Statement then
6049 declare
6050 Case_Alt : Node_Id;
996ae0b0
RK
6051 begin
6052 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6053 while Present (Case_Alt) loop
6054 Check_Statement_Sequence (Statements (Case_Alt));
6055 Next_Non_Pragma (Case_Alt);
6056 end loop;
6057 end;
6058
6059 return;
6060
6061 -- Block statement, check its handled sequence of statements
6062
6063 elsif Kind = N_Block_Statement then
6064 declare
6065 Err1 : Boolean;
6066
6067 begin
6068 Check_Returns
6069 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6070
6071 if Err1 then
6072 Err := True;
6073 end if;
6074
6075 return;
6076 end;
6077
6078 -- Loop statement. If there is an iteration scheme, we can definitely
6079 -- fall out of the loop. Similarly if there is an exit statement, we
6080 -- can fall out. In either case we need a following return.
6081
6082 elsif Kind = N_Loop_Statement then
6083 if Present (Iteration_Scheme (Last_Stm))
6084 or else Has_Exit (Entity (Identifier (Last_Stm)))
6085 then
6086 null;
6087
f3d57416
RW
6088 -- A loop with no exit statement or iteration scheme is either
6089 -- an infinite loop, or it has some other exit (raise/return).
996ae0b0
RK
6090 -- In either case, no warning is required.
6091
6092 else
6093 return;
6094 end if;
6095
6096 -- Timed entry call, check entry call and delay alternatives
6097
6098 -- Note: in expanded code, the timed entry call has been converted
6099 -- to a set of expanded statements on which the check will work
6100 -- correctly in any case.
6101
6102 elsif Kind = N_Timed_Entry_Call then
6103 declare
6104 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6105 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6106
6107 begin
6108 -- If statement sequence of entry call alternative is missing,
6109 -- then we can definitely fall through, and we post the error
6110 -- message on the entry call alternative itself.
6111
6112 if No (Statements (ECA)) then
6113 Last_Stm := ECA;
6114
6115 -- If statement sequence of delay alternative is missing, then
6116 -- we can definitely fall through, and we post the error
6117 -- message on the delay alternative itself.
6118
6119 -- Note: if both ECA and DCA are missing the return, then we
6120 -- post only one message, should be enough to fix the bugs.
6121 -- If not we will get a message next time on the DCA when the
a90bd866 6122 -- ECA is fixed.
996ae0b0
RK
6123
6124 elsif No (Statements (DCA)) then
6125 Last_Stm := DCA;
6126
6127 -- Else check both statement sequences
6128
6129 else
6130 Check_Statement_Sequence (Statements (ECA));
6131 Check_Statement_Sequence (Statements (DCA));
6132 return;
6133 end if;
6134 end;
6135
6136 -- Conditional entry call, check entry call and else part
6137
6138 -- Note: in expanded code, the conditional entry call has been
6139 -- converted to a set of expanded statements on which the check
6140 -- will work correctly in any case.
6141
6142 elsif Kind = N_Conditional_Entry_Call then
6143 declare
6144 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6145
6146 begin
6147 -- If statement sequence of entry call alternative is missing,
6148 -- then we can definitely fall through, and we post the error
6149 -- message on the entry call alternative itself.
6150
6151 if No (Statements (ECA)) then
6152 Last_Stm := ECA;
6153
6154 -- Else check statement sequence and else part
6155
6156 else
6157 Check_Statement_Sequence (Statements (ECA));
6158 Check_Statement_Sequence (Else_Statements (Last_Stm));
6159 return;
6160 end if;
6161 end;
6162 end if;
6163
6164 -- If we fall through, issue appropriate message
6165
6166 if Mode = 'F' then
7b27e183
AC
6167
6168 -- Kill warning if last statement is a raise exception call,
6169 -- or a pragma Assert (False). Note that with assertions enabled,
6170 -- such a pragma has been converted into a raise exception call
6171 -- already, so the Assert_False is for the assertions off case.
6172
6173 if not Raise_Exception_Call and then not Assert_False then
b465ef6f
AC
6174
6175 -- In GNATprove mode, it is an error to have a missing return
6176
43417b90 6177 Error_Msg_Warn := SPARK_Mode /= On;
7b27e183
AC
6178
6179 -- Issue error message or warning
6180
4a28b181
AC
6181 Error_Msg_N
6182 ("RETURN statement missing following this statement<<!",
6183 Last_Stm);
6184 Error_Msg_N
6185 ("\Program_Error ]<<!", Last_Stm);
996ae0b0
RK
6186 end if;
6187
6188 -- Note: we set Err even though we have not issued a warning
6189 -- because we still have a case of a missing return. This is
6190 -- an extremely marginal case, probably will never be noticed
6191 -- but we might as well get it right.
6192
6193 Err := True;
6194
c8ef728f
ES
6195 -- Otherwise we have the case of a procedure marked No_Return
6196
996ae0b0 6197 else
800621e0 6198 if not Raise_Exception_Call then
4a28b181
AC
6199 if GNATprove_Mode then
6200 Error_Msg_N
6201 ("implied return after this statement "
6202 & "would have raised Program_Error", Last_Stm);
6203 else
6204 Error_Msg_N
6205 ("implied return after this statement "
6206 & "will raise Program_Error??", Last_Stm);
6207 end if;
6208
43417b90 6209 Error_Msg_Warn := SPARK_Mode /= On;
800621e0 6210 Error_Msg_NE
4a28b181 6211 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
800621e0 6212 end if;
c8ef728f
ES
6213
6214 declare
6215 RE : constant Node_Id :=
6216 Make_Raise_Program_Error (Sloc (Last_Stm),
6217 Reason => PE_Implicit_Return);
6218 begin
6219 Insert_After (Last_Stm, RE);
6220 Analyze (RE);
6221 end;
996ae0b0
RK
6222 end if;
6223 end Check_Statement_Sequence;
6224
6225 -- Start of processing for Check_Returns
6226
6227 begin
6228 Err := False;
6229 Check_Statement_Sequence (Statements (HSS));
6230
6231 if Present (Exception_Handlers (HSS)) then
6232 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6233 while Present (Handler) loop
6234 Check_Statement_Sequence (Statements (Handler));
6235 Next_Non_Pragma (Handler);
6236 end loop;
6237 end if;
6238 end Check_Returns;
6239
6240 ----------------------------
6241 -- Check_Subprogram_Order --
6242 ----------------------------
6243
6244 procedure Check_Subprogram_Order (N : Node_Id) is
6245
6246 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
dc36a7e3
RD
6247 -- This is used to check if S1 > S2 in the sense required by this test,
6248 -- for example nameab < namec, but name2 < name10.
996ae0b0 6249
82c80734
RD
6250 -----------------------------
6251 -- Subprogram_Name_Greater --
6252 -----------------------------
6253
996ae0b0
RK
6254 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6255 L1, L2 : Positive;
6256 N1, N2 : Natural;
6257
6258 begin
67336960
AC
6259 -- Deal with special case where names are identical except for a
6260 -- numerical suffix. These are handled specially, taking the numeric
6261 -- ordering from the suffix into account.
996ae0b0
RK
6262
6263 L1 := S1'Last;
6264 while S1 (L1) in '0' .. '9' loop
6265 L1 := L1 - 1;
6266 end loop;
6267
6268 L2 := S2'Last;
6269 while S2 (L2) in '0' .. '9' loop
6270 L2 := L2 - 1;
6271 end loop;
6272
67336960 6273 -- If non-numeric parts non-equal, do straight compare
996ae0b0 6274
67336960
AC
6275 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6276 return S1 > S2;
996ae0b0
RK
6277
6278 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6279 -- that a missing suffix is treated as numeric zero in this test.
6280
6281 else
6282 N1 := 0;
6283 while L1 < S1'Last loop
6284 L1 := L1 + 1;
6285 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6286 end loop;
6287
6288 N2 := 0;
6289 while L2 < S2'Last loop
6290 L2 := L2 + 1;
6291 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6292 end loop;
6293
6294 return N1 > N2;
6295 end if;
6296 end Subprogram_Name_Greater;
6297
6298 -- Start of processing for Check_Subprogram_Order
6299
6300 begin
6301 -- Check body in alpha order if this is option
6302
fbf5a39b 6303 if Style_Check
bc202b70 6304 and then Style_Check_Order_Subprograms
996ae0b0
RK
6305 and then Nkind (N) = N_Subprogram_Body
6306 and then Comes_From_Source (N)
6307 and then In_Extended_Main_Source_Unit (N)
6308 then
6309 declare
6310 LSN : String_Ptr
6311 renames Scope_Stack.Table
6312 (Scope_Stack.Last).Last_Subprogram_Name;
6313
6314 Body_Id : constant Entity_Id :=
6315 Defining_Entity (Specification (N));
6316
6317 begin
6318 Get_Decoded_Name_String (Chars (Body_Id));
6319
6320 if LSN /= null then
6321 if Subprogram_Name_Greater
6322 (LSN.all, Name_Buffer (1 .. Name_Len))
6323 then
6324 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6325 end if;
6326
6327 Free (LSN);
6328 end if;
6329
6330 LSN := new String'(Name_Buffer (1 .. Name_Len));
6331 end;
6332 end if;
6333 end Check_Subprogram_Order;
6334
6335 ------------------------------
6336 -- Check_Subtype_Conformant --
6337 ------------------------------
6338
6339 procedure Check_Subtype_Conformant
ce2b6ba5
JM
6340 (New_Id : Entity_Id;
6341 Old_Id : Entity_Id;
6342 Err_Loc : Node_Id := Empty;
f307415a
AC
6343 Skip_Controlling_Formals : Boolean := False;
6344 Get_Inst : Boolean := False)
996ae0b0
RK
6345 is
6346 Result : Boolean;
81db9d77 6347 pragma Warnings (Off, Result);
996ae0b0
RK
6348 begin
6349 Check_Conformance
ce2b6ba5 6350 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
f307415a
AC
6351 Skip_Controlling_Formals => Skip_Controlling_Formals,
6352 Get_Inst => Get_Inst);
996ae0b0
RK
6353 end Check_Subtype_Conformant;
6354
6355 ---------------------------
6356 -- Check_Type_Conformant --
6357 ---------------------------
6358
6359 procedure Check_Type_Conformant
6360 (New_Id : Entity_Id;
6361 Old_Id : Entity_Id;
6362 Err_Loc : Node_Id := Empty)
6363 is
6364 Result : Boolean;
81db9d77 6365 pragma Warnings (Off, Result);
996ae0b0
RK
6366 begin
6367 Check_Conformance
6368 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
6369 end Check_Type_Conformant;
6370
806f6d37
AC
6371 ---------------------------
6372 -- Can_Override_Operator --
6373 ---------------------------
6374
6375 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
6376 Typ : Entity_Id;
f146302c 6377
806f6d37
AC
6378 begin
6379 if Nkind (Subp) /= N_Defining_Operator_Symbol then
6380 return False;
6381
6382 else
6383 Typ := Base_Type (Etype (First_Formal (Subp)));
6384
f146302c
AC
6385 -- Check explicitly that the operation is a primitive of the type
6386
806f6d37 6387 return Operator_Matches_Spec (Subp, Subp)
f146302c 6388 and then not Is_Generic_Type (Typ)
806f6d37
AC
6389 and then Scope (Subp) = Scope (Typ)
6390 and then not Is_Class_Wide_Type (Typ);
6391 end if;
6392 end Can_Override_Operator;
6393
996ae0b0
RK
6394 ----------------------
6395 -- Conforming_Types --
6396 ----------------------
6397
6398 function Conforming_Types
6399 (T1 : Entity_Id;
6400 T2 : Entity_Id;
6401 Ctype : Conformance_Type;
d05ef0ab 6402 Get_Inst : Boolean := False) return Boolean
996ae0b0
RK
6403 is
6404 Type_1 : Entity_Id := T1;
6405 Type_2 : Entity_Id := T2;
af4b9434 6406 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
996ae0b0
RK
6407
6408 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
0a36105d
JM
6409 -- If neither T1 nor T2 are generic actual types, or if they are in
6410 -- different scopes (e.g. parent and child instances), then verify that
6411 -- the base types are equal. Otherwise T1 and T2 must be on the same
6412 -- subtype chain. The whole purpose of this procedure is to prevent
6413 -- spurious ambiguities in an instantiation that may arise if two
6414 -- distinct generic types are instantiated with the same actual.
6415
5d37ba92
ES
6416 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
6417 -- An access parameter can designate an incomplete type. If the
6418 -- incomplete type is the limited view of a type from a limited_
6419 -- with_clause, check whether the non-limited view is available. If
6420 -- it is a (non-limited) incomplete type, get the full view.
6421
0a36105d
JM
6422 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
6423 -- Returns True if and only if either T1 denotes a limited view of T2
6424 -- or T2 denotes a limited view of T1. This can arise when the limited
6425 -- with view of a type is used in a subprogram declaration and the
6426 -- subprogram body is in the scope of a regular with clause for the
6427 -- same unit. In such a case, the two type entities can be considered
6428 -- identical for purposes of conformance checking.
996ae0b0
RK
6429
6430 ----------------------
6431 -- Base_Types_Match --
6432 ----------------------
6433
6434 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
8fde064e
AC
6435 BT1 : constant Entity_Id := Base_Type (T1);
6436 BT2 : constant Entity_Id := Base_Type (T2);
6437
996ae0b0
RK
6438 begin
6439 if T1 = T2 then
6440 return True;
6441
8fde064e 6442 elsif BT1 = BT2 then
996ae0b0 6443
0a36105d 6444 -- The following is too permissive. A more precise test should
996ae0b0
RK
6445 -- check that the generic actual is an ancestor subtype of the
6446 -- other ???.
586ecbf3 6447
70f4ad20
AC
6448 -- See code in Find_Corresponding_Spec that applies an additional
6449 -- filter to handle accidental amiguities in instances.
996ae0b0
RK
6450
6451 return not Is_Generic_Actual_Type (T1)
07fc65c4
GB
6452 or else not Is_Generic_Actual_Type (T2)
6453 or else Scope (T1) /= Scope (T2);
996ae0b0 6454
8fde064e 6455 -- If T2 is a generic actual type it is declared as the subtype of
2995860f
AC
6456 -- the actual. If that actual is itself a subtype we need to use its
6457 -- own base type to check for compatibility.
8fde064e
AC
6458
6459 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
6460 return True;
6461
6462 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
6463 return True;
6464
0a36105d
JM
6465 else
6466 return False;
6467 end if;
6468 end Base_Types_Match;
aa720a54 6469
5d37ba92
ES
6470 --------------------------
6471 -- Find_Designated_Type --
6472 --------------------------
6473
6474 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
6475 Desig : Entity_Id;
6476
6477 begin
6478 Desig := Directly_Designated_Type (T);
6479
6480 if Ekind (Desig) = E_Incomplete_Type then
6481
6482 -- If regular incomplete type, get full view if available
6483
6484 if Present (Full_View (Desig)) then
6485 Desig := Full_View (Desig);
6486
6487 -- If limited view of a type, get non-limited view if available,
6488 -- and check again for a regular incomplete type.
6489
6490 elsif Present (Non_Limited_View (Desig)) then
6491 Desig := Get_Full_View (Non_Limited_View (Desig));
6492 end if;
6493 end if;
6494
6495 return Desig;
6496 end Find_Designated_Type;
6497
0a36105d
JM
6498 -------------------------------
6499 -- Matches_Limited_With_View --
6500 -------------------------------
6501
6502 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
6503 begin
6504 -- In some cases a type imported through a limited_with clause, and
6505 -- its nonlimited view are both visible, for example in an anonymous
6506 -- access-to-class-wide type in a formal. Both entities designate the
6507 -- same type.
6508
7b56a91b 6509 if From_Limited_With (T1) and then T2 = Available_View (T1) then
aa720a54
AC
6510 return True;
6511
7b56a91b 6512 elsif From_Limited_With (T2) and then T1 = Available_View (T2) then
41251c60 6513 return True;
3e24afaa 6514
7b56a91b
AC
6515 elsif From_Limited_With (T1)
6516 and then From_Limited_With (T2)
3e24afaa
AC
6517 and then Available_View (T1) = Available_View (T2)
6518 then
6519 return True;
41251c60 6520
996ae0b0
RK
6521 else
6522 return False;
6523 end if;
0a36105d 6524 end Matches_Limited_With_View;
996ae0b0 6525
ec4867fa 6526 -- Start of processing for Conforming_Types
758c442c 6527
996ae0b0 6528 begin
8fde064e
AC
6529 -- The context is an instance association for a formal access-to-
6530 -- subprogram type; the formal parameter types require mapping because
6531 -- they may denote other formal parameters of the generic unit.
996ae0b0
RK
6532
6533 if Get_Inst then
6534 Type_1 := Get_Instance_Of (T1);
6535 Type_2 := Get_Instance_Of (T2);
6536 end if;
6537
0a36105d
JM
6538 -- If one of the types is a view of the other introduced by a limited
6539 -- with clause, treat these as conforming for all purposes.
996ae0b0 6540
0a36105d
JM
6541 if Matches_Limited_With_View (T1, T2) then
6542 return True;
6543
6544 elsif Base_Types_Match (Type_1, Type_2) then
996ae0b0
RK
6545 return Ctype <= Mode_Conformant
6546 or else Subtypes_Statically_Match (Type_1, Type_2);
6547
6548 elsif Is_Incomplete_Or_Private_Type (Type_1)
6549 and then Present (Full_View (Type_1))
6550 and then Base_Types_Match (Full_View (Type_1), Type_2)
6551 then
6552 return Ctype <= Mode_Conformant
6553 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
6554
6555 elsif Ekind (Type_2) = E_Incomplete_Type
6556 and then Present (Full_View (Type_2))
6557 and then Base_Types_Match (Type_1, Full_View (Type_2))
6558 then
6559 return Ctype <= Mode_Conformant
6560 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
fbf5a39b
AC
6561
6562 elsif Is_Private_Type (Type_2)
6563 and then In_Instance
6564 and then Present (Full_View (Type_2))
6565 and then Base_Types_Match (Type_1, Full_View (Type_2))
6566 then
6567 return Ctype <= Mode_Conformant
6568 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
996ae0b0
RK
6569 end if;
6570
0a36105d 6571 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
466c2127
AC
6572 -- treated recursively because they carry a signature. As far as
6573 -- conformance is concerned, convention plays no role, and either
6574 -- or both could be access to protected subprograms.
af4b9434
AC
6575
6576 Are_Anonymous_Access_To_Subprogram_Types :=
466c2127
AC
6577 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
6578 E_Anonymous_Access_Protected_Subprogram_Type)
f937473f 6579 and then
466c2127
AC
6580 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
6581 E_Anonymous_Access_Protected_Subprogram_Type);
af4b9434 6582
996ae0b0 6583 -- Test anonymous access type case. For this case, static subtype
5d37ba92
ES
6584 -- matching is required for mode conformance (RM 6.3.1(15)). We check
6585 -- the base types because we may have built internal subtype entities
6586 -- to handle null-excluding types (see Process_Formals).
996ae0b0 6587
5d37ba92
ES
6588 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
6589 and then
6590 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
8fde064e
AC
6591
6592 -- Ada 2005 (AI-254)
6593
6594 or else Are_Anonymous_Access_To_Subprogram_Types
996ae0b0
RK
6595 then
6596 declare
6597 Desig_1 : Entity_Id;
6598 Desig_2 : Entity_Id;
6599
6600 begin
885c4871 6601 -- In Ada 2005, access constant indicators must match for
5d37ba92 6602 -- subtype conformance.
9dcb52e1 6603
0791fbe9 6604 if Ada_Version >= Ada_2005
5d37ba92
ES
6605 and then Ctype >= Subtype_Conformant
6606 and then
6607 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
6608 then
6609 return False;
996ae0b0
RK
6610 end if;
6611
5d37ba92 6612 Desig_1 := Find_Designated_Type (Type_1);
5d37ba92 6613 Desig_2 := Find_Designated_Type (Type_2);
996ae0b0 6614
5d37ba92 6615 -- If the context is an instance association for a formal
82c80734
RD
6616 -- access-to-subprogram type; formal access parameter designated
6617 -- types require mapping because they may denote other formal
6618 -- parameters of the generic unit.
996ae0b0
RK
6619
6620 if Get_Inst then
6621 Desig_1 := Get_Instance_Of (Desig_1);
6622 Desig_2 := Get_Instance_Of (Desig_2);
6623 end if;
6624
82c80734
RD
6625 -- It is possible for a Class_Wide_Type to be introduced for an
6626 -- incomplete type, in which case there is a separate class_ wide
6627 -- type for the full view. The types conform if their Etypes
6628 -- conform, i.e. one may be the full view of the other. This can
6629 -- only happen in the context of an access parameter, other uses
6630 -- of an incomplete Class_Wide_Type are illegal.
996ae0b0 6631
fbf5a39b 6632 if Is_Class_Wide_Type (Desig_1)
4adf3c50
AC
6633 and then
6634 Is_Class_Wide_Type (Desig_2)
996ae0b0
RK
6635 then
6636 return
fbf5a39b
AC
6637 Conforming_Types
6638 (Etype (Base_Type (Desig_1)),
6639 Etype (Base_Type (Desig_2)), Ctype);
af4b9434
AC
6640
6641 elsif Are_Anonymous_Access_To_Subprogram_Types then
0791fbe9 6642 if Ada_Version < Ada_2005 then
758c442c
GD
6643 return Ctype = Type_Conformant
6644 or else
af4b9434
AC
6645 Subtypes_Statically_Match (Desig_1, Desig_2);
6646
758c442c
GD
6647 -- We must check the conformance of the signatures themselves
6648
6649 else
6650 declare
6651 Conformant : Boolean;
6652 begin
6653 Check_Conformance
6654 (Desig_1, Desig_2, Ctype, False, Conformant);
6655 return Conformant;
6656 end;
6657 end if;
6658
996ae0b0
RK
6659 else
6660 return Base_Type (Desig_1) = Base_Type (Desig_2)
6661 and then (Ctype = Type_Conformant
8fde064e
AC
6662 or else
6663 Subtypes_Statically_Match (Desig_1, Desig_2));
996ae0b0
RK
6664 end if;
6665 end;
6666
6667 -- Otherwise definitely no match
6668
6669 else
c8ef728f
ES
6670 if ((Ekind (Type_1) = E_Anonymous_Access_Type
6671 and then Is_Access_Type (Type_2))
6672 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8fde064e 6673 and then Is_Access_Type (Type_1)))
c8ef728f
ES
6674 and then
6675 Conforming_Types
6676 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
6677 then
6678 May_Hide_Profile := True;
6679 end if;
6680
996ae0b0
RK
6681 return False;
6682 end if;
996ae0b0
RK
6683 end Conforming_Types;
6684
6685 --------------------------
6686 -- Create_Extra_Formals --
6687 --------------------------
6688
6689 procedure Create_Extra_Formals (E : Entity_Id) is
6690 Formal : Entity_Id;
ec4867fa 6691 First_Extra : Entity_Id := Empty;
996ae0b0
RK
6692 Last_Extra : Entity_Id;
6693 Formal_Type : Entity_Id;
6694 P_Formal : Entity_Id := Empty;
6695
ec4867fa
ES
6696 function Add_Extra_Formal
6697 (Assoc_Entity : Entity_Id;
6698 Typ : Entity_Id;
6699 Scope : Entity_Id;
6700 Suffix : String) return Entity_Id;
6701 -- Add an extra formal to the current list of formals and extra formals.
6702 -- The extra formal is added to the end of the list of extra formals,
6703 -- and also returned as the result. These formals are always of mode IN.
6704 -- The new formal has the type Typ, is declared in Scope, and its name
6705 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
cd5a9750
AC
6706 -- The following suffixes are currently used. They should not be changed
6707 -- without coordinating with CodePeer, which makes use of these to
6708 -- provide better messages.
6709
d92eccc3
AC
6710 -- O denotes the Constrained bit.
6711 -- L denotes the accessibility level.
cd5a9750
AC
6712 -- BIP_xxx denotes an extra formal for a build-in-place function. See
6713 -- the full list in exp_ch6.BIP_Formal_Kind.
996ae0b0 6714
fbf5a39b
AC
6715 ----------------------
6716 -- Add_Extra_Formal --
6717 ----------------------
6718
ec4867fa
ES
6719 function Add_Extra_Formal
6720 (Assoc_Entity : Entity_Id;
6721 Typ : Entity_Id;
6722 Scope : Entity_Id;
6723 Suffix : String) return Entity_Id
6724 is
996ae0b0 6725 EF : constant Entity_Id :=
ec4867fa
ES
6726 Make_Defining_Identifier (Sloc (Assoc_Entity),
6727 Chars => New_External_Name (Chars (Assoc_Entity),
f937473f 6728 Suffix => Suffix));
996ae0b0
RK
6729
6730 begin
82c80734
RD
6731 -- A little optimization. Never generate an extra formal for the
6732 -- _init operand of an initialization procedure, since it could
6733 -- never be used.
996ae0b0
RK
6734
6735 if Chars (Formal) = Name_uInit then
6736 return Empty;
6737 end if;
6738
6739 Set_Ekind (EF, E_In_Parameter);
6740 Set_Actual_Subtype (EF, Typ);
6741 Set_Etype (EF, Typ);
ec4867fa 6742 Set_Scope (EF, Scope);
996ae0b0
RK
6743 Set_Mechanism (EF, Default_Mechanism);
6744 Set_Formal_Validity (EF);
6745
ec4867fa
ES
6746 if No (First_Extra) then
6747 First_Extra := EF;
6748 Set_Extra_Formals (Scope, First_Extra);
6749 end if;
6750
6751 if Present (Last_Extra) then
6752 Set_Extra_Formal (Last_Extra, EF);
6753 end if;
6754
996ae0b0 6755 Last_Extra := EF;
ec4867fa 6756
996ae0b0
RK
6757 return EF;
6758 end Add_Extra_Formal;
6759
6760 -- Start of processing for Create_Extra_Formals
6761
6762 begin
8fde064e
AC
6763 -- We never generate extra formals if expansion is not active because we
6764 -- don't need them unless we are generating code.
f937473f
RD
6765
6766 if not Expander_Active then
6767 return;
6768 end if;
6769
e2441021
AC
6770 -- No need to generate extra formals in interface thunks whose target
6771 -- primitive has no extra formals.
6772
6773 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
6774 return;
6775 end if;
6776
82c80734 6777 -- If this is a derived subprogram then the subtypes of the parent
16b05213 6778 -- subprogram's formal parameters will be used to determine the need
82c80734 6779 -- for extra formals.
996ae0b0
RK
6780
6781 if Is_Overloadable (E) and then Present (Alias (E)) then
6782 P_Formal := First_Formal (Alias (E));
6783 end if;
6784
6785 Last_Extra := Empty;
6786 Formal := First_Formal (E);
6787 while Present (Formal) loop
6788 Last_Extra := Formal;
6789 Next_Formal (Formal);
6790 end loop;
6791
f937473f 6792 -- If Extra_formals were already created, don't do it again. This
82c80734
RD
6793 -- situation may arise for subprogram types created as part of
6794 -- dispatching calls (see Expand_Dispatching_Call)
996ae0b0 6795
8fde064e 6796 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
996ae0b0
RK
6797 return;
6798 end if;
6799
19590d70
GD
6800 -- If the subprogram is a predefined dispatching subprogram then don't
6801 -- generate any extra constrained or accessibility level formals. In
6802 -- general we suppress these for internal subprograms (by not calling
6803 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
6804 -- generated stream attributes do get passed through because extra
6805 -- build-in-place formals are needed in some cases (limited 'Input).
6806
bac7206d 6807 if Is_Predefined_Internal_Operation (E) then
63585f75 6808 goto Test_For_Func_Result_Extras;
19590d70
GD
6809 end if;
6810
996ae0b0 6811 Formal := First_Formal (E);
996ae0b0
RK
6812 while Present (Formal) loop
6813
6814 -- Create extra formal for supporting the attribute 'Constrained.
6815 -- The case of a private type view without discriminants also
6816 -- requires the extra formal if the underlying type has defaulted
6817 -- discriminants.
6818
6819 if Ekind (Formal) /= E_In_Parameter then
6820 if Present (P_Formal) then
6821 Formal_Type := Etype (P_Formal);
6822 else
6823 Formal_Type := Etype (Formal);
6824 end if;
6825
5d09245e
AC
6826 -- Do not produce extra formals for Unchecked_Union parameters.
6827 -- Jump directly to the end of the loop.
6828
6829 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
6830 goto Skip_Extra_Formal_Generation;
6831 end if;
6832
996ae0b0
RK
6833 if not Has_Discriminants (Formal_Type)
6834 and then Ekind (Formal_Type) in Private_Kind
6835 and then Present (Underlying_Type (Formal_Type))
6836 then
6837 Formal_Type := Underlying_Type (Formal_Type);
6838 end if;
6839
5e5db3b4
GD
6840 -- Suppress the extra formal if formal's subtype is constrained or
6841 -- indefinite, or we're compiling for Ada 2012 and the underlying
6842 -- type is tagged and limited. In Ada 2012, a limited tagged type
6843 -- can have defaulted discriminants, but 'Constrained is required
6844 -- to return True, so the formal is never needed (see AI05-0214).
6845 -- Note that this ensures consistency of calling sequences for
6846 -- dispatching operations when some types in a class have defaults
6847 -- on discriminants and others do not (and requiring the extra
6848 -- formal would introduce distributed overhead).
6849
b5bdffcc
AC
6850 -- If the type does not have a completion yet, treat as prior to
6851 -- Ada 2012 for consistency.
6852
996ae0b0 6853 if Has_Discriminants (Formal_Type)
f937473f
RD
6854 and then not Is_Constrained (Formal_Type)
6855 and then not Is_Indefinite_Subtype (Formal_Type)
5e5db3b4 6856 and then (Ada_Version < Ada_2012
b5bdffcc
AC
6857 or else No (Underlying_Type (Formal_Type))
6858 or else not
6859 (Is_Limited_Type (Formal_Type)
6860 and then
6861 (Is_Tagged_Type
6862 (Underlying_Type (Formal_Type)))))
996ae0b0
RK
6863 then
6864 Set_Extra_Constrained
d92eccc3 6865 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
996ae0b0
RK
6866 end if;
6867 end if;
6868
0a36105d
JM
6869 -- Create extra formal for supporting accessibility checking. This
6870 -- is done for both anonymous access formals and formals of named
6871 -- access types that are marked as controlling formals. The latter
6872 -- case can occur when Expand_Dispatching_Call creates a subprogram
6873 -- type and substitutes the types of access-to-class-wide actuals
6874 -- for the anonymous access-to-specific-type of controlling formals.
5d37ba92
ES
6875 -- Base_Type is applied because in cases where there is a null
6876 -- exclusion the formal may have an access subtype.
996ae0b0
RK
6877
6878 -- This is suppressed if we specifically suppress accessibility
f937473f 6879 -- checks at the package level for either the subprogram, or the
fbf5a39b
AC
6880 -- package in which it resides. However, we do not suppress it
6881 -- simply if the scope has accessibility checks suppressed, since
6882 -- this could cause trouble when clients are compiled with a
6883 -- different suppression setting. The explicit checks at the
6884 -- package level are safe from this point of view.
996ae0b0 6885
5d37ba92 6886 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
0a36105d 6887 or else (Is_Controlling_Formal (Formal)
5d37ba92 6888 and then Is_Access_Type (Base_Type (Etype (Formal)))))
996ae0b0 6889 and then not
fbf5a39b 6890 (Explicit_Suppress (E, Accessibility_Check)
996ae0b0 6891 or else
fbf5a39b 6892 Explicit_Suppress (Scope (E), Accessibility_Check))
996ae0b0 6893 and then
c8ef728f 6894 (No (P_Formal)
996ae0b0
RK
6895 or else Present (Extra_Accessibility (P_Formal)))
6896 then
811c6a85 6897 Set_Extra_Accessibility
d92eccc3 6898 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
996ae0b0
RK
6899 end if;
6900
5d09245e
AC
6901 -- This label is required when skipping extra formal generation for
6902 -- Unchecked_Union parameters.
6903
6904 <<Skip_Extra_Formal_Generation>>
6905
f937473f
RD
6906 if Present (P_Formal) then
6907 Next_Formal (P_Formal);
6908 end if;
6909
996ae0b0
RK
6910 Next_Formal (Formal);
6911 end loop;
ec4867fa 6912
63585f75
SB
6913 <<Test_For_Func_Result_Extras>>
6914
6915 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
6916 -- function call is ... determined by the point of call ...".
6917
6918 if Needs_Result_Accessibility_Level (E) then
6919 Set_Extra_Accessibility_Of_Result
6920 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
6921 end if;
19590d70 6922
ec4867fa 6923 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
f937473f
RD
6924 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
6925
0791fbe9 6926 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
ec4867fa 6927 declare
f937473f 6928 Result_Subt : constant Entity_Id := Etype (E);
1a36a0cd 6929 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
2fcc44fa 6930 Formal_Typ : Entity_Id;
f937473f 6931
2fcc44fa 6932 Discard : Entity_Id;
f937473f 6933 pragma Warnings (Off, Discard);
ec4867fa
ES
6934
6935 begin
f937473f 6936 -- In the case of functions with unconstrained result subtypes,
9a1bc6d5
AC
6937 -- add a 4-state formal indicating whether the return object is
6938 -- allocated by the caller (1), or should be allocated by the
6939 -- callee on the secondary stack (2), in the global heap (3), or
6940 -- in a user-defined storage pool (4). For the moment we just use
6941 -- Natural for the type of this formal. Note that this formal
6942 -- isn't usually needed in the case where the result subtype is
6943 -- constrained, but it is needed when the function has a tagged
6944 -- result, because generally such functions can be called in a
6945 -- dispatching context and such calls must be handled like calls
6946 -- to a class-wide function.
0a36105d 6947
1bb6e262 6948 if Needs_BIP_Alloc_Form (E) then
f937473f
RD
6949 Discard :=
6950 Add_Extra_Formal
6951 (E, Standard_Natural,
6952 E, BIP_Formal_Suffix (BIP_Alloc_Form));
200b7162 6953
8417f4b2 6954 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
3e452820
AC
6955 -- use a user-defined pool. This formal is not added on
6956 -- .NET/JVM/ZFP as those targets do not support pools.
200b7162 6957
ea10ca9c
AC
6958 if VM_Target = No_VM
6959 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
3e452820 6960 then
8417f4b2
AC
6961 Discard :=
6962 Add_Extra_Formal
6963 (E, RTE (RE_Root_Storage_Pool_Ptr),
6964 E, BIP_Formal_Suffix (BIP_Storage_Pool));
6965 end if;
f937473f 6966 end if;
ec4867fa 6967
df3e68b1 6968 -- In the case of functions whose result type needs finalization,
ca5af305 6969 -- add an extra formal which represents the finalization master.
df3e68b1 6970
ca5af305 6971 if Needs_BIP_Finalization_Master (E) then
f937473f
RD
6972 Discard :=
6973 Add_Extra_Formal
ca5af305
AC
6974 (E, RTE (RE_Finalization_Master_Ptr),
6975 E, BIP_Formal_Suffix (BIP_Finalization_Master));
f937473f
RD
6976 end if;
6977
94bbf008
AC
6978 -- When the result type contains tasks, add two extra formals: the
6979 -- master of the tasks to be created, and the caller's activation
6980 -- chain.
f937473f 6981
1a36a0cd 6982 if Has_Task (Full_Subt) then
f937473f
RD
6983 Discard :=
6984 Add_Extra_Formal
6985 (E, RTE (RE_Master_Id),
af89615f 6986 E, BIP_Formal_Suffix (BIP_Task_Master));
f937473f
RD
6987 Discard :=
6988 Add_Extra_Formal
6989 (E, RTE (RE_Activation_Chain_Access),
6990 E, BIP_Formal_Suffix (BIP_Activation_Chain));
6991 end if;
ec4867fa 6992
f937473f
RD
6993 -- All build-in-place functions get an extra formal that will be
6994 -- passed the address of the return object within the caller.
ec4867fa 6995
1a36a0cd
AC
6996 Formal_Typ :=
6997 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
ec4867fa 6998
1a36a0cd
AC
6999 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
7000 Set_Etype (Formal_Typ, Formal_Typ);
7001 Set_Depends_On_Private
7002 (Formal_Typ, Has_Private_Component (Formal_Typ));
7003 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
7004 Set_Is_Access_Constant (Formal_Typ, False);
ec4867fa 7005
1a36a0cd
AC
7006 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
7007 -- the designated type comes from the limited view (for back-end
7008 -- purposes).
ec4867fa 7009
7b56a91b
AC
7010 Set_From_Limited_With
7011 (Formal_Typ, From_Limited_With (Result_Subt));
f937473f 7012
1a36a0cd
AC
7013 Layout_Type (Formal_Typ);
7014
7015 Discard :=
7016 Add_Extra_Formal
7017 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
ec4867fa
ES
7018 end;
7019 end if;
996ae0b0
RK
7020 end Create_Extra_Formals;
7021
7022 -----------------------------
7023 -- Enter_Overloaded_Entity --
7024 -----------------------------
7025
7026 procedure Enter_Overloaded_Entity (S : Entity_Id) is
7027 E : Entity_Id := Current_Entity_In_Scope (S);
7028 C_E : Entity_Id := Current_Entity (S);
7029
7030 begin
7031 if Present (E) then
7032 Set_Has_Homonym (E);
7033 Set_Has_Homonym (S);
7034 end if;
7035
7036 Set_Is_Immediately_Visible (S);
7037 Set_Scope (S, Current_Scope);
7038
7039 -- Chain new entity if front of homonym in current scope, so that
7040 -- homonyms are contiguous.
7041
8fde064e 7042 if Present (E) and then E /= C_E then
996ae0b0
RK
7043 while Homonym (C_E) /= E loop
7044 C_E := Homonym (C_E);
7045 end loop;
7046
7047 Set_Homonym (C_E, S);
7048
7049 else
7050 E := C_E;
7051 Set_Current_Entity (S);
7052 end if;
7053
7054 Set_Homonym (S, E);
7055
2352eadb
AC
7056 if Is_Inherited_Operation (S) then
7057 Append_Inherited_Subprogram (S);
7058 else
7059 Append_Entity (S, Current_Scope);
7060 end if;
7061
996ae0b0
RK
7062 Set_Public_Status (S);
7063
7064 if Debug_Flag_E then
7065 Write_Str ("New overloaded entity chain: ");
7066 Write_Name (Chars (S));
996ae0b0 7067
82c80734 7068 E := S;
996ae0b0
RK
7069 while Present (E) loop
7070 Write_Str (" "); Write_Int (Int (E));
7071 E := Homonym (E);
7072 end loop;
7073
7074 Write_Eol;
7075 end if;
7076
7077 -- Generate warning for hiding
7078
7079 if Warn_On_Hiding
7080 and then Comes_From_Source (S)
7081 and then In_Extended_Main_Source_Unit (S)
7082 then
7083 E := S;
7084 loop
7085 E := Homonym (E);
7086 exit when No (E);
7087
7fc53871
AC
7088 -- Warn unless genuine overloading. Do not emit warning on
7089 -- hiding predefined operators in Standard (these are either an
7090 -- (artifact of our implicit declarations, or simple noise) but
7091 -- keep warning on a operator defined on a local subtype, because
7092 -- of the real danger that different operators may be applied in
7093 -- various parts of the program.
996ae0b0 7094
1f250383
AC
7095 -- Note that if E and S have the same scope, there is never any
7096 -- hiding. Either the two conflict, and the program is illegal,
7097 -- or S is overriding an implicit inherited subprogram.
7098
7099 if Scope (E) /= Scope (S)
7100 and then (not Is_Overloadable (E)
8d606a78 7101 or else Subtype_Conformant (E, S))
f937473f
RD
7102 and then (Is_Immediately_Visible (E)
7103 or else
7104 Is_Potentially_Use_Visible (S))
996ae0b0 7105 then
7fc53871
AC
7106 if Scope (E) /= Standard_Standard then
7107 Error_Msg_Sloc := Sloc (E);
3ccedacc 7108 Error_Msg_N ("declaration of & hides one #?h?", S);
7fc53871
AC
7109
7110 elsif Nkind (S) = N_Defining_Operator_Symbol
7111 and then
1f250383 7112 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
7fc53871
AC
7113 then
7114 Error_Msg_N
dbfeb4fa 7115 ("declaration of & hides predefined operator?h?", S);
7fc53871 7116 end if;
996ae0b0
RK
7117 end if;
7118 end loop;
7119 end if;
7120 end Enter_Overloaded_Entity;
7121
e5a58fac
AC
7122 -----------------------------
7123 -- Check_Untagged_Equality --
7124 -----------------------------
7125
7126 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
7127 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
7128 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
7129 Obj_Decl : Node_Id;
7130
7131 begin
7c0c194b
AC
7132 -- This check applies only if we have a subprogram declaration with an
7133 -- untagged record type.
b2834fbd
AC
7134
7135 if Nkind (Decl) /= N_Subprogram_Declaration
7136 or else not Is_Record_Type (Typ)
7137 or else Is_Tagged_Type (Typ)
e5a58fac 7138 then
b2834fbd
AC
7139 return;
7140 end if;
e5a58fac 7141
b2834fbd
AC
7142 -- In Ada 2012 case, we will output errors or warnings depending on
7143 -- the setting of debug flag -gnatd.E.
7144
7145 if Ada_Version >= Ada_2012 then
7146 Error_Msg_Warn := Debug_Flag_Dot_EE;
7147
7148 -- In earlier versions of Ada, nothing to do unless we are warning on
7149 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
7150
7151 else
7152 if not Warn_On_Ada_2012_Compatibility then
7153 return;
7154 end if;
7155 end if;
7156
7157 -- Cases where the type has already been frozen
e5a58fac 7158
b2834fbd
AC
7159 if Is_Frozen (Typ) then
7160
7161 -- If the type is not declared in a package, or if we are in the body
7162 -- of the package or in some other scope, the new operation is not
7163 -- primitive, and therefore legal, though suspicious. Should we
7164 -- generate a warning in this case ???
7165
7166 if Ekind (Scope (Typ)) /= E_Package
7167 or else Scope (Typ) /= Current_Scope
7168 then
7169 return;
7170
7171 -- If the type is a generic actual (sub)type, the operation is not
7172 -- primitive either because the base type is declared elsewhere.
7173
7174 elsif Is_Generic_Actual_Type (Typ) then
7175 return;
7176
7177 -- Here we have a definite error of declaration after freezing
7178
7179 else
7180 if Ada_Version >= Ada_2012 then
ae6ede77 7181 Error_Msg_NE
3ccedacc 7182 ("equality operator must be declared before type & is "
b2834fbd
AC
7183 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
7184
7185 -- In Ada 2012 mode with error turned to warning, output one
7186 -- more warning to warn that the equality operation may not
7187 -- compose. This is the consequence of ignoring the error.
7188
7189 if Error_Msg_Warn then
7190 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
7191 end if;
21a5b575
AC
7192
7193 else
7194 Error_Msg_NE
b2834fbd
AC
7195 ("equality operator must be declared before type& is "
7196 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
7197 end if;
7198
7199 -- If we are in the package body, we could just move the
7200 -- declaration to the package spec, so add a message saying that.
7201
7202 if In_Package_Body (Scope (Typ)) then
7203 if Ada_Version >= Ada_2012 then
7204 Error_Msg_N
7205 ("\move declaration to package spec<<", Eq_Op);
7206 else
7207 Error_Msg_N
7208 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
7209 end if;
21a5b575 7210
b2834fbd
AC
7211 -- Otherwise try to find the freezing point
7212
7213 else
21a5b575 7214 Obj_Decl := Next (Parent (Typ));
dbfeb4fa 7215 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
21a5b575
AC
7216 if Nkind (Obj_Decl) = N_Object_Declaration
7217 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
7218 then
b2834fbd
AC
7219 -- Freezing point, output warnings
7220
7221 if Ada_Version >= Ada_2012 then
7222 Error_Msg_NE
7223 ("type& is frozen by declaration??", Obj_Decl, Typ);
7224 Error_Msg_N
7225 ("\an equality operator cannot be declared after "
7226 & "this point??",
7227 Obj_Decl);
7228 else
7229 Error_Msg_NE
7230 ("type& is frozen by declaration (Ada 2012)?y?",
7231 Obj_Decl, Typ);
7232 Error_Msg_N
7233 ("\an equality operator cannot be declared after "
7234 & "this point (Ada 2012)?y?",
7235 Obj_Decl);
7236 end if;
7237
21a5b575
AC
7238 exit;
7239 end if;
7240
7241 Next (Obj_Decl);
7242 end loop;
7243 end if;
b2834fbd 7244 end if;
e5a58fac 7245
b2834fbd
AC
7246 -- Here if type is not frozen yet. It is illegal to have a primitive
7247 -- equality declared in the private part if the type is visible.
21a5b575 7248
b2834fbd
AC
7249 elsif not In_Same_List (Parent (Typ), Decl)
7250 and then not Is_Limited_Type (Typ)
7251 then
7252 -- Shouldn't we give an RM reference here???
21a5b575 7253
b2834fbd
AC
7254 if Ada_Version >= Ada_2012 then
7255 Error_Msg_N
7256 ("equality operator appears too late<<", Eq_Op);
7257 else
7258 Error_Msg_N
7259 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
e5a58fac 7260 end if;
b2834fbd
AC
7261
7262 -- No error detected
7263
7264 else
7265 return;
e5a58fac
AC
7266 end if;
7267 end Check_Untagged_Equality;
7268
996ae0b0
RK
7269 -----------------------------
7270 -- Find_Corresponding_Spec --
7271 -----------------------------
7272
d44202ba
HK
7273 function Find_Corresponding_Spec
7274 (N : Node_Id;
7275 Post_Error : Boolean := True) return Entity_Id
7276 is
996ae0b0
RK
7277 Spec : constant Node_Id := Specification (N);
7278 Designator : constant Entity_Id := Defining_Entity (Spec);
7279
7280 E : Entity_Id;
7281
70f4ad20
AC
7282 function Different_Generic_Profile (E : Entity_Id) return Boolean;
7283 -- Even if fully conformant, a body may depend on a generic actual when
7284 -- the spec does not, or vice versa, in which case they were distinct
7285 -- entities in the generic.
7286
7287 -------------------------------
7288 -- Different_Generic_Profile --
7289 -------------------------------
7290
7291 function Different_Generic_Profile (E : Entity_Id) return Boolean is
7292 F1, F2 : Entity_Id;
7293
2995860f
AC
7294 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
7295 -- Check that the types of corresponding formals have the same
7296 -- generic actual if any. We have to account for subtypes of a
7297 -- generic formal, declared between a spec and a body, which may
73999267
AC
7298 -- appear distinct in an instance but matched in the generic, and
7299 -- the subtype may be used either in the spec or the body of the
7300 -- subprogram being checked.
2995860f
AC
7301
7302 -------------------------
7303 -- Same_Generic_Actual --
7304 -------------------------
7305
7306 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
73999267
AC
7307
7308 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
7309 -- Predicate to check whether S1 is a subtype of S2 in the source
7310 -- of the instance.
7311
7312 -------------------------
7313 -- Is_Declared_Subtype --
7314 -------------------------
7315
7316 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
7317 begin
7318 return Comes_From_Source (Parent (S1))
7319 and then Nkind (Parent (S1)) = N_Subtype_Declaration
7320 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
7321 and then Entity (Subtype_Indication (Parent (S1))) = S2;
7322 end Is_Declared_Subtype;
7323
7324 -- Start of processing for Same_Generic_Actual
7325
2995860f
AC
7326 begin
7327 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
73999267
AC
7328 or else Is_Declared_Subtype (T1, T2)
7329 or else Is_Declared_Subtype (T2, T1);
2995860f
AC
7330 end Same_Generic_Actual;
7331
7332 -- Start of processing for Different_Generic_Profile
7333
70f4ad20 7334 begin
2995860f
AC
7335 if not In_Instance then
7336 return False;
7337
7338 elsif Ekind (E) = E_Function
7339 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
70f4ad20
AC
7340 then
7341 return True;
7342 end if;
7343
7344 F1 := First_Formal (Designator);
7345 F2 := First_Formal (E);
70f4ad20 7346 while Present (F1) loop
2995860f 7347 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
70f4ad20
AC
7348 return True;
7349 end if;
7350
7351 Next_Formal (F1);
7352 Next_Formal (F2);
7353 end loop;
7354
7355 return False;
7356 end Different_Generic_Profile;
7357
7358 -- Start of processing for Find_Corresponding_Spec
7359
996ae0b0
RK
7360 begin
7361 E := Current_Entity (Designator);
996ae0b0
RK
7362 while Present (E) loop
7363
7364 -- We are looking for a matching spec. It must have the same scope,
7365 -- and the same name, and either be type conformant, or be the case
7366 -- of a library procedure spec and its body (which belong to one
7367 -- another regardless of whether they are type conformant or not).
7368
7369 if Scope (E) = Current_Scope then
fbf5a39b
AC
7370 if Current_Scope = Standard_Standard
7371 or else (Ekind (E) = Ekind (Designator)
586ecbf3 7372 and then Type_Conformant (E, Designator))
996ae0b0
RK
7373 then
7374 -- Within an instantiation, we know that spec and body are
70f4ad20
AC
7375 -- subtype conformant, because they were subtype conformant in
7376 -- the generic. We choose the subtype-conformant entity here as
7377 -- well, to resolve spurious ambiguities in the instance that
7378 -- were not present in the generic (i.e. when two different
7379 -- types are given the same actual). If we are looking for a
7380 -- spec to match a body, full conformance is expected.
996ae0b0
RK
7381
7382 if In_Instance then
7383 Set_Convention (Designator, Convention (E));
7384
0187b60e
AC
7385 -- Skip past subprogram bodies and subprogram renamings that
7386 -- may appear to have a matching spec, but that aren't fully
7387 -- conformant with it. That can occur in cases where an
7388 -- actual type causes unrelated homographs in the instance.
7389
7390 if Nkind_In (N, N_Subprogram_Body,
7391 N_Subprogram_Renaming_Declaration)
996ae0b0 7392 and then Present (Homonym (E))
c7b9d548 7393 and then not Fully_Conformant (Designator, E)
996ae0b0
RK
7394 then
7395 goto Next_Entity;
7396
c7b9d548 7397 elsif not Subtype_Conformant (Designator, E) then
996ae0b0 7398 goto Next_Entity;
70f4ad20
AC
7399
7400 elsif Different_Generic_Profile (E) then
7401 goto Next_Entity;
996ae0b0
RK
7402 end if;
7403 end if;
7404
25ebc085
AC
7405 -- Ada 2012 (AI05-0165): For internally generated bodies of
7406 -- null procedures locate the internally generated spec. We
7407 -- enforce mode conformance since a tagged type may inherit
7408 -- from interfaces several null primitives which differ only
7409 -- in the mode of the formals.
7410
7411 if not (Comes_From_Source (E))
7412 and then Is_Null_Procedure (E)
7413 and then not Mode_Conformant (Designator, E)
7414 then
7415 null;
7416
4d8f3296
ES
7417 -- For null procedures coming from source that are completions,
7418 -- analysis of the generated body will establish the link.
7419
7420 elsif Comes_From_Source (E)
7421 and then Nkind (Spec) = N_Procedure_Specification
7422 and then Null_Present (Spec)
7423 then
7424 return E;
7425
25ebc085 7426 elsif not Has_Completion (E) then
996ae0b0
RK
7427 if Nkind (N) /= N_Subprogram_Body_Stub then
7428 Set_Corresponding_Spec (N, E);
7429 end if;
7430
7431 Set_Has_Completion (E);
7432 return E;
7433
7434 elsif Nkind (Parent (N)) = N_Subunit then
7435
7436 -- If this is the proper body of a subunit, the completion
7437 -- flag is set when analyzing the stub.
7438
7439 return E;
7440
70f4ad20
AC
7441 -- If E is an internal function with a controlling result that
7442 -- was created for an operation inherited by a null extension,
7443 -- it may be overridden by a body without a previous spec (one
2995860f 7444 -- more reason why these should be shunned). In that case we
70f4ad20
AC
7445 -- remove the generated body if present, because the current
7446 -- one is the explicit overriding.
81db9d77
ES
7447
7448 elsif Ekind (E) = E_Function
0791fbe9 7449 and then Ada_Version >= Ada_2005
81db9d77
ES
7450 and then not Comes_From_Source (E)
7451 and then Has_Controlling_Result (E)
7452 and then Is_Null_Extension (Etype (E))
7453 and then Comes_From_Source (Spec)
7454 then
7455 Set_Has_Completion (E, False);
7456
1366997b
AC
7457 if Expander_Active
7458 and then Nkind (Parent (E)) = N_Function_Specification
7459 then
81db9d77
ES
7460 Remove
7461 (Unit_Declaration_Node
1366997b
AC
7462 (Corresponding_Body (Unit_Declaration_Node (E))));
7463
81db9d77
ES
7464 return E;
7465
1366997b
AC
7466 -- If expansion is disabled, or if the wrapper function has
7467 -- not been generated yet, this a late body overriding an
7468 -- inherited operation, or it is an overriding by some other
7469 -- declaration before the controlling result is frozen. In
7470 -- either case this is a declaration of a new entity.
81db9d77
ES
7471
7472 else
7473 return Empty;
7474 end if;
7475
d44202ba
HK
7476 -- If the body already exists, then this is an error unless
7477 -- the previous declaration is the implicit declaration of a
756ef2a0
AC
7478 -- derived subprogram. It is also legal for an instance to
7479 -- contain type conformant overloadable declarations (but the
7480 -- generic declaration may not), per 8.3(26/2).
996ae0b0
RK
7481
7482 elsif No (Alias (E))
7483 and then not Is_Intrinsic_Subprogram (E)
7484 and then not In_Instance
d44202ba 7485 and then Post_Error
996ae0b0
RK
7486 then
7487 Error_Msg_Sloc := Sloc (E);
8dbd1460 7488
07fc65c4
GB
7489 if Is_Imported (E) then
7490 Error_Msg_NE
7491 ("body not allowed for imported subprogram & declared#",
7492 N, E);
7493 else
7494 Error_Msg_NE ("duplicate body for & declared#", N, E);
7495 end if;
996ae0b0
RK
7496 end if;
7497
d44202ba
HK
7498 -- Child units cannot be overloaded, so a conformance mismatch
7499 -- between body and a previous spec is an error.
7500
996ae0b0
RK
7501 elsif Is_Child_Unit (E)
7502 and then
7503 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
7504 and then
5d37ba92 7505 Nkind (Parent (Unit_Declaration_Node (Designator))) =
d44202ba
HK
7506 N_Compilation_Unit
7507 and then Post_Error
996ae0b0 7508 then
996ae0b0
RK
7509 Error_Msg_N
7510 ("body of child unit does not match previous declaration", N);
7511 end if;
7512 end if;
7513
7514 <<Next_Entity>>
7515 E := Homonym (E);
7516 end loop;
7517
7518 -- On exit, we know that no previous declaration of subprogram exists
7519
7520 return Empty;
7521 end Find_Corresponding_Spec;
7522
7523 ----------------------
7524 -- Fully_Conformant --
7525 ----------------------
7526
7527 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7528 Result : Boolean;
996ae0b0
RK
7529 begin
7530 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
7531 return Result;
7532 end Fully_Conformant;
7533
7534 ----------------------------------
7535 -- Fully_Conformant_Expressions --
7536 ----------------------------------
7537
7538 function Fully_Conformant_Expressions
7539 (Given_E1 : Node_Id;
d05ef0ab 7540 Given_E2 : Node_Id) return Boolean
996ae0b0
RK
7541 is
7542 E1 : constant Node_Id := Original_Node (Given_E1);
7543 E2 : constant Node_Id := Original_Node (Given_E2);
7544 -- We always test conformance on original nodes, since it is possible
7545 -- for analysis and/or expansion to make things look as though they
7546 -- conform when they do not, e.g. by converting 1+2 into 3.
7547
7548 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
7549 renames Fully_Conformant_Expressions;
7550
7551 function FCL (L1, L2 : List_Id) return Boolean;
70f4ad20
AC
7552 -- Compare elements of two lists for conformance. Elements have to be
7553 -- conformant, and actuals inserted as default parameters do not match
7554 -- explicit actuals with the same value.
996ae0b0
RK
7555
7556 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
e895b435 7557 -- Compare an operator node with a function call
996ae0b0
RK
7558
7559 ---------
7560 -- FCL --
7561 ---------
7562
7563 function FCL (L1, L2 : List_Id) return Boolean is
7564 N1, N2 : Node_Id;
7565
7566 begin
7567 if L1 = No_List then
7568 N1 := Empty;
7569 else
7570 N1 := First (L1);
7571 end if;
7572
7573 if L2 = No_List then
7574 N2 := Empty;
7575 else
7576 N2 := First (L2);
7577 end if;
7578
70f4ad20 7579 -- Compare two lists, skipping rewrite insertions (we want to compare
a90bd866 7580 -- the original trees, not the expanded versions).
996ae0b0
RK
7581
7582 loop
7583 if Is_Rewrite_Insertion (N1) then
7584 Next (N1);
7585 elsif Is_Rewrite_Insertion (N2) then
7586 Next (N2);
7587 elsif No (N1) then
7588 return No (N2);
7589 elsif No (N2) then
7590 return False;
7591 elsif not FCE (N1, N2) then
7592 return False;
7593 else
7594 Next (N1);
7595 Next (N2);
7596 end if;
7597 end loop;
7598 end FCL;
7599
7600 ---------
7601 -- FCO --
7602 ---------
7603
7604 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
7605 Actuals : constant List_Id := Parameter_Associations (Call_Node);
7606 Act : Node_Id;
7607
7608 begin
7609 if No (Actuals)
7610 or else Entity (Op_Node) /= Entity (Name (Call_Node))
7611 then
7612 return False;
7613
7614 else
7615 Act := First (Actuals);
7616
7617 if Nkind (Op_Node) in N_Binary_Op then
996ae0b0
RK
7618 if not FCE (Left_Opnd (Op_Node), Act) then
7619 return False;
7620 end if;
7621
7622 Next (Act);
7623 end if;
7624
7625 return Present (Act)
7626 and then FCE (Right_Opnd (Op_Node), Act)
7627 and then No (Next (Act));
7628 end if;
7629 end FCO;
7630
7631 -- Start of processing for Fully_Conformant_Expressions
7632
7633 begin
7634 -- Non-conformant if paren count does not match. Note: if some idiot
7635 -- complains that we don't do this right for more than 3 levels of
a90bd866 7636 -- parentheses, they will be treated with the respect they deserve.
996ae0b0
RK
7637
7638 if Paren_Count (E1) /= Paren_Count (E2) then
7639 return False;
7640
82c80734
RD
7641 -- If same entities are referenced, then they are conformant even if
7642 -- they have different forms (RM 8.3.1(19-20)).
996ae0b0
RK
7643
7644 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
7645 if Present (Entity (E1)) then
7646 return Entity (E1) = Entity (E2)
7647 or else (Chars (Entity (E1)) = Chars (Entity (E2))
7648 and then Ekind (Entity (E1)) = E_Discriminant
7649 and then Ekind (Entity (E2)) = E_In_Parameter);
7650
7651 elsif Nkind (E1) = N_Expanded_Name
7652 and then Nkind (E2) = N_Expanded_Name
7653 and then Nkind (Selector_Name (E1)) = N_Character_Literal
7654 and then Nkind (Selector_Name (E2)) = N_Character_Literal
7655 then
7656 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
7657
7658 else
7659 -- Identifiers in component associations don't always have
7660 -- entities, but their names must conform.
7661
7662 return Nkind (E1) = N_Identifier
7663 and then Nkind (E2) = N_Identifier
7664 and then Chars (E1) = Chars (E2);
7665 end if;
7666
7667 elsif Nkind (E1) = N_Character_Literal
7668 and then Nkind (E2) = N_Expanded_Name
7669 then
7670 return Nkind (Selector_Name (E2)) = N_Character_Literal
7671 and then Chars (E1) = Chars (Selector_Name (E2));
7672
7673 elsif Nkind (E2) = N_Character_Literal
7674 and then Nkind (E1) = N_Expanded_Name
7675 then
7676 return Nkind (Selector_Name (E1)) = N_Character_Literal
7677 and then Chars (E2) = Chars (Selector_Name (E1));
7678
8fde064e 7679 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
996ae0b0
RK
7680 return FCO (E1, E2);
7681
8fde064e 7682 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
996ae0b0
RK
7683 return FCO (E2, E1);
7684
7685 -- Otherwise we must have the same syntactic entity
7686
7687 elsif Nkind (E1) /= Nkind (E2) then
7688 return False;
7689
7690 -- At this point, we specialize by node type
7691
7692 else
7693 case Nkind (E1) is
7694
7695 when N_Aggregate =>
7696 return
7697 FCL (Expressions (E1), Expressions (E2))
19d846a0
RD
7698 and then
7699 FCL (Component_Associations (E1),
7700 Component_Associations (E2));
996ae0b0
RK
7701
7702 when N_Allocator =>
7703 if Nkind (Expression (E1)) = N_Qualified_Expression
7704 or else
7705 Nkind (Expression (E2)) = N_Qualified_Expression
7706 then
7707 return FCE (Expression (E1), Expression (E2));
7708
7709 -- Check that the subtype marks and any constraints
7710 -- are conformant
7711
7712 else
7713 declare
7714 Indic1 : constant Node_Id := Expression (E1);
7715 Indic2 : constant Node_Id := Expression (E2);
7716 Elt1 : Node_Id;
7717 Elt2 : Node_Id;
7718
7719 begin
7720 if Nkind (Indic1) /= N_Subtype_Indication then
7721 return
7722 Nkind (Indic2) /= N_Subtype_Indication
7723 and then Entity (Indic1) = Entity (Indic2);
7724
7725 elsif Nkind (Indic2) /= N_Subtype_Indication then
7726 return
7727 Nkind (Indic1) /= N_Subtype_Indication
7728 and then Entity (Indic1) = Entity (Indic2);
7729
7730 else
7731 if Entity (Subtype_Mark (Indic1)) /=
7732 Entity (Subtype_Mark (Indic2))
7733 then
7734 return False;
7735 end if;
7736
7737 Elt1 := First (Constraints (Constraint (Indic1)));
7738 Elt2 := First (Constraints (Constraint (Indic2)));
996ae0b0
RK
7739 while Present (Elt1) and then Present (Elt2) loop
7740 if not FCE (Elt1, Elt2) then
7741 return False;
7742 end if;
7743
7744 Next (Elt1);
7745 Next (Elt2);
7746 end loop;
7747
7748 return True;
7749 end if;
7750 end;
7751 end if;
7752
7753 when N_Attribute_Reference =>
7754 return
7755 Attribute_Name (E1) = Attribute_Name (E2)
7756 and then FCL (Expressions (E1), Expressions (E2));
7757
7758 when N_Binary_Op =>
7759 return
7760 Entity (E1) = Entity (E2)
7761 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
7762 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
7763
514d0fc5 7764 when N_Short_Circuit | N_Membership_Test =>
996ae0b0
RK
7765 return
7766 FCE (Left_Opnd (E1), Left_Opnd (E2))
7767 and then
7768 FCE (Right_Opnd (E1), Right_Opnd (E2));
7769
19d846a0
RD
7770 when N_Case_Expression =>
7771 declare
7772 Alt1 : Node_Id;
7773 Alt2 : Node_Id;
7774
7775 begin
7776 if not FCE (Expression (E1), Expression (E2)) then
7777 return False;
7778
7779 else
7780 Alt1 := First (Alternatives (E1));
7781 Alt2 := First (Alternatives (E2));
7782 loop
7783 if Present (Alt1) /= Present (Alt2) then
7784 return False;
7785 elsif No (Alt1) then
7786 return True;
7787 end if;
7788
7789 if not FCE (Expression (Alt1), Expression (Alt2))
7790 or else not FCL (Discrete_Choices (Alt1),
7791 Discrete_Choices (Alt2))
7792 then
7793 return False;
7794 end if;
7795
7796 Next (Alt1);
7797 Next (Alt2);
7798 end loop;
7799 end if;
7800 end;
7801
996ae0b0
RK
7802 when N_Character_Literal =>
7803 return
7804 Char_Literal_Value (E1) = Char_Literal_Value (E2);
7805
7806 when N_Component_Association =>
7807 return
7808 FCL (Choices (E1), Choices (E2))
19d846a0
RD
7809 and then
7810 FCE (Expression (E1), Expression (E2));
996ae0b0 7811
996ae0b0
RK
7812 when N_Explicit_Dereference =>
7813 return
7814 FCE (Prefix (E1), Prefix (E2));
7815
7816 when N_Extension_Aggregate =>
7817 return
7818 FCL (Expressions (E1), Expressions (E2))
7819 and then Null_Record_Present (E1) =
7820 Null_Record_Present (E2)
7821 and then FCL (Component_Associations (E1),
7822 Component_Associations (E2));
7823
7824 when N_Function_Call =>
7825 return
7826 FCE (Name (E1), Name (E2))
19d846a0
RD
7827 and then
7828 FCL (Parameter_Associations (E1),
7829 Parameter_Associations (E2));
996ae0b0 7830
9b16cb57
RD
7831 when N_If_Expression =>
7832 return
7833 FCL (Expressions (E1), Expressions (E2));
7834
996ae0b0
RK
7835 when N_Indexed_Component =>
7836 return
7837 FCE (Prefix (E1), Prefix (E2))
19d846a0
RD
7838 and then
7839 FCL (Expressions (E1), Expressions (E2));
996ae0b0
RK
7840
7841 when N_Integer_Literal =>
7842 return (Intval (E1) = Intval (E2));
7843
7844 when N_Null =>
7845 return True;
7846
7847 when N_Operator_Symbol =>
7848 return
7849 Chars (E1) = Chars (E2);
7850
7851 when N_Others_Choice =>
7852 return True;
7853
7854 when N_Parameter_Association =>
7855 return
996ae0b0
RK
7856 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
7857 and then FCE (Explicit_Actual_Parameter (E1),
7858 Explicit_Actual_Parameter (E2));
7859
7860 when N_Qualified_Expression =>
7861 return
7862 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
19d846a0
RD
7863 and then
7864 FCE (Expression (E1), Expression (E2));
996ae0b0 7865
2010d078
AC
7866 when N_Quantified_Expression =>
7867 if not FCE (Condition (E1), Condition (E2)) then
7868 return False;
7869 end if;
7870
7871 if Present (Loop_Parameter_Specification (E1))
7872 and then Present (Loop_Parameter_Specification (E2))
7873 then
7874 declare
7875 L1 : constant Node_Id :=
7876 Loop_Parameter_Specification (E1);
7877 L2 : constant Node_Id :=
7878 Loop_Parameter_Specification (E2);
7879
7880 begin
7881 return
7882 Reverse_Present (L1) = Reverse_Present (L2)
7883 and then
7884 FCE (Defining_Identifier (L1),
7885 Defining_Identifier (L2))
7886 and then
7887 FCE (Discrete_Subtype_Definition (L1),
7888 Discrete_Subtype_Definition (L2));
7889 end;
7890
804670f1
AC
7891 elsif Present (Iterator_Specification (E1))
7892 and then Present (Iterator_Specification (E2))
7893 then
2010d078
AC
7894 declare
7895 I1 : constant Node_Id := Iterator_Specification (E1);
7896 I2 : constant Node_Id := Iterator_Specification (E2);
7897
7898 begin
7899 return
7900 FCE (Defining_Identifier (I1),
7901 Defining_Identifier (I2))
7902 and then
7903 Of_Present (I1) = Of_Present (I2)
7904 and then
7905 Reverse_Present (I1) = Reverse_Present (I2)
7906 and then FCE (Name (I1), Name (I2))
7907 and then FCE (Subtype_Indication (I1),
7908 Subtype_Indication (I2));
7909 end;
804670f1
AC
7910
7911 -- The quantified expressions used different specifications to
7912 -- walk their respective ranges.
7913
7914 else
7915 return False;
2010d078
AC
7916 end if;
7917
996ae0b0
RK
7918 when N_Range =>
7919 return
7920 FCE (Low_Bound (E1), Low_Bound (E2))
19d846a0
RD
7921 and then
7922 FCE (High_Bound (E1), High_Bound (E2));
996ae0b0
RK
7923
7924 when N_Real_Literal =>
7925 return (Realval (E1) = Realval (E2));
7926
7927 when N_Selected_Component =>
7928 return
7929 FCE (Prefix (E1), Prefix (E2))
19d846a0
RD
7930 and then
7931 FCE (Selector_Name (E1), Selector_Name (E2));
996ae0b0
RK
7932
7933 when N_Slice =>
7934 return
7935 FCE (Prefix (E1), Prefix (E2))
19d846a0
RD
7936 and then
7937 FCE (Discrete_Range (E1), Discrete_Range (E2));
996ae0b0
RK
7938
7939 when N_String_Literal =>
7940 declare
7941 S1 : constant String_Id := Strval (E1);
7942 S2 : constant String_Id := Strval (E2);
7943 L1 : constant Nat := String_Length (S1);
7944 L2 : constant Nat := String_Length (S2);
7945
7946 begin
7947 if L1 /= L2 then
7948 return False;
7949
7950 else
7951 for J in 1 .. L1 loop
7952 if Get_String_Char (S1, J) /=
7953 Get_String_Char (S2, J)
7954 then
7955 return False;
7956 end if;
7957 end loop;
7958
7959 return True;
7960 end if;
7961 end;
7962
7963 when N_Type_Conversion =>
7964 return
7965 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
19d846a0
RD
7966 and then
7967 FCE (Expression (E1), Expression (E2));
996ae0b0
RK
7968
7969 when N_Unary_Op =>
7970 return
7971 Entity (E1) = Entity (E2)
19d846a0
RD
7972 and then
7973 FCE (Right_Opnd (E1), Right_Opnd (E2));
996ae0b0
RK
7974
7975 when N_Unchecked_Type_Conversion =>
7976 return
7977 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
19d846a0
RD
7978 and then
7979 FCE (Expression (E1), Expression (E2));
996ae0b0
RK
7980
7981 -- All other node types cannot appear in this context. Strictly
7982 -- we should raise a fatal internal error. Instead we just ignore
7983 -- the nodes. This means that if anyone makes a mistake in the
2995860f
AC
7984 -- expander and mucks an expression tree irretrievably, the result
7985 -- will be a failure to detect a (probably very obscure) case
7986 -- of non-conformance, which is better than bombing on some
996ae0b0
RK
7987 -- case where two expressions do in fact conform.
7988
7989 when others =>
7990 return True;
7991
7992 end case;
7993 end if;
7994 end Fully_Conformant_Expressions;
7995
fbf5a39b
AC
7996 ----------------------------------------
7997 -- Fully_Conformant_Discrete_Subtypes --
7998 ----------------------------------------
7999
8000 function Fully_Conformant_Discrete_Subtypes
8001 (Given_S1 : Node_Id;
d05ef0ab 8002 Given_S2 : Node_Id) return Boolean
fbf5a39b
AC
8003 is
8004 S1 : constant Node_Id := Original_Node (Given_S1);
8005 S2 : constant Node_Id := Original_Node (Given_S2);
8006
8007 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
82c80734
RD
8008 -- Special-case for a bound given by a discriminant, which in the body
8009 -- is replaced with the discriminal of the enclosing type.
fbf5a39b
AC
8010
8011 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
e895b435 8012 -- Check both bounds
fbf5a39b 8013
5d37ba92
ES
8014 -----------------------
8015 -- Conforming_Bounds --
8016 -----------------------
8017
fbf5a39b
AC
8018 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8019 begin
8020 if Is_Entity_Name (B1)
8021 and then Is_Entity_Name (B2)
8022 and then Ekind (Entity (B1)) = E_Discriminant
8023 then
8024 return Chars (B1) = Chars (B2);
8025
8026 else
8027 return Fully_Conformant_Expressions (B1, B2);
8028 end if;
8029 end Conforming_Bounds;
8030
5d37ba92
ES
8031 -----------------------
8032 -- Conforming_Ranges --
8033 -----------------------
8034
fbf5a39b
AC
8035 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
8036 begin
8037 return
8038 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
8039 and then
8040 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
8041 end Conforming_Ranges;
8042
8043 -- Start of processing for Fully_Conformant_Discrete_Subtypes
8044
8045 begin
8046 if Nkind (S1) /= Nkind (S2) then
8047 return False;
8048
8049 elsif Is_Entity_Name (S1) then
8050 return Entity (S1) = Entity (S2);
8051
8052 elsif Nkind (S1) = N_Range then
8053 return Conforming_Ranges (S1, S2);
8054
8055 elsif Nkind (S1) = N_Subtype_Indication then
8056 return
8057 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
8058 and then
8059 Conforming_Ranges
8060 (Range_Expression (Constraint (S1)),
8061 Range_Expression (Constraint (S2)));
8062 else
8063 return True;
8064 end if;
8065 end Fully_Conformant_Discrete_Subtypes;
8066
996ae0b0
RK
8067 --------------------
8068 -- Install_Entity --
8069 --------------------
8070
8071 procedure Install_Entity (E : Entity_Id) is
8072 Prev : constant Entity_Id := Current_Entity (E);
996ae0b0
RK
8073 begin
8074 Set_Is_Immediately_Visible (E);
8075 Set_Current_Entity (E);
8076 Set_Homonym (E, Prev);
8077 end Install_Entity;
8078
8079 ---------------------
8080 -- Install_Formals --
8081 ---------------------
8082
8083 procedure Install_Formals (Id : Entity_Id) is
8084 F : Entity_Id;
996ae0b0
RK
8085 begin
8086 F := First_Formal (Id);
996ae0b0
RK
8087 while Present (F) loop
8088 Install_Entity (F);
8089 Next_Formal (F);
8090 end loop;
8091 end Install_Formals;
8092
ce2b6ba5
JM
8093 -----------------------------
8094 -- Is_Interface_Conformant --
8095 -----------------------------
8096
8097 function Is_Interface_Conformant
8098 (Tagged_Type : Entity_Id;
8099 Iface_Prim : Entity_Id;
8100 Prim : Entity_Id) return Boolean
8101 is
9e92ad49
AC
8102 -- The operation may in fact be an inherited (implicit) operation
8103 -- rather than the original interface primitive, so retrieve the
8104 -- ultimate ancestor.
8105
8106 Iface : constant Entity_Id :=
8107 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
fceeaab6
ES
8108 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
8109
25ebc085
AC
8110 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
8111 -- Return the controlling formal of Prim
8112
59e6b23c
AC
8113 ------------------------
8114 -- Controlling_Formal --
8115 ------------------------
8116
25ebc085 8117 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
15918371 8118 E : Entity_Id;
59e6b23c 8119
25ebc085 8120 begin
15918371 8121 E := First_Entity (Prim);
25ebc085
AC
8122 while Present (E) loop
8123 if Is_Formal (E) and then Is_Controlling_Formal (E) then
8124 return E;
8125 end if;
8126
8127 Next_Entity (E);
8128 end loop;
8129
8130 return Empty;
8131 end Controlling_Formal;
8132
8133 -- Local variables
8134
8135 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
8136 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
8137
8138 -- Start of processing for Is_Interface_Conformant
8139
ce2b6ba5
JM
8140 begin
8141 pragma Assert (Is_Subprogram (Iface_Prim)
8142 and then Is_Subprogram (Prim)
8143 and then Is_Dispatching_Operation (Iface_Prim)
8144 and then Is_Dispatching_Operation (Prim));
8145
fceeaab6 8146 pragma Assert (Is_Interface (Iface)
ce2b6ba5
JM
8147 or else (Present (Alias (Iface_Prim))
8148 and then
8149 Is_Interface
8150 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
8151
8152 if Prim = Iface_Prim
8153 or else not Is_Subprogram (Prim)
8154 or else Ekind (Prim) /= Ekind (Iface_Prim)
8155 or else not Is_Dispatching_Operation (Prim)
8156 or else Scope (Prim) /= Scope (Tagged_Type)
fceeaab6 8157 or else No (Typ)
8a49a499 8158 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
ce2b6ba5
JM
8159 or else not Primitive_Names_Match (Iface_Prim, Prim)
8160 then
8161 return False;
8162
25ebc085
AC
8163 -- The mode of the controlling formals must match
8164
8165 elsif Present (Iface_Ctrl_F)
15918371
AC
8166 and then Present (Prim_Ctrl_F)
8167 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
25ebc085
AC
8168 then
8169 return False;
8170
8171 -- Case of a procedure, or a function whose result type matches the
8172 -- result type of the interface primitive, or a function that has no
8173 -- controlling result (I or access I).
ce2b6ba5
JM
8174
8175 elsif Ekind (Iface_Prim) = E_Procedure
8176 or else Etype (Prim) = Etype (Iface_Prim)
fceeaab6 8177 or else not Has_Controlling_Result (Prim)
ce2b6ba5 8178 then
b4d7b435
AC
8179 return Type_Conformant
8180 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
ce2b6ba5 8181
2995860f
AC
8182 -- Case of a function returning an interface, or an access to one. Check
8183 -- that the return types correspond.
ce2b6ba5 8184
fceeaab6
ES
8185 elsif Implements_Interface (Typ, Iface) then
8186 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9a3c9940
RD
8187 /=
8188 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
fceeaab6
ES
8189 then
8190 return False;
fceeaab6
ES
8191 else
8192 return
9e92ad49 8193 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
ce2b6ba5 8194 Skip_Controlling_Formals => True);
fceeaab6 8195 end if;
ce2b6ba5 8196
fceeaab6
ES
8197 else
8198 return False;
ce2b6ba5 8199 end if;
ce2b6ba5
JM
8200 end Is_Interface_Conformant;
8201
996ae0b0
RK
8202 ---------------------------------
8203 -- Is_Non_Overriding_Operation --
8204 ---------------------------------
8205
8206 function Is_Non_Overriding_Operation
8207 (Prev_E : Entity_Id;
d05ef0ab 8208 New_E : Entity_Id) return Boolean
996ae0b0
RK
8209 is
8210 Formal : Entity_Id;
8211 F_Typ : Entity_Id;
8212 G_Typ : Entity_Id := Empty;
8213
8214 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
5d37ba92
ES
8215 -- If F_Type is a derived type associated with a generic actual subtype,
8216 -- then return its Generic_Parent_Type attribute, else return Empty.
996ae0b0
RK
8217
8218 function Types_Correspond
8219 (P_Type : Entity_Id;
d05ef0ab 8220 N_Type : Entity_Id) return Boolean;
82c80734
RD
8221 -- Returns true if and only if the types (or designated types in the
8222 -- case of anonymous access types) are the same or N_Type is derived
8223 -- directly or indirectly from P_Type.
996ae0b0
RK
8224
8225 -----------------------------
8226 -- Get_Generic_Parent_Type --
8227 -----------------------------
8228
8229 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
8230 G_Typ : Entity_Id;
702d2020 8231 Defn : Node_Id;
996ae0b0
RK
8232 Indic : Node_Id;
8233
8234 begin
8235 if Is_Derived_Type (F_Typ)
8236 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
8237 then
82c80734
RD
8238 -- The tree must be traversed to determine the parent subtype in
8239 -- the generic unit, which unfortunately isn't always available
8240 -- via semantic attributes. ??? (Note: The use of Original_Node
8241 -- is needed for cases where a full derived type has been
8242 -- rewritten.)
996ae0b0 8243
702d2020
AC
8244 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
8245 if Nkind (Defn) = N_Derived_Type_Definition then
8246 Indic := Subtype_Indication (Defn);
996ae0b0 8247
702d2020
AC
8248 if Nkind (Indic) = N_Subtype_Indication then
8249 G_Typ := Entity (Subtype_Mark (Indic));
8250 else
8251 G_Typ := Entity (Indic);
8252 end if;
996ae0b0 8253
702d2020
AC
8254 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
8255 and then Present (Generic_Parent_Type (Parent (G_Typ)))
8256 then
8257 return Generic_Parent_Type (Parent (G_Typ));
8258 end if;
996ae0b0
RK
8259 end if;
8260 end if;
8261
8262 return Empty;
8263 end Get_Generic_Parent_Type;
8264
8265 ----------------------
8266 -- Types_Correspond --
8267 ----------------------
8268
8269 function Types_Correspond
8270 (P_Type : Entity_Id;
d05ef0ab 8271 N_Type : Entity_Id) return Boolean
996ae0b0
RK
8272 is
8273 Prev_Type : Entity_Id := Base_Type (P_Type);
8274 New_Type : Entity_Id := Base_Type (N_Type);
8275
8276 begin
8277 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
8278 Prev_Type := Designated_Type (Prev_Type);
8279 end if;
8280
8281 if Ekind (New_Type) = E_Anonymous_Access_Type then
8282 New_Type := Designated_Type (New_Type);
8283 end if;
8284
8285 if Prev_Type = New_Type then
8286 return True;
8287
8288 elsif not Is_Class_Wide_Type (New_Type) then
8289 while Etype (New_Type) /= New_Type loop
8290 New_Type := Etype (New_Type);
8291 if New_Type = Prev_Type then
8292 return True;
8293 end if;
8294 end loop;
8295 end if;
8296 return False;
8297 end Types_Correspond;
8298
8299 -- Start of processing for Is_Non_Overriding_Operation
8300
8301 begin
82c80734
RD
8302 -- In the case where both operations are implicit derived subprograms
8303 -- then neither overrides the other. This can only occur in certain
8304 -- obscure cases (e.g., derivation from homographs created in a generic
8305 -- instantiation).
996ae0b0
RK
8306
8307 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
8308 return True;
8309
8310 elsif Ekind (Current_Scope) = E_Package
8311 and then Is_Generic_Instance (Current_Scope)
8312 and then In_Private_Part (Current_Scope)
8313 and then Comes_From_Source (New_E)
8314 then
702d2020
AC
8315 -- We examine the formals and result type of the inherited operation,
8316 -- to determine whether their type is derived from (the instance of)
8317 -- a generic type. The first such formal or result type is the one
8318 -- tested.
996ae0b0
RK
8319
8320 Formal := First_Formal (Prev_E);
996ae0b0
RK
8321 while Present (Formal) loop
8322 F_Typ := Base_Type (Etype (Formal));
8323
8324 if Ekind (F_Typ) = E_Anonymous_Access_Type then
8325 F_Typ := Designated_Type (F_Typ);
8326 end if;
8327
8328 G_Typ := Get_Generic_Parent_Type (F_Typ);
702d2020 8329 exit when Present (G_Typ);
996ae0b0
RK
8330
8331 Next_Formal (Formal);
8332 end loop;
8333
c8ef728f 8334 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
996ae0b0
RK
8335 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
8336 end if;
8337
8338 if No (G_Typ) then
8339 return False;
8340 end if;
8341
8dbd1460
AC
8342 -- If the generic type is a private type, then the original operation
8343 -- was not overriding in the generic, because there was no primitive
8344 -- operation to override.
996ae0b0
RK
8345
8346 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
8347 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
8dbd1460 8348 N_Formal_Private_Type_Definition
996ae0b0
RK
8349 then
8350 return True;
8351
8352 -- The generic parent type is the ancestor of a formal derived
8353 -- type declaration. We need to check whether it has a primitive
8354 -- operation that should be overridden by New_E in the generic.
8355
8356 else
8357 declare
8358 P_Formal : Entity_Id;
8359 N_Formal : Entity_Id;
8360 P_Typ : Entity_Id;
8361 N_Typ : Entity_Id;
8362 P_Prim : Entity_Id;
8363 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
8364
8365 begin
8366 while Present (Prim_Elt) loop
8367 P_Prim := Node (Prim_Elt);
fbf5a39b 8368
996ae0b0
RK
8369 if Chars (P_Prim) = Chars (New_E)
8370 and then Ekind (P_Prim) = Ekind (New_E)
8371 then
8372 P_Formal := First_Formal (P_Prim);
8373 N_Formal := First_Formal (New_E);
8374 while Present (P_Formal) and then Present (N_Formal) loop
8375 P_Typ := Etype (P_Formal);
8376 N_Typ := Etype (N_Formal);
8377
8378 if not Types_Correspond (P_Typ, N_Typ) then
8379 exit;
8380 end if;
8381
8382 Next_Entity (P_Formal);
8383 Next_Entity (N_Formal);
8384 end loop;
8385
82c80734
RD
8386 -- Found a matching primitive operation belonging to the
8387 -- formal ancestor type, so the new subprogram is
8388 -- overriding.
996ae0b0 8389
c8ef728f
ES
8390 if No (P_Formal)
8391 and then No (N_Formal)
996ae0b0
RK
8392 and then (Ekind (New_E) /= E_Function
8393 or else
8fde064e
AC
8394 Types_Correspond
8395 (Etype (P_Prim), Etype (New_E)))
996ae0b0
RK
8396 then
8397 return False;
8398 end if;
8399 end if;
8400
8401 Next_Elmt (Prim_Elt);
8402 end loop;
8403
2995860f
AC
8404 -- If no match found, then the new subprogram does not override
8405 -- in the generic (nor in the instance).
996ae0b0 8406
260359e3
AC
8407 -- If the type in question is not abstract, and the subprogram
8408 -- is, this will be an error if the new operation is in the
8409 -- private part of the instance. Emit a warning now, which will
8410 -- make the subsequent error message easier to understand.
8411
8412 if not Is_Abstract_Type (F_Typ)
8413 and then Is_Abstract_Subprogram (Prev_E)
8414 and then In_Private_Part (Current_Scope)
8415 then
8416 Error_Msg_Node_2 := F_Typ;
8417 Error_Msg_NE
3ccedacc
AC
8418 ("private operation& in generic unit does not override "
8419 & "any primitive operation of& (RM 12.3 (18))??",
260359e3
AC
8420 New_E, New_E);
8421 end if;
8422
996ae0b0
RK
8423 return True;
8424 end;
8425 end if;
8426 else
8427 return False;
8428 end if;
8429 end Is_Non_Overriding_Operation;
8430
beacce02
AC
8431 -------------------------------------
8432 -- List_Inherited_Pre_Post_Aspects --
8433 -------------------------------------
8434
8435 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
8436 begin
e606088a 8437 if Opt.List_Inherited_Aspects
b9696ffb 8438 and then Is_Subprogram_Or_Generic_Subprogram (E)
beacce02
AC
8439 then
8440 declare
dbfeb4fa 8441 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
beacce02
AC
8442 P : Node_Id;
8443
8444 begin
8445 for J in Inherited'Range loop
d6095153 8446 P := Pre_Post_Conditions (Contract (Inherited (J)));
beacce02
AC
8447 while Present (P) loop
8448 Error_Msg_Sloc := Sloc (P);
8449
8450 if Class_Present (P) and then not Split_PPC (P) then
8451 if Pragma_Name (P) = Name_Precondition then
3ccedacc
AC
8452 Error_Msg_N ("info: & inherits `Pre''Class` aspect "
8453 & "from #?L?", E);
beacce02 8454 else
3ccedacc
AC
8455 Error_Msg_N ("info: & inherits `Post''Class` aspect "
8456 & "from #?L?", E);
beacce02
AC
8457 end if;
8458 end if;
8459
8460 P := Next_Pragma (P);
8461 end loop;
8462 end loop;
8463 end;
8464 end if;
8465 end List_Inherited_Pre_Post_Aspects;
8466
996ae0b0
RK
8467 ------------------------------
8468 -- Make_Inequality_Operator --
8469 ------------------------------
8470
8471 -- S is the defining identifier of an equality operator. We build a
8472 -- subprogram declaration with the right signature. This operation is
8473 -- intrinsic, because it is always expanded as the negation of the
8474 -- call to the equality function.
8475
8476 procedure Make_Inequality_Operator (S : Entity_Id) is
8477 Loc : constant Source_Ptr := Sloc (S);
8478 Decl : Node_Id;
8479 Formals : List_Id;
8480 Op_Name : Entity_Id;
8481
c8ef728f
ES
8482 FF : constant Entity_Id := First_Formal (S);
8483 NF : constant Entity_Id := Next_Formal (FF);
996ae0b0
RK
8484
8485 begin
c8ef728f 8486 -- Check that equality was properly defined, ignore call if not
996ae0b0 8487
c8ef728f 8488 if No (NF) then
996ae0b0
RK
8489 return;
8490 end if;
8491
c8ef728f
ES
8492 declare
8493 A : constant Entity_Id :=
8494 Make_Defining_Identifier (Sloc (FF),
8495 Chars => Chars (FF));
8496
5d37ba92
ES
8497 B : constant Entity_Id :=
8498 Make_Defining_Identifier (Sloc (NF),
8499 Chars => Chars (NF));
c8ef728f
ES
8500
8501 begin
8502 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
8503
8504 Formals := New_List (
8505 Make_Parameter_Specification (Loc,
8506 Defining_Identifier => A,
8507 Parameter_Type =>
e4494292 8508 New_Occurrence_Of (Etype (First_Formal (S)),
c8ef728f
ES
8509 Sloc (Etype (First_Formal (S))))),
8510
8511 Make_Parameter_Specification (Loc,
8512 Defining_Identifier => B,
8513 Parameter_Type =>
e4494292 8514 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
c8ef728f
ES
8515 Sloc (Etype (Next_Formal (First_Formal (S)))))));
8516
8517 Decl :=
8518 Make_Subprogram_Declaration (Loc,
8519 Specification =>
8520 Make_Function_Specification (Loc,
8521 Defining_Unit_Name => Op_Name,
8522 Parameter_Specifications => Formals,
8523 Result_Definition =>
e4494292 8524 New_Occurrence_Of (Standard_Boolean, Loc)));
c8ef728f
ES
8525
8526 -- Insert inequality right after equality if it is explicit or after
8527 -- the derived type when implicit. These entities are created only
2995860f
AC
8528 -- for visibility purposes, and eventually replaced in the course
8529 -- of expansion, so they do not need to be attached to the tree and
8530 -- seen by the back-end. Keeping them internal also avoids spurious
c8ef728f
ES
8531 -- freezing problems. The declaration is inserted in the tree for
8532 -- analysis, and removed afterwards. If the equality operator comes
8533 -- from an explicit declaration, attach the inequality immediately
8534 -- after. Else the equality is inherited from a derived type
8535 -- declaration, so insert inequality after that declaration.
8536
8537 if No (Alias (S)) then
8538 Insert_After (Unit_Declaration_Node (S), Decl);
8539 elsif Is_List_Member (Parent (S)) then
8540 Insert_After (Parent (S), Decl);
8541 else
8542 Insert_After (Parent (Etype (First_Formal (S))), Decl);
8543 end if;
996ae0b0 8544
c8ef728f
ES
8545 Mark_Rewrite_Insertion (Decl);
8546 Set_Is_Intrinsic_Subprogram (Op_Name);
8547 Analyze (Decl);
8548 Remove (Decl);
8549 Set_Has_Completion (Op_Name);
8550 Set_Corresponding_Equality (Op_Name, S);
f937473f 8551 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
c8ef728f 8552 end;
996ae0b0
RK
8553 end Make_Inequality_Operator;
8554
8555 ----------------------
8556 -- May_Need_Actuals --
8557 ----------------------
8558
8559 procedure May_Need_Actuals (Fun : Entity_Id) is
8560 F : Entity_Id;
8561 B : Boolean;
8562
8563 begin
8564 F := First_Formal (Fun);
8565 B := True;
996ae0b0
RK
8566 while Present (F) loop
8567 if No (Default_Value (F)) then
8568 B := False;
8569 exit;
8570 end if;
8571
8572 Next_Formal (F);
8573 end loop;
8574
8575 Set_Needs_No_Actuals (Fun, B);
8576 end May_Need_Actuals;
8577
8578 ---------------------
8579 -- Mode_Conformant --
8580 ---------------------
8581
8582 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8583 Result : Boolean;
996ae0b0
RK
8584 begin
8585 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
8586 return Result;
8587 end Mode_Conformant;
8588
8589 ---------------------------
8590 -- New_Overloaded_Entity --
8591 ---------------------------
8592
8593 procedure New_Overloaded_Entity
8594 (S : Entity_Id;
8595 Derived_Type : Entity_Id := Empty)
8596 is
ec4867fa 8597 Overridden_Subp : Entity_Id := Empty;
758c442c
GD
8598 -- Set if the current scope has an operation that is type-conformant
8599 -- with S, and becomes hidden by S.
8600
5d37ba92
ES
8601 Is_Primitive_Subp : Boolean;
8602 -- Set to True if the new subprogram is primitive
8603
fbf5a39b
AC
8604 E : Entity_Id;
8605 -- Entity that S overrides
8606
996ae0b0 8607 Prev_Vis : Entity_Id := Empty;
ec4867fa
ES
8608 -- Predecessor of E in Homonym chain
8609
5d37ba92
ES
8610 procedure Check_For_Primitive_Subprogram
8611 (Is_Primitive : out Boolean;
8612 Is_Overriding : Boolean := False);
8613 -- If the subprogram being analyzed is a primitive operation of the type
8614 -- of a formal or result, set the Has_Primitive_Operations flag on the
8615 -- type, and set Is_Primitive to True (otherwise set to False). Set the
8616 -- corresponding flag on the entity itself for later use.
8617
ec4867fa
ES
8618 procedure Check_Synchronized_Overriding
8619 (Def_Id : Entity_Id;
ec4867fa
ES
8620 Overridden_Subp : out Entity_Id);
8621 -- First determine if Def_Id is an entry or a subprogram either defined
8622 -- in the scope of a task or protected type, or is a primitive of such
8623 -- a type. Check whether Def_Id overrides a subprogram of an interface
8624 -- implemented by the synchronized type, return the overridden entity
8625 -- or Empty.
758c442c 8626
996ae0b0
RK
8627 function Is_Private_Declaration (E : Entity_Id) return Boolean;
8628 -- Check that E is declared in the private part of the current package,
8629 -- or in the package body, where it may hide a previous declaration.
fbf5a39b 8630 -- We can't use In_Private_Part by itself because this flag is also
996ae0b0
RK
8631 -- set when freezing entities, so we must examine the place of the
8632 -- declaration in the tree, and recognize wrapper packages as well.
8633
2ddc2000
AC
8634 function Is_Overriding_Alias
8635 (Old_E : Entity_Id;
8636 New_E : Entity_Id) return Boolean;
8637 -- Check whether new subprogram and old subprogram are both inherited
8638 -- from subprograms that have distinct dispatch table entries. This can
2995860f
AC
8639 -- occur with derivations from instances with accidental homonyms. The
8640 -- function is conservative given that the converse is only true within
8641 -- instances that contain accidental overloadings.
2ddc2000 8642
5d37ba92
ES
8643 ------------------------------------
8644 -- Check_For_Primitive_Subprogram --
8645 ------------------------------------
996ae0b0 8646
5d37ba92
ES
8647 procedure Check_For_Primitive_Subprogram
8648 (Is_Primitive : out Boolean;
8649 Is_Overriding : Boolean := False)
ec4867fa 8650 is
996ae0b0
RK
8651 Formal : Entity_Id;
8652 F_Typ : Entity_Id;
07fc65c4 8653 B_Typ : Entity_Id;
996ae0b0
RK
8654
8655 function Visible_Part_Type (T : Entity_Id) return Boolean;
8dbd1460
AC
8656 -- Returns true if T is declared in the visible part of the current
8657 -- package scope; otherwise returns false. Assumes that T is declared
8658 -- in a package.
996ae0b0
RK
8659
8660 procedure Check_Private_Overriding (T : Entity_Id);
8661 -- Checks that if a primitive abstract subprogram of a visible
8dbd1460
AC
8662 -- abstract type is declared in a private part, then it must override
8663 -- an abstract subprogram declared in the visible part. Also checks
8664 -- that if a primitive function with a controlling result is declared
8665 -- in a private part, then it must override a function declared in
8666 -- the visible part.
996ae0b0
RK
8667
8668 ------------------------------
8669 -- Check_Private_Overriding --
8670 ------------------------------
8671
8672 procedure Check_Private_Overriding (T : Entity_Id) is
8673 begin
51c16e29 8674 if Is_Package_Or_Generic_Package (Current_Scope)
996ae0b0
RK
8675 and then In_Private_Part (Current_Scope)
8676 and then Visible_Part_Type (T)
8677 and then not In_Instance
8678 then
f937473f
RD
8679 if Is_Abstract_Type (T)
8680 and then Is_Abstract_Subprogram (S)
8681 and then (not Is_Overriding
8dbd1460 8682 or else not Is_Abstract_Subprogram (E))
996ae0b0 8683 then
3ccedacc
AC
8684 Error_Msg_N ("abstract subprograms must be visible "
8685 & "(RM 3.9.3(10))!", S);
758c442c 8686
8fde064e
AC
8687 elsif Ekind (S) = E_Function and then not Is_Overriding then
8688 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
3ccedacc
AC
8689 Error_Msg_N ("private function with tagged result must"
8690 & " override visible-part function", S);
8691 Error_Msg_N ("\move subprogram to the visible part"
8692 & " (RM 3.9.3(10))", S);
2e79de51
AC
8693
8694 -- AI05-0073: extend this test to the case of a function
8695 -- with a controlling access result.
8696
8697 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
8698 and then Is_Tagged_Type (Designated_Type (Etype (S)))
8699 and then
8700 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
dbe945f1 8701 and then Ada_Version >= Ada_2012
2e79de51
AC
8702 then
8703 Error_Msg_N
8704 ("private function with controlling access result "
3ccedacc 8705 & "must override visible-part function", S);
2e79de51
AC
8706 Error_Msg_N
8707 ("\move subprogram to the visible part"
3ccedacc 8708 & " (RM 3.9.3(10))", S);
2e79de51 8709 end if;
996ae0b0
RK
8710 end if;
8711 end if;
8712 end Check_Private_Overriding;
8713
8714 -----------------------
8715 -- Visible_Part_Type --
8716 -----------------------
8717
8718 function Visible_Part_Type (T : Entity_Id) return Boolean is
07fc65c4
GB
8719 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
8720 N : Node_Id;
996ae0b0
RK
8721
8722 begin
8dbd1460
AC
8723 -- If the entity is a private type, then it must be declared in a
8724 -- visible part.
996ae0b0
RK
8725
8726 if Ekind (T) in Private_Kind then
8727 return True;
8728 end if;
8729
8730 -- Otherwise, we traverse the visible part looking for its
8731 -- corresponding declaration. We cannot use the declaration
8732 -- node directly because in the private part the entity of a
8733 -- private type is the one in the full view, which does not
8734 -- indicate that it is the completion of something visible.
8735
07fc65c4 8736 N := First (Visible_Declarations (Specification (P)));
996ae0b0
RK
8737 while Present (N) loop
8738 if Nkind (N) = N_Full_Type_Declaration
8739 and then Present (Defining_Identifier (N))
8740 and then T = Defining_Identifier (N)
8741 then
8742 return True;
8743
800621e0
RD
8744 elsif Nkind_In (N, N_Private_Type_Declaration,
8745 N_Private_Extension_Declaration)
996ae0b0
RK
8746 and then Present (Defining_Identifier (N))
8747 and then T = Full_View (Defining_Identifier (N))
8748 then
8749 return True;
8750 end if;
8751
8752 Next (N);
8753 end loop;
8754
8755 return False;
8756 end Visible_Part_Type;
8757
5d37ba92 8758 -- Start of processing for Check_For_Primitive_Subprogram
996ae0b0
RK
8759
8760 begin
5d37ba92
ES
8761 Is_Primitive := False;
8762
996ae0b0
RK
8763 if not Comes_From_Source (S) then
8764 null;
8765
5d37ba92 8766 -- If subprogram is at library level, it is not primitive operation
15ce9ca2
AC
8767
8768 elsif Current_Scope = Standard_Standard then
8769 null;
8770
b9b2405f 8771 elsif (Is_Package_Or_Generic_Package (Current_Scope)
996ae0b0 8772 and then not In_Package_Body (Current_Scope))
82c80734 8773 or else Is_Overriding
996ae0b0 8774 then
07fc65c4 8775 -- For function, check return type
996ae0b0 8776
07fc65c4 8777 if Ekind (S) = E_Function then
5d37ba92
ES
8778 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
8779 F_Typ := Designated_Type (Etype (S));
8780 else
8781 F_Typ := Etype (S);
8782 end if;
8783
8784 B_Typ := Base_Type (F_Typ);
07fc65c4 8785
5d37ba92
ES
8786 if Scope (B_Typ) = Current_Scope
8787 and then not Is_Class_Wide_Type (B_Typ)
8788 and then not Is_Generic_Type (B_Typ)
8789 then
8790 Is_Primitive := True;
07fc65c4 8791 Set_Has_Primitive_Operations (B_Typ);
5d37ba92 8792 Set_Is_Primitive (S);
07fc65c4
GB
8793 Check_Private_Overriding (B_Typ);
8794 end if;
996ae0b0
RK
8795 end if;
8796
07fc65c4 8797 -- For all subprograms, check formals
996ae0b0 8798
07fc65c4 8799 Formal := First_Formal (S);
996ae0b0
RK
8800 while Present (Formal) loop
8801 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
8802 F_Typ := Designated_Type (Etype (Formal));
8803 else
8804 F_Typ := Etype (Formal);
8805 end if;
8806
07fc65c4
GB
8807 B_Typ := Base_Type (F_Typ);
8808
ec4867fa
ES
8809 if Ekind (B_Typ) = E_Access_Subtype then
8810 B_Typ := Base_Type (B_Typ);
8811 end if;
8812
5d37ba92
ES
8813 if Scope (B_Typ) = Current_Scope
8814 and then not Is_Class_Wide_Type (B_Typ)
8815 and then not Is_Generic_Type (B_Typ)
8816 then
8817 Is_Primitive := True;
8818 Set_Is_Primitive (S);
07fc65c4
GB
8819 Set_Has_Primitive_Operations (B_Typ);
8820 Check_Private_Overriding (B_Typ);
996ae0b0
RK
8821 end if;
8822
8823 Next_Formal (Formal);
8824 end loop;
1aee1fb3
AC
8825
8826 -- Special case: An equality function can be redefined for a type
8827 -- occurring in a declarative part, and won't otherwise be treated as
8828 -- a primitive because it doesn't occur in a package spec and doesn't
8829 -- override an inherited subprogram. It's important that we mark it
8830 -- primitive so it can be returned by Collect_Primitive_Operations
8831 -- and be used in composing the equality operation of later types
8832 -- that have a component of the type.
8833
8834 elsif Chars (S) = Name_Op_Eq
8835 and then Etype (S) = Standard_Boolean
8836 then
8837 B_Typ := Base_Type (Etype (First_Formal (S)));
8838
8839 if Scope (B_Typ) = Current_Scope
8840 and then
8841 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
8842 and then not Is_Limited_Type (B_Typ)
8843 then
8844 Is_Primitive := True;
8845 Set_Is_Primitive (S);
8846 Set_Has_Primitive_Operations (B_Typ);
8847 Check_Private_Overriding (B_Typ);
8848 end if;
996ae0b0 8849 end if;
5d37ba92
ES
8850 end Check_For_Primitive_Subprogram;
8851
8852 -----------------------------------
8853 -- Check_Synchronized_Overriding --
8854 -----------------------------------
8855
8856 procedure Check_Synchronized_Overriding
8857 (Def_Id : Entity_Id;
5d37ba92
ES
8858 Overridden_Subp : out Entity_Id)
8859 is
5d37ba92
ES
8860 Ifaces_List : Elist_Id;
8861 In_Scope : Boolean;
8862 Typ : Entity_Id;
8863
8aa15e3b
JM
8864 function Matches_Prefixed_View_Profile
8865 (Prim_Params : List_Id;
8866 Iface_Params : List_Id) return Boolean;
8867 -- Determine whether a subprogram's parameter profile Prim_Params
8868 -- matches that of a potentially overridden interface subprogram
8869 -- Iface_Params. Also determine if the type of first parameter of
8870 -- Iface_Params is an implemented interface.
8871
8aa15e3b
JM
8872 -----------------------------------
8873 -- Matches_Prefixed_View_Profile --
8874 -----------------------------------
8875
8876 function Matches_Prefixed_View_Profile
8877 (Prim_Params : List_Id;
8878 Iface_Params : List_Id) return Boolean
8879 is
8880 Iface_Id : Entity_Id;
8881 Iface_Param : Node_Id;
8882 Iface_Typ : Entity_Id;
8883 Prim_Id : Entity_Id;
8884 Prim_Param : Node_Id;
8885 Prim_Typ : Entity_Id;
8886
8887 function Is_Implemented
8888 (Ifaces_List : Elist_Id;
8889 Iface : Entity_Id) return Boolean;
8890 -- Determine if Iface is implemented by the current task or
8891 -- protected type.
8892
8893 --------------------
8894 -- Is_Implemented --
8895 --------------------
8896
8897 function Is_Implemented
8898 (Ifaces_List : Elist_Id;
8899 Iface : Entity_Id) return Boolean
8900 is
8901 Iface_Elmt : Elmt_Id;
8902
8903 begin
8904 Iface_Elmt := First_Elmt (Ifaces_List);
8905 while Present (Iface_Elmt) loop
8906 if Node (Iface_Elmt) = Iface then
8907 return True;
8908 end if;
8909
8910 Next_Elmt (Iface_Elmt);
8911 end loop;
8912
8913 return False;
8914 end Is_Implemented;
8915
8916 -- Start of processing for Matches_Prefixed_View_Profile
8917
8918 begin
8919 Iface_Param := First (Iface_Params);
8920 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
8921
8922 if Is_Access_Type (Iface_Typ) then
8923 Iface_Typ := Designated_Type (Iface_Typ);
8924 end if;
8925
8926 Prim_Param := First (Prim_Params);
8927
8928 -- The first parameter of the potentially overridden subprogram
8929 -- must be an interface implemented by Prim.
8930
8931 if not Is_Interface (Iface_Typ)
8932 or else not Is_Implemented (Ifaces_List, Iface_Typ)
8933 then
8934 return False;
8935 end if;
8936
8937 -- The checks on the object parameters are done, move onto the
8938 -- rest of the parameters.
8939
8940 if not In_Scope then
8941 Prim_Param := Next (Prim_Param);
8942 end if;
8943
8944 Iface_Param := Next (Iface_Param);
8945 while Present (Iface_Param) and then Present (Prim_Param) loop
8946 Iface_Id := Defining_Identifier (Iface_Param);
8947 Iface_Typ := Find_Parameter_Type (Iface_Param);
8948
8aa15e3b
JM
8949 Prim_Id := Defining_Identifier (Prim_Param);
8950 Prim_Typ := Find_Parameter_Type (Prim_Param);
8951
15e4986c
JM
8952 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
8953 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
8954 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
8955 then
8956 Iface_Typ := Designated_Type (Iface_Typ);
8957 Prim_Typ := Designated_Type (Prim_Typ);
8aa15e3b
JM
8958 end if;
8959
8960 -- Case of multiple interface types inside a parameter profile
8961
8962 -- (Obj_Param : in out Iface; ...; Param : Iface)
8963
8964 -- If the interface type is implemented, then the matching type
8965 -- in the primitive should be the implementing record type.
8966
8967 if Ekind (Iface_Typ) = E_Record_Type
8968 and then Is_Interface (Iface_Typ)
8969 and then Is_Implemented (Ifaces_List, Iface_Typ)
8970 then
8971 if Prim_Typ /= Typ then
8972 return False;
8973 end if;
8974
8975 -- The two parameters must be both mode and subtype conformant
8976
8977 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
8978 or else not
8979 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
8980 then
8981 return False;
8982 end if;
8983
8984 Next (Iface_Param);
8985 Next (Prim_Param);
8986 end loop;
8987
8988 -- One of the two lists contains more parameters than the other
8989
8990 if Present (Iface_Param) or else Present (Prim_Param) then
8991 return False;
8992 end if;
8993
8994 return True;
8995 end Matches_Prefixed_View_Profile;
8996
8997 -- Start of processing for Check_Synchronized_Overriding
8998
5d37ba92
ES
8999 begin
9000 Overridden_Subp := Empty;
9001
8aa15e3b
JM
9002 -- Def_Id must be an entry or a subprogram. We should skip predefined
9003 -- primitives internally generated by the frontend; however at this
9004 -- stage predefined primitives are still not fully decorated. As a
9005 -- minor optimization we skip here internally generated subprograms.
5d37ba92 9006
8aa15e3b
JM
9007 if (Ekind (Def_Id) /= E_Entry
9008 and then Ekind (Def_Id) /= E_Function
9009 and then Ekind (Def_Id) /= E_Procedure)
9010 or else not Comes_From_Source (Def_Id)
5d37ba92
ES
9011 then
9012 return;
9013 end if;
9014
9015 -- Search for the concurrent declaration since it contains the list
9016 -- of all implemented interfaces. In this case, the subprogram is
9017 -- declared within the scope of a protected or a task type.
9018
9019 if Present (Scope (Def_Id))
9020 and then Is_Concurrent_Type (Scope (Def_Id))
9021 and then not Is_Generic_Actual_Type (Scope (Def_Id))
9022 then
9023 Typ := Scope (Def_Id);
9024 In_Scope := True;
9025
8aa15e3b 9026 -- The enclosing scope is not a synchronized type and the subprogram
4adf3c50 9027 -- has no formals.
8aa15e3b
JM
9028
9029 elsif No (First_Formal (Def_Id)) then
9030 return;
5d37ba92 9031
8aa15e3b 9032 -- The subprogram has formals and hence it may be a primitive of a
4adf3c50 9033 -- concurrent type.
5d37ba92 9034
8aa15e3b
JM
9035 else
9036 Typ := Etype (First_Formal (Def_Id));
9037
9038 if Is_Access_Type (Typ) then
9039 Typ := Directly_Designated_Type (Typ);
8c3dd7a8
JM
9040 end if;
9041
8aa15e3b
JM
9042 if Is_Concurrent_Type (Typ)
9043 and then not Is_Generic_Actual_Type (Typ)
5d37ba92 9044 then
5d37ba92
ES
9045 In_Scope := False;
9046
9047 -- This case occurs when the concurrent type is declared within
9048 -- a generic unit. As a result the corresponding record has been
9049 -- built and used as the type of the first formal, we just have
9050 -- to retrieve the corresponding concurrent type.
9051
8aa15e3b 9052 elsif Is_Concurrent_Record_Type (Typ)
dd54644b 9053 and then not Is_Class_Wide_Type (Typ)
8aa15e3b 9054 and then Present (Corresponding_Concurrent_Type (Typ))
5d37ba92 9055 then
8aa15e3b 9056 Typ := Corresponding_Concurrent_Type (Typ);
5d37ba92
ES
9057 In_Scope := False;
9058
9059 else
9060 return;
9061 end if;
8aa15e3b
JM
9062 end if;
9063
9064 -- There is no overriding to check if is an inherited operation in a
9065 -- type derivation on for a generic actual.
9066
9067 Collect_Interfaces (Typ, Ifaces_List);
9068
9069 if Is_Empty_Elmt_List (Ifaces_List) then
5d37ba92
ES
9070 return;
9071 end if;
9072
8aa15e3b
JM
9073 -- Determine whether entry or subprogram Def_Id overrides a primitive
9074 -- operation that belongs to one of the interfaces in Ifaces_List.
5d37ba92 9075
8aa15e3b
JM
9076 declare
9077 Candidate : Entity_Id := Empty;
9078 Hom : Entity_Id := Empty;
9079 Iface_Typ : Entity_Id;
9080 Subp : Entity_Id := Empty;
9081
9082 begin
4adf3c50 9083 -- Traverse the homonym chain, looking for a potentially
8aa15e3b
JM
9084 -- overridden subprogram that belongs to an implemented
9085 -- interface.
9086
9087 Hom := Current_Entity_In_Scope (Def_Id);
9088 while Present (Hom) loop
9089 Subp := Hom;
9090
15e4986c
JM
9091 if Subp = Def_Id
9092 or else not Is_Overloadable (Subp)
9093 or else not Is_Primitive (Subp)
9094 or else not Is_Dispatching_Operation (Subp)
79afa047 9095 or else not Present (Find_Dispatching_Type (Subp))
15e4986c 9096 or else not Is_Interface (Find_Dispatching_Type (Subp))
8aa15e3b 9097 then
15e4986c 9098 null;
8aa15e3b 9099
15e4986c 9100 -- Entries and procedures can override abstract or null
4adf3c50 9101 -- interface procedures.
8aa15e3b 9102
15e4986c 9103 elsif (Ekind (Def_Id) = E_Procedure
8fde064e 9104 or else Ekind (Def_Id) = E_Entry)
8aa15e3b 9105 and then Ekind (Subp) = E_Procedure
8aa15e3b
JM
9106 and then Matches_Prefixed_View_Profile
9107 (Parameter_Specifications (Parent (Def_Id)),
9108 Parameter_Specifications (Parent (Subp)))
9109 then
9110 Candidate := Subp;
9111
15e4986c
JM
9112 -- For an overridden subprogram Subp, check whether the mode
9113 -- of its first parameter is correct depending on the kind
9114 -- of synchronized type.
8aa15e3b 9115
15e4986c
JM
9116 declare
9117 Formal : constant Node_Id := First_Formal (Candidate);
9118
9119 begin
9120 -- In order for an entry or a protected procedure to
9121 -- override, the first parameter of the overridden
9122 -- routine must be of mode "out", "in out" or
9123 -- access-to-variable.
9124
8fde064e 9125 if Ekind_In (Candidate, E_Entry, E_Procedure)
15e4986c
JM
9126 and then Is_Protected_Type (Typ)
9127 and then Ekind (Formal) /= E_In_Out_Parameter
9128 and then Ekind (Formal) /= E_Out_Parameter
8fde064e
AC
9129 and then Nkind (Parameter_Type (Parent (Formal))) /=
9130 N_Access_Definition
15e4986c
JM
9131 then
9132 null;
9133
9134 -- All other cases are OK since a task entry or routine
9135 -- does not have a restriction on the mode of the first
9136 -- parameter of the overridden interface routine.
9137
9138 else
9139 Overridden_Subp := Candidate;
9140 return;
9141 end if;
9142 end;
8aa15e3b
JM
9143
9144 -- Functions can override abstract interface functions
9145
9146 elsif Ekind (Def_Id) = E_Function
9147 and then Ekind (Subp) = E_Function
8aa15e3b
JM
9148 and then Matches_Prefixed_View_Profile
9149 (Parameter_Specifications (Parent (Def_Id)),
9150 Parameter_Specifications (Parent (Subp)))
9151 and then Etype (Result_Definition (Parent (Def_Id))) =
9152 Etype (Result_Definition (Parent (Subp)))
9153 then
9154 Overridden_Subp := Subp;
9155 return;
9156 end if;
9157
9158 Hom := Homonym (Hom);
9159 end loop;
9160
4adf3c50
AC
9161 -- After examining all candidates for overriding, we are left with
9162 -- the best match which is a mode incompatible interface routine.
9163 -- Do not emit an error if the Expander is active since this error
9164 -- will be detected later on after all concurrent types are
9165 -- expanded and all wrappers are built. This check is meant for
9166 -- spec-only compilations.
8aa15e3b 9167
4adf3c50 9168 if Present (Candidate) and then not Expander_Active then
8aa15e3b
JM
9169 Iface_Typ :=
9170 Find_Parameter_Type (Parent (First_Formal (Candidate)));
9171
4adf3c50
AC
9172 -- Def_Id is primitive of a protected type, declared inside the
9173 -- type, and the candidate is primitive of a limited or
9174 -- synchronized interface.
8aa15e3b
JM
9175
9176 if In_Scope
9177 and then Is_Protected_Type (Typ)
9178 and then
9179 (Is_Limited_Interface (Iface_Typ)
c199ccf7
AC
9180 or else Is_Protected_Interface (Iface_Typ)
9181 or else Is_Synchronized_Interface (Iface_Typ)
9182 or else Is_Task_Interface (Iface_Typ))
8aa15e3b 9183 then
dd54644b 9184 Error_Msg_PT (Parent (Typ), Candidate);
8aa15e3b 9185 end if;
5d37ba92 9186 end if;
8aa15e3b
JM
9187
9188 Overridden_Subp := Candidate;
9189 return;
9190 end;
5d37ba92
ES
9191 end Check_Synchronized_Overriding;
9192
9193 ----------------------------
9194 -- Is_Private_Declaration --
9195 ----------------------------
9196
9197 function Is_Private_Declaration (E : Entity_Id) return Boolean is
9198 Priv_Decls : List_Id;
9199 Decl : constant Node_Id := Unit_Declaration_Node (E);
9200
9201 begin
9202 if Is_Package_Or_Generic_Package (Current_Scope)
9203 and then In_Private_Part (Current_Scope)
9204 then
9205 Priv_Decls :=
d12b19fa 9206 Private_Declarations (Package_Specification (Current_Scope));
5d37ba92
ES
9207
9208 return In_Package_Body (Current_Scope)
9209 or else
9210 (Is_List_Member (Decl)
a4901c08 9211 and then List_Containing (Decl) = Priv_Decls)
5d37ba92 9212 or else (Nkind (Parent (Decl)) = N_Package_Specification
a4901c08
AC
9213 and then not
9214 Is_Compilation_Unit
9215 (Defining_Entity (Parent (Decl)))
9216 and then List_Containing (Parent (Parent (Decl))) =
9217 Priv_Decls);
5d37ba92
ES
9218 else
9219 return False;
9220 end if;
9221 end Is_Private_Declaration;
996ae0b0 9222
2ddc2000
AC
9223 --------------------------
9224 -- Is_Overriding_Alias --
9225 --------------------------
9226
9227 function Is_Overriding_Alias
9228 (Old_E : Entity_Id;
9229 New_E : Entity_Id) return Boolean
9230 is
9231 AO : constant Entity_Id := Alias (Old_E);
9232 AN : constant Entity_Id := Alias (New_E);
2ddc2000
AC
9233 begin
9234 return Scope (AO) /= Scope (AN)
9235 or else No (DTC_Entity (AO))
9236 or else No (DTC_Entity (AN))
9237 or else DT_Position (AO) = DT_Position (AN);
9238 end Is_Overriding_Alias;
9239
996ae0b0
RK
9240 -- Start of processing for New_Overloaded_Entity
9241
9242 begin
fbf5a39b
AC
9243 -- We need to look for an entity that S may override. This must be a
9244 -- homonym in the current scope, so we look for the first homonym of
9245 -- S in the current scope as the starting point for the search.
9246
9247 E := Current_Entity_In_Scope (S);
9248
947430d5
AC
9249 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
9250 -- They are directly added to the list of primitive operations of
9251 -- Derived_Type, unless this is a rederivation in the private part
9252 -- of an operation that was already derived in the visible part of
9253 -- the current package.
9254
0791fbe9 9255 if Ada_Version >= Ada_2005
947430d5
AC
9256 and then Present (Derived_Type)
9257 and then Present (Alias (S))
9258 and then Is_Dispatching_Operation (Alias (S))
9259 and then Present (Find_Dispatching_Type (Alias (S)))
9260 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
9261 then
9262 -- For private types, when the full-view is processed we propagate to
9263 -- the full view the non-overridden entities whose attribute "alias"
9264 -- references an interface primitive. These entities were added by
9265 -- Derive_Subprograms to ensure that interface primitives are
9266 -- covered.
9267
9268 -- Inside_Freeze_Actions is non zero when S corresponds with an
9269 -- internal entity that links an interface primitive with its
9270 -- covering primitive through attribute Interface_Alias (see
4adf3c50 9271 -- Add_Internal_Interface_Entities).
947430d5
AC
9272
9273 if Inside_Freezing_Actions = 0
9274 and then Is_Package_Or_Generic_Package (Current_Scope)
9275 and then In_Private_Part (Current_Scope)
9276 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
9277 and then Nkind (Parent (S)) = N_Full_Type_Declaration
9278 and then Full_View (Defining_Identifier (Parent (E)))
9279 = Defining_Identifier (Parent (S))
9280 and then Alias (E) = Alias (S)
9281 then
9282 Check_Operation_From_Private_View (S, E);
9283 Set_Is_Dispatching_Operation (S);
9284
9285 -- Common case
9286
9287 else
9288 Enter_Overloaded_Entity (S);
9289 Check_Dispatching_Operation (S, Empty);
9290 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9291 end if;
9292
9293 return;
9294 end if;
9295
fbf5a39b
AC
9296 -- If there is no homonym then this is definitely not overriding
9297
996ae0b0
RK
9298 if No (E) then
9299 Enter_Overloaded_Entity (S);
9300 Check_Dispatching_Operation (S, Empty);
5d37ba92 9301 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
996ae0b0 9302
2995860f
AC
9303 -- If subprogram has an explicit declaration, check whether it has an
9304 -- overriding indicator.
758c442c 9305
ec4867fa 9306 if Comes_From_Source (S) then
8aa15e3b 9307 Check_Synchronized_Overriding (S, Overridden_Subp);
ea034236
AC
9308
9309 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
9310 -- it may have overridden some hidden inherited primitive. Update
308e6f3a 9311 -- Overridden_Subp to avoid spurious errors when checking the
ea034236
AC
9312 -- overriding indicator.
9313
9314 if Ada_Version >= Ada_2012
9315 and then No (Overridden_Subp)
9316 and then Is_Dispatching_Operation (S)
038140ed 9317 and then Present (Overridden_Operation (S))
ea034236
AC
9318 then
9319 Overridden_Subp := Overridden_Operation (S);
9320 end if;
9321
5d37ba92
ES
9322 Check_Overriding_Indicator
9323 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
758c442c
GD
9324 end if;
9325
fbf5a39b
AC
9326 -- If there is a homonym that is not overloadable, then we have an
9327 -- error, except for the special cases checked explicitly below.
9328
996ae0b0
RK
9329 elsif not Is_Overloadable (E) then
9330
9331 -- Check for spurious conflict produced by a subprogram that has the
9332 -- same name as that of the enclosing generic package. The conflict
9333 -- occurs within an instance, between the subprogram and the renaming
9334 -- declaration for the package. After the subprogram, the package
9335 -- renaming declaration becomes hidden.
9336
9337 if Ekind (E) = E_Package
9338 and then Present (Renamed_Object (E))
9339 and then Renamed_Object (E) = Current_Scope
9340 and then Nkind (Parent (Renamed_Object (E))) =
9341 N_Package_Specification
9342 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
9343 then
9344 Set_Is_Hidden (E);
9345 Set_Is_Immediately_Visible (E, False);
9346 Enter_Overloaded_Entity (S);
9347 Set_Homonym (S, Homonym (E));
9348 Check_Dispatching_Operation (S, Empty);
5d37ba92 9349 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
996ae0b0
RK
9350
9351 -- If the subprogram is implicit it is hidden by the previous
82c80734
RD
9352 -- declaration. However if it is dispatching, it must appear in the
9353 -- dispatch table anyway, because it can be dispatched to even if it
9354 -- cannot be called directly.
996ae0b0 9355
4adf3c50 9356 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
996ae0b0
RK
9357 Set_Scope (S, Current_Scope);
9358
9359 if Is_Dispatching_Operation (Alias (S)) then
9360 Check_Dispatching_Operation (S, Empty);
9361 end if;
9362
9363 return;
9364
9365 else
9366 Error_Msg_Sloc := Sloc (E);
996ae0b0 9367
f3d57416 9368 -- Generate message, with useful additional warning if in generic
996ae0b0
RK
9369
9370 if Is_Generic_Unit (E) then
5d37ba92
ES
9371 Error_Msg_N ("previous generic unit cannot be overloaded", S);
9372 Error_Msg_N ("\& conflicts with declaration#", S);
9373 else
9374 Error_Msg_N ("& conflicts with declaration#", S);
996ae0b0
RK
9375 end if;
9376
9377 return;
9378 end if;
9379
fbf5a39b
AC
9380 -- E exists and is overloadable
9381
996ae0b0 9382 else
8aa15e3b 9383 Check_Synchronized_Overriding (S, Overridden_Subp);
758c442c 9384
82c80734
RD
9385 -- Loop through E and its homonyms to determine if any of them is
9386 -- the candidate for overriding by S.
996ae0b0
RK
9387
9388 while Present (E) loop
fbf5a39b
AC
9389
9390 -- Definitely not interesting if not in the current scope
9391
996ae0b0
RK
9392 if Scope (E) /= Current_Scope then
9393 null;
9394
aca90db9
AC
9395 -- A function can overload the name of an abstract state. The
9396 -- state can be viewed as a function with a profile that cannot
9397 -- be matched by anything.
9398
9399 elsif Ekind (S) = E_Function
9400 and then Ekind (E) = E_Abstract_State
9401 then
9402 Enter_Overloaded_Entity (S);
9403 return;
9404
2995860f
AC
9405 -- Ada 2012 (AI05-0165): For internally generated bodies of null
9406 -- procedures locate the internally generated spec. We enforce
9407 -- mode conformance since a tagged type may inherit from
9408 -- interfaces several null primitives which differ only in
9409 -- the mode of the formals.
25ebc085
AC
9410
9411 elsif not Comes_From_Source (S)
9412 and then Is_Null_Procedure (S)
9413 and then not Mode_Conformant (E, S)
9414 then
9415 null;
9416
fbf5a39b
AC
9417 -- Check if we have type conformance
9418
ec4867fa 9419 elsif Type_Conformant (E, S) then
c8ef728f 9420
82c80734
RD
9421 -- If the old and new entities have the same profile and one
9422 -- is not the body of the other, then this is an error, unless
9423 -- one of them is implicitly declared.
996ae0b0
RK
9424
9425 -- There are some cases when both can be implicit, for example
9426 -- when both a literal and a function that overrides it are
f3d57416 9427 -- inherited in a derivation, or when an inherited operation
ec4867fa 9428 -- of a tagged full type overrides the inherited operation of
f3d57416 9429 -- a private extension. Ada 83 had a special rule for the
885c4871 9430 -- literal case. In Ada 95, the later implicit operation hides
82c80734
RD
9431 -- the former, and the literal is always the former. In the
9432 -- odd case where both are derived operations declared at the
9433 -- same point, both operations should be declared, and in that
9434 -- case we bypass the following test and proceed to the next
df46b832
AC
9435 -- part. This can only occur for certain obscure cases in
9436 -- instances, when an operation on a type derived from a formal
9437 -- private type does not override a homograph inherited from
9438 -- the actual. In subsequent derivations of such a type, the
9439 -- DT positions of these operations remain distinct, if they
9440 -- have been set.
996ae0b0
RK
9441
9442 if Present (Alias (S))
9443 and then (No (Alias (E))
9444 or else Comes_From_Source (E)
2ddc2000 9445 or else Is_Abstract_Subprogram (S)
df46b832
AC
9446 or else
9447 (Is_Dispatching_Operation (E)
84c0a895 9448 and then Is_Overriding_Alias (E, S)))
df46b832 9449 and then Ekind (E) /= E_Enumeration_Literal
996ae0b0 9450 then
82c80734
RD
9451 -- When an derived operation is overloaded it may be due to
9452 -- the fact that the full view of a private extension
996ae0b0
RK
9453 -- re-inherits. It has to be dealt with.
9454
e660dbf7 9455 if Is_Package_Or_Generic_Package (Current_Scope)
996ae0b0
RK
9456 and then In_Private_Part (Current_Scope)
9457 then
9458 Check_Operation_From_Private_View (S, E);
9459 end if;
9460
038140ed
AC
9461 -- In any case the implicit operation remains hidden by the
9462 -- existing declaration, which is overriding. Indicate that
9463 -- E overrides the operation from which S is inherited.
996ae0b0 9464
038140ed 9465 if Present (Alias (S)) then
039538bc
AC
9466 Set_Overridden_Operation (E, Alias (S));
9467 Inherit_Subprogram_Contract (E, Alias (S));
9468
038140ed 9469 else
039538bc
AC
9470 Set_Overridden_Operation (E, S);
9471 Inherit_Subprogram_Contract (E, S);
038140ed 9472 end if;
758c442c
GD
9473
9474 if Comes_From_Source (E) then
5d37ba92 9475 Check_Overriding_Indicator (E, S, Is_Primitive => False);
758c442c
GD
9476 end if;
9477
996ae0b0
RK
9478 return;
9479
26a43556
AC
9480 -- Within an instance, the renaming declarations for actual
9481 -- subprograms may become ambiguous, but they do not hide each
9482 -- other.
996ae0b0
RK
9483
9484 elsif Ekind (E) /= E_Entry
9485 and then not Comes_From_Source (E)
9486 and then not Is_Generic_Instance (E)
9487 and then (Present (Alias (E))
9488 or else Is_Intrinsic_Subprogram (E))
9489 and then (not In_Instance
9490 or else No (Parent (E))
9491 or else Nkind (Unit_Declaration_Node (E)) /=
8dbd1460 9492 N_Subprogram_Renaming_Declaration)
996ae0b0 9493 then
26a43556
AC
9494 -- A subprogram child unit is not allowed to override an
9495 -- inherited subprogram (10.1.1(20)).
996ae0b0
RK
9496
9497 if Is_Child_Unit (S) then
9498 Error_Msg_N
9499 ("child unit overrides inherited subprogram in parent",
9500 S);
9501 return;
9502 end if;
9503
9504 if Is_Non_Overriding_Operation (E, S) then
9505 Enter_Overloaded_Entity (S);
8dbd1460 9506
c8ef728f 9507 if No (Derived_Type)
996ae0b0
RK
9508 or else Is_Tagged_Type (Derived_Type)
9509 then
9510 Check_Dispatching_Operation (S, Empty);
9511 end if;
9512
9513 return;
9514 end if;
9515
9516 -- E is a derived operation or an internal operator which
9517 -- is being overridden. Remove E from further visibility.
9518 -- Furthermore, if E is a dispatching operation, it must be
9519 -- replaced in the list of primitive operations of its type
9520 -- (see Override_Dispatching_Operation).
9521
ec4867fa 9522 Overridden_Subp := E;
758c442c 9523
996ae0b0
RK
9524 declare
9525 Prev : Entity_Id;
9526
9527 begin
9528 Prev := First_Entity (Current_Scope);
8fde064e 9529 while Present (Prev) and then Next_Entity (Prev) /= E loop
996ae0b0
RK
9530 Next_Entity (Prev);
9531 end loop;
9532
9533 -- It is possible for E to be in the current scope and
9534 -- yet not in the entity chain. This can only occur in a
9535 -- generic context where E is an implicit concatenation
9536 -- in the formal part, because in a generic body the
9537 -- entity chain starts with the formals.
9538
9539 pragma Assert
9540 (Present (Prev) or else Chars (E) = Name_Op_Concat);
9541
9542 -- E must be removed both from the entity_list of the
9543 -- current scope, and from the visibility chain
9544
9545 if Debug_Flag_E then
9546 Write_Str ("Override implicit operation ");
9547 Write_Int (Int (E));
9548 Write_Eol;
9549 end if;
9550
9551 -- If E is a predefined concatenation, it stands for four
9552 -- different operations. As a result, a single explicit
9553 -- declaration does not hide it. In a possible ambiguous
9554 -- situation, Disambiguate chooses the user-defined op,
9555 -- so it is correct to retain the previous internal one.
9556
9557 if Chars (E) /= Name_Op_Concat
9558 or else Ekind (E) /= E_Operator
9559 then
9560 -- For nondispatching derived operations that are
9561 -- overridden by a subprogram declared in the private
8dbd1460
AC
9562 -- part of a package, we retain the derived subprogram
9563 -- but mark it as not immediately visible. If the
9564 -- derived operation was declared in the visible part
9565 -- then this ensures that it will still be visible
9566 -- outside the package with the proper signature
9567 -- (calls from outside must also be directed to this
9568 -- version rather than the overriding one, unlike the
9569 -- dispatching case). Calls from inside the package
9570 -- will still resolve to the overriding subprogram
9571 -- since the derived one is marked as not visible
9572 -- within the package.
996ae0b0
RK
9573
9574 -- If the private operation is dispatching, we achieve
9575 -- the overriding by keeping the implicit operation
9865d858 9576 -- but setting its alias to be the overriding one. In
996ae0b0
RK
9577 -- this fashion the proper body is executed in all
9578 -- cases, but the original signature is used outside
9579 -- of the package.
9580
9581 -- If the overriding is not in the private part, we
9582 -- remove the implicit operation altogether.
9583
9584 if Is_Private_Declaration (S) then
996ae0b0
RK
9585 if not Is_Dispatching_Operation (E) then
9586 Set_Is_Immediately_Visible (E, False);
9587 else
e895b435 9588 -- Work done in Override_Dispatching_Operation,
a46cde68 9589 -- so nothing else needs to be done here.
996ae0b0
RK
9590
9591 null;
9592 end if;
996ae0b0 9593
fbf5a39b
AC
9594 else
9595 -- Find predecessor of E in Homonym chain
996ae0b0
RK
9596
9597 if E = Current_Entity (E) then
9598 Prev_Vis := Empty;
9599 else
9600 Prev_Vis := Current_Entity (E);
9601 while Homonym (Prev_Vis) /= E loop
9602 Prev_Vis := Homonym (Prev_Vis);
9603 end loop;
9604 end if;
9605
9606 if Prev_Vis /= Empty then
9607
9608 -- Skip E in the visibility chain
9609
9610 Set_Homonym (Prev_Vis, Homonym (E));
9611
9612 else
9613 Set_Name_Entity_Id (Chars (E), Homonym (E));
9614 end if;
9615
9616 Set_Next_Entity (Prev, Next_Entity (E));
9617
9618 if No (Next_Entity (Prev)) then
9619 Set_Last_Entity (Current_Scope, Prev);
9620 end if;
996ae0b0
RK
9621 end if;
9622 end if;
9623
9624 Enter_Overloaded_Entity (S);
1c1289e7
AC
9625
9626 -- For entities generated by Derive_Subprograms the
9627 -- overridden operation is the inherited primitive
9628 -- (which is available through the attribute alias).
9629
9630 if not (Comes_From_Source (E))
9631 and then Is_Dispatching_Operation (E)
f9673bb0
AC
9632 and then Find_Dispatching_Type (E) =
9633 Find_Dispatching_Type (S)
1c1289e7
AC
9634 and then Present (Alias (E))
9635 and then Comes_From_Source (Alias (E))
9636 then
039538bc
AC
9637 Set_Overridden_Operation (S, Alias (E));
9638 Inherit_Subprogram_Contract (S, Alias (E));
2fe829ae 9639
6320f5e1
AC
9640 -- Normal case of setting entity as overridden
9641
9642 -- Note: Static_Initialization and Overridden_Operation
9643 -- attributes use the same field in subprogram entities.
9644 -- Static_Initialization is only defined for internal
9645 -- initialization procedures, where Overridden_Operation
9646 -- is irrelevant. Therefore the setting of this attribute
9647 -- must check whether the target is an init_proc.
9648
2fe829ae 9649 elsif not Is_Init_Proc (S) then
039538bc
AC
9650 Set_Overridden_Operation (S, E);
9651 Inherit_Subprogram_Contract (S, E);
1c1289e7
AC
9652 end if;
9653
5d37ba92 9654 Check_Overriding_Indicator (S, E, Is_Primitive => True);
996ae0b0 9655
fc53fe76 9656 -- If S is a user-defined subprogram or a null procedure
38ef8ebe
AC
9657 -- expanded to override an inherited null procedure, or a
9658 -- predefined dispatching primitive then indicate that E
038140ed 9659 -- overrides the operation from which S is inherited.
fc53fe76
AC
9660
9661 if Comes_From_Source (S)
9662 or else
9663 (Present (Parent (S))
9664 and then
9665 Nkind (Parent (S)) = N_Procedure_Specification
9666 and then
9667 Null_Present (Parent (S)))
38ef8ebe
AC
9668 or else
9669 (Present (Alias (E))
f16e8df9
RD
9670 and then
9671 Is_Predefined_Dispatching_Operation (Alias (E)))
fc53fe76 9672 then
c8ef728f 9673 if Present (Alias (E)) then
039538bc
AC
9674 Set_Overridden_Operation (S, Alias (E));
9675 Inherit_Subprogram_Contract (S, Alias (E));
41251c60
JM
9676 end if;
9677 end if;
9678
996ae0b0 9679 if Is_Dispatching_Operation (E) then
fbf5a39b 9680
82c80734 9681 -- An overriding dispatching subprogram inherits the
f9673bb0 9682 -- convention of the overridden subprogram (AI-117).
996ae0b0
RK
9683
9684 Set_Convention (S, Convention (E));
41251c60
JM
9685 Check_Dispatching_Operation (S, E);
9686
996ae0b0
RK
9687 else
9688 Check_Dispatching_Operation (S, Empty);
9689 end if;
9690
5d37ba92
ES
9691 Check_For_Primitive_Subprogram
9692 (Is_Primitive_Subp, Is_Overriding => True);
996ae0b0
RK
9693 goto Check_Inequality;
9694 end;
9695
9696 -- Apparent redeclarations in instances can occur when two
9697 -- formal types get the same actual type. The subprograms in
9698 -- in the instance are legal, even if not callable from the
9699 -- outside. Calls from within are disambiguated elsewhere.
9700 -- For dispatching operations in the visible part, the usual
9701 -- rules apply, and operations with the same profile are not
9702 -- legal (B830001).
9703
9704 elsif (In_Instance_Visible_Part
9705 and then not Is_Dispatching_Operation (E))
9706 or else In_Instance_Not_Visible
9707 then
9708 null;
9709
9710 -- Here we have a real error (identical profile)
9711
9712 else
9713 Error_Msg_Sloc := Sloc (E);
9714
9715 -- Avoid cascaded errors if the entity appears in
9716 -- subsequent calls.
9717
9718 Set_Scope (S, Current_Scope);
9719
5d37ba92
ES
9720 -- Generate error, with extra useful warning for the case
9721 -- of a generic instance with no completion.
996ae0b0
RK
9722
9723 if Is_Generic_Instance (S)
9724 and then not Has_Completion (E)
9725 then
9726 Error_Msg_N
5d37ba92
ES
9727 ("instantiation cannot provide body for&", S);
9728 Error_Msg_N ("\& conflicts with declaration#", S);
9729 else
9730 Error_Msg_N ("& conflicts with declaration#", S);
996ae0b0
RK
9731 end if;
9732
9733 return;
9734 end if;
9735
9736 else
c8ef728f
ES
9737 -- If one subprogram has an access parameter and the other
9738 -- a parameter of an access type, calls to either might be
9739 -- ambiguous. Verify that parameters match except for the
9740 -- access parameter.
9741
9742 if May_Hide_Profile then
9743 declare
ec4867fa
ES
9744 F1 : Entity_Id;
9745 F2 : Entity_Id;
8dbd1460 9746
c8ef728f
ES
9747 begin
9748 F1 := First_Formal (S);
9749 F2 := First_Formal (E);
9750 while Present (F1) and then Present (F2) loop
9751 if Is_Access_Type (Etype (F1)) then
9752 if not Is_Access_Type (Etype (F2))
9753 or else not Conforming_Types
9754 (Designated_Type (Etype (F1)),
9755 Designated_Type (Etype (F2)),
9756 Type_Conformant)
9757 then
9758 May_Hide_Profile := False;
9759 end if;
9760
9761 elsif
9762 not Conforming_Types
9763 (Etype (F1), Etype (F2), Type_Conformant)
9764 then
9765 May_Hide_Profile := False;
9766 end if;
9767
9768 Next_Formal (F1);
9769 Next_Formal (F2);
9770 end loop;
9771
9772 if May_Hide_Profile
9773 and then No (F1)
9774 and then No (F2)
9775 then
dbfeb4fa 9776 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
c8ef728f
ES
9777 end if;
9778 end;
9779 end if;
996ae0b0
RK
9780 end if;
9781
996ae0b0
RK
9782 E := Homonym (E);
9783 end loop;
9784
9785 -- On exit, we know that S is a new entity
9786
9787 Enter_Overloaded_Entity (S);
5d37ba92
ES
9788 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9789 Check_Overriding_Indicator
9790 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
996ae0b0 9791
c4d67e2d 9792 -- Overloading is not allowed in SPARK, except for operators
8ed68165 9793
c4d67e2d
AC
9794 if Nkind (S) /= N_Defining_Operator_Symbol then
9795 Error_Msg_Sloc := Sloc (Homonym (S));
ce5ba43a 9796 Check_SPARK_05_Restriction
c4d67e2d
AC
9797 ("overloading not allowed with entity#", S);
9798 end if;
8ed68165 9799
82c80734
RD
9800 -- If S is a derived operation for an untagged type then by
9801 -- definition it's not a dispatching operation (even if the parent
e917aec2
RD
9802 -- operation was dispatching), so Check_Dispatching_Operation is not
9803 -- called in that case.
996ae0b0 9804
c8ef728f 9805 if No (Derived_Type)
996ae0b0
RK
9806 or else Is_Tagged_Type (Derived_Type)
9807 then
9808 Check_Dispatching_Operation (S, Empty);
9809 end if;
9810 end if;
9811
82c80734
RD
9812 -- If this is a user-defined equality operator that is not a derived
9813 -- subprogram, create the corresponding inequality. If the operation is
9814 -- dispatching, the expansion is done elsewhere, and we do not create
9815 -- an explicit inequality operation.
996ae0b0
RK
9816
9817 <<Check_Inequality>>
9818 if Chars (S) = Name_Op_Eq
9819 and then Etype (S) = Standard_Boolean
9820 and then Present (Parent (S))
9821 and then not Is_Dispatching_Operation (S)
9822 then
9823 Make_Inequality_Operator (S);
b2834fbd 9824 Check_Untagged_Equality (S);
996ae0b0 9825 end if;
996ae0b0
RK
9826 end New_Overloaded_Entity;
9827
9828 ---------------------
9829 -- Process_Formals --
9830 ---------------------
9831
9832 procedure Process_Formals
07fc65c4 9833 (T : List_Id;
996ae0b0
RK
9834 Related_Nod : Node_Id)
9835 is
9836 Param_Spec : Node_Id;
9837 Formal : Entity_Id;
9838 Formal_Type : Entity_Id;
9839 Default : Node_Id;
9840 Ptype : Entity_Id;
9841
800621e0
RD
9842 Num_Out_Params : Nat := 0;
9843 First_Out_Param : Entity_Id := Empty;
21d27997 9844 -- Used for setting Is_Only_Out_Parameter
800621e0 9845
7b56a91b 9846 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
950d217a
AC
9847 -- Determine whether an access type designates a type coming from a
9848 -- limited view.
9849
07fc65c4 9850 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
82c80734
RD
9851 -- Check whether the default has a class-wide type. After analysis the
9852 -- default has the type of the formal, so we must also check explicitly
9853 -- for an access attribute.
07fc65c4 9854
7b56a91b
AC
9855 ----------------------------------
9856 -- Designates_From_Limited_With --
9857 ----------------------------------
950d217a 9858
7b56a91b 9859 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
950d217a
AC
9860 Desig : Entity_Id := Typ;
9861
9862 begin
9863 if Is_Access_Type (Desig) then
9864 Desig := Directly_Designated_Type (Desig);
9865 end if;
9866
9867 if Is_Class_Wide_Type (Desig) then
9868 Desig := Root_Type (Desig);
9869 end if;
9870
9871 return
7b56a91b
AC
9872 Ekind (Desig) = E_Incomplete_Type
9873 and then From_Limited_With (Desig);
9874 end Designates_From_Limited_With;
950d217a 9875
07fc65c4
GB
9876 ---------------------------
9877 -- Is_Class_Wide_Default --
9878 ---------------------------
9879
9880 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
9881 begin
9882 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
9883 or else (Nkind (D) = N_Attribute_Reference
0f853035
YM
9884 and then Attribute_Name (D) = Name_Access
9885 and then Is_Class_Wide_Type (Etype (Prefix (D))));
07fc65c4
GB
9886 end Is_Class_Wide_Default;
9887
9888 -- Start of processing for Process_Formals
9889
996ae0b0
RK
9890 begin
9891 -- In order to prevent premature use of the formals in the same formal
9892 -- part, the Ekind is left undefined until all default expressions are
9893 -- analyzed. The Ekind is established in a separate loop at the end.
9894
9895 Param_Spec := First (T);
996ae0b0 9896 while Present (Param_Spec) loop
996ae0b0 9897 Formal := Defining_Identifier (Param_Spec);
5d37ba92 9898 Set_Never_Set_In_Source (Formal, True);
996ae0b0
RK
9899 Enter_Name (Formal);
9900
9901 -- Case of ordinary parameters
9902
9903 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
9904 Find_Type (Parameter_Type (Param_Spec));
9905 Ptype := Parameter_Type (Param_Spec);
9906
9907 if Ptype = Error then
9908 goto Continue;
9909 end if;
9910
9911 Formal_Type := Entity (Ptype);
9912
ec4867fa
ES
9913 if Is_Incomplete_Type (Formal_Type)
9914 or else
9915 (Is_Class_Wide_Type (Formal_Type)
8fde064e 9916 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
996ae0b0 9917 then
93bcda23
AC
9918 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
9919 -- primitive operations, as long as their completion is
9920 -- in the same declarative part. If in the private part
9921 -- this means that the type cannot be a Taft-amendment type.
cec29135
ES
9922 -- Check is done on package exit. For access to subprograms,
9923 -- the use is legal for Taft-amendment types.
fbf5a39b 9924
6eddd7b4
AC
9925 -- Ada 2012: tagged incomplete types are allowed as generic
9926 -- formal types. They do not introduce dependencies and the
9927 -- corresponding generic subprogram does not have a delayed
5b6f12c7
AC
9928 -- freeze, because it does not need a freeze node. However,
9929 -- it is still the case that untagged incomplete types cannot
9930 -- be Taft-amendment types and must be completed in private
9931 -- part, so the subprogram must appear in the list of private
9932 -- dependents of the type.
9933
9934 if Is_Tagged_Type (Formal_Type)
b973629e 9935 or else (Ada_Version >= Ada_2012
1ebc2612
AC
9936 and then not From_Limited_With (Formal_Type)
9937 and then not Is_Generic_Type (Formal_Type))
5b6f12c7 9938 then
93bcda23 9939 if Ekind (Scope (Current_Scope)) = E_Package
6eddd7b4 9940 and then not Is_Generic_Type (Formal_Type)
93bcda23
AC
9941 and then not Is_Class_Wide_Type (Formal_Type)
9942 then
cec29135
ES
9943 if not Nkind_In
9944 (Parent (T), N_Access_Function_Definition,
9945 N_Access_Procedure_Definition)
9946 then
9947 Append_Elmt
9948 (Current_Scope,
09c954dc 9949 To => Private_Dependents (Base_Type (Formal_Type)));
4637729f
AC
9950
9951 -- Freezing is delayed to ensure that Register_Prim
9952 -- will get called for this operation, which is needed
9953 -- in cases where static dispatch tables aren't built.
9954 -- (Note that the same is done for controlling access
9955 -- parameter cases in function Access_Definition.)
9956
13fa2acb
AC
9957 if not Is_Thunk (Current_Scope) then
9958 Set_Has_Delayed_Freeze (Current_Scope);
9959 end if;
cec29135 9960 end if;
93bcda23 9961 end if;
fbf5a39b 9962
0a36105d
JM
9963 -- Special handling of Value_Type for CIL case
9964
9965 elsif Is_Value_Type (Formal_Type) then
9966 null;
9967
800621e0
RD
9968 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
9969 N_Access_Procedure_Definition)
996ae0b0 9970 then
dd386db0
AC
9971 -- AI05-0151: Tagged incomplete types are allowed in all
9972 -- formal parts. Untagged incomplete types are not allowed
b973629e
AC
9973 -- in bodies. Limited views of either kind are not allowed
9974 -- if there is no place at which the non-limited view can
9975 -- become available.
a18e3d62 9976
1ebc2612
AC
9977 -- Incomplete formal untagged types are not allowed in
9978 -- subprogram bodies (but are legal in their declarations).
9979
9980 if Is_Generic_Type (Formal_Type)
9981 and then not Is_Tagged_Type (Formal_Type)
9982 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
9983 then
9984 Error_Msg_N
9985 ("invalid use of formal incomplete type", Param_Spec);
dd386db0 9986
1ebc2612 9987 elsif Ada_Version >= Ada_2012 then
b973629e
AC
9988 if Is_Tagged_Type (Formal_Type)
9989 and then (not From_Limited_With (Formal_Type)
9990 or else not In_Package_Body)
9991 then
dd386db0
AC
9992 null;
9993
0f1a6a0b 9994 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
b973629e 9995 N_Accept_Alternative,
0f1a6a0b
AC
9996 N_Entry_Body,
9997 N_Subprogram_Body)
dd386db0
AC
9998 then
9999 Error_Msg_NE
10000 ("invalid use of untagged incomplete type&",
0f1a6a0b 10001 Ptype, Formal_Type);
dd386db0
AC
10002 end if;
10003
10004 else
10005 Error_Msg_NE
10006 ("invalid use of incomplete type&",
0f1a6a0b 10007 Param_Spec, Formal_Type);
dd386db0
AC
10008
10009 -- Further checks on the legality of incomplete types
10010 -- in formal parts are delayed until the freeze point
10011 -- of the enclosing subprogram or access to subprogram.
10012 end if;
996ae0b0
RK
10013 end if;
10014
10015 elsif Ekind (Formal_Type) = E_Void then
0f1a6a0b
AC
10016 Error_Msg_NE
10017 ("premature use of&",
10018 Parameter_Type (Param_Spec), Formal_Type);
996ae0b0
RK
10019 end if;
10020
fecbd779
AC
10021 -- Ada 2012 (AI-142): Handle aliased parameters
10022
10023 if Ada_Version >= Ada_2012
10024 and then Aliased_Present (Param_Spec)
10025 then
10026 Set_Is_Aliased (Formal);
10027 end if;
10028
0ab80019 10029 -- Ada 2005 (AI-231): Create and decorate an internal subtype
7324bf49 10030 -- declaration corresponding to the null-excluding type of the
d8db0bca
JM
10031 -- formal in the enclosing scope. Finally, replace the parameter
10032 -- type of the formal with the internal subtype.
7324bf49 10033
0791fbe9 10034 if Ada_Version >= Ada_2005
41251c60 10035 and then Null_Exclusion_Present (Param_Spec)
7324bf49 10036 then
ec4867fa 10037 if not Is_Access_Type (Formal_Type) then
ed2233dc 10038 Error_Msg_N
0a36105d
JM
10039 ("`NOT NULL` allowed only for an access type", Param_Spec);
10040
ec4867fa
ES
10041 else
10042 if Can_Never_Be_Null (Formal_Type)
10043 and then Comes_From_Source (Related_Nod)
10044 then
ed2233dc 10045 Error_Msg_NE
0a36105d 10046 ("`NOT NULL` not allowed (& already excludes null)",
0f1a6a0b 10047 Param_Spec, Formal_Type);
ec4867fa 10048 end if;
41251c60 10049
ec4867fa
ES
10050 Formal_Type :=
10051 Create_Null_Excluding_Itype
10052 (T => Formal_Type,
10053 Related_Nod => Related_Nod,
10054 Scope_Id => Scope (Current_Scope));
0a36105d 10055
fcf848c4
AC
10056 -- If the designated type of the itype is an itype that is
10057 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10058 -- on the access subtype, to prevent order-of-elaboration
10059 -- issues in the backend.
0a36105d
JM
10060
10061 -- Example:
10062 -- type T is access procedure;
10063 -- procedure Op (O : not null T);
10064
fcf848c4
AC
10065 if Is_Itype (Directly_Designated_Type (Formal_Type))
10066 and then
10067 not Is_Frozen (Directly_Designated_Type (Formal_Type))
10068 then
0a36105d
JM
10069 Set_Has_Delayed_Freeze (Formal_Type);
10070 end if;
ec4867fa 10071 end if;
7324bf49
AC
10072 end if;
10073
996ae0b0
RK
10074 -- An access formal type
10075
10076 else
10077 Formal_Type :=
10078 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
7324bf49 10079
f937473f
RD
10080 -- No need to continue if we already notified errors
10081
10082 if not Present (Formal_Type) then
10083 return;
10084 end if;
10085
0ab80019 10086 -- Ada 2005 (AI-254)
7324bf49 10087
af4b9434
AC
10088 declare
10089 AD : constant Node_Id :=
10090 Access_To_Subprogram_Definition
10091 (Parameter_Type (Param_Spec));
10092 begin
10093 if Present (AD) and then Protected_Present (AD) then
10094 Formal_Type :=
10095 Replace_Anonymous_Access_To_Protected_Subprogram
f937473f 10096 (Param_Spec);
af4b9434
AC
10097 end if;
10098 end;
996ae0b0
RK
10099 end if;
10100
10101 Set_Etype (Formal, Formal_Type);
0f853035 10102
fecbd779
AC
10103 -- Deal with default expression if present
10104
fbf5a39b 10105 Default := Expression (Param_Spec);
996ae0b0
RK
10106
10107 if Present (Default) then
ce5ba43a 10108 Check_SPARK_05_Restriction
fe5d3068 10109 ("default expression is not allowed", Default);
38171f43 10110
996ae0b0 10111 if Out_Present (Param_Spec) then
ed2233dc 10112 Error_Msg_N
996ae0b0
RK
10113 ("default initialization only allowed for IN parameters",
10114 Param_Spec);
10115 end if;
10116
10117 -- Do the special preanalysis of the expression (see section on
10118 -- "Handling of Default Expressions" in the spec of package Sem).
10119
21d27997 10120 Preanalyze_Spec_Expression (Default, Formal_Type);
996ae0b0 10121
f29b857f
ES
10122 -- An access to constant cannot be the default for
10123 -- an access parameter that is an access to variable.
2eb160f2
ST
10124
10125 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10126 and then not Is_Access_Constant (Formal_Type)
10127 and then Is_Access_Type (Etype (Default))
10128 and then Is_Access_Constant (Etype (Default))
10129 then
f29b857f 10130 Error_Msg_N
84c0a895
AC
10131 ("formal that is access to variable cannot be initialized "
10132 & "with an access-to-constant expression", Default);
2eb160f2
ST
10133 end if;
10134
d8db0bca
JM
10135 -- Check that the designated type of an access parameter's default
10136 -- is not a class-wide type unless the parameter's designated type
10137 -- is also class-wide.
996ae0b0
RK
10138
10139 if Ekind (Formal_Type) = E_Anonymous_Access_Type
7b56a91b 10140 and then not Designates_From_Limited_With (Formal_Type)
07fc65c4 10141 and then Is_Class_Wide_Default (Default)
996ae0b0
RK
10142 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
10143 then
07fc65c4
GB
10144 Error_Msg_N
10145 ("access to class-wide expression not allowed here", Default);
996ae0b0 10146 end if;
4755cce9
JM
10147
10148 -- Check incorrect use of dynamically tagged expressions
10149
10150 if Is_Tagged_Type (Formal_Type) then
10151 Check_Dynamically_Tagged_Expression
10152 (Expr => Default,
10153 Typ => Formal_Type,
10154 Related_Nod => Default);
10155 end if;
996ae0b0
RK
10156 end if;
10157
41251c60
JM
10158 -- Ada 2005 (AI-231): Static checks
10159
0791fbe9 10160 if Ada_Version >= Ada_2005
41251c60
JM
10161 and then Is_Access_Type (Etype (Formal))
10162 and then Can_Never_Be_Null (Etype (Formal))
10163 then
10164 Null_Exclusion_Static_Checks (Param_Spec);
10165 end if;
10166
f1bd0415
AC
10167 -- The following checks are relevant when SPARK_Mode is on as these
10168 -- are not standard Ada legality rules.
6c3c671e 10169
ea26c8e4
HK
10170 if SPARK_Mode = On then
10171 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
f1bd0415 10172
ea26c8e4
HK
10173 -- A function cannot have a parameter of mode IN OUT or OUT
10174 -- (SPARK RM 6.1).
f1bd0415 10175
ea26c8e4
HK
10176 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
10177 Error_Msg_N
10178 ("function cannot have parameter of mode `OUT` or "
10179 & "`IN OUT`", Formal);
f1bd0415 10180
d780e54f
AC
10181 -- A function cannot have an effectively volatile formal
10182 -- parameter (SPARK RM 7.1.3(10)).
ea26c8e4 10183
d780e54f 10184 elsif Is_Effectively_Volatile (Formal) then
ea26c8e4
HK
10185 Error_Msg_N
10186 ("function cannot have a volatile formal parameter",
10187 Formal);
10188 end if;
10189
d780e54f
AC
10190 -- A procedure cannot have an effectively volatile formal
10191 -- parameter of mode IN because it behaves as a constant
10192 -- (SPARK RM 7.1.3(6)).
ea26c8e4
HK
10193
10194 elsif Ekind (Scope (Formal)) = E_Procedure
10195 and then Ekind (Formal) = E_In_Parameter
d780e54f 10196 and then Is_Effectively_Volatile (Formal)
ea26c8e4 10197 then
f1bd0415 10198 Error_Msg_N
ea26c8e4 10199 ("formal parameter of mode `IN` cannot be volatile", Formal);
f1bd0415 10200 end if;
6c3c671e
AC
10201 end if;
10202
996ae0b0
RK
10203 <<Continue>>
10204 Next (Param_Spec);
10205 end loop;
10206
82c80734
RD
10207 -- If this is the formal part of a function specification, analyze the
10208 -- subtype mark in the context where the formals are visible but not
10209 -- yet usable, and may hide outer homographs.
10210
10211 if Nkind (Related_Nod) = N_Function_Specification then
10212 Analyze_Return_Type (Related_Nod);
10213 end if;
10214
996ae0b0
RK
10215 -- Now set the kind (mode) of each formal
10216
10217 Param_Spec := First (T);
996ae0b0
RK
10218 while Present (Param_Spec) loop
10219 Formal := Defining_Identifier (Param_Spec);
10220 Set_Formal_Mode (Formal);
10221
10222 if Ekind (Formal) = E_In_Parameter then
10223 Set_Default_Value (Formal, Expression (Param_Spec));
10224
10225 if Present (Expression (Param_Spec)) then
10226 Default := Expression (Param_Spec);
10227
10228 if Is_Scalar_Type (Etype (Default)) then
5ebfaacf
AC
10229 if Nkind (Parameter_Type (Param_Spec)) /=
10230 N_Access_Definition
996ae0b0
RK
10231 then
10232 Formal_Type := Entity (Parameter_Type (Param_Spec));
996ae0b0 10233 else
5ebfaacf
AC
10234 Formal_Type :=
10235 Access_Definition
10236 (Related_Nod, Parameter_Type (Param_Spec));
996ae0b0
RK
10237 end if;
10238
10239 Apply_Scalar_Range_Check (Default, Formal_Type);
10240 end if;
2820d220 10241 end if;
800621e0
RD
10242
10243 elsif Ekind (Formal) = E_Out_Parameter then
10244 Num_Out_Params := Num_Out_Params + 1;
10245
10246 if Num_Out_Params = 1 then
10247 First_Out_Param := Formal;
10248 end if;
10249
10250 elsif Ekind (Formal) = E_In_Out_Parameter then
10251 Num_Out_Params := Num_Out_Params + 1;
996ae0b0
RK
10252 end if;
10253
4172a8e3
AC
10254 -- Skip remaining processing if formal type was in error
10255
10256 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
10257 goto Next_Parameter;
10258 end if;
10259
fecbd779
AC
10260 -- Force call by reference if aliased
10261
10262 if Is_Aliased (Formal) then
10263 Set_Mechanism (Formal, By_Reference);
5ebfaacf
AC
10264
10265 -- Warn if user asked this to be passed by copy
10266
10267 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10268 Error_Msg_N
b785e0b8 10269 ("cannot pass aliased parameter & by copy??", Formal);
5ebfaacf
AC
10270 end if;
10271
10272 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
10273
10274 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10275 Set_Mechanism (Formal, By_Copy);
10276
10277 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
10278 Set_Mechanism (Formal, By_Reference);
fecbd779
AC
10279 end if;
10280
4172a8e3 10281 <<Next_Parameter>>
996ae0b0
RK
10282 Next (Param_Spec);
10283 end loop;
800621e0
RD
10284
10285 if Present (First_Out_Param) and then Num_Out_Params = 1 then
10286 Set_Is_Only_Out_Parameter (First_Out_Param);
10287 end if;
996ae0b0
RK
10288 end Process_Formals;
10289
fbf5a39b
AC
10290 ----------------------------
10291 -- Reference_Body_Formals --
10292 ----------------------------
10293
10294 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
10295 Fs : Entity_Id;
10296 Fb : Entity_Id;
10297
10298 begin
10299 if Error_Posted (Spec) then
10300 return;
10301 end if;
10302
0a36105d
JM
10303 -- Iterate over both lists. They may be of different lengths if the two
10304 -- specs are not conformant.
10305
fbf5a39b
AC
10306 Fs := First_Formal (Spec);
10307 Fb := First_Formal (Bod);
0a36105d 10308 while Present (Fs) and then Present (Fb) loop
fbf5a39b
AC
10309 Generate_Reference (Fs, Fb, 'b');
10310
10311 if Style_Check then
10312 Style.Check_Identifier (Fb, Fs);
10313 end if;
10314
10315 Set_Spec_Entity (Fb, Fs);
10316 Set_Referenced (Fs, False);
10317 Next_Formal (Fs);
10318 Next_Formal (Fb);
10319 end loop;
10320 end Reference_Body_Formals;
10321
996ae0b0
RK
10322 -------------------------
10323 -- Set_Actual_Subtypes --
10324 -------------------------
10325
10326 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
84c0a895
AC
10327 Decl : Node_Id;
10328 Formal : Entity_Id;
10329 T : Entity_Id;
10330 First_Stmt : Node_Id := Empty;
10331 AS_Needed : Boolean;
996ae0b0
RK
10332
10333 begin
f3d57416 10334 -- If this is an empty initialization procedure, no need to create
fbf5a39b
AC
10335 -- actual subtypes (small optimization).
10336
8fde064e 10337 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
fbf5a39b
AC
10338 return;
10339 end if;
10340
996ae0b0
RK
10341 Formal := First_Formal (Subp);
10342 while Present (Formal) loop
10343 T := Etype (Formal);
10344
e895b435 10345 -- We never need an actual subtype for a constrained formal
996ae0b0
RK
10346
10347 if Is_Constrained (T) then
10348 AS_Needed := False;
10349
82c80734 10350 -- If we have unknown discriminants, then we do not need an actual
a90bd866 10351 -- subtype, or more accurately we cannot figure it out. Note that
82c80734 10352 -- all class-wide types have unknown discriminants.
996ae0b0
RK
10353
10354 elsif Has_Unknown_Discriminants (T) then
10355 AS_Needed := False;
10356
82c80734
RD
10357 -- At this stage we have an unconstrained type that may need an
10358 -- actual subtype. For sure the actual subtype is needed if we have
10359 -- an unconstrained array type.
996ae0b0
RK
10360
10361 elsif Is_Array_Type (T) then
10362 AS_Needed := True;
10363
d8db0bca
JM
10364 -- The only other case needing an actual subtype is an unconstrained
10365 -- record type which is an IN parameter (we cannot generate actual
10366 -- subtypes for the OUT or IN OUT case, since an assignment can
10367 -- change the discriminant values. However we exclude the case of
10368 -- initialization procedures, since discriminants are handled very
10369 -- specially in this context, see the section entitled "Handling of
10370 -- Discriminants" in Einfo.
10371
10372 -- We also exclude the case of Discrim_SO_Functions (functions used
10373 -- in front end layout mode for size/offset values), since in such
10374 -- functions only discriminants are referenced, and not only are such
10375 -- subtypes not needed, but they cannot always be generated, because
10376 -- of order of elaboration issues.
996ae0b0
RK
10377
10378 elsif Is_Record_Type (T)
10379 and then Ekind (Formal) = E_In_Parameter
10380 and then Chars (Formal) /= Name_uInit
5d09245e 10381 and then not Is_Unchecked_Union (T)
996ae0b0
RK
10382 and then not Is_Discrim_SO_Function (Subp)
10383 then
10384 AS_Needed := True;
10385
10386 -- All other cases do not need an actual subtype
10387
10388 else
10389 AS_Needed := False;
10390 end if;
10391
10392 -- Generate actual subtypes for unconstrained arrays and
10393 -- unconstrained discriminated records.
10394
10395 if AS_Needed then
7324bf49 10396 if Nkind (N) = N_Accept_Statement then
fbf5a39b 10397
57a8057a 10398 -- If expansion is active, the formal is replaced by a local
fbf5a39b
AC
10399 -- variable that renames the corresponding entry of the
10400 -- parameter block, and it is this local variable that may
da94696d 10401 -- require an actual subtype.
fbf5a39b 10402
4460a9bc 10403 if Expander_Active then
fbf5a39b
AC
10404 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
10405 else
10406 Decl := Build_Actual_Subtype (T, Formal);
10407 end if;
10408
996ae0b0
RK
10409 if Present (Handled_Statement_Sequence (N)) then
10410 First_Stmt :=
10411 First (Statements (Handled_Statement_Sequence (N)));
10412 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
10413 Mark_Rewrite_Insertion (Decl);
10414 else
82c80734
RD
10415 -- If the accept statement has no body, there will be no
10416 -- reference to the actuals, so no need to compute actual
10417 -- subtypes.
996ae0b0
RK
10418
10419 return;
10420 end if;
10421
10422 else
fbf5a39b 10423 Decl := Build_Actual_Subtype (T, Formal);
996ae0b0
RK
10424 Prepend (Decl, Declarations (N));
10425 Mark_Rewrite_Insertion (Decl);
10426 end if;
10427
82c80734
RD
10428 -- The declaration uses the bounds of an existing object, and
10429 -- therefore needs no constraint checks.
2820d220 10430
7324bf49 10431 Analyze (Decl, Suppress => All_Checks);
2820d220 10432
996ae0b0
RK
10433 -- We need to freeze manually the generated type when it is
10434 -- inserted anywhere else than in a declarative part.
10435
10436 if Present (First_Stmt) then
10437 Insert_List_Before_And_Analyze (First_Stmt,
c159409f 10438 Freeze_Entity (Defining_Identifier (Decl), N));
fcadacf7
ES
10439
10440 -- Ditto if the type has a dynamic predicate, because the
10441 -- generated function will mention the actual subtype.
10442
10443 elsif Has_Dynamic_Predicate_Aspect (T) then
10444 Insert_List_Before_And_Analyze (Decl,
10445 Freeze_Entity (Defining_Identifier (Decl), N));
996ae0b0
RK
10446 end if;
10447
fbf5a39b 10448 if Nkind (N) = N_Accept_Statement
4460a9bc 10449 and then Expander_Active
fbf5a39b
AC
10450 then
10451 Set_Actual_Subtype (Renamed_Object (Formal),
10452 Defining_Identifier (Decl));
10453 else
10454 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
10455 end if;
996ae0b0
RK
10456 end if;
10457
10458 Next_Formal (Formal);
10459 end loop;
10460 end Set_Actual_Subtypes;
10461
10462 ---------------------
10463 -- Set_Formal_Mode --
10464 ---------------------
10465
10466 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
10467 Spec : constant Node_Id := Parent (Formal_Id);
10468
10469 begin
10470 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
10471 -- since we ensure that corresponding actuals are always valid at the
10472 -- point of the call.
10473
10474 if Out_Present (Spec) then
fc999c5d
RD
10475 if Ekind_In (Scope (Formal_Id), E_Function, E_Generic_Function) then
10476
b4ca2d2c 10477 -- [IN] OUT parameters allowed for functions in Ada 2012
c56a9ba4
AC
10478
10479 if Ada_Version >= Ada_2012 then
e6425869
AC
10480
10481 -- Even in Ada 2012 operators can only have IN parameters
10482
10483 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
10484 Error_Msg_N ("operators can only have IN parameters", Spec);
10485 end if;
10486
c56a9ba4
AC
10487 if In_Present (Spec) then
10488 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10489 else
10490 Set_Ekind (Formal_Id, E_Out_Parameter);
10491 end if;
10492
fc999c5d
RD
10493 Set_Has_Out_Or_In_Out_Parameter (Scope (Formal_Id), True);
10494
b4ca2d2c
AC
10495 -- But not in earlier versions of Ada
10496
c56a9ba4
AC
10497 else
10498 Error_Msg_N ("functions can only have IN parameters", Spec);
10499 Set_Ekind (Formal_Id, E_In_Parameter);
10500 end if;
996ae0b0
RK
10501
10502 elsif In_Present (Spec) then
10503 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10504
10505 else
fbf5a39b
AC
10506 Set_Ekind (Formal_Id, E_Out_Parameter);
10507 Set_Never_Set_In_Source (Formal_Id, True);
10508 Set_Is_True_Constant (Formal_Id, False);
10509 Set_Current_Value (Formal_Id, Empty);
996ae0b0
RK
10510 end if;
10511
10512 else
10513 Set_Ekind (Formal_Id, E_In_Parameter);
10514 end if;
10515
fbf5a39b 10516 -- Set Is_Known_Non_Null for access parameters since the language
82c80734
RD
10517 -- guarantees that access parameters are always non-null. We also set
10518 -- Can_Never_Be_Null, since there is no way to change the value.
fbf5a39b
AC
10519
10520 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
2820d220 10521
885c4871 10522 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
2813bb6b 10523 -- null; In Ada 2005, only if then null_exclusion is explicit.
2820d220 10524
0791fbe9 10525 if Ada_Version < Ada_2005
2813bb6b 10526 or else Can_Never_Be_Null (Etype (Formal_Id))
2820d220
AC
10527 then
10528 Set_Is_Known_Non_Null (Formal_Id);
10529 Set_Can_Never_Be_Null (Formal_Id);
10530 end if;
2813bb6b 10531
41251c60
JM
10532 -- Ada 2005 (AI-231): Null-exclusion access subtype
10533
2813bb6b
ES
10534 elsif Is_Access_Type (Etype (Formal_Id))
10535 and then Can_Never_Be_Null (Etype (Formal_Id))
10536 then
2813bb6b 10537 Set_Is_Known_Non_Null (Formal_Id);
a1d72281
EB
10538
10539 -- We can also set Can_Never_Be_Null (thus preventing some junk
10540 -- access checks) for the case of an IN parameter, which cannot
10541 -- be changed, or for an IN OUT parameter, which can be changed but
10542 -- not to a null value. But for an OUT parameter, the initial value
10543 -- passed in can be null, so we can't set this flag in that case.
10544
10545 if Ekind (Formal_Id) /= E_Out_Parameter then
10546 Set_Can_Never_Be_Null (Formal_Id);
10547 end if;
fbf5a39b
AC
10548 end if;
10549
996ae0b0
RK
10550 Set_Mechanism (Formal_Id, Default_Mechanism);
10551 Set_Formal_Validity (Formal_Id);
10552 end Set_Formal_Mode;
10553
10554 -------------------------
10555 -- Set_Formal_Validity --
10556 -------------------------
10557
10558 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
10559 begin
82c80734
RD
10560 -- If no validity checking, then we cannot assume anything about the
10561 -- validity of parameters, since we do not know there is any checking
10562 -- of the validity on the call side.
996ae0b0
RK
10563
10564 if not Validity_Checks_On then
10565 return;
10566
fbf5a39b
AC
10567 -- If validity checking for parameters is enabled, this means we are
10568 -- not supposed to make any assumptions about argument values.
10569
10570 elsif Validity_Check_Parameters then
10571 return;
10572
10573 -- If we are checking in parameters, we will assume that the caller is
10574 -- also checking parameters, so we can assume the parameter is valid.
10575
996ae0b0
RK
10576 elsif Ekind (Formal_Id) = E_In_Parameter
10577 and then Validity_Check_In_Params
10578 then
10579 Set_Is_Known_Valid (Formal_Id, True);
10580
fbf5a39b
AC
10581 -- Similar treatment for IN OUT parameters
10582
996ae0b0
RK
10583 elsif Ekind (Formal_Id) = E_In_Out_Parameter
10584 and then Validity_Check_In_Out_Params
10585 then
10586 Set_Is_Known_Valid (Formal_Id, True);
10587 end if;
10588 end Set_Formal_Validity;
10589
10590 ------------------------
10591 -- Subtype_Conformant --
10592 ------------------------
10593
ce2b6ba5
JM
10594 function Subtype_Conformant
10595 (New_Id : Entity_Id;
10596 Old_Id : Entity_Id;
10597 Skip_Controlling_Formals : Boolean := False) return Boolean
10598 is
996ae0b0 10599 Result : Boolean;
996ae0b0 10600 begin
ce2b6ba5
JM
10601 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
10602 Skip_Controlling_Formals => Skip_Controlling_Formals);
996ae0b0
RK
10603 return Result;
10604 end Subtype_Conformant;
10605
10606 ---------------------
10607 -- Type_Conformant --
10608 ---------------------
10609
41251c60
JM
10610 function Type_Conformant
10611 (New_Id : Entity_Id;
10612 Old_Id : Entity_Id;
10613 Skip_Controlling_Formals : Boolean := False) return Boolean
10614 is
996ae0b0 10615 Result : Boolean;
996ae0b0 10616 begin
c8ef728f 10617 May_Hide_Profile := False;
41251c60
JM
10618 Check_Conformance
10619 (New_Id, Old_Id, Type_Conformant, False, Result,
10620 Skip_Controlling_Formals => Skip_Controlling_Formals);
996ae0b0
RK
10621 return Result;
10622 end Type_Conformant;
10623
10624 -------------------------------
10625 -- Valid_Operator_Definition --
10626 -------------------------------
10627
10628 procedure Valid_Operator_Definition (Designator : Entity_Id) is
10629 N : Integer := 0;
10630 F : Entity_Id;
10631 Id : constant Name_Id := Chars (Designator);
10632 N_OK : Boolean;
10633
10634 begin
10635 F := First_Formal (Designator);
996ae0b0
RK
10636 while Present (F) loop
10637 N := N + 1;
10638
10639 if Present (Default_Value (F)) then
ed2233dc 10640 Error_Msg_N
996ae0b0
RK
10641 ("default values not allowed for operator parameters",
10642 Parent (F));
220d1fd9
AC
10643
10644 -- For function instantiations that are operators, we must check
10645 -- separately that the corresponding generic only has in-parameters.
84c0a895
AC
10646 -- For subprogram declarations this is done in Set_Formal_Mode. Such
10647 -- an error could not arise in earlier versions of the language.
220d1fd9
AC
10648
10649 elsif Ekind (F) /= E_In_Parameter then
84c0a895 10650 Error_Msg_N ("operators can only have IN parameters", F);
996ae0b0
RK
10651 end if;
10652
10653 Next_Formal (F);
10654 end loop;
10655
10656 -- Verify that user-defined operators have proper number of arguments
10657 -- First case of operators which can only be unary
10658
b69cd36a 10659 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
996ae0b0
RK
10660 N_OK := (N = 1);
10661
10662 -- Case of operators which can be unary or binary
10663
b69cd36a 10664 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
996ae0b0
RK
10665 N_OK := (N in 1 .. 2);
10666
10667 -- All other operators can only be binary
10668
10669 else
10670 N_OK := (N = 2);
10671 end if;
10672
10673 if not N_OK then
10674 Error_Msg_N
10675 ("incorrect number of arguments for operator", Designator);
10676 end if;
10677
10678 if Id = Name_Op_Ne
10679 and then Base_Type (Etype (Designator)) = Standard_Boolean
10680 and then not Is_Intrinsic_Subprogram (Designator)
10681 then
10682 Error_Msg_N
84c0a895 10683 ("explicit definition of inequality not allowed", Designator);
996ae0b0
RK
10684 end if;
10685 end Valid_Operator_Definition;
10686
10687end Sem_Ch6;