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