OSDN Git Service

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