src/share/classes/com/sun/tools/javac/comp/Flow.java

Thu, 05 Aug 2010 09:44:54 +0100

author
mcimadamore
date
Thu, 05 Aug 2010 09:44:54 +0100
changeset 629
0fe472f4a332
parent 617
62f3f07002ea
child 676
bfdfc13fe641
permissions
-rw-r--r--

6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
Summary: default annotation value is not attributed
Reviewed-by: jjg, darcy

     1 /*
     2  * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 //todo: one might eliminate uninits.andSets when monotonic
    28 package com.sun.tools.javac.comp;
    30 import java.util.HashMap;
    31 import java.util.Map;
    32 import java.util.LinkedHashMap;
    34 import com.sun.tools.javac.code.*;
    35 import com.sun.tools.javac.tree.*;
    36 import com.sun.tools.javac.util.*;
    37 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
    39 import com.sun.tools.javac.code.Symbol.*;
    40 import com.sun.tools.javac.comp.Resolve;
    41 import com.sun.tools.javac.tree.JCTree.*;
    43 import static com.sun.tools.javac.code.Flags.*;
    44 import static com.sun.tools.javac.code.Kinds.*;
    45 import static com.sun.tools.javac.code.TypeTags.*;
    47 /** This pass implements dataflow analysis for Java programs.
    48  *  Liveness analysis checks that every statement is reachable.
    49  *  Exception analysis ensures that every checked exception that is
    50  *  thrown is declared or caught.  Definite assignment analysis
    51  *  ensures that each variable is assigned when used.  Definite
    52  *  unassignment analysis ensures that no final variable is assigned
    53  *  more than once.
    54  *
    55  *  <p>The second edition of the JLS has a number of problems in the
    56  *  specification of these flow analysis problems. This implementation
    57  *  attempts to address those issues.
    58  *
    59  *  <p>First, there is no accommodation for a finally clause that cannot
    60  *  complete normally. For liveness analysis, an intervening finally
    61  *  clause can cause a break, continue, or return not to reach its
    62  *  target.  For exception analysis, an intervening finally clause can
    63  *  cause any exception to be "caught".  For DA/DU analysis, the finally
    64  *  clause can prevent a transfer of control from propagating DA/DU
    65  *  state to the target.  In addition, code in the finally clause can
    66  *  affect the DA/DU status of variables.
    67  *
    68  *  <p>For try statements, we introduce the idea of a variable being
    69  *  definitely unassigned "everywhere" in a block.  A variable V is
    70  *  "unassigned everywhere" in a block iff it is unassigned at the
    71  *  beginning of the block and there is no reachable assignment to V
    72  *  in the block.  An assignment V=e is reachable iff V is not DA
    73  *  after e.  Then we can say that V is DU at the beginning of the
    74  *  catch block iff V is DU everywhere in the try block.  Similarly, V
    75  *  is DU at the beginning of the finally block iff V is DU everywhere
    76  *  in the try block and in every catch block.  Specifically, the
    77  *  following bullet is added to 16.2.2
    78  *  <pre>
    79  *      V is <em>unassigned everywhere</em> in a block if it is
    80  *      unassigned before the block and there is no reachable
    81  *      assignment to V within the block.
    82  *  </pre>
    83  *  <p>In 16.2.15, the third bullet (and all of its sub-bullets) for all
    84  *  try blocks is changed to
    85  *  <pre>
    86  *      V is definitely unassigned before a catch block iff V is
    87  *      definitely unassigned everywhere in the try block.
    88  *  </pre>
    89  *  <p>The last bullet (and all of its sub-bullets) for try blocks that
    90  *  have a finally block is changed to
    91  *  <pre>
    92  *      V is definitely unassigned before the finally block iff
    93  *      V is definitely unassigned everywhere in the try block
    94  *      and everywhere in each catch block of the try statement.
    95  *  </pre>
    96  *  <p>In addition,
    97  *  <pre>
    98  *      V is definitely assigned at the end of a constructor iff
    99  *      V is definitely assigned after the block that is the body
   100  *      of the constructor and V is definitely assigned at every
   101  *      return that can return from the constructor.
   102  *  </pre>
   103  *  <p>In addition, each continue statement with the loop as its target
   104  *  is treated as a jump to the end of the loop body, and "intervening"
   105  *  finally clauses are treated as follows: V is DA "due to the
   106  *  continue" iff V is DA before the continue statement or V is DA at
   107  *  the end of any intervening finally block.  V is DU "due to the
   108  *  continue" iff any intervening finally cannot complete normally or V
   109  *  is DU at the end of every intervening finally block.  This "due to
   110  *  the continue" concept is then used in the spec for the loops.
   111  *
   112  *  <p>Similarly, break statements must consider intervening finally
   113  *  blocks.  For liveness analysis, a break statement for which any
   114  *  intervening finally cannot complete normally is not considered to
   115  *  cause the target statement to be able to complete normally. Then
   116  *  we say V is DA "due to the break" iff V is DA before the break or
   117  *  V is DA at the end of any intervening finally block.  V is DU "due
   118  *  to the break" iff any intervening finally cannot complete normally
   119  *  or V is DU at the break and at the end of every intervening
   120  *  finally block.  (I suspect this latter condition can be
   121  *  simplified.)  This "due to the break" is then used in the spec for
   122  *  all statements that can be "broken".
   123  *
   124  *  <p>The return statement is treated similarly.  V is DA "due to a
   125  *  return statement" iff V is DA before the return statement or V is
   126  *  DA at the end of any intervening finally block.  Note that we
   127  *  don't have to worry about the return expression because this
   128  *  concept is only used for construcrors.
   129  *
   130  *  <p>There is no spec in JLS2 for when a variable is definitely
   131  *  assigned at the end of a constructor, which is needed for final
   132  *  fields (8.3.1.2).  We implement the rule that V is DA at the end
   133  *  of the constructor iff it is DA and the end of the body of the
   134  *  constructor and V is DA "due to" every return of the constructor.
   135  *
   136  *  <p>Intervening finally blocks similarly affect exception analysis.  An
   137  *  intervening finally that cannot complete normally allows us to ignore
   138  *  an otherwise uncaught exception.
   139  *
   140  *  <p>To implement the semantics of intervening finally clauses, all
   141  *  nonlocal transfers (break, continue, return, throw, method call that
   142  *  can throw a checked exception, and a constructor invocation that can
   143  *  thrown a checked exception) are recorded in a queue, and removed
   144  *  from the queue when we complete processing the target of the
   145  *  nonlocal transfer.  This allows us to modify the queue in accordance
   146  *  with the above rules when we encounter a finally clause.  The only
   147  *  exception to this [no pun intended] is that checked exceptions that
   148  *  are known to be caught or declared to be caught in the enclosing
   149  *  method are not recorded in the queue, but instead are recorded in a
   150  *  global variable "Set<Type> thrown" that records the type of all
   151  *  exceptions that can be thrown.
   152  *
   153  *  <p>Other minor issues the treatment of members of other classes
   154  *  (always considered DA except that within an anonymous class
   155  *  constructor, where DA status from the enclosing scope is
   156  *  preserved), treatment of the case expression (V is DA before the
   157  *  case expression iff V is DA after the switch expression),
   158  *  treatment of variables declared in a switch block (the implied
   159  *  DA/DU status after the switch expression is DU and not DA for
   160  *  variables defined in a switch block), the treatment of boolean ?:
   161  *  expressions (The JLS rules only handle b and c non-boolean; the
   162  *  new rule is that if b and c are boolean valued, then V is
   163  *  (un)assigned after a?b:c when true/false iff V is (un)assigned
   164  *  after b when true/false and V is (un)assigned after c when
   165  *  true/false).
   166  *
   167  *  <p>There is the remaining question of what syntactic forms constitute a
   168  *  reference to a variable.  It is conventional to allow this.x on the
   169  *  left-hand-side to initialize a final instance field named x, yet
   170  *  this.x isn't considered a "use" when appearing on a right-hand-side
   171  *  in most implementations.  Should parentheses affect what is
   172  *  considered a variable reference?  The simplest rule would be to
   173  *  allow unqualified forms only, parentheses optional, and phase out
   174  *  support for assigning to a final field via this.x.
   175  *
   176  *  <p><b>This is NOT part of any supported API.
   177  *  If you write code that depends on this, you do so at your own risk.
   178  *  This code and its internal interfaces are subject to change or
   179  *  deletion without notice.</b>
   180  */
   181 public class Flow extends TreeScanner {
   182     protected static final Context.Key<Flow> flowKey =
   183         new Context.Key<Flow>();
   185     private final Names names;
   186     private final Log log;
   187     private final Symtab syms;
   188     private final Types types;
   189     private final Check chk;
   190     private       TreeMaker make;
   191     private final Resolve rs;
   192     private Env<AttrContext> attrEnv;
   193     private       Lint lint;
   194     private final boolean allowRethrowAnalysis;
   196     public static Flow instance(Context context) {
   197         Flow instance = context.get(flowKey);
   198         if (instance == null)
   199             instance = new Flow(context);
   200         return instance;
   201     }
   203     protected Flow(Context context) {
   204         context.put(flowKey, this);
   205         names = Names.instance(context);
   206         log = Log.instance(context);
   207         syms = Symtab.instance(context);
   208         types = Types.instance(context);
   209         chk = Check.instance(context);
   210         lint = Lint.instance(context);
   211         rs = Resolve.instance(context);
   212         Source source = Source.instance(context);
   213         allowRethrowAnalysis = source.allowMulticatch();
   214     }
   216     /** A flag that indicates whether the last statement could
   217      *  complete normally.
   218      */
   219     private boolean alive;
   221     /** The set of definitely assigned variables.
   222      */
   223     Bits inits;
   225     /** The set of definitely unassigned variables.
   226      */
   227     Bits uninits;
   229     HashMap<Symbol, List<Type>> multicatchTypes;
   231     /** The set of variables that are definitely unassigned everywhere
   232      *  in current try block. This variable is maintained lazily; it is
   233      *  updated only when something gets removed from uninits,
   234      *  typically by being assigned in reachable code.  To obtain the
   235      *  correct set of variables which are definitely unassigned
   236      *  anywhere in current try block, intersect uninitsTry and
   237      *  uninits.
   238      */
   239     Bits uninitsTry;
   241     /** When analyzing a condition, inits and uninits are null.
   242      *  Instead we have:
   243      */
   244     Bits initsWhenTrue;
   245     Bits initsWhenFalse;
   246     Bits uninitsWhenTrue;
   247     Bits uninitsWhenFalse;
   249     /** A mapping from addresses to variable symbols.
   250      */
   251     VarSymbol[] vars;
   253     /** The current class being defined.
   254      */
   255     JCClassDecl classDef;
   257     /** The first variable sequence number in this class definition.
   258      */
   259     int firstadr;
   261     /** The next available variable sequence number.
   262      */
   263     int nextadr;
   265     /** The list of possibly thrown declarable exceptions.
   266      */
   267     List<Type> thrown;
   269     /** The list of exceptions that are either caught or declared to be
   270      *  thrown.
   271      */
   272     List<Type> caught;
   274     /** The list of unreferenced automatic resources.
   275      */
   276     Map<VarSymbol, JCVariableDecl> unrefdResources;
   278     /** Set when processing a loop body the second time for DU analysis. */
   279     boolean loopPassTwo = false;
   281     /*-------------------- Environments ----------------------*/
   283     /** A pending exit.  These are the statements return, break, and
   284      *  continue.  In addition, exception-throwing expressions or
   285      *  statements are put here when not known to be caught.  This
   286      *  will typically result in an error unless it is within a
   287      *  try-finally whose finally block cannot complete normally.
   288      */
   289     static class PendingExit {
   290         JCTree tree;
   291         Bits inits;
   292         Bits uninits;
   293         Type thrown;
   294         PendingExit(JCTree tree, Bits inits, Bits uninits) {
   295             this.tree = tree;
   296             this.inits = inits.dup();
   297             this.uninits = uninits.dup();
   298         }
   299         PendingExit(JCTree tree, Type thrown) {
   300             this.tree = tree;
   301             this.thrown = thrown;
   302         }
   303     }
   305     /** The currently pending exits that go from current inner blocks
   306      *  to an enclosing block, in source order.
   307      */
   308     ListBuffer<PendingExit> pendingExits;
   310     /*-------------------- Exceptions ----------------------*/
   312     /** Complain that pending exceptions are not caught.
   313      */
   314     void errorUncaught() {
   315         for (PendingExit exit = pendingExits.next();
   316              exit != null;
   317              exit = pendingExits.next()) {
   318             boolean synthetic = classDef != null &&
   319                 classDef.pos == exit.tree.pos;
   320             log.error(exit.tree.pos(),
   321                       synthetic
   322                       ? "unreported.exception.default.constructor"
   323                       : "unreported.exception.need.to.catch.or.throw",
   324                       exit.thrown);
   325         }
   326     }
   328     /** Record that exception is potentially thrown and check that it
   329      *  is caught.
   330      */
   331     void markThrown(JCTree tree, Type exc) {
   332         if (!chk.isUnchecked(tree.pos(), exc)) {
   333             if (!chk.isHandled(exc, caught))
   334                 pendingExits.append(new PendingExit(tree, exc));
   335             thrown = chk.incl(exc, thrown);
   336         }
   337     }
   339     /*-------------- Processing variables ----------------------*/
   341     /** Do we need to track init/uninit state of this symbol?
   342      *  I.e. is symbol either a local or a blank final variable?
   343      */
   344     boolean trackable(VarSymbol sym) {
   345         return
   346             (sym.owner.kind == MTH ||
   347              ((sym.flags() & (FINAL | HASINIT | PARAMETER)) == FINAL &&
   348               classDef.sym.isEnclosedBy((ClassSymbol)sym.owner)));
   349     }
   351     /** Initialize new trackable variable by setting its address field
   352      *  to the next available sequence number and entering it under that
   353      *  index into the vars array.
   354      */
   355     void newVar(VarSymbol sym) {
   356         if (nextadr == vars.length) {
   357             VarSymbol[] newvars = new VarSymbol[nextadr * 2];
   358             System.arraycopy(vars, 0, newvars, 0, nextadr);
   359             vars = newvars;
   360         }
   361         sym.adr = nextadr;
   362         vars[nextadr] = sym;
   363         inits.excl(nextadr);
   364         uninits.incl(nextadr);
   365         nextadr++;
   366     }
   368     /** Record an initialization of a trackable variable.
   369      */
   370     void letInit(DiagnosticPosition pos, VarSymbol sym) {
   371         if (sym.adr >= firstadr && trackable(sym)) {
   372             if ((sym.flags() & FINAL) != 0) {
   373                 if ((sym.flags() & PARAMETER) != 0) {
   374                     if ((sym.flags() & DISJOINT) != 0) { //multi-catch parameter
   375                         log.error(pos, "multicatch.parameter.may.not.be.assigned",
   376                                   sym);
   377                     }
   378                     else {
   379                         log.error(pos, "final.parameter.may.not.be.assigned",
   380                               sym);
   381                     }
   382                 } else if (!uninits.isMember(sym.adr)) {
   383                     log.error(pos,
   384                               loopPassTwo
   385                               ? "var.might.be.assigned.in.loop"
   386                               : "var.might.already.be.assigned",
   387                               sym);
   388                 } else if (!inits.isMember(sym.adr)) {
   389                     // reachable assignment
   390                     uninits.excl(sym.adr);
   391                     uninitsTry.excl(sym.adr);
   392                 } else {
   393                     //log.rawWarning(pos, "unreachable assignment");//DEBUG
   394                     uninits.excl(sym.adr);
   395                 }
   396             }
   397             inits.incl(sym.adr);
   398         } else if ((sym.flags() & FINAL) != 0) {
   399             log.error(pos, "var.might.already.be.assigned", sym);
   400         }
   401     }
   403     /** If tree is either a simple name or of the form this.name or
   404      *  C.this.name, and tree represents a trackable variable,
   405      *  record an initialization of the variable.
   406      */
   407     void letInit(JCTree tree) {
   408         tree = TreeInfo.skipParens(tree);
   409         if (tree.getTag() == JCTree.IDENT || tree.getTag() == JCTree.SELECT) {
   410             Symbol sym = TreeInfo.symbol(tree);
   411             letInit(tree.pos(), (VarSymbol)sym);
   412         }
   413     }
   415     /** Check that trackable variable is initialized.
   416      */
   417     void checkInit(DiagnosticPosition pos, VarSymbol sym) {
   418         if ((sym.adr >= firstadr || sym.owner.kind != TYP) &&
   419             trackable(sym) &&
   420             !inits.isMember(sym.adr)) {
   421             log.error(pos, "var.might.not.have.been.initialized",
   422                       sym);
   423             inits.incl(sym.adr);
   424         }
   425     }
   427     /*-------------------- Handling jumps ----------------------*/
   429     /** Record an outward transfer of control. */
   430     void recordExit(JCTree tree) {
   431         pendingExits.append(new PendingExit(tree, inits, uninits));
   432         markDead();
   433     }
   435     /** Resolve all breaks of this statement. */
   436     boolean resolveBreaks(JCTree tree,
   437                           ListBuffer<PendingExit> oldPendingExits) {
   438         boolean result = false;
   439         List<PendingExit> exits = pendingExits.toList();
   440         pendingExits = oldPendingExits;
   441         for (; exits.nonEmpty(); exits = exits.tail) {
   442             PendingExit exit = exits.head;
   443             if (exit.tree.getTag() == JCTree.BREAK &&
   444                 ((JCBreak) exit.tree).target == tree) {
   445                 inits.andSet(exit.inits);
   446                 uninits.andSet(exit.uninits);
   447                 result = true;
   448             } else {
   449                 pendingExits.append(exit);
   450             }
   451         }
   452         return result;
   453     }
   455     /** Resolve all continues of this statement. */
   456     boolean resolveContinues(JCTree tree) {
   457         boolean result = false;
   458         List<PendingExit> exits = pendingExits.toList();
   459         pendingExits = new ListBuffer<PendingExit>();
   460         for (; exits.nonEmpty(); exits = exits.tail) {
   461             PendingExit exit = exits.head;
   462             if (exit.tree.getTag() == JCTree.CONTINUE &&
   463                 ((JCContinue) exit.tree).target == tree) {
   464                 inits.andSet(exit.inits);
   465                 uninits.andSet(exit.uninits);
   466                 result = true;
   467             } else {
   468                 pendingExits.append(exit);
   469             }
   470         }
   471         return result;
   472     }
   474     /** Record that statement is unreachable.
   475      */
   476     void markDead() {
   477         inits.inclRange(firstadr, nextadr);
   478         uninits.inclRange(firstadr, nextadr);
   479         alive = false;
   480     }
   482     /** Split (duplicate) inits/uninits into WhenTrue/WhenFalse sets
   483      */
   484     void split() {
   485         initsWhenFalse = inits.dup();
   486         uninitsWhenFalse = uninits.dup();
   487         initsWhenTrue = inits;
   488         uninitsWhenTrue = uninits;
   489         inits = uninits = null;
   490     }
   492     /** Merge (intersect) inits/uninits from WhenTrue/WhenFalse sets.
   493      */
   494     void merge() {
   495         inits = initsWhenFalse.andSet(initsWhenTrue);
   496         uninits = uninitsWhenFalse.andSet(uninitsWhenTrue);
   497     }
   499 /* ************************************************************************
   500  * Visitor methods for statements and definitions
   501  *************************************************************************/
   503     /** Analyze a definition.
   504      */
   505     void scanDef(JCTree tree) {
   506         scanStat(tree);
   507         if (tree != null && tree.getTag() == JCTree.BLOCK && !alive) {
   508             log.error(tree.pos(),
   509                       "initializer.must.be.able.to.complete.normally");
   510         }
   511     }
   513     /** Analyze a statement. Check that statement is reachable.
   514      */
   515     void scanStat(JCTree tree) {
   516         if (!alive && tree != null) {
   517             log.error(tree.pos(), "unreachable.stmt");
   518             if (tree.getTag() != JCTree.SKIP) alive = true;
   519         }
   520         scan(tree);
   521     }
   523     /** Analyze list of statements.
   524      */
   525     void scanStats(List<? extends JCStatement> trees) {
   526         if (trees != null)
   527             for (List<? extends JCStatement> l = trees; l.nonEmpty(); l = l.tail)
   528                 scanStat(l.head);
   529     }
   531     /** Analyze an expression. Make sure to set (un)inits rather than
   532      *  (un)initsWhenTrue(WhenFalse) on exit.
   533      */
   534     void scanExpr(JCTree tree) {
   535         if (tree != null) {
   536             scan(tree);
   537             if (inits == null) merge();
   538         }
   539     }
   541     /** Analyze a list of expressions.
   542      */
   543     void scanExprs(List<? extends JCExpression> trees) {
   544         if (trees != null)
   545             for (List<? extends JCExpression> l = trees; l.nonEmpty(); l = l.tail)
   546                 scanExpr(l.head);
   547     }
   549     /** Analyze a condition. Make sure to set (un)initsWhenTrue(WhenFalse)
   550      *  rather than (un)inits on exit.
   551      */
   552     void scanCond(JCTree tree) {
   553         if (tree.type.isFalse()) {
   554             if (inits == null) merge();
   555             initsWhenTrue = inits.dup();
   556             initsWhenTrue.inclRange(firstadr, nextadr);
   557             uninitsWhenTrue = uninits.dup();
   558             uninitsWhenTrue.inclRange(firstadr, nextadr);
   559             initsWhenFalse = inits;
   560             uninitsWhenFalse = uninits;
   561         } else if (tree.type.isTrue()) {
   562             if (inits == null) merge();
   563             initsWhenFalse = inits.dup();
   564             initsWhenFalse.inclRange(firstadr, nextadr);
   565             uninitsWhenFalse = uninits.dup();
   566             uninitsWhenFalse.inclRange(firstadr, nextadr);
   567             initsWhenTrue = inits;
   568             uninitsWhenTrue = uninits;
   569         } else {
   570             scan(tree);
   571             if (inits != null) split();
   572         }
   573         inits = uninits = null;
   574     }
   576     /* ------------ Visitor methods for various sorts of trees -------------*/
   578     public void visitClassDef(JCClassDecl tree) {
   579         if (tree.sym == null) return;
   581         JCClassDecl classDefPrev = classDef;
   582         List<Type> thrownPrev = thrown;
   583         List<Type> caughtPrev = caught;
   584         boolean alivePrev = alive;
   585         int firstadrPrev = firstadr;
   586         int nextadrPrev = nextadr;
   587         ListBuffer<PendingExit> pendingExitsPrev = pendingExits;
   588         Lint lintPrev = lint;
   590         pendingExits = new ListBuffer<PendingExit>();
   591         if (tree.name != names.empty) {
   592             caught = List.nil();
   593             firstadr = nextadr;
   594         }
   595         classDef = tree;
   596         thrown = List.nil();
   597         lint = lint.augment(tree.sym.attributes_field);
   599         try {
   600             // define all the static fields
   601             for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
   602                 if (l.head.getTag() == JCTree.VARDEF) {
   603                     JCVariableDecl def = (JCVariableDecl)l.head;
   604                     if ((def.mods.flags & STATIC) != 0) {
   605                         VarSymbol sym = def.sym;
   606                         if (trackable(sym))
   607                             newVar(sym);
   608                     }
   609                 }
   610             }
   612             // process all the static initializers
   613             for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
   614                 if (l.head.getTag() != JCTree.METHODDEF &&
   615                     (TreeInfo.flags(l.head) & STATIC) != 0) {
   616                     scanDef(l.head);
   617                     errorUncaught();
   618                 }
   619             }
   621             // add intersection of all thrown clauses of initial constructors
   622             // to set of caught exceptions, unless class is anonymous.
   623             if (tree.name != names.empty) {
   624                 boolean firstConstructor = true;
   625                 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
   626                     if (TreeInfo.isInitialConstructor(l.head)) {
   627                         List<Type> mthrown =
   628                             ((JCMethodDecl) l.head).sym.type.getThrownTypes();
   629                         if (firstConstructor) {
   630                             caught = mthrown;
   631                             firstConstructor = false;
   632                         } else {
   633                             caught = chk.intersect(mthrown, caught);
   634                         }
   635                     }
   636                 }
   637             }
   639             // define all the instance fields
   640             for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
   641                 if (l.head.getTag() == JCTree.VARDEF) {
   642                     JCVariableDecl def = (JCVariableDecl)l.head;
   643                     if ((def.mods.flags & STATIC) == 0) {
   644                         VarSymbol sym = def.sym;
   645                         if (trackable(sym))
   646                             newVar(sym);
   647                     }
   648                 }
   649             }
   651             // process all the instance initializers
   652             for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
   653                 if (l.head.getTag() != JCTree.METHODDEF &&
   654                     (TreeInfo.flags(l.head) & STATIC) == 0) {
   655                     scanDef(l.head);
   656                     errorUncaught();
   657                 }
   658             }
   660             // in an anonymous class, add the set of thrown exceptions to
   661             // the throws clause of the synthetic constructor and propagate
   662             // outwards.
   663             if (tree.name == names.empty) {
   664                 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
   665                     if (TreeInfo.isInitialConstructor(l.head)) {
   666                         JCMethodDecl mdef = (JCMethodDecl)l.head;
   667                         mdef.thrown = make.Types(thrown);
   668                         mdef.sym.type.setThrown(thrown);
   669                     }
   670                 }
   671                 thrownPrev = chk.union(thrown, thrownPrev);
   672             }
   674             // process all the methods
   675             for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
   676                 if (l.head.getTag() == JCTree.METHODDEF) {
   677                     scan(l.head);
   678                     errorUncaught();
   679                 }
   680             }
   682             thrown = thrownPrev;
   683         } finally {
   684             pendingExits = pendingExitsPrev;
   685             alive = alivePrev;
   686             nextadr = nextadrPrev;
   687             firstadr = firstadrPrev;
   688             caught = caughtPrev;
   689             classDef = classDefPrev;
   690             lint = lintPrev;
   691         }
   692     }
   694     public void visitMethodDef(JCMethodDecl tree) {
   695         if (tree.body == null) return;
   697         List<Type> caughtPrev = caught;
   698         List<Type> mthrown = tree.sym.type.getThrownTypes();
   699         Bits initsPrev = inits.dup();
   700         Bits uninitsPrev = uninits.dup();
   701         int nextadrPrev = nextadr;
   702         int firstadrPrev = firstadr;
   703         Lint lintPrev = lint;
   705         lint = lint.augment(tree.sym.attributes_field);
   707         assert pendingExits.isEmpty();
   709         try {
   710             boolean isInitialConstructor =
   711                 TreeInfo.isInitialConstructor(tree);
   713             if (!isInitialConstructor)
   714                 firstadr = nextadr;
   715             for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
   716                 JCVariableDecl def = l.head;
   717                 scan(def);
   718                 inits.incl(def.sym.adr);
   719                 uninits.excl(def.sym.adr);
   720             }
   721             if (isInitialConstructor)
   722                 caught = chk.union(caught, mthrown);
   723             else if ((tree.sym.flags() & (BLOCK | STATIC)) != BLOCK)
   724                 caught = mthrown;
   725             // else we are in an instance initializer block;
   726             // leave caught unchanged.
   728             alive = true;
   729             scanStat(tree.body);
   731             if (alive && tree.sym.type.getReturnType().tag != VOID)
   732                 log.error(TreeInfo.diagEndPos(tree.body), "missing.ret.stmt");
   734             if (isInitialConstructor) {
   735                 for (int i = firstadr; i < nextadr; i++)
   736                     if (vars[i].owner == classDef.sym)
   737                         checkInit(TreeInfo.diagEndPos(tree.body), vars[i]);
   738             }
   739             List<PendingExit> exits = pendingExits.toList();
   740             pendingExits = new ListBuffer<PendingExit>();
   741             while (exits.nonEmpty()) {
   742                 PendingExit exit = exits.head;
   743                 exits = exits.tail;
   744                 if (exit.thrown == null) {
   745                     assert exit.tree.getTag() == JCTree.RETURN;
   746                     if (isInitialConstructor) {
   747                         inits = exit.inits;
   748                         for (int i = firstadr; i < nextadr; i++)
   749                             checkInit(exit.tree.pos(), vars[i]);
   750                     }
   751                 } else {
   752                     // uncaught throws will be reported later
   753                     pendingExits.append(exit);
   754                 }
   755             }
   756         } finally {
   757             inits = initsPrev;
   758             uninits = uninitsPrev;
   759             nextadr = nextadrPrev;
   760             firstadr = firstadrPrev;
   761             caught = caughtPrev;
   762             lint = lintPrev;
   763         }
   764     }
   766     public void visitVarDef(JCVariableDecl tree) {
   767         boolean track = trackable(tree.sym);
   768         if (track && tree.sym.owner.kind == MTH) newVar(tree.sym);
   769         if (tree.init != null) {
   770             Lint lintPrev = lint;
   771             lint = lint.augment(tree.sym.attributes_field);
   772             try{
   773                 scanExpr(tree.init);
   774                 if (track) letInit(tree.pos(), tree.sym);
   775             } finally {
   776                 lint = lintPrev;
   777             }
   778         }
   779     }
   781     public void visitBlock(JCBlock tree) {
   782         int nextadrPrev = nextadr;
   783         scanStats(tree.stats);
   784         nextadr = nextadrPrev;
   785     }
   787     public void visitDoLoop(JCDoWhileLoop tree) {
   788         ListBuffer<PendingExit> prevPendingExits = pendingExits;
   789         boolean prevLoopPassTwo = loopPassTwo;
   790         pendingExits = new ListBuffer<PendingExit>();
   791         do {
   792             Bits uninitsEntry = uninits.dup();
   793             scanStat(tree.body);
   794             alive |= resolveContinues(tree);
   795             scanCond(tree.cond);
   796             if (log.nerrors != 0 ||
   797                 loopPassTwo ||
   798                 uninitsEntry.diffSet(uninitsWhenTrue).nextBit(firstadr)==-1)
   799                 break;
   800             inits = initsWhenTrue;
   801             uninits = uninitsEntry.andSet(uninitsWhenTrue);
   802             loopPassTwo = true;
   803             alive = true;
   804         } while (true);
   805         loopPassTwo = prevLoopPassTwo;
   806         inits = initsWhenFalse;
   807         uninits = uninitsWhenFalse;
   808         alive = alive && !tree.cond.type.isTrue();
   809         alive |= resolveBreaks(tree, prevPendingExits);
   810     }
   812     public void visitWhileLoop(JCWhileLoop tree) {
   813         ListBuffer<PendingExit> prevPendingExits = pendingExits;
   814         boolean prevLoopPassTwo = loopPassTwo;
   815         Bits initsCond;
   816         Bits uninitsCond;
   817         pendingExits = new ListBuffer<PendingExit>();
   818         do {
   819             Bits uninitsEntry = uninits.dup();
   820             scanCond(tree.cond);
   821             initsCond = initsWhenFalse;
   822             uninitsCond = uninitsWhenFalse;
   823             inits = initsWhenTrue;
   824             uninits = uninitsWhenTrue;
   825             alive = !tree.cond.type.isFalse();
   826             scanStat(tree.body);
   827             alive |= resolveContinues(tree);
   828             if (log.nerrors != 0 ||
   829                 loopPassTwo ||
   830                 uninitsEntry.diffSet(uninits).nextBit(firstadr) == -1)
   831                 break;
   832             uninits = uninitsEntry.andSet(uninits);
   833             loopPassTwo = true;
   834             alive = true;
   835         } while (true);
   836         loopPassTwo = prevLoopPassTwo;
   837         inits = initsCond;
   838         uninits = uninitsCond;
   839         alive = resolveBreaks(tree, prevPendingExits) ||
   840             !tree.cond.type.isTrue();
   841     }
   843     public void visitForLoop(JCForLoop tree) {
   844         ListBuffer<PendingExit> prevPendingExits = pendingExits;
   845         boolean prevLoopPassTwo = loopPassTwo;
   846         int nextadrPrev = nextadr;
   847         scanStats(tree.init);
   848         Bits initsCond;
   849         Bits uninitsCond;
   850         pendingExits = new ListBuffer<PendingExit>();
   851         do {
   852             Bits uninitsEntry = uninits.dup();
   853             if (tree.cond != null) {
   854                 scanCond(tree.cond);
   855                 initsCond = initsWhenFalse;
   856                 uninitsCond = uninitsWhenFalse;
   857                 inits = initsWhenTrue;
   858                 uninits = uninitsWhenTrue;
   859                 alive = !tree.cond.type.isFalse();
   860             } else {
   861                 initsCond = inits.dup();
   862                 initsCond.inclRange(firstadr, nextadr);
   863                 uninitsCond = uninits.dup();
   864                 uninitsCond.inclRange(firstadr, nextadr);
   865                 alive = true;
   866             }
   867             scanStat(tree.body);
   868             alive |= resolveContinues(tree);
   869             scan(tree.step);
   870             if (log.nerrors != 0 ||
   871                 loopPassTwo ||
   872                 uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
   873                 break;
   874             uninits = uninitsEntry.andSet(uninits);
   875             loopPassTwo = true;
   876             alive = true;
   877         } while (true);
   878         loopPassTwo = prevLoopPassTwo;
   879         inits = initsCond;
   880         uninits = uninitsCond;
   881         alive = resolveBreaks(tree, prevPendingExits) ||
   882             tree.cond != null && !tree.cond.type.isTrue();
   883         nextadr = nextadrPrev;
   884     }
   886     public void visitForeachLoop(JCEnhancedForLoop tree) {
   887         visitVarDef(tree.var);
   889         ListBuffer<PendingExit> prevPendingExits = pendingExits;
   890         boolean prevLoopPassTwo = loopPassTwo;
   891         int nextadrPrev = nextadr;
   892         scan(tree.expr);
   893         Bits initsStart = inits.dup();
   894         Bits uninitsStart = uninits.dup();
   896         letInit(tree.pos(), tree.var.sym);
   897         pendingExits = new ListBuffer<PendingExit>();
   898         do {
   899             Bits uninitsEntry = uninits.dup();
   900             scanStat(tree.body);
   901             alive |= resolveContinues(tree);
   902             if (log.nerrors != 0 ||
   903                 loopPassTwo ||
   904                 uninitsEntry.diffSet(uninits).nextBit(firstadr) == -1)
   905                 break;
   906             uninits = uninitsEntry.andSet(uninits);
   907             loopPassTwo = true;
   908             alive = true;
   909         } while (true);
   910         loopPassTwo = prevLoopPassTwo;
   911         inits = initsStart;
   912         uninits = uninitsStart.andSet(uninits);
   913         resolveBreaks(tree, prevPendingExits);
   914         alive = true;
   915         nextadr = nextadrPrev;
   916     }
   918     public void visitLabelled(JCLabeledStatement tree) {
   919         ListBuffer<PendingExit> prevPendingExits = pendingExits;
   920         pendingExits = new ListBuffer<PendingExit>();
   921         scanStat(tree.body);
   922         alive |= resolveBreaks(tree, prevPendingExits);
   923     }
   925     public void visitSwitch(JCSwitch tree) {
   926         ListBuffer<PendingExit> prevPendingExits = pendingExits;
   927         pendingExits = new ListBuffer<PendingExit>();
   928         int nextadrPrev = nextadr;
   929         scanExpr(tree.selector);
   930         Bits initsSwitch = inits;
   931         Bits uninitsSwitch = uninits.dup();
   932         boolean hasDefault = false;
   933         for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
   934             alive = true;
   935             inits = initsSwitch.dup();
   936             uninits = uninits.andSet(uninitsSwitch);
   937             JCCase c = l.head;
   938             if (c.pat == null)
   939                 hasDefault = true;
   940             else
   941                 scanExpr(c.pat);
   942             scanStats(c.stats);
   943             addVars(c.stats, initsSwitch, uninitsSwitch);
   944             // Warn about fall-through if lint switch fallthrough enabled.
   945             if (!loopPassTwo &&
   946                 alive &&
   947                 lint.isEnabled(Lint.LintCategory.FALLTHROUGH) &&
   948                 c.stats.nonEmpty() && l.tail.nonEmpty())
   949                 log.warning(Lint.LintCategory.FALLTHROUGH,
   950                             l.tail.head.pos(),
   951                             "possible.fall-through.into.case");
   952         }
   953         if (!hasDefault) {
   954             inits.andSet(initsSwitch);
   955             alive = true;
   956         }
   957         alive |= resolveBreaks(tree, prevPendingExits);
   958         nextadr = nextadrPrev;
   959     }
   960     // where
   961         /** Add any variables defined in stats to inits and uninits. */
   962         private static void addVars(List<JCStatement> stats, Bits inits,
   963                                     Bits uninits) {
   964             for (;stats.nonEmpty(); stats = stats.tail) {
   965                 JCTree stat = stats.head;
   966                 if (stat.getTag() == JCTree.VARDEF) {
   967                     int adr = ((JCVariableDecl) stat).sym.adr;
   968                     inits.excl(adr);
   969                     uninits.incl(adr);
   970                 }
   971             }
   972         }
   974     public void visitTry(JCTry tree) {
   975         List<Type> caughtPrev = caught;
   976         List<Type> thrownPrev = thrown;
   977         Map<VarSymbol, JCVariableDecl> unrefdResourcesPrev = unrefdResources;
   978         thrown = List.nil();
   979         for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
   980             List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ?
   981                     ((JCTypeDisjoint)l.head.param.vartype).components :
   982                     List.of(l.head.param.vartype);
   983             for (JCExpression ct : subClauses) {
   984                 caught = chk.incl(ct.type, caught);
   985             }
   986         }
   987         Bits uninitsTryPrev = uninitsTry;
   988         ListBuffer<PendingExit> prevPendingExits = pendingExits;
   989         pendingExits = new ListBuffer<PendingExit>();
   990         Bits initsTry = inits.dup();
   991         uninitsTry = uninits.dup();
   992         unrefdResources = new LinkedHashMap<VarSymbol, JCVariableDecl>();
   993         for (JCTree resource : tree.resources) {
   994             if (resource instanceof JCVariableDecl) {
   995                 JCVariableDecl vdecl = (JCVariableDecl) resource;
   996                 visitVarDef(vdecl);
   997                 unrefdResources.put(vdecl.sym, vdecl);
   998             } else if (resource instanceof JCExpression) {
   999                 scanExpr((JCExpression) resource);
  1000             } else {
  1001                 throw new AssertionError(tree);  // parser error
  1004         for (JCTree resource : tree.resources) {
  1005             List<Type> closeableSupertypes = resource.type.isCompound() ?
  1006                 types.interfaces(resource.type).prepend(types.supertype(resource.type)) :
  1007                 List.of(resource.type);
  1008             for (Type sup : closeableSupertypes) {
  1009                 if (types.asSuper(sup, syms.autoCloseableType.tsym) != null) {
  1010                     Symbol closeMethod = rs.resolveInternalMethod(tree,
  1011                             attrEnv,
  1012                             sup,
  1013                             names.close,
  1014                             List.<Type>nil(),
  1015                             List.<Type>nil());
  1016                     if (closeMethod.kind == MTH) {
  1017                         for (Type t : ((MethodSymbol)closeMethod).getThrownTypes()) {
  1018                             markThrown(tree.body, t);
  1024         scanStat(tree.body);
  1025         List<Type> thrownInTry = thrown;
  1026         thrown = thrownPrev;
  1027         caught = caughtPrev;
  1028         boolean aliveEnd = alive;
  1029         uninitsTry.andSet(uninits);
  1030         Bits initsEnd = inits;
  1031         Bits uninitsEnd = uninits;
  1032         int nextadrCatch = nextadr;
  1034         if (!unrefdResources.isEmpty() &&
  1035                 lint.isEnabled(Lint.LintCategory.ARM)) {
  1036             for (Map.Entry<VarSymbol, JCVariableDecl> e : unrefdResources.entrySet()) {
  1037                 log.warning(e.getValue().pos(),
  1038                             "automatic.resource.not.referenced", e.getKey());
  1042         List<Type> caughtInTry = List.nil();
  1043         for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
  1044             alive = true;
  1045             JCVariableDecl param = l.head.param;
  1046             List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ?
  1047                     ((JCTypeDisjoint)l.head.param.vartype).components :
  1048                     List.of(l.head.param.vartype);
  1049             List<Type> ctypes = List.nil();
  1050             List<Type> rethrownTypes = chk.diff(thrownInTry, caughtInTry);
  1051             for (JCExpression ct : subClauses) {
  1052                 Type exc = ct.type;
  1053                 ctypes = ctypes.append(exc);
  1054                 if (types.isSameType(exc, syms.objectType))
  1055                     continue;
  1056                 if (chk.subset(exc, caughtInTry)) {
  1057                     log.error(l.head.pos(),
  1058                               "except.already.caught", exc);
  1059                 } else if (!chk.isUnchecked(l.head.pos(), exc) &&
  1060                            exc.tsym != syms.throwableType.tsym &&
  1061                            exc.tsym != syms.exceptionType.tsym &&
  1062                            !chk.intersects(exc, thrownInTry)) {
  1063                     log.error(l.head.pos(),
  1064                               "except.never.thrown.in.try", exc);
  1066                 caughtInTry = chk.incl(exc, caughtInTry);
  1068             inits = initsTry.dup();
  1069             uninits = uninitsTry.dup();
  1070             scan(param);
  1071             inits.incl(param.sym.adr);
  1072             uninits.excl(param.sym.adr);
  1073             multicatchTypes.put(param.sym, chk.intersect(ctypes, rethrownTypes));
  1074             scanStat(l.head.body);
  1075             initsEnd.andSet(inits);
  1076             uninitsEnd.andSet(uninits);
  1077             nextadr = nextadrCatch;
  1078             multicatchTypes.remove(param.sym);
  1079             aliveEnd |= alive;
  1081         if (tree.finalizer != null) {
  1082             List<Type> savedThrown = thrown;
  1083             thrown = List.nil();
  1084             inits = initsTry.dup();
  1085             uninits = uninitsTry.dup();
  1086             ListBuffer<PendingExit> exits = pendingExits;
  1087             pendingExits = prevPendingExits;
  1088             alive = true;
  1089             scanStat(tree.finalizer);
  1090             if (!alive) {
  1091                 // discard exits and exceptions from try and finally
  1092                 thrown = chk.union(thrown, thrownPrev);
  1093                 if (!loopPassTwo &&
  1094                     lint.isEnabled(Lint.LintCategory.FINALLY)) {
  1095                     log.warning(Lint.LintCategory.FINALLY,
  1096                             TreeInfo.diagEndPos(tree.finalizer),
  1097                             "finally.cannot.complete");
  1099             } else {
  1100                 thrown = chk.union(thrown, chk.diff(thrownInTry, caughtInTry));
  1101                 thrown = chk.union(thrown, savedThrown);
  1102                 uninits.andSet(uninitsEnd);
  1103                 // FIX: this doesn't preserve source order of exits in catch
  1104                 // versus finally!
  1105                 while (exits.nonEmpty()) {
  1106                     PendingExit exit = exits.next();
  1107                     if (exit.inits != null) {
  1108                         exit.inits.orSet(inits);
  1109                         exit.uninits.andSet(uninits);
  1111                     pendingExits.append(exit);
  1113                 inits.orSet(initsEnd);
  1114                 alive = aliveEnd;
  1116         } else {
  1117             thrown = chk.union(thrown, chk.diff(thrownInTry, caughtInTry));
  1118             inits = initsEnd;
  1119             uninits = uninitsEnd;
  1120             alive = aliveEnd;
  1121             ListBuffer<PendingExit> exits = pendingExits;
  1122             pendingExits = prevPendingExits;
  1123             while (exits.nonEmpty()) pendingExits.append(exits.next());
  1125         uninitsTry.andSet(uninitsTryPrev).andSet(uninits);
  1126         unrefdResources = unrefdResourcesPrev;
  1129     public void visitConditional(JCConditional tree) {
  1130         scanCond(tree.cond);
  1131         Bits initsBeforeElse = initsWhenFalse;
  1132         Bits uninitsBeforeElse = uninitsWhenFalse;
  1133         inits = initsWhenTrue;
  1134         uninits = uninitsWhenTrue;
  1135         if (tree.truepart.type.tag == BOOLEAN &&
  1136             tree.falsepart.type.tag == BOOLEAN) {
  1137             // if b and c are boolean valued, then
  1138             // v is (un)assigned after a?b:c when true iff
  1139             //    v is (un)assigned after b when true and
  1140             //    v is (un)assigned after c when true
  1141             scanCond(tree.truepart);
  1142             Bits initsAfterThenWhenTrue = initsWhenTrue.dup();
  1143             Bits initsAfterThenWhenFalse = initsWhenFalse.dup();
  1144             Bits uninitsAfterThenWhenTrue = uninitsWhenTrue.dup();
  1145             Bits uninitsAfterThenWhenFalse = uninitsWhenFalse.dup();
  1146             inits = initsBeforeElse;
  1147             uninits = uninitsBeforeElse;
  1148             scanCond(tree.falsepart);
  1149             initsWhenTrue.andSet(initsAfterThenWhenTrue);
  1150             initsWhenFalse.andSet(initsAfterThenWhenFalse);
  1151             uninitsWhenTrue.andSet(uninitsAfterThenWhenTrue);
  1152             uninitsWhenFalse.andSet(uninitsAfterThenWhenFalse);
  1153         } else {
  1154             scanExpr(tree.truepart);
  1155             Bits initsAfterThen = inits.dup();
  1156             Bits uninitsAfterThen = uninits.dup();
  1157             inits = initsBeforeElse;
  1158             uninits = uninitsBeforeElse;
  1159             scanExpr(tree.falsepart);
  1160             inits.andSet(initsAfterThen);
  1161             uninits.andSet(uninitsAfterThen);
  1165     public void visitIf(JCIf tree) {
  1166         scanCond(tree.cond);
  1167         Bits initsBeforeElse = initsWhenFalse;
  1168         Bits uninitsBeforeElse = uninitsWhenFalse;
  1169         inits = initsWhenTrue;
  1170         uninits = uninitsWhenTrue;
  1171         scanStat(tree.thenpart);
  1172         if (tree.elsepart != null) {
  1173             boolean aliveAfterThen = alive;
  1174             alive = true;
  1175             Bits initsAfterThen = inits.dup();
  1176             Bits uninitsAfterThen = uninits.dup();
  1177             inits = initsBeforeElse;
  1178             uninits = uninitsBeforeElse;
  1179             scanStat(tree.elsepart);
  1180             inits.andSet(initsAfterThen);
  1181             uninits.andSet(uninitsAfterThen);
  1182             alive = alive | aliveAfterThen;
  1183         } else {
  1184             inits.andSet(initsBeforeElse);
  1185             uninits.andSet(uninitsBeforeElse);
  1186             alive = true;
  1192     public void visitBreak(JCBreak tree) {
  1193         recordExit(tree);
  1196     public void visitContinue(JCContinue tree) {
  1197         recordExit(tree);
  1200     public void visitReturn(JCReturn tree) {
  1201         scanExpr(tree.expr);
  1202         // if not initial constructor, should markDead instead of recordExit
  1203         recordExit(tree);
  1206     public void visitThrow(JCThrow tree) {
  1207         scanExpr(tree.expr);
  1208         Symbol sym = TreeInfo.symbol(tree.expr);
  1209         if (sym != null &&
  1210             sym.kind == VAR &&
  1211             (sym.flags() & FINAL) != 0 &&
  1212             multicatchTypes.get(sym) != null &&
  1213             allowRethrowAnalysis) {
  1214             for (Type t : multicatchTypes.get(sym)) {
  1215                 markThrown(tree, t);
  1218         else {
  1219             markThrown(tree, tree.expr.type);
  1221         markDead();
  1224     public void visitApply(JCMethodInvocation tree) {
  1225         scanExpr(tree.meth);
  1226         scanExprs(tree.args);
  1227         for (List<Type> l = tree.meth.type.getThrownTypes(); l.nonEmpty(); l = l.tail)
  1228             markThrown(tree, l.head);
  1231     public void visitNewClass(JCNewClass tree) {
  1232         scanExpr(tree.encl);
  1233         scanExprs(tree.args);
  1234        // scan(tree.def);
  1235         for (List<Type> l = tree.constructorType.getThrownTypes();
  1236              l.nonEmpty();
  1237              l = l.tail) {
  1238             markThrown(tree, l.head);
  1240         List<Type> caughtPrev = caught;
  1241         try {
  1242             // If the new class expression defines an anonymous class,
  1243             // analysis of the anonymous constructor may encounter thrown
  1244             // types which are unsubstituted type variables.
  1245             // However, since the constructor's actual thrown types have
  1246             // already been marked as thrown, it is safe to simply include
  1247             // each of the constructor's formal thrown types in the set of
  1248             // 'caught/declared to be thrown' types, for the duration of
  1249             // the class def analysis.
  1250             if (tree.def != null)
  1251                 for (List<Type> l = tree.constructor.type.getThrownTypes();
  1252                      l.nonEmpty();
  1253                      l = l.tail) {
  1254                     caught = chk.incl(l.head, caught);
  1256             scan(tree.def);
  1258         finally {
  1259             caught = caughtPrev;
  1263     public void visitNewArray(JCNewArray tree) {
  1264         scanExprs(tree.dims);
  1265         scanExprs(tree.elems);
  1268     public void visitAssert(JCAssert tree) {
  1269         Bits initsExit = inits.dup();
  1270         Bits uninitsExit = uninits.dup();
  1271         scanCond(tree.cond);
  1272         uninitsExit.andSet(uninitsWhenTrue);
  1273         if (tree.detail != null) {
  1274             inits = initsWhenFalse;
  1275             uninits = uninitsWhenFalse;
  1276             scanExpr(tree.detail);
  1278         inits = initsExit;
  1279         uninits = uninitsExit;
  1282     public void visitAssign(JCAssign tree) {
  1283         JCTree lhs = TreeInfo.skipParens(tree.lhs);
  1284         if (!(lhs instanceof JCIdent)) scanExpr(lhs);
  1285         scanExpr(tree.rhs);
  1286         letInit(lhs);
  1289     public void visitAssignop(JCAssignOp tree) {
  1290         scanExpr(tree.lhs);
  1291         scanExpr(tree.rhs);
  1292         letInit(tree.lhs);
  1295     public void visitUnary(JCUnary tree) {
  1296         switch (tree.getTag()) {
  1297         case JCTree.NOT:
  1298             scanCond(tree.arg);
  1299             Bits t = initsWhenFalse;
  1300             initsWhenFalse = initsWhenTrue;
  1301             initsWhenTrue = t;
  1302             t = uninitsWhenFalse;
  1303             uninitsWhenFalse = uninitsWhenTrue;
  1304             uninitsWhenTrue = t;
  1305             break;
  1306         case JCTree.PREINC: case JCTree.POSTINC:
  1307         case JCTree.PREDEC: case JCTree.POSTDEC:
  1308             scanExpr(tree.arg);
  1309             letInit(tree.arg);
  1310             break;
  1311         default:
  1312             scanExpr(tree.arg);
  1316     public void visitBinary(JCBinary tree) {
  1317         switch (tree.getTag()) {
  1318         case JCTree.AND:
  1319             scanCond(tree.lhs);
  1320             Bits initsWhenFalseLeft = initsWhenFalse;
  1321             Bits uninitsWhenFalseLeft = uninitsWhenFalse;
  1322             inits = initsWhenTrue;
  1323             uninits = uninitsWhenTrue;
  1324             scanCond(tree.rhs);
  1325             initsWhenFalse.andSet(initsWhenFalseLeft);
  1326             uninitsWhenFalse.andSet(uninitsWhenFalseLeft);
  1327             break;
  1328         case JCTree.OR:
  1329             scanCond(tree.lhs);
  1330             Bits initsWhenTrueLeft = initsWhenTrue;
  1331             Bits uninitsWhenTrueLeft = uninitsWhenTrue;
  1332             inits = initsWhenFalse;
  1333             uninits = uninitsWhenFalse;
  1334             scanCond(tree.rhs);
  1335             initsWhenTrue.andSet(initsWhenTrueLeft);
  1336             uninitsWhenTrue.andSet(uninitsWhenTrueLeft);
  1337             break;
  1338         default:
  1339             scanExpr(tree.lhs);
  1340             scanExpr(tree.rhs);
  1344     public void visitAnnotatedType(JCAnnotatedType tree) {
  1345         // annotations don't get scanned
  1346         tree.underlyingType.accept(this);
  1349     public void visitIdent(JCIdent tree) {
  1350         if (tree.sym.kind == VAR) {
  1351             checkInit(tree.pos(), (VarSymbol)tree.sym);
  1352             referenced(tree.sym);
  1356     void referenced(Symbol sym) {
  1357         if (unrefdResources != null && unrefdResources.containsKey(sym)) {
  1358             unrefdResources.remove(sym);
  1362     public void visitTypeCast(JCTypeCast tree) {
  1363         super.visitTypeCast(tree);
  1364         if (!tree.type.isErroneous()
  1365             && lint.isEnabled(Lint.LintCategory.CAST)
  1366             && types.isSameType(tree.expr.type, tree.clazz.type)
  1367             && !(ignoreAnnotatedCasts && containsTypeAnnotation(tree.clazz))) {
  1368             log.warning(Lint.LintCategory.CAST,
  1369                     tree.pos(), "redundant.cast", tree.expr.type);
  1373     public void visitTopLevel(JCCompilationUnit tree) {
  1374         // Do nothing for TopLevel since each class is visited individually
  1377 /**************************************************************************
  1378  * utility methods for ignoring type-annotated casts lint checking
  1379  *************************************************************************/
  1380     private static final boolean ignoreAnnotatedCasts = true;
  1381     private static class AnnotationFinder extends TreeScanner {
  1382         public boolean foundTypeAnno = false;
  1383         public void visitAnnotation(JCAnnotation tree) {
  1384             foundTypeAnno = foundTypeAnno || (tree instanceof JCTypeAnnotation);
  1388     private boolean containsTypeAnnotation(JCTree e) {
  1389         AnnotationFinder finder = new AnnotationFinder();
  1390         finder.scan(e);
  1391         return finder.foundTypeAnno;
  1394 /**************************************************************************
  1395  * main method
  1396  *************************************************************************/
  1398     /** Perform definite assignment/unassignment analysis on a tree.
  1399      */
  1400     public void analyzeTree(Env<AttrContext> env, TreeMaker make) {
  1401         try {
  1402             attrEnv = env;
  1403             JCTree tree = env.tree;
  1404             this.make = make;
  1405             inits = new Bits();
  1406             uninits = new Bits();
  1407             uninitsTry = new Bits();
  1408             initsWhenTrue = initsWhenFalse =
  1409                 uninitsWhenTrue = uninitsWhenFalse = null;
  1410             if (vars == null)
  1411                 vars = new VarSymbol[32];
  1412             else
  1413                 for (int i=0; i<vars.length; i++)
  1414                     vars[i] = null;
  1415             firstadr = 0;
  1416             nextadr = 0;
  1417             pendingExits = new ListBuffer<PendingExit>();
  1418             multicatchTypes = new HashMap<Symbol, List<Type>>();
  1419             alive = true;
  1420             this.thrown = this.caught = null;
  1421             this.classDef = null;
  1422             scan(tree);
  1423         } finally {
  1424             // note that recursive invocations of this method fail hard
  1425             inits = uninits = uninitsTry = null;
  1426             initsWhenTrue = initsWhenFalse =
  1427                 uninitsWhenTrue = uninitsWhenFalse = null;
  1428             if (vars != null) for (int i=0; i<vars.length; i++)
  1429                 vars[i] = null;
  1430             firstadr = 0;
  1431             nextadr = 0;
  1432             pendingExits = null;
  1433             this.make = null;
  1434             this.thrown = this.caught = null;
  1435             this.classDef = null;

mercurial