1 /* Code to test for "definitive [un]assignment".
2 Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005 Free Software Foundation,
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)
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. */
30 #include "coretypes.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. */
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
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) \
88 while (--n >= 0) DST[n] = SRC1[n] & SRC2[n]; \
91 #define UNION(DST, SRC1, SRC2) \
92 UNIONN (DST, SRC1, SRC2, num_current_words)
94 #define UNIONN(DST, SRC1, SRC2, N) \
96 while (--n >= 0) DST[n] = SRC1[n] | SRC2[n]; \
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
);
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. */
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
))
176 /* We only care about final parameters. */
177 else if (TREE_CODE (exp
) == PARM_DECL
)
179 if (DECL_FINAL (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
)))
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);
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
)
219 = TREE_INT_CST_LOW (TREE_OPERAND (op0
, 1));
221 = TYPE_OTABLE_METHODS (DECL_OWNER (table
));
223 for (element
= otable_methods
;
225 element
= TREE_CHAIN (element
))
229 tree purpose
= TREE_PURPOSE (element
);
230 if (TREE_CODE (purpose
) == FIELD_DECL
)
246 final_assign_error (tree name
)
248 error ("Can't reassign a value to the final variable %qs",
249 IDENTIFIER_POINTER (name
));
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. */
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. */
298 check_bool2_init (enum tree_code code
, tree exp0
, tree exp1
,
299 words before
, words when_false
, words when_true
)
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
);
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
);
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
);
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. */
365 check_bool_init (tree exp
, words before
, words when_false
, words when_true
)
367 switch (TREE_CODE (exp
))
370 check_cond_init (TREE_OPERAND (exp
, 0), TREE_OPERAND (exp
, 1),
371 TREE_OPERAND (exp
, 2),
372 before
, when_false
, when_true
);
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
);
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
);
386 check_bool_init (TREE_OPERAND (exp
, 0), before
, when_true
, when_false
);
394 check_bool2_init (TREE_CODE (exp
),
395 TREE_OPERAND (exp
, 0), TREE_OPERAND (exp
, 1),
396 before
, when_false
, when_true
);
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
);
409 if (integer_zerop (exp
))
412 COPY (when_false
, before
);
416 SET_ALL (when_false
);
417 COPY (when_true
, before
);
422 check_init (exp
, before
);
423 COPY (when_false
, before
);
424 COPY (when_true
, before
);
428 /* Used to keep track of control flow branches. */
432 struct alternatives
*outer
;
434 /* The value of num_current_locals at the start of this compound. */
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. */
441 int save_start_current_locals
;
443 /* If num_current_words==1, combined==&one_word, for efficiency. */
446 /* The intersection of the "after" sets from previous branches. */
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 ? ¤t.one_word \
463 : ALLOC_WORDS (num_current_words); \
464 SET_ALL (current.combined); \
465 current.outer = alternatives; \
466 alternatives = ¤t; \
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. */
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 != ¤t.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. */
498 check_init (tree exp
, words before
)
501 location_t save_location
= input_location
;
503 if (EXPR_HAS_LOCATION (exp
))
504 input_location
= EXPR_LOCATION (exp
);
505 switch (TREE_CODE (exp
))
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;
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;
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
)
545 check_init (TREE_OPERAND (exp
, 1), before
);
546 check_final_reassigned (tmp
, before
);
547 index
= DECL_BIT_INDEX (tmp
);
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
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;
568 else if (TREE_CODE (tmp
= TREE_OPERAND (exp
, 0)) == COMPONENT_REF
)
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
));
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
);
587 if (BLOCK_EXPR_BODY (exp
))
589 tree decl
= BLOCK_EXPR_DECLS (exp
);
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
);
605 num_current_words
= words_needed
;
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
))))
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
;
639 num_current_words
= save_num_current_words
;
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
);
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
;
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 if (TREE_CODE (alt
->block
) != LOOP_EXPR
)
678 check_bool_init (TREE_OPERAND (exp
, 0), before
, when_false
, when_true
);
679 done_alternative (when_true
, alt
);
680 COPY (before
, when_false
);
681 RELEASE_BUFFERS(when_true
);
684 case LABELED_BLOCK_EXPR
:
686 struct alternatives alt
;
687 BEGIN_ALTERNATIVES (before
, alt
);
689 if (LABELED_BLOCK_BODY (exp
))
690 check_init (LABELED_BLOCK_BODY (exp
), before
);
691 done_alternative (before
, &alt
);
692 END_ALTERNATIVES (before
, alt
);
695 case EXIT_BLOCK_EXPR
:
697 tree block
= TREE_OPERAND (exp
, 0);
698 struct alternatives
*alt
= alternatives
;
699 while (alt
->block
!= block
)
701 done_alternative (before
, alt
);
707 struct alternatives alt
;
709 check_init (TREE_OPERAND (exp
, 0), before
);
710 BEGIN_ALTERNATIVES (before
, alt
);
711 alt
.saved
= ALLOC_BUFFER(buf
, num_current_words
);
712 COPY (alt
.saved
, before
);
714 check_init (TREE_OPERAND (exp
, 1), before
);
715 done_alternative (before
, &alt
);
716 if (! SWITCH_HAS_DEFAULT (exp
))
717 done_alternative (alt
.saved
, &alt
);
718 FREE_BUFFER(alt
.saved
, buf
);
719 END_ALTERNATIVES (before
, alt
);
726 struct alternatives
*alt
= alternatives
;
727 while (TREE_CODE (alt
->block
) != SWITCH_EXPR
)
729 COPYN (before
, alt
->saved
, WORDS_NEEDED (2 * alt
->num_locals
));
730 for (i
= alt
->num_locals
; i
< num_current_locals
; i
++)
731 CLEAR_ASSIGNED (before
, i
);
737 tree try_clause
= TREE_OPERAND (exp
, 0);
738 tree clause
= TREE_OPERAND (exp
, 1);
740 words tmp
= (num_current_words
<= 2 ? buf
741 : ALLOC_WORDS (2 * num_current_words
));
742 words save
= tmp
+ num_current_words
;
743 struct alternatives alt
;
744 BEGIN_ALTERNATIVES (before
, alt
);
747 check_init (try_clause
, tmp
);
748 done_alternative (tmp
, &alt
);
749 for ( ; clause
!= NULL_TREE
; clause
= TREE_CHAIN (clause
))
751 tree catch_clause
= TREE_OPERAND (clause
, 0);
753 check_init (catch_clause
, tmp
);
754 done_alternative (tmp
, &alt
);
760 END_ALTERNATIVES (before
, alt
);
764 case TRY_FINALLY_EXPR
:
766 DECLARE_BUFFERS(tmp
, 1);
768 check_init (TREE_OPERAND (exp
, 0), before
);
769 check_init (TREE_OPERAND (exp
, 1), tmp
);
770 UNION (before
, before
, tmp
);
771 RELEASE_BUFFERS(tmp
);
777 if (TREE_OPERAND (exp
, 0))
778 check_init (TREE_OPERAND (exp
, 0), before
);
779 goto never_continues
;
787 case TRUTH_ANDIF_EXPR
:
788 case TRUTH_ORIF_EXPR
:
790 DECLARE_BUFFERS(when_true
, 2);
791 words when_false
= when_true
+ num_current_words
;
792 check_bool_init (exp
, before
, when_false
, when_true
);
793 INTERSECT (before
, when_false
, when_true
);
794 RELEASE_BUFFERS(when_true
);
799 if (IS_EMPTY_STMT (exp
))
801 /* ... else fall through ... */
802 case UNARY_PLUS_EXPR
:
815 case NON_LVALUE_EXPR
:
816 case INSTANCEOF_EXPR
:
821 /* Avoid needless recursion. */
822 exp
= TREE_OPERAND (exp
, 0);
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);
845 if (IS_INIT_CHECKED (exp
))
847 IS_INIT_CHECKED (exp
) = 1;
848 exp
= TREE_OPERAND (exp
, 0);
889 check_init (TREE_OPERAND (exp
, 0), before
);
890 /* Avoid needless recursion, especially for COMPOUND_EXPR. */
891 exp
= TREE_OPERAND (exp
, 1);
900 case JAVA_EXC_OBJ_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
;
921 tree x
= CONSTRUCTOR_ELTS (TREE_OPERAND (exp
, 0));
922 for ( ; x
!= NULL_TREE
; x
= TREE_CHAIN (x
))
923 check_init (TREE_VALUE (x
), before
);
927 case EXPR_WITH_FILE_LOCATION
:
929 location_t saved_location
= input_location
;
930 tree body
= EXPR_WFL_NODE (exp
);
931 if (IS_EMPTY_STMT (body
))
933 #ifdef USE_MAPPED_LOCATION
934 input_location
= EXPR_LOCATION (exp
);
936 input_filename
= EXPR_WFL_FILENAME (exp
);
937 input_line
= EXPR_WFL_LINENO (exp
);
939 check_init (body
, before
);
940 input_location
= saved_location
;
946 ("internal error in check-init: tree code not implemented: %s",
947 tree_code_name
[(int) TREE_CODE (exp
)]);
949 input_location
= save_location
;
953 check_for_initialization (tree body
, tree mdecl
)
958 tree owner
= DECL_CONTEXT (mdecl
);
959 int is_static_method
= METHOD_STATIC (mdecl
);
960 /* We don't need to check final fields of <init> it it calls this(). */
961 int is_finit_method
= DECL_FINIT_P (mdecl
) || DECL_INSTINIT_P (mdecl
);
963 = (is_finit_method
|| DECL_CLINIT_P (mdecl
)
964 || (DECL_INIT_P (mdecl
) && ! DECL_INIT_CALLS_THIS (mdecl
)));
966 start_current_locals
= num_current_locals
= 0;
967 num_current_words
= 2;
972 for (decl
= TYPE_FIELDS (owner
);
973 decl
!= NULL_TREE
; decl
= TREE_CHAIN (decl
))
975 if (DECL_FINAL (decl
) && FIELD_STATIC (decl
) == is_static_method
)
977 if (DECL_FIELD_FINAL_IUD (decl
))
978 DECL_BIT_INDEX (decl
) = -1;
980 DECL_BIT_INDEX (decl
) = num_current_locals
++;
983 words_needed
= WORDS_NEEDED (2 * num_current_locals
);
984 if (words_needed
> 2)
986 num_current_words
= words_needed
;
987 before
= ALLOC_WORDS(words_needed
);
990 for (decl
= TYPE_FIELDS (owner
);
991 decl
!= NULL_TREE
; decl
= TREE_CHAIN (decl
))
993 if (FIELD_FINAL (decl
) && FIELD_STATIC (decl
) == is_static_method
)
995 if (! DECL_FIELD_FINAL_IUD (decl
))
997 CLEAR_ASSIGNED (before
, i
);
998 SET_UNASSIGNED (before
, i
);
1006 check_init (body
, before
);
1010 for (decl
= TYPE_FIELDS (owner
);
1011 decl
!= NULL_TREE
; decl
= TREE_CHAIN (decl
))
1013 if (FIELD_FINAL (decl
) && FIELD_STATIC (decl
) == is_static_method
)
1015 int index
= DECL_BIT_INDEX (decl
);
1016 if (index
>= 0 && ! ASSIGNED_P (before
, index
))
1018 if (! is_finit_method
)
1019 error ("%Jfinal field %qD may not have been initialized",
1022 else if (is_finit_method
)
1023 DECL_FIELD_FINAL_IUD (decl
) = 1;
1025 /* Re-set to initial state, since we later may use the
1026 same bit for DECL_POINTER_ALIAS_SET. */
1027 DECL_BIT_INDEX (decl
) = -1;
1032 start_current_locals
= num_current_locals
= 0;