]> gcc.gnu.org Git - gcc.git/blob - gcc/java/check-init.c
Make-lang.in, [...]: Replace "GNU CC" with "GCC" in the copyright header.
[gcc.git] / gcc / java / check-init.c
1 /* Code to test for "definitive [un]assignment".
2 Copyright (C) 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24
25 /* Written by Per Bothner <bothner@cygnus.com>, January 1999. */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "flags.h" /* Needed for optimize. */
33 #include "java-tree.h"
34 #include "toplev.h" /* Needed for fatal. */
35
36 /* The basic idea is that we assign each local variable declaration
37 and each blank final field an index, and then we pass around
38 bitstrings, where the (2*i)'th bit is set if decl whose DECL_BIT_INDEX
39 is i is definitely assigned, and the the (2*i=1)'th bit is set if
40 decl whose DECL_BIT_INDEX is i is definitely unassigned */
41
42 /* One segment of a bitstring. */
43 typedef unsigned int word;
44
45 /* Pointer to a bitstring. */
46 typedef word *words;
47
48 /* Number of locals variables currently active. */
49 static int num_current_locals = 0;
50
51 /* The value of num_current_locals when we entered the closest
52 enclosing LOOP_EXPR. */
53 static int loop_current_locals;
54
55 /* The index of the first local variable in the current block.
56
57 The variables whose DECL_BIT_INDEX are in the range from
58 start_current_locals (inclusive) up to num_current_locals (exclusive)
59 are declared in the "current" block. If there is a loop or branch
60 form, we set start_current_locals to num_current_locals to indicate
61 there is no current block.
62
63 The point is that if a variable in the current block is set,
64 there are no other control paths that we have to worry about.
65 Hence, we can remove it from the set of variables we are
66 checking, making its bit index available for some other variable.
67 For simplicity, we only do that if the variable's bit index
68 is (num_current_locals-1); freeing up its bit index is then
69 just a simple matter of decrementing num_current_locals.
70 The reason this is worth doing is that it is simple, and
71 allows us to use short (usually one-word) bit-strings,
72 even for methods with thousands of local variables, as
73 long as most of them are initialized immediately after or in
74 their declaration. */
75 static int start_current_locals = 0;
76
77 static int num_current_words;
78
79 static tree wfl;
80
81 #define COPYN(DST, SRC, NWORDS) memcpy (DST, SRC, NWORDS * sizeof(word))
82 #define COPY(DST, SRC) COPYN (DST, SRC, num_current_words)
83
84 #define SET_ALL(DST) memset (DST, ~0, num_current_words * sizeof(word))
85 #define CLEAR_ALL(DST) memset (DST, 0, num_current_words * sizeof(word))
86
87 #define INTERSECTN(DST, SRC1, SRC2, N) \
88 do { int n = N; \
89 while (--n >= 0) DST[n] = SRC1[n] & SRC2[n]; \
90 } while (0)
91
92 #define UNION(DST, SRC1, SRC2) \
93 UNIONN (DST, SRC1, SRC2, num_current_words)
94
95 #define UNIONN(DST, SRC1, SRC2, N) \
96 do { int n = N; \
97 while (--n >= 0) DST[n] = SRC1[n] | SRC2[n]; \
98 } while (0)
99
100 #define INTERSECT(DST, SRC1, SRC2) \
101 INTERSECTN (DST, SRC1, SRC2, num_current_words)
102
103 #define WORD_SIZE ((unsigned int)(sizeof(word) * BITS_PER_UNIT))
104
105 static void check_bool_init PARAMS ((tree, words, words, words));
106 static void check_init PARAMS ((tree, words));
107 static void check_cond_init PARAMS ((tree, tree, tree, words, words, words));
108 static void check_bool2_init PARAMS ((enum tree_code, tree, tree, words, words, words));
109 struct alternatives;
110 static void done_alternative PARAMS ((words, struct alternatives *));
111 static tree get_variable_decl PARAMS ((tree));
112 static void final_assign_error PARAMS ((tree));
113 static void check_final_reassigned PARAMS ((tree, words));
114
115 #define ALLOC_WORDS(NUM) (xmalloc ((NUM) * sizeof (word)))
116 #define FREE_WORDS(PTR) (free (PTR))
117
118 /* DECLARE_BUFFERS is used to allocate NUMBUFFER bit sets, each of
119 which is an array of length num_current_words number of words.
120 Declares a new local variable BUFFER to hold the result (or rather
121 a pointer to the first of the bit sets). In almost all cases
122 num_current_words will be 1 or at most 2, so we try to stack
123 allocate the arrays in that case, using a stack array
124 named BUFFER##_short. Each DECLARE_BUFFERS must be matched by
125 a corresponding RELEASE_BUFFERS to avoid memory leaks. */
126
127 #define DECLARE_BUFFERS(BUFFER, NUMBUFFERS) \
128 word BUFFER##_short[2 * NUMBUFFERS]; \
129 words BUFFER = ALLOC_BUFFER(BUFFER##_short, NUMBUFFERS * num_current_words)
130
131 #define RELEASE_BUFFERS(BUFFER) \
132 FREE_BUFFER(BUFFER, BUFFER##_short)
133
134 #define ALLOC_BUFFER(SHORTBUFFER, NUMWORDS) \
135 ((NUMWORDS) * sizeof(word) <= sizeof(SHORTBUFFER) ? SHORTBUFFER \
136 : ALLOC_WORDS(NUMWORDS))
137
138 #define FREE_BUFFER(BUFFER, SHORTBUFFER) \
139 if (BUFFER != SHORTBUFFER) FREE_WORDS(BUFFER)
140
141 #define SET_P(WORDS, BIT) \
142 (WORDS[(BIT) / WORD_SIZE] & (1 << ((BIT) % WORD_SIZE)))
143
144 #define CLEAR_BIT(WORDS, BIT) \
145 (WORDS[(BIT) / WORD_SIZE] &= ~ (1 << ((BIT) % WORD_SIZE)))
146
147 #define SET_BIT(WORDS, BIT) \
148 (WORDS[(BIT) / WORD_SIZE] |= (1 << ((BIT) % WORD_SIZE)))
149
150 #define WORDS_NEEDED(BITS) (((BITS)+(WORD_SIZE-1))/(WORD_SIZE))
151
152 #define ASSIGNED_P(WORDS, BIT) SET_P(WORDS, 2 * (BIT))
153 #define UNASSIGNED_P(WORDS, BIT) SET_P(WORDS, 2 * (BIT) + 1)
154
155 #define SET_ASSIGNED(WORDS, INDEX) SET_BIT (WORDS, 2 * (INDEX))
156 #define SET_UNASSIGNED(WORDS, INDEX) SET_BIT (WORDS, 2 * (INDEX) + 1)
157
158 #define CLEAR_ASSIGNED(WORDS, INDEX) CLEAR_BIT (WORDS, 2 * (INDEX))
159 #define CLEAR_UNASSIGNED(WORDS, INDEX) CLEAR_BIT (WORDS, 2 * (INDEX) + 1)
160
161 /* Get the "interesting" declaration from a MODIFY_EXPR or COMPONENT_REF.
162 Return the declaration or NULL_TREE if no interesting declaration. */
163
164 static tree
165 get_variable_decl (exp)
166 tree exp;
167 {
168 if (TREE_CODE (exp) == VAR_DECL)
169 {
170 if (! TREE_STATIC (exp) || FIELD_FINAL (exp))
171 return exp;
172 }
173 /* We only care about final parameters. */
174 else if (TREE_CODE (exp) == PARM_DECL)
175 {
176 if (DECL_FINAL (exp))
177 return exp;
178 }
179 /* See if exp is this.field. */
180 else if (TREE_CODE (exp) == COMPONENT_REF)
181 {
182 tree op0 = TREE_OPERAND (exp, 0);
183 tree op1 = TREE_OPERAND (exp, 1);
184 tree mdecl = current_function_decl;
185 if (TREE_CODE (op0) == INDIRECT_REF
186 && TREE_CODE (op1) == FIELD_DECL
187 && ! METHOD_STATIC (mdecl)
188 && FIELD_FINAL (op1))
189 {
190 op0 = TREE_OPERAND (op0, 0);
191 if (op0 == BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)))
192 return op1;
193 }
194 }
195 return NULL_TREE;
196 }
197
198 static void
199 final_assign_error (name)
200 tree name;
201 {
202 static const char format[]
203 = "can't reassign a value to the final variable '%s'";
204 parse_error_context (wfl, format, IDENTIFIER_POINTER (name));
205 }
206
207 static void
208 check_final_reassigned (decl, before)
209 tree decl;
210 words before;
211 {
212 int index = DECL_BIT_INDEX (decl);
213 /* A final local already assigned or a final parameter
214 assigned must be reported as errors */
215 if (DECL_FINAL (decl) && index != -2
216 && (index < loop_current_locals /* I.e. -1, or outside current loop. */
217 || ! UNASSIGNED_P (before, index)))
218 {
219 final_assign_error (DECL_NAME (decl));
220 }
221 }
222
223 /* Check a conditional form (TEST_EXP ? THEN_EXP : ELSE_EXP) for
224 definite [un]assignment.
225 BEFORE, WHEN_FALSE, and WHEN_TRUE are as in check_bool_init. */
226
227 static void
228 check_cond_init (test_exp, then_exp, else_exp,
229 before, when_false, when_true)
230 tree test_exp, then_exp, else_exp;
231 words before, when_false, when_true;
232 {
233 int save_start_current_locals = start_current_locals;
234 DECLARE_BUFFERS(test_false, 6);
235 words test_true = test_false + num_current_words;
236 words then_false = test_true + num_current_words;
237 words then_true = then_false + num_current_words;
238 words else_false = then_true + num_current_words;
239 words else_true = else_false + num_current_words;
240 start_current_locals = num_current_locals;
241
242 check_bool_init (test_exp, before, test_false, test_true);
243 check_bool_init (then_exp, test_true, then_false, then_true);
244 check_bool_init (else_exp, test_false, else_false, else_true);
245 INTERSECT (when_false, then_false, else_false);
246 INTERSECT (when_true, then_true, else_true);
247 RELEASE_BUFFERS(test_false);
248 start_current_locals = save_start_current_locals;
249 }
250
251 /* Check a boolean binary form CODE (EXP0, EXP1),
252 where CODE is one of EQ_EXPR, BIT_AND_EXPR, or BIT_IOR_EXPR.
253 BEFORE, WHEN_FALSE, and WHEN_TRUE are as in check_bool_init. */
254
255 static void
256 check_bool2_init (code, exp0, exp1, before, when_false, when_true)
257 enum tree_code code; tree exp0, exp1;
258 words before, when_false, when_true;
259 {
260 word buf[2*4];
261 words tmp = num_current_words <= 2 ? buf
262 : ALLOC_WORDS (4 * num_current_words);
263 words when_false_0 = tmp;
264 words when_false_1 = tmp+num_current_words;
265 words when_true_0 = tmp+2*num_current_words;
266 words when_true_1 = tmp+3*num_current_words;
267 check_bool_init (exp0, before, when_false_0, when_true_0);
268 INTERSECT (before, when_false_0, when_true_0);
269 check_bool_init (exp1, before, when_false_1, when_true_1);
270
271 INTERSECT (before, when_false_1, when_true_1);
272
273 if (code == EQ_EXPR)
274 {
275 /* Now set:
276 * when_true = (when_false_1 INTERSECTION when_true_1)
277 * UNION (when_true_0 INTERSECTION when_false_1)
278 * UNION (when_false_0 INTERSECTION when_true_1);
279 * using when_false and before as temporary working areas. */
280 INTERSECT (when_true, when_true_0, when_false_1);
281 INTERSECT (when_false, when_true_0, when_false_1);
282 UNION (when_true, when_true, when_false);
283 UNION (when_true, when_true, before);
284
285 /* Now set:
286 * when_false = (when_false_1 INTERSECTION when_true_1)
287 * UNION (when_true_0 INTERSECTION when_true_1)
288 * UNION (when_false_0 INTERSECTION when_false_1);
289 * using before as a temporary working area. */
290 INTERSECT (when_false, when_true_0, when_true_1);
291 UNION (when_false, when_false, before);
292 INTERSECT (before, when_false_0, when_false_1);
293 UNION (when_false, when_false, before);
294 }
295 else if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
296 {
297 UNION (when_true, when_true_0, when_true_1);
298 INTERSECT (when_false, when_false_0, when_false_1);
299 UNION (when_false, when_false, before);
300 }
301 else /* if (code == BIT_IOR_EXPR || code == TRUTH_OR_EXPR) */
302 {
303 UNION (when_false, when_false_0, when_false_1);
304 INTERSECT (when_true, when_true_0, when_true_1);
305 UNION (when_true, when_true, before);
306 }
307
308 if (tmp != buf)
309 FREE_WORDS (tmp);
310 }
311
312 /* Check a boolean expression EXP for definite [un]assignment.
313 BEFORE is the set of variables definitely [un]assigned before the
314 conditional. (This bitstring may be modified arbitrarily in this function.)
315 On output, WHEN_FALSE is the set of variables [un]definitely assigned after
316 the conditional when the conditional is false.
317 On output, WHEN_TRUE is the set of variables definitely [un]assigned after
318 the conditional when the conditional is true.
319 (WHEN_FALSE and WHEN_TRUE are overwritten with initial values ignored.)
320 (None of BEFORE, WHEN_FALSE, or WHEN_TRUE can overlap, as they may
321 be used as temporary working areas. */
322
323 static void
324 check_bool_init (exp, before, when_false, when_true)
325 tree exp;
326 words before, when_false, when_true;
327 {
328 switch (TREE_CODE (exp))
329 {
330 case COND_EXPR:
331 check_cond_init (TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
332 TREE_OPERAND (exp, 2),
333 before, when_false, when_true);
334 return;
335
336 case TRUTH_ANDIF_EXPR:
337 check_cond_init (TREE_OPERAND (exp, 0),
338 TREE_OPERAND (exp, 1), boolean_false_node,
339 before, when_false, when_true);
340 return;
341 case TRUTH_ORIF_EXPR:
342 check_cond_init (TREE_OPERAND (exp, 0),
343 boolean_true_node, TREE_OPERAND (exp, 1),
344 before, when_false, when_true);
345 return;
346 case TRUTH_NOT_EXPR:
347 check_bool_init (TREE_OPERAND (exp, 0), before, when_true, when_false);
348 return;
349 case MODIFY_EXPR:
350 {
351 tree tmp = TREE_OPERAND (exp, 0);
352 if ((tmp = get_variable_decl (tmp)) != NULL_TREE)
353 {
354 int index;
355 check_bool_init (TREE_OPERAND (exp, 1), before,
356 when_false, when_true);
357 check_final_reassigned (tmp, before);
358 index = DECL_BIT_INDEX (tmp);
359 if (index >= 0)
360 {
361 SET_ASSIGNED (when_false, index);
362 SET_ASSIGNED (when_true, index);
363 CLEAR_UNASSIGNED (when_false, index);
364 CLEAR_UNASSIGNED (when_true, index);
365 }
366 break;
367 }
368 }
369 goto do_default;
370
371 case BIT_AND_EXPR:
372 case BIT_IOR_EXPR:
373 case TRUTH_AND_EXPR:
374 case TRUTH_OR_EXPR:
375 case EQ_EXPR:
376 check_bool2_init (TREE_CODE (exp),
377 TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
378 before, when_false, when_true);
379 return;
380
381 case TRUTH_XOR_EXPR:
382 case BIT_XOR_EXPR:
383 case NE_EXPR:
384 /* Just like EQ_EXPR, but switch when_true and when_false. */
385 check_bool2_init (EQ_EXPR, TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
386 before, when_true, when_false);
387
388 return;
389
390 case INTEGER_CST:
391 if (integer_zerop (exp))
392 {
393 SET_ALL (when_true);
394 COPY (when_false, before);
395 }
396 else
397 {
398 SET_ALL (when_false);
399 COPY (when_true, before);
400 }
401 break;
402 default:
403 do_default:
404 check_init (exp, before);
405 COPY (when_false, before);
406 COPY (when_true, before);
407 }
408 }
409
410 /* Used to keep track of control flow branches. */
411
412 struct alternatives
413 {
414 struct alternatives *outer;
415
416 /* The value of num_current_locals at the start of this compound. */
417 int num_locals;
418
419 /* The value of the "before" set at the start of the control stucture.
420 Used for SWITCH_EXPR but not set for LABELED_BLOCK_EXPR. */
421 words saved;
422
423 int save_start_current_locals;
424
425 /* If num_current_words==1, combined==&one_word, for efficiency. */
426 word one_word;
427
428 /* The intersection of the "after" sets from previous branches. */
429 words combined;
430
431 tree block;
432 };
433
434 struct alternatives * alternatives = NULL;
435
436 /* Begin handling a control flow branch.
437 BEFORE is the state of [un]assigned variables on entry.
438 CURRENT is a struct alt to manage the branch alternatives. */
439
440 #define BEGIN_ALTERNATIVES(before, current) \
441 { \
442 current.saved = NULL; \
443 current.num_locals = num_current_locals; \
444 current.combined = num_current_words <= 1 ? &current.one_word \
445 : ALLOC_WORDS (num_current_words); \
446 SET_ALL (current.combined); \
447 current.outer = alternatives; \
448 alternatives = &current; \
449 current.save_start_current_locals = start_current_locals; \
450 start_current_locals = num_current_locals; \
451 }
452
453 /* We have finished with one branch of branching control flow.
454 Store the [un]assigned state, merging (intersecting) it with the state
455 of previous alternative branches. */
456
457 static void
458 done_alternative (after, current)
459 words after;
460 struct alternatives *current;
461 {
462 INTERSECTN (current->combined, current->combined, after,
463 WORDS_NEEDED (2 * current->num_locals));
464 }
465
466 /* Used when we done with a control flow branch and are all merged again.
467 * AFTER is the merged state of [un]assigned variables,
468 CURRENT is a struct alt that was passed to BEGIN_ALTERNATIVES. */
469
470 #define END_ALTERNATIVES(after, current) \
471 { \
472 alternatives = current.outer; \
473 COPY (after, current.combined); \
474 if (current.combined != &current.one_word) \
475 FREE_WORDS (current.combined); \
476 start_current_locals = current.save_start_current_locals; \
477 }
478
479 /* Check for (un)initialized local variables in EXP. */
480
481 static void
482 check_init (exp, before)
483 tree exp;
484 words before;
485 {
486 tree tmp;
487 again:
488 switch (TREE_CODE (exp))
489 {
490 case VAR_DECL:
491 case PARM_DECL:
492 if (! FIELD_STATIC (exp) && DECL_NAME (exp) != NULL_TREE
493 && DECL_NAME (exp) != this_identifier_node)
494 {
495 int index = DECL_BIT_INDEX (exp);
496 /* We don't want to report and mark as non initialized class
497 initialization flags. */
498 if (! LOCAL_CLASS_INITIALIZATION_FLAG_P (exp)
499 && index >= 0 && ! ASSIGNED_P (before, index))
500 {
501 parse_error_context
502 (wfl, "Variable `%s' may not have been initialized",
503 IDENTIFIER_POINTER (DECL_NAME (exp)));
504 /* Suppress further errors. */
505 DECL_BIT_INDEX (exp) = -2;
506 }
507 }
508 break;
509
510 case COMPONENT_REF:
511 check_init (TREE_OPERAND (exp, 0), before);
512 if ((tmp = get_variable_decl (exp)) != NULL_TREE)
513 {
514 int index = DECL_BIT_INDEX (tmp);
515 if (index >= 0 && ! ASSIGNED_P (before, index))
516 {
517 parse_error_context
518 (wfl, "variable '%s' may not have been initialized",
519 IDENTIFIER_POINTER (DECL_NAME (tmp)));
520 /* Suppress further errors. */
521 DECL_BIT_INDEX (tmp) = -2;
522 }
523 }
524 break;
525
526 case MODIFY_EXPR:
527 tmp = TREE_OPERAND (exp, 0);
528 /* We're interested in variable declaration and parameter
529 declaration when they're declared with the `final' modifier. */
530 if ((tmp = get_variable_decl (tmp)) != NULL_TREE)
531 {
532 int index;
533 check_init (TREE_OPERAND (exp, 1), before);
534 check_final_reassigned (tmp, before);
535 index = DECL_BIT_INDEX (tmp);
536 if (index >= 0)
537 {
538 SET_ASSIGNED (before, index);
539 CLEAR_UNASSIGNED (before, index);
540 }
541 /* Minor optimization. See comment for start_current_locals.
542 If we're optimizing for class initialization, we keep
543 this information to check whether the variable is
544 definitely assigned when once we checked the whole
545 function. */
546 if (! STATIC_CLASS_INIT_OPT_P () /* FIXME */
547 && index >= start_current_locals
548 && index == num_current_locals - 1)
549 {
550 num_current_locals--;
551 DECL_BIT_INDEX (tmp) = -1;
552 }
553 break;
554 }
555 else if (TREE_CODE (tmp = TREE_OPERAND (exp, 0)) == COMPONENT_REF)
556 {
557 tree decl;
558 check_init (tmp, before);
559 check_init (TREE_OPERAND (exp, 1), before);
560 decl = TREE_OPERAND (tmp, 1);
561 if (DECL_FINAL (decl))
562 final_assign_error (DECL_NAME (decl));
563 break;
564 }
565 else if (TREE_CODE (tmp) == COMPONENT_REF && IS_ARRAY_LENGTH_ACCESS (tmp))
566 {
567 /* We can't emit a more specific message here, because when
568 compiling to bytecodes we don't get here. */
569 final_assign_error (length_identifier_node);
570 }
571 else
572 goto binop;
573 case BLOCK:
574 if (BLOCK_EXPR_BODY (exp))
575 {
576 tree decl = BLOCK_EXPR_DECLS (exp);
577 int words_needed;
578 word* tmp;
579 int i;
580 int save_start_current_locals = start_current_locals;
581 int save_num_current_words = num_current_words;
582 start_current_locals = num_current_locals;
583 for (; decl != NULL_TREE; decl = TREE_CHAIN (decl))
584 {
585 DECL_BIT_INDEX (decl) = num_current_locals++;
586 }
587 words_needed = WORDS_NEEDED (2 * num_current_locals);
588 if (words_needed > num_current_words)
589 {
590 tmp = ALLOC_WORDS (words_needed);
591 COPY (tmp, before);
592 num_current_words = words_needed;
593 }
594 else
595 tmp = before;
596 for (i = start_current_locals; i < num_current_locals; i++)
597 {
598 CLEAR_ASSIGNED (tmp, i);
599 SET_UNASSIGNED (tmp, i);
600 }
601 check_init (BLOCK_EXPR_BODY (exp), tmp);
602
603 /* Re-set DECL_BIT_INDEX since it is also DECL_POINTER_ALIAS_SET. */
604 for (decl = BLOCK_EXPR_DECLS (exp);
605 decl != NULL_TREE; decl = TREE_CHAIN (decl))
606 {
607 if (LOCAL_CLASS_INITIALIZATION_FLAG_P (decl))
608 {
609 int index = DECL_BIT_INDEX (decl);
610 tree fndecl = DECL_CONTEXT (decl);
611 if (fndecl && METHOD_STATIC (fndecl)
612 && (DECL_INITIAL (decl) == boolean_true_node
613 || (index >= 0 && ASSIGNED_P (tmp, index))))
614 *(htab_find_slot
615 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl),
616 DECL_FUNCTION_INIT_TEST_CLASS (decl), INSERT)) =
617 DECL_FUNCTION_INIT_TEST_CLASS (decl);
618 }
619 DECL_BIT_INDEX (decl) = -1;
620 }
621
622 num_current_locals = start_current_locals;
623 start_current_locals = save_start_current_locals;
624 if (tmp != before)
625 {
626 num_current_words = save_num_current_words;
627 COPY (before, tmp);
628 FREE_WORDS (tmp);
629 }
630 }
631 break;
632 case LOOP_EXPR:
633 {
634 /* The JLS 2nd edition discusses a complication determining
635 definite unassignment of loop statements. They define a
636 "hypothetical" analysis model. We do something much
637 simpler: We just disallow assignments inside loops to final
638 variables declared outside the loop. This means we may
639 disallow some contrived assignments that the JLS, but I
640 can't see how anything except a very contrived testcase (a
641 do-while whose condition is false?) would care. */
642
643 struct alternatives alt;
644 int save_loop_current_locals = loop_current_locals;
645 int save_start_current_locals = start_current_locals;
646 loop_current_locals = num_current_locals;
647 start_current_locals = num_current_locals;
648 BEGIN_ALTERNATIVES (before, alt);
649 alt.block = exp;
650 check_init (TREE_OPERAND (exp, 0), before);
651 END_ALTERNATIVES (before, alt);
652 loop_current_locals = save_loop_current_locals;
653 start_current_locals = save_start_current_locals;
654 return;
655 }
656 case EXIT_EXPR:
657 {
658 struct alternatives *alt = alternatives;
659 DECLARE_BUFFERS(when_true, 2);
660 words when_false = when_true + num_current_words;
661 #ifdef ENABLE_JC1_CHECKING
662 if (TREE_CODE (alt->block) != LOOP_EXPR)
663 abort ();
664 #endif
665 check_bool_init (TREE_OPERAND (exp, 0), before, when_false, when_true);
666 done_alternative (when_true, alt);
667 COPY (before, when_false);
668 RELEASE_BUFFERS(when_true);
669 return;
670 }
671 case LABELED_BLOCK_EXPR:
672 {
673 struct alternatives alt;
674 BEGIN_ALTERNATIVES (before, alt);
675 alt.block = exp;
676 if (LABELED_BLOCK_BODY (exp))
677 check_init (LABELED_BLOCK_BODY (exp), before);
678 done_alternative (before, &alt);
679 END_ALTERNATIVES (before, alt);
680 return;
681 }
682 case EXIT_BLOCK_EXPR:
683 {
684 tree block = TREE_OPERAND (exp, 0);
685 struct alternatives *alt = alternatives;
686 while (alt->block != block)
687 alt = alt->outer;
688 done_alternative (before, alt);
689 SET_ALL (before);
690 return;
691 }
692 case SWITCH_EXPR:
693 {
694 struct alternatives alt;
695 word buf[2];
696 check_init (TREE_OPERAND (exp, 0), before);
697 BEGIN_ALTERNATIVES (before, alt);
698 alt.saved = ALLOC_BUFFER(buf, num_current_words);
699 COPY (alt.saved, before);
700 alt.block = exp;
701 check_init (TREE_OPERAND (exp, 1), before);
702 done_alternative (before, &alt);
703 if (! SWITCH_HAS_DEFAULT (exp))
704 done_alternative (alt.saved, &alt);
705 FREE_BUFFER(alt.saved, buf);
706 END_ALTERNATIVES (before, alt);
707 return;
708 }
709 case CASE_EXPR:
710 case DEFAULT_EXPR:
711 {
712 int i;
713 struct alternatives *alt = alternatives;
714 while (TREE_CODE (alt->block) != SWITCH_EXPR)
715 alt = alt->outer;
716 COPYN (before, alt->saved, WORDS_NEEDED (2 * alt->num_locals));
717 for (i = alt->num_locals; i < num_current_locals; i++)
718 CLEAR_ASSIGNED (before, i);
719 break;
720 }
721
722 case TRY_EXPR:
723 {
724 tree try_clause = TREE_OPERAND (exp, 0);
725 tree clause = TREE_OPERAND (exp, 1);
726 word buf[2*2];
727 words tmp = (num_current_words <= 2 ? buf
728 : ALLOC_WORDS (2 * num_current_words));
729 words save = tmp + num_current_words;
730 struct alternatives alt;
731 BEGIN_ALTERNATIVES (before, alt);
732 COPY (save, before);
733 COPY (tmp, save);
734 check_init (try_clause, tmp);
735 done_alternative (tmp, &alt);
736 for ( ; clause != NULL_TREE; clause = TREE_CHAIN (clause))
737 {
738 tree catch_clause = TREE_OPERAND (clause, 0);
739 COPY (tmp, save);
740 check_init (catch_clause, tmp);
741 done_alternative (tmp, &alt);
742 }
743 if (tmp != buf)
744 {
745 FREE_WORDS (tmp);
746 }
747 END_ALTERNATIVES (before, alt);
748 }
749 return;
750
751 case TRY_FINALLY_EXPR:
752 {
753 DECLARE_BUFFERS(tmp, 1);
754 COPY (tmp, before);
755 check_init (TREE_OPERAND (exp, 0), before);
756 check_init (TREE_OPERAND (exp, 1), tmp);
757 UNION (before, before, tmp);
758 RELEASE_BUFFERS(tmp);
759 }
760 return;
761
762 case RETURN_EXPR:
763 case THROW_EXPR:
764 if (TREE_OPERAND (exp, 0))
765 check_init (TREE_OPERAND (exp, 0), before);
766 goto never_continues;
767
768 case ERROR_MARK:
769 never_continues:
770 SET_ALL (before);
771 return;
772
773 case COND_EXPR:
774 case TRUTH_ANDIF_EXPR:
775 case TRUTH_ORIF_EXPR:
776 {
777 DECLARE_BUFFERS(when_true, 2);
778 words when_false = when_true + num_current_words;
779 check_bool_init (exp, before, when_false, when_true);
780 INTERSECT (before, when_false, when_true);
781 RELEASE_BUFFERS(when_true);
782 }
783 break;
784
785 case NOP_EXPR:
786 if (exp == empty_stmt_node)
787 break;
788 /* ... else fall through ... */
789 case UNARY_PLUS_EXPR:
790 case NEGATE_EXPR:
791 case TRUTH_AND_EXPR:
792 case TRUTH_OR_EXPR:
793 case TRUTH_XOR_EXPR:
794 case TRUTH_NOT_EXPR:
795 case BIT_NOT_EXPR:
796 case CONVERT_EXPR:
797 case BIT_FIELD_REF:
798 case FLOAT_EXPR:
799 case FIX_TRUNC_EXPR:
800 case INDIRECT_REF:
801 case ADDR_EXPR:
802 case NON_LVALUE_EXPR:
803 case INSTANCEOF_EXPR:
804 case FIX_CEIL_EXPR:
805 case FIX_FLOOR_EXPR:
806 case FIX_ROUND_EXPR:
807 case ABS_EXPR:
808 case FFS_EXPR:
809 /* Avoid needless recursion. */
810 exp = TREE_OPERAND (exp, 0);
811 goto again;
812
813 case PREDECREMENT_EXPR:
814 case PREINCREMENT_EXPR:
815 case POSTDECREMENT_EXPR:
816 case POSTINCREMENT_EXPR:
817 tmp = get_variable_decl (TREE_OPERAND (exp, 0));
818 if (tmp != NULL_TREE && DECL_FINAL (tmp))
819 final_assign_error (DECL_NAME (tmp));
820
821 /* Avoid needless recursion. */
822 exp = TREE_OPERAND (exp, 0);
823 goto again;
824
825 case SAVE_EXPR:
826 if (IS_INIT_CHECKED (exp))
827 return;
828 IS_INIT_CHECKED (exp) = 1;
829 exp = TREE_OPERAND (exp, 0);
830 goto again;
831
832 case COMPOUND_EXPR:
833 case PLUS_EXPR:
834 case MINUS_EXPR:
835 case MULT_EXPR:
836 case TRUNC_DIV_EXPR:
837 case TRUNC_MOD_EXPR:
838 case RDIV_EXPR:
839 case LSHIFT_EXPR:
840 case RSHIFT_EXPR:
841 case URSHIFT_EXPR:
842 case BIT_AND_EXPR:
843 case BIT_XOR_EXPR:
844 case BIT_IOR_EXPR:
845 case EQ_EXPR:
846 case NE_EXPR:
847 case GT_EXPR:
848 case GE_EXPR:
849 case LT_EXPR:
850 case LE_EXPR:
851 case MAX_EXPR:
852 case MIN_EXPR:
853 case ARRAY_REF:
854 case LROTATE_EXPR:
855 case RROTATE_EXPR:
856 case CEIL_DIV_EXPR:
857 case FLOOR_DIV_EXPR:
858 case ROUND_DIV_EXPR:
859 case CEIL_MOD_EXPR:
860 case FLOOR_MOD_EXPR:
861 case ROUND_MOD_EXPR:
862 case EXACT_DIV_EXPR:
863 binop:
864 check_init (TREE_OPERAND (exp, 0), before);
865 /* Avoid needless recursion, especially for COMPOUND_EXPR. */
866 exp = TREE_OPERAND (exp, 1);
867 goto again;
868
869 case RESULT_DECL:
870 case FUNCTION_DECL:
871 case INTEGER_CST:
872 case REAL_CST:
873 case STRING_CST:
874 case JAVA_EXC_OBJ_EXPR:
875 break;
876
877 case NEW_CLASS_EXPR:
878 case CALL_EXPR:
879 {
880 tree func = TREE_OPERAND (exp, 0);
881 tree x = TREE_OPERAND (exp, 1);
882 if (TREE_CODE (func) == ADDR_EXPR)
883 func = TREE_OPERAND (func, 0);
884 check_init (func, before);
885
886 for ( ; x != NULL_TREE; x = TREE_CHAIN (x))
887 check_init (TREE_VALUE (x), before);
888 if (func == throw_node)
889 goto never_continues;
890 }
891 break;
892
893 case NEW_ARRAY_INIT:
894 {
895 tree x = CONSTRUCTOR_ELTS (TREE_OPERAND (exp, 0));
896 for ( ; x != NULL_TREE; x = TREE_CHAIN (x))
897 check_init (TREE_VALUE (x), before);
898 }
899 break;
900
901 case EXPR_WITH_FILE_LOCATION:
902 {
903 const char *saved_input_filename = input_filename;
904 tree saved_wfl = wfl;
905 tree body = EXPR_WFL_NODE (exp);
906 int saved_lineno = lineno;
907 if (body == empty_stmt_node)
908 break;
909 wfl = exp;
910 input_filename = EXPR_WFL_FILENAME (exp);
911 lineno = EXPR_WFL_LINENO (exp);
912 check_init (body, before);
913 input_filename = saved_input_filename;
914 lineno = saved_lineno;
915 wfl = saved_wfl;
916 }
917 break;
918
919 default:
920 internal_error
921 ("internal error in check-init: tree code not implemented: %s",
922 tree_code_name [(int) TREE_CODE (exp)]);
923 }
924 }
925
926 void
927 check_for_initialization (body, mdecl)
928 tree body, mdecl;
929 {
930 tree decl;
931 word buf[2];
932 words before = buf;
933 tree owner = DECL_CONTEXT (mdecl);
934 int is_static_method = METHOD_STATIC (mdecl);
935 /* We don't need to check final fields of <init> it it calls this(). */
936 int is_finit_method = DECL_FINIT_P (mdecl) || DECL_INSTINIT_P (mdecl);
937 int is_init_method
938 = (is_finit_method || DECL_CLINIT_P (mdecl)
939 || (DECL_INIT_P (mdecl) && ! DECL_INIT_CALLS_THIS (mdecl)));
940
941 start_current_locals = num_current_locals = 0;
942 num_current_words = 2;
943
944 if (is_init_method)
945 {
946 int words_needed, i;
947 for (decl = TYPE_FIELDS (owner);
948 decl != NULL_TREE; decl = TREE_CHAIN (decl))
949 {
950 if (DECL_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
951 {
952 if (DECL_FIELD_FINAL_IUD (decl))
953 DECL_BIT_INDEX (decl) = -1;
954 else
955 DECL_BIT_INDEX (decl) = num_current_locals++;
956 }
957 }
958 words_needed = WORDS_NEEDED (2 * num_current_locals);
959 if (words_needed > 2)
960 {
961 num_current_words = words_needed;
962 before = ALLOC_WORDS(words_needed);
963 }
964 i = 0;
965 for (decl = TYPE_FIELDS (owner);
966 decl != NULL_TREE; decl = TREE_CHAIN (decl))
967 {
968 if (FIELD_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
969 {
970 if (! DECL_FIELD_FINAL_IUD (decl))
971 {
972 CLEAR_ASSIGNED (before, i);
973 SET_UNASSIGNED (before, i);
974 i++;
975 }
976 }
977 }
978
979 }
980
981 check_init (body, before);
982
983 if (is_init_method)
984 {
985 for (decl = TYPE_FIELDS (owner);
986 decl != NULL_TREE; decl = TREE_CHAIN (decl))
987 {
988 if (FIELD_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
989 {
990 int index = DECL_BIT_INDEX (decl);
991 if (index >= 0 && ! ASSIGNED_P (before, index))
992 {
993 if (! is_finit_method)
994 error_with_decl (decl, "final field '%s' may not have been initialized");
995 }
996 else if (is_finit_method)
997 DECL_FIELD_FINAL_IUD (decl) = 1;
998
999 /* Re-set to initial state, since we later may use the
1000 same bit for DECL_POINTER_ALIAS_SET. */
1001 DECL_BIT_INDEX (decl) = -1;
1002 }
1003 }
1004 }
1005
1006 start_current_locals = num_current_locals = 0;
1007 }
This page took 0.084383 seconds and 5 git commands to generate.