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 @@ -36,6 +36,7 @@

import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

import static de.peeeq.wurstscript.jassIm.JassIm.*;
Expand Down Expand Up @@ -436,6 +437,33 @@ public List<TypeParamDef> capturedOwnerTypeParameters(ImClass imClass) {
return JassIm.ImClassType(ownerClass, typeArguments);
}

/**
* Translates the types of a class member's declaration, its signature or its type, in the type variable context of
* the class which declares it, whatever asked for it first. A static inner class names the parameters it captured
* from its owner by its own variables; the owner's code, or a closure in it, can ask for a member of the inner
* class before that class is translated, and the owner's context would give the member the owner's variables,
* which the inner class's instantiation does not bind. {@code own} are the declaration's own variables.
*/
private <R> R inDeclarationContext(de.peeeq.wurstscript.ast.Element declaration,
Map<TypeParamDef, ImTypeVar> own, Supplier<R> translation) {
ClassDef declaringClass = declaration.attrNearestClassDef();
Map<TypeParamDef, ImTypeVar> captured = Collections.emptyMap();
if (declaringClass != null && capturingOwnerOf(declaringClass) != null) {
getClassFor(declaringClass); // records what the class captured
captured = getTypeVarOverridesForClass(declaringClass);
}
List<Map<TypeParamDef, ImTypeVar>> requesterContext = new ArrayList<>(typeVarOverrideStack);
typeVarOverrideStack.clear();
pushTypeVarOverrides(captured);
pushTypeVarOverrides(own);
try {
return translation.get();
} finally {
typeVarOverrideStack.clear();
typeVarOverrideStack.addAll(requesterContext);
}
}

public void pushTypeVarOverrides(Map<TypeParamDef, ImTypeVar> m) {
if (m != null && !m.isEmpty()) typeVarOverrideStack.push(m);
}
Expand Down Expand Up @@ -1671,11 +1699,19 @@ public ImFunction getFuncFor(TranslatedToImFunction funcDef) {
ImTypeVars typeVars = collectTypeVarsForFunction(funcDef);
ImFunction f = ImFunction(funcDef, name, typeVars, ImVars(), ImVoid(), ImVars(), ImStmts(), flags);
Map<TypeParamDef, ImTypeVar> ov = getTypeVarOverridesForFunction(f);
pushTypeVarOverrides(ov);
try {
funcDef.imCreateFuncSkeleton(this, f);
} finally {
popTypeVarOverrides(ov);
if (funcDef instanceof ExprClosure) {
// an expression, typed where it is written
pushTypeVarOverrides(ov);
try {
funcDef.imCreateFuncSkeleton(this, f);
} finally {
popTypeVarOverrides(ov);
}
} else {
inDeclarationContext(funcDef, ov, () -> {
funcDef.imCreateFuncSkeleton(this, f);
return null;
});
}

addFunction(f, funcDef);
Expand Down Expand Up @@ -1940,12 +1976,17 @@ public ImVar getVarFor(VarDef varDef) {
if (v == null) {
requireCompilationUnit(varDef);
Map<TypeParamDef, ImTypeVar> ov = getOwnerTypeVarOverridesForStaticClassVar(varDef);
pushTypeVarOverrides(ov);
ImType type;
try {
type = varDef.attrTyp().imTranslateType(this);
} finally {
popTypeVarOverrides(ov);
if (varDef instanceof GlobalVarDef && varDef.attrNearestClassOrInterface() != null) {
// a field or static of a class, which any code can ask for first
type = inDeclarationContext(varDef, ov, () -> varDef.attrTyp().imTranslateType(this));
} else {
pushTypeVarOverrides(ov);
try {
type = varDef.attrTyp().imTranslateType(this);
} finally {
popTypeVarOverrides(ov);
}
}
String name = varDef.getName();
if (isNamedScopeVar(varDef)) {
Expand Down Expand Up @@ -3223,35 +3264,11 @@ private void addCapturedTypeVarsFromOwningGeneric(ImTypeVars typeVariables, Clas
return;
}

boolean isStatic = cd.attrIsStatic();
if (isIteratorLike(s)) WLogger.trace(() -> "[GENCAP] isStatic=" + isStatic);
if (!isStatic) return;

de.peeeq.wurstscript.ast.Element parent = cd.getParent();
de.peeeq.wurstscript.ast.Element parent2 = parent == null ? null : parent.getParent();

AstElementWithTypeParameters owner = null;
String ownerInfo = null;

if (parent2 instanceof ModuleInstanciation mi) {
ClassDef o = mi.attrNearestClassDef();
owner = o;
ownerInfo = "moduleInst=" + mi.getName() + " owner=" + (o == null ? "null" : o.getName());
} else if (parent2 instanceof ClassDef o) {
owner = o;
ownerInfo = "outerClass=" + o.getName();
} else if (parent2 instanceof InterfaceDef o) {
owner = o;
ownerInfo = "outerInterface=" + o.getName();
} else {
if (isIteratorLike(s)) WLogger.trace(() -> "[GENCAP] parent2 not ModuleInstanciation/ClassDef/InterfaceDef -> skip");
return;
AstElementWithTypeParameters owner = capturingOwnerOf(cd);
if (isIteratorLike(s) && WLogger.isTraceEnabled()) {
WLogger.trace("[GENCAP] static=" + cd.attrIsStatic() + " owner=" + owner);
}

if (isIteratorLike(s) && WLogger.isTraceEnabled()) WLogger.trace("[GENCAP] " + ownerInfo);

if (owner == null) return;
if (owner == cd) return;
if (owner instanceof ClassOrInterface ownerScope) {
capturingOwnerByStaticClass.put(cd, ownerScope);
}
Expand All @@ -3260,11 +3277,7 @@ private void addCapturedTypeVarsFromOwningGeneric(ImTypeVars typeVariables, Clas
Map<TypeParamDef, ImTypeVar> override =
capturedOwnerTypeVarsByStaticClass.computeIfAbsent(cd, k -> new IdentityHashMap<>());

for (TypeParamDef tp : owner.getTypeParameters()) {
if (!(tp.getTypeParamConstraints() instanceof TypeExprList)) {
continue;
}

for (TypeParamDef tp : typeParametersCapturedFrom(owner)) {
ImTypeVar captured = override.get(tp);
if (captured == null) {
// Create a *fresh* ImTypeVar node (do NOT reuse getTypeVar(tp))
Expand Down Expand Up @@ -3297,6 +3310,54 @@ private void addCapturedTypeVarsFromOwningGeneric(ImTypeVars typeVariables, Clas
}
}

/**
* The class or interface whose type parameters a static inner class sees: the one it is written in, or the class
* using the module it is written in. Null for any other class.
*/
private static @Nullable AstElementWithTypeParameters capturingOwnerOf(ClassDef cd) {
if (!cd.attrIsStatic()) {
return null;
}
de.peeeq.wurstscript.ast.Element parent = cd.getParent();
de.peeeq.wurstscript.ast.Element parent2 = parent == null ? null : parent.getParent();
AstElementWithTypeParameters owner;
if (parent2 instanceof ModuleInstanciation mi) {
owner = mi.attrNearestClassDef();
} else if (parent2 instanceof ClassDef o) {
owner = o;
} else if (parent2 instanceof InterfaceDef o) {
owner = o;
} else {
return null;
}
return owner == cd ? null : owner;
}

/**
* The type parameters a static inner class of {@code owner} captures, in the order of the owner's type variables:
* the owner's own, then, when the owner is a static inner class too, those it captured itself. So a class nested
* two levels deep in a generic class still has the outermost class's parameters.
*/
private static List<TypeParamDef> typeParametersCapturedFrom(AstElementWithTypeParameters owner) {
List<TypeParamDef> result = new ArrayList<>();
for (TypeParamDef tp : owner.getTypeParameters()) {
if (tp.getTypeParamConstraints() instanceof TypeExprList) {
result.add(tp);
}
}
if (owner instanceof ClassDef ownerClass) {
AstElementWithTypeParameters outer = capturingOwnerOf(ownerClass);
if (outer != null) {
for (TypeParamDef tp : typeParametersCapturedFrom(outer)) {
if (!result.contains(tp)) {
result.add(tp);
}
}
}
}
return result;
}


private final Map<ClassOrInterface, @Nullable ImClass> classForStructureDef = Maps.newLinkedHashMap();

Expand Down Expand Up @@ -3347,7 +3408,8 @@ public ImMethod getMethodFor(FuncDef f) {
// otherwise EliminateClasses dispatch lookup can fail.
String methodName = imFunc.getName();
WLogger.trace(() -> "[GENCAP] getMethodFor " + elementNameWithPath(f) + " -> methodName=" + methodName);
m = JassIm.ImMethod(f, selfType(f), methodName, imFunc, Lists.newArrayList(), Lists.newArrayList(), "", false);
ImClassType methodClass = inDeclarationContext(f, Collections.emptyMap(), () -> selfType(f));
m = JassIm.ImMethod(f, methodClass, methodName, imFunc, Lists.newArrayList(), Lists.newArrayList(), "", false);
methodForFuncDef.put(f, m);
}
return m;
Expand Down
Loading
Loading