X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fexpressions%2FEIf.java;h=b714d7678aa7e4c88461b5c3ac529a0fc75414bb;hp=e1ccd53ddc3bc2a7bdf99d30fe12eb7ac19e6044;hb=a8758de5bc19e5adb3f618d3038743a164f09912;hpb=12d9af17384d960b75d58c3935d2b7b46d93e87b diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EIf.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EIf.java index e1ccd53dd..b714d7678 100755 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EIf.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EIf.java @@ -9,12 +9,14 @@ import org.simantics.scl.compiler.errors.Locations; import org.simantics.scl.compiler.internal.codegen.references.IVal; import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter; import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator; +import org.simantics.scl.compiler.internal.interpreted.IConstant; import org.simantics.scl.compiler.internal.interpreted.IExpression; import org.simantics.scl.compiler.internal.interpreted.IIf; import org.simantics.scl.compiler.top.ExpressionInterpretationContext; import org.simantics.scl.compiler.types.Type; import org.simantics.scl.compiler.types.Types; import org.simantics.scl.compiler.types.exceptions.MatchException; +import org.simantics.scl.runtime.tuple.Tuple0; import gnu.trove.map.hash.TObjectIntHashMap; import gnu.trove.set.hash.THashSet; @@ -23,7 +25,7 @@ import gnu.trove.set.hash.TIntHashSet; public class EIf extends Expression { public Expression condition; public Expression then_; - public Expression else_; + public Expression else_; // may be null public EIf(Expression condition, Expression then_, Expression else_) { this.condition = condition; @@ -41,7 +43,8 @@ public class EIf extends Expression { public void collectRefs(TObjectIntHashMap allRefs, TIntHashSet refs) { condition.collectRefs(allRefs, refs); then_.collectRefs(allRefs, refs); - else_.collectRefs(allRefs, refs); + if(else_ != null) + else_.collectRefs(allRefs, refs); } @Override @@ -49,7 +52,8 @@ public class EIf extends Expression { TIntHashSet vars) { condition.collectVars(allVars, vars); then_.collectVars(allVars, vars); - else_.collectVars(allVars, vars); + if(else_ != null) + else_.collectVars(allVars, vars); } @Override @@ -60,20 +64,20 @@ public class EIf extends Expression { @Override public IVal toVal(Environment env, CodeWriter w) { IVal conditionVal = condition.toVal(env, w); - - CodeWriter thenBlock = w.createBlock(); - CodeWriter elseBlock = w.createBlock(); - CodeWriter joinPoint = w.createBlock(getType()); - - w.if_(conditionVal, thenBlock.getContinuation(), elseBlock.getContinuation()); - + CodeWriter thenBlock = w.createBlock(); + if(else_ != null) { + CodeWriter elseBlock = w.createBlock(); + w.if_(conditionVal, thenBlock.getContinuation(), elseBlock.getContinuation()); + + IVal elseVal = else_.toVal(env, elseBlock); + elseBlock.jump(joinPoint.getContinuation(), elseVal); + } + else { + w.if_(conditionVal, thenBlock.getContinuation(), joinPoint.getContinuation()); + } IVal thenVal = then_.toVal(env, thenBlock); thenBlock.jump(joinPoint.getContinuation(), thenVal); - - IVal elseVal = else_.toVal(env, elseBlock); - elseBlock.jump(joinPoint.getContinuation(), elseVal); - w.continueAs(joinPoint); return w.getParameters()[0]; @@ -83,14 +87,16 @@ public class EIf extends Expression { public void collectFreeVariables(THashSet vars) { condition.collectFreeVariables(vars); then_.collectFreeVariables(vars); - else_.collectFreeVariables(vars); + if(else_ != null) + else_.collectFreeVariables(vars); } @Override public Expression simplify(SimplificationContext context) { condition = condition.simplify(context); then_ = then_.simplify(context); - else_ = else_.simplify(context); + if(else_ != null) + else_ = else_.simplify(context); return this; } @@ -98,7 +104,8 @@ public class EIf extends Expression { public Expression resolve(TranslationContext context) { condition = condition.resolve(context); then_ = then_.resolve(context); - else_ = else_.resolve(context); + if(else_ != null) + else_ = else_.resolve(context); return this; } @@ -106,14 +113,17 @@ public class EIf extends Expression { public Expression replace(ReplaceContext context) { return new EIf(condition.replace(context), then_.replace(context), - else_.replace(context)); + else_ == null ? null : else_.replace(context)); } @Override public Expression checkBasicType(TypingContext context, Type requiredType) { condition = condition.checkType(context, Types.BOOLEAN); then_ = then_.checkType(context, requiredType); - else_ = else_.checkType(context, requiredType); + if(else_ != null) + else_ = else_.checkType(context, requiredType); + else + context.getErrorLog().log(location, "Else branch is required because the return value of the if expression is used."); return this; } @@ -121,7 +131,8 @@ public class EIf extends Expression { public Expression checkIgnoredType(TypingContext context) { condition = condition.checkType(context, Types.BOOLEAN); then_ = then_.checkIgnoredType(context); - else_ = else_.checkIgnoredType(context); + if(else_ != null) + else_ = else_.checkIgnoredType(context); return this; } @@ -129,20 +140,22 @@ public class EIf extends Expression { public Expression decorate(ExpressionDecorator decorator) { condition = condition.decorate(decorator); then_ = then_.decorate(decorator); - else_ = else_.decorate(decorator); + if(else_ != null) + else_ = else_.decorate(decorator); return decorator.decorate(this); } @Override public boolean isEffectful() { - return condition.isEffectful() || then_.isEffectful() || else_.isEffectful(); + return condition.isEffectful() || then_.isEffectful() || (else_ != null && else_.isEffectful()); } @Override public void collectEffects(THashSet effects) { condition.collectEffects(effects); then_.collectEffects(effects); - else_.collectEffects(effects); + if(else_ != null) + else_.collectEffects(effects); } @Override @@ -151,7 +164,8 @@ public class EIf extends Expression { location = loc; condition.setLocationDeep(loc); then_.setLocationDeep(loc); - else_.setLocationDeep(loc); + if(else_ != null) + else_.setLocationDeep(loc); } } @@ -162,26 +176,16 @@ public class EIf extends Expression { @Override public IExpression toIExpression(ExpressionInterpretationContext target) { - return new IIf(condition.toIExpression(target), then_.toIExpression(target), else_.toIExpression(target)); - } - - public Expression getCondition() { - return condition; - } - - public Expression getThen() { - return then_; - } - - public Expression getElse() { - return else_; + return new IIf(condition.toIExpression(target), then_.toIExpression(target), + else_ != null ? else_.toIExpression(target) : new IConstant(Tuple0.INSTANCE)); } @Override public void forVariables(VariableProcedure procedure) { condition.forVariables(procedure); then_.forVariables(procedure); - else_.forVariables(procedure); + if(else_ != null) + else_.forVariables(procedure); } @Override public Expression accept(ExpressionTransformer transformer) {