OSDN Git Service

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