* loop-invariant.c (invariant_for_use, check_dependencies): Fail for
[official-gcc.git] / gcc / ada / sem_eval.ads
blob31f7d634102c2ab804c10b64695134d252d690e0
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ E V A L --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1992-2005, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 -- This package contains various subprograms involved in compile time
28 -- evaluation of expressions and checks for staticness of expressions and
29 -- types. It also contains the circuitry for checking for violations of pure
30 -- and preelaborated conditions (this naturally goes here, since these rules
31 -- involve consideration of staticness).
33 -- Note: the static evaluation for attributes is found in Sem_Attr even though
34 -- logically it belongs here. We have done this so that it is easier to add
35 -- new attributes to GNAT.
37 with Types; use Types;
38 with Uintp; use Uintp;
39 with Urealp; use Urealp;
41 package Sem_Eval is
43 ------------------------------------
44 -- Handling of Static Expressions --
45 ------------------------------------
47 -- This package contains a set of routine that process individual
48 -- subexpression nodes with the objective of folding (precomputing) the
49 -- value of static expressions that are known at compile time and properly
50 -- computing the setting of two flags that appear in every subexpression
51 -- node:
53 -- Is_Static_Expression
55 -- This flag is set on any expression that is static according to the
56 -- rules in (RM 4.9(3-32)).
58 -- Raises_Constraint_Error
60 -- This flag indicatest that it is known at compile time that the
61 -- evaluation of an expression raises constraint error. If the
62 -- expression is static, and this flag is off, then it is also known at
63 -- compile time that the expression does not raise constraint error
64 -- (i.e. the flag is accurate for static expressions, and conservative
65 -- for non-static expressions.
67 -- If a static expression does not raise constraint error, then the
68 -- Raises_Constraint_Error flag is off, and the expression must be computed
69 -- at compile time, which means that it has the form of either a literal,
70 -- or a constant that is itself (recursively) either a literal or a
71 -- constant.
73 -- The above rules must be followed exactly in order for legality checks to
74 -- be accurate. For subexpressions that are not static according to the RM
75 -- definition, they are sometimes folded anyway, but of course in this case
76 -- Is_Static_Expression is not set.
78 -------------------------------
79 -- Compile-Time Known Values --
80 -------------------------------
82 -- For most legality checking purposes the flag Is_Static_Expression
83 -- defined in Sinfo should be used. This package also provides a routine
84 -- called Is_OK_Static_Expression which in addition of checking that an
85 -- expression is static in the RM 4.9 sense, it checks that the expression
86 -- does not raise constraint error. In fact for certain legality checks not
87 -- only do we need to ascertain that the expression is static, but we must
88 -- also ensure that it does not raise constraint error.
90 -- Neither of Is_Static_Expression and Is_OK_Static_Expression should be
91 -- used for compile time evaluation purposes. In fact certain expression
92 -- whose value is known at compile time are not static in the RM 4.9 sense.
93 -- A typical example is:
95 -- C : constant Integer := Record_Type'Size;
97 -- The expression 'C' is not static in the technical RM sense, but for many
98 -- simple record types, the size is in fact known at compile time. When we
99 -- are trying to perform compile time constant folding (for instance for
100 -- expressions like C + 1, Is_Static_Expression or Is_OK_Static_Expression
101 -- are not the right functions to test if folding is possible. Instead, we
102 -- use Compile_Time_Known_Value. All static expressions that do not raise
103 -- constraint error (i.e. those for which Is_OK_Static_Expression is true)
104 -- are known at compile time, but as shown by the above example, there are
105 -- cases of non-static expressions which are known at compile time.
107 -----------------
108 -- Subprograms --
109 -----------------
111 procedure Check_Non_Static_Context (N : Node_Id);
112 -- Deals with the special check required for a static expression that
113 -- appears in a non-static context, i.e. is not part of a larger static
114 -- expression (see RM 4.9(35)), i.e. the value of the expression must be
115 -- within the base range of the base type of its expected type. A check is
116 -- also made for expressions that are inside the base range, but outside
117 -- the range of the expected subtype (this is a warning message rather than
118 -- an illegality).
120 -- Note: most cases of non-static context checks are handled within
121 -- Sem_Eval itself, including all cases of expressions at the outer level
122 -- (i.e. those that are not a subexpression). Currently the only outside
123 -- customer for this procedure is Sem_Attr (because Eval_Attribute is
124 -- there). There is also one special case arising from ranges (see body of
125 -- Resolve_Range).
127 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id);
128 -- N is either a string literal, or a constraint error node. In the latter
129 -- case, the situation is already dealt with, and the call has no effect.
130 -- In the former case, if the target type, Ttyp is constrained, then a
131 -- check is made to see if the string literal is of appropriate length.
133 type Compare_Result is (LT, LE, EQ, GT, GE, NE, Unknown);
134 subtype Compare_GE is Compare_Result range EQ .. GE;
135 subtype Compare_LE is Compare_Result range LT .. EQ;
136 function Compile_Time_Compare
137 (L, R : Node_Id;
138 Rec : Boolean := False) return Compare_Result;
139 -- Given two expression nodes, finds out whether it can be determined at
140 -- compile time how the runtime values will compare. An Unknown result
141 -- means that the result of a comparison cannot be determined at compile
142 -- time, otherwise the returned result indicates the known result of the
143 -- comparison, given as tightly as possible (i.e. EQ or LT is preferred
144 -- returned value to LE). Rec is a parameter that is set True for a
145 -- recursive call from within Compile_Time_Compare to avoid some infinite
146 -- recursion cases. It should never be set by a client.
148 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id);
149 -- This procedure is called after it has been determined that Expr is not
150 -- static when it is required to be. Msg is the text of a message that
151 -- explains the error. This procedure checks if an error is already posted
152 -- on Expr, if so, it does nothing unless All_Errors_Mode is set in which
153 -- case this flag is ignored. Otherwise the given message is posted using
154 -- Error_Msg_F, and then Why_Not_Static is called on Expr to generate
155 -- additional messages. The string given as Msg should end with ! to make
156 -- it an unconditional message, to ensure that if it is posted, the entire
157 -- set of messages is all posted.
159 function Is_OK_Static_Expression (N : Node_Id) return Boolean;
160 -- An OK static expression is one that is static in the RM definition sense
161 -- and which does not raise constraint error. For most legality checking
162 -- purposes you should use Is_Static_Expression. For those legality checks
163 -- where the expression N should not raise constaint error use this
164 -- routine. This routine is *not* to be used in contexts where the test is
165 -- for compile time evaluation purposes. Use Compile_Time_Known_Value
166 -- instead (see section on "Compile-Time Known Values" above).
168 function Is_Static_Range (N : Node_Id) return Boolean;
169 -- Determine if range is static, as defined in RM 4.9(26). The only allowed
170 -- argument is an N_Range node (but note that the semantic analysis of
171 -- equivalent range attribute references already turned them into the
172 -- equivalent range).
174 function Is_OK_Static_Range (N : Node_Id) return Boolean;
175 -- Like Is_Static_Range, but also makes sure that the bounds of the range
176 -- are compile-time evaluable (i.e. do not raise constraint error). A
177 -- result of true means that the bounds are compile time evaluable. A
178 -- result of false means they are not (either because the range is not
179 -- static, or because one or the other bound raises CE).
181 function Is_Static_Subtype (Typ : Entity_Id) return Boolean;
182 -- Determines whether a subtype fits the definition of an Ada static
183 -- subtype as given in (RM 4.9(26)).
185 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean;
186 -- Like Is_Static_Subtype but also makes sure that the bounds of the
187 -- subtype are compile-time evaluable (i.e. do not raise constraint error).
188 -- A result of true means that the bounds are compile time evaluable. A
189 -- result of false means they are not (either because the range is not
190 -- static, or because one or the other bound raises CE).
192 function Subtypes_Statically_Compatible
193 (T1 : Entity_Id;
194 T2 : Entity_Id) return Boolean;
195 -- Returns true if the subtypes are unconstrained or the constraint on
196 -- on T1 is statically compatible with T2 (as defined by 4.9.1(4)).
197 -- Otherwise returns false.
199 function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean;
200 -- Determine whether two types T1, T2, which have the same base type,
201 -- are statically matching subtypes (RM 4.9.1(1-2)).
203 function Compile_Time_Known_Value (Op : Node_Id) return Boolean;
204 -- Returns true if Op is an expression not raising constraint error whose
205 -- value is known at compile time. This is true if Op is a static
206 -- expression, but can also be true for expressions which are technically
207 -- non-static but which are in fact known at compile time, such as the
208 -- static lower bound of a non-static range or the value of a constant
209 -- object whose initial value is static. Note that this routine is defended
210 -- against unanalyzed expressions. Such expressions will not cause a
211 -- blowup, they may cause pessimistic (i.e. False) results to be returned.
213 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean;
214 -- Similar to Compile_Time_Known_Value, but also returns True if the value
215 -- is a compile time known aggregate, i.e. an aggregate all of whose
216 -- constituent expressions are either compile time known values or compile
217 -- time known aggregates.
219 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean;
220 -- If T is an array whose index bounds are all known at compile time, then
221 -- True is returned, if T is not an array, or one or more of its index
222 -- bounds is not known at compile time, then False is returned.
224 function Expr_Value (N : Node_Id) return Uint;
225 -- Returns the folded value of the expression N. This function is called in
226 -- instances where it has already been determined that the expression is
227 -- static or its value is compile time known (Compile_Time_Known_Value (N)
228 -- returns True). This version is used for integer values, and enumeration
229 -- or character literals. In the latter two cases, the value returned is
230 -- the Pos value in the relevant enumeration type. It can also be used for
231 -- fixed-point values, in which case it returns the corresponding integer
232 -- value. It cannot be used for floating-point values.
234 function Expr_Value_E (N : Node_Id) return Entity_Id;
235 -- Returns the folded value of the expression. This function is called in
236 -- instances where it has already been determined that the expression is
237 -- static or its value known at compile time. This version is used for
238 -- enumeration types and returns the corresponding enumeration literal.
240 function Expr_Value_R (N : Node_Id) return Ureal;
241 -- Returns the folded value of the expression. This function is called in
242 -- instances where it has already been determined that the expression is
243 -- static or its value known at compile time. This version is used for real
244 -- values (including both the floating-point and fixed-point cases). In the
245 -- case of a fixed-point type, the real value is returned (cf above version
246 -- returning Uint).
248 function Expr_Value_S (N : Node_Id) return Node_Id;
249 -- Returns the folded value of the expression. This function is called
250 -- in instances where it has already been determined that the expression
251 -- is static or its value is known at compile time. This version is used
252 -- for string types and returns the corresponding N_String_Literal node.
254 function Expr_Rep_Value (N : Node_Id) return Uint;
255 -- This is identical to Expr_Value, except in the case of enumeration
256 -- literals of types for which an enumeration representation clause has
257 -- been given, in which case it returns the representation value rather
258 -- than the pos value. This is the value that is needed for generating code
259 -- sequences, while the Expr_Value value is appropriate for compile time
260 -- constraint errors or getting the logical value. Note that this function
261 -- does NOT concern itself with biased values, if the caller needs a
262 -- properly biased value, the subtraction of the bias must be handled
263 -- explicitly.
265 procedure Eval_Actual (N : Node_Id);
266 procedure Eval_Allocator (N : Node_Id);
267 procedure Eval_Arithmetic_Op (N : Node_Id);
268 procedure Eval_Call (N : Node_Id);
269 procedure Eval_Character_Literal (N : Node_Id);
270 procedure Eval_Concatenation (N : Node_Id);
271 procedure Eval_Conditional_Expression (N : Node_Id);
272 procedure Eval_Entity_Name (N : Node_Id);
273 procedure Eval_Indexed_Component (N : Node_Id);
274 procedure Eval_Integer_Literal (N : Node_Id);
275 procedure Eval_Logical_Op (N : Node_Id);
276 procedure Eval_Membership_Op (N : Node_Id);
277 procedure Eval_Named_Integer (N : Node_Id);
278 procedure Eval_Named_Real (N : Node_Id);
279 procedure Eval_Op_Expon (N : Node_Id);
280 procedure Eval_Op_Not (N : Node_Id);
281 procedure Eval_Real_Literal (N : Node_Id);
282 procedure Eval_Relational_Op (N : Node_Id);
283 procedure Eval_Shift (N : Node_Id);
284 procedure Eval_Short_Circuit (N : Node_Id);
285 procedure Eval_Slice (N : Node_Id);
286 procedure Eval_String_Literal (N : Node_Id);
287 procedure Eval_Qualified_Expression (N : Node_Id);
288 procedure Eval_Type_Conversion (N : Node_Id);
289 procedure Eval_Unary_Op (N : Node_Id);
290 procedure Eval_Unchecked_Conversion (N : Node_Id);
292 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean);
293 -- Rewrite N with a new N_String_Literal node as the result of the compile
294 -- time evaluation of the node N. Val is the resulting string value from
295 -- the folding operation. The Is_Static_Expression flag is set in the
296 -- result node. The result is fully analyzed and resolved. Static indicates
297 -- whether the result should be considered static or not (True = consider
298 -- static). The point here is that normally all string literals are static,
299 -- but if this was the result of some sequence of evaluation where values
300 -- were known at compile time but not static, then the result is not
301 -- static.
303 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean);
304 -- Rewrite N with a (N_Integer_Literal, N_Identifier, N_Character_Literal)
305 -- node as the result of the compile time evaluation of the node N. Val is
306 -- the result in the integer case and is the position of the literal in the
307 -- literals list for the enumeration case. Is_Static_Expression is set True
308 -- in the result node. The result is fully analyzed/resolved. Static
309 -- indicates whether the result should be considered static or not (True =
310 -- consider static). The point here is that normally all string literals
311 -- are static, but if this was the result of some sequence of evaluation
312 -- where values were known at compile time but not static, then the result
313 -- is not static.
315 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean);
316 -- Rewrite N with a new N_Real_Literal node as the result of the compile
317 -- time evaluation of the node N. Val is the resulting real value from the
318 -- folding operation. The Is_Static_Expression flag is set in the result
319 -- node. The result is fully analyzed and result. Static indicates whether
320 -- the result should be considered static or not (True = consider static).
321 -- The point here is that normally all string literals are static, but if
322 -- this was the result of some sequence of evaluation where values were
323 -- known at compile time but not static, then the result is not static.
325 function Is_In_Range
326 (N : Node_Id;
327 Typ : Entity_Id;
328 Fixed_Int : Boolean := False;
329 Int_Real : Boolean := False) return Boolean;
330 -- Returns True if it can be guaranteed at compile time that expression is
331 -- known to be in range of the subtype Typ. If the values of N or of either
332 -- bouds of Type are unknown at compile time, False will always be
333 -- returned. A result of False does not mean that the expression is out of
334 -- range, merely that it cannot be determined at compile time that it is in
335 -- range. If Typ is a floating point type or Int_Real is set, any integer
336 -- value is treated as though it was a real value (i.e. the underlying real
337 -- value is used). In this case we use the corresponding real value, both
338 -- for the bounds of Typ, and for the value of the expression N. If Typ is
339 -- a fixed type or a discrete type and Int_Real is False but flag Fixed_Int
340 -- is True then any fixed-point value is treated as though it was discrete
341 -- value (i.e. the underlying integer value is used). In this case we use
342 -- the corresponding integer value, both for the bounds of Typ, and for the
343 -- value of the expression N. If Typ is a discret type and Fixed_Int as
344 -- well as Int_Real are false, intere values are used throughout.
346 function Is_Out_Of_Range
347 (N : Node_Id;
348 Typ : Entity_Id;
349 Fixed_Int : Boolean := False;
350 Int_Real : Boolean := False) return Boolean;
351 -- Returns True if it can be guaranteed at compile time that expression is
352 -- known to be out of range of the subtype Typ. True is returned if Typ is
353 -- a scalar type, at least one of whose bounds is known at compile time,
354 -- and N is a compile time known expression which can be determined to be
355 -- outside a compile_time known bound of Typ. A result of False does not
356 -- mean that the expression is in range, but rather merely that it cannot
357 -- be determined at compile time that it is out of range. Flags Int_Real
358 -- and Fixed_Int are used as in routine Is_In_Range above.
360 function In_Subrange_Of
361 (T1 : Entity_Id;
362 T2 : Entity_Id;
363 Fixed_Int : Boolean := False) return Boolean;
364 -- Returns True if it can be guaranteed at compile time that the range of
365 -- values for scalar type T1 are always in the range of scalar type T2. A
366 -- result of False does not mean that T1 is not in T2's subrange, only that
367 -- it cannot be determined at compile time. Flag Fixed_Int is used as in
368 -- routine Is_In_Range above.
370 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean;
371 -- Returns True if it can guarantee that Lo .. Hi is a null range. If it
372 -- cannot (because the value of Lo or Hi is not known at compile time) then
373 -- it returns False.
375 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean;
376 -- Returns True if it can guarantee that Lo .. Hi is not a null range. If
377 -- it cannot (because the value of Lo or Hi is not known at compile time)
378 -- then it returns False.
380 procedure Why_Not_Static (Expr : Node_Id);
381 -- This procedure may be called after generating an error message that
382 -- complains that something is non-static. If it finds good reasons, it
383 -- generates one or more error messages pointing the appropriate offending
384 -- component of the expression. If no good reasons can be figured out, then
385 -- no messages are generated. The expectation here is that the caller has
386 -- already issued a message complaining that the expression is non-static.
387 -- Note that this message should be placed using Error_Msg_F or
388 -- Error_Msg_FE, so that it will sort before any messages placed by this
389 -- call. Note that it is fine to call Why_Not_Static with something that is
390 -- not an expression, and usually this has no effect, but in some cases
391 -- (N_Parameter_Association or N_Range), it makes sense for the internal
392 -- recursive calls.
394 procedure Initialize;
395 -- Initializes the internal data structures. Must be called before each
396 -- separate main program unit (e.g. in a GNSA/ASIS context).
398 private
399 -- The Eval routines are all marked inline, since they are called once
401 pragma Inline (Eval_Actual);
402 pragma Inline (Eval_Allocator);
403 pragma Inline (Eval_Character_Literal);
404 pragma Inline (Eval_Conditional_Expression);
405 pragma Inline (Eval_Indexed_Component);
406 pragma Inline (Eval_Named_Integer);
407 pragma Inline (Eval_Named_Real);
408 pragma Inline (Eval_Real_Literal);
409 pragma Inline (Eval_Shift);
410 pragma Inline (Eval_Slice);
411 pragma Inline (Eval_String_Literal);
412 pragma Inline (Eval_Unchecked_Conversion);
414 pragma Inline (Is_OK_Static_Expression);
416 end Sem_Eval;