Mark ChangeLog
[official-gcc.git] / gcc / java / check-init.c
blob6dc0deee7fdc8e45b4f5280b9858d21c34766707
1 /* Code to test for "definitive [un]assignment".
2 Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005 Free Software Foundation,
3 Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Per Bothner <bothner@cygnus.com>, January 1999. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "flags.h" /* Needed for optimize. */
34 #include "java-tree.h"
35 #include "toplev.h" /* Needed for fatal. */
37 /* The basic idea is that we assign each local variable declaration
38 and each blank final field an index, and then we pass around
39 bitstrings, where the (2*i)'th bit is set if decl whose DECL_BIT_INDEX
40 is i is definitely assigned, and the the (2*i=1)'th bit is set if
41 decl whose DECL_BIT_INDEX is i is definitely unassigned */
43 /* One segment of a bitstring. */
44 typedef unsigned int word;
46 /* Pointer to a bitstring. */
47 typedef word *words;
49 /* Number of locals variables currently active. */
50 static int num_current_locals = 0;
52 /* The value of num_current_locals when we entered the closest
53 enclosing LOOP_EXPR. */
54 static int loop_current_locals;
56 /* The index of the first local variable in the current block.
58 The variables whose DECL_BIT_INDEX are in the range from
59 start_current_locals (inclusive) up to num_current_locals (exclusive)
60 are declared in the "current" block. If there is a loop or branch
61 form, we set start_current_locals to num_current_locals to indicate
62 there is no current block.
64 The point is that if a variable in the current block is set,
65 there are no other control paths that we have to worry about.
66 Hence, we can remove it from the set of variables we are
67 checking, making its bit index available for some other variable.
68 For simplicity, we only do that if the variable's bit index
69 is (num_current_locals-1); freeing up its bit index is then
70 just a simple matter of decrementing num_current_locals.
71 The reason this is worth doing is that it is simple, and
72 allows us to use short (usually one-word) bit-strings,
73 even for methods with thousands of local variables, as
74 long as most of them are initialized immediately after or in
75 their declaration. */
76 static int start_current_locals = 0;
78 static int num_current_words;
80 static tree wfl;
82 #define COPYN(DST, SRC, NWORDS) memcpy (DST, SRC, NWORDS * sizeof(word))
83 #define COPY(DST, SRC) COPYN (DST, SRC, num_current_words)
85 #define SET_ALL(DST) memset (DST, ~0, num_current_words * sizeof(word))
86 #define CLEAR_ALL(DST) memset (DST, 0, num_current_words * sizeof(word))
88 #define INTERSECTN(DST, SRC1, SRC2, N) \
89 do { int n = N; \
90 while (--n >= 0) DST[n] = SRC1[n] & SRC2[n]; \
91 } while (0)
93 #define UNION(DST, SRC1, SRC2) \
94 UNIONN (DST, SRC1, SRC2, num_current_words)
96 #define UNIONN(DST, SRC1, SRC2, N) \
97 do { int n = N; \
98 while (--n >= 0) DST[n] = SRC1[n] | SRC2[n]; \
99 } while (0)
101 #define INTERSECT(DST, SRC1, SRC2) \
102 INTERSECTN (DST, SRC1, SRC2, num_current_words)
104 #define WORD_SIZE ((unsigned int)(sizeof(word) * BITS_PER_UNIT))
106 static void check_bool_init (tree, words, words, words);
107 static void check_init (tree, words);
108 static void check_cond_init (tree, tree, tree, words, words, words);
109 static void check_bool2_init (enum tree_code, tree, tree, words, words, words);
110 struct alternatives;
111 static void done_alternative (words, struct alternatives *);
112 static tree get_variable_decl (tree);
113 static void final_assign_error (tree);
114 static void check_final_reassigned (tree, words);
116 #define ALLOC_WORDS(NUM) (xmalloc ((NUM) * sizeof (word)))
117 #define FREE_WORDS(PTR) (free (PTR))
119 /* DECLARE_BUFFERS is used to allocate NUMBUFFER bit sets, each of
120 which is an array of length num_current_words number of words.
121 Declares a new local variable BUFFER to hold the result (or rather
122 a pointer to the first of the bit sets). In almost all cases
123 num_current_words will be 1 or at most 2, so we try to stack
124 allocate the arrays in that case, using a stack array
125 named BUFFER##_short. Each DECLARE_BUFFERS must be matched by
126 a corresponding RELEASE_BUFFERS to avoid memory leaks. */
128 #define DECLARE_BUFFERS(BUFFER, NUMBUFFERS) \
129 word BUFFER##_short[2 * NUMBUFFERS]; \
130 words BUFFER = ALLOC_BUFFER(BUFFER##_short, NUMBUFFERS * num_current_words)
132 #define RELEASE_BUFFERS(BUFFER) \
133 FREE_BUFFER(BUFFER, BUFFER##_short)
135 #define ALLOC_BUFFER(SHORTBUFFER, NUMWORDS) \
136 ((NUMWORDS) * sizeof(word) <= sizeof(SHORTBUFFER) ? SHORTBUFFER \
137 : ALLOC_WORDS(NUMWORDS))
139 #define FREE_BUFFER(BUFFER, SHORTBUFFER) \
140 if (BUFFER != SHORTBUFFER) FREE_WORDS(BUFFER)
142 #define SET_P(WORDS, BIT) \
143 (WORDS[(BIT) / WORD_SIZE] & (1 << ((BIT) % WORD_SIZE)))
145 #define CLEAR_BIT(WORDS, BIT) \
146 (WORDS[(BIT) / WORD_SIZE] &= ~ (1 << ((BIT) % WORD_SIZE)))
148 #define SET_BIT(WORDS, BIT) \
149 (WORDS[(BIT) / WORD_SIZE] |= (1 << ((BIT) % WORD_SIZE)))
151 #define WORDS_NEEDED(BITS) (((BITS)+(WORD_SIZE-1))/(WORD_SIZE))
153 #define ASSIGNED_P(WORDS, BIT) SET_P(WORDS, 2 * (BIT))
154 #define UNASSIGNED_P(WORDS, BIT) SET_P(WORDS, 2 * (BIT) + 1)
156 #define SET_ASSIGNED(WORDS, INDEX) SET_BIT (WORDS, 2 * (INDEX))
157 #define SET_UNASSIGNED(WORDS, INDEX) SET_BIT (WORDS, 2 * (INDEX) + 1)
159 #define CLEAR_ASSIGNED(WORDS, INDEX) CLEAR_BIT (WORDS, 2 * (INDEX))
160 #define CLEAR_UNASSIGNED(WORDS, INDEX) CLEAR_BIT (WORDS, 2 * (INDEX) + 1)
162 /* Get the "interesting" declaration from a MODIFY_EXPR or COMPONENT_REF.
163 Return the declaration or NULL_TREE if no interesting declaration. */
165 static tree
166 get_variable_decl (tree exp)
168 /* A static field can be wrapped in a COMPOUND_EXPR where the first
169 argument initializes the class. */
170 if (TREE_CODE (exp) == COMPOUND_EXPR)
171 exp = extract_field_decl (exp);
173 if (TREE_CODE (exp) == VAR_DECL)
175 if (! TREE_STATIC (exp) || FIELD_FINAL (exp))
176 return exp;
178 /* We only care about final parameters. */
179 else if (TREE_CODE (exp) == PARM_DECL)
181 if (DECL_FINAL (exp))
182 return exp;
184 /* See if exp is this.field. */
185 else if (TREE_CODE (exp) == COMPONENT_REF)
187 tree op0 = TREE_OPERAND (exp, 0);
188 tree op1 = TREE_OPERAND (exp, 1);
189 tree mdecl = current_function_decl;
190 if (TREE_CODE (op0) == INDIRECT_REF
191 && TREE_CODE (op1) == FIELD_DECL
192 && ! METHOD_STATIC (mdecl)
193 && FIELD_FINAL (op1))
195 op0 = TREE_OPERAND (op0, 0);
196 if (op0 == BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)))
197 return op1;
200 else if (TREE_CODE (exp) == INDIRECT_REF)
202 /* For indirect dispatch, look for an expression of the form
203 (indirect_ref (+ (array_ref otable <N>) this)).
204 FIXME: it would probably be better to generate a JAVA_FIELD_REF
205 expression that gets converted to OTABLE access at
206 gimplification time. */
207 exp = TREE_OPERAND (exp, 0);
208 if (TREE_CODE (exp) == PLUS_EXPR)
210 tree op0 = TREE_OPERAND (exp, 0);
211 STRIP_NOPS (op0);
212 if (TREE_CODE (op0) == ARRAY_REF)
214 tree table = TREE_OPERAND (op0, 0);
215 if (TREE_CODE (table) == VAR_DECL
216 && DECL_LANG_SPECIFIC (table)
217 && DECL_OWNER (table)
218 && TYPE_OTABLE_DECL (DECL_OWNER (table)) == table)
220 HOST_WIDE_INT index
221 = TREE_INT_CST_LOW (TREE_OPERAND (op0, 1));
222 tree otable_methods
223 = TYPE_OTABLE_METHODS (DECL_OWNER (table));
224 tree element;
225 for (element = otable_methods;
226 element;
227 element = TREE_CHAIN (element))
229 if (index == 1)
231 tree purpose = TREE_PURPOSE (element);
232 if (TREE_CODE (purpose) == FIELD_DECL)
233 return purpose;
234 else
235 return NULL_TREE;
237 --index;
244 return NULL_TREE;
247 static void
248 final_assign_error (tree name)
250 parse_error_context (wfl,
251 "Can't reassign a value to the final variable %qs",
252 IDENTIFIER_POINTER (name));
255 static void
256 check_final_reassigned (tree decl, words before)
258 int index = DECL_BIT_INDEX (decl);
259 /* A final local already assigned or a final parameter
260 assigned must be reported as errors */
261 if (DECL_FINAL (decl) && index != -2
262 && (index < loop_current_locals /* I.e. -1, or outside current loop. */
263 || (DECL_LOCAL_FINAL_IUD (decl) ? ASSIGNED_P (before, index)
264 : ! UNASSIGNED_P (before, index))))
266 final_assign_error (DECL_NAME (decl));
270 /* Check a conditional form (TEST_EXP ? THEN_EXP : ELSE_EXP) for
271 definite [un]assignment.
272 BEFORE, WHEN_FALSE, and WHEN_TRUE are as in check_bool_init. */
274 static void
275 check_cond_init (tree test_exp, tree then_exp, tree else_exp,
276 words before, words when_false, words when_true)
278 int save_start_current_locals = start_current_locals;
279 DECLARE_BUFFERS(test_false, 6);
280 words test_true = test_false + num_current_words;
281 words then_false = test_true + num_current_words;
282 words then_true = then_false + num_current_words;
283 words else_false = then_true + num_current_words;
284 words else_true = else_false + num_current_words;
285 start_current_locals = num_current_locals;
287 check_bool_init (test_exp, before, test_false, test_true);
288 check_bool_init (then_exp, test_true, then_false, then_true);
289 check_bool_init (else_exp, test_false, else_false, else_true);
290 INTERSECT (when_false, then_false, else_false);
291 INTERSECT (when_true, then_true, else_true);
292 RELEASE_BUFFERS(test_false);
293 start_current_locals = save_start_current_locals;
296 /* Check a boolean binary form CODE (EXP0, EXP1),
297 where CODE is one of EQ_EXPR, BIT_AND_EXPR, or BIT_IOR_EXPR.
298 BEFORE, WHEN_FALSE, and WHEN_TRUE are as in check_bool_init. */
300 static void
301 check_bool2_init (enum tree_code code, tree exp0, tree exp1,
302 words before, words when_false, words when_true)
304 word buf[2*4];
305 words tmp = num_current_words <= 2 ? buf
306 : ALLOC_WORDS (4 * num_current_words);
307 words when_false_0 = tmp;
308 words when_false_1 = tmp+num_current_words;
309 words when_true_0 = tmp+2*num_current_words;
310 words when_true_1 = tmp+3*num_current_words;
311 check_bool_init (exp0, before, when_false_0, when_true_0);
312 INTERSECT (before, when_false_0, when_true_0);
313 check_bool_init (exp1, before, when_false_1, when_true_1);
315 INTERSECT (before, when_false_1, when_true_1);
317 if (code == EQ_EXPR)
319 /* Now set:
320 * when_true = (when_false_1 INTERSECTION when_true_1)
321 * UNION (when_true_0 INTERSECTION when_false_1)
322 * UNION (when_false_0 INTERSECTION when_true_1);
323 * using when_false and before as temporary working areas. */
324 INTERSECT (when_true, when_true_0, when_false_1);
325 INTERSECT (when_false, when_true_0, when_false_1);
326 UNION (when_true, when_true, when_false);
327 UNION (when_true, when_true, before);
329 /* Now set:
330 * when_false = (when_false_1 INTERSECTION when_true_1)
331 * UNION (when_true_0 INTERSECTION when_true_1)
332 * UNION (when_false_0 INTERSECTION when_false_1);
333 * using before as a temporary working area. */
334 INTERSECT (when_false, when_true_0, when_true_1);
335 UNION (when_false, when_false, before);
336 INTERSECT (before, when_false_0, when_false_1);
337 UNION (when_false, when_false, before);
339 else if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
341 UNION (when_true, when_true_0, when_true_1);
342 INTERSECT (when_false, when_false_0, when_false_1);
343 UNION (when_false, when_false, before);
345 else /* if (code == BIT_IOR_EXPR || code == TRUTH_OR_EXPR) */
347 UNION (when_false, when_false_0, when_false_1);
348 INTERSECT (when_true, when_true_0, when_true_1);
349 UNION (when_true, when_true, before);
352 if (tmp != buf)
353 FREE_WORDS (tmp);
356 /* Check a boolean expression EXP for definite [un]assignment.
357 BEFORE is the set of variables definitely [un]assigned before the
358 conditional. (This bitstring may be modified arbitrarily in this function.)
359 On output, WHEN_FALSE is the set of variables [un]definitely assigned after
360 the conditional when the conditional is false.
361 On output, WHEN_TRUE is the set of variables definitely [un]assigned after
362 the conditional when the conditional is true.
363 (WHEN_FALSE and WHEN_TRUE are overwritten with initial values ignored.)
364 (None of BEFORE, WHEN_FALSE, or WHEN_TRUE can overlap, as they may
365 be used as temporary working areas. */
367 static void
368 check_bool_init (tree exp, words before, words when_false, words when_true)
370 switch (TREE_CODE (exp))
372 case COND_EXPR:
373 check_cond_init (TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
374 TREE_OPERAND (exp, 2),
375 before, when_false, when_true);
376 return;
378 case TRUTH_ANDIF_EXPR:
379 check_cond_init (TREE_OPERAND (exp, 0),
380 TREE_OPERAND (exp, 1), boolean_false_node,
381 before, when_false, when_true);
382 return;
383 case TRUTH_ORIF_EXPR:
384 check_cond_init (TREE_OPERAND (exp, 0),
385 boolean_true_node, TREE_OPERAND (exp, 1),
386 before, when_false, when_true);
387 return;
388 case TRUTH_NOT_EXPR:
389 check_bool_init (TREE_OPERAND (exp, 0), before, when_true, when_false);
390 return;
392 case BIT_AND_EXPR:
393 case BIT_IOR_EXPR:
394 case TRUTH_AND_EXPR:
395 case TRUTH_OR_EXPR:
396 case EQ_EXPR:
397 check_bool2_init (TREE_CODE (exp),
398 TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
399 before, when_false, when_true);
400 return;
402 case TRUTH_XOR_EXPR:
403 case BIT_XOR_EXPR:
404 case NE_EXPR:
405 /* Just like EQ_EXPR, but switch when_true and when_false. */
406 check_bool2_init (EQ_EXPR, TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
407 before, when_true, when_false);
409 return;
411 case INTEGER_CST:
412 if (integer_zerop (exp))
414 SET_ALL (when_true);
415 COPY (when_false, before);
417 else
419 SET_ALL (when_false);
420 COPY (when_true, before);
422 break;
424 default:
425 check_init (exp, before);
426 COPY (when_false, before);
427 COPY (when_true, before);
431 /* Used to keep track of control flow branches. */
433 struct alternatives
435 struct alternatives *outer;
437 /* The value of num_current_locals at the start of this compound. */
438 int num_locals;
440 /* The value of the "before" set at the start of the control structure.
441 Used for SWITCH_EXPR but not set for LABELED_BLOCK_EXPR. */
442 words saved;
444 int save_start_current_locals;
446 /* If num_current_words==1, combined==&one_word, for efficiency. */
447 word one_word;
449 /* The intersection of the "after" sets from previous branches. */
450 words combined;
452 tree block;
455 struct alternatives * alternatives = NULL;
457 /* Begin handling a control flow branch.
458 BEFORE is the state of [un]assigned variables on entry.
459 CURRENT is a struct alt to manage the branch alternatives. */
461 #define BEGIN_ALTERNATIVES(before, current) \
463 current.saved = NULL; \
464 current.num_locals = num_current_locals; \
465 current.combined = num_current_words <= 1 ? &current.one_word \
466 : ALLOC_WORDS (num_current_words); \
467 SET_ALL (current.combined); \
468 current.outer = alternatives; \
469 alternatives = &current; \
470 current.save_start_current_locals = start_current_locals; \
471 start_current_locals = num_current_locals; \
474 /* We have finished with one branch of branching control flow.
475 Store the [un]assigned state, merging (intersecting) it with the state
476 of previous alternative branches. */
478 static void
479 done_alternative (words after, struct alternatives *current)
481 INTERSECTN (current->combined, current->combined, after,
482 WORDS_NEEDED (2 * current->num_locals));
485 /* Used when we done with a control flow branch and are all merged again.
486 * AFTER is the merged state of [un]assigned variables,
487 CURRENT is a struct alt that was passed to BEGIN_ALTERNATIVES. */
489 #define END_ALTERNATIVES(after, current) \
491 alternatives = current.outer; \
492 COPY (after, current.combined); \
493 if (current.combined != &current.one_word) \
494 FREE_WORDS (current.combined); \
495 start_current_locals = current.save_start_current_locals; \
498 /* Check for (un)initialized local variables in EXP. */
500 static void
501 check_init (tree exp, words before)
503 tree tmp;
504 again:
505 switch (TREE_CODE (exp))
507 case VAR_DECL:
508 case PARM_DECL:
509 if (! FIELD_STATIC (exp) && DECL_NAME (exp) != NULL_TREE
510 && DECL_NAME (exp) != this_identifier_node)
512 int index = DECL_BIT_INDEX (exp);
513 /* We don't want to report and mark as non initialized class
514 initialization flags. */
515 if (! LOCAL_CLASS_INITIALIZATION_FLAG_P (exp)
516 && index >= 0 && ! ASSIGNED_P (before, index))
518 parse_error_context
519 (wfl, "Variable %qs may not have been initialized",
520 IDENTIFIER_POINTER (DECL_NAME (exp)));
521 /* Suppress further errors. */
522 DECL_BIT_INDEX (exp) = -2;
525 break;
527 case COMPONENT_REF:
528 check_init (TREE_OPERAND (exp, 0), before);
529 if ((tmp = get_variable_decl (exp)) != NULL_TREE)
531 int index = DECL_BIT_INDEX (tmp);
532 if (index >= 0 && ! ASSIGNED_P (before, index))
534 parse_error_context
535 (wfl, "variable %qs may not have been initialized",
536 IDENTIFIER_POINTER (DECL_NAME (tmp)));
537 /* Suppress further errors. */
538 DECL_BIT_INDEX (tmp) = -2;
541 break;
543 case MODIFY_EXPR:
544 tmp = TREE_OPERAND (exp, 0);
545 /* We're interested in variable declaration and parameter
546 declaration when they're declared with the `final' modifier. */
547 if ((tmp = get_variable_decl (tmp)) != NULL_TREE)
549 int index;
550 check_init (TREE_OPERAND (exp, 1), before);
551 check_final_reassigned (tmp, before);
552 index = DECL_BIT_INDEX (tmp);
553 if (index >= 0)
555 SET_ASSIGNED (before, index);
556 CLEAR_UNASSIGNED (before, index);
558 /* Minor optimization. See comment for start_current_locals.
559 If we're optimizing for class initialization, we keep
560 this information to check whether the variable is
561 definitely assigned when once we checked the whole
562 function. */
563 if (! STATIC_CLASS_INIT_OPT_P () /* FIXME */
564 && ! DECL_FINAL (tmp)
565 && index >= start_current_locals
566 && index == num_current_locals - 1)
568 num_current_locals--;
569 DECL_BIT_INDEX (tmp) = -1;
571 break;
573 else if (TREE_CODE (tmp = TREE_OPERAND (exp, 0)) == COMPONENT_REF)
575 tree decl;
576 check_init (tmp, before);
577 check_init (TREE_OPERAND (exp, 1), before);
578 decl = TREE_OPERAND (tmp, 1);
579 if (DECL_FINAL (decl))
580 final_assign_error (DECL_NAME (decl));
581 break;
583 else if (TREE_CODE (tmp) == COMPONENT_REF && IS_ARRAY_LENGTH_ACCESS (tmp))
585 /* We can't emit a more specific message here, because when
586 compiling to bytecodes we don't get here. */
587 final_assign_error (length_identifier_node);
589 else
590 goto binop;
591 case BLOCK:
592 if (BLOCK_EXPR_BODY (exp))
594 tree decl = BLOCK_EXPR_DECLS (exp);
595 int words_needed;
596 word* tmp;
597 int i;
598 int save_start_current_locals = start_current_locals;
599 int save_num_current_words = num_current_words;
600 start_current_locals = num_current_locals;
601 for (; decl != NULL_TREE; decl = TREE_CHAIN (decl))
603 DECL_BIT_INDEX (decl) = num_current_locals++;
605 words_needed = WORDS_NEEDED (2 * num_current_locals);
606 if (words_needed > num_current_words)
608 tmp = ALLOC_WORDS (words_needed);
609 COPY (tmp, before);
610 num_current_words = words_needed;
612 else
613 tmp = before;
614 for (i = start_current_locals; i < num_current_locals; i++)
616 CLEAR_ASSIGNED (tmp, i);
617 SET_UNASSIGNED (tmp, i);
619 check_init (BLOCK_EXPR_BODY (exp), tmp);
621 /* Re-set DECL_BIT_INDEX since it is also DECL_POINTER_ALIAS_SET. */
622 for (decl = BLOCK_EXPR_DECLS (exp);
623 decl != NULL_TREE; decl = TREE_CHAIN (decl))
625 if (LOCAL_CLASS_INITIALIZATION_FLAG_P (decl))
627 int index = DECL_BIT_INDEX (decl);
628 tree fndecl = DECL_CONTEXT (decl);
629 if (fndecl && METHOD_STATIC (fndecl)
630 && (DECL_INITIAL (decl) == boolean_true_node
631 || (index >= 0 && ASSIGNED_P (tmp, index))))
632 *(htab_find_slot
633 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl),
634 DECL_FUNCTION_INIT_TEST_CLASS (decl), INSERT)) =
635 DECL_FUNCTION_INIT_TEST_CLASS (decl);
637 DECL_BIT_INDEX (decl) = -1;
640 num_current_locals = start_current_locals;
641 start_current_locals = save_start_current_locals;
642 if (tmp != before)
644 num_current_words = save_num_current_words;
645 COPY (before, tmp);
646 FREE_WORDS (tmp);
649 break;
650 case LOOP_EXPR:
652 /* The JLS 2nd edition discusses a complication determining
653 definite unassignment of loop statements. They define a
654 "hypothetical" analysis model. We do something much
655 simpler: We just disallow assignments inside loops to final
656 variables declared outside the loop. This means we may
657 disallow some contrived assignments that the JLS, but I
658 can't see how anything except a very contrived testcase (a
659 do-while whose condition is false?) would care. */
661 struct alternatives alt;
662 int save_loop_current_locals = loop_current_locals;
663 int save_start_current_locals = start_current_locals;
664 loop_current_locals = num_current_locals;
665 start_current_locals = num_current_locals;
666 BEGIN_ALTERNATIVES (before, alt);
667 alt.block = exp;
668 check_init (TREE_OPERAND (exp, 0), before);
669 END_ALTERNATIVES (before, alt);
670 loop_current_locals = save_loop_current_locals;
671 start_current_locals = save_start_current_locals;
672 return;
674 case EXIT_EXPR:
676 struct alternatives *alt = alternatives;
677 DECLARE_BUFFERS(when_true, 2);
678 words when_false = when_true + num_current_words;
679 #ifdef ENABLE_JC1_CHECKING
680 if (TREE_CODE (alt->block) != LOOP_EXPR)
681 abort ();
682 #endif
683 check_bool_init (TREE_OPERAND (exp, 0), before, when_false, when_true);
684 done_alternative (when_true, alt);
685 COPY (before, when_false);
686 RELEASE_BUFFERS(when_true);
687 return;
689 case LABELED_BLOCK_EXPR:
691 struct alternatives alt;
692 BEGIN_ALTERNATIVES (before, alt);
693 alt.block = exp;
694 if (LABELED_BLOCK_BODY (exp))
695 check_init (LABELED_BLOCK_BODY (exp), before);
696 done_alternative (before, &alt);
697 END_ALTERNATIVES (before, alt);
698 return;
700 case EXIT_BLOCK_EXPR:
702 tree block = TREE_OPERAND (exp, 0);
703 struct alternatives *alt = alternatives;
704 while (alt->block != block)
705 alt = alt->outer;
706 done_alternative (before, alt);
707 SET_ALL (before);
708 return;
710 case SWITCH_EXPR:
712 struct alternatives alt;
713 word buf[2];
714 check_init (TREE_OPERAND (exp, 0), before);
715 BEGIN_ALTERNATIVES (before, alt);
716 alt.saved = ALLOC_BUFFER(buf, num_current_words);
717 COPY (alt.saved, before);
718 alt.block = exp;
719 check_init (TREE_OPERAND (exp, 1), before);
720 done_alternative (before, &alt);
721 if (! SWITCH_HAS_DEFAULT (exp))
722 done_alternative (alt.saved, &alt);
723 FREE_BUFFER(alt.saved, buf);
724 END_ALTERNATIVES (before, alt);
725 return;
727 case CASE_EXPR:
728 case DEFAULT_EXPR:
730 int i;
731 struct alternatives *alt = alternatives;
732 while (TREE_CODE (alt->block) != SWITCH_EXPR)
733 alt = alt->outer;
734 COPYN (before, alt->saved, WORDS_NEEDED (2 * alt->num_locals));
735 for (i = alt->num_locals; i < num_current_locals; i++)
736 CLEAR_ASSIGNED (before, i);
737 break;
740 case TRY_EXPR:
742 tree try_clause = TREE_OPERAND (exp, 0);
743 tree clause = TREE_OPERAND (exp, 1);
744 word buf[2*2];
745 words tmp = (num_current_words <= 2 ? buf
746 : ALLOC_WORDS (2 * num_current_words));
747 words save = tmp + num_current_words;
748 struct alternatives alt;
749 BEGIN_ALTERNATIVES (before, alt);
750 COPY (save, before);
751 COPY (tmp, save);
752 check_init (try_clause, tmp);
753 done_alternative (tmp, &alt);
754 for ( ; clause != NULL_TREE; clause = TREE_CHAIN (clause))
756 tree catch_clause = TREE_OPERAND (clause, 0);
757 COPY (tmp, save);
758 check_init (catch_clause, tmp);
759 done_alternative (tmp, &alt);
761 if (tmp != buf)
763 FREE_WORDS (tmp);
765 END_ALTERNATIVES (before, alt);
767 return;
769 case TRY_FINALLY_EXPR:
771 DECLARE_BUFFERS(tmp, 1);
772 COPY (tmp, before);
773 check_init (TREE_OPERAND (exp, 0), before);
774 check_init (TREE_OPERAND (exp, 1), tmp);
775 UNION (before, before, tmp);
776 RELEASE_BUFFERS(tmp);
778 return;
780 case RETURN_EXPR:
781 case THROW_EXPR:
782 if (TREE_OPERAND (exp, 0))
783 check_init (TREE_OPERAND (exp, 0), before);
784 goto never_continues;
786 case ERROR_MARK:
787 never_continues:
788 SET_ALL (before);
789 return;
791 case COND_EXPR:
792 case TRUTH_ANDIF_EXPR:
793 case TRUTH_ORIF_EXPR:
795 DECLARE_BUFFERS(when_true, 2);
796 words when_false = when_true + num_current_words;
797 check_bool_init (exp, before, when_false, when_true);
798 INTERSECT (before, when_false, when_true);
799 RELEASE_BUFFERS(when_true);
801 break;
803 case NOP_EXPR:
804 if (IS_EMPTY_STMT (exp))
805 break;
806 /* ... else fall through ... */
807 case UNARY_PLUS_EXPR:
808 case NEGATE_EXPR:
809 case TRUTH_AND_EXPR:
810 case TRUTH_OR_EXPR:
811 case TRUTH_XOR_EXPR:
812 case TRUTH_NOT_EXPR:
813 case BIT_NOT_EXPR:
814 case CONVERT_EXPR:
815 case BIT_FIELD_REF:
816 case FLOAT_EXPR:
817 case FIX_TRUNC_EXPR:
818 case INDIRECT_REF:
819 case ADDR_EXPR:
820 case NON_LVALUE_EXPR:
821 case INSTANCEOF_EXPR:
822 case FIX_CEIL_EXPR:
823 case FIX_FLOOR_EXPR:
824 case FIX_ROUND_EXPR:
825 case ABS_EXPR:
826 /* Avoid needless recursion. */
827 exp = TREE_OPERAND (exp, 0);
828 goto again;
830 case PREDECREMENT_EXPR:
831 case PREINCREMENT_EXPR:
832 case POSTDECREMENT_EXPR:
833 case POSTINCREMENT_EXPR:
834 tmp = get_variable_decl (TREE_OPERAND (exp, 0));
835 if (tmp != NULL_TREE && DECL_FINAL (tmp))
836 final_assign_error (DECL_NAME (tmp));
837 else if (TREE_CODE (tmp = TREE_OPERAND (exp, 0)) == COMPONENT_REF)
839 /* Take care of array length accesses too. */
840 tree decl = TREE_OPERAND (tmp, 1);
841 if (DECL_FINAL (decl))
842 final_assign_error (DECL_NAME (decl));
845 /* Avoid needless recursion. */
846 exp = TREE_OPERAND (exp, 0);
847 goto again;
849 case SAVE_EXPR:
850 if (IS_INIT_CHECKED (exp))
851 return;
852 IS_INIT_CHECKED (exp) = 1;
853 exp = TREE_OPERAND (exp, 0);
854 goto again;
856 case COMPOUND_EXPR:
857 case PLUS_EXPR:
858 case MINUS_EXPR:
859 case MULT_EXPR:
860 case TRUNC_DIV_EXPR:
861 case TRUNC_MOD_EXPR:
862 case RDIV_EXPR:
863 case LSHIFT_EXPR:
864 case RSHIFT_EXPR:
865 case URSHIFT_EXPR:
866 case BIT_AND_EXPR:
867 case BIT_XOR_EXPR:
868 case BIT_IOR_EXPR:
869 case EQ_EXPR:
870 case NE_EXPR:
871 case GT_EXPR:
872 case GE_EXPR:
873 case LT_EXPR:
874 case LE_EXPR:
875 case MAX_EXPR:
876 case MIN_EXPR:
877 case ARRAY_REF:
878 case LROTATE_EXPR:
879 case RROTATE_EXPR:
880 case CEIL_DIV_EXPR:
881 case FLOOR_DIV_EXPR:
882 case ROUND_DIV_EXPR:
883 case CEIL_MOD_EXPR:
884 case FLOOR_MOD_EXPR:
885 case ROUND_MOD_EXPR:
886 case EXACT_DIV_EXPR:
887 case UNLT_EXPR:
888 case UNLE_EXPR:
889 case UNGT_EXPR:
890 case UNGE_EXPR:
891 case UNEQ_EXPR:
892 case LTGT_EXPR:
893 binop:
894 check_init (TREE_OPERAND (exp, 0), before);
895 /* Avoid needless recursion, especially for COMPOUND_EXPR. */
896 exp = TREE_OPERAND (exp, 1);
897 goto again;
899 case RESULT_DECL:
900 case FUNCTION_DECL:
901 case INTEGER_CST:
902 case REAL_CST:
903 case STRING_CST:
904 case DECL_EXPR:
905 case JAVA_EXC_OBJ_EXPR:
906 break;
908 case NEW_CLASS_EXPR:
909 case CALL_EXPR:
911 tree func = TREE_OPERAND (exp, 0);
912 tree x = TREE_OPERAND (exp, 1);
913 if (TREE_CODE (func) == ADDR_EXPR)
914 func = TREE_OPERAND (func, 0);
915 check_init (func, before);
917 for ( ; x != NULL_TREE; x = TREE_CHAIN (x))
918 check_init (TREE_VALUE (x), before);
919 if (func == throw_node)
920 goto never_continues;
922 break;
924 case NEW_ARRAY_INIT:
926 tree x = CONSTRUCTOR_ELTS (TREE_OPERAND (exp, 0));
927 for ( ; x != NULL_TREE; x = TREE_CHAIN (x))
928 check_init (TREE_VALUE (x), before);
930 break;
932 case EXPR_WITH_FILE_LOCATION:
934 location_t saved_location = input_location;
935 tree saved_wfl = wfl;
936 tree body = EXPR_WFL_NODE (exp);
937 if (IS_EMPTY_STMT (body))
938 break;
939 wfl = exp;
940 #ifdef USE_MAPPED_LOCATION
941 input_location = EXPR_LOCATION (exp);
942 #else
943 input_filename = EXPR_WFL_FILENAME (exp);
944 input_line = EXPR_WFL_LINENO (exp);
945 #endif
946 check_init (body, before);
947 input_location = saved_location;
948 wfl = saved_wfl;
950 break;
952 default:
953 internal_error
954 ("internal error in check-init: tree code not implemented: %s",
955 tree_code_name [(int) TREE_CODE (exp)]);
959 void
960 check_for_initialization (tree body, tree mdecl)
962 tree decl;
963 word buf[2];
964 words before = buf;
965 tree owner = DECL_CONTEXT (mdecl);
966 int is_static_method = METHOD_STATIC (mdecl);
967 /* We don't need to check final fields of <init> it it calls this(). */
968 int is_finit_method = DECL_FINIT_P (mdecl) || DECL_INSTINIT_P (mdecl);
969 int is_init_method
970 = (is_finit_method || DECL_CLINIT_P (mdecl)
971 || (DECL_INIT_P (mdecl) && ! DECL_INIT_CALLS_THIS (mdecl)));
973 start_current_locals = num_current_locals = 0;
974 num_current_words = 2;
976 if (is_init_method)
978 int words_needed, i;
979 for (decl = TYPE_FIELDS (owner);
980 decl != NULL_TREE; decl = TREE_CHAIN (decl))
982 if (DECL_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
984 if (DECL_FIELD_FINAL_IUD (decl))
985 DECL_BIT_INDEX (decl) = -1;
986 else
987 DECL_BIT_INDEX (decl) = num_current_locals++;
990 words_needed = WORDS_NEEDED (2 * num_current_locals);
991 if (words_needed > 2)
993 num_current_words = words_needed;
994 before = ALLOC_WORDS(words_needed);
996 i = 0;
997 for (decl = TYPE_FIELDS (owner);
998 decl != NULL_TREE; decl = TREE_CHAIN (decl))
1000 if (FIELD_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
1002 if (! DECL_FIELD_FINAL_IUD (decl))
1004 CLEAR_ASSIGNED (before, i);
1005 SET_UNASSIGNED (before, i);
1006 i++;
1013 check_init (body, before);
1015 if (is_init_method)
1017 for (decl = TYPE_FIELDS (owner);
1018 decl != NULL_TREE; decl = TREE_CHAIN (decl))
1020 if (FIELD_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
1022 int index = DECL_BIT_INDEX (decl);
1023 if (index >= 0 && ! ASSIGNED_P (before, index))
1025 if (! is_finit_method)
1026 error ("%Jfinal field %qD may not have been initialized",
1027 decl, decl);
1029 else if (is_finit_method)
1030 DECL_FIELD_FINAL_IUD (decl) = 1;
1032 /* Re-set to initial state, since we later may use the
1033 same bit for DECL_POINTER_ALIAS_SET. */
1034 DECL_BIT_INDEX (decl) = -1;
1039 start_current_locals = num_current_locals = 0;