Debian Codegraph @

gcc/ada/sem_ch13.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 3 --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
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- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Local_Restrict;
27 with Types; use Types;
28 with Sem_Disp; use Sem_Disp;
29 with Uintp; use Uintp;
30
31 package Sem_Ch13 is
32 function All_Membership_Choices_Static (Expr : Node_Id) return Boolean;
33 -- Given a membership test, returns True iff all choices are static.
34
35 procedure Analyze_At_Clause (N : Node_Id);
36 procedure Analyze_Attribute_Definition_Clause (N : Node_Id);
37 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id);
38 procedure Analyze_Free_Statement (N : Node_Id);
39 procedure Analyze_Freeze_Entity (N : Node_Id);
40 procedure Analyze_Freeze_Generic_Entity (N : Node_Id);
41 procedure Analyze_Record_Representation_Clause (N : Node_Id);
42 procedure Analyze_Code_Statement (N : Node_Id);
43
44 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id);
45 -- This procedure is called to analyze aspect specifications for node N. E
46 -- is the corresponding entity declared by the declaration node N. Callers
47 -- should check that Has_Aspects (N) is True before calling this routine.
48
49 procedure Analyze_Aspects_On_Subprogram_Body_Or_Stub (N : Node_Id);
50 -- Analyze the aspect specifications of [generic] subprogram body or stub
51 -- N. Callers should check that Has_Aspects (N) is True before calling the
52 -- routine. This routine diagnoses misplaced aspects that should appear on
53 -- the initial declaration of N and offers suggestions for replacements.
54
55 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id);
56 -- Called from Freeze where R is a record entity for which reverse bit
57 -- order is specified and there is at least one component clause. Note:
58 -- component positions are normally adjusted as per AI95-0133, unless
59 -- -gnatd.p is used to restore original Ada 95 mode.
60
61 procedure Check_Record_Representation_Clause (N : Node_Id);
62 -- This procedure completes the analysis of a record representation clause
63 -- N. It is called at freeze time after adjustment of component clause bit
64 -- positions for possible non-standard bit order. In the case of Ada 2005
65 -- (machine scalar) mode, this adjustment can make substantial changes, so
66 -- some checks, in particular for component overlaps cannot be done at the
67 -- time the record representation clause is first seen, but must be delayed
68 -- till freeze time, and in particular is called after calling the above
69 -- procedure for adjusting record bit positions for reverse bit order.
70
71 procedure Initialize;
72 -- Initialize internal tables for new compilation
73
74 procedure Kill_Rep_Clause (N : Node_Id);
75 -- This procedure is called for a rep clause N when we are in -gnatI mode
76 -- (Ignore_Rep_Clauses). It replaces the node N with a null statement. This
77 -- is only called if Ignore_Rep_Clauses is True.
78
79 procedure Set_Enum_Esize (T : Entity_Id);
80 -- This routine sets the Esize field for an enumeration type T, based
81 -- on the current representation information available for T. Note that
82 -- the setting of the RM_Size field is not affected.
83
84 Unknown_Minimum_Size : constant Nonzero_Int := -1;
85
86 function Minimum_Size
87 (T : Entity_Id;
88 Biased : Boolean := False) return Int;
89 -- Given an elementary type, determines the minimum number of bits required
90 -- to represent all values of the type. This function may not be called
91 -- with any other types. If the flag Biased is set True, then the minimum
92 -- size calculation that biased representation is used in the case of a
93 -- discrete type, e.g. the range 7..8 gives a minimum size of 4 with
94 -- Biased set to False, and 1 with Biased set to True. Note that the
95 -- biased parameter only has an effect if the type is not biased, it
96 -- causes Minimum_Size to indicate the minimum size of an object with
97 -- the given type, of the size the type would have if it were biased. If
98 -- the type is already biased, then Minimum_Size returns the biased size,
99 -- regardless of the setting of Biased. Also, fixed-point types are never
100 -- biased in the current implementation. If the size is not known at
101 -- compile time, this function returns Unknown_Minimum_Size.
102
103 procedure Check_Constant_Address_Clause (Expr : Node_Id; U_Ent : Entity_Id);
104 -- Expr is an expression for an address clause. This procedure checks
105 -- that the expression is constant, in the limited sense that it is safe
106 -- to evaluate it at the point the object U_Ent is declared, rather than
107 -- at the point of the address clause. The condition for this to be true
108 -- is that the expression has no variables, no constants declared after
109 -- U_Ent, and no calls to non-pure functions. If this condition is not
110 -- met, then an appropriate error message is posted. This check is applied
111 -- at the point an object with an address clause is frozen, as well as for
112 -- address clauses for tasks and entries.
113
114 procedure Check_Function_For_Indexing_Aspect
115 (ASN : Node_Id;
116 Typ : Entity_Id;
117 Subp : Entity_Id;
118 Valid : out Boolean);
119 -- Check Subp to see whether it's a valid function for Typ's indexing
120 -- aspect ASN (as specified by the rules given in RM 4.1.6(1-3)), flagging
121 -- an error if Subp is not an eligible indexing function (unless Subp is
122 -- declared outside the scope of E, in which case it's simply ignored
123 -- rather than considered an error; see AI22-0084). If valid for indexing,
124 -- then Subp is added to ASN's Aspect_Subprograms list, and Valid is set
125 -- to True (otherwise False).
126
127 procedure Check_Size
128 (N : Node_Id;
129 T : Entity_Id;
130 Siz : Uint;
131 Biased : out Boolean);
132 -- Called when size Siz is specified for subtype T. This subprogram checks
133 -- that the size is appropriate, posting errors on node N as required. This
134 -- check is effective for elementary types and bit-packed arrays. For
135 -- composite types, a check is only made if an explicit size has been given
136 -- for the type (and the specified size must match). The parameter Biased
137 -- is set False if the size specified did not require the use of biased
138 -- representation, and True if biased representation was required to meet
139 -- the size requirement. Note that Biased is only set if the type is not
140 -- currently biased, but biasing it is the only way to meet the
141 -- requirement. If the type is currently biased, then this biased size is
142 -- used in the initial check, and Biased is False. For a Component_Size
143 -- clause, T is the component type.
144
145 function Has_Compatible_Representation
146 (Target_Typ, Operand_Typ : Entity_Id) return Boolean;
147 -- Given an explicit or implicit conversion from Operand_Typ to Target_Typ,
148 -- determine whether the types have compatible or different representation,
149 -- thus requiring special processing for the conversion in the latter case.
150 -- A False result is possible only for array, enumeration and record types.
151
152 procedure Parse_Aspect_Aggregate
153 (N : Node_Id;
154 Empty_Subp : in out Node_Id;
155 Add_Named_Subp : in out Node_Id;
156 Add_Unnamed_Subp : in out Node_Id;
157 New_Indexed_Subp : in out Node_Id;
158 Assign_Indexed_Subp : in out Node_Id);
159 -- Utility to unpack the subprograms in an occurrence of aspect Aggregate;
160 -- used to verify the structure of the aspect, and resolve and expand an
161 -- aggregate for a container type that carries the aspect.
162
163 function Parse_Aspect_Local_Restrictions (Aspect_Spec : Node_Id)
164 return Local_Restrict.Local_Restriction_Set;
165 -- Utility to unpack the set of local restrictions specified in a
166 -- Local_Restrictions aspect specification.
167
168 function Parse_Aspect_Stable_Properties
169 (Aspect_Spec : Node_Id; Negated : out Boolean) return Subprogram_List;
170 -- Utility to unpack the subprograms in a Stable_Properties list;
171 -- in the case of the aspect of a type, Negated will always be False.
172
173 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean;
174 -- Called at start of processing a representation clause/pragma. Used to
175 -- check that the representation item is not being applied to an incomplete
176 -- type or to a generic formal type or a type derived from a generic formal
177 -- type. Returns False if no such error occurs. If this error does occur,
178 -- appropriate error messages are posted on node N, and True is returned.
179
180 generic
181 with procedure Replace_Type_Reference (N : Node_Id);
182 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id);
183 -- This is used to scan an expression for a predicate or invariant aspect
184 -- replacing occurrences of the name of the subtype to which the aspect
185 -- applies with appropriate references to the parameter of the predicate
186 -- function or invariant procedure. The procedure passed as a generic
187 -- parameter does the actual replacement of node N, which is either a
188 -- simple direct reference to T, or a selected component that represents
189 -- an appropriately qualified occurrence of T.
190 --
191 -- This also replaces each reference to a component, entry, or protected
192 -- procedure with a selected component whose prefix is the parameter.
193 -- For example, Component_Name becomes Parameter.Component_Name, where
194 -- Parameter is the parameter, which is of type T.
195
196 function Rep_Item_Too_Late
197 (T : Entity_Id;
198 N : Node_Id;
199 FOnly : Boolean := False) return Boolean;
200 -- Called at the start of processing a representation clause or a
201 -- representation pragma. Used to check that a representation item for
202 -- entity T does not appear too late (according to the rules in RM 13.1(9)
203 -- and RM 13.1(10)). N is the associated node, which in the pragma case
204 -- is the pragma or representation clause itself, used for placing error
205 -- messages if the item is too late.
206 --
207 -- FOnly is a flag that causes only the freezing rule (para 9) to be
208 -- applied, and the tests of para 10 are skipped. This is appropriate for
209 -- both subtype related attributes (Alignment and Size) and for stream
210 -- attributes, which, although certainly not subtype related attributes,
211 -- clearly should not be subject to the para 10 restrictions (see
212 -- AI95-00137). Similarly, we also skip the para 10 restrictions for
213 -- the Storage_Size case where they also clearly do not apply, and for
214 -- Stream_Convert which is in the same category as the stream attributes.
215 --
216 -- If the rep item is too late, an appropriate message is output and True
217 -- is returned, which is a signal that the caller should abandon processing
218 -- for the item. If the item is not too late, then False is returned, and
219 -- the caller can continue processing the item.
220 --
221 -- If no error is detected, this call also as a side effect links the
222 -- representation item onto the head of the representation item chain
223 -- (referenced by the First_Rep_Item field of the entity).
224 --
225 -- Note: Rep_Item_Too_Late must be called with the underlying type in the
226 -- case of a private or incomplete type. The protocol is to first check for
227 -- Rep_Item_Too_Early using the initial entity, then take the underlying
228 -- type, then call Rep_Item_Too_Late on the result.
229 --
230 -- Note: Calls to Rep_Item_Too_Late are ignored for the case of attribute
231 -- definition clauses which have From_Aspect_Specification set. This is
232 -- because such clauses are linked on to the Rep_Item chain in procedure
233 -- Sem_Ch13.Analyze_Aspect_Specifications. See that procedure for details.
234
235 procedure Validate_Unchecked_Conversion
236 (N : Node_Id;
237 Act_Unit : Entity_Id);
238 -- Validate a call to unchecked conversion. N is the node for the actual
239 -- instantiation, which is used only for error messages. Act_Unit is the
240 -- entity for the instantiation, from which the actual types etc. for this
241 -- instantiation can be determined. This procedure makes an entry in a
242 -- table and/or generates an N_Validate_Unchecked_Conversion node. The
243 -- actual checking is done in Validate_Unchecked_Conversions or in the
244 -- back end as required.
245
246 procedure Validate_Unchecked_Conversions;
247 -- This routine is called after calling the back end to validate unchecked
248 -- conversions for size and alignment appropriateness. The reason it is
249 -- called that late is to take advantage of any back-annotation of size
250 -- and alignment performed by the back end.
251
252 procedure Validate_Address_Clauses;
253 -- This is called after the back end has been called (and thus after the
254 -- alignments of objects have been back annotated). It goes through the
255 -- table of saved address clauses checking for suspicious alignments and
256 -- if necessary issuing warnings.
257
258 -----------------------------------
259 -- Handling of Aspect Visibility --
260 -----------------------------------
261
262 -- The visibility of aspects is tricky. First, the visibility is delayed
263 -- to the freeze point. This is not too complicated, what we do is simply
264 -- to leave the aspect "laying in wait" for the freeze point, and at that
265 -- point materialize and analyze the corresponding attribute definition
266 -- clause or pragma. There is some special processing for preconditions
267 -- and postonditions, where the pragmas themselves deal with the required
268 -- delay, but basically the approach is the same, delay analysis of the
269 -- expression to the freeze point.
270
271 -- Much harder is the requirement for diagnosing cases in which an early
272 -- freeze causes a change in visibility. Consider:
273
274 -- package AspectVis is
275 -- R_Size : constant Integer := 32;
276 --
277 -- package Inner is
278 -- type R is new Integer with
279 -- Size => R_Size;
280 -- F : R; -- freezes
281 -- R_Size : constant Integer := 64;
282 -- S : constant Integer := R'Size; -- 32 not 64
283 -- end Inner;
284 -- end AspectVis;
285
286 -- Here the 32 not 64 shows what would be expected if this program were
287 -- legal, since the evaluation of R_Size has to be done at the freeze
288 -- point and gets the outer definition not the inner one.
289
290 -- But the language rule requires this program to be diagnosed as illegal
291 -- because the visibility changes between the freeze point and the end of
292 -- the declarative region.
293
294 -- To meet this requirement, we first note that the Expression field of the
295 -- N_Aspect_Specification node holds the raw unanalyzed expression, which
296 -- will get used in processing the aspect. At the time of analyzing the
297 -- N_Aspect_Specification node, we create a complete copy of the expression
298 -- and store it in the entity field of the Identifier (an odd usage, but
299 -- the identifier is not used except to identify the aspect, so its Entity
300 -- field is otherwise unused, and we are short of room in the node).
301
302 -- This copy stays unanalyzed up to the freeze point, where we analyze the
303 -- resulting pragma or attribute definition clause, except that in the
304 -- case of invariants and predicates, we mark occurrences of the subtype
305 -- name as having the entity of the subprogram parameter, so that they
306 -- will not cause trouble in the following steps.
307
308 -- Then at the freeze point, we create another copy of this unanalyzed
309 -- expression. By this time we no longer need the Expression field for
310 -- other purposes, so we can store it there. Now we have two copies of
311 -- the original unanalyzed expression. One of them gets preanalyzed at
312 -- the freeze point to capture the visibility at the freeze point.
313
314 -- Now when we hit the freeze all at the end of the declarative part, if
315 -- we come across a frozen entity with delayed aspects, we still have one
316 -- copy of the unanalyzed expression available in the node, and we again
317 -- do a preanalysis using that copy and the visibility at the end of the
318 -- declarative part. Now we have two preanalyzed expression (preanalysis
319 -- is good enough, since we are only interested in referenced entities).
320 -- One captures the visibility at the freeze point, the other captures the
321 -- visibility at the end of the declarative part. We see if the entities
322 -- in these two expressions are the same, by seeing if the two expressions
323 -- are fully conformant, and if not, issue appropriate error messages.
324
325 -- Quite an awkward approach, but this is an awkard requirement
326
327 procedure Analyze_Aspects_At_Freeze_Point
328 (E : Entity_Id;
329 Nonoverridable_Only : Boolean := False);
330 -- Analyzes all the delayed aspects for entity E at the freeze point,
331 -- unless Nonoverridable_Only is True, in which case only nonoverridable
332 -- aspects are analyzed (those aspects have special requirements). Note
333 -- that this does not include dealing with inheriting delayed aspects from
334 -- the parent or base type in the case where a derived type or a subtype is
335 -- frozen. Callers should check that Has_Delayed_Aspects (E) is True before
336 -- calling this routine.
337
338 procedure Check_Aspects_At_End_Of_Declarations (E : Entity_Id);
339 -- Performs the processing described above at the freeze all point, and
340 -- issues appropriate error messages if the visibility has indeed changed.
341 -- Callers should check that Has_Delayed_Aspects (E) is True before calling
342 -- this routine.
343
344 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id);
345 -- Given an entity Typ that denotes a derived type or a subtype, this
346 -- routine performs the inheritance of aspects at the freeze point.
347
348 -- ??? Note that, for now, just a limited number of representation aspects
349 -- have been inherited here so far. Many of them are still inherited in
350 -- Sem_Ch3 and need to be dealt with. Here is a non-exhaustive list of
351 -- aspects that likely also need to be moved to this routine: Alignment,
352 -- Component_Alignment, Component_Size, Machine_Radix, Object_Size, Pack,
353 -- Predicates, Preelaborable_Initialization, Size and Small.
354
355 procedure Inherit_Delayed_Rep_Aspects (Typ : Entity_Id);
356 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
357 -- a derived type can inherit aspects from its parent which have been
358 -- specified at the time of the derivation using an aspect, as in:
359 --
360 -- type A is range 1 .. 10
361 -- with Size => Not_Defined_Yet;
362 -- ..
363 -- type B is new A;
364 -- ..
365 -- Not_Defined_Yet : constant := 64;
366 --
367 -- In this example, the Size of A is considered to be specified prior
368 -- to the derivation, and thus inherited, even though the value is not
369 -- known at the time of derivation. To deal with this, we use two entity
370 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
371 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
372 -- the derived type (B here). If this flag is set when the derived type
373 -- is frozen, then this procedure is called to ensure proper inheritance
374 -- of all delayed aspects from the parent type.
375
376 -- ??? Obviously we ought not to have two mechanisms to do the same thing
377
378 procedure Resolve_Aspect_Expressions (E : Entity_Id);
379 -- Name resolution of an aspect expression happens at the end of the
380 -- current declarative part or at the freeze point for the entity,
381 -- whichever comes first. For declarations in the visible part of a
382 -- package, name resolution takes place before analysis of the private
383 -- part even though the freeze point of the entity may appear later.
384
385 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id);
386 -- For SPARK 2014 formal containers. The expression has the form of an
387 -- aggregate, and each entry must denote a function with the proper syntax
388 -- for First, Next, and Has_Element. Optionally an Element primitive may
389 -- also be defined.
390
391 procedure Validate_Literal_Aspect (Typ : Entity_Id; ASN : Node_Id);
392 -- Check legality of Integer_Literal, Real_Literal, and String_Literal
393 -- aspect specifications.
394
395 procedure Install_Discriminants (E : Entity_Id);
396 -- Make visible the discriminants of type entity E
397
398 procedure Uninstall_Discriminants (E : Entity_Id);
399 -- Remove visibility to the discriminants of type entity E
400
401 end Sem_Ch13;