annotate gcc/ada/sem_eval.ads @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- S E M _ E V A L --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
9 -- Copyright (C) 1992-2019, Free Software Foundation, Inc. --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
kono
parents:
diff changeset
17 -- for more details. You should have received a copy of the GNU General --
kono
parents:
diff changeset
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
kono
parents:
diff changeset
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
kono
parents:
diff changeset
20 -- --
kono
parents:
diff changeset
21 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
23 -- --
kono
parents:
diff changeset
24 ------------------------------------------------------------------------------
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 -- This package contains various subprograms involved in compile time
kono
parents:
diff changeset
27 -- evaluation of expressions and checks for staticness of expressions and
kono
parents:
diff changeset
28 -- types. It also contains the circuitry for checking for violations of pure
kono
parents:
diff changeset
29 -- and preelaborated conditions (this naturally goes here, since these rules
kono
parents:
diff changeset
30 -- involve consideration of staticness).
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- Note: the static evaluation for attributes is found in Sem_Attr even though
kono
parents:
diff changeset
33 -- logically it belongs here. We have done this so that it is easier to add
kono
parents:
diff changeset
34 -- new attributes to GNAT.
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 with Types; use Types;
kono
parents:
diff changeset
37 with Uintp; use Uintp;
kono
parents:
diff changeset
38 with Urealp; use Urealp;
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 package Sem_Eval is
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 ------------------------------------
kono
parents:
diff changeset
43 -- Handling of Static Expressions --
kono
parents:
diff changeset
44 ------------------------------------
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 -- This package contains a set of routines that process individual
kono
parents:
diff changeset
47 -- subexpression nodes with the objective of folding (precomputing) the
kono
parents:
diff changeset
48 -- value of static expressions that are known at compile time and properly
kono
parents:
diff changeset
49 -- computing the setting of two flags that appear in every subexpression
kono
parents:
diff changeset
50 -- node:
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 -- Is_Static_Expression
kono
parents:
diff changeset
53
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
54 -- True for static expressions, as defined in RM-4.9.
111
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 -- Raises_Constraint_Error
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 -- This flag indicates that it is known at compile time that the
kono
parents:
diff changeset
59 -- evaluation of an expression raises constraint error. If the
kono
parents:
diff changeset
60 -- expression is static, and this flag is off, then it is also known at
kono
parents:
diff changeset
61 -- compile time that the expression does not raise constraint error
kono
parents:
diff changeset
62 -- (i.e. the flag is accurate for static expressions, and conservative
kono
parents:
diff changeset
63 -- for non-static expressions.
kono
parents:
diff changeset
64
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
65 -- See also Is_OK_Static_Expression, which is True for static
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
66 -- expressions that do not raise Constraint_Error. This is used in most
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
67 -- legality checks, because static expressions that raise Constraint_Error
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
68 -- are usually illegal.
111
kono
parents:
diff changeset
69
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
70 -- See also Compile_Time_Known_Value, which is True for an expression whose
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
71 -- value is known at compile time. In this case, the expression is folded
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
72 -- to a literal or to a constant that is itself (recursively) either a
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
73 -- literal or a constant
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
74
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
75 -- Is_[OK_]Static_Expression are used for legality checks, whereas
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
76 -- Compile_Time_Known_Value is used for optimization purposes.
111
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 -- When we are analyzing and evaluating static expressions, we propagate
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
79 -- both flags. Usually if a subexpression raises a Constraint_Error, then
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
80 -- so will its parent expression, and Raise_Constraint_Error will be
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
81 -- propagated to this parent. The exception is conditional cases like
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
82 -- (True or else 1/0 = 0), which results in an expression that has the
111
kono
parents:
diff changeset
83 -- Is_Static_Expression flag True, and Raises_Constraint_Error False. Even
kono
parents:
diff changeset
84 -- though 1/0 would raise an exception, the right operand is never actually
kono
parents:
diff changeset
85 -- executed, so the expression as a whole does not raise CE.
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 -- Finally, the case of static predicates. These are applied only to entire
kono
parents:
diff changeset
88 -- expressions, not to subexpressions, so we do not have the case of having
kono
parents:
diff changeset
89 -- to propagate this information. We handle this case simply by resetting
kono
parents:
diff changeset
90 -- the Is_Static_Expression flag if a static predicate fails. Note that we
kono
parents:
diff changeset
91 -- can't use this simpler approach for the constraint error case because of
kono
parents:
diff changeset
92 -- the (True or else 1/0 = 0) example discussed above.
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 -------------------------------
kono
parents:
diff changeset
95 -- Compile-Time Known Values --
kono
parents:
diff changeset
96 -------------------------------
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 -- For most legality checking purposes the flag Is_Static_Expression
kono
parents:
diff changeset
99 -- defined in Sinfo should be used. This package also provides a routine
kono
parents:
diff changeset
100 -- called Is_OK_Static_Expression which in addition of checking that an
kono
parents:
diff changeset
101 -- expression is static in the RM 4.9 sense, it checks that the expression
kono
parents:
diff changeset
102 -- does not raise constraint error. In fact for certain legality checks not
kono
parents:
diff changeset
103 -- only do we need to ascertain that the expression is static, but we must
kono
parents:
diff changeset
104 -- also ensure that it does not raise constraint error.
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 -- Neither of Is_Static_Expression and Is_OK_Static_Expression should be
kono
parents:
diff changeset
107 -- used for compile time evaluation purposes. In fact certain expression
kono
parents:
diff changeset
108 -- whose value may be known at compile time are not static in the RM 4.9
kono
parents:
diff changeset
109 -- sense. A typical example is:
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 -- C : constant Integer := Record_Type'Size;
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 -- The expression 'C' is not static in the technical RM sense, but for many
kono
parents:
diff changeset
114 -- simple record types, the size is in fact known at compile time. When we
kono
parents:
diff changeset
115 -- are trying to perform compile time constant folding (for instance for
kono
parents:
diff changeset
116 -- expressions like C + 1, Is_Static_Expression or Is_OK_Static_Expression
kono
parents:
diff changeset
117 -- are not the right functions to test if folding is possible. Instead, we
kono
parents:
diff changeset
118 -- use Compile_Time_Known_Value. All static expressions that do not raise
kono
parents:
diff changeset
119 -- constraint error (i.e. those for which Is_OK_Static_Expression is true)
kono
parents:
diff changeset
120 -- are known at compile time, but as shown by the above example, there may
kono
parents:
diff changeset
121 -- be cases of non-static expressions which are known at compile time.
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 -----------------
kono
parents:
diff changeset
124 -- Subprograms --
kono
parents:
diff changeset
125 -----------------
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 procedure Check_Expression_Against_Static_Predicate
kono
parents:
diff changeset
128 (Expr : Node_Id;
kono
parents:
diff changeset
129 Typ : Entity_Id);
kono
parents:
diff changeset
130 -- Determine whether an arbitrary expression satisfies the static predicate
kono
parents:
diff changeset
131 -- of a type. The routine does nothing if Expr is not known at compile time
kono
parents:
diff changeset
132 -- or Typ lacks a static predicate, otherwise it may emit a warning if the
kono
parents:
diff changeset
133 -- expression is prohibited by the predicate. If the expression is a static
kono
parents:
diff changeset
134 -- expression and it fails a predicate that was not explicitly stated to be
kono
parents:
diff changeset
135 -- a dynamic predicate, then an additional warning is given, and the flag
kono
parents:
diff changeset
136 -- Is_Static_Expression is reset on Expr.
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 procedure Check_Non_Static_Context (N : Node_Id);
kono
parents:
diff changeset
139 -- Deals with the special check required for a static expression that
kono
parents:
diff changeset
140 -- appears in a non-static context, i.e. is not part of a larger static
kono
parents:
diff changeset
141 -- expression (see RM 4.9(35)), i.e. the value of the expression must be
kono
parents:
diff changeset
142 -- within the base range of the base type of its expected type. A check is
kono
parents:
diff changeset
143 -- also made for expressions that are inside the base range, but outside
kono
parents:
diff changeset
144 -- the range of the expected subtype (this is a warning message rather than
kono
parents:
diff changeset
145 -- an illegality).
kono
parents:
diff changeset
146 --
kono
parents:
diff changeset
147 -- Note: most cases of non-static context checks are handled within
kono
parents:
diff changeset
148 -- Sem_Eval itself, including all cases of expressions at the outer level
kono
parents:
diff changeset
149 -- (i.e. those that are not a subexpression). Currently the only outside
kono
parents:
diff changeset
150 -- customer for this procedure is Sem_Attr (because Eval_Attribute is
kono
parents:
diff changeset
151 -- there). There is also one special case arising from ranges (see body of
kono
parents:
diff changeset
152 -- Resolve_Range).
kono
parents:
diff changeset
153 --
kono
parents:
diff changeset
154 -- Note: this procedure is also called by GNATprove on real literals
kono
parents:
diff changeset
155 -- that are not sub-expressions of static expressions, to convert them to
kono
parents:
diff changeset
156 -- machine numbers, as GNATprove cannot perform this conversion contrary
kono
parents:
diff changeset
157 -- to gigi.
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id);
kono
parents:
diff changeset
160 -- N is either a string literal, or a constraint error node. In the latter
kono
parents:
diff changeset
161 -- case, the situation is already dealt with, and the call has no effect.
kono
parents:
diff changeset
162 -- In the former case, if the target type, Ttyp is constrained, then a
kono
parents:
diff changeset
163 -- check is made to see if the string literal is of appropriate length.
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 type Compare_Result is (LT, LE, EQ, GT, GE, NE, Unknown);
kono
parents:
diff changeset
166 subtype Compare_GE is Compare_Result range EQ .. GE;
kono
parents:
diff changeset
167 subtype Compare_LE is Compare_Result range LT .. EQ;
kono
parents:
diff changeset
168 -- Result subtypes for Compile_Time_Compare subprograms
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 function Compile_Time_Compare
kono
parents:
diff changeset
171 (L, R : Node_Id;
kono
parents:
diff changeset
172 Assume_Valid : Boolean) return Compare_Result;
kono
parents:
diff changeset
173 pragma Inline (Compile_Time_Compare);
kono
parents:
diff changeset
174 -- Given two expression nodes, finds out whether it can be determined at
kono
parents:
diff changeset
175 -- compile time how the runtime values will compare. An Unknown result
kono
parents:
diff changeset
176 -- means that the result of a comparison cannot be determined at compile
kono
parents:
diff changeset
177 -- time, otherwise the returned result indicates the known result of the
kono
parents:
diff changeset
178 -- comparison, given as tightly as possible (i.e. EQ or LT is preferred
kono
parents:
diff changeset
179 -- returned value to LE). If Assume_Valid is true, the result reflects
kono
parents:
diff changeset
180 -- the result of assuming that entities involved in the comparison have
kono
parents:
diff changeset
181 -- valid representations. If Assume_Valid is false, then the base type of
kono
parents:
diff changeset
182 -- any involved entity is used so that no assumption of validity is made.
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 function Compile_Time_Compare
kono
parents:
diff changeset
185 (L, R : Node_Id;
kono
parents:
diff changeset
186 Diff : access Uint;
kono
parents:
diff changeset
187 Assume_Valid : Boolean;
kono
parents:
diff changeset
188 Rec : Boolean := False) return Compare_Result;
kono
parents:
diff changeset
189 -- This version of Compile_Time_Compare returns extra information if the
kono
parents:
diff changeset
190 -- result is GT or LT. In these cases, if the magnitude of the difference
kono
parents:
diff changeset
191 -- can be determined at compile time, this (positive) magnitude is returned
kono
parents:
diff changeset
192 -- in Diff.all. If the magnitude of the difference cannot be determined
kono
parents:
diff changeset
193 -- then Diff.all contains No_Uint on return. Rec is a parameter that is set
kono
parents:
diff changeset
194 -- True for a recursive call from within Compile_Time_Compare to avoid some
kono
parents:
diff changeset
195 -- infinite recursion cases. It should never be set by a client.
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean;
kono
parents:
diff changeset
198 -- If T is an array whose index bounds are all known at compile time, then
kono
parents:
diff changeset
199 -- True is returned. If T is not an array type, or one or more of its index
kono
parents:
diff changeset
200 -- bounds is not known at compile time, then False is returned.
kono
parents:
diff changeset
201
kono
parents:
diff changeset
202 function Compile_Time_Known_Value (Op : Node_Id) return Boolean;
kono
parents:
diff changeset
203 -- Returns true if Op is an expression not raising Constraint_Error whose
kono
parents:
diff changeset
204 -- value is known at compile time and for which a call to Expr_Value can
kono
parents:
diff changeset
205 -- be used to determine this value. This is always true if Op is a static
kono
parents:
diff changeset
206 -- expression, but can also be true for expressions which are technically
kono
parents:
diff changeset
207 -- non-static but which are in fact known at compile time. Some examples of
kono
parents:
diff changeset
208 -- such expressions are the static lower bound of a non-static range or the
kono
parents:
diff changeset
209 -- value of a constant object whose initial value is itself compile time
kono
parents:
diff changeset
210 -- known in the sense of this routine. Note that this routine is defended
kono
parents:
diff changeset
211 -- against unanalyzed expressions. Such expressions will not cause a
kono
parents:
diff changeset
212 -- blowup, they may cause pessimistic (i.e. False) results to be returned.
kono
parents:
diff changeset
213 -- In general we take a pessimistic view. False does not mean the value
kono
parents:
diff changeset
214 -- could not be known at compile time, but True means that absolutely
kono
parents:
diff changeset
215 -- definition it is known at compile time and it is safe to call
kono
parents:
diff changeset
216 -- Expr_Value[_XX] on the expression Op.
kono
parents:
diff changeset
217 --
kono
parents:
diff changeset
218 -- Note that we don't define precisely the set of expressions that return
kono
parents:
diff changeset
219 -- True. Callers should not make any assumptions regarding the value that
kono
parents:
diff changeset
220 -- is returned for non-static expressions. Functional behavior should never
kono
parents:
diff changeset
221 -- be affected by whether a given non-static expression returns True or
kono
parents:
diff changeset
222 -- False when this function is called. In other words this is purely for
kono
parents:
diff changeset
223 -- efficiency optimization purposes. The code generated can often be more
kono
parents:
diff changeset
224 -- efficient with compile time known values, e.g. range analysis for the
kono
parents:
diff changeset
225 -- purpose of removing checks is more effective if we know precise bounds.
kono
parents:
diff changeset
226
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
227 -- WARNING: There is a matching C declaration of this subprogram in fe.h
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
228
111
kono
parents:
diff changeset
229 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean;
kono
parents:
diff changeset
230 -- Similar to Compile_Time_Known_Value, but also returns True if the value
kono
parents:
diff changeset
231 -- is a compile-time-known aggregate, i.e. an aggregate all of whose
kono
parents:
diff changeset
232 -- constituent expressions are either compile-time-known values (based on
kono
parents:
diff changeset
233 -- calling Compile_Time_Known_Value) or compile-time-known aggregates.
kono
parents:
diff changeset
234 -- Note that the aggregate could still involve run-time checks that might
kono
parents:
diff changeset
235 -- fail (such as for subtype checks in component associations), but the
kono
parents:
diff changeset
236 -- evaluation of the expressions themselves will not raise an exception.
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 function CRT_Safe_Compile_Time_Known_Value (Op : Node_Id) return Boolean;
kono
parents:
diff changeset
239 -- In the case of configurable run-times, there may be an issue calling
kono
parents:
diff changeset
240 -- Compile_Time_Known_Value with non-static expressions where the legality
kono
parents:
diff changeset
241 -- of the program is not well-defined. Consider this example:
kono
parents:
diff changeset
242 --
kono
parents:
diff changeset
243 -- X := B ** C;
kono
parents:
diff changeset
244 --
kono
parents:
diff changeset
245 -- Now if C is compile time known, and has the value 4, then inline code
kono
parents:
diff changeset
246 -- can be generated at compile time, instead of calling a run-time routine.
kono
parents:
diff changeset
247 -- That's fine in the normal case, but when we have a configurable run-time
kono
parents:
diff changeset
248 -- the run-time routine may not be available. This means that the program
kono
parents:
diff changeset
249 -- will be rejected if C is not known at compile time. We don't want the
kono
parents:
diff changeset
250 -- legality of a program to depend on how clever the implementation of this
kono
parents:
diff changeset
251 -- function is. If the run-time in use lacks the exponentiation routine,
kono
parents:
diff changeset
252 -- then what we say is that exponentiation is permitted if the exponent is
kono
parents:
diff changeset
253 -- officially static and has a value in the range 0 .. 4.
kono
parents:
diff changeset
254 --
kono
parents:
diff changeset
255 -- In a case like this, we use CRT_Safe_Compile_Time_Known_Value to avoid
kono
parents:
diff changeset
256 -- this effect. This routine will return False for a non-static expression
kono
parents:
diff changeset
257 -- if we are in configurable run-time mode, even if the expression would
kono
parents:
diff changeset
258 -- normally be considered compile-time known.
kono
parents:
diff changeset
259
kono
parents:
diff changeset
260 function Expr_Rep_Value (N : Node_Id) return Uint;
kono
parents:
diff changeset
261 -- This is identical to Expr_Value, except in the case of enumeration
kono
parents:
diff changeset
262 -- literals of types for which an enumeration representation clause has
kono
parents:
diff changeset
263 -- been given, in which case it returns the representation value rather
kono
parents:
diff changeset
264 -- than the pos value. This is the value that is needed for generating code
kono
parents:
diff changeset
265 -- sequences, while the Expr_Value value is appropriate for compile time
kono
parents:
diff changeset
266 -- constraint errors or getting the logical value. Note that this function
kono
parents:
diff changeset
267 -- does NOT concern itself with biased values, if the caller needs a
kono
parents:
diff changeset
268 -- properly biased value, the subtraction of the bias must be handled
kono
parents:
diff changeset
269 -- explicitly.
kono
parents:
diff changeset
270
kono
parents:
diff changeset
271 function Expr_Value (N : Node_Id) return Uint;
kono
parents:
diff changeset
272 -- Returns the folded value of the expression N. This function is called in
kono
parents:
diff changeset
273 -- instances where it has already been determined that the expression is
kono
parents:
diff changeset
274 -- static or its value is compile time known (Compile_Time_Known_Value (N)
kono
parents:
diff changeset
275 -- returns True). This version is used for integer values, and enumeration
kono
parents:
diff changeset
276 -- or character literals. In the latter two cases, the value returned is
kono
parents:
diff changeset
277 -- the Pos value in the relevant enumeration type. It can also be used for
kono
parents:
diff changeset
278 -- fixed-point values, in which case it returns the corresponding integer
kono
parents:
diff changeset
279 -- value. It cannot be used for floating-point values.
kono
parents:
diff changeset
280
kono
parents:
diff changeset
281 function Expr_Value_E (N : Node_Id) return Entity_Id;
kono
parents:
diff changeset
282 -- Returns the folded value of the expression. This function is called in
kono
parents:
diff changeset
283 -- instances where it has already been determined that the expression is
kono
parents:
diff changeset
284 -- static or its value known at compile time. This version is used for
kono
parents:
diff changeset
285 -- enumeration types and returns the corresponding enumeration literal.
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 function Expr_Value_R (N : Node_Id) return Ureal;
kono
parents:
diff changeset
288 -- Returns the folded value of the expression. This function is called in
kono
parents:
diff changeset
289 -- instances where it has already been determined that the expression is
kono
parents:
diff changeset
290 -- static or its value known at compile time. This version is used for real
kono
parents:
diff changeset
291 -- values (including both the floating-point and fixed-point cases). In the
kono
parents:
diff changeset
292 -- case of a fixed-point type, the real value is returned (cf above version
kono
parents:
diff changeset
293 -- returning Uint).
kono
parents:
diff changeset
294
kono
parents:
diff changeset
295 function Expr_Value_S (N : Node_Id) return Node_Id;
kono
parents:
diff changeset
296 -- Returns the folded value of the expression. This function is called
kono
parents:
diff changeset
297 -- in instances where it has already been determined that the expression
kono
parents:
diff changeset
298 -- is static or its value is known at compile time. This version is used
kono
parents:
diff changeset
299 -- for string types and returns the corresponding N_String_Literal node.
kono
parents:
diff changeset
300
kono
parents:
diff changeset
301 procedure Eval_Actual (N : Node_Id);
kono
parents:
diff changeset
302 procedure Eval_Allocator (N : Node_Id);
kono
parents:
diff changeset
303 procedure Eval_Arithmetic_Op (N : Node_Id);
kono
parents:
diff changeset
304 procedure Eval_Call (N : Node_Id);
kono
parents:
diff changeset
305 procedure Eval_Case_Expression (N : Node_Id);
kono
parents:
diff changeset
306 procedure Eval_Character_Literal (N : Node_Id);
kono
parents:
diff changeset
307 procedure Eval_Concatenation (N : Node_Id);
kono
parents:
diff changeset
308 procedure Eval_Entity_Name (N : Node_Id);
kono
parents:
diff changeset
309 procedure Eval_If_Expression (N : Node_Id);
kono
parents:
diff changeset
310 procedure Eval_Indexed_Component (N : Node_Id);
kono
parents:
diff changeset
311 procedure Eval_Integer_Literal (N : Node_Id);
kono
parents:
diff changeset
312 procedure Eval_Logical_Op (N : Node_Id);
kono
parents:
diff changeset
313 procedure Eval_Membership_Op (N : Node_Id);
kono
parents:
diff changeset
314 procedure Eval_Named_Integer (N : Node_Id);
kono
parents:
diff changeset
315 procedure Eval_Named_Real (N : Node_Id);
kono
parents:
diff changeset
316 procedure Eval_Op_Expon (N : Node_Id);
kono
parents:
diff changeset
317 procedure Eval_Op_Not (N : Node_Id);
kono
parents:
diff changeset
318 procedure Eval_Real_Literal (N : Node_Id);
kono
parents:
diff changeset
319 procedure Eval_Relational_Op (N : Node_Id);
kono
parents:
diff changeset
320 procedure Eval_Shift (N : Node_Id);
kono
parents:
diff changeset
321 procedure Eval_Short_Circuit (N : Node_Id);
kono
parents:
diff changeset
322 procedure Eval_Slice (N : Node_Id);
kono
parents:
diff changeset
323 procedure Eval_String_Literal (N : Node_Id);
kono
parents:
diff changeset
324 procedure Eval_Qualified_Expression (N : Node_Id);
kono
parents:
diff changeset
325 procedure Eval_Type_Conversion (N : Node_Id);
kono
parents:
diff changeset
326 procedure Eval_Unary_Op (N : Node_Id);
kono
parents:
diff changeset
327 procedure Eval_Unchecked_Conversion (N : Node_Id);
kono
parents:
diff changeset
328
kono
parents:
diff changeset
329 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id);
kono
parents:
diff changeset
330 -- This procedure is called after it has been determined that Expr is not
kono
parents:
diff changeset
331 -- static when it is required to be. Msg is the text of a message that
kono
parents:
diff changeset
332 -- explains the error. This procedure checks if an error is already posted
kono
parents:
diff changeset
333 -- on Expr, if so, it does nothing unless All_Errors_Mode is set in which
kono
parents:
diff changeset
334 -- case this flag is ignored. Otherwise the given message is posted using
kono
parents:
diff changeset
335 -- Error_Msg_F, and then Why_Not_Static is called on Expr to generate
kono
parents:
diff changeset
336 -- additional messages. The string given as Msg should end with ! to make
kono
parents:
diff changeset
337 -- it an unconditional message, to ensure that if it is posted, the entire
kono
parents:
diff changeset
338 -- set of messages is all posted.
kono
parents:
diff changeset
339
kono
parents:
diff changeset
340 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean);
kono
parents:
diff changeset
341 -- Rewrite N with a new N_String_Literal node as the result of the compile
kono
parents:
diff changeset
342 -- time evaluation of the node N. Val is the resulting string value from
kono
parents:
diff changeset
343 -- the folding operation. The Is_Static_Expression flag is set in the
kono
parents:
diff changeset
344 -- result node. The result is fully analyzed and resolved. Static indicates
kono
parents:
diff changeset
345 -- whether the result should be considered static or not (True = consider
kono
parents:
diff changeset
346 -- static). The point here is that normally all string literals are static,
kono
parents:
diff changeset
347 -- but if this was the result of some sequence of evaluation where values
kono
parents:
diff changeset
348 -- were known at compile time but not static, then the result is not
kono
parents:
diff changeset
349 -- static. The call has no effect if Raises_Constraint_Error (N) is True,
kono
parents:
diff changeset
350 -- since there is no point in folding if we have an error.
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean);
kono
parents:
diff changeset
353 -- Rewrite N with a (N_Integer_Literal, N_Identifier, N_Character_Literal)
kono
parents:
diff changeset
354 -- node as the result of the compile time evaluation of the node N. Val is
kono
parents:
diff changeset
355 -- the result in the integer case and is the position of the literal in the
kono
parents:
diff changeset
356 -- literals list for the enumeration case. Is_Static_Expression is set True
kono
parents:
diff changeset
357 -- in the result node. The result is fully analyzed/resolved. Static
kono
parents:
diff changeset
358 -- indicates whether the result should be considered static or not (True =
kono
parents:
diff changeset
359 -- consider static). The point here is that normally all integer literals
kono
parents:
diff changeset
360 -- are static, but if this was the result of some sequence of evaluation
kono
parents:
diff changeset
361 -- where values were known at compile time but not static, then the result
kono
parents:
diff changeset
362 -- is not static. The call has no effect if Raises_Constraint_Error (N) is
kono
parents:
diff changeset
363 -- True, since there is no point in folding if we have an error.
kono
parents:
diff changeset
364
kono
parents:
diff changeset
365 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean);
kono
parents:
diff changeset
366 -- Rewrite N with a new N_Real_Literal node as the result of the compile
kono
parents:
diff changeset
367 -- time evaluation of the node N. Val is the resulting real value from the
kono
parents:
diff changeset
368 -- folding operation. The Is_Static_Expression flag is set in the result
kono
parents:
diff changeset
369 -- node. The result is fully analyzed and result. Static indicates whether
kono
parents:
diff changeset
370 -- the result should be considered static or not (True = consider static).
kono
parents:
diff changeset
371 -- The point here is that normally all string literals are static, but if
kono
parents:
diff changeset
372 -- this was the result of some sequence of evaluation where values were
kono
parents:
diff changeset
373 -- known at compile time but not static, then the result is not static.
kono
parents:
diff changeset
374 -- The call has no effect if Raises_Constraint_Error (N) is True, since
kono
parents:
diff changeset
375 -- there is no point in folding if we have an error.
kono
parents:
diff changeset
376
kono
parents:
diff changeset
377 function Is_In_Range
kono
parents:
diff changeset
378 (N : Node_Id;
kono
parents:
diff changeset
379 Typ : Entity_Id;
kono
parents:
diff changeset
380 Assume_Valid : Boolean := False;
kono
parents:
diff changeset
381 Fixed_Int : Boolean := False;
kono
parents:
diff changeset
382 Int_Real : Boolean := False) return Boolean;
kono
parents:
diff changeset
383 -- Returns True if it can be guaranteed at compile time that expression
kono
parents:
diff changeset
384 -- N is known to be in range of the subtype Typ. A result of False does
kono
parents:
diff changeset
385 -- not mean that the expression is out of range, merely that it cannot be
kono
parents:
diff changeset
386 -- determined at compile time that it is in range. If Typ is a floating
kono
parents:
diff changeset
387 -- point type or Int_Real is set, any integer value is treated as though it
kono
parents:
diff changeset
388 -- was a real value (i.e. the underlying real value is used). In this case
kono
parents:
diff changeset
389 -- we use the corresponding real value, both for the bounds of Typ, and for
kono
parents:
diff changeset
390 -- the value of the expression N. If Typ is a fixed type or a discrete type
kono
parents:
diff changeset
391 -- and Int_Real is False but flag Fixed_Int is True then any fixed-point
kono
parents:
diff changeset
392 -- value is treated as though it was discrete value (i.e. the underlying
kono
parents:
diff changeset
393 -- integer value is used). In this case we use the corresponding integer
kono
parents:
diff changeset
394 -- value, both for the bounds of Typ, and for the value of the expression
kono
parents:
diff changeset
395 -- N. If Typ is a discrete type and Fixed_Int as well as Int_Real are
kono
parents:
diff changeset
396 -- false, integer values are used throughout.
kono
parents:
diff changeset
397 --
kono
parents:
diff changeset
398 -- If Assume_Valid is set True, then N is always assumed to contain a valid
kono
parents:
diff changeset
399 -- value. If Assume_Valid is set False, then N may be invalid (unless there
kono
parents:
diff changeset
400 -- is some independent way of knowing that it is valid, i.e. either it is
kono
parents:
diff changeset
401 -- an entity with Is_Known_Valid set, or Assume_No_Invalid_Values is True.
kono
parents:
diff changeset
402
kono
parents:
diff changeset
403 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean;
kono
parents:
diff changeset
404 -- Returns True if it can guarantee that Lo .. Hi is a null range. If it
kono
parents:
diff changeset
405 -- cannot (because the value of Lo or Hi is not known at compile time) then
kono
parents:
diff changeset
406 -- it returns False.
kono
parents:
diff changeset
407
kono
parents:
diff changeset
408 function Is_OK_Static_Expression (N : Node_Id) return Boolean;
kono
parents:
diff changeset
409 -- An OK static expression is one that is static in the RM definition sense
kono
parents:
diff changeset
410 -- and which does not raise constraint error. For most legality checking
kono
parents:
diff changeset
411 -- purposes you should use Is_Static_Expression. For those legality checks
kono
parents:
diff changeset
412 -- where the expression N should not raise constraint error use this
kono
parents:
diff changeset
413 -- routine. This routine is *not* to be used in contexts where the test is
kono
parents:
diff changeset
414 -- for compile time evaluation purposes. Use Compile_Time_Known_Value
kono
parents:
diff changeset
415 -- instead (see section on "Compile-Time Known Values" above).
kono
parents:
diff changeset
416
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
417 -- WARNING: There is a matching C declaration of this subprogram in fe.h
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
418
111
kono
parents:
diff changeset
419 function Is_OK_Static_Range (N : Node_Id) return Boolean;
kono
parents:
diff changeset
420 -- Determines if range is static, as defined in RM 4.9(26), and also checks
kono
parents:
diff changeset
421 -- that neither bound of the range raises constraint error, thus ensuring
kono
parents:
diff changeset
422 -- that both bounds of the range are compile-time evaluable (i.e. do not
kono
parents:
diff changeset
423 -- raise constraint error). A result of true means that the bounds are
kono
parents:
diff changeset
424 -- compile time evaluable. A result of false means they are not (either
kono
parents:
diff changeset
425 -- because the range is not static, or because one or the other bound
kono
parents:
diff changeset
426 -- raises CE).
kono
parents:
diff changeset
427
kono
parents:
diff changeset
428 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean;
kono
parents:
diff changeset
429 -- Determines whether a subtype fits the definition of an Ada static
kono
parents:
diff changeset
430 -- subtype as given in (RM 4.9(26)) with the additional check that neither
kono
parents:
diff changeset
431 -- bound raises constraint error (meaning that Expr_Value[_R|S] can be used
kono
parents:
diff changeset
432 -- on these bounds).
kono
parents:
diff changeset
433 --
kono
parents:
diff changeset
434 -- This differs from Is_Static_Subtype in that it includes the constraint
kono
parents:
diff changeset
435 -- error checks, which are missing from Is_Static_Subtype.
kono
parents:
diff changeset
436
kono
parents:
diff changeset
437 function Is_Out_Of_Range
kono
parents:
diff changeset
438 (N : Node_Id;
kono
parents:
diff changeset
439 Typ : Entity_Id;
kono
parents:
diff changeset
440 Assume_Valid : Boolean := False;
kono
parents:
diff changeset
441 Fixed_Int : Boolean := False;
kono
parents:
diff changeset
442 Int_Real : Boolean := False) return Boolean;
kono
parents:
diff changeset
443 -- Returns True if it can be guaranteed at compile time that expression is
kono
parents:
diff changeset
444 -- known to be out of range of the subtype Typ. True is returned if Typ is
kono
parents:
diff changeset
445 -- a scalar type, and the value of N can be determined to be outside the
kono
parents:
diff changeset
446 -- range of Typ. A result of False does not mean that the expression is in
kono
parents:
diff changeset
447 -- range, but rather merely that it cannot be determined at compile time
kono
parents:
diff changeset
448 -- that it is out of range. The parameters Assume_Valid, Fixed_Int, and
kono
parents:
diff changeset
449 -- Int_Real are as described for Is_In_Range above.
kono
parents:
diff changeset
450
kono
parents:
diff changeset
451 function Is_Static_Subtype (Typ : Entity_Id) return Boolean;
kono
parents:
diff changeset
452 -- Determines whether a subtype fits the definition of an Ada static
kono
parents:
diff changeset
453 -- subtype as given in (RM 4.9(26)).
kono
parents:
diff changeset
454 --
kono
parents:
diff changeset
455 -- This differs from Is_OK_Static_Subtype (which is what must be used by
kono
parents:
diff changeset
456 -- clients) in that it does not care whether the bounds raise a constraint
kono
parents:
diff changeset
457 -- error exception or not. Used for checking whether expressions are static
kono
parents:
diff changeset
458 -- in the 4.9 sense (without worrying about exceptions).
kono
parents:
diff changeset
459
kono
parents:
diff changeset
460 function Is_Statically_Unevaluated (Expr : Node_Id) return Boolean;
kono
parents:
diff changeset
461 -- This function returns True if the given expression Expr is statically
kono
parents:
diff changeset
462 -- unevaluated, as defined in (RM 4.9 (32.1-32.6)).
kono
parents:
diff changeset
463
kono
parents:
diff changeset
464 function In_Subrange_Of
kono
parents:
diff changeset
465 (T1 : Entity_Id;
kono
parents:
diff changeset
466 T2 : Entity_Id;
kono
parents:
diff changeset
467 Fixed_Int : Boolean := False) return Boolean;
kono
parents:
diff changeset
468 -- Returns True if it can be guaranteed at compile time that the range of
kono
parents:
diff changeset
469 -- values for scalar type T1 are always in the range of scalar type T2. A
kono
parents:
diff changeset
470 -- result of False does not mean that T1 is not in T2's subrange, only that
kono
parents:
diff changeset
471 -- it cannot be determined at compile time. Flag Fixed_Int is used as in
kono
parents:
diff changeset
472 -- routine Is_In_Range above.
kono
parents:
diff changeset
473
kono
parents:
diff changeset
474 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean;
kono
parents:
diff changeset
475 -- Returns True if it can guarantee that Lo .. Hi is not a null range. If
kono
parents:
diff changeset
476 -- it cannot (because the value of Lo or Hi is not known at compile time)
kono
parents:
diff changeset
477 -- then it returns False.
kono
parents:
diff changeset
478
kono
parents:
diff changeset
479 function Predicates_Match (T1, T2 : Entity_Id) return Boolean;
kono
parents:
diff changeset
480 -- In Ada 2012, subtypes statically match if their static predicates
kono
parents:
diff changeset
481 -- match as well. This function performs the required check that
kono
parents:
diff changeset
482 -- predicates match. Separated out from Subtypes_Statically_Match so
kono
parents:
diff changeset
483 -- that it can be used in specializing error messages.
kono
parents:
diff changeset
484
kono
parents:
diff changeset
485 function Subtypes_Statically_Compatible
kono
parents:
diff changeset
486 (T1 : Entity_Id;
kono
parents:
diff changeset
487 T2 : Entity_Id;
kono
parents:
diff changeset
488 Formal_Derived_Matching : Boolean := False) return Boolean;
kono
parents:
diff changeset
489 -- Returns true if the subtypes are unconstrained or the constraint on
kono
parents:
diff changeset
490 -- on T1 is statically compatible with T2 (as defined by 4.9.1(4)).
kono
parents:
diff changeset
491 -- Otherwise returns false. Formal_Derived_Matching indicates whether
kono
parents:
diff changeset
492 -- the type T1 is a generic actual being checked against ancestor T2
kono
parents:
diff changeset
493 -- in a formal derived type association.
kono
parents:
diff changeset
494
kono
parents:
diff changeset
495 function Subtypes_Statically_Match
kono
parents:
diff changeset
496 (T1 : Entity_Id;
kono
parents:
diff changeset
497 T2 : Entity_Id;
kono
parents:
diff changeset
498 Formal_Derived_Matching : Boolean := False) return Boolean;
kono
parents:
diff changeset
499 -- Determine whether two types T1, T2, which have the same base type,
kono
parents:
diff changeset
500 -- are statically matching subtypes (RM 4.9.1(1-2)). Also includes the
kono
parents:
diff changeset
501 -- extra GNAT rule that object sizes must match (this can be false for
kono
parents:
diff changeset
502 -- types that match in the RM sense because of use of 'Object_Size),
kono
parents:
diff changeset
503 -- except when testing a generic actual T1 against an ancestor T2 in a
kono
parents:
diff changeset
504 -- formal derived type association (indicated by Formal_Derived_Matching).
kono
parents:
diff changeset
505
kono
parents:
diff changeset
506 procedure Test_Comparison
kono
parents:
diff changeset
507 (Op : Node_Id;
kono
parents:
diff changeset
508 Assume_Valid : Boolean;
kono
parents:
diff changeset
509 True_Result : out Boolean;
kono
parents:
diff changeset
510 False_Result : out Boolean);
kono
parents:
diff changeset
511 -- Determine the outcome of evaluating comparison operator Op using routine
kono
parents:
diff changeset
512 -- Compile_Time_Compare. Assume_Valid should be set when the operands are
kono
parents:
diff changeset
513 -- to be assumed valid. Flags True_Result and False_Result are set when the
kono
parents:
diff changeset
514 -- comparison evaluates to True or False respectively.
kono
parents:
diff changeset
515
kono
parents:
diff changeset
516 procedure Why_Not_Static (Expr : Node_Id);
kono
parents:
diff changeset
517 -- This procedure may be called after generating an error message that
kono
parents:
diff changeset
518 -- complains that something is non-static. If it finds good reasons, it
kono
parents:
diff changeset
519 -- generates one or more error messages pointing the appropriate offending
kono
parents:
diff changeset
520 -- component of the expression. If no good reasons can be figured out, then
kono
parents:
diff changeset
521 -- no messages are generated. The expectation here is that the caller has
kono
parents:
diff changeset
522 -- already issued a message complaining that the expression is non-static.
kono
parents:
diff changeset
523 -- Note that this message should be placed using Error_Msg_F or
kono
parents:
diff changeset
524 -- Error_Msg_FE, so that it will sort before any messages placed by this
kono
parents:
diff changeset
525 -- call. Note that it is fine to call Why_Not_Static with something that
kono
parents:
diff changeset
526 -- is not an expression, and usually this has no effect, but in some cases
kono
parents:
diff changeset
527 -- (N_Parameter_Association or N_Range), it makes sense for the internal
kono
parents:
diff changeset
528 -- recursive calls.
kono
parents:
diff changeset
529 --
kono
parents:
diff changeset
530 -- Note that these messages are not continuation messages, instead they are
kono
parents:
diff changeset
531 -- separate unconditional messages, marked with '!'. The reason for this is
kono
parents:
diff changeset
532 -- that they can be posted at a different location from the main message as
kono
parents:
diff changeset
533 -- documented above ("appropriate offending component"), and continuation
kono
parents:
diff changeset
534 -- messages must always point to the same location as the parent message.
kono
parents:
diff changeset
535
kono
parents:
diff changeset
536 procedure Initialize;
kono
parents:
diff changeset
537 -- Initializes the internal data structures. Must be called before each
kono
parents:
diff changeset
538 -- separate main program unit (e.g. in a GNSA/ASIS context).
kono
parents:
diff changeset
539
kono
parents:
diff changeset
540 private
kono
parents:
diff changeset
541 -- The Eval routines are all marked inline, since they are called once
kono
parents:
diff changeset
542
kono
parents:
diff changeset
543 pragma Inline (Eval_Actual);
kono
parents:
diff changeset
544 pragma Inline (Eval_Allocator);
kono
parents:
diff changeset
545 pragma Inline (Eval_Character_Literal);
kono
parents:
diff changeset
546 pragma Inline (Eval_If_Expression);
kono
parents:
diff changeset
547 pragma Inline (Eval_Indexed_Component);
kono
parents:
diff changeset
548 pragma Inline (Eval_Named_Integer);
kono
parents:
diff changeset
549 pragma Inline (Eval_Named_Real);
kono
parents:
diff changeset
550 pragma Inline (Eval_Real_Literal);
kono
parents:
diff changeset
551 pragma Inline (Eval_Shift);
kono
parents:
diff changeset
552 pragma Inline (Eval_Slice);
kono
parents:
diff changeset
553 pragma Inline (Eval_String_Literal);
kono
parents:
diff changeset
554 pragma Inline (Eval_Unchecked_Conversion);
kono
parents:
diff changeset
555
kono
parents:
diff changeset
556 pragma Inline (Is_OK_Static_Expression);
kono
parents:
diff changeset
557
kono
parents:
diff changeset
558 end Sem_Eval;