Skip to content

Commit

Permalink
Merge pull request #65 from usethesource/two-stage-method-templates
Browse files Browse the repository at this point in the history
we added resolution of two stage method templates and also increased methodOverrides with the relation (one to many) for every possible resolution of instantiated method templates
  • Loading branch information
jurgenvinju authored Oct 17, 2023
2 parents 40176de + 1f3d3ff commit 91f2772
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 34 deletions.
68 changes: 40 additions & 28 deletions src/lang/cpp/internal/BindingsResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,15 @@
import org.eclipse.cdt.internal.core.dom.parser.c.CEnumeration;
import org.eclipse.cdt.internal.core.dom.parser.c.CFunction;
import org.eclipse.cdt.internal.core.dom.parser.c.CVariable;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNameBase;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPDeferredClassInstance;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPTwoPhaseBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownMemberClass;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownMemberClassInstance;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPFunctionSet;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics;
import org.eclipse.cdt.internal.core.pdom.dom.cpp.IPDOMCPPClassType;
import org.eclipse.cdt.internal.core.pdom.dom.cpp.IPDOMCPPEnumType;
import org.eclipse.cdt.internal.core.pdom.dom.cpp.IPDOMCPPTemplateParameter;
Expand Down Expand Up @@ -200,15 +203,15 @@ private ISourceLocation resolveOwner(IBinding binding, ISourceLocation origin) t
return translationUnit;
}
else {
return resolveBinding(owner, origin);
return resolveBinding(null, owner, origin);
}
}

public static ISourceLocation failedBinding(String scheme) {
return URIUtil.correctLocation(scheme, "", UUID.randomUUID().toString());
}

public ISourceLocation resolveBinding(IBinding binding, ISourceLocation origin) {
public ISourceLocation resolveBinding(IASTNameOwner owner, IBinding binding, ISourceLocation origin) {
try {
if (binding == null) {
return failedBinding("unresolved");
Expand Down Expand Up @@ -250,7 +253,7 @@ public ISourceLocation resolveBinding(IBinding binding, ISourceLocation origin)
return resolveICPPBinding((ICPPBinding) binding, origin);
}
if (binding instanceof ICPPTwoPhaseBinding) {
return resolveICPPTwoPhaseBinding((ICPPTwoPhaseBinding) binding, origin);
return resolveICPPTwoPhaseBinding(owner, (ICPPTwoPhaseBinding) binding, origin);
}
}
catch (URISyntaxException e) {
Expand All @@ -260,10 +263,19 @@ public ISourceLocation resolveBinding(IBinding binding, ISourceLocation origin)
throw new RuntimeException("Encountered unknown Binding: " + binding.getName());
}

private ISourceLocation resolveICPPTwoPhaseBinding(ICPPTwoPhaseBinding binding, ISourceLocation origin) {
private ISourceLocation resolveICPPTwoPhaseBinding(IASTNameOwner owner, ICPPTwoPhaseBinding binding, ISourceLocation origin) throws URISyntaxException {
if (binding instanceof CPPFunctionSet) {
return resolveCPPFunctionSet(owner, (CPPFunctionSet) binding, origin);
}

throw new RuntimeException("Trying to resolve ICPPTwoPhaseBinding " + binding.getClass().getSimpleName());
}

private ISourceLocation resolveCPPFunctionSet(IASTNameOwner owner, CPPFunctionSet binding, ISourceLocation origin) throws URISyntaxException {
// now we don't know which of the alternatives are going to be used.
return ownedBinding(binding, "cpp+functionSet", origin);
}

private ISourceLocation resolveIVariable(IVariable binding, ISourceLocation origin) throws URISyntaxException {
if (binding instanceof ICPPVariable) {
return resolveICPPVariable((ICPPVariable) binding, origin);
Expand Down Expand Up @@ -778,7 +790,7 @@ public ISourceLocation resolveBinding(IASTNameOwner node, ISourceLocation origin
if (node instanceof IASTNamedTypeSpecifier) {
return resolveNamedTypeSpecifier((IASTNamedTypeSpecifier) node);
}
if (node instanceof IASTPreprocessorMacroDefinition) { // TODO
if (node instanceof IASTPreprocessorMacroDefinition) {
return resolvePreprocessorMacroDefinition((IASTPreprocessorMacroDefinition) node);
}
if (node instanceof ICPPASTAliasDeclaration) {
Expand Down Expand Up @@ -831,46 +843,46 @@ public ISourceLocation resolveBinding(IASTNameOwner node, ISourceLocation origin
}

private ISourceLocation resolveUsingDeclaration(ICPPASTUsingDeclaration node) throws URISyntaxException {
return resolveBinding(node.getName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveUsingDirective(ICPPASTUsingDirective node) throws URISyntaxException {
return resolveBinding(node.getQualifiedName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getQualifiedName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveTemplateId(ICPPASTTemplateId node) throws URISyntaxException {
return resolveBinding(node.resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveTemplatedTypeTemplateParameter(ICPPASTTemplatedTypeTemplateParameter node)
throws URISyntaxException {
return resolveBinding(node.getName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveSimpleTypeTemplateParameter(ICPPASTSimpleTypeTemplateParameter node)
throws URISyntaxException {
return resolveBinding(node.getName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveQualifiedName(ICPPASTQualifiedName node) throws URISyntaxException {
return resolveBinding(node.resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolvePointerToMember(ICPPASTPointerToMember node) throws URISyntaxException {
return resolveBinding(node.getName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveNamespaceDefinition(ICPPASTNamespaceDefinition node) throws URISyntaxException {
return resolveBinding(node.getName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveNamespaceAlias(ICPPASTNamespaceAlias node) throws URISyntaxException {
return resolveBinding(node.getAlias().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getAlias().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveConstructorChainInitializer(ICPPASTConstructorChainInitializer node)
throws URISyntaxException {
return resolveBinding(node.getMemberInitializerId().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getMemberInitializerId().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveCapture(ICPPASTCapture node) throws URISyntaxException {
Expand All @@ -879,15 +891,15 @@ private ISourceLocation resolveCapture(ICPPASTCapture node) throws URISyntaxExce
out("Resolving this capture; returning dummy value");
return FIXME;
}
return resolveBinding(name.resolveBinding(), getSourceLocation(node));
return resolveBinding(node, name.resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveBaseSpecifier(ICPPASTBaseSpecifier node) throws URISyntaxException {
return resolveBinding(node.getNameSpecifier().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getNameSpecifier().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveAliasDeclaration(ICPPASTAliasDeclaration node) throws URISyntaxException {
return resolveBinding(node.getAlias().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getAlias().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolvePreprocessorMacroDefinition(IASTPreprocessorMacroDefinition node) {
Expand All @@ -896,47 +908,47 @@ private ISourceLocation resolvePreprocessorMacroDefinition(IASTPreprocessorMacro

private ISourceLocation resolveNamedTypeSpecifier(IASTNamedTypeSpecifier node) throws URISyntaxException {
IBinding binding = node.getName().resolveBinding();
return resolveBinding(binding, getSourceLocation(node));
return resolveBinding(node,binding, getSourceLocation(node));
}

private ISourceLocation resolveLabelStatement(IASTLabelStatement node) throws URISyntaxException {
return resolveBinding(node.getName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveIdExpression(IASTIdExpression node) throws URISyntaxException {
return resolveBinding(node.getName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveGotoStatement(IASTGotoStatement node) throws URISyntaxException {
return resolveBinding(node.getName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveFieldReference(IASTFieldReference node) throws URISyntaxException {
return resolveBinding(node.getFieldName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getFieldName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveEnumerator(IASTEnumerator node) throws URISyntaxException {
return resolveBinding(node.getName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveEnumerationSpecifier(IASTEnumerationSpecifier node) throws URISyntaxException {
return resolveBinding(node.getName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveElaboratedTypeSpecifier(IASTElaboratedTypeSpecifier node) throws URISyntaxException {
return resolveBinding(node.getName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveDeclarator(IASTDeclarator node) throws URISyntaxException {
if (node.getName() == null) {
out("resolveDeclarator has null name. " + node.getClass().getSimpleName() + ": " + node.getRawSignature());
return FIXME;
}
return resolveBinding(node.getName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getName().resolveBinding(), getSourceLocation(node));
}

private ISourceLocation resolveCompositeTypeSpecifier(IASTCompositeTypeSpecifier node) throws URISyntaxException {
return resolveBinding(node.getName().resolveBinding(), getSourceLocation(node));
return resolveBinding(node, node.getName().resolveBinding(), getSourceLocation(node));
}

public ISourceLocation makeBinding(String scheme, String authority, String path) {
Expand Down
33 changes: 29 additions & 4 deletions src/lang/cpp/internal/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@
import org.eclipse.cdt.internal.core.dom.parser.c.CASTParameterDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTCompoundStatementExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ClassTypeHelper;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPTwoPhaseBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPFunctionSet;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics;
import org.eclipse.core.runtime.CoreException;
import org.rascalmpl.debug.IRascalMonitor;
Expand Down Expand Up @@ -502,23 +504,46 @@ public ISet getMethodOverrides(IASTTranslationUnit tu, ISet newResolutions) {
Set<IBinding> bindings = new HashSet<>();
Stream.of(anc.getNames()).forEach(it -> bindings.add(it.resolveBinding()));
ISetWriter methodOverrides = vf.setWriter();

// first we add all methods in classes that override some other methods
bindings.stream().filter(ICPPMethod.class::isInstance).forEach(override -> {
Stream.of(ClassTypeHelper.findOverridden((ICPPMethod) override)).forEach(base -> {
try {
methodOverrides.insert(vf.tuple(br.resolveBinding(base, locs.forNode(tu)), br.resolveBinding(override, locs.forNode(tu))));
// TODO: should it not be the origin of the declaring method AST?
methodOverrides.insert(vf.tuple(br.resolveBinding(null, base, locs.forNode(tu)), br.resolveBinding(null, override, locs.forNode(tu))));
} catch (FactTypeUseException e) {
err("Got FactTypeUseException\n" + e.getMessage());
}
});
});

// then we add the template methods that may resolve to more specific methods after expansion
bindings.stream()
.filter(CPPFunctionSet.class::isInstance)
.map(CPPFunctionSet.class::cast)
.forEach(binding -> {
// TODO: should it not be the origin of the declaring method AST?
ISourceLocation base = br.resolveBinding(null, binding, locs.forNode(tu) );

for (IBinding override : binding.getBindings()) {
// TODO: should it not be the origin of the declaring method AST?
methodOverrides.insert(
vf.tuple(
base,
br.resolveBinding(null, override, locs.forNode(tu))
)
);
}
});

// finally we add a mapping from abstract constructor calls to their implementations
methodOverrides.appendAll(newResolutions);
return methodOverrides.done();
}

public ISet getMacroDefinitionsFromTranslationUnit(IASTTranslationUnit tu) {
return Stream.of(tu.getMacroDefinitions()).map(it -> {
return vf.tuple(br.resolveBinding(it.getName().resolveBinding(), locs.forNode(it)), locs.forNode(it));
return vf.tuple(br.resolveBinding(it, it.getName().resolveBinding(), locs.forNode(it)), locs.forNode(it));
}).collect(vf.setWriter());
}

Expand Down Expand Up @@ -572,7 +597,7 @@ public IValue setM3IncludeInformationFromTranslationUnit(IASTTranslationUnit tu,
public ISet getMacroExpansionsFromTranslationUnit(IASTTranslationUnit tu) {
ISetWriter macros = vf.setWriter();
Stream.of(tu.getMacroExpansions()).forEach(it -> {
ISourceLocation decl = br.resolveBinding(it.getMacroReference().resolveBinding(), locs.forNode(it));
ISourceLocation decl = br.resolveBinding(null, it.getMacroReference().resolveBinding(), locs.forNode(it));
macros.insert(vf.tuple(locs.forNode(it), decl));
});
return macros.done();
Expand Down Expand Up @@ -2865,7 +2890,7 @@ private int visit(ICPPASTNewExpression expression) {
expression.getTypeId().accept(this);
IConstructor typeId = stack.pop();
IBinding constructor = CPPSemantics.findImplicitlyCalledConstructor(expression);
ISourceLocation decl = constructor != null ? br.resolveBinding(constructor, loc) : null;
ISourceLocation decl = constructor != null ? br.resolveBinding(null, constructor, loc) : null;
ISourceLocation newDecl = decl != null
? changeScheme(decl, "cpp+new")
: URIUtil.correctLocation("cpp+new", "",
Expand Down
4 changes: 2 additions & 2 deletions src/lang/cpp/internal/TypeResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void out(String msg) {
}

private ISourceLocation getDecl(IBinding binding, ISourceLocation origin) {
return br.resolveBinding(binding, origin);
return br.resolveBinding(null, binding, origin);
}

private IList handleTemplateParameters(ICPPASTTemplateDeclaration declaration) {
Expand Down Expand Up @@ -623,7 +623,7 @@ private IConstructor resolveICPPUnknownType(ICPPUnknownType type, ISourceLocatio
}

private IConstructor resolveIEnumeration(IEnumeration type, ISourceLocation origin) {
ISourceLocation decl = br.resolveBinding(type, origin);
ISourceLocation decl = br.resolveBinding(null, type, origin);
return builder.TypeSymbol_enumeration(decl);
}

Expand Down

0 comments on commit 91f2772

Please sign in to comment.