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 @@ -398,7 +398,7 @@ argumentList:
;

exprFunctionCall:
funcName=ID typeArgs argumentList
funcName=(ID|THIS) typeArgs argumentList
;

exprNewObject:'new' className=ID typeArgs argumentList?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ public static FuncLink calculate(final ExprFuncRef node) {


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

if (result == null) {
Expand All @@ -181,6 +184,20 @@ public static FuncLink calculate(final ExprFuncRef node) {
return result;
}

private static boolean isConstructorThisCall(ExprFunctionCall node) {
if (!node.getFuncName().equals("this")) {
return false;
}
Element current = node;
while (current != null) {
if (current instanceof ConstructorDef) {
return true;
}
current = current.getParent();
}
return false;
}

private static @Nullable FuncLink getExtensionFunction(Expr left, Expr right, WurstOperator op) {
String funcName = op.getOverloadingFuncName();
if (funcName == null || nativeOperator(op, left.attrTyp(), right.attrTyp(), left)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ private static FunctionSignature filterSigs(
Collection<FunctionSignature> sigs,
List<WurstType> argTypes, StmtCall location) {
if (sigs.isEmpty()) {
if (location instanceof ExprFunctionCall && isConstructorThisCall((ExprFunctionCall) location)) {
return FunctionSignature.empty;
}
if (!isInitTrigFunc(location)) {
if (location instanceof ExprMemberMethodDot) {
ExprMemberMethodDot emmd = (ExprMemberMethodDot) location;
Expand Down Expand Up @@ -117,6 +120,20 @@ private static FunctionSignature filterSigs(
return candidates.get(0);
}

private static boolean isConstructorThisCall(ExprFunctionCall call) {
if (!call.getFuncName().equals("this")) {
return false;
}
Element current = call;
while (current != null) {
if (current instanceof ConstructorDef) {
return true;
}
current = current.getParent();
}
return false;
}

private static List<FunctionSignature> filterByIfNotDefinedAnnotation(List<FunctionSignature> candidates) {
List<FunctionSignature> list = new ArrayList<>();
for (FunctionSignature sig : candidates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,35 @@ void handleError(List<String> hints) {
}.resolve(constructors, node).orElse(null);
}

public static @Nullable ConstructorDef resolveThisCall(List<ConstructorDef> constructors, final FunctionCall node) {
return new OverloadingResolver<ConstructorDef, FunctionCall>() {

@Override
int getParameterCount(ConstructorDef f) {
return f.getParameters().size();
}

@Override
WurstType getParameterType(ConstructorDef f, int i) {
return f.getParameters().get(i).attrTyp();
}

@Override
int getArgumentCount(FunctionCall c) {
return c.getArgs().size();
}

@Override
WurstType getArgumentType(FunctionCall c, int i) {
return c.getArgs().get(i).attrTyp();
}

@Override
void handleError(List<String> hints) {
node.addError("No suitable constructor found. \n" + Utils.join(hints, ", \n"));
}
}.resolve(constructors, node).orElse(null);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,26 @@ public static ImmutableCollection<FuncLink> calculate(final ExprMemberMethod nod
}

public static ImmutableCollection<FuncLink> calculate(final ExprFunctionCall node) {
if (isConstructorThisCall(node)) {
return ImmutableList.of();
}
return searchFunction(node.getFuncName(), node);
}

private static boolean isConstructorThisCall(ExprFunctionCall node) {
if (!node.getFuncName().equals("this")) {
return false;
}
Element current = node;
while (current != null) {
if (current instanceof ConstructorDef) {
return true;
}
current = current.getParent();
}
return false;
}

private static ImmutableCollection<FuncLink> getExtensionFunction(Expr left, Expr right, WurstOperator op) {
String funcName = op.getOverloadingFuncName();
if (funcName == null || nativeOperator(left.attrTyp(), right.attrTyp(), left)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,22 @@ public abstract class WurstGui {
public abstract void showInfoMessage(String message);

public void sendError(CompileError err) {
if (shouldSuppressWarning(err)) {
return;
}
errors.add(err);
}

private boolean shouldSuppressWarning(CompileError err) {
if (err.getErrorType() != ErrorType.WARNING) {
return false;
}
String file = err.getSource().getFile();
String normalized = file.replace('\\', '/').toLowerCase();
return normalized.contains("/_build/dependencies/")
|| normalized.startsWith("_build/dependencies/");
}

public void clearErrors() {
errors.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.peeeq.wurstscript.ast.*;
import de.peeeq.wurstscript.ast.Element;
import de.peeeq.wurstscript.attributes.OverloadingResolver;
import de.peeeq.wurstscript.jassIm.Element.DefaultVisitor;
import de.peeeq.wurstscript.jassIm.*;
import de.peeeq.wurstscript.types.*;
Expand Down Expand Up @@ -382,36 +383,75 @@ private void createConstructFunc(ConstructorDef constr) {
ConstructorDef trace = constr;
ImFunction f = translator.getConstructFunc(constr);
ImVar thisVar = translator.getThisVar(constr);
ConstructorDef superConstr = constr.attrSuperConstructor();
if (superConstr != null) {
// call super constructor
ImFunction superConstrFunc = translator.getConstructFunc(superConstr);
ImExprs arguments = ImExprs(ImVarAccess(thisVar));
for (Expr a : superArgs(constr)) {
arguments.add(a.imTranslateExpr(translator, f));
int firstRelevantIndex = firstRelevantStatementIndex(constr);
ExprFunctionCall thisCall = null;
if (firstRelevantIndex >= 0 && constr.getBody().get(firstRelevantIndex) instanceof ExprFunctionCall) {
ExprFunctionCall first = (ExprFunctionCall) constr.getBody().get(firstRelevantIndex);
if (first.getFuncName().equals("this")) {
thisCall = first;
}
ImTypeArguments typeArgs = ImTypeArguments();
ClassDef classDef = constr.attrNearestClassDef();
assert classDef != null;
WurstType extendedType = classDef.getExtendedClass().attrTyp();
if (extendedType instanceof WurstTypeClass) {
WurstTypeClass extendedTypeC = (WurstTypeClass) extendedType;
for (WurstTypeBoundTypeParam bt : extendedTypeC.getTypeParameters()) {
if (bt.isTemplateTypeParameter()) {
typeArgs.add(bt.imTranslateToTypeArgument(translator));
}
int bodyStartIndex = 0;
if (thisCall != null) {
ConstructorDef calledConstr = OverloadingResolver.resolveThisCall(
constr.attrNearestClassOrModule().getConstructors(),
thisCall
);
if (calledConstr != null && calledConstr != constr) {
ImFunction calledConstrFunc = translator.getConstructFunc(calledConstr);
ImExprs arguments = ImExprs(ImVarAccess(thisVar));
for (Expr a : thisCall.getArgs()) {
arguments.add(a.imTranslateExpr(translator, f));
}
f.getBody().add(ImFunctionCall(trace, calledConstrFunc, classTypeArgs(), arguments, false, CallType.NORMAL));
bodyStartIndex = firstRelevantIndex + 1;
}
} else {
ConstructorDef superConstr = constr.attrSuperConstructor();
if (superConstr != null) {
// call super constructor
ImFunction superConstrFunc = translator.getConstructFunc(superConstr);
ImExprs arguments = ImExprs(ImVarAccess(thisVar));
for (Expr a : superArgs(constr)) {
arguments.add(a.imTranslateExpr(translator, f));
}
ImTypeArguments typeArgs = ImTypeArguments();
ClassDef classDef = constr.attrNearestClassDef();
assert classDef != null;
WurstType extendedType = classDef.getExtendedClass().attrTyp();
if (extendedType instanceof WurstTypeClass) {
WurstTypeClass extendedTypeC = (WurstTypeClass) extendedType;
for (WurstTypeBoundTypeParam bt : extendedTypeC.getTypeParameters()) {
if (bt.isTemplateTypeParameter()) {
typeArgs.add(bt.imTranslateToTypeArgument(translator));
}
}
}
f.getBody().add(ImFunctionCall(trace, superConstrFunc, typeArgs, arguments, false, CallType.NORMAL));
}
f.getBody().add(ImFunctionCall(trace, superConstrFunc, typeArgs, arguments, false, CallType.NORMAL));
// call classInitFunc:
f.getBody().add(ImFunctionCall(trace, classInitFunc, classTypeArgs(), JassIm.ImExprs(JassIm.ImVarAccess(thisVar)), false, CallType.NORMAL));
}
// call classInitFunc:
// constructor user code
f.getBody().addAll(translator.translateStatements(f, constr.getBody().subList(bodyStartIndex, constr.getBody().size())));
}

private int firstRelevantStatementIndex(ConstructorDef constr) {
for (int i = 0; i < constr.getBody().size(); i++) {
WStatement s = constr.getBody().get(i);
if (!(s instanceof StartFunctionStatement) && !(s instanceof EndFunctionStatement)) {
return i;
}
}
return -1;
}

private ImTypeArguments classTypeArgs() {
ImTypeArguments typeArguments = JassIm.ImTypeArguments();
for (ImTypeVar tv : imClass.getTypeVariables()) {
typeArguments.add(JassIm.ImTypeArgument(JassIm.ImTypeVarRef(tv), Collections.emptyMap()));
}
f.getBody().add(ImFunctionCall(trace, classInitFunc, typeArguments, JassIm.ImExprs(JassIm.ImVarAccess(thisVar)), false, CallType.NORMAL));
// constructor user code
f.getBody().addAll(translator.translateStatements(f, constr.getBody()));
return typeArguments;
}

private void translateClassInitFunc() {
Expand Down
Loading
Loading