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)
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. */
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. */
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
71 static int start_current_locals = 0;
73 static int num_current_words;
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) \
85 while (--n >= 0) DST[n] = SRC1[n] & SRC2[n]; \
88 #define UNION(DST, SRC1, SRC2) \
89 UNIONN (DST, SRC1, SRC2, num_current_words)
91 #define UNIONN(DST, SRC1, SRC2, N) \
93 while (--n >= 0) DST[n] = SRC1[n] | SRC2[n]; \
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));
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) (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. */
161 get_variable_decl (exp)
164 if (TREE_CODE (exp) == VAR_DECL)
166 if (! TREE_STATIC (exp) || FIELD_FINAL (exp))
169 /* We only care about final parameters. */
170 else if (TREE_CODE (exp) == PARM_DECL)
172 if (DECL_FINAL (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)))
195 final_assign_error (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));
204 check_final_reassigned (decl, 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. */
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. */
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;
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);
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);
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);
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. */
320 check_bool_init (exp, before, when_false, when_true)
322 words before, when_false, when_true;
324 switch (TREE_CODE (exp))
327 check_cond_init (TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
328 TREE_OPERAND (exp, 2),
329 before, when_false, when_true);
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);
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);
343 check_bool_init (TREE_OPERAND (exp, 0), before, when_true, when_false);
347 tree tmp = TREE_OPERAND (exp, 0);
348 if ((tmp = get_variable_decl (tmp)) != NULL_TREE)
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);
357 SET_ASSIGNED (when_false, index);
358 SET_ASSIGNED (when_true, index);
359 CLEAR_UNASSIGNED (when_false, index);
360 CLEAR_UNASSIGNED (when_true, index);
372 check_bool2_init (TREE_CODE (exp),
373 TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
374 before, when_false, when_true);
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);
387 if (integer_zerop (exp))
390 COPY (when_false, before);
394 SET_ALL (when_false);
395 COPY (when_true, before);
400 check_init (exp, before);
401 COPY (when_false, before);
402 COPY (when_true, before);
406 /* Used to keep track of control flow branches. */
410 struct alternatives *outer;
412 /* The value of num_current_locals at the start of this compound. */
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. */
419 int save_start_current_locals;
421 /* If num_current_words==1, combined==&one_word, for efficiency. */
424 /* The intersection of the "after" sets from previous branches. */
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 ? ¤t.one_word \
441 : ALLOC_WORDS (num_current_words); \
442 SET_ALL (current.combined); \
443 current.outer = alternatives; \
444 alternatives = ¤t; \
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. */
454 done_alternative (after, current)
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 != ¤t.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. */
478 check_init (exp, before)
484 switch (TREE_CODE (exp))
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))
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;
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))
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;
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)
529 check_init (TREE_OPERAND (exp, 1), before);
530 check_final_reassigned (tmp, before);
531 index = DECL_BIT_INDEX (tmp);
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
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;
551 else if (TREE_CODE (tmp = TREE_OPERAND (exp, 0)) == COMPONENT_REF)
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));
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);
570 if (BLOCK_EXPR_BODY (exp))
572 tree decl = BLOCK_EXPR_DECLS (exp);
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);
588 num_current_words = words_needed;
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))))
611 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl),
612 DECL_FUNCTION_INIT_TEST_CLASS (decl), INSERT)) =
613 DECL_FUNCTION_INIT_TEST_CLASS (decl);
615 DECL_BIT_INDEX (decl) = -1;
618 num_current_locals = start_current_locals;
619 start_current_locals = save_start_current_locals;
622 num_current_words = save_num_current_words;
630 /* The JLS 2nd edition discusses a complication determining
631 definite unassignment of loop statements. They define a
632 "hypothetical" analysis model. We do something much
633 simpler: We just disallow assignments inside loops to final
634 variables declared outside the loop. This means we may
635 disallow some contrived assignments that the JLS, but I
636 can't see how anything except a very contrived testcase (a
637 do-while whose condition is false?) would care. */
639 struct alternatives alt;
640 int save_loop_current_locals = loop_current_locals;
641 int save_start_current_locals = start_current_locals;
642 loop_current_locals = num_current_locals;
643 start_current_locals = num_current_locals;
644 BEGIN_ALTERNATIVES (before, alt);
646 check_init (TREE_OPERAND (exp, 0), before);
647 END_ALTERNATIVES (before, alt);
648 loop_current_locals = save_loop_current_locals;
649 start_current_locals = save_start_current_locals;
654 struct alternatives *alt = alternatives;
655 DECLARE_BUFFERS(when_true, 2);
656 words when_false = when_true + num_current_words;
657 #ifdef ENABLE_JC1_CHECKING
658 if (TREE_CODE (alt->block) != LOOP_EXPR)
661 check_bool_init (TREE_OPERAND (exp, 0), before, when_false, when_true);
662 done_alternative (when_true, alt);
663 COPY (before, when_false);
664 RELEASE_BUFFERS(when_true);
667 case LABELED_BLOCK_EXPR:
669 struct alternatives alt;
670 BEGIN_ALTERNATIVES (before, alt);
672 if (LABELED_BLOCK_BODY (exp))
673 check_init (LABELED_BLOCK_BODY (exp), before);
674 done_alternative (before, &alt);
675 END_ALTERNATIVES (before, alt);
678 case EXIT_BLOCK_EXPR:
680 tree block = TREE_OPERAND (exp, 0);
681 struct alternatives *alt = alternatives;
682 while (alt->block != block)
684 done_alternative (before, alt);
690 struct alternatives alt;
692 check_init (TREE_OPERAND (exp, 0), before);
693 BEGIN_ALTERNATIVES (before, alt);
694 alt.saved = ALLOC_BUFFER(buf, num_current_words);
695 COPY (alt.saved, before);
697 check_init (TREE_OPERAND (exp, 1), before);
698 done_alternative (before, &alt);
699 if (! SWITCH_HAS_DEFAULT (exp))
700 done_alternative (alt.saved, &alt);
701 FREE_BUFFER(alt.saved, buf);
702 END_ALTERNATIVES (before, alt);
709 struct alternatives *alt = alternatives;
710 while (TREE_CODE (alt->block) != SWITCH_EXPR)
712 COPYN (before, alt->saved, WORDS_NEEDED (2 * alt->num_locals));
713 for (i = alt->num_locals; i < num_current_locals; i++)
714 CLEAR_ASSIGNED (before, i);
720 tree try_clause = TREE_OPERAND (exp, 0);
721 tree clause = TREE_OPERAND (exp, 1);
723 words tmp = (num_current_words <= 2 ? buf
724 : ALLOC_WORDS (2 * num_current_words));
725 words save = tmp + num_current_words;
726 struct alternatives alt;
727 BEGIN_ALTERNATIVES (before, alt);
730 check_init (try_clause, tmp);
731 done_alternative (tmp, &alt);
732 for ( ; clause != NULL_TREE; clause = TREE_CHAIN (clause))
734 tree catch_clause = TREE_OPERAND (clause, 0);
736 check_init (catch_clause, tmp);
737 done_alternative (tmp, &alt);
743 END_ALTERNATIVES (before, alt);
747 case TRY_FINALLY_EXPR:
749 DECLARE_BUFFERS(tmp, 1);
751 check_init (TREE_OPERAND (exp, 0), before);
752 check_init (TREE_OPERAND (exp, 1), tmp);
753 UNION (before, before, tmp);
754 RELEASE_BUFFERS(tmp);
760 if (TREE_OPERAND (exp, 0))
761 check_init (TREE_OPERAND (exp, 0), before);
762 goto never_continues;
770 case TRUTH_ANDIF_EXPR:
771 case TRUTH_ORIF_EXPR:
773 DECLARE_BUFFERS(when_true, 2);
774 words when_false = when_true + num_current_words;
775 check_bool_init (exp, before, when_false, when_true);
776 INTERSECT (before, when_false, when_true);
777 RELEASE_BUFFERS(when_true);
782 if (exp == empty_stmt_node)
784 /* ... else fall through ... */
785 case UNARY_PLUS_EXPR:
798 case NON_LVALUE_EXPR:
799 case INSTANCEOF_EXPR:
805 /* Avoid needless recursion. */
806 exp = TREE_OPERAND (exp, 0);
809 case PREDECREMENT_EXPR:
810 case PREINCREMENT_EXPR:
811 case POSTDECREMENT_EXPR:
812 case POSTINCREMENT_EXPR:
813 tmp = get_variable_decl (TREE_OPERAND (exp, 0));
814 if (tmp != NULL_TREE && DECL_FINAL (tmp))
815 final_assign_error (DECL_NAME (tmp));
817 /* Avoid needless recursion. */
818 exp = TREE_OPERAND (exp, 0);
822 if (IS_INIT_CHECKED (exp))
824 IS_INIT_CHECKED (exp) = 1;
825 exp = TREE_OPERAND (exp, 0);
860 check_init (TREE_OPERAND (exp, 0), before);
861 /* Avoid needless recursion, especially for COMPOUND_EXPR. */
862 exp = TREE_OPERAND (exp, 1);
870 case JAVA_EXC_OBJ_EXPR:
876 tree func = TREE_OPERAND (exp, 0);
877 tree x = TREE_OPERAND (exp, 1);
878 if (TREE_CODE (func) == ADDR_EXPR)
879 func = TREE_OPERAND (func, 0);
880 check_init (func, before);
882 for ( ; x != NULL_TREE; x = TREE_CHAIN (x))
883 check_init (TREE_VALUE (x), before);
884 if (func == throw_node)
885 goto never_continues;
891 tree x = CONSTRUCTOR_ELTS (TREE_OPERAND (exp, 0));
892 for ( ; x != NULL_TREE; x = TREE_CHAIN (x))
893 check_init (TREE_VALUE (x), before);
897 case EXPR_WITH_FILE_LOCATION:
899 const char *saved_input_filename = input_filename;
900 tree saved_wfl = wfl;
901 tree body = EXPR_WFL_NODE (exp);
902 int saved_lineno = lineno;
903 if (body == empty_stmt_node)
906 input_filename = EXPR_WFL_FILENAME (exp);
907 lineno = EXPR_WFL_LINENO (exp);
908 check_init (body, before);
909 input_filename = saved_input_filename;
910 lineno = saved_lineno;
917 ("internal error in check-init: tree code not implemented: %s",
918 tree_code_name [(int) TREE_CODE (exp)]);
923 check_for_initialization (body, mdecl)
929 tree owner = DECL_CONTEXT (mdecl);
930 int is_static_method = METHOD_STATIC (mdecl);
931 /* We don't need to check final fields of <init> it it calls this(). */
932 int is_finit_method = DECL_FINIT_P (mdecl) || DECL_INSTINIT_P (mdecl);
934 = (is_finit_method || DECL_CLINIT_P (mdecl)
935 || (DECL_INIT_P (mdecl) && ! DECL_INIT_CALLS_THIS (mdecl)));
937 start_current_locals = num_current_locals = 0;
938 num_current_words = 2;
943 for (decl = TYPE_FIELDS (owner);
944 decl != NULL_TREE; decl = TREE_CHAIN (decl))
946 if (DECL_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
948 if (DECL_FIELD_FINAL_IUD (decl))
949 DECL_BIT_INDEX (decl) = -1;
951 DECL_BIT_INDEX (decl) = num_current_locals++;
954 words_needed = WORDS_NEEDED (2 * num_current_locals);
955 if (words_needed > 2)
957 num_current_words = words_needed;
958 before = ALLOC_WORDS(words_needed);
961 for (decl = TYPE_FIELDS (owner);
962 decl != NULL_TREE; decl = TREE_CHAIN (decl))
964 if (FIELD_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
966 if (! DECL_FIELD_FINAL_IUD (decl))
968 CLEAR_ASSIGNED (before, i);
969 SET_UNASSIGNED (before, i);
977 check_init (body, before);
981 for (decl = TYPE_FIELDS (owner);
982 decl != NULL_TREE; decl = TREE_CHAIN (decl))
984 if (FIELD_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
986 int index = DECL_BIT_INDEX (decl);
987 if (index >= 0 && ! ASSIGNED_P (before, index))
989 if (! is_finit_method)
990 error_with_decl (decl, "final field '%s' may not have been initialized");
992 else if (is_finit_method)
993 DECL_FIELD_FINAL_IUD (decl) = 1;
995 /* Re-set to initial state, since we later may use the
996 same bit for DECL_POINTER_ALIAS_SET. */
997 DECL_BIT_INDEX (decl) = -1;
1002 start_current_locals = num_current_locals = 0;