Merge from mainline
[official-gcc.git] / gcc / java / check-init.c
blob6eb1f162a7a4723fb6f67b3cd76f476b53b6fa18
1 /* Code to test for "definitive [un]assignment".
2 Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006 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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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 (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 #define COPYN(DST, SRC, NWORDS) memcpy (DST, SRC, NWORDS * sizeof(word))
81 #define COPY(DST, SRC) COPYN (DST, SRC, num_current_words)
83 #define SET_ALL(DST) memset (DST, ~0, num_current_words * sizeof(word))
84 #define CLEAR_ALL(DST) memset (DST, 0, num_current_words * sizeof(word))
86 #define INTERSECTN(DST, SRC1, SRC2, N) \
87 do { int n = N; \
88 while (--n >= 0) DST[n] = SRC1[n] & SRC2[n]; \
89 } while (0)
91 #define UNION(DST, SRC1, SRC2) \
92 UNIONN (DST, SRC1, SRC2, num_current_words)
94 #define UNIONN(DST, SRC1, SRC2, N) \
95 do { int n = N; \
96 while (--n >= 0) DST[n] = SRC1[n] | SRC2[n]; \
97 } while (0)
99 #define INTERSECT(DST, SRC1, SRC2) \
100 INTERSECTN (DST, SRC1, SRC2, num_current_words)
102 #define WORD_SIZE ((unsigned int)(sizeof(word) * BITS_PER_UNIT))
104 static void check_bool_init (tree, words, words, words);
105 static void check_init (tree, words);
106 static void check_cond_init (tree, tree, tree, words, words, words);
107 static void check_bool2_init (enum tree_code, tree, tree, words, words, words);
108 struct alternatives;
109 static void done_alternative (words, struct alternatives *);
110 static tree get_variable_decl (tree);
111 static void final_assign_error (tree);
112 static void check_final_reassigned (tree, words);
114 #define ALLOC_WORDS(NUM) (xmalloc ((NUM) * sizeof (word)))
115 #define FREE_WORDS(PTR) (free (PTR))
117 /* DECLARE_BUFFERS is used to allocate NUMBUFFER bit sets, each of
118 which is an array of length num_current_words number of words.
119 Declares a new local variable BUFFER to hold the result (or rather
120 a pointer to the first of the bit sets). In almost all cases
121 num_current_words will be 1 or at most 2, so we try to stack
122 allocate the arrays in that case, using a stack array
123 named BUFFER##_short. Each DECLARE_BUFFERS must be matched by
124 a corresponding RELEASE_BUFFERS to avoid memory leaks. */
126 #define DECLARE_BUFFERS(BUFFER, NUMBUFFERS) \
127 word BUFFER##_short[2 * NUMBUFFERS]; \
128 words BUFFER = ALLOC_BUFFER(BUFFER##_short, NUMBUFFERS * num_current_words)
130 #define RELEASE_BUFFERS(BUFFER) \
131 FREE_BUFFER(BUFFER, BUFFER##_short)
133 #define ALLOC_BUFFER(SHORTBUFFER, NUMWORDS) \
134 ((NUMWORDS) * sizeof(word) <= sizeof(SHORTBUFFER) ? SHORTBUFFER \
135 : ALLOC_WORDS(NUMWORDS))
137 #define FREE_BUFFER(BUFFER, SHORTBUFFER) \
138 if (BUFFER != SHORTBUFFER) FREE_WORDS(BUFFER)
140 #define SET_P(WORDS, BIT) \
141 (WORDS[(BIT) / WORD_SIZE] & (1 << ((BIT) % WORD_SIZE)))
143 #define CLEAR_BIT(WORDS, BIT) \
144 (WORDS[(BIT) / WORD_SIZE] &= ~ (1 << ((BIT) % WORD_SIZE)))
146 #define SET_BIT(WORDS, BIT) \
147 (WORDS[(BIT) / WORD_SIZE] |= (1 << ((BIT) % WORD_SIZE)))
149 #define WORDS_NEEDED(BITS) (((BITS)+(WORD_SIZE-1))/(WORD_SIZE))
151 #define ASSIGNED_P(WORDS, BIT) SET_P(WORDS, 2 * (BIT))
152 #define UNASSIGNED_P(WORDS, BIT) SET_P(WORDS, 2 * (BIT) + 1)
154 #define SET_ASSIGNED(WORDS, INDEX) SET_BIT (WORDS, 2 * (INDEX))
155 #define SET_UNASSIGNED(WORDS, INDEX) SET_BIT (WORDS, 2 * (INDEX) + 1)
157 #define CLEAR_ASSIGNED(WORDS, INDEX) CLEAR_BIT (WORDS, 2 * (INDEX))
158 #define CLEAR_UNASSIGNED(WORDS, INDEX) CLEAR_BIT (WORDS, 2 * (INDEX) + 1)
160 /* Get the "interesting" declaration from a MODIFY_EXPR or COMPONENT_REF.
161 Return the declaration or NULL_TREE if no interesting declaration. */
163 static tree
164 get_variable_decl (tree exp)
166 /* A static field can be wrapped in a COMPOUND_EXPR where the first
167 argument initializes the class. */
168 if (TREE_CODE (exp) == COMPOUND_EXPR)
169 exp = extract_field_decl (exp);
171 if (TREE_CODE (exp) == VAR_DECL)
173 if (! TREE_STATIC (exp) || FIELD_FINAL (exp))
174 return exp;
176 /* We only care about final parameters. */
177 else if (TREE_CODE (exp) == PARM_DECL)
179 if (DECL_FINAL (exp))
180 return exp;
182 /* See if exp is this.field. */
183 else if (TREE_CODE (exp) == COMPONENT_REF)
185 tree op0 = TREE_OPERAND (exp, 0);
186 tree op1 = TREE_OPERAND (exp, 1);
187 tree mdecl = current_function_decl;
188 if (TREE_CODE (op0) == INDIRECT_REF
189 && TREE_CODE (op1) == FIELD_DECL
190 && ! METHOD_STATIC (mdecl)
191 && FIELD_FINAL (op1))
193 op0 = TREE_OPERAND (op0, 0);
194 if (op0 == BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)))
195 return op1;
198 else if (TREE_CODE (exp) == INDIRECT_REF)
200 /* For indirect dispatch, look for an expression of the form
201 (indirect_ref (+ (array_ref otable <N>) this)).
202 FIXME: it would probably be better to generate a JAVA_FIELD_REF
203 expression that gets converted to OTABLE access at
204 gimplification time. */
205 exp = TREE_OPERAND (exp, 0);
206 if (TREE_CODE (exp) == PLUS_EXPR)
208 tree op0 = TREE_OPERAND (exp, 0);
209 STRIP_NOPS (op0);
210 if (TREE_CODE (op0) == ARRAY_REF)
212 tree table = TREE_OPERAND (op0, 0);
213 if (TREE_CODE (table) == VAR_DECL
214 && DECL_LANG_SPECIFIC (table)
215 && DECL_OWNER (table)
216 && TYPE_OTABLE_DECL (DECL_OWNER (table)) == table)
218 HOST_WIDE_INT index
219 = TREE_INT_CST_LOW (TREE_OPERAND (op0, 1));
220 tree otable_methods
221 = TYPE_OTABLE_METHODS (DECL_OWNER (table));
222 tree element;
223 for (element = otable_methods;
224 element;
225 element = TREE_CHAIN (element))
227 if (index == 1)
229 tree purpose = TREE_PURPOSE (element);
230 if (TREE_CODE (purpose) == FIELD_DECL)
231 return purpose;
232 else
233 return NULL_TREE;
235 --index;
242 return NULL_TREE;
245 static void
246 final_assign_error (tree name)
248 error ("Can't reassign a value to the final variable %qs",
249 IDENTIFIER_POINTER (name));
252 static void
253 check_final_reassigned (tree decl, words before)
255 int index = DECL_BIT_INDEX (decl);
256 /* A final local already assigned or a final parameter
257 assigned must be reported as errors */
258 if (DECL_FINAL (decl) && index != -2
259 && (index < loop_current_locals /* I.e. -1, or outside current loop. */
260 || (DECL_LOCAL_FINAL_IUD (decl) ? ASSIGNED_P (before, index)
261 : ! UNASSIGNED_P (before, index))))
263 final_assign_error (DECL_NAME (decl));
267 /* Check a conditional form (TEST_EXP ? THEN_EXP : ELSE_EXP) for
268 definite [un]assignment.
269 BEFORE, WHEN_FALSE, and WHEN_TRUE are as in check_bool_init. */
271 static void
272 check_cond_init (tree test_exp, tree then_exp, tree else_exp,
273 words before, words when_false, words when_true)
275 int save_start_current_locals = start_current_locals;
276 DECLARE_BUFFERS(test_false, 6);
277 words test_true = test_false + num_current_words;
278 words then_false = test_true + num_current_words;
279 words then_true = then_false + num_current_words;
280 words else_false = then_true + num_current_words;
281 words else_true = else_false + num_current_words;
282 start_current_locals = num_current_locals;
284 check_bool_init (test_exp, before, test_false, test_true);
285 check_bool_init (then_exp, test_true, then_false, then_true);
286 check_bool_init (else_exp, test_false, else_false, else_true);
287 INTERSECT (when_false, then_false, else_false);
288 INTERSECT (when_true, then_true, else_true);
289 RELEASE_BUFFERS(test_false);
290 start_current_locals = save_start_current_locals;
293 /* Check a boolean binary form CODE (EXP0, EXP1),
294 where CODE is one of EQ_EXPR, BIT_AND_EXPR, or BIT_IOR_EXPR.
295 BEFORE, WHEN_FALSE, and WHEN_TRUE are as in check_bool_init. */
297 static void
298 check_bool2_init (enum tree_code code, tree exp0, tree exp1,
299 words before, words when_false, words when_true)
301 word buf[2*4];
302 words tmp = num_current_words <= 2 ? buf
303 : ALLOC_WORDS (4 * num_current_words);
304 words when_false_0 = tmp;
305 words when_false_1 = tmp+num_current_words;
306 words when_true_0 = tmp+2*num_current_words;
307 words when_true_1 = tmp+3*num_current_words;
308 check_bool_init (exp0, before, when_false_0, when_true_0);
309 INTERSECT (before, when_false_0, when_true_0);
310 check_bool_init (exp1, before, when_false_1, when_true_1);
312 INTERSECT (before, when_false_1, when_true_1);
314 if (code == EQ_EXPR)
316 /* Now set:
317 * when_true = (when_false_1 INTERSECTION when_true_1)
318 * UNION (when_true_0 INTERSECTION when_false_1)
319 * UNION (when_false_0 INTERSECTION when_true_1);
320 * using when_false and before as temporary working areas. */
321 INTERSECT (when_true, when_true_0, when_false_1);
322 INTERSECT (when_false, when_true_0, when_false_1);
323 UNION (when_true, when_true, when_false);
324 UNION (when_true, when_true, before);
326 /* Now set:
327 * when_false = (when_false_1 INTERSECTION when_true_1)
328 * UNION (when_true_0 INTERSECTION when_true_1)
329 * UNION (when_false_0 INTERSECTION when_false_1);
330 * using before as a temporary working area. */
331 INTERSECT (when_false, when_true_0, when_true_1);
332 UNION (when_false, when_false, before);
333 INTERSECT (before, when_false_0, when_false_1);
334 UNION (when_false, when_false, before);
336 else if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
338 UNION (when_true, when_true_0, when_true_1);
339 INTERSECT (when_false, when_false_0, when_false_1);
340 UNION (when_false, when_false, before);
342 else /* if (code == BIT_IOR_EXPR || code == TRUTH_OR_EXPR) */
344 UNION (when_false, when_false_0, when_false_1);
345 INTERSECT (when_true, when_true_0, when_true_1);
346 UNION (when_true, when_true, before);
349 if (tmp != buf)
350 FREE_WORDS (tmp);
353 /* Check a boolean expression EXP for definite [un]assignment.
354 BEFORE is the set of variables definitely [un]assigned before the
355 conditional. (This bitstring may be modified arbitrarily in this function.)
356 On output, WHEN_FALSE is the set of variables [un]definitely assigned after
357 the conditional when the conditional is false.
358 On output, WHEN_TRUE is the set of variables definitely [un]assigned after
359 the conditional when the conditional is true.
360 (WHEN_FALSE and WHEN_TRUE are overwritten with initial values ignored.)
361 (None of BEFORE, WHEN_FALSE, or WHEN_TRUE can overlap, as they may
362 be used as temporary working areas. */
364 static void
365 check_bool_init (tree exp, words before, words when_false, words when_true)
367 switch (TREE_CODE (exp))
369 case COND_EXPR:
370 check_cond_init (TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
371 TREE_OPERAND (exp, 2),
372 before, when_false, when_true);
373 return;
375 case TRUTH_ANDIF_EXPR:
376 check_cond_init (TREE_OPERAND (exp, 0),
377 TREE_OPERAND (exp, 1), boolean_false_node,
378 before, when_false, when_true);
379 return;
380 case TRUTH_ORIF_EXPR:
381 check_cond_init (TREE_OPERAND (exp, 0),
382 boolean_true_node, TREE_OPERAND (exp, 1),
383 before, when_false, when_true);
384 return;
385 case TRUTH_NOT_EXPR:
386 check_bool_init (TREE_OPERAND (exp, 0), before, when_true, when_false);
387 return;
389 case BIT_AND_EXPR:
390 case BIT_IOR_EXPR:
391 case TRUTH_AND_EXPR:
392 case TRUTH_OR_EXPR:
393 case EQ_EXPR:
394 check_bool2_init (TREE_CODE (exp),
395 TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
396 before, when_false, when_true);
397 return;
399 case TRUTH_XOR_EXPR:
400 case BIT_XOR_EXPR:
401 case NE_EXPR:
402 /* Just like EQ_EXPR, but switch when_true and when_false. */
403 check_bool2_init (EQ_EXPR, TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
404 before, when_true, when_false);
406 return;
408 case INTEGER_CST:
409 if (integer_zerop (exp))
411 SET_ALL (when_true);
412 COPY (when_false, before);
414 else
416 SET_ALL (when_false);
417 COPY (when_true, before);
419 break;
421 default:
422 check_init (exp, before);
423 COPY (when_false, before);
424 COPY (when_true, before);
428 /* Used to keep track of control flow branches. */
430 struct alternatives
432 struct alternatives *outer;
434 /* The value of num_current_locals at the start of this compound. */
435 int num_locals;
437 /* The value of the "before" set at the start of the control structure.
438 Used for SWITCH_EXPR but not set for LABELED_BLOCK_EXPR. */
439 words saved;
441 int save_start_current_locals;
443 /* If num_current_words==1, combined==&one_word, for efficiency. */
444 word one_word;
446 /* The intersection of the "after" sets from previous branches. */
447 words combined;
449 tree block;
452 struct alternatives * alternatives = NULL;
454 /* Begin handling a control flow branch.
455 BEFORE is the state of [un]assigned variables on entry.
456 CURRENT is a struct alt to manage the branch alternatives. */
458 #define BEGIN_ALTERNATIVES(before, current) \
460 current.saved = NULL; \
461 current.num_locals = num_current_locals; \
462 current.combined = num_current_words <= 1 ? &current.one_word \
463 : ALLOC_WORDS (num_current_words); \
464 SET_ALL (current.combined); \
465 current.outer = alternatives; \
466 alternatives = &current; \
467 current.save_start_current_locals = start_current_locals; \
468 start_current_locals = num_current_locals; \
471 /* We have finished with one branch of branching control flow.
472 Store the [un]assigned state, merging (intersecting) it with the state
473 of previous alternative branches. */
475 static void
476 done_alternative (words after, struct alternatives *current)
478 INTERSECTN (current->combined, current->combined, after,
479 WORDS_NEEDED (2 * current->num_locals));
482 /* Used when we done with a control flow branch and are all merged again.
483 * AFTER is the merged state of [un]assigned variables,
484 CURRENT is a struct alt that was passed to BEGIN_ALTERNATIVES. */
486 #define END_ALTERNATIVES(after, current) \
488 alternatives = current.outer; \
489 COPY (after, current.combined); \
490 if (current.combined != &current.one_word) \
491 FREE_WORDS (current.combined); \
492 start_current_locals = current.save_start_current_locals; \
495 /* Check for (un)initialized local variables in EXP. */
497 static void
498 check_init (tree exp, words before)
500 tree tmp;
501 location_t save_location = input_location;
502 again:
503 if (EXPR_HAS_LOCATION (exp))
504 input_location = EXPR_LOCATION (exp);
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 error ("variable %qD may not have been initialized", exp);
519 DECL_BIT_INDEX (exp) = -2;
522 break;
524 case COMPONENT_REF:
525 check_init (TREE_OPERAND (exp, 0), before);
526 if ((tmp = get_variable_decl (exp)) != NULL_TREE)
528 int index = DECL_BIT_INDEX (tmp);
529 if (index >= 0 && ! ASSIGNED_P (before, index))
531 error ("variable %qD may not have been initialized", tmp);
532 /* Suppress further errors. */
533 DECL_BIT_INDEX (tmp) = -2;
536 break;
538 case MODIFY_EXPR:
539 tmp = TREE_OPERAND (exp, 0);
540 /* We're interested in variable declaration and parameter
541 declaration when they're declared with the `final' modifier. */
542 if ((tmp = get_variable_decl (tmp)) != NULL_TREE)
544 int index;
545 check_init (TREE_OPERAND (exp, 1), before);
546 check_final_reassigned (tmp, before);
547 index = DECL_BIT_INDEX (tmp);
548 if (index >= 0)
550 SET_ASSIGNED (before, index);
551 CLEAR_UNASSIGNED (before, index);
553 /* Minor optimization. See comment for start_current_locals.
554 If we're optimizing for class initialization, we keep
555 this information to check whether the variable is
556 definitely assigned when once we checked the whole
557 function. */
558 if (! STATIC_CLASS_INIT_OPT_P () /* FIXME */
559 && ! DECL_FINAL (tmp)
560 && index >= start_current_locals
561 && index == num_current_locals - 1)
563 num_current_locals--;
564 DECL_BIT_INDEX (tmp) = -1;
566 break;
568 else if (TREE_CODE (tmp = TREE_OPERAND (exp, 0)) == COMPONENT_REF)
570 tree decl;
571 check_init (tmp, before);
572 check_init (TREE_OPERAND (exp, 1), before);
573 decl = TREE_OPERAND (tmp, 1);
574 if (DECL_FINAL (decl))
575 final_assign_error (DECL_NAME (decl));
576 break;
578 else if (TREE_CODE (tmp) == COMPONENT_REF && IS_ARRAY_LENGTH_ACCESS (tmp))
580 /* We can't emit a more specific message here, because when
581 compiling to bytecodes we don't get here. */
582 final_assign_error (length_identifier_node);
584 else
585 goto binop;
586 case BLOCK:
587 if (BLOCK_EXPR_BODY (exp))
589 tree decl = BLOCK_EXPR_DECLS (exp);
590 int words_needed;
591 word* tmp;
592 int i;
593 int save_start_current_locals = start_current_locals;
594 int save_num_current_words = num_current_words;
595 start_current_locals = num_current_locals;
596 for (; decl != NULL_TREE; decl = TREE_CHAIN (decl))
598 DECL_BIT_INDEX (decl) = num_current_locals++;
600 words_needed = WORDS_NEEDED (2 * num_current_locals);
601 if (words_needed > num_current_words)
603 tmp = ALLOC_WORDS (words_needed);
604 COPY (tmp, before);
605 num_current_words = words_needed;
607 else
608 tmp = before;
609 for (i = start_current_locals; i < num_current_locals; i++)
611 CLEAR_ASSIGNED (tmp, i);
612 SET_UNASSIGNED (tmp, i);
614 check_init (BLOCK_EXPR_BODY (exp), tmp);
616 /* Re-set DECL_BIT_INDEX since it is also DECL_POINTER_ALIAS_SET. */
617 for (decl = BLOCK_EXPR_DECLS (exp);
618 decl != NULL_TREE; decl = TREE_CHAIN (decl))
620 if (LOCAL_CLASS_INITIALIZATION_FLAG_P (decl))
622 int index = DECL_BIT_INDEX (decl);
623 tree fndecl = DECL_CONTEXT (decl);
624 if (fndecl && METHOD_STATIC (fndecl)
625 && (DECL_INITIAL (decl) == boolean_true_node
626 || (index >= 0 && ASSIGNED_P (tmp, index))))
627 *(htab_find_slot
628 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl),
629 DECL_FUNCTION_INIT_TEST_CLASS (decl), INSERT)) =
630 DECL_FUNCTION_INIT_TEST_CLASS (decl);
632 DECL_BIT_INDEX (decl) = -1;
635 num_current_locals = start_current_locals;
636 start_current_locals = save_start_current_locals;
637 if (tmp != before)
639 num_current_words = save_num_current_words;
640 COPY (before, tmp);
641 FREE_WORDS (tmp);
644 break;
645 case LOOP_EXPR:
647 /* The JLS 2nd edition discusses a complication determining
648 definite unassignment of loop statements. They define a
649 "hypothetical" analysis model. We do something much
650 simpler: We just disallow assignments inside loops to final
651 variables declared outside the loop. This means we may
652 disallow some contrived assignments that the JLS, but I
653 can't see how anything except a very contrived testcase (a
654 do-while whose condition is false?) would care. */
656 struct alternatives alt;
657 int save_loop_current_locals = loop_current_locals;
658 int save_start_current_locals = start_current_locals;
659 loop_current_locals = num_current_locals;
660 start_current_locals = num_current_locals;
661 BEGIN_ALTERNATIVES (before, alt);
662 alt.block = exp;
663 check_init (TREE_OPERAND (exp, 0), before);
664 END_ALTERNATIVES (before, alt);
665 loop_current_locals = save_loop_current_locals;
666 start_current_locals = save_start_current_locals;
667 break;
669 case EXIT_EXPR:
671 struct alternatives *alt = alternatives;
672 DECLARE_BUFFERS(when_true, 2);
673 words when_false = when_true + num_current_words;
674 #ifdef ENABLE_JC1_CHECKING
675 gcc_assert (TREE_CODE (alt->block) == LOOP_EXPR);
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 break;
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 break;
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 break;
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 break;
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 break;
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 break;
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 break;
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 VIEW_CONVERT_EXPR:
810 case BIT_FIELD_REF:
811 case FLOAT_EXPR:
812 case FIX_TRUNC_EXPR:
813 case INDIRECT_REF:
814 case ADDR_EXPR:
815 case NON_LVALUE_EXPR:
816 case INSTANCEOF_EXPR:
817 case FIX_CEIL_EXPR:
818 case FIX_FLOOR_EXPR:
819 case FIX_ROUND_EXPR:
820 case ABS_EXPR:
821 /* Avoid needless recursion. */
822 exp = TREE_OPERAND (exp, 0);
823 goto again;
825 case PREDECREMENT_EXPR:
826 case PREINCREMENT_EXPR:
827 case POSTDECREMENT_EXPR:
828 case POSTINCREMENT_EXPR:
829 tmp = get_variable_decl (TREE_OPERAND (exp, 0));
830 if (tmp != NULL_TREE && DECL_FINAL (tmp))
831 final_assign_error (DECL_NAME (tmp));
832 else if (TREE_CODE (tmp = TREE_OPERAND (exp, 0)) == COMPONENT_REF)
834 /* Take care of array length accesses too. */
835 tree decl = TREE_OPERAND (tmp, 1);
836 if (DECL_FINAL (decl))
837 final_assign_error (DECL_NAME (decl));
840 /* Avoid needless recursion. */
841 exp = TREE_OPERAND (exp, 0);
842 goto again;
844 case SAVE_EXPR:
845 if (IS_INIT_CHECKED (exp))
846 break;
847 IS_INIT_CHECKED (exp) = 1;
848 exp = TREE_OPERAND (exp, 0);
849 goto again;
851 case COMPOUND_EXPR:
852 case PLUS_EXPR:
853 case MINUS_EXPR:
854 case MULT_EXPR:
855 case TRUNC_DIV_EXPR:
856 case TRUNC_MOD_EXPR:
857 case RDIV_EXPR:
858 case LSHIFT_EXPR:
859 case RSHIFT_EXPR:
860 case URSHIFT_EXPR:
861 case BIT_AND_EXPR:
862 case BIT_XOR_EXPR:
863 case BIT_IOR_EXPR:
864 case EQ_EXPR:
865 case NE_EXPR:
866 case GT_EXPR:
867 case GE_EXPR:
868 case LT_EXPR:
869 case LE_EXPR:
870 case MAX_EXPR:
871 case MIN_EXPR:
872 case ARRAY_REF:
873 case LROTATE_EXPR:
874 case RROTATE_EXPR:
875 case CEIL_DIV_EXPR:
876 case FLOOR_DIV_EXPR:
877 case ROUND_DIV_EXPR:
878 case CEIL_MOD_EXPR:
879 case FLOOR_MOD_EXPR:
880 case ROUND_MOD_EXPR:
881 case EXACT_DIV_EXPR:
882 case UNLT_EXPR:
883 case UNLE_EXPR:
884 case UNGT_EXPR:
885 case UNGE_EXPR:
886 case UNEQ_EXPR:
887 case LTGT_EXPR:
888 binop:
889 check_init (TREE_OPERAND (exp, 0), before);
890 /* Avoid needless recursion, especially for COMPOUND_EXPR. */
891 exp = TREE_OPERAND (exp, 1);
892 goto again;
894 case RESULT_DECL:
895 case FUNCTION_DECL:
896 case INTEGER_CST:
897 case REAL_CST:
898 case STRING_CST:
899 case DECL_EXPR:
900 case JAVA_EXC_OBJ_EXPR:
901 break;
903 case NEW_CLASS_EXPR:
904 case CALL_EXPR:
906 tree func = TREE_OPERAND (exp, 0);
907 tree x = TREE_OPERAND (exp, 1);
908 if (TREE_CODE (func) == ADDR_EXPR)
909 func = TREE_OPERAND (func, 0);
910 check_init (func, before);
912 for ( ; x != NULL_TREE; x = TREE_CHAIN (x))
913 check_init (TREE_VALUE (x), before);
914 if (func == throw_node)
915 goto never_continues;
917 break;
919 case NEW_ARRAY_INIT:
921 tree value;
922 unsigned HOST_WIDE_INT idx;
923 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (TREE_OPERAND (exp, 0)),
924 idx, value)
925 check_init (value, before);
927 break;
929 case EXPR_WITH_FILE_LOCATION:
931 location_t saved_location = input_location;
932 tree body = EXPR_WFL_NODE (exp);
933 if (IS_EMPTY_STMT (body))
934 break;
935 #ifdef USE_MAPPED_LOCATION
936 input_location = EXPR_LOCATION (exp);
937 #else
938 input_filename = EXPR_WFL_FILENAME (exp);
939 input_line = EXPR_WFL_LINENO (exp);
940 #endif
941 check_init (body, before);
942 input_location = saved_location;
944 break;
946 default:
947 internal_error
948 ("internal error in check-init: tree code not implemented: %s",
949 tree_code_name [(int) TREE_CODE (exp)]);
951 input_location = save_location;
954 void
955 check_for_initialization (tree body, tree mdecl)
957 tree decl;
958 word buf[2];
959 words before = buf;
960 tree owner = DECL_CONTEXT (mdecl);
961 int is_static_method = METHOD_STATIC (mdecl);
962 /* We don't need to check final fields of <init> it it calls this(). */
963 int is_finit_method = DECL_FINIT_P (mdecl) || DECL_INSTINIT_P (mdecl);
964 int is_init_method
965 = (is_finit_method || DECL_CLINIT_P (mdecl)
966 || (DECL_INIT_P (mdecl) && ! DECL_INIT_CALLS_THIS (mdecl)));
968 start_current_locals = num_current_locals = 0;
969 num_current_words = 2;
971 if (is_init_method)
973 int words_needed, i;
974 for (decl = TYPE_FIELDS (owner);
975 decl != NULL_TREE; decl = TREE_CHAIN (decl))
977 if (DECL_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
979 if (DECL_FIELD_FINAL_IUD (decl))
980 DECL_BIT_INDEX (decl) = -1;
981 else
982 DECL_BIT_INDEX (decl) = num_current_locals++;
985 words_needed = WORDS_NEEDED (2 * num_current_locals);
986 if (words_needed > 2)
988 num_current_words = words_needed;
989 before = ALLOC_WORDS(words_needed);
991 i = 0;
992 for (decl = TYPE_FIELDS (owner);
993 decl != NULL_TREE; decl = TREE_CHAIN (decl))
995 if (FIELD_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
997 if (! DECL_FIELD_FINAL_IUD (decl))
999 CLEAR_ASSIGNED (before, i);
1000 SET_UNASSIGNED (before, i);
1001 i++;
1008 check_init (body, before);
1010 if (is_init_method)
1012 for (decl = TYPE_FIELDS (owner);
1013 decl != NULL_TREE; decl = TREE_CHAIN (decl))
1015 if (FIELD_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
1017 int index = DECL_BIT_INDEX (decl);
1018 if (index >= 0 && ! ASSIGNED_P (before, index))
1020 if (! is_finit_method)
1021 error ("%Jfinal field %qD may not have been initialized",
1022 decl, decl);
1024 else if (is_finit_method)
1025 DECL_FIELD_FINAL_IUD (decl) = 1;
1027 /* Re-set to initial state, since we later may use the
1028 same bit for DECL_POINTER_ALIAS_SET. */
1029 DECL_BIT_INDEX (decl) = -1;
1034 start_current_locals = num_current_locals = 0;