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