1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 -- This package contains various subprograms involved in compile time
30 -- evaluation of expressions and checks for staticness of expressions
31 -- and types. It also contains the circuitry for checking for violations
32 -- of pure and preelaborated conditions (this naturally goes here, since
33 -- these rules involve consideration of staticness).
35 -- Note: the static evaluation for attributes is found in Sem_Attr even
36 -- though logically it belongs here. We have done this so that it is easier
37 -- to add new attributes to GNAT.
39 with Types
; use Types
;
40 with Uintp
; use Uintp
;
41 with Urealp
; use Urealp
;
45 ------------------------------------
46 -- Handling of Static Expressions --
47 ------------------------------------
49 -- This package contains a set of routine that process individual
50 -- subexpression nodes with the objective of folding (precomputing)
51 -- the value of static expressions that are known at compile time and
52 -- properly computing the setting of two flags that appear in every
53 -- subexpression node:
55 -- Is_Static_Expression
57 -- This flag is set on any expression that is static according
58 -- to the rules in (RM 4.9(3-32)).
60 -- Raises_Constraint_Error
62 -- This flag indicatest that it is known at compile time that the
63 -- evaluation of an expression raises constraint error. If the
64 -- expression is static, and this flag is off, then it is also known
65 -- at compile time that the expression does not raise constraint error
66 -- (i.e. the flag is accurate for static expressions, and conservative
67 -- for non-static expressions.
69 -- If a static expression does not raise constraint error, then the
70 -- Raises_Constraint_Error flag is off, and the expression must be
71 -- computed at compile time, which means that it has the form of either
72 -- a literal, or a constant that is itself (recursively) either a literal
75 -- The above rules must be followed exactly in order for legality
76 -- checks to be accurate. For subexpressions that are not static
77 -- according to the RM definition, they are sometimes folded anyway,
78 -- but of course in this case Is_Static_Expression is not set.
80 -------------------------------
81 -- Compile-Time Known Values --
82 -------------------------------
84 -- For most legality checking purposes the flag Is_Static_Expression
85 -- defined in Sinfo should be used. This package also provides
86 -- a routine called Is_OK_Static_Expression which in addition of
87 -- checking that an expression is static in the RM 4.9 sense, it
88 -- checks that the expression does not raise constraint error. In
89 -- fact for certain legality checks not only do we need to ascertain
90 -- that the expression is static, but we must also ensure that it
91 -- does not raise constraint error.
93 -- Neither of Is_Static_Expression and Is_OK_Static_Expression should
94 -- be used for compile time evaluation purposes. In fact certain
95 -- expression whose value is known at compile time are not static
96 -- in the RM 4.9 sense. A typical example is:
98 -- C : constant Integer := Record_Type'Size;
100 -- The expression 'C' is not static in the technical RM sense, but for
101 -- many simple record types, the size is in fact known at compile time.
102 -- When we are trying to perform compile time constant folding (for
103 -- instance for expressions such as 'C + 1', Is_Static_Expression or
104 -- Is_OK_Static_Expression are not the right functions to test to see
105 -- if folding is possible. Instead, we use Compile_Time_Know_Value.
106 -- All static expressions that do not raise constraint error (i.e.
107 -- those for which Is_OK_Static_Expression is true) are known at
108 -- compile time, but as shown by the above example, there are cases
109 -- of non-static expressions which are known at compile time.
115 procedure Check_Non_Static_Context
(N
: Node_Id
);
116 -- Deals with the special check required for a static expression that
117 -- appears in a non-static context, i.e. is not part of a larger static
118 -- expression (see RM 4.9(35)), i.e. the value of the expression must be
119 -- within the base range of the base type of its expected type. A check
120 -- is also made for expressions that are inside the base range, but
121 -- outside the range of the expected subtype (this is a warning message
122 -- rather than an illegality).
124 -- Note: most cases of non-static context checks are handled within
125 -- Sem_Eval itself, including all cases of expressions at the outer
126 -- level (i.e. those that are not a subexpression). Currently the only
127 -- outside customer for this procedure is Sem_Attr (because Eval_Attribute
128 -- is there). There is also one special case arising from ranges (see body
129 -- of Resolve_Range).
131 procedure Check_String_Literal_Length
(N
: Node_Id
; Ttype
: Entity_Id
);
132 -- N is either a string literal, or a constraint error node. In the latter
133 -- case, the situation is already dealt with, and the call has no effect.
134 -- In the former case, if the target type, Ttyp is constrained, then a
135 -- check is made to see if the string literal is of appropriate length.
137 type Compare_Result
is (LT
, LE
, EQ
, GT
, GE
, NE
, Unknown
);
138 subtype Compare_GE
is Compare_Result
range EQ
.. GE
;
139 subtype Compare_LE
is Compare_Result
range LT
.. EQ
;
140 function Compile_Time_Compare
(L
, R
: Node_Id
) return Compare_Result
;
141 -- Given two expression nodes, finds out whether it can be determined
142 -- at compile time how the runtime values will compare. An Unknown
143 -- result means that the result of a comparison cannot be determined at
144 -- compile time, otherwise the returned result indicates the known result
145 -- of the comparison, given as tightly as possible (i.e. EQ or LT is a
146 -- preferred returned value to LE).
148 function Is_OK_Static_Expression
(N
: Node_Id
) return Boolean;
149 -- An OK static expression is one that is static in the RM definition
150 -- sense and which does not raise constraint error. For most legality
151 -- checking purposes you should use Is_Static_Expression. For those
152 -- legality checks where the expression N should not raise constaint
153 -- error use this routine. This routine is *not* to be used in contexts
154 -- where the test is for compile time evaluation purposes. Use routine
155 -- Compile_Time_Known_Value instead (see section on "Compile-Time Known
158 function Is_Static_Range
(N
: Node_Id
) return Boolean;
159 -- Determine if range is static, as defined in RM 4.9(26). The only
160 -- allowed argument is an N_Range node (but note that the semantic
161 -- analysis of equivalent range attribute references already turned
162 -- them into the equivalent range).
164 function Is_OK_Static_Range
(N
: Node_Id
) return Boolean;
165 -- Like Is_Static_Range, but also makes sure that the bounds of the
166 -- range are compile-time evaluable (i.e. do not raise constraint error).
167 -- A result of true means that the bounds are compile time evaluable.
168 -- A result of false means they are not (either because the range is
169 -- not static, or because one or the other bound raises CE).
171 function Is_Static_Subtype
(Typ
: Entity_Id
) return Boolean;
172 -- Determines whether a subtype fits the definition of an Ada static
173 -- subtype as given in (RM 4.9(26)).
175 function Is_OK_Static_Subtype
(Typ
: Entity_Id
) return Boolean;
176 -- Like Is_Static_Subtype but also makes sure that the bounds of the
177 -- subtype are compile-time evaluable (i.e. do not raise constraint
178 -- error). A result of true means that the bounds are compile time
179 -- evaluable. A result of false means they are not (either because the
180 -- range is not static, or because one or the other bound raises CE).
182 function Subtypes_Statically_Compatible
186 -- Returns true if the subtypes are unconstrained or the constraint on
187 -- on T1 is statically compatible with T2 (as defined by 4.9.1(4)).
188 -- Otherwise returns false.
190 function Subtypes_Statically_Match
(T1
, T2
: Entity_Id
) return Boolean;
191 -- Determine whether two types T1, T2, which have the same base type,
192 -- are statically matching subtypes (RM 4.9.1(1-2)).
194 function Compile_Time_Known_Value
(Op
: Node_Id
) return Boolean;
195 -- Returns true if Op is an expression not raising constraint error
196 -- whose value is known at compile time. This is true if Op is a static
197 -- expression, but can also be true for expressions which are
198 -- technically non-static but which are in fact known at compile time,
199 -- such as the static lower bound of a non-static range or the value
200 -- of a constant object whose initial value is static. Note that this
201 -- routine is defended against unanalyzed expressions. Such expressions
202 -- will not cause a blowup, they may cause pessimistic (i.e. False)
203 -- results to be returned.
205 function Compile_Time_Known_Value_Or_Aggr
(Op
: Node_Id
) return Boolean;
206 -- Similar to Compile_Time_Known_Value, but also returns True if the
207 -- value is a compile time known aggregate, i.e. an aggregate all of
208 -- whose constituent expressions are either compile time known values
209 -- or compile time known aggregates.
211 function Expr_Value
(N
: Node_Id
) return Uint
;
212 -- Returns the folded value of the expression N. This function is called
213 -- in instances where it has already been determined that the expression
214 -- is static or its value is known at compile time (ie the call to
215 -- Compile_Time_Known_Value (N) returns True). This version is used for
216 -- integer values, and enumeration or character literals. In the latter
217 -- two cases, the value returned is the Pos value in the relevant
218 -- enumeration type. It can also be used for fixed-point values, in
219 -- which case it returns the corresponding integer value. It cannot be
220 -- used for floating-point values.
222 function Expr_Value_E
(N
: Node_Id
) return Entity_Id
;
223 -- Returns the folded value of the expression. This function is called
224 -- in instances where it has already been determined that the expression
225 -- is static or its value known at compile time. This version is used
226 -- for enumeration types and returns the corresponding enumeration
229 function Expr_Value_R
(N
: Node_Id
) return Ureal
;
230 -- Returns the folded value of the expression. This function is called
231 -- in instances where it has already been determined that the expression
232 -- is static or its value known at compile time. This version is used
233 -- for real values (including both the floating-point and fixed-point
234 -- cases). In the case of a fixed-point type, the real value is returned
235 -- (cf above version returning Uint).
237 function Expr_Value_S
(N
: Node_Id
) return Node_Id
;
238 -- Returns the folded value of the expression. This function is called
239 -- in instances where it has already been determined that the expression
240 -- is static or its value is known at compile time. This version is used
241 -- for string types and returns the corresponding N_String_Literal node.
243 function Expr_Rep_Value
(N
: Node_Id
) return Uint
;
244 -- This is identical to Expr_Value, except in the case of enumeration
245 -- literals of types for which an enumeration representation clause has
246 -- been given, in which case it returns the representation value rather
247 -- than the pos value. This is the value that is needed for generating
248 -- code sequences, while the Expr_Value value is appropriate for compile
249 -- time constraint errors or getting the logical value. Note that this
250 -- function does NOT concern itself with biased values, if the caller
251 -- needs a properly biased value, the subtraction of the bias must be
252 -- handled explicitly.
254 procedure Eval_Actual
(N
: Node_Id
);
255 procedure Eval_Allocator
(N
: Node_Id
);
256 procedure Eval_Arithmetic_Op
(N
: Node_Id
);
257 procedure Eval_Character_Literal
(N
: Node_Id
);
258 procedure Eval_Concatenation
(N
: Node_Id
);
259 procedure Eval_Conditional_Expression
(N
: Node_Id
);
260 procedure Eval_Entity_Name
(N
: Node_Id
);
261 procedure Eval_Indexed_Component
(N
: Node_Id
);
262 procedure Eval_Integer_Literal
(N
: Node_Id
);
263 procedure Eval_Logical_Op
(N
: Node_Id
);
264 procedure Eval_Membership_Op
(N
: Node_Id
);
265 procedure Eval_Named_Integer
(N
: Node_Id
);
266 procedure Eval_Named_Real
(N
: Node_Id
);
267 procedure Eval_Op_Expon
(N
: Node_Id
);
268 procedure Eval_Op_Not
(N
: Node_Id
);
269 procedure Eval_Real_Literal
(N
: Node_Id
);
270 procedure Eval_Relational_Op
(N
: Node_Id
);
271 procedure Eval_Shift
(N
: Node_Id
);
272 procedure Eval_Short_Circuit
(N
: Node_Id
);
273 procedure Eval_Slice
(N
: Node_Id
);
274 procedure Eval_String_Literal
(N
: Node_Id
);
275 procedure Eval_Qualified_Expression
(N
: Node_Id
);
276 procedure Eval_Type_Conversion
(N
: Node_Id
);
277 procedure Eval_Unary_Op
(N
: Node_Id
);
278 procedure Eval_Unchecked_Conversion
(N
: Node_Id
);
280 procedure Fold_Str
(N
: Node_Id
; Val
: String_Id
);
281 -- Rewrite N with a new N_String_Literal node as the result of the
282 -- compile time evaluation of the node N. Val is the resulting string
283 -- value from the folding operation. The Is_Static_Expression flag is
284 -- set in the result node. The result is fully analyzed and resolved.
286 procedure Fold_Uint
(N
: Node_Id
; Val
: Uint
);
287 -- Rewrite N with a (N_Integer_Literal, N_Identifier, N_Character_Literal)
288 -- node as the result of the compile time evaluation of the node N. Val
289 -- is the result in the integer case and is the position of the literal
290 -- in the literals list for the enumeration case. Is_Static_Expression
291 -- is set True in the result node. The result is fully analyzed/resolved.
293 procedure Fold_Ureal
(N
: Node_Id
; Val
: Ureal
);
294 -- Rewrite N with a new N_Real_Literal node as the result of the compile
295 -- time evaluation of the node N. Val is the resulting real value from
296 -- the folding operation. The Is_Static_Expression flag is set in the
297 -- result node. The result is fully analyzed and result.
302 Fixed_Int
: Boolean := False;
303 Int_Real
: Boolean := False)
305 -- Returns True if it can be guaranteed at compile time that expression
306 -- N is known to be in range of the subtype Typ. If the values of N or
307 -- of either bouds of Type are unknown at compile time, False will
308 -- always be returned. A result of False does not mean that the
309 -- expression is out of range, merely that it cannot be determined at
310 -- compile time that it is in range. If Typ is a floating point type or
311 -- Int_Real is set, any integer value is treated as though it was a real
312 -- value (i.e. the underlying real value is used). In this case we use
313 -- the corresponding real value, both for the bounds of Typ, and for the
314 -- value of the expression N. If Typ is a fixed type or a discrete type
315 -- and Int_Real is False but flag Fixed_Int is True then any fixed-point
316 -- value is treated as though it was a discrete value (i.e. the
317 -- underlying integer value is used). In this case we use the
318 -- corresponding integer value, both for the bounds of Typ, and for the
319 -- value of the expression N. If Typ is a discret type and Fixed_Int as
320 -- well as Int_Real are false, intere values are used throughout.
322 function Is_Out_Of_Range
325 Fixed_Int
: Boolean := False;
326 Int_Real
: Boolean := False)
328 -- Returns True if it can be guaranteed at compile time that expression
329 -- N is known to be out of range of the subtype Typ. True is returned
330 -- if Typ is a scalar type, at least one of whose bounds is known at
331 -- compile time, and N is a compile time known expression which can be
332 -- determined to be outside a compile_time known bound of Typ. A result
333 -- of False does not mean that the expression is in range, merely that
334 -- it cannot be determined at compile time that it is out of range. Flags
335 -- Int_Real and Fixed_Int are used like in routine Is_In_Range above.
337 function In_Subrange_Of
340 Fixed_Int
: Boolean := False)
342 -- Returns True if it can be guaranteed at compile time that the range
343 -- of values for scalar type T1 are always in the range of scalar type
344 -- T2. A result of False does not mean that T1 is not in T2's subrange,
345 -- only that it cannot be determined at compile time. Flag Fixed_Int is
346 -- used is like in routine Is_In_Range_Above.
348 function Is_Null_Range
(Lo
: Node_Id
; Hi
: Node_Id
) return Boolean;
349 -- Returns True if it can guarantee that Lo .. Hi is a null range.
350 -- If it cannot (because the value of Lo or Hi is not known at compile
351 -- time) then it returns False.
353 function Not_Null_Range
(Lo
: Node_Id
; Hi
: Node_Id
) return Boolean;
354 -- Returns True if it can guarantee that Lo .. Hi is not a null range.
355 -- If it cannot (because the value of Lo or Hi is not known at compile
356 -- time) then it returns False.
359 -- The Eval routines are all marked inline, since they are called once
361 pragma Inline
(Eval_Actual
);
362 pragma Inline
(Eval_Allocator
);
363 pragma Inline
(Eval_Character_Literal
);
364 pragma Inline
(Eval_Conditional_Expression
);
365 pragma Inline
(Eval_Indexed_Component
);
366 pragma Inline
(Eval_Integer_Literal
);
367 pragma Inline
(Eval_Named_Integer
);
368 pragma Inline
(Eval_Named_Real
);
369 pragma Inline
(Eval_Real_Literal
);
370 pragma Inline
(Eval_Shift
);
371 pragma Inline
(Eval_Slice
);
372 pragma Inline
(Eval_String_Literal
);
373 pragma Inline
(Eval_Unchecked_Conversion
);
375 pragma Inline
(Is_OK_Static_Expression
);