| 1 |
706 |
jeremybenn |
------------------------------------------------------------------------------
|
| 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-2010, 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 Table;
|
| 27 |
|
|
with Types; use Types;
|
| 28 |
|
|
with Uintp; use Uintp;
|
| 29 |
|
|
|
| 30 |
|
|
package Sem_Ch13 is
|
| 31 |
|
|
procedure Analyze_At_Clause (N : Node_Id);
|
| 32 |
|
|
procedure Analyze_Attribute_Definition_Clause (N : Node_Id);
|
| 33 |
|
|
procedure Analyze_Enumeration_Representation_Clause (N : Node_Id);
|
| 34 |
|
|
procedure Analyze_Free_Statement (N : Node_Id);
|
| 35 |
|
|
procedure Analyze_Freeze_Entity (N : Node_Id);
|
| 36 |
|
|
procedure Analyze_Record_Representation_Clause (N : Node_Id);
|
| 37 |
|
|
procedure Analyze_Code_Statement (N : Node_Id);
|
| 38 |
|
|
|
| 39 |
|
|
procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id);
|
| 40 |
|
|
-- This procedure is called to analyze aspect specifications for node N. E
|
| 41 |
|
|
-- is the corresponding entity declared by the declaration node N. Callers
|
| 42 |
|
|
-- should check that Has_Aspects (N) is True before calling this routine.
|
| 43 |
|
|
|
| 44 |
|
|
procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id);
|
| 45 |
|
|
-- Called from Freeze where R is a record entity for which reverse bit
|
| 46 |
|
|
-- order is specified and there is at least one component clause. Adjusts
|
| 47 |
|
|
-- component positions according to either Ada 95 or Ada 2005 (AI-133).
|
| 48 |
|
|
|
| 49 |
|
|
procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id);
|
| 50 |
|
|
-- Typ is a private type with invariants (indicated by Has_Invariants being
|
| 51 |
|
|
-- set for Typ, indicating the presence of pragma Invariant entries on the
|
| 52 |
|
|
-- rep chain, note that Invariant aspects have already been converted to
|
| 53 |
|
|
-- pragma Invariant), then this procedure builds the spec and body for the
|
| 54 |
|
|
-- corresponding Invariant procedure, inserting them at appropriate points
|
| 55 |
|
|
-- in the package specification N. Invariant_Procedure is set for Typ. Note
|
| 56 |
|
|
-- that this procedure is called at the end of processing the declarations
|
| 57 |
|
|
-- in the visible part (i.e. the right point for visibility analysis of
|
| 58 |
|
|
-- the invariant expression).
|
| 59 |
|
|
|
| 60 |
|
|
procedure Check_Record_Representation_Clause (N : Node_Id);
|
| 61 |
|
|
-- This procedure completes the analysis of a record representation clause
|
| 62 |
|
|
-- N. It is called at freeze time after adjustment of component clause bit
|
| 63 |
|
|
-- positions for possible non-standard bit order. In the case of Ada 2005
|
| 64 |
|
|
-- (machine scalar) mode, this adjustment can make substantial changes, so
|
| 65 |
|
|
-- some checks, in particular for component overlaps cannot be done at the
|
| 66 |
|
|
-- time the record representation clause is first seen, but must be delayed
|
| 67 |
|
|
-- till freeze time, and in particular is called after calling the above
|
| 68 |
|
|
-- procedure for adjusting record bit positions for reverse bit order.
|
| 69 |
|
|
|
| 70 |
|
|
procedure Initialize;
|
| 71 |
|
|
-- Initialize internal tables for new compilation
|
| 72 |
|
|
|
| 73 |
|
|
procedure Set_Enum_Esize (T : Entity_Id);
|
| 74 |
|
|
-- This routine sets the Esize field for an enumeration type T, based
|
| 75 |
|
|
-- on the current representation information available for T. Note that
|
| 76 |
|
|
-- the setting of the RM_Size field is not affected. This routine also
|
| 77 |
|
|
-- initializes the alignment field to zero.
|
| 78 |
|
|
|
| 79 |
|
|
function Minimum_Size
|
| 80 |
|
|
(T : Entity_Id;
|
| 81 |
|
|
Biased : Boolean := False) return Nat;
|
| 82 |
|
|
-- Given an elementary type, determines the minimum number of bits required
|
| 83 |
|
|
-- to represent all values of the type. This function may not be called
|
| 84 |
|
|
-- with any other types. If the flag Biased is set True, then the minimum
|
| 85 |
|
|
-- size calculation that biased representation is used in the case of a
|
| 86 |
|
|
-- discrete type, e.g. the range 7..8 gives a minimum size of 4 with
|
| 87 |
|
|
-- Biased set to False, and 1 with Biased set to True. Note that the
|
| 88 |
|
|
-- biased parameter only has an effect if the type is not biased, it
|
| 89 |
|
|
-- causes Minimum_Size to indicate the minimum size of an object with
|
| 90 |
|
|
-- the given type, of the size the type would have if it were biased. If
|
| 91 |
|
|
-- the type is already biased, then Minimum_Size returns the biased size,
|
| 92 |
|
|
-- regardless of the setting of Biased. Also, fixed-point types are never
|
| 93 |
|
|
-- biased in the current implementation. If the size is not known at
|
| 94 |
|
|
-- compile time, this function returns 0.
|
| 95 |
|
|
|
| 96 |
|
|
procedure Check_Constant_Address_Clause (Expr : Node_Id; U_Ent : Entity_Id);
|
| 97 |
|
|
-- Expr is an expression for an address clause. This procedure checks
|
| 98 |
|
|
-- that the expression is constant, in the limited sense that it is safe
|
| 99 |
|
|
-- to evaluate it at the point the object U_Ent is declared, rather than
|
| 100 |
|
|
-- at the point of the address clause. The condition for this to be true
|
| 101 |
|
|
-- is that the expression has no variables, no constants declared after
|
| 102 |
|
|
-- U_Ent, and no calls to non-pure functions. If this condition is not
|
| 103 |
|
|
-- met, then an appropriate error message is posted. This check is applied
|
| 104 |
|
|
-- at the point an object with an address clause is frozen, as well as for
|
| 105 |
|
|
-- address clauses for tasks and entries.
|
| 106 |
|
|
|
| 107 |
|
|
procedure Check_Size
|
| 108 |
|
|
(N : Node_Id;
|
| 109 |
|
|
T : Entity_Id;
|
| 110 |
|
|
Siz : Uint;
|
| 111 |
|
|
Biased : out Boolean);
|
| 112 |
|
|
-- Called when size Siz is specified for subtype T. This subprogram checks
|
| 113 |
|
|
-- that the size is appropriate, posting errors on node N as required.
|
| 114 |
|
|
-- This check is effective for elementary types and bit-packed arrays.
|
| 115 |
|
|
-- For other non-elementary types, a check is only made if an explicit
|
| 116 |
|
|
-- size has been given for the type (and the specified size must match).
|
| 117 |
|
|
-- The parameter Biased is set False if the size specified did not require
|
| 118 |
|
|
-- the use of biased representation, and True if biased representation
|
| 119 |
|
|
-- was required to meet the size requirement. Note that Biased is only
|
| 120 |
|
|
-- set if the type is not currently biased, but biasing it is the only
|
| 121 |
|
|
-- way to meet the requirement. If the type is currently biased, then
|
| 122 |
|
|
-- this biased size is used in the initial check, and Biased is False.
|
| 123 |
|
|
-- If the size is too small, and an error message is given, then both
|
| 124 |
|
|
-- Esize and RM_Size are reset to the allowed minimum value in T.
|
| 125 |
|
|
|
| 126 |
|
|
function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean;
|
| 127 |
|
|
-- Called at the start of processing a representation clause or a
|
| 128 |
|
|
-- representation pragma. Used to check that the representation item
|
| 129 |
|
|
-- is not being applied to an incomplete type or to a generic formal
|
| 130 |
|
|
-- type or a type derived from a generic formal type. Returns False if
|
| 131 |
|
|
-- no such error occurs. If this error does occur, appropriate error
|
| 132 |
|
|
-- messages are posted on node N, and True is returned.
|
| 133 |
|
|
|
| 134 |
|
|
function Rep_Item_Too_Late
|
| 135 |
|
|
(T : Entity_Id;
|
| 136 |
|
|
N : Node_Id;
|
| 137 |
|
|
FOnly : Boolean := False) return Boolean;
|
| 138 |
|
|
-- Called at the start of processing a representation clause or a
|
| 139 |
|
|
-- representation pragma. Used to check that a representation item
|
| 140 |
|
|
-- for entity T does not appear too late (according to the rules in
|
| 141 |
|
|
-- RM 13.1(9) and RM 13.1(10)). N is the associated node, which in
|
| 142 |
|
|
-- the pragma case is the pragma or representation clause itself, used
|
| 143 |
|
|
-- for placing error messages if the item is too late.
|
| 144 |
|
|
--
|
| 145 |
|
|
-- Fonly is a flag that causes only the freezing rule (para 9) to be
|
| 146 |
|
|
-- applied, and the tests of para 10 are skipped. This is appropriate
|
| 147 |
|
|
-- for both subtype related attributes (Alignment and Size) and for
|
| 148 |
|
|
-- stream attributes, which, although certainly not subtype related
|
| 149 |
|
|
-- attributes, clearly should not be subject to the para 10 restrictions
|
| 150 |
|
|
-- (see AI95-00137). Similarly, we also skip the para 10 restrictions for
|
| 151 |
|
|
-- the Storage_Size case where they also clearly do not apply, and for
|
| 152 |
|
|
-- Stream_Convert which is in the same category as the stream attributes.
|
| 153 |
|
|
--
|
| 154 |
|
|
-- If the rep item is too late, an appropriate message is output and
|
| 155 |
|
|
-- True is returned, which is a signal that the caller should abandon
|
| 156 |
|
|
-- processing for the item. If the item is not too late, then False
|
| 157 |
|
|
-- is returned, and the caller can continue processing the item.
|
| 158 |
|
|
--
|
| 159 |
|
|
-- If no error is detected, this call also as a side effect links the
|
| 160 |
|
|
-- representation item onto the head of the representation item chain
|
| 161 |
|
|
-- (referenced by the First_Rep_Item field of the entity).
|
| 162 |
|
|
--
|
| 163 |
|
|
-- Note: Rep_Item_Too_Late must be called with the underlying type in
|
| 164 |
|
|
-- the case of a private or incomplete type. The protocol is to first
|
| 165 |
|
|
-- check for Rep_Item_Too_Early using the initial entity, then take the
|
| 166 |
|
|
-- underlying type, then call Rep_Item_Too_Late on the result.
|
| 167 |
|
|
--
|
| 168 |
|
|
-- Note: Calls to Rep_Item_Too_Late are ignored for the case of attribute
|
| 169 |
|
|
-- definition clauses which have From_Aspect_Specification set. This is
|
| 170 |
|
|
-- because such clauses are linked on to the Rep_Item chain in procedure
|
| 171 |
|
|
-- Sem_Ch13.Analyze_Aspect_Specifications. See that procedure for details.
|
| 172 |
|
|
|
| 173 |
|
|
function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean;
|
| 174 |
|
|
-- Given two types, where the two types are related by possible derivation,
|
| 175 |
|
|
-- determines if the two types have the same representation, or different
|
| 176 |
|
|
-- representations, requiring the special processing for representation
|
| 177 |
|
|
-- change. A False result is possible only for array, enumeration or
|
| 178 |
|
|
-- record types.
|
| 179 |
|
|
|
| 180 |
|
|
procedure Validate_Unchecked_Conversion
|
| 181 |
|
|
(N : Node_Id;
|
| 182 |
|
|
Act_Unit : Entity_Id);
|
| 183 |
|
|
-- Validate a call to unchecked conversion. N is the node for the actual
|
| 184 |
|
|
-- instantiation, which is used only for error messages. Act_Unit is the
|
| 185 |
|
|
-- entity for the instantiation, from which the actual types etc. for this
|
| 186 |
|
|
-- instantiation can be determined. This procedure makes an entry in a
|
| 187 |
|
|
-- table and/or generates an N_Validate_Unchecked_Conversion node. The
|
| 188 |
|
|
-- actual checking is done in Validate_Unchecked_Conversions or in the
|
| 189 |
|
|
-- back end as required.
|
| 190 |
|
|
|
| 191 |
|
|
procedure Validate_Unchecked_Conversions;
|
| 192 |
|
|
-- This routine is called after calling the backend to validate unchecked
|
| 193 |
|
|
-- conversions for size and alignment appropriateness. The reason it is
|
| 194 |
|
|
-- called that late is to take advantage of any back-annotation of size
|
| 195 |
|
|
-- and alignment performed by the backend.
|
| 196 |
|
|
|
| 197 |
|
|
procedure Validate_Address_Clauses;
|
| 198 |
|
|
-- This is called after the back end has been called (and thus after the
|
| 199 |
|
|
-- alignments of objects have been back annotated). It goes through the
|
| 200 |
|
|
-- table of saved address clauses checking for suspicious alignments and
|
| 201 |
|
|
-- if necessary issuing warnings.
|
| 202 |
|
|
|
| 203 |
|
|
procedure Validate_Independence;
|
| 204 |
|
|
-- This is called after the back end has been called (and thus after the
|
| 205 |
|
|
-- layout of components has been back annotated). It goes through the
|
| 206 |
|
|
-- table of saved pragma Independent[_Component] entries, checking that
|
| 207 |
|
|
-- independence can be achieved, and if necessary issuing error messages.
|
| 208 |
|
|
|
| 209 |
|
|
-------------------------------------
|
| 210 |
|
|
-- Table for Validate_Independence --
|
| 211 |
|
|
-------------------------------------
|
| 212 |
|
|
|
| 213 |
|
|
-- If a legal pragma Independent or Independent_Components is given for
|
| 214 |
|
|
-- an entity, then an entry is made in this table, to be checked by a
|
| 215 |
|
|
-- call to Validate_Independence after back annotation of layout is done.
|
| 216 |
|
|
|
| 217 |
|
|
type Independence_Check_Record is record
|
| 218 |
|
|
N : Node_Id;
|
| 219 |
|
|
-- The pragma Independent or Independent_Components
|
| 220 |
|
|
|
| 221 |
|
|
E : Entity_Id;
|
| 222 |
|
|
-- The entity to which it applies
|
| 223 |
|
|
end record;
|
| 224 |
|
|
|
| 225 |
|
|
package Independence_Checks is new Table.Table (
|
| 226 |
|
|
Table_Component_Type => Independence_Check_Record,
|
| 227 |
|
|
Table_Index_Type => Int,
|
| 228 |
|
|
Table_Low_Bound => 1,
|
| 229 |
|
|
Table_Initial => 20,
|
| 230 |
|
|
Table_Increment => 200,
|
| 231 |
|
|
Table_Name => "Independence_Checks");
|
| 232 |
|
|
|
| 233 |
|
|
-----------------------------------
|
| 234 |
|
|
-- Handling of Aspect Visibility --
|
| 235 |
|
|
-----------------------------------
|
| 236 |
|
|
|
| 237 |
|
|
-- The visibility of aspects is tricky. First, the visibility is delayed
|
| 238 |
|
|
-- to the freeze point. This is not too complicated, what we do is simply
|
| 239 |
|
|
-- to leave the aspect "laying in wait" for the freeze point, and at that
|
| 240 |
|
|
-- point materialize and analye the corresponding attribute definition
|
| 241 |
|
|
-- clause or pragma. There is some special processing for preconditions
|
| 242 |
|
|
-- and postonditions, where the pragmas themselves deal with the required
|
| 243 |
|
|
-- delay, but basically the approach is the same, delay analysis of the
|
| 244 |
|
|
-- expression to the freeze point.
|
| 245 |
|
|
|
| 246 |
|
|
-- Much harder is the requirement for diagnosing cases in which an early
|
| 247 |
|
|
-- freeze causes a change in visibility. Consider:
|
| 248 |
|
|
|
| 249 |
|
|
-- package AspectVis is
|
| 250 |
|
|
-- R_Size : constant Integer := 32;
|
| 251 |
|
|
--
|
| 252 |
|
|
-- package Inner is
|
| 253 |
|
|
-- type R is new Integer with
|
| 254 |
|
|
-- Size => R_Size;
|
| 255 |
|
|
-- F : R; -- freezes
|
| 256 |
|
|
-- R_Size : constant Integer := 64;
|
| 257 |
|
|
-- S : constant Integer := R'Size; -- 32 not 64
|
| 258 |
|
|
-- end Inner;
|
| 259 |
|
|
-- end AspectVis;
|
| 260 |
|
|
|
| 261 |
|
|
-- Here the 32 not 64 shows what would be expected if this program were
|
| 262 |
|
|
-- legal, since the evaluation of R_Size has to be done at the freeze
|
| 263 |
|
|
-- point and gets the outer definition not the inner one.
|
| 264 |
|
|
|
| 265 |
|
|
-- But the language rule requires this program to be diagnosed as illegal
|
| 266 |
|
|
-- because the visibility changes between the freeze point and the end of
|
| 267 |
|
|
-- the declarative region.
|
| 268 |
|
|
|
| 269 |
|
|
-- To meet this requirement, we first note that the Expression field of the
|
| 270 |
|
|
-- N_Aspect_Specification node holds the raw unanalyzed expression, which
|
| 271 |
|
|
-- will get used in processing the aspect. At the time of analyzing the
|
| 272 |
|
|
-- N_Aspect_Specification node, we create a complete copy of the expression
|
| 273 |
|
|
-- and store it in the entity field of the Identifier (an odd usage, but
|
| 274 |
|
|
-- the identifier is not used except to identify the aspect, so its Entity
|
| 275 |
|
|
-- field is otherwise unused, and we are short of room in the node).
|
| 276 |
|
|
|
| 277 |
|
|
-- This copy stays unanalyzed up to the freeze point, where we analyze the
|
| 278 |
|
|
-- resulting pragma or attribute definition clause, except that in the
|
| 279 |
|
|
-- case of invariants and predicates, we mark occurrences of the subtype
|
| 280 |
|
|
-- name as having the entity of the subprogram parameter, so that they
|
| 281 |
|
|
-- will not cause trouble in the following steps.
|
| 282 |
|
|
|
| 283 |
|
|
-- Then at the freeze point, we create another copy of this unanalyzed
|
| 284 |
|
|
-- expression. By this time we no longer need the Expression field for
|
| 285 |
|
|
-- other purposes, so we can store it there. Now we have two copies of
|
| 286 |
|
|
-- the original unanalyzed expression. One of them gets preanalyzed at
|
| 287 |
|
|
-- the freeze point to capture the visibility at the freeze point.
|
| 288 |
|
|
|
| 289 |
|
|
-- Now when we hit the freeze all at the end of the declarative part, if
|
| 290 |
|
|
-- we come across a frozen entity with delayed aspects, we still have one
|
| 291 |
|
|
-- copy of the unanalyzed expression available in the node, and we again
|
| 292 |
|
|
-- do a preanalysis using that copy and the visibility at the end of the
|
| 293 |
|
|
-- declarative part. Now we have two preanalyzed expression (preanalysis
|
| 294 |
|
|
-- is good enough, since we are only interested in referenced entities).
|
| 295 |
|
|
-- One captures the visibility at the freeze point, the other captures the
|
| 296 |
|
|
-- visibility at the end of the declarative part. We see if the entities
|
| 297 |
|
|
-- in these two expressions are the same, by seeing if the two expressions
|
| 298 |
|
|
-- are fully conformant, and if not, issue appropriate error messages.
|
| 299 |
|
|
|
| 300 |
|
|
-- Quite an awkward procedure, but this is an awkard requirement!
|
| 301 |
|
|
|
| 302 |
|
|
procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id);
|
| 303 |
|
|
-- Performs the processing described above at the freeze point, ASN is the
|
| 304 |
|
|
-- N_Aspect_Specification node for the aspect.
|
| 305 |
|
|
|
| 306 |
|
|
procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id);
|
| 307 |
|
|
-- Performs the processing described above at the freeze all point, and
|
| 308 |
|
|
-- issues appropriate error messages if the visibility has indeed changed.
|
| 309 |
|
|
-- Again, ASN is the N_Aspect_Specification node for the aspect.
|
| 310 |
|
|
end Sem_Ch13;
|