Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import de.peeeq.wurstscript.WurstOperator;
import de.peeeq.wurstscript.ast.*;
import de.peeeq.wurstscript.attributes.names.FuncLink;
import de.peeeq.wurstscript.attributes.names.NameResolution;
import de.peeeq.wurstscript.attributes.names.Visibility;
import de.peeeq.wurstscript.types.*;
import de.peeeq.wurstscript.utils.Utils;
Expand All @@ -18,6 +19,9 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static de.peeeq.wurstscript.attributes.AttrPossibleFunctionSignatures.*;
import static de.peeeq.wurstscript.attributes.names.NameResolution.lookupMemberFuncs;


/**
* this attribute find the variable definition for every variable reference
Expand Down Expand Up @@ -65,18 +69,102 @@ public static FuncLink calculate(final ExprFuncRef node) {
}

public static @Nullable FuncLink calculate(final ExprMemberMethod node) {
WurstType recvT = node.getLeft().attrTyp();
var raw = NameResolution.lookupMemberFuncs(node, recvT, node.getFuncName(), /*showErrors=*/false);

Expr left = node.getLeft();
WurstType leftType = left.attrTyp();
String funcName = node.getFuncName();
java.util.ArrayList<FuncLink> visible = new java.util.ArrayList<>(raw.size());
java.util.ArrayList<FuncLink> hidden = new java.util.ArrayList<>(raw.size());
for (var f : raw) {
if (isVisible(f)) visible.add(f); else hidden.add(f);
}

@Nullable FuncLink result = searchMemberFunc(node, leftType, funcName, argumentTypes(node));
if (result == null) {
node.addError("The method " + funcName + " is undefined for receiver of type " + leftType);
if (!raw.isEmpty() && visible.isEmpty()) {
// Keep the classic diagnostic the tests look for:
node.addError("The method " + node.getFuncName() + " is not visible here.");
return null; // don’t leak a def to downstream passes/codegen
}
return result;

java.util.List<FuncLink> methods = new java.util.ArrayList<>();
java.util.List<FuncLink> exts = new java.util.ArrayList<>();
for (var f : visible) {
if (isExtension(f)) exts.add(f); else methods.add(f);
}

if (!exts.isEmpty()) {
exts = keepMostSpecificReceivers(exts, FuncLink::getReceiverType, node);
}

java.util.ArrayList<FuncLink> cands = new java.util.ArrayList<>(methods.size() + exts.size());
cands.addAll(methods);
cands.addAll(exts);

var argTypes = AttrFuncDef.argumentTypesPre(node);

// Pass 1: exact matches
java.util.ArrayList<FuncLink> exactLinks = new java.util.ArrayList<>();
java.util.ArrayList<FunctionSignature> exactSigs = new java.util.ArrayList<>();
for (var f : cands) {
var sig = FunctionSignature.fromNameLink(f);
var m = sig.matchAgainstArgs(argTypes, node);
if (m != null) {
exactLinks.add(f);
exactSigs.add(m);
}
}
if (!exactLinks.isEmpty()) {
// methods vs others
java.util.ArrayList<Integer> methodIdxs = new java.util.ArrayList<>();
for (int i = 0; i < exactLinks.size(); i++) {
if (!isExtension(exactLinks.get(i))) methodIdxs.add(i);
}
if (methodIdxs.size() > 1) {
// filter method candidates by most specific receiver
java.util.ArrayList<FunctionSignature> methSigs = new java.util.ArrayList<>();
for (int i : methodIdxs) methSigs.add(exactSigs.get(i));
methSigs = (java.util.ArrayList<FunctionSignature>) keepMostSpecificReceivers(
methSigs, FunctionSignature::getReceiverType, node
);
// pick the first of the survivors
var chosenSig = methSigs.get(0);
// find corresponding link
for (int i = 0; i < exactSigs.size(); i++) {
if (exactSigs.get(i) == chosenSig) {
return exactLinks.get(i).withTypeArgBinding(node, chosenSig.getMapping());
}
}
} else if (methodIdxs.size() == 1) {
int i = methodIdxs.get(0);
return exactLinks.get(i).withTypeArgBinding(node, exactSigs.get(i).getMapping());
} else {
// no methods, only extensions exact → pick first (they were narrowed already)
return exactLinks.get(0).withTypeArgBinding(node, exactSigs.get(0).getMapping());
}
}

// Pass 2: best-effort (unchanged)
int bestBad = Integer.MAX_VALUE;
FuncLink best = null;
FunctionSignature bestSig = null;
for (var f : cands) {
var sig = FunctionSignature.fromNameLink(f);
var r = sig.tryMatchAgainstArgs(argTypes, node.getArgs(), node);
if (r.getBadness() < bestBad) {
bestBad = r.getBadness();
best = f;
bestSig = r.getSig();
}
}
return best == null ? null : best.withTypeArgBinding(node, bestSig.getMapping());
}



public static @Nullable FunctionDefinition calculateDef(final ExprMemberMethod node) {
var fl = node.attrFuncLink();
return fl == null ? null : fl.getDef();
}


public static @Nullable FuncLink calculate(final ExprFunctionCall node) {
FuncLink result = searchFunction(node.getFuncName(), node, argumentTypes(node));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@ public class AttrFunctionSignature {
public static FunctionSignature calculate(StmtCall fc) {
Collection<FunctionSignature> sigs = fc.attrPossibleFunctionSignatures();
FunctionSignature sig = filterSigs(sigs, argTypes(fc), fc);

VariableBinding mapping = sig.getMapping();
for (CompileError error : mapping.getErrors()) {
fc.getErrorHandler().sendError(error);
}
if (mapping.hasUnboundTypeVars()) {

// If any argument is a closure, let it be typed using the selected signature’s
// expected parameter types before complaining about unbound type variables.
boolean hasClosureArg = false;
if (fc instanceof AstElementWithArgs) {
for (Expr a : ((AstElementWithArgs) fc).getArgs()) {
if (a instanceof ExprClosure) {
hasClosureArg = true;
break;
}
}
}

if (mapping.hasUnboundTypeVars() && !hasClosureArg) {
fc.addError("Cannot infer type for type parameter " + mapping.printUnboundTypeVars());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
package de.peeeq.wurstscript.attributes;

import de.peeeq.wurstscript.ast.ModuleDef;
import de.peeeq.wurstscript.ast.ModuleInstanciation;
import de.peeeq.wurstscript.ast.TypeDef;
import de.peeeq.wurstscript.ast.*;
import de.peeeq.wurstscript.utils.Utils;
import org.eclipse.jdt.annotation.Nullable;

public class AttrModuleInstanciations {
public final class AttrModuleInstanciations {

private AttrModuleInstanciations() {}

public static @Nullable ModuleDef getModuleOrigin(ModuleInstanciation mi) {
TypeDef def = mi.getParent().lookupType(mi.getName());
if (def instanceof ModuleDef) {
return (ModuleDef) def;
} else {
// NOTE: For ModuleInstanciation the "name" used for resolution has historically been getName().
// Keep this to preserve prior behavior.
final String name = mi.getName();

// 1) Normal path: resolve relative to the lexical parent (old behavior)
final Element parent = mi.getParent();
if (parent != null) {
TypeDef def = parent.lookupType(name, /*showErrors*/ false);
Copy link

Copilot AI Oct 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected 'showErrors' parameter comment formatting. Use standard JavaDoc style with equals sign: '/showErrors=/ false' instead of '/showErrors/ false'.

Copilot uses AI. Check for mistakes.
if (def instanceof ModuleDef) {
return (ModuleDef) def;
}
// Attached but not found -> keep the old error
mi.addError("Could not find module origin for " + Utils.printElement(mi));
return null;
}

// 2) Detached during incremental build: try the nearest attached scope
final WScope scope = mi.attrNearestScope();
if (scope != null) {
TypeDef def = scope.lookupType(name, /*showErrors*/ false);
Copy link

Copilot AI Oct 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected 'showErrors' parameter comment formatting. Use standard JavaDoc style with equals sign: '/showErrors=/ false' instead of '/showErrors/ false'.

Copilot uses AI. Check for mistakes.
if (def instanceof ModuleDef) {
return (ModuleDef) def;
}
}

// 3) Still not found and we're detached: this can be a transient state,
// so don't emit an error here. Return null and let callers handle gracefully.
return null;
}

}
Loading
Loading