1 /* Code to test for "definitive [un]assignment".
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GNU CC; see the file COPYING. If not, write to
16 the Free Software Foundation, 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
19 Java and all Java-based marks are trademarks or registered trademarks
20 of Sun Microsystems, Inc. in the United States and other countries.
21 The Free Software Foundation is independent of Sun Microsystems, Inc. */
23 /* Written by Per Bothner <bothner@cygnus.com>, January 1999. */
28 #include "flags.h" /* Needed for optimize. */
29 #include "java-tree.h"
30 #include "toplev.h" /* Needed for fatal. */
32 /* The basic idea is that we assign each local variable declaration
33 and each blank final field an index, and then we pass around
34 bitstrings, where the (2*i)'th bit is set if decl whose DECL_BIT_INDEX
35 is i is definitely assigned, and the the (2*i=1)'th bit is set if
36 decl whose DECL_BIT_INDEX is i is definitely unassigned */
38 /* One segment of a bitstring. */
39 typedef unsigned int word
;
41 /* Pointer to a bitstring. */
44 /* Number of locals variables currently active. */
45 static int num_current_locals
= 0;
47 /* The value of num_current_locals when we entered the closest
48 enclosing LOOP_EXPR. */
49 static int loop_current_locals
;
51 /* The index of the first local variable in the current block.
53 The variables whose DECL_BIT_INDEX are in the range from
54 start_current_locals (inclusive) up to num_current_locals (exclusive)
55 are declared in the "current" block. If there is a loop or branch
56 form, we set start_current_locals to num_current_locals to indicate
57 there is no current block.
59 The point is that if a variable in the current block is set,
60 there are no other control paths that we have to worry about.
61 Hence, we can remove it from the set of variables we are
62 checking, making its bit index available for some other variable.
63 For simplicity, we only do that if the variable's bit index
64 is (num_current_locals-1); freeing up its bit index is then
65 just a simple matter of decrementing num_current_locals.
66 The reason this is worth doing is that it is simple, and
67 allows us to use short (usually one-word) bit-strings,
68 even for methods with thousands of local variables, as
69 long as most of them are initialized immediately after or in
71 static int start_current_locals
= 0;
73 static int num_current_words
;
77 #define COPYN(DST, SRC, NWORDS) memcpy (DST, SRC, NWORDS * sizeof(word))
78 #define COPY(DST, SRC) COPYN (DST, SRC, num_current_words)
80 #define SET_ALL(DST) memset (DST, ~0, num_current_words * sizeof(word))
81 #define CLEAR_ALL(DST) memset (DST, 0, num_current_words * sizeof(word))
83 #define INTERSECTN(DST, SRC1, SRC2, N) \
85 while (--n >= 0) DST[n] = SRC1[n] & SRC2[n]; \
88 #define UNION(DST, SRC1, SRC2) \
89 UNIONN (DST, SRC1, SRC2, num_current_words)
91 #define UNIONN(DST, SRC1, SRC2, N) \
93 while (--n >= 0) DST[n] = SRC1[n] | SRC2[n]; \
96 #define INTERSECT(DST, SRC1, SRC2) \
97 INTERSECTN (DST, SRC1, SRC2, num_current_words)
99 #define WORD_SIZE ((unsigned int)(sizeof(word) * BITS_PER_UNIT))
101 static void check_bool_init
PARAMS ((tree
, words
, words
, words
));
102 static void check_init
PARAMS ((tree
, words
));
103 static void check_cond_init
PARAMS ((tree
, tree
, tree
, words
, words
, words
));
104 static void check_bool2_init
PARAMS ((enum tree_code
, tree
, tree
, words
, words
, words
));
106 static void done_alternative
PARAMS ((words
, struct alternatives
*));
107 static tree get_variable_decl
PARAMS ((tree
));
108 static void final_assign_error
PARAMS ((tree
));
109 static void check_final_reassigned
PARAMS ((tree
, words
));
111 #define ALLOC_WORDS(NUM) ((word*) xmalloc ((NUM) * sizeof (word)))
112 #define FREE_WORDS(PTR) (free (PTR))
114 /* DECLARE_BUFFERS is used to allocate NUMBUFFER bit sets, each of
115 which is an array of length num_current_words number of words.
116 Declares a new local variable BUFFER to hold the result (or rather
117 a pointer to the first of the bit sets). In almost all cases
118 num_current_words will be 1 or at most 2, so we try to stack
119 allocate the arrays in that case, using a stack array
120 named BUFFER##_short. Each DECLARE_BUFFERS must be matched by
121 a corresponding RELEASE_BUFFERS to avoid memory leaks. */
123 #define DECLARE_BUFFERS(BUFFER, NUMBUFFERS) \
124 word BUFFER##_short[2 * NUMBUFFERS]; \
125 words BUFFER = ALLOC_BUFFER(BUFFER##_short, NUMBUFFERS * num_current_words)
127 #define RELEASE_BUFFERS(BUFFER) \
128 FREE_BUFFER(BUFFER, BUFFER##_short)
130 #define ALLOC_BUFFER(SHORTBUFFER, NUMWORDS) \
131 ((NUMWORDS) * sizeof(word) <= sizeof(SHORTBUFFER) ? SHORTBUFFER \
132 : ALLOC_WORDS(NUMWORDS))
134 #define FREE_BUFFER(BUFFER, SHORTBUFFER) \
135 if (BUFFER != SHORTBUFFER) FREE_WORDS(BUFFER)
137 #define SET_P(WORDS, BIT) \
138 (WORDS[(BIT) / WORD_SIZE] & (1 << ((BIT) % WORD_SIZE)))
140 #define CLEAR_BIT(WORDS, BIT) \
141 (WORDS[(BIT) / WORD_SIZE] &= ~ (1 << ((BIT) % WORD_SIZE)))
143 #define SET_BIT(WORDS, BIT) \
144 (WORDS[(BIT) / WORD_SIZE] |= (1 << ((BIT) % WORD_SIZE)))
146 #define WORDS_NEEDED(BITS) (((BITS)+(WORD_SIZE-1))/(WORD_SIZE))
148 #define ASSIGNED_P(WORDS, BIT) SET_P(WORDS, 2 * (BIT))
149 #define UNASSIGNED_P(WORDS, BIT) SET_P(WORDS, 2 * (BIT) + 1)
151 #define SET_ASSIGNED(WORDS, INDEX) SET_BIT (WORDS, 2 * (INDEX))
152 #define SET_UNASSIGNED(WORDS, INDEX) SET_BIT (WORDS, 2 * (INDEX) + 1)
154 #define CLEAR_ASSIGNED(WORDS, INDEX) CLEAR_BIT (WORDS, 2 * (INDEX))
155 #define CLEAR_UNASSIGNED(WORDS, INDEX) CLEAR_BIT (WORDS, 2 * (INDEX) + 1)
157 /* Get the "interesting" declaration from a MODIFY_EXPR or COMPONENT_REF.
158 Return the declaration or NULL_TREE if no interesting declaration. */
161 get_variable_decl (exp
)
164 if (TREE_CODE (exp
) == VAR_DECL
)
166 if (! TREE_STATIC (exp
) || FIELD_FINAL (exp
))
169 /* We only care about final parameters. */
170 else if (TREE_CODE (exp
) == PARM_DECL
)
172 if (DECL_FINAL (exp
))
175 /* See if exp is this.field. */
176 else if (TREE_CODE (exp
) == COMPONENT_REF
)
178 tree op0
= TREE_OPERAND (exp
, 0);
179 tree op1
= TREE_OPERAND (exp
, 1);
180 tree mdecl
= current_function_decl
;
181 if (TREE_CODE (op0
) == INDIRECT_REF
182 && TREE_CODE (op1
) == FIELD_DECL
183 && ! METHOD_STATIC (mdecl
)
184 && FIELD_FINAL (op1
))
186 op0
= TREE_OPERAND (op0
, 0);
187 if (op0
== BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl
)))
195 final_assign_error (name
)
198 static const char format
[]
199 = "can't reassign a value to the final variable '%s'";
200 parse_error_context (wfl
, format
, IDENTIFIER_POINTER (name
));
204 check_final_reassigned (decl
, before
)
208 int index
= DECL_BIT_INDEX (decl
);
209 /* A final local already assigned or a final parameter
210 assigned must be reported as errors */
211 if (DECL_FINAL (decl
) && index
!= -2
212 && (index
< loop_current_locals
/* I.e. -1, or outside current loop. */
213 || ! UNASSIGNED_P (before
, index
)))
215 final_assign_error (DECL_NAME (decl
));
219 /* Check a conditional form (TEST_EXP ? THEN_EXP : ELSE_EXP) for
220 definite [un]assignment.
221 BEFORE, WHEN_FALSE, and WHEN_TRUE are as in check_bool_init. */
224 check_cond_init (test_exp
, then_exp
, else_exp
,
225 before
, when_false
, when_true
)
226 tree test_exp
, then_exp
, else_exp
;
227 words before
, when_false
, when_true
;
229 int save_start_current_locals
= start_current_locals
;
230 DECLARE_BUFFERS(test_false
, 6);
231 words test_true
= test_false
+ num_current_words
;
232 words then_false
= test_true
+ num_current_words
;
233 words then_true
= then_false
+ num_current_words
;
234 words else_false
= then_true
+ num_current_words
;
235 words else_true
= else_false
+ num_current_words
;
236 start_current_locals
= num_current_locals
;
238 check_bool_init (test_exp
, before
, test_false
, test_true
);
239 check_bool_init (then_exp
, test_true
, then_false
, then_true
);
240 check_bool_init (else_exp
, test_false
, else_false
, else_true
);
241 INTERSECT (when_false
, then_false
, else_false
);
242 INTERSECT (when_true
, then_true
, else_true
);
243 RELEASE_BUFFERS(test_false
);
244 start_current_locals
= save_start_current_locals
;
247 /* Check a boolean binary form CODE (EXP0, EXP1),
248 where CODE is one of EQ_EXPR, BIT_AND_EXPR, or BIT_IOR_EXPR.
249 BEFORE, WHEN_FALSE, and WHEN_TRUE are as in check_bool_init. */
252 check_bool2_init (code
, exp0
, exp1
, before
, when_false
, when_true
)
253 enum tree_code code
; tree exp0
, exp1
;
254 words before
, when_false
, when_true
;
257 words tmp
= num_current_words
<= 2 ? buf
258 : ALLOC_WORDS (4 * num_current_words
);
259 words when_false_0
= tmp
;
260 words when_false_1
= tmp
+num_current_words
;
261 words when_true_0
= tmp
+2*num_current_words
;
262 words when_true_1
= tmp
+3*num_current_words
;
263 check_bool_init (exp0
, before
, when_false_0
, when_true_0
);
264 INTERSECT (before
, when_false_0
, when_true_0
);
265 check_bool_init (exp1
, before
, when_false_1
, when_true_1
);
267 INTERSECT (before
, when_false_1
, when_true_1
);
272 * when_true = (when_false_1 INTERSECTION when_true_1)
273 * UNION (when_true_0 INTERSECTION when_false_1)
274 * UNION (when_false_0 INTERSECTION when_true_1);
275 * using when_false and before as temporary working areas. */
276 INTERSECT (when_true
, when_true_0
, when_false_1
);
277 INTERSECT (when_false
, when_true_0
, when_false_1
);
278 UNION (when_true
, when_true
, when_false
);
279 UNION (when_true
, when_true
, before
);
282 * when_false = (when_false_1 INTERSECTION when_true_1)
283 * UNION (when_true_0 INTERSECTION when_true_1)
284 * UNION (when_false_0 INTERSECTION when_false_1);
285 * using before as a temporary working area. */
286 INTERSECT (when_false
, when_true_0
, when_true_1
);
287 UNION (when_false
, when_false
, before
);
288 INTERSECT (before
, when_false_0
, when_false_1
);
289 UNION (when_false
, when_false
, before
);
291 else if (code
== BIT_AND_EXPR
|| code
== TRUTH_AND_EXPR
)
293 UNION (when_true
, when_true_0
, when_true_1
);
294 INTERSECT (when_false
, when_false_0
, when_false_1
);
295 UNION (when_false
, when_false
, before
);
297 else /* if (code == BIT_IOR_EXPR || code == TRUTH_OR_EXPR) */
299 UNION (when_false
, when_false_0
, when_false_1
);
300 INTERSECT (when_true
, when_true_0
, when_true_1
);
301 UNION (when_true
, when_true
, before
);
308 /* Check a boolean expression EXP for definite [un]assignment.
309 BEFORE is the set of variables definitely [un]assigned before the
310 conditional. (This bitstring may be modified arbitrarily in this function.)
311 On output, WHEN_FALSE is the set of variables [un]definitely assigned after
312 the conditional when the conditional is false.
313 On output, WHEN_TRUE is the set of variables definitely [un]assigned after
314 the conditional when the conditional is true.
315 (WHEN_FALSE and WHEN_TRUE are overwritten with initial values ignored.)
316 (None of BEFORE, WHEN_FALSE, or WHEN_TRUE can overlap, as they may
317 be used as temporary working areas. */
320 check_bool_init (exp
, before
, when_false
, when_true
)
322 words before
, when_false
, when_true
;
324 switch (TREE_CODE (exp
))
327 check_cond_init (TREE_OPERAND (exp
, 0), TREE_OPERAND (exp
, 1),
328 TREE_OPERAND (exp
, 2),
329 before
, when_false
, when_true
);
332 case TRUTH_ANDIF_EXPR
:
333 check_cond_init (TREE_OPERAND (exp
, 0),
334 TREE_OPERAND (exp
, 1), boolean_false_node
,
335 before
, when_false
, when_true
);
337 case TRUTH_ORIF_EXPR
:
338 check_cond_init (TREE_OPERAND (exp
, 0),
339 boolean_true_node
, TREE_OPERAND (exp
, 1),
340 before
, when_false
, when_true
);
343 check_bool_init (TREE_OPERAND (exp
, 0), before
, when_true
, when_false
);
347 tree tmp
= TREE_OPERAND (exp
, 0);
348 if ((tmp
= get_variable_decl (tmp
)) != NULL_TREE
)
351 check_bool_init (TREE_OPERAND (exp
, 1), before
,
352 when_false
, when_true
);
353 check_final_reassigned (tmp
, before
);
354 index
= DECL_BIT_INDEX (tmp
);
357 SET_ASSIGNED (when_false
, index
);
358 SET_ASSIGNED (when_true
, index
);
359 CLEAR_UNASSIGNED (when_false
, index
);
360 CLEAR_UNASSIGNED (when_true
, index
);
372 check_bool2_init (TREE_CODE (exp
),
373 TREE_OPERAND (exp
, 0), TREE_OPERAND (exp
, 1),
374 before
, when_false
, when_true
);
380 /* Just like EQ_EXPR, but switch when_true and when_false. */
381 check_bool2_init (EQ_EXPR
, TREE_OPERAND (exp
, 0), TREE_OPERAND (exp
, 1),
382 before
, when_true
, when_false
);
387 if (integer_zerop (exp
))
390 COPY (when_false
, before
);
394 SET_ALL (when_false
);
395 COPY (when_true
, before
);
400 check_init (exp
, before
);
401 COPY (when_false
, before
);
402 COPY (when_true
, before
);
406 /* Used to keep track of control flow branches. */
410 struct alternatives
*outer
;
412 /* The value of num_current_locals at the start of this compound. */
415 /* The value of the "before" set at the start of the control stucture.
416 Used for SWITCH_EXPR but not set for LABELED_BLOCK_EXPR. */
419 int save_start_current_locals
;
421 /* If num_current_words==1, combined==&one_word, for efficiency. */
424 /* The intersection of the "after" sets from previous branches. */
430 struct alternatives
* alternatives
= NULL
;
432 /* Begin handling a control flow branch.
433 BEFORE is the state of [un]assigned variables on entry.
434 CURRENT is a struct alt to manage the branch alternatives. */
436 #define BEGIN_ALTERNATIVES(before, current) \
438 current.saved = NULL; \
439 current.num_locals = num_current_locals; \
440 current.combined = num_current_words <= 1 ? ¤t.one_word \
441 : ALLOC_WORDS (num_current_words); \
442 SET_ALL (current.combined); \
443 current.outer = alternatives; \
444 alternatives = ¤t; \
445 current.save_start_current_locals = start_current_locals; \
446 start_current_locals = num_current_locals; \
449 /* We have finished with one branch of branching control flow.
450 Store the [un]assigned state, merging (intersecting) it with the state
451 of previous alternative branches. */
454 done_alternative (after
, current
)
456 struct alternatives
*current
;
458 INTERSECTN (current
->combined
, current
->combined
, after
,
459 WORDS_NEEDED (2 * current
->num_locals
));
462 /* Used when we done with a control flow branch and are all merged again.
463 * AFTER is the merged state of [un]assigned variables,
464 CURRENT is a struct alt that was passed to BEGIN_ALTERNATIVES. */
466 #define END_ALTERNATIVES(after, current) \
468 alternatives = current.outer; \
469 COPY (after, current.combined); \
470 if (current.combined != ¤t.one_word) \
471 FREE_WORDS (current.combined); \
472 start_current_locals = current.save_start_current_locals; \
475 /* Check for (un)initialized local variables in EXP. */
478 check_init (exp
, before
)
484 switch (TREE_CODE (exp
))
488 if (! FIELD_STATIC (exp
) && DECL_NAME (exp
) != NULL_TREE
489 && DECL_NAME (exp
) != this_identifier_node
)
491 int index
= DECL_BIT_INDEX (exp
);
492 /* We don't want to report and mark as non initialized class
493 initialization flags. */
494 if (! LOCAL_CLASS_INITIALIZATION_FLAG_P (exp
)
495 && index
>= 0 && ! ASSIGNED_P (before
, index
))
498 (wfl
, "Variable `%s' may not have been initialized",
499 IDENTIFIER_POINTER (DECL_NAME (exp
)));
500 /* Suppress further errors. */
501 DECL_BIT_INDEX (exp
) = -2;
507 check_init (TREE_OPERAND (exp
, 0), before
);
508 if ((tmp
= get_variable_decl (exp
)) != NULL_TREE
)
510 int index
= DECL_BIT_INDEX (tmp
);
511 if (index
>= 0 && ! ASSIGNED_P (before
, index
))
514 (wfl
, "variable '%s' may not have been initialized",
515 IDENTIFIER_POINTER (DECL_NAME (tmp
)));
516 /* Suppress further errors. */
517 DECL_BIT_INDEX (tmp
) = -2;
523 tmp
= TREE_OPERAND (exp
, 0);
524 /* We're interested in variable declaration and parameter
525 declaration when they're declared with the `final' modifier. */
526 if ((tmp
= get_variable_decl (tmp
)) != NULL_TREE
)
529 check_init (TREE_OPERAND (exp
, 1), before
);
530 check_final_reassigned (tmp
, before
);
531 index
= DECL_BIT_INDEX (tmp
);
534 SET_ASSIGNED (before
, index
);
535 CLEAR_UNASSIGNED (before
, index
);
537 /* Minor optimization. See comment for start_current_locals.
538 If we're optimizing for class initialization, we keep
539 this information to check whether the variable is
540 definitely assigned when once we checked the whole
542 if (! STATIC_CLASS_INIT_OPT_P () /* FIXME */
543 && index
>= start_current_locals
544 && index
== num_current_locals
- 1)
546 num_current_locals
--;
547 DECL_BIT_INDEX (tmp
) = -1;
551 else if (TREE_CODE (tmp
= TREE_OPERAND (exp
, 0)) == COMPONENT_REF
)
554 check_init (tmp
, before
);
555 check_init (TREE_OPERAND (exp
, 1), before
);
556 decl
= TREE_OPERAND (tmp
, 1);
557 if (DECL_FINAL (decl
))
558 final_assign_error (DECL_NAME (decl
));
561 else if (TREE_CODE (tmp
) == COMPONENT_REF
&& IS_ARRAY_LENGTH_ACCESS (tmp
))
563 /* We can't emit a more specific message here, because when
564 compiling to bytecodes we don't get here. */
565 final_assign_error (length_identifier_node
);
570 if (BLOCK_EXPR_BODY (exp
))
572 tree decl
= BLOCK_EXPR_DECLS (exp
);
576 int save_start_current_locals
= start_current_locals
;
577 int save_num_current_words
= num_current_words
;
578 start_current_locals
= num_current_locals
;
579 for (; decl
!= NULL_TREE
; decl
= TREE_CHAIN (decl
))
581 DECL_BIT_INDEX (decl
) = num_current_locals
++;
583 words_needed
= WORDS_NEEDED (2 * num_current_locals
);
584 if (words_needed
> num_current_words
)
586 tmp
= ALLOC_WORDS (words_needed
);
588 num_current_words
= words_needed
;
592 for (i
= start_current_locals
; i
< num_current_locals
; i
++)
594 CLEAR_ASSIGNED (tmp
, i
);
595 SET_UNASSIGNED (tmp
, i
);
597 check_init (BLOCK_EXPR_BODY (exp
), tmp
);
599 /* Re-set DECL_BIT_INDEX since it is also DECL_POINTER_ALIAS_SET. */
600 for (decl
= BLOCK_EXPR_DECLS (exp
);
601 decl
!= NULL_TREE
; decl
= TREE_CHAIN (decl
))
603 if (LOCAL_CLASS_INITIALIZATION_FLAG_P (decl
))
605 int index
= DECL_BIT_INDEX (decl
);
606 tree fndecl
= DECL_CONTEXT (decl
);
607 if (fndecl
&& METHOD_STATIC (fndecl
)
608 && (DECL_INITIAL (decl
) == boolean_true_node
609 || (index
>= 0 && ASSIGNED_P (tmp
, index
))))
610 hash_lookup (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl
),
611 DECL_FUNCTION_INIT_TEST_CLASS(decl
), TRUE
, NULL
);
613 DECL_BIT_INDEX (decl
) = -1;
616 num_current_locals
= start_current_locals
;
617 start_current_locals
= save_start_current_locals
;
620 num_current_words
= save_num_current_words
;
628 /* The JLS 2nd edition discusses a complication determining
629 definite unassignment of loop statements. They define a
630 "hypothetical" analysis model. We do something much
631 simpler: We just disallow assignments inside loops to final
632 variables declared outside the loop. This means we may
633 disallow some contrived assignments that the JLS, but I
634 can't see how anything except a very contrived testcase (a
635 do-while whose condition is false?) would care. */
637 struct alternatives alt
;
638 int save_loop_current_locals
= loop_current_locals
;
639 int save_start_current_locals
= start_current_locals
;
640 loop_current_locals
= num_current_locals
;
641 start_current_locals
= num_current_locals
;
642 BEGIN_ALTERNATIVES (before
, alt
);
644 check_init (TREE_OPERAND (exp
, 0), before
);
645 END_ALTERNATIVES (before
, alt
);
646 loop_current_locals
= save_loop_current_locals
;
647 start_current_locals
= save_start_current_locals
;
652 struct alternatives
*alt
= alternatives
;
653 DECLARE_BUFFERS(when_true
, 2);
654 words when_false
= when_true
+ num_current_words
;
655 #ifdef ENABLE_JC1_CHECKING
656 if (TREE_CODE (alt
->block
) != LOOP_EXPR
)
659 check_bool_init (TREE_OPERAND (exp
, 0), before
, when_false
, when_true
);
660 done_alternative (when_true
, alt
);
661 COPY (before
, when_false
);
662 RELEASE_BUFFERS(when_true
);
665 case LABELED_BLOCK_EXPR
:
667 struct alternatives alt
;
668 BEGIN_ALTERNATIVES (before
, alt
);
670 if (LABELED_BLOCK_BODY (exp
))
671 check_init (LABELED_BLOCK_BODY (exp
), before
);
672 done_alternative (before
, &alt
);
673 END_ALTERNATIVES (before
, alt
);
676 case EXIT_BLOCK_EXPR
:
678 tree block
= TREE_OPERAND (exp
, 0);
679 struct alternatives
*alt
= alternatives
;
680 while (alt
->block
!= block
)
682 done_alternative (before
, alt
);
688 struct alternatives alt
;
690 check_init (TREE_OPERAND (exp
, 0), before
);
691 BEGIN_ALTERNATIVES (before
, alt
);
692 alt
.saved
= ALLOC_BUFFER(buf
, num_current_words
);
693 COPY (alt
.saved
, before
);
695 check_init (TREE_OPERAND (exp
, 1), before
);
696 done_alternative (before
, &alt
);
697 if (! SWITCH_HAS_DEFAULT (exp
))
698 done_alternative (alt
.saved
, &alt
);
699 FREE_BUFFER(alt
.saved
, buf
);
700 END_ALTERNATIVES (before
, alt
);
707 struct alternatives
*alt
= alternatives
;
708 while (TREE_CODE (alt
->block
) != SWITCH_EXPR
)
710 COPYN (before
, alt
->saved
, WORDS_NEEDED (2 * alt
->num_locals
));
711 for (i
= alt
->num_locals
; i
< num_current_locals
; i
++)
712 CLEAR_ASSIGNED (before
, i
);
718 tree try_clause
= TREE_OPERAND (exp
, 0);
719 tree clause
= TREE_OPERAND (exp
, 1);
721 words tmp
= (num_current_words
<= 2 ? buf
722 : ALLOC_WORDS (2 * num_current_words
));
723 words save
= tmp
+ num_current_words
;
724 struct alternatives alt
;
725 BEGIN_ALTERNATIVES (before
, alt
);
728 check_init (try_clause
, tmp
);
729 done_alternative (tmp
, &alt
);
730 for ( ; clause
!= NULL_TREE
; clause
= TREE_CHAIN (clause
))
732 tree catch_clause
= TREE_OPERAND (clause
, 0);
734 check_init (catch_clause
, tmp
);
735 done_alternative (tmp
, &alt
);
741 END_ALTERNATIVES (before
, alt
);
745 case TRY_FINALLY_EXPR
:
747 DECLARE_BUFFERS(tmp
, 1);
749 check_init (TREE_OPERAND (exp
, 0), before
);
750 check_init (TREE_OPERAND (exp
, 1), tmp
);
751 UNION (before
, before
, tmp
);
752 RELEASE_BUFFERS(tmp
);
758 if (TREE_OPERAND (exp
, 0))
759 check_init (TREE_OPERAND (exp
, 0), before
);
760 goto never_continues
;
768 case TRUTH_ANDIF_EXPR
:
769 case TRUTH_ORIF_EXPR
:
771 DECLARE_BUFFERS(when_true
, 2);
772 words when_false
= when_true
+ num_current_words
;
773 check_bool_init (exp
, before
, when_false
, when_true
);
774 INTERSECT (before
, when_false
, when_true
);
775 RELEASE_BUFFERS(when_true
);
780 if (exp
== empty_stmt_node
)
782 /* ... else fall through ... */
783 case UNARY_PLUS_EXPR
:
796 case NON_LVALUE_EXPR
:
797 case INSTANCEOF_EXPR
:
803 /* Avoid needless recursion. */
804 exp
= TREE_OPERAND (exp
, 0);
807 case PREDECREMENT_EXPR
:
808 case PREINCREMENT_EXPR
:
809 case POSTDECREMENT_EXPR
:
810 case POSTINCREMENT_EXPR
:
811 tmp
= get_variable_decl (TREE_OPERAND (exp
, 0));
812 if (tmp
!= NULL_TREE
&& DECL_FINAL (tmp
))
813 final_assign_error (DECL_NAME (tmp
));
815 /* Avoid needless recursion. */
816 exp
= TREE_OPERAND (exp
, 0);
820 if (IS_INIT_CHECKED (exp
))
822 IS_INIT_CHECKED (exp
) = 1;
823 exp
= TREE_OPERAND (exp
, 0);
858 check_init (TREE_OPERAND (exp
, 0), before
);
859 /* Avoid needless recursion, especially for COMPOUND_EXPR. */
860 exp
= TREE_OPERAND (exp
, 1);
868 case JAVA_EXC_OBJ_EXPR
:
874 tree func
= TREE_OPERAND (exp
, 0);
875 tree x
= TREE_OPERAND (exp
, 1);
876 if (TREE_CODE (func
) == ADDR_EXPR
)
877 func
= TREE_OPERAND (func
, 0);
878 check_init (func
, before
);
880 for ( ; x
!= NULL_TREE
; x
= TREE_CHAIN (x
))
881 check_init (TREE_VALUE (x
), before
);
882 if (func
== throw_node
)
883 goto never_continues
;
889 tree x
= CONSTRUCTOR_ELTS (TREE_OPERAND (exp
, 0));
890 for ( ; x
!= NULL_TREE
; x
= TREE_CHAIN (x
))
891 check_init (TREE_VALUE (x
), before
);
895 case EXPR_WITH_FILE_LOCATION
:
897 const char *saved_input_filename
= input_filename
;
898 tree saved_wfl
= wfl
;
899 tree body
= EXPR_WFL_NODE (exp
);
900 int saved_lineno
= lineno
;
901 if (body
== empty_stmt_node
)
904 input_filename
= EXPR_WFL_FILENAME (exp
);
905 lineno
= EXPR_WFL_LINENO (exp
);
906 check_init (body
, before
);
907 input_filename
= saved_input_filename
;
908 lineno
= saved_lineno
;
915 ("internal error in check-init: tree code not implemented: %s",
916 tree_code_name
[(int) TREE_CODE (exp
)]);
921 check_for_initialization (body
, mdecl
)
927 tree owner
= DECL_CONTEXT (mdecl
);
928 int is_static_method
= METHOD_STATIC (mdecl
);
929 /* We don't need to check final fields of <init> it it calls this(). */
930 int is_finit_method
= DECL_FINIT_P (mdecl
) || DECL_INSTINIT_P (mdecl
);
932 = (is_finit_method
|| DECL_CLINIT_P (mdecl
)
933 || (DECL_INIT_P (mdecl
) && ! DECL_INIT_CALLS_THIS (mdecl
)));
935 start_current_locals
= num_current_locals
= 0;
936 num_current_words
= 2;
941 for (decl
= TYPE_FIELDS (owner
);
942 decl
!= NULL_TREE
; decl
= TREE_CHAIN (decl
))
944 if (DECL_FINAL (decl
) && FIELD_STATIC (decl
) == is_static_method
)
946 if (DECL_FIELD_FINAL_IUD (decl
))
947 DECL_BIT_INDEX (decl
) = -1;
949 DECL_BIT_INDEX (decl
) = num_current_locals
++;
952 words_needed
= WORDS_NEEDED (2 * num_current_locals
);
953 if (words_needed
> 2)
955 num_current_words
= words_needed
;
956 before
= ALLOC_WORDS(words_needed
);
959 for (decl
= TYPE_FIELDS (owner
);
960 decl
!= NULL_TREE
; decl
= TREE_CHAIN (decl
))
962 if (FIELD_FINAL (decl
) && FIELD_STATIC (decl
) == is_static_method
)
964 if (! DECL_FIELD_FINAL_IUD (decl
))
966 CLEAR_ASSIGNED (before
, i
);
967 SET_UNASSIGNED (before
, i
);
975 check_init (body
, before
);
979 for (decl
= TYPE_FIELDS (owner
);
980 decl
!= NULL_TREE
; decl
= TREE_CHAIN (decl
))
982 if (FIELD_FINAL (decl
) && FIELD_STATIC (decl
) == is_static_method
)
984 int index
= DECL_BIT_INDEX (decl
);
985 if (index
>= 0 && ! ASSIGNED_P (before
, index
))
987 if (! is_finit_method
)
988 error_with_decl (decl
, "final field '%s' may not have been initialized");
990 else if (is_finit_method
)
991 DECL_FIELD_FINAL_IUD (decl
) = 1;
993 /* Re-set to initial state, since we later may use the
994 same bit for DECL_POINTER_ALIAS_SET. */
995 DECL_BIT_INDEX (decl
) = -1;
1000 start_current_locals
= num_current_locals
= 0;
1003 /* Call for every element in DECL_FUNCTION_INITIALIZED_CLASS_TABLE of
1004 a method to consider whether the type indirectly described by ENTRY
1005 is definitly initialized and thus remembered as such. */
1008 attach_initialized_static_class (entry
, ptr
)
1009 struct hash_entry
*entry
;
1012 struct init_test_hash_entry
*ite
= (struct init_test_hash_entry
*) entry
;
1013 tree fndecl
= DECL_CONTEXT (ite
->init_test_decl
);
1014 int index
= DECL_BIT_INDEX (ite
->init_test_decl
);
1016 /* If the initializer flag has been definitly assigned (not taking
1017 into account its first mandatory assignment which has been
1018 already added but escaped analysis.) */
1019 if (fndecl
&& METHOD_STATIC (fndecl
)
1020 && (DECL_INITIAL (ite
->init_test_decl
) == boolean_true_node
1021 || (index
>= 0 && ASSIGNED_P (((word
*) ptr
), index
))))
1022 hash_lookup (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl
),
1023 entry
->key
, TRUE
, NULL
);