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