< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java

Print this page
rev 50902 : imported patch 8206122-Use-Queue-in-place-of-ArrayList-when-need-to-remove-first-element
   1 /*
   2  * Copyright (c) 2014, 2017, 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  */
  25 
  26 package com.sun.tools.javac.comp;
  27 
  28 import java.util.ArrayList;

  29 
  30 import com.sun.source.tree.LambdaExpressionTree;
  31 import com.sun.tools.javac.code.Source;
  32 import com.sun.tools.javac.code.Source.Feature;
  33 import com.sun.tools.javac.code.Type;
  34 import com.sun.tools.javac.code.Types;
  35 import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext;
  36 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  37 import com.sun.tools.javac.tree.JCTree;
  38 import com.sun.tools.javac.tree.JCTree.JCBlock;
  39 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
  40 import com.sun.tools.javac.tree.JCTree.JCDoWhileLoop;
  41 import com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop;
  42 import com.sun.tools.javac.tree.JCTree.JCForLoop;
  43 import com.sun.tools.javac.tree.JCTree.JCIf;
  44 import com.sun.tools.javac.tree.JCTree.JCLambda;
  45 import com.sun.tools.javac.tree.JCTree.JCLambda.ParameterKind;
  46 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
  47 import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
  48 import com.sun.tools.javac.tree.JCTree.JCNewClass;


 504      * Dummy deferral handler.
 505      */
 506     DeferredAnalysisHelper flushDeferredHelper = new DeferredAnalysisHelper() {
 507         @Override
 508         public void queue(RewritingContext rewriting) {
 509             //do nothing
 510         }
 511 
 512         @Override
 513         public void flush(Env<AttrContext> flushEnv) {
 514             //do nothing
 515         }
 516     };
 517 
 518     /**
 519      * Simple deferral handler. All tasks belonging to the same outermost class are added to
 520      * the same queue. The queue is flushed after flow analysis (only if no error occurred).
 521      */
 522     DeferredAnalysisHelper queueDeferredHelper = new DeferredAnalysisHelper() {
 523 
 524         Map<ClassSymbol, ArrayList<RewritingContext>> Q = new HashMap<>();
 525 
 526         @Override
 527         public void queue(RewritingContext rewriting) {
 528             ArrayList<RewritingContext> s = Q.computeIfAbsent(rewriting.env.enclClass.sym.outermostClass(), k -> new ArrayList<>());
 529             s.add(rewriting);
 530         }
 531 
 532         @Override
 533         public void flush(Env<AttrContext> flushEnv) {
 534             if (!Q.isEmpty()) {
 535                 DeferredAnalysisHelper prevHelper = deferredAnalysisHelper;
 536                 try {
 537                     deferredAnalysisHelper = flushDeferredHelper;
 538                     ArrayList<RewritingContext> rewritings = Q.get(flushEnv.enclClass.sym.outermostClass());
 539                     while (rewritings != null && !rewritings.isEmpty()) {
 540                         doAnalysis(rewritings.remove(0));
 541                     }
 542                 } finally {
 543                     deferredAnalysisHelper = prevHelper;
 544                 }
 545             }
 546         }
 547     };
 548 
 549     DeferredAnalysisHelper deferredAnalysisHelper = queueDeferredHelper;
 550 
 551     void doAnalysis(RewritingContext rewriting) {
 552         DiagnosticSource prevSource = log.currentSource();
 553         LocalCacheContext localCacheContext = argumentAttr.withLocalCacheContext();
 554         try {
 555             log.useSource(rewriting.env.toplevel.getSourceFile());
 556 
 557             JCStatement treeToAnalyze = (JCStatement)rewriting.originalTree;
 558             if (rewriting.env.info.scope.owner.kind == Kind.TYP) {
 559                 //add a block to hoist potential dangling variable declarations
 560                 treeToAnalyze = make.at(Position.NOPOS)


   1 /*
   2  * Copyright (c) 2014, 2018, 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  */
  25 
  26 package com.sun.tools.javac.comp;
  27 
  28 import java.util.ArrayDeque;
  29 import java.util.Queue;
  30 
  31 import com.sun.source.tree.LambdaExpressionTree;
  32 import com.sun.tools.javac.code.Source;
  33 import com.sun.tools.javac.code.Source.Feature;
  34 import com.sun.tools.javac.code.Type;
  35 import com.sun.tools.javac.code.Types;
  36 import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext;
  37 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  38 import com.sun.tools.javac.tree.JCTree;
  39 import com.sun.tools.javac.tree.JCTree.JCBlock;
  40 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
  41 import com.sun.tools.javac.tree.JCTree.JCDoWhileLoop;
  42 import com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop;
  43 import com.sun.tools.javac.tree.JCTree.JCForLoop;
  44 import com.sun.tools.javac.tree.JCTree.JCIf;
  45 import com.sun.tools.javac.tree.JCTree.JCLambda;
  46 import com.sun.tools.javac.tree.JCTree.JCLambda.ParameterKind;
  47 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
  48 import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
  49 import com.sun.tools.javac.tree.JCTree.JCNewClass;


 505      * Dummy deferral handler.
 506      */
 507     DeferredAnalysisHelper flushDeferredHelper = new DeferredAnalysisHelper() {
 508         @Override
 509         public void queue(RewritingContext rewriting) {
 510             //do nothing
 511         }
 512 
 513         @Override
 514         public void flush(Env<AttrContext> flushEnv) {
 515             //do nothing
 516         }
 517     };
 518 
 519     /**
 520      * Simple deferral handler. All tasks belonging to the same outermost class are added to
 521      * the same queue. The queue is flushed after flow analysis (only if no error occurred).
 522      */
 523     DeferredAnalysisHelper queueDeferredHelper = new DeferredAnalysisHelper() {
 524 
 525         Map<ClassSymbol, Queue<RewritingContext>> Q = new HashMap<>();
 526 
 527         @Override
 528         public void queue(RewritingContext rewriting) {
 529             Queue<RewritingContext> s = Q.computeIfAbsent(rewriting.env.enclClass.sym.outermostClass(), k -> new ArrayDeque<>());
 530             s.add(rewriting);
 531         }
 532 
 533         @Override
 534         public void flush(Env<AttrContext> flushEnv) {
 535             if (!Q.isEmpty()) {
 536                 DeferredAnalysisHelper prevHelper = deferredAnalysisHelper;
 537                 try {
 538                     deferredAnalysisHelper = flushDeferredHelper;
 539                     Queue<RewritingContext> rewritings = Q.get(flushEnv.enclClass.sym.outermostClass());
 540                     while (rewritings != null && !rewritings.isEmpty()) {
 541                         doAnalysis(rewritings.remove());
 542                     }
 543                 } finally {
 544                     deferredAnalysisHelper = prevHelper;
 545                 }
 546             }
 547         }
 548     };
 549 
 550     DeferredAnalysisHelper deferredAnalysisHelper = queueDeferredHelper;
 551 
 552     void doAnalysis(RewritingContext rewriting) {
 553         DiagnosticSource prevSource = log.currentSource();
 554         LocalCacheContext localCacheContext = argumentAttr.withLocalCacheContext();
 555         try {
 556             log.useSource(rewriting.env.toplevel.getSourceFile());
 557 
 558             JCStatement treeToAnalyze = (JCStatement)rewriting.originalTree;
 559             if (rewriting.env.info.scope.owner.kind == Kind.TYP) {
 560                 //add a block to hoist potential dangling variable declarations
 561                 treeToAnalyze = make.at(Position.NOPOS)


< prev index next >