OSDN Git Service

* check-init.c (check_init): Fix typo freeing memory twice.
[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 "tree.h"
28 #include "flags.h" /* Needed for optimize. */
29 #include "java-tree.h"
30 #include "toplev.h" /* Needed for fatal. */
31
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 */
37
38 /* One segment of a bitstring. */
39 typedef unsigned int word;
40
41 /* Pointer to a bitstring. */
42 typedef word *words;
43
44 /* Number of locals variables currently active. */
45 static int num_current_locals = 0;
46
47 /* The value of num_current_locals when we entered the closest
48    enclosing LOOP_EXPR. */
49 static int loop_current_locals;
50
51 /* The index of the first local variable in the current block.
52
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.
58
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
70    their declaration. */
71 static int start_current_locals = 0;
72
73 static int num_current_words;
74
75 static tree wfl;
76
77 #define COPYN(DST, SRC, NWORDS) memcpy (DST, SRC, NWORDS * sizeof(word))
78 #define COPY(DST, SRC) COPYN (DST, SRC, num_current_words)
79
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))
82
83 #define INTERSECTN(DST, SRC1, SRC2, N) \
84   do { int n = N; \
85   while (--n >= 0) DST[n] = SRC1[n] & SRC2[n]; \
86   } while (0)
87
88 #define UNION(DST, SRC1, SRC2) \
89   UNIONN (DST, SRC1, SRC2, num_current_words)
90
91 #define UNIONN(DST, SRC1, SRC2, N) \
92   do { int n = N; \
93   while (--n >= 0) DST[n] = SRC1[n] | SRC2[n]; \
94   } while (0)
95
96 #define INTERSECT(DST, SRC1, SRC2) \
97   INTERSECTN (DST, SRC1, SRC2, num_current_words)
98
99 #define WORD_SIZE  ((unsigned int)(sizeof(word) * BITS_PER_UNIT))
100
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));
105 struct alternatives;
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));
110
111 #define ALLOC_WORDS(NUM) ((word*) xmalloc ((NUM) * sizeof (word)))
112 #define FREE_WORDS(PTR) (free (PTR))
113
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.  */
122
123 #define DECLARE_BUFFERS(BUFFER, NUMBUFFERS) \
124   word BUFFER##_short[2 * NUMBUFFERS]; \
125   words BUFFER = ALLOC_BUFFER(BUFFER##_short, NUMBUFFERS * num_current_words)
126
127 #define RELEASE_BUFFERS(BUFFER) \
128   FREE_BUFFER(BUFFER, BUFFER##_short)
129
130 #define ALLOC_BUFFER(SHORTBUFFER, NUMWORDS) \
131   ((NUMWORDS) * sizeof(word) <= sizeof(SHORTBUFFER) ? SHORTBUFFER \
132    : ALLOC_WORDS(NUMWORDS))
133
134 #define FREE_BUFFER(BUFFER, SHORTBUFFER) \
135   if (BUFFER != SHORTBUFFER) FREE_WORDS(BUFFER)
136
137 #define SET_P(WORDS, BIT) \
138   (WORDS[(BIT) / WORD_SIZE] & (1 << ((BIT) % WORD_SIZE)))
139
140 #define CLEAR_BIT(WORDS, BIT) \
141   (WORDS[(BIT) / WORD_SIZE] &= ~ (1 << ((BIT) % WORD_SIZE)))
142
143 #define SET_BIT(WORDS, BIT) \
144   (WORDS[(BIT) / WORD_SIZE] |= (1 << ((BIT) % WORD_SIZE)))
145
146 #define WORDS_NEEDED(BITS) (((BITS)+(WORD_SIZE-1))/(WORD_SIZE))
147
148 #define ASSIGNED_P(WORDS, BIT)  SET_P(WORDS, 2 * (BIT))
149 #define UNASSIGNED_P(WORDS, BIT)  SET_P(WORDS, 2 * (BIT) + 1)
150
151 #define SET_ASSIGNED(WORDS, INDEX) SET_BIT (WORDS, 2 * (INDEX))
152 #define SET_UNASSIGNED(WORDS, INDEX) SET_BIT (WORDS, 2 * (INDEX) + 1)
153
154 #define CLEAR_ASSIGNED(WORDS, INDEX) CLEAR_BIT (WORDS, 2 * (INDEX))
155 #define CLEAR_UNASSIGNED(WORDS, INDEX) CLEAR_BIT (WORDS, 2 * (INDEX) + 1)
156
157 /* Get the "interesting" declaration from a MODIFY_EXPR or COMPONENT_REF.
158    Return the declaration or NULL_TREE if no interesting declaration.  */
159
160 static tree
161 get_variable_decl (exp)
162      tree exp;
163 {
164   if (TREE_CODE (exp) == VAR_DECL)
165     {
166       if (! TREE_STATIC (exp) ||  FIELD_FINAL (exp))
167         return exp;
168     }
169   /* We only care about final parameters. */
170   else if (TREE_CODE (exp) == PARM_DECL)
171     {
172       if (DECL_FINAL (exp))
173         return exp;
174     }
175   /* See if exp is this.field. */
176   else if (TREE_CODE (exp) == COMPONENT_REF)
177     {
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))
185         {
186           op0 = TREE_OPERAND (op0, 0);
187           if (op0 == BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)))
188             return op1;
189         }
190     }
191   return NULL_TREE;
192 }
193
194 static void
195 final_assign_error (name)
196      tree name;
197 {
198   static const char format[]
199     = "can't re-assign here a value to the final variable '%s'";
200   parse_error_context (wfl, format, IDENTIFIER_POINTER (name));
201 }
202
203 static void
204 check_final_reassigned (decl, before)
205      tree decl;
206      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           || ! UNASSIGNED_P (before, index)))
214     {
215       final_assign_error (DECL_NAME (decl));
216     }
217 }
218
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. */
222
223 static void
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;
228 {
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;
237
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;
245 }
246
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. */
250
251 static void
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;
255 {
256   word buf[2*4];
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);
266
267   INTERSECT (before, when_false_1, when_true_1);
268
269   if (code == EQ_EXPR)
270     {
271       /* Now set:
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);
280
281       /* Now set:
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);
290     }
291   else if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
292     {
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);
296     }
297   else /* if (code == BIT_IOR_EXPR || code == TRUTH_OR_EXPR) */
298     {
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);
302     }
303
304   if (tmp != buf)
305     FREE_WORDS (tmp);
306 }
307
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. */
318
319 static void
320 check_bool_init (exp, before, when_false, when_true)
321      tree exp;
322      words before, when_false, when_true;
323 {
324   switch (TREE_CODE (exp))
325     {
326     case COND_EXPR:
327       check_cond_init (TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
328                        TREE_OPERAND (exp, 2),
329                        before, when_false, when_true);
330       return;
331
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);
336       return;
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);
341       return;
342     case TRUTH_NOT_EXPR:
343       check_bool_init (TREE_OPERAND (exp, 0), before, when_true, when_false);
344       return;
345     case MODIFY_EXPR:
346       {
347         tree tmp = TREE_OPERAND (exp, 0);
348         if ((tmp = get_variable_decl (tmp)) != NULL_TREE)
349           {
350             int index;
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);
355             if (index >= 0)
356               {
357                 SET_ASSIGNED (when_false, index);
358                 SET_ASSIGNED (when_true, index);
359                 CLEAR_UNASSIGNED (when_false, index);
360                 CLEAR_UNASSIGNED (when_true, index);
361               }
362             break;
363           }
364       }
365       goto do_default;
366
367     case BIT_AND_EXPR:
368     case BIT_IOR_EXPR:
369     case TRUTH_AND_EXPR:
370     case TRUTH_OR_EXPR:
371     case EQ_EXPR:
372       check_bool2_init (TREE_CODE (exp),
373                         TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
374                         before, when_false, when_true);
375       return;
376
377     case TRUTH_XOR_EXPR:
378     case BIT_XOR_EXPR:
379     case NE_EXPR:
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);
383
384       return;
385
386     case INTEGER_CST:
387       if (integer_zerop (exp))
388         {
389           SET_ALL (when_true);
390           COPY (when_false, before);
391         }
392       else
393         {
394           SET_ALL (when_false);
395           COPY (when_true, before);
396         }
397       break;
398     default:
399     do_default:
400       check_init (exp, before);
401       COPY (when_false, before);
402       COPY (when_true, before);
403     }
404 }
405
406 /* Used to keep track of control flow branches. */
407
408 struct alternatives
409 {
410   struct alternatives *outer;
411
412   /* The value of num_current_locals at the start of this compound. */
413   int num_locals;
414
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. */
417   words saved;
418
419   int save_start_current_locals;
420
421   /* If num_current_words==1, combined==&one_word, for efficiency. */
422   word one_word;
423
424   /* The intersection of the "after" sets from previous branches. */
425   words combined;
426
427   tree block;
428 };
429
430 struct alternatives * alternatives = NULL;
431
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. */
435
436 #define BEGIN_ALTERNATIVES(before, current) \
437 { \
438   current.saved = NULL; \
439   current.num_locals = num_current_locals; \
440   current.combined = num_current_words <= 1 ? &current.one_word \
441     : ALLOC_WORDS (num_current_words); \
442   SET_ALL (current.combined); \
443   current.outer = alternatives; \
444   alternatives = &current; \
445   current.save_start_current_locals = start_current_locals; \
446   start_current_locals = num_current_locals; \
447 }
448
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. */
452
453 static void
454 done_alternative (after, current)
455      words after;
456      struct alternatives *current; 
457 {
458   INTERSECTN (current->combined, current->combined, after,
459               WORDS_NEEDED (2 * current->num_locals));
460 }
461
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. */
465
466 #define END_ALTERNATIVES(after, current) \
467 { \
468   alternatives = current.outer; \
469   COPY (after, current.combined); \
470   if (current.combined != &current.one_word) \
471     FREE_WORDS (current.combined); \
472   start_current_locals = current.save_start_current_locals; \
473 }
474
475 /* Check for (un)initialized local variables in EXP.  */
476
477 static void
478 check_init (exp, before)
479      tree exp;
480      words before;
481 {
482   tree tmp;
483  again:
484   switch (TREE_CODE (exp))
485     {
486     case VAR_DECL:
487     case PARM_DECL:
488       if (! FIELD_STATIC (exp) && DECL_NAME (exp) != NULL_TREE
489           && DECL_NAME (exp) != this_identifier_node)
490         {
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))
496             {
497               parse_error_context 
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;
502             }
503         }
504       break;
505
506     case COMPONENT_REF:
507       check_init (TREE_OPERAND (exp, 0), before);
508       if ((tmp = get_variable_decl (exp)) != NULL_TREE)
509         {
510           int index = DECL_BIT_INDEX (tmp);
511           if (index >= 0 && ! ASSIGNED_P (before, index))
512             {
513               parse_error_context 
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;
518             }
519         }
520       break;
521       
522     case MODIFY_EXPR:
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)
527         {
528           int index;
529           check_init (TREE_OPERAND (exp, 1), before);
530           check_final_reassigned (tmp, before);
531           index = DECL_BIT_INDEX (tmp);
532           if (index >= 0)
533             {
534               SET_ASSIGNED (before, index);
535               CLEAR_UNASSIGNED (before, index);
536             }
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
541              function. */
542           if (! STATIC_CLASS_INIT_OPT_P () /* FIXME */
543               && index >= start_current_locals
544               && index == num_current_locals - 1)
545             {
546               num_current_locals--;
547               DECL_BIT_INDEX (tmp) = -1;
548             }
549          break;
550        }
551       else if (TREE_CODE (tmp = TREE_OPERAND (exp, 0)) == COMPONENT_REF)
552         {
553           tree decl;
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));
559           break;
560         }
561       else if (TREE_CODE (tmp) == INDIRECT_REF && IS_ARRAY_LENGTH_ACCESS (tmp))
562         {
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);
566         }
567      else
568        goto binop;
569     case BLOCK:
570       if (BLOCK_EXPR_BODY (exp))
571         {
572           tree decl = BLOCK_EXPR_DECLS (exp);
573           int words_needed;
574           word* tmp;
575           int i;
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))
580             {
581               DECL_BIT_INDEX (decl) = num_current_locals++;
582             }
583           words_needed = WORDS_NEEDED (2 * num_current_locals);
584           if (words_needed > num_current_words)
585             {
586               tmp = ALLOC_WORDS (words_needed);
587               COPY (tmp, before);
588               num_current_words = words_needed;
589             }
590           else
591             tmp = before;
592           for (i = start_current_locals;  i < num_current_locals;  i++)
593             {
594               CLEAR_ASSIGNED (tmp, i);
595               SET_UNASSIGNED (tmp, i);
596             }
597           check_init (BLOCK_EXPR_BODY (exp), tmp);
598
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))
602             {
603               if (LOCAL_CLASS_INITIALIZATION_FLAG_P (decl))
604                 {
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);  
612                 }
613               DECL_BIT_INDEX (decl) = -1;
614             }
615
616           num_current_locals = start_current_locals;
617           start_current_locals = save_start_current_locals;
618           if (tmp != before)
619             {
620               num_current_words = save_num_current_words;
621               COPY (before, tmp);
622               FREE_WORDS (tmp);
623             }
624         }
625       break;
626     case LOOP_EXPR:
627       {
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. */
636
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);
643         alt.block = exp;
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;
648         return;
649       }
650     case EXIT_EXPR:
651       {
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)
657           abort ();
658 #endif
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);
663         return;
664       }
665     case LABELED_BLOCK_EXPR:
666       {
667         struct alternatives alt;
668         BEGIN_ALTERNATIVES (before, alt);
669         alt.block = exp;
670         if (LABELED_BLOCK_BODY (exp))
671           check_init (LABELED_BLOCK_BODY (exp), before);
672         done_alternative (before, &alt);
673         END_ALTERNATIVES (before, alt);
674         return;
675       }
676     case EXIT_BLOCK_EXPR:
677       {
678         tree block = TREE_OPERAND (exp, 0);
679         struct alternatives *alt = alternatives;
680         while (alt->block != block)
681           alt = alt->outer;
682         done_alternative (before, alt);
683         SET_ALL (before);
684         return;
685       }
686     case SWITCH_EXPR:
687       {
688         struct alternatives alt;
689         word buf[2];
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);
694         alt.block = exp;
695         check_init (TREE_OPERAND (exp, 1), before);
696         done_alternative (before, &alt);
697         FREE_BUFFER(alt.saved, buf);
698         END_ALTERNATIVES (before, alt);
699         return;
700       }
701     case CASE_EXPR:
702     case DEFAULT_EXPR:
703       {
704         int i;
705         struct alternatives *alt = alternatives;
706         while (TREE_CODE (alt->block) != SWITCH_EXPR)
707           alt = alt->outer;
708         COPYN (before, alt->saved, WORDS_NEEDED (2 * alt->num_locals));
709         for (i = alt->num_locals;  i < num_current_locals;  i++)
710           CLEAR_ASSIGNED (before, i);
711         break;
712       }
713
714     case TRY_EXPR:
715       {
716         tree try_clause = TREE_OPERAND (exp, 0);
717         tree clause = TREE_OPERAND (exp, 1);
718         word buf[2*2];
719         words tmp = (num_current_words <= 2 ? buf
720                     : ALLOC_WORDS (2 * num_current_words));
721         words save = tmp + num_current_words;
722         struct alternatives alt;
723         BEGIN_ALTERNATIVES (before, alt);
724         COPY (save, before);
725         COPY (tmp, save);
726         check_init (try_clause, tmp);
727         done_alternative (tmp, &alt);
728         for ( ; clause != NULL_TREE;  clause = TREE_CHAIN (clause))
729           {
730             tree catch_clause = TREE_OPERAND (clause, 0);
731             COPY (tmp, save);
732             check_init (catch_clause, tmp);
733             done_alternative (tmp, &alt);
734           }
735         if (tmp != buf)
736           {
737             FREE_WORDS (tmp);
738           }
739         END_ALTERNATIVES (before, alt);
740       }
741     return;
742
743     case TRY_FINALLY_EXPR:
744       {
745         DECLARE_BUFFERS(tmp, 1);
746         COPY (tmp, before);
747         check_init (TREE_OPERAND (exp, 0), before);
748         check_init (TREE_OPERAND (exp, 1), tmp);
749         UNION (before, before, tmp);
750         RELEASE_BUFFERS(tmp);
751       }
752       return;
753
754     case RETURN_EXPR:
755     case THROW_EXPR:
756       if (TREE_OPERAND (exp, 0))
757         check_init (TREE_OPERAND (exp, 0), before);
758       goto never_continues;
759
760     case ERROR_MARK:
761     never_continues:
762       SET_ALL (before);
763       return;
764       
765     case COND_EXPR:
766     case TRUTH_ANDIF_EXPR:
767     case TRUTH_ORIF_EXPR:
768       {
769         DECLARE_BUFFERS(when_true, 2);
770         words when_false = when_true + num_current_words;
771         check_bool_init (exp, before, when_false, when_true);
772         INTERSECT (before, when_false, when_true);
773         RELEASE_BUFFERS(when_true);
774       }
775       break;
776
777     case NOP_EXPR:
778       if (exp == empty_stmt_node)
779         break;
780       /* ... else fall through ... */
781     case UNARY_PLUS_EXPR:
782     case NEGATE_EXPR:
783     case TRUTH_AND_EXPR:
784     case TRUTH_OR_EXPR:
785     case TRUTH_XOR_EXPR:
786     case TRUTH_NOT_EXPR:
787     case BIT_NOT_EXPR:
788     case CONVERT_EXPR:
789     case BIT_FIELD_REF:
790     case FLOAT_EXPR:
791     case FIX_TRUNC_EXPR:
792     case INDIRECT_REF:
793     case ADDR_EXPR:
794     case PREDECREMENT_EXPR:
795     case PREINCREMENT_EXPR:
796     case POSTDECREMENT_EXPR:
797     case POSTINCREMENT_EXPR:
798     case NON_LVALUE_EXPR:
799     case INSTANCEOF_EXPR:
800     case FIX_CEIL_EXPR:
801     case FIX_FLOOR_EXPR:
802     case FIX_ROUND_EXPR:
803     case ABS_EXPR:
804     case FFS_EXPR:
805       /* Avoid needless recursion. */
806       exp = TREE_OPERAND (exp, 0);
807       goto again;
808
809     case SAVE_EXPR:
810       if (IS_INIT_CHECKED (exp))
811         return;
812       IS_INIT_CHECKED (exp) = 1;
813       exp = TREE_OPERAND (exp, 0);
814       goto again;
815
816     case COMPOUND_EXPR:
817     case PLUS_EXPR:
818     case MINUS_EXPR:
819     case MULT_EXPR:
820     case TRUNC_DIV_EXPR:
821     case TRUNC_MOD_EXPR:
822     case RDIV_EXPR:
823     case LSHIFT_EXPR:
824     case RSHIFT_EXPR:
825     case URSHIFT_EXPR:
826     case BIT_AND_EXPR:
827     case BIT_XOR_EXPR:
828     case BIT_IOR_EXPR:
829     case EQ_EXPR: 
830     case NE_EXPR:
831     case GT_EXPR:
832     case GE_EXPR:
833     case LT_EXPR:
834     case LE_EXPR:
835     case MAX_EXPR:
836     case MIN_EXPR:
837     case ARRAY_REF:
838     case LROTATE_EXPR:
839     case RROTATE_EXPR:
840     case CEIL_DIV_EXPR:
841     case FLOOR_DIV_EXPR:
842     case ROUND_DIV_EXPR:
843     case CEIL_MOD_EXPR:
844     case FLOOR_MOD_EXPR:
845     case ROUND_MOD_EXPR:
846     case EXACT_DIV_EXPR:
847     binop:
848       check_init (TREE_OPERAND (exp, 0), before);
849       /* Avoid needless recursion, especially for COMPOUND_EXPR. */
850       exp = TREE_OPERAND (exp, 1);
851       goto again;
852
853     case RESULT_DECL:
854     case FUNCTION_DECL:
855     case INTEGER_CST:
856     case REAL_CST:
857     case STRING_CST:
858     case JAVA_EXC_OBJ_EXPR:
859       break;
860
861     case NEW_CLASS_EXPR:
862     case CALL_EXPR:
863       {
864         tree func = TREE_OPERAND (exp, 0);
865         tree x = TREE_OPERAND (exp, 1);
866         if (TREE_CODE (func) == ADDR_EXPR)
867           func = TREE_OPERAND (func, 0);
868         check_init (func, before);
869
870         for ( ;  x != NULL_TREE;  x = TREE_CHAIN (x))
871           check_init (TREE_VALUE (x), before);
872         if (func == throw_node)
873           goto never_continues;
874       }
875       break;
876
877     case NEW_ARRAY_INIT:
878       {
879         tree x = CONSTRUCTOR_ELTS (TREE_OPERAND (exp, 0));
880         for ( ;  x != NULL_TREE;  x = TREE_CHAIN (x))
881           check_init (TREE_VALUE (x), before);
882       }
883       break;
884
885     case EXPR_WITH_FILE_LOCATION:
886       {
887         const char *saved_input_filename = input_filename;
888         tree saved_wfl = wfl;
889         tree body = EXPR_WFL_NODE (exp);
890         int saved_lineno = lineno;
891         if (body == empty_stmt_node)
892           break;
893         wfl = exp;
894         input_filename = EXPR_WFL_FILENAME (exp);
895         lineno = EXPR_WFL_LINENO (exp);
896         check_init (body, before);
897         input_filename = saved_input_filename;
898         lineno = saved_lineno;
899         wfl = saved_wfl;
900       }
901       break;
902       
903     default:
904       internal_error
905         ("internal error in check-init: tree code not implemented: %s",
906          tree_code_name [(int) TREE_CODE (exp)]);
907     }
908 }
909
910 void
911 check_for_initialization (body, mdecl)
912      tree body, mdecl;
913 {
914   tree decl;
915   word buf[2];
916   words before = buf;
917   tree owner = DECL_CONTEXT (mdecl);
918   int is_static_method = METHOD_STATIC (mdecl);
919   /* We don't need to check final fields of <init> it it calls this(). */
920   int is_finit_method = DECL_FINIT_P (mdecl) || DECL_INSTINIT_P (mdecl);
921   int is_init_method
922     = (is_finit_method || DECL_CLINIT_P (mdecl)
923        || (DECL_INIT_P (mdecl) && ! DECL_INIT_CALLS_THIS (mdecl)));
924
925   start_current_locals = num_current_locals = 0;
926   num_current_words = 2;
927
928   if (is_init_method)
929     {
930       int words_needed, i;
931       for (decl = TYPE_FIELDS (owner);
932            decl != NULL_TREE;  decl = TREE_CHAIN (decl))
933         {
934           if (DECL_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
935             {
936               if (DECL_FIELD_FINAL_IUD (decl))
937                 DECL_BIT_INDEX (decl) = -1;
938               else
939                 DECL_BIT_INDEX (decl) = num_current_locals++;
940             }
941         }
942       words_needed = WORDS_NEEDED (2 * num_current_locals);
943       if (words_needed > 2)
944         {
945           num_current_words = words_needed;
946           before = ALLOC_WORDS(words_needed);
947         }
948       i = 0;
949       for (decl = TYPE_FIELDS (owner);
950            decl != NULL_TREE;  decl = TREE_CHAIN (decl))
951         {
952           if (FIELD_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
953             {
954               if (! DECL_FIELD_FINAL_IUD (decl))
955                 {
956                   CLEAR_ASSIGNED (before, i);
957                   SET_UNASSIGNED (before, i);
958                   i++;
959                 }
960             }
961         }
962
963     }
964
965   check_init (body, before);
966
967   if (is_init_method)
968     {
969       for (decl = TYPE_FIELDS (owner);
970            decl != NULL_TREE;  decl = TREE_CHAIN (decl))
971         {
972           if (FIELD_FINAL (decl) && FIELD_STATIC (decl) == is_static_method)
973             {
974               int index = DECL_BIT_INDEX (decl);
975               if (index >= 0 && ! ASSIGNED_P (before, index))
976                 {
977                   if (! is_finit_method)
978                     error_with_decl (decl, "final field '%s' may not have been initialized");
979                 }
980               else if (is_finit_method)
981                 DECL_FIELD_FINAL_IUD (decl) = 1;
982
983               /* Re-set to initial state, since we later may use the
984                  same bit for DECL_POINTER_ALIAS_SET. */
985               DECL_BIT_INDEX (decl) = -1;
986             }
987         }
988     }
989
990   start_current_locals = num_current_locals = 0;
991 }
992
993 /* Call for every element in DECL_FUNCTION_INITIALIZED_CLASS_TABLE of
994    a method to consider whether the type indirectly described by ENTRY
995    is definitly initialized and thus remembered as such. */
996
997 bool
998 attach_initialized_static_class (entry, ptr)
999      struct hash_entry *entry;
1000      PTR ptr;
1001 {
1002   struct init_test_hash_entry *ite = (struct init_test_hash_entry *) entry;
1003   tree fndecl = DECL_CONTEXT (ite->init_test_decl);
1004   int index = DECL_BIT_INDEX (ite->init_test_decl);
1005
1006   /* If the initializer flag has been definitly assigned (not taking
1007      into account its first mandatory assignment which has been
1008      already added but escaped analysis.) */
1009   if (fndecl && METHOD_STATIC (fndecl)
1010       && (DECL_INITIAL (ite->init_test_decl) == boolean_true_node
1011           || (index >= 0 && ASSIGNED_P (((word *) ptr), index))))
1012     hash_lookup (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl),
1013                  entry->key, TRUE, NULL);
1014   return true;
1015 }