* call.c: Fix comment formatting.
[official-gcc.git] / gcc / java / check-init.c
blobe02237664bd3dee9fdab7a1c7d3cbc8075d9b465
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 *(htab_find_slot
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;
620 if (tmp != before)
622 num_current_words = save_num_current_words;
623 COPY (before, tmp);
624 FREE_WORDS (tmp);
627 break;
628 case LOOP_EXPR:
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);
645 alt.block = exp;
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;
650 return;
652 case EXIT_EXPR:
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)
659 abort ();
660 #endif
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);
665 return;
667 case LABELED_BLOCK_EXPR:
669 struct alternatives alt;
670 BEGIN_ALTERNATIVES (before, alt);
671 alt.block = exp;
672 if (LABELED_BLOCK_BODY (exp))
673 check_init (LABELED_BLOCK_BODY (exp), before);
674 done_alternative (before, &alt);
675 END_ALTERNATIVES (before, alt);
676 return;
678 case EXIT_BLOCK_EXPR:
680 tree block = TREE_OPERAND (exp, 0);
681 struct alternatives *alt = alternatives;
682 while (alt->block != block)
683 alt = alt->outer;
684 done_alternative (before, alt);
685 SET_ALL (before);
686 return;
688 case SWITCH_EXPR:
690 struct alternatives alt;
691 word buf[2];
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);
696 alt.block = exp;
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);
703 return;
705 case CASE_EXPR:
706 case DEFAULT_EXPR:
708 int i;
709 struct alternatives *alt = alternatives;
710 while (TREE_CODE (alt->block) != SWITCH_EXPR)
711 alt = alt->outer;
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);
715 break;
718 case TRY_EXPR:
720 tree try_clause = TREE_OPERAND (exp, 0);
721 tree clause = TREE_OPERAND (exp, 1);
722 word buf[2*2];
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);
728 COPY (save, before);
729 COPY (tmp, save);
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);
735 COPY (tmp, save);
736 check_init (catch_clause, tmp);
737 done_alternative (tmp, &alt);
739 if (tmp != buf)
741 FREE_WORDS (tmp);
743 END_ALTERNATIVES (before, alt);
745 return;
747 case TRY_FINALLY_EXPR:
749 DECLARE_BUFFERS(tmp, 1);
750 COPY (tmp, before);
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);
756 return;
758 case RETURN_EXPR:
759 case THROW_EXPR:
760 if (TREE_OPERAND (exp, 0))
761 check_init (TREE_OPERAND (exp, 0), before);
762 goto never_continues;
764 case ERROR_MARK:
765 never_continues:
766 SET_ALL (before);
767 return;
769 case COND_EXPR:
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);
779 break;
781 case NOP_EXPR:
782 if (exp == empty_stmt_node)
783 break;
784 /* ... else fall through ... */
785 case UNARY_PLUS_EXPR:
786 case NEGATE_EXPR:
787 case TRUTH_AND_EXPR:
788 case TRUTH_OR_EXPR:
789 case TRUTH_XOR_EXPR:
790 case TRUTH_NOT_EXPR:
791 case BIT_NOT_EXPR:
792 case CONVERT_EXPR:
793 case BIT_FIELD_REF:
794 case FLOAT_EXPR:
795 case FIX_TRUNC_EXPR:
796 case INDIRECT_REF:
797 case ADDR_EXPR:
798 case NON_LVALUE_EXPR:
799 case INSTANCEOF_EXPR:
800 case FIX_CEIL_EXPR:
801 case FIX_FLOOR_EXPR:
802 case FIX_ROUND_EXPR:
803 case ABS_EXPR:
804 case FFS_EXPR:
805 /* Avoid needless recursion. */
806 exp = TREE_OPERAND (exp, 0);
807 goto again;
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);
819 goto again;
821 case SAVE_EXPR:
822 if (IS_INIT_CHECKED (exp))
823 return;
824 IS_INIT_CHECKED (exp) = 1;
825 exp = TREE_OPERAND (exp, 0);
826 goto again;
828 case COMPOUND_EXPR:
829 case PLUS_EXPR:
830 case MINUS_EXPR:
831 case MULT_EXPR:
832 case TRUNC_DIV_EXPR:
833 case TRUNC_MOD_EXPR:
834 case RDIV_EXPR:
835 case LSHIFT_EXPR:
836 case RSHIFT_EXPR:
837 case URSHIFT_EXPR:
838 case BIT_AND_EXPR:
839 case BIT_XOR_EXPR:
840 case BIT_IOR_EXPR:
841 case EQ_EXPR:
842 case NE_EXPR:
843 case GT_EXPR:
844 case GE_EXPR:
845 case LT_EXPR:
846 case LE_EXPR:
847 case MAX_EXPR:
848 case MIN_EXPR:
849 case ARRAY_REF:
850 case LROTATE_EXPR:
851 case RROTATE_EXPR:
852 case CEIL_DIV_EXPR:
853 case FLOOR_DIV_EXPR:
854 case ROUND_DIV_EXPR:
855 case CEIL_MOD_EXPR:
856 case FLOOR_MOD_EXPR:
857 case ROUND_MOD_EXPR:
858 case EXACT_DIV_EXPR:
859 binop:
860 check_init (TREE_OPERAND (exp, 0), before);
861 /* Avoid needless recursion, especially for COMPOUND_EXPR. */
862 exp = TREE_OPERAND (exp, 1);
863 goto again;
865 case RESULT_DECL:
866 case FUNCTION_DECL:
867 case INTEGER_CST:
868 case REAL_CST:
869 case STRING_CST:
870 case JAVA_EXC_OBJ_EXPR:
871 break;
873 case NEW_CLASS_EXPR:
874 case CALL_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;
887 break;
889 case NEW_ARRAY_INIT:
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);
895 break;
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)
904 break;
905 wfl = exp;
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;
911 wfl = saved_wfl;
913 break;
915 default:
916 internal_error
917 ("internal error in check-init: tree code not implemented: %s",
918 tree_code_name [(int) TREE_CODE (exp)]);
922 void
923 check_for_initialization (body, mdecl)
924 tree body, mdecl;
926 tree decl;
927 word buf[2];
928 words before = buf;
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);
933 int is_init_method
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;
940 if (is_init_method)
942 int words_needed, i;
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;
950 else
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);
960 i = 0;
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);
970 i++;
977 check_init (body, before);
979 if (is_init_method)
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;