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