forked from phoedos/pmd
Cleanup symtable factory
This commit is contained in:
@ -101,6 +101,11 @@ public final class StreamImpl {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Node> toList() {
|
||||
return Collections.unmodifiableList(Arrays.asList(array));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Iterator<Node> baseIterator() {
|
||||
return Arrays.asList(array).iterator();
|
||||
|
@ -135,7 +135,7 @@ public final class ASTVariableId extends AbstractTypedSymbolDeclarator<JVariable
|
||||
* identifier.
|
||||
*/
|
||||
public boolean isUnnamed() {
|
||||
return "_".equals(name) && getLanguageVersion().compareToVersion("22") >= 0;
|
||||
return "_".equals(name) && getLanguageVersion().compareToVersion("21-preview") >= 0;
|
||||
}
|
||||
|
||||
/** Returns the name of the variable. */
|
||||
|
@ -17,9 +17,11 @@ import java.util.Map.Entry;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.symbols.table.coreimpl.MostlySingularMultimap.Builder;
|
||||
import net.sourceforge.pmd.lang.java.symbols.table.coreimpl.MostlySingularMultimap.MapMaker;
|
||||
import net.sourceforge.pmd.util.CollectionUtil;
|
||||
import net.sourceforge.pmd.util.IteratorUtil;
|
||||
|
||||
/**
|
||||
* Build a shadow chain for some type.
|
||||
@ -126,8 +128,9 @@ public abstract class ShadowChainBuilder<S, I> {
|
||||
|
||||
// convenience to build name resolvers
|
||||
|
||||
public <N> ResolverBuilder groupByName(Iterable<? extends N> input, Function<? super N, ? extends S> symbolFetcher) {
|
||||
return new ResolverBuilder(newMapBuilder().groupBy(CollectionUtil.map(input, symbolFetcher), this::getSimpleName));
|
||||
public <N> ResolverBuilder groupByName(Iterable<? extends N> input, Function<? super N, ? extends @Nullable S> symbolFetcher) {
|
||||
Iterable<? extends S> mapped = () -> IteratorUtil.mapNotNull(input.iterator(), symbolFetcher);
|
||||
return new ResolverBuilder(newMapBuilder().groupBy(mapped, this::getSimpleName));
|
||||
}
|
||||
|
||||
public ResolverBuilder groupByName(Iterable<? extends S> tparams) {
|
||||
|
@ -23,7 +23,6 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.NodeStream;
|
||||
import net.sourceforge.pmd.lang.ast.SemanticErrorReporter;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameters;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTList;
|
||||
@ -366,13 +365,13 @@ final class SymTableFactory {
|
||||
* of fields by local variables and formals.
|
||||
*/
|
||||
JSymbolTable bodyDeclaration(JSymbolTable parent, JClassType enclosing, @Nullable ASTFormalParameters formals, @Nullable ASTTypeParameters tparams) {
|
||||
NodeStream<ASTVariableId> namedFormals = ASTList.orEmptyStream(formals).map(ASTFormalParameter::getVarId);
|
||||
if (unnamedVariableIsSupported()) {
|
||||
namedFormals = namedFormals.filterNot(ASTVariableId::isUnnamed);
|
||||
}
|
||||
return new SymbolTableImpl(
|
||||
VARS.shadow(varNode(parent), ScopeInfo.FORMAL_PARAM, VARS.groupByName(namedFormals, fp -> {
|
||||
JVariableSymbol sym = fp.getSymbol();
|
||||
VARS.shadow(varNode(parent), ScopeInfo.FORMAL_PARAM, VARS.groupByName(ASTList.orEmptyStream(formals), fp -> {
|
||||
ASTVariableId varId = fp.getVarId();
|
||||
if (varId.isUnnamed()) {
|
||||
return null;
|
||||
}
|
||||
JVariableSymbol sym = varId.getSymbol();
|
||||
return sym.getTypeSystem().sigOf(enclosing, (JFormalParamSymbol) sym);
|
||||
})),
|
||||
TYPES.shadow(typeNode(parent), ScopeInfo.TYPE_PARAM, TYPES.groupByName(ASTList.orEmptyStream(tparams), ASTTypeParameter::getTypeMirror)),
|
||||
@ -391,7 +390,7 @@ final class SymTableFactory {
|
||||
JSymbolTable localVarSymTable(JSymbolTable parent, JClassType enclosing, Iterable<ASTVariableId> ids) {
|
||||
List<JVariableSig> sigs = new ArrayList<>();
|
||||
for (ASTVariableId id : ids) {
|
||||
if (unnamedVariableIsSupported() && id.isUnnamed()) {
|
||||
if (id.isUnnamed()) {
|
||||
continue;
|
||||
}
|
||||
sigs.add(id.getTypeSystem().sigOf(enclosing, (JLocalVariableSymbol) id.getSymbol()));
|
||||
@ -399,16 +398,12 @@ final class SymTableFactory {
|
||||
return SymbolTableImpl.withVars(parent, VARS.augment(varNode(parent), false, ScopeInfo.LOCAL, VARS.groupByName(sigs)));
|
||||
}
|
||||
|
||||
JSymbolTable localVarSymTable(JSymbolTable parent, JClassType enclosing, JVariableSymbol id) {
|
||||
if (unnamedVariableIsSupported() && "_".equals(id.getSimpleName())) {
|
||||
// unnamed variable
|
||||
JSymbolTable localVarSymTable(JSymbolTable parent, JClassType enclosing, ASTVariableId id) {
|
||||
assert !id.isField();
|
||||
if (id.isUnnamed()) { // checks language version already
|
||||
return parent;
|
||||
}
|
||||
return SymbolTableImpl.withVars(parent, VARS.augment(varNode(parent), false, ScopeInfo.LOCAL, id.getTypeSystem().sigOf(enclosing, (JLocalVariableSymbol) id)));
|
||||
}
|
||||
|
||||
private boolean unnamedVariableIsSupported() {
|
||||
return processor.getJdkVersion() >= 22;
|
||||
return SymbolTableImpl.withVars(parent, VARS.augment(varNode(parent), false, ScopeInfo.LOCAL, id.getTypeSystem().sigOf(enclosing, (JLocalVariableSymbol) id.getSymbol())));
|
||||
}
|
||||
|
||||
JSymbolTable localTypeSymTable(JSymbolTable parent, JClassType sym) {
|
||||
|
@ -428,7 +428,7 @@ public final class SymbolTableResolver {
|
||||
int pushed = 0;
|
||||
for (ASTVariableDeclarator declarator : st.children(ASTVariableDeclarator.class)) {
|
||||
ASTVariableId varId = declarator.getVarId();
|
||||
pushed += pushOnStack(f.localVarSymTable(top(), enclosing(), varId.getSymbol()));
|
||||
pushed += pushOnStack(f.localVarSymTable(top(), enclosing(), varId));
|
||||
// visit initializer
|
||||
setTopSymbolTableAndVisit(declarator.getInitializer(), ctx);
|
||||
}
|
||||
@ -443,7 +443,7 @@ public final class SymbolTableResolver {
|
||||
ASTVariableId varId = node.getVarId();
|
||||
setTopSymbolTableAndVisit(varId.getTypeNode(), ctx);
|
||||
|
||||
int pushed = pushOnStack(f.localVarSymTable(top(), enclosing(), varId.getSymbol()));
|
||||
int pushed = pushOnStack(f.localVarSymTable(top(), enclosing(), varId));
|
||||
ASTStatement body = node.getBody();
|
||||
// unless it's a block the body statement may never set a
|
||||
// symbol table that would have this table as parent,
|
||||
@ -479,7 +479,7 @@ public final class SymbolTableResolver {
|
||||
|
||||
@Override
|
||||
public Void visit(ASTCatchClause node, @NonNull ReferenceCtx ctx) {
|
||||
int pushed = pushOnStack(f.localVarSymTable(top(), enclosing(), node.getParameter().getVarId().getSymbol()));
|
||||
int pushed = pushOnStack(f.localVarSymTable(top(), enclosing(), node.getParameter().getVarId()));
|
||||
setTopSymbolTableAndVisitAllChildren(node, ctx);
|
||||
popStack(pushed);
|
||||
return null;
|
||||
|
Reference in New Issue
Block a user