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. */
27 #include "coretypes.h"
30 #include "flags.h" /* Needed for optimize. */
31 #include "java-tree.h"
32 #include "toplev.h" /* Needed for fatal. */
34 /* The basic idea is that we assign each local variable declaration
35 and each blank final field an index, and then we pass around
36 bitstrings, where the (2*i)'th bit is set if decl whose DECL_BIT_INDEX
37 is i is definitely assigned, and the the (2*i=1)'th bit is set if
38 decl whose DECL_BIT_INDEX is i is definitely unassigned */
40 /* One segment of a bitstring. */
41 typedef unsigned int word;
43 /* Pointer to a bitstring. */
46 /* Number of locals variables currently active. */
47 static int num_current_locals = 0;
49 /* The value of num_current_locals when we entered the closest
50 enclosing LOOP_EXPR. */
51 static int loop_current_locals;
53 /* The index of the first local variable in the current block.
55 The variables whose DECL_BIT_INDEX are in the range from
56 start_current_locals (inclusive) up to num_current_locals (exclusive)
57 are declared in the "current" block. If there is a loop or branch
58 form, we set start_current_locals to num_current_locals to indicate
59 there is no current block.
61 The point is that if a variable in the current block is set,
62 there are no other control paths that we have to worry about.
63 Hence, we can remove it from the set of variables we are
64 checking, making its bit index available for some other variable.
65 For simplicity, we only do that if the variable's bit index
66 is (num_current_locals-1); freeing up its bit index is then
67 just a simple matter of decrementing num_current_locals.
68 The reason this is worth doing is that it is simple, and
69 allows us to use short (usually one-word) bit-strings,
70 even for methods with thousands of local variables, as
71 long as most of them are initialized immediately after or in
73 static int start_current_locals = 0;
75 static int num_current_words;
79 #define COPYN(DST, SRC, NWORDS) memcpy (DST, SRC, NWORDS * sizeof(word))
80 #define COPY(DST, SRC) COPYN (DST, SRC, num_current_words)
82 #define SET_ALL(DST) memset (DST, ~0, num_current_words * sizeof(word))
83 #define CLEAR_ALL(DST) memset (DST, 0, num_current_words * sizeof(word))
85 #define INTERSECTN(DST, SRC1, SRC2, N) \
87 while (--n >= 0) DST[n] = SRC1[n] & SRC2[n]; \
90 #define UNION(DST, SRC1, SRC2) \
91 UNIONN (DST, SRC1, SRC2, num_current_words)
93 #define UNIONN(DST, SRC1, SRC2, N) \
95 while (--n >= 0) DST[n] = SRC1[n] | SRC2[n]; \
98 #define INTERSECT(DST, SRC1, SRC2) \
99 INTERSECTN (DST, SRC1, SRC2, num_current_words)
101 #define WORD_SIZE ((unsigned int)(sizeof(word) * BITS_PER_UNIT))
103 static void check_bool_init PARAMS ((tree, words, words, words));
104 static void check_init PARAMS ((tree, words));
105 static void check_cond_init PARAMS ((tree, tree, tree, words, words, words));
106 static void check_bool2_init PARAMS ((enum tree_code, tree, tree, words, words, words));
108 static void done_alternative PARAMS ((words, struct alternatives *));
109 static tree get_variable_decl PARAMS ((tree));
110 static void final_assign_error PARAMS ((tree));
111 static void check_final_reassigned PARAMS ((tree, words));
113 #define ALLOC_WORDS(NUM) (xmalloc ((NUM) * sizeof (word)))
114 #define FREE_WORDS(PTR) (free (PTR))
116 /* DECLARE_BUFFERS is used to allocate NUMBUFFER bit sets, each of
117 which is an array of length num_current_words number of words.
118 Declares a new local variable BUFFER to hold the result (or rather
119 a pointer to the first of the bit sets). In almost all cases
120 num_current_words will be 1 or at most 2, so we try to stack
121 allocate the arrays in that case, using a stack array
122 named BUFFER##_short. Each DECLARE_BUFFERS must be matched by
123 a corresponding RELEASE_BUFFERS to avoid memory leaks. */
125 #define DECLARE_BUFFERS(BUFFER, NUMBUFFERS) \
126 word BUFFER##_short[2 * NUMBUFFERS]; \
127 words BUFFER = ALLOC_BUFFER(BUFFER##_short, NUMBUFFERS * num_current_words)
129 #define RELEASE_BUFFERS(BUFFER) \
130 FREE_BUFFER(BUFFER, BUFFER##_short)
132 #define ALLOC_BUFFER(SHORTBUFFER, NUMWORDS) \
133 ((NUMWORDS) * sizeof(word) <= sizeof(SHORTBUFFER) ? SHORTBUFFER \
134 : ALLOC_WORDS(NUMWORDS))
136 #define FREE_BUFFER(BUFFER, SHORTBUFFER) \
137 if (BUFFER != SHORTBUFFER) FREE_WORDS(BUFFER)
139 #define SET_P(WORDS, BIT) \
140 (WORDS[(BIT) / WORD_SIZE] & (1 << ((BIT) % WORD_SIZE)))
142 #define CLEAR_BIT(WORDS, BIT) \
143 (WORDS[(BIT) / WORD_SIZE] &= ~ (1 << ((BIT) % WORD_SIZE)))
145 #define SET_BIT(WORDS, BIT) \
146 (WORDS[(BIT) / WORD_SIZE] |= (1 << ((BIT) % WORD_SIZE)))
148 #define WORDS_NEEDED(BITS) (((BITS)+(WORD_SIZE-1))/(WORD_SIZE))
150 #define ASSIGNED_P(WORDS, BIT) SET_P(WORDS, 2 * (BIT))
151 #define UNASSIGNED_P(WORDS, BIT) SET_P(WORDS, 2 * (BIT) + 1)
153 #define SET_ASSIGNED(WORDS, INDEX) SET_BIT (WORDS, 2 * (INDEX))
154 #define SET_UNASSIGNED(WORDS, INDEX) SET_BIT (WORDS, 2 * (INDEX) + 1)
156 #define CLEAR_ASSIGNED(WORDS, INDEX) CLEAR_BIT (WORDS, 2 * (INDEX))
157 #define CLEAR_UNASSIGNED(WORDS, INDEX) CLEAR_BIT (WORDS, 2 * (INDEX) + 1)
159 /* Get the "interesting" declaration from a MODIFY_EXPR or COMPONENT_REF.
160 Return the declaration or NULL_TREE if no interesting declaration. */
163 get_variable_decl (exp)
166 if (TREE_CODE (exp) == VAR_DECL)
168 if (! TREE_STATIC (exp) || FIELD_FINAL (exp))
171 /* We only care about final parameters. */
172 else if (TREE_CODE (exp) == PARM_DECL)
174 if (DECL_FINAL (exp))
177 /* See if exp is this.field. */
178 else if (TREE_CODE (exp) == COMPONENT_REF)
180 tree op0 = TREE_OPERAND (exp, 0);
181 tree op1 = TREE_OPERAND (exp, 1);
182 tree mdecl = current_function_decl;
183 if (TREE_CODE (op0) == INDIRECT_REF
184 && TREE_CODE (op1) == FIELD_DECL
185 && ! METHOD_STATIC (mdecl)
186 && FIELD_FINAL (op1))
188 op0 = TREE_OPERAND (op0, 0);
189 if (op0 == BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)))
197 final_assign_error (name)
200 static const char format[]
201 = "can't reassign a value to the final variable '%s'";
202 parse_error_context (wfl, format, IDENTIFIER_POINTER (name));
206 check_final_reassigned (decl, before)
210 int index = DECL_BIT_INDEX (decl);
211 /* A final local already assigned or a final parameter
212 assigned must be reported as errors */
213 if (DECL_FINAL (decl) && index != -2
214 && (index < loop_current_locals /* I.e. -1, or outside current loop. */
215 || ! UNASSIGNED_P (before, index)))
217 final_assign_error (DECL_NAME (decl));
221 /* Check a conditional form (TEST_EXP ? THEN_EXP : ELSE_EXP) for
222 definite [un]assignment.
223 BEFORE, WHEN_FALSE, and WHEN_TRUE are as in check_bool_init. */
226 check_cond_init (test_exp, then_exp, else_exp,
227 before, when_false, when_true)
228 tree test_exp, then_exp, else_exp;
229 words before, when_false, when_true;
231 int save_start_current_locals = start_current_locals;
232 DECLARE_BUFFERS(test_false, 6);
233 words test_true = test_false + num_current_words;
234 words then_false = test_true + num_current_words;
235 words then_true = then_false + num_current_words;
236 words else_false = then_true + num_current_words;
237 words else_true = else_false + num_current_words;
238 start_current_locals = num_current_locals;
240 check_bool_init (test_exp, before, test_false, test_true);
241 check_bool_init (then_exp, test_true, then_false, then_true);
242 check_bool_init (else_exp, test_false, else_false, else_true);
243 INTERSECT (when_false, then_false, else_false);
244 INTERSECT (when_true, then_true, else_true);
245 RELEASE_BUFFERS(test_false);
246 start_current_locals = save_start_current_locals;
249 /* Check a boolean binary form CODE (EXP0, EXP1),
250 where CODE is one of EQ_EXPR, BIT_AND_EXPR, or BIT_IOR_EXPR.
251 BEFORE, WHEN_FALSE, and WHEN_TRUE are as in check_bool_init. */
254 check_bool2_init (code, exp0, exp1, before, when_false, when_true)
255 enum tree_code code; tree exp0, exp1;
256 words before, when_false, when_true;
259 words tmp = num_current_words <= 2 ? buf
260 : ALLOC_WORDS (4 * num_current_words);
261 words when_false_0 = tmp;
262 words when_false_1 = tmp+num_current_words;
263 words when_true_0 = tmp+2*num_current_words;
264 words when_true_1 = tmp+3*num_current_words;
265 check_bool_init (exp0, before, when_false_0, when_true_0);
266 INTERSECT (before, when_false_0, when_true_0);
267 check_bool_init (exp1, before, when_false_1, when_true_1);
269 INTERSECT (before, when_false_1, when_true_1);
274 * when_true = (when_false_1 INTERSECTION when_true_1)
275 * UNION (when_true_0 INTERSECTION when_false_1)
276 * UNION (when_false_0 INTERSECTION when_true_1);
277 * using when_false and before as temporary working areas. */
278 INTERSECT (when_true, when_true_0, when_false_1);
279 INTERSECT (when_false, when_true_0, when_false_1);
280 UNION (when_true, when_true, when_false);
281 UNION (when_true, when_true, before);
284 * when_false = (when_false_1 INTERSECTION when_true_1)
285 * UNION (when_true_0 INTERSECTION when_true_1)
286 * UNION (when_false_0 INTERSECTION when_false_1);
287 * using before as a temporary working area. */
288 INTERSECT (when_false, when_true_0, when_true_1);
289 UNION (when_false, when_false, before);
290 INTERSECT (before, when_false_0, when_false_1);
291 UNION (when_false, when_false, before);
293 else if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
295 UNION (when_true, when_true_0, when_true_1);
296 INTERSECT (when_false, when_false_0, when_false_1);
297 UNION (when_false, when_false, before);
299 else /* if (code == BIT_IOR_EXPR || code == TRUTH_OR_EXPR) */
301 UNION (when_false, when_false_0, when_false_1);
302 INTERSECT (when_true, when_true_0, when_true_1);
303 UNION (when_true, when_true, before);
310 /* Check a boolean expression EXP for definite [un]assignment.
311 BEFORE is the set of variables definitely [un]assigned before the
312 conditional. (This bitstring may be modified arbitrarily in this function.)
313 On output, WHEN_FALSE is the set of variables [un]definitely assigned after
314 the conditional when the conditional is false.
315 On output, WHEN_TRUE is the set of variables definitely [un]assigned after
316 the conditional when the conditional is true.
317 (WHEN_FALSE and WHEN_TRUE are overwritten with initial values ignored.)
318 (None of BEFORE, WHEN_FALSE, or WHEN_TRUE can overlap, as they may
319 be used as temporary working areas. */
322 check_bool_init (exp, before, when_false, when_true)
324 words before, when_false, when_true;
326 switch (TREE_CODE (exp))
329 check_cond_init (TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
330 TREE_OPERAND (exp, 2),
331 before, when_false, when_true);
334 case TRUTH_ANDIF_EXPR:
335 check_cond_init (TREE_OPERAND (exp, 0),
336 TREE_OPERAND (exp, 1), boolean_false_node,
337 before, when_false, when_true);
339 case TRUTH_ORIF_EXPR:
340 check_cond_init (TREE_OPERAND (exp, 0),
341 boolean_true_node, TREE_OPERAND (exp, 1),
342 before, when_false, when_true);
345 check_bool_init (TREE_OPERAND (exp, 0), before, when_true, when_false);
349 tree tmp = TREE_OPERAND (exp, 0);
350 if ((tmp = get_variable_decl (tmp)) != NULL_TREE)
353 check_bool_init (TREE_OPERAND (exp, 1), before,
354 when_false, when_true);
355 check_final_reassigned (tmp, before);
356 index = DECL_BIT_INDEX (tmp);
359 SET_ASSIGNED (when_false, index);
360 SET_ASSIGNED (when_true, index);
361 CLEAR_UNASSIGNED (when_false, index);
362 CLEAR_UNASSIGNED (when_true, index);
374 check_bool2_init (TREE_CODE (exp),
375 TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
376 before, when_false, when_true);
382 /* Just like EQ_EXPR, but switch when_true and when_false. */
383 check_bool2_init (EQ_EXPR, TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
384 before, when_true, when_false);
389 if (integer_zerop (exp))
392 COPY (when_false, before);
396 SET_ALL (when_false);
397 COPY (when_true, before);
402 check_init (exp, before);
403 COPY (when_false, before);
404 COPY (when_true, before);
408 /* Used to keep track of control flow branches. */
412 struct alternatives *outer;
414 /* The value of num_current_locals at the start of this compound. */
417 /* The value of the "before" set at the start of the control stucture.
418 Used for SWITCH_EXPR but not set for LABELED_BLOCK_EXPR. */
421 int save_start_current_locals;
423 /* If num_current_words==1, combined==&one_word, for efficiency. */
426 /* The intersection of the "after" sets from previous branches. */
432 struct alternatives * alternatives = NULL;
434 /* Begin handling a control flow branch.
435 BEFORE is the state of [un]assigned variables on entry.
436 CURRENT is a struct alt to manage the branch alternatives. */
438 #define BEGIN_ALTERNATIVES(before, current) \
440 current.saved = NULL; \
441 current.num_locals = num_current_locals; \
442 current.combined = num_current_words <= 1 ? ¤t.one_word \
443 : ALLOC_WORDS (num_current_words); \
444 SET_ALL (current.combined); \
445 current.outer = alternatives; \
446 alternatives = ¤t; \
447 current.save_start_current_locals = start_current_locals; \
448 start_current_locals = num_current_locals; \
451 /* We have finished with one branch of branching control flow.
452 Store the [un]assigned state, merging (intersecting) it with the state
453 of previous alternative branches. */
456 done_alternative (after, current)
458 struct alternatives *current;
460 INTERSECTN (current->combined, current->combined, after,
461 WORDS_NEEDED (2 * current->num_locals));
464 /* Used when we done with a control flow branch and are all merged again.
465 * AFTER is the merged state of [un]assigned variables,
466 CURRENT is a struct alt that was passed to BEGIN_ALTERNATIVES. */
468 #define END_ALTERNATIVES(after, current) \
470 alternatives = current.outer; \
471 COPY (after, current.combined); \
472 if (current.combined != ¤t.one_word) \
473 FREE_WORDS (current.combined); \
474 start_current_locals = current.save_start_current_locals; \
477 /* Check for (un)initialized local variables in EXP. */
480 check_init (exp, before)
486 switch (TREE_CODE (exp))
490 if (! FIELD_STATIC (exp) && DECL_NAME (exp) != NULL_TREE
491 && DECL_NAME (exp) != this_identifier_node)
493 int index = DECL_BIT_INDEX (exp);
494 /* We don't want to report and mark as non initialized class
495 initialization flags. */
496 if (! LOCAL_CLASS_INITIALIZATION_FLAG_P (exp)
497 && index >= 0 && ! ASSIGNED_P (before, index))
500 (wfl, "Variable `%s' may not have been initialized",
501 IDENTIFIER_POINTER (DECL_NAME (exp)));
502 /* Suppress further errors. */
503 DECL_BIT_INDEX (exp) = -2;
509 check_init (TREE_OPERAND (exp, 0), before);
510 if ((tmp = get_variable_decl (exp)) != NULL_TREE)
512 int index = DECL_BIT_INDEX (tmp);
513 if (index >= 0 && ! ASSIGNED_P (before, index))
516 (wfl, "variable '%s' may not have been initialized",
517 IDENTIFIER_POINTER (DECL_NAME (tmp)));
518 /* Suppress further errors. */
519 DECL_BIT_INDEX (tmp) = -2;
525 tmp = TREE_OPERAND (exp, 0);
526 /* We're interested in variable declaration and parameter
527 declaration when they're declared with the `final' modifier. */
528 if ((tmp = get_variable_decl (tmp)) != NULL_TREE)
531 check_init (TREE_OPERAND (exp, 1), before);
532 check_final_reassigned (tmp, before);
533 index = DECL_BIT_INDEX (tmp);
536 SET_ASSIGNED (before, index);
537 CLEAR_UNASSIGNED (before, index);
539 /* Minor optimization. See comment for start_current_locals.
540 If we're optimizing for class initialization, we keep
541 this information to check whether the variable is
542 definitely assigned when once we checked the whole
544 if (! STATIC_CLASS_INIT_OPT_P () /* FIXME */
545 && index >= start_current_locals
546 && index == num_current_locals - 1)
548 num_current_locals--;
549 DECL_BIT_INDEX (tmp) = -1;
553 else if (TREE_CODE (tmp = TREE_OPERAND (exp, 0)) == COMPONENT_REF)
556 check_init (tmp, before);
557 check_init (TREE_OPERAND (exp, 1), before);
558 decl = TREE_OPERAND (tmp, 1);
559 if (DECL_FINAL (decl))
560 final_assign_error (DECL_NAME (decl));
563 else if (TREE_CODE (tmp) == COMPONENT_REF && IS_ARRAY_LENGTH_ACCESS (tmp))
565 /* We can't emit a more specific message here, because when
566 compiling to bytecodes we don't get here. */
567 final_assign_error (length_identifier_node);
572 if (BLOCK_EXPR_BODY (exp))
574 tree decl = BLOCK_EXPR_DECLS (exp);
578 int save_start_current_locals = start_current_locals;
579 int save_num_current_words = num_current_words;
580 start_current_locals = num_current_locals;
581 for (; decl != NULL_TREE; decl = TREE_CHAIN (decl))
583 DECL_BIT_INDEX (decl) = num_current_locals++;
585 words_needed = WORDS_NEEDED (2 * num_current_locals);
586 if (words_needed > num_current_words)
588 tmp = ALLOC_WORDS (words_needed);
590 num_current_words = words_needed;
594 for (i = start_current_locals; i < num_current_locals; i++)
596 CLEAR_ASSIGNED (tmp, i);
597 SET_UNASSIGNED (tmp, i);
599 check_init (BLOCK_EXPR_BODY (exp), tmp);
601 /* Re-set DECL_BIT_INDEX since it is also DECL_POINTER_ALIAS_SET. */
602 for (decl = BLOCK_EXPR_DECLS (exp);
603 decl != NULL_TREE; decl = TREE_CHAIN (decl))
605 if (LOCAL_CLASS_INITIALIZATION_FLAG_P (decl))
607 int index = DECL_BIT_INDEX (decl);
608 tree fndecl = DECL_CONTEXT (decl);
609 if (fndecl && METHOD_STATIC (fndecl)
610 && (DECL_INITIAL (decl) == boolean_true_node
611 || (index >= 0 && ASSIGNED_P (tmp, index))))
613 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl),
614 DECL_FUNCTION_INIT_TEST_CLASS (decl), INSERT)) =
615 DECL_FUNCTION_INIT_TEST_CLASS (decl);
617 DECL_BIT_INDEX (decl) = -1;
620 num_current_locals = start_current_locals;
621 start_current_locals = save_start_current_locals;
624 num_current_words = save_num_current_words;
632 /* The JLS 2nd edition discusses a complication determining
633 definite unassignment of loop statements. They define a
634 "hypothetical" analysis model. We do something much
635 simpler: We just disallow assignments inside loops to final
636 variables declared outside the loop. This means we may
637 disallow some contrived assignments that the JLS, but I
638 can't see how anything except a very contrived testcase (a
639 do-while whose condition is false?) would care. */
641 struct alternatives alt;
642 int save_loop_current_locals = loop_current_locals;
643 int save_start_current_locals = start_current_locals;
644 loop_current_locals = num_current_locals;
645 start_current_locals = num_current_locals;
646 BEGIN_ALTERNATIVES (before, alt);
648 check_init (TREE_OPERAND (exp, 0), before);
649 END_ALTERNATIVES (before, alt);
650 loop_current_locals = save_loop_current_locals;
651 start_current_locals = save_start_current_locals;
656 struct alternatives *alt = alternatives;
657 DECLARE_BUFFERS(when_true, 2);
658 words when_false = when_true + num_current_words;
659 #ifdef ENABLE_JC1_CHECKING
660 if (TREE_CODE (alt->block) != LOOP_EXPR)
663 check_bool_init (TREE_OPERAND (exp, 0), before, when_false, when_true);
664 done_alternative (when_true, alt);
665 COPY (before, when_false);
666 RELEASE_BUFFERS(when_true);
669 case LABELED_BLOCK_EXPR:
671 struct alternatives alt;
672 BEGIN_ALTERNATIVES (before, alt);
674 if (LABELED_BLOCK_BODY (exp))
675 check_init (LABELED_BLOCK_BODY (exp), before);
676 done_alternative (before, &alt);
677 END_ALTERNATIVES (before, alt);
680 case EXIT_BLOCK_EXPR:
682 tree block = TREE_OPERAND (exp, 0);
683 struct alternatives *alt = alternatives;
684 while (alt->block != block)
686 done_alternative (before, alt);
692 struct alternatives alt;
694 check_init (TREE_OPERAND (exp, 0), before);
695 BEGIN_ALTERNATIVES (before, alt);
696 alt.saved = ALLOC_BUFFER(buf, num_current_words);
697 COPY (alt.saved, before);
699 check_init (TREE_OPERAND (exp, 1), before);
700 done_alternative (before, &alt);
701 if (! SWITCH_HAS_DEFAULT (exp))
702 done_alternative (alt.saved, &alt);
703 FREE_BUFFER(alt.saved, buf);
704 END_ALTERNATIVES (before, alt);
711 struct alternatives *alt = alternatives;
712 while (TREE_CODE (alt->block) != SWITCH_EXPR)
714 COPYN (before, alt->saved, WORDS_NEEDED (2 * alt->num_locals));
715 for (i = alt->num_locals; i < num_current_locals; i++)
716 CLEAR_ASSIGNED (before, i);
722 tree try_clause = TREE_OPERAND (exp, 0);
723 tree clause = TREE_OPERAND (exp, 1);
725 words tmp = (num_current_words <= 2 ? buf
726 : ALLOC_WORDS (2 * num_current_words));
727 words save = tmp + num_current_words;
728 struct alternatives alt;
729 BEGIN_ALTERNATIVES (before, alt);
732 check_init (try_clause, tmp);
733 done_alternative (tmp, &alt);
734 for ( ; clause != NULL_TREE; clause = TREE_CHAIN (clause))
736 tree catch_clause = TREE_OPERAND (clause, 0);
738 check_init (catch_clause, tmp);
739 done_alternative (tmp, &alt);
745 END_ALTERNATIVES (before, alt);
749 case TRY_FINALLY_EXPR:
751 DECLARE_BUFFERS(tmp, 1);
753 check_init (TREE_OPERAND (exp, 0), before);
754 check_init (TREE_OPERAND (exp, 1), tmp);
755 UNION (before, before, tmp);
756 RELEASE_BUFFERS(tmp);
762 if (TREE_OPERAND (exp, 0))
763 check_init (TREE_OPERAND (exp, 0), before);
764 goto never_continues;
772 case TRUTH_ANDIF_EXPR:
773 case TRUTH_ORIF_EXPR:
775 DECLARE_BUFFERS(when_true, 2);
776 words when_false = when_true + num_current_words;
777 check_bool_init (exp, before, when_false, when_true);
778 INTERSECT (before, when_false, when_true);
779 RELEASE_BUFFERS(when_true);
784 if (exp == empty_stmt_node)
786 /* ... else fall through ... */
787 case UNARY_PLUS_EXPR:
800 case NON_LVALUE_EXPR:
801 case INSTANCEOF_EXPR:
807 /* Avoid needless recursion. */
808 exp = TREE_OPERAND (exp, 0);
811 case PREDECREMENT_EXPR:
812 case PREINCREMENT_EXPR:
813 case POSTDECREMENT_EXPR:
814 case POSTINCREMENT_EXPR:
815 tmp = get_variable_decl (TREE_OPERAND (exp, 0));
816 if (tmp != NULL_TREE && DECL_FINAL (tmp))
817 final_assign_error (DECL_NAME (tmp));
819 /* Avoid needless recursion. */
820 exp = TREE_OPERAND (exp, 0);
824 if (IS_INIT_CHECKED (exp))
826 IS_INIT_CHECKED (exp) = 1;
827 exp = TREE_OPERAND (exp, 0);
862 check_init (TREE_OPERAND (exp, 0), before);
863 /* Avoid needless recursion, especially for COMPOUND_EXPR. */
864 exp = TREE_OPERAND (exp, 1);
872 case JAVA_EXC_OBJ_EXPR:
878 tree func = TREE_OPERAND (exp, 0);
879 tree x = TREE_OPERAND (exp, 1);
880 if (TREE_CODE (func) == ADDR_EXPR)
881 func = TREE_OPERAND (func, 0);
882 check_init (func, before);
884 for ( ; x != NULL_TREE; x = TREE_CHAIN (x))
885 check_init (TREE_VALUE (x), before);
886 if (func == throw_node)
887 goto never_continues;
893 tree x = CONSTRUCTOR_ELTS (TREE_OPERAND (exp, 0));
894 for ( ; x != NULL_TREE; x = TREE_CHAIN (x))
895 check_init (TREE_VALUE (x), before);
899 case EXPR_WITH_FILE_LOCATION:
901 const char *saved_input_filename = input_filename;
902 tree saved_wfl = wfl;
903 tree body = EXPR_WFL_NODE (exp);
904 int saved_lineno = lineno;
905 if (body == empty_stmt_node)
908 input_filename = EXPR_WFL_FILENAME (exp);
909 lineno = EXPR_WFL_LINENO (exp);
910 check_init (body, before);
911 input_filename = saved_input_filename;
912 lineno = saved_lineno;
919 ("internal error in check-init: tree code not implemented: %s",
920 tree_code_name [(int) TREE_CODE (exp)]);
925 check_for_initialization (body, mdecl)
931 tree owner = DECL_CONTEXT (mdecl);
932 int is_static_method = METHOD_STATIC (mdecl);
933 /* We don't need to check final fields of <init> it it calls this(). */
934 int is_finit_method = DECL_FINIT_P (mdecl) || DECL_INSTINIT_P (mdecl);
936 = (is_finit_method || DECL_CLINIT_P (mdecl)
937 || (DECL_INIT_P (mdecl) && ! DECL_INIT_CALLS_THIS (mdecl)));
939 start_current_locals = num_current_locals = 0;
940 num_current_words = 2;
945 for (decl = TYPE_FIELDS (owner);
946 decl != NULL_TREE; decl = TREE_CHAIN (decl))
948 if (DECL_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
950 if (DECL_FIELD_FINAL_IUD (decl))
951 DECL_BIT_INDEX (decl) = -1;
953 DECL_BIT_INDEX (decl) = num_current_locals++;
956 words_needed = WORDS_NEEDED (2 * num_current_locals);
957 if (words_needed > 2)
959 num_current_words = words_needed;
960 before = ALLOC_WORDS(words_needed);
963 for (decl = TYPE_FIELDS (owner);
964 decl != NULL_TREE; decl = TREE_CHAIN (decl))
966 if (FIELD_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
968 if (! DECL_FIELD_FINAL_IUD (decl))
970 CLEAR_ASSIGNED (before, i);
971 SET_UNASSIGNED (before, i);
979 check_init (body, before);
983 for (decl = TYPE_FIELDS (owner);
984 decl != NULL_TREE; decl = TREE_CHAIN (decl))
986 if (FIELD_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
988 int index = DECL_BIT_INDEX (decl);
989 if (index >= 0 && ! ASSIGNED_P (before, index))
991 if (! is_finit_method)
992 error_with_decl (decl, "final field '%s' may not have been initialized");
994 else if (is_finit_method)
995 DECL_FIELD_FINAL_IUD (decl) = 1;
997 /* Re-set to initial state, since we later may use the
998 same bit for DECL_POINTER_ALIAS_SET. */
999 DECL_BIT_INDEX (decl) = -1;
1004 start_current_locals = num_current_locals = 0;