Skip to content

Commit

Permalink
Adds UnitTreeVisitor, a thin extension of TreeVisitor that contains C…
Browse files Browse the repository at this point in the history
…ompilationUnit specific context objects. Convert most visitors to extend UnitTreeVisitor, requiring a CompilationUnit on construction.

	Change on 2016/10/12 by kstanger <[email protected]>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=135944447
  • Loading branch information
kstanger authored and tomball committed Oct 12, 2016
1 parent c1ed3ab commit 6f3d811
Show file tree
Hide file tree
Showing 44 changed files with 337 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ public List<List<Edge>> findCycles() throws IOException {
Parser.Handler handler = new Parser.Handler() {
@Override
public void handleParsedUnit(String path, CompilationUnit unit) {
new LambdaTypeElementAdder().run(unit);
new LambdaTypeElementAdder(unit).run();
typeCollector.visitAST(unit);
new OuterReferenceResolver(captureInfo).run(unit);
new OuterReferenceResolver(unit, captureInfo).run();
}
};
parser.parseFiles(sourceFiles, handler, options.sourceVersion());
Expand Down
1 change: 1 addition & 0 deletions translator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ JAVA_SOURCES = \
ast/TypeLiteral.java \
ast/TypeMethodReference.java \
ast/UnionType.java \
ast/UnitTreeVisitor.java \
ast/VariableDeclaration.java \
ast/VariableDeclarationExpression.java \
ast/VariableDeclarationFragment.java \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,11 @@

package com.google.devtools.j2objc.ast;

import com.google.devtools.j2objc.types.Types;
import com.google.devtools.j2objc.util.NameTable;
import com.google.devtools.j2objc.util.ParserEnvironment;

/**
* Base visitor class for the J2ObjC tree.
*/
public class TreeVisitor {

protected CompilationUnit unit = null;
protected ParserEnvironment env = null;
protected Types typeEnv = null;
protected NameTable nameTable = null;

/**
* Executes this visitor on a specified node. This entry point should
* be used instead of visit(), so that certain state can be initialized.
*
* @param node the top-level node to visit.
*/
public void run(TreeNode node) {
unit = TreeUtil.getCompilationUnit(node);
env = unit.getEnv();
typeEnv = unit.getTypeEnv();
nameTable = unit.getNameTable();
node.accept(this);
unit = null;
typeEnv = null;
nameTable = null;
}

public boolean preVisit(TreeNode node) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.devtools.j2objc.ast;

import com.google.devtools.j2objc.types.Types;
import com.google.devtools.j2objc.util.NameTable;
import com.google.devtools.j2objc.util.ParserEnvironment;

/**
* A TreeVisitor that contains CompilationUnit specific state accessible by subclasses.
*/
public class UnitTreeVisitor extends TreeVisitor {

protected final CompilationUnit unit;
protected final ParserEnvironment env;
protected final Types typeEnv;
protected final NameTable nameTable;

public UnitTreeVisitor(CompilationUnit unit) {
this.unit = unit;
env = unit.getEnv();
typeEnv = unit.getTypeEnv();
nameTable = unit.getNameTable();
}

public void run() {
unit.accept(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@
import com.google.devtools.j2objc.types.ImplementationImportCollector;
import com.google.devtools.j2objc.types.Import;
import com.google.devtools.j2objc.util.NameTable;

import org.eclipse.jdt.core.dom.ITypeBinding;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.eclipse.jdt.core.dom.ITypeBinding;

/**
* Contains the generated source code and additional context for a single Java
Expand Down Expand Up @@ -89,15 +86,15 @@ public static GeneratedType fromTypeDeclaration(AbstractTypeDeclaration typeNode
}

HeaderImportCollector headerCollector =
new HeaderImportCollector(HeaderImportCollector.Filter.PUBLIC_ONLY);
headerCollector.collect(typeNode);
new HeaderImportCollector(unit, HeaderImportCollector.Filter.PUBLIC_ONLY);
typeNode.accept(headerCollector);

HeaderImportCollector privateDeclarationCollector =
new HeaderImportCollector(HeaderImportCollector.Filter.PRIVATE_ONLY);
privateDeclarationCollector.collect(typeNode);
new HeaderImportCollector(unit, HeaderImportCollector.Filter.PRIVATE_ONLY);
typeNode.accept(privateDeclarationCollector);

ImplementationImportCollector importCollector = new ImplementationImportCollector();
importCollector.collect(typeNode);
ImplementationImportCollector importCollector = new ImplementationImportCollector(unit);
typeNode.accept(importCollector);

SourceBuilder builder = new SourceBuilder(Options.emitLineDirectives());
TypeDeclarationGenerator.generate(builder, typeNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@
import com.google.devtools.j2objc.ast.ThrowStatement;
import com.google.devtools.j2objc.ast.TreeNode;
import com.google.devtools.j2objc.ast.TreeUtil;
import com.google.devtools.j2objc.ast.TreeVisitor;
import com.google.devtools.j2objc.ast.TryStatement;
import com.google.devtools.j2objc.ast.Type;
import com.google.devtools.j2objc.ast.TypeLiteral;
import com.google.devtools.j2objc.ast.TypeMethodReference;
import com.google.devtools.j2objc.ast.UnionType;
import com.google.devtools.j2objc.ast.UnitTreeVisitor;
import com.google.devtools.j2objc.ast.VariableDeclarationExpression;
import com.google.devtools.j2objc.ast.VariableDeclarationFragment;
import com.google.devtools.j2objc.ast.VariableDeclarationStatement;
Expand Down Expand Up @@ -125,7 +125,7 @@
*
* @author Tom Ball
*/
public class StatementGenerator extends TreeVisitor {
public class StatementGenerator extends UnitTreeVisitor {

private final SourceBuilder buffer;
private final boolean useReferenceCounting;
Expand All @@ -135,11 +135,12 @@ public static String generate(TreeNode node, int currentLine) {
if (node == null) {
throw new NullPointerException("cannot generate a null statement");
}
generator.run(node);
node.accept(generator);
return generator.getResult();
}

private StatementGenerator(TreeNode node, int currentLine) {
super(TreeUtil.getCompilationUnit(node));
buffer = new SourceBuilder(Options.emitLineDirectives(), currentLine);
useReferenceCounting = !Options.useARC();
}
Expand Down
Loading

0 comments on commit 6f3d811

Please sign in to comment.