Permalink
Browse files

[xbase] Some fixes:

- [xbase.lib] ExclusiveRange.getSize -> #size
- [xbase][annotations] Fix in ConstantExpressionsInterpreter
- [xbase][typesystem] Fix in compiler for early exists in branches
  • Loading branch information...
1 parent d68e20d commit cdbfd778f7ba2f2ecb59db91c7a1f13e2bc1d44c @szarnekow szarnekow committed Mar 14, 2013
@@ -128,7 +128,7 @@ public ExclusiveRange(int start, int end, boolean increment) {
* @return the number of elements in this ExclusiveRange.
*/
@Pure
- public int getSize() {
+ public int size() {
return (isEmpty()) ? 0 : Math.abs(last - first) + 1;
}
@@ -24,7 +24,6 @@ class ConstantExpressionsInterpreter {
* @param expectedType the expected type, or <code>null</code> if no type expectation exists.
*/
def Object evaluate(XExpression expression, LightweightTypeReference expectedType) {
-
switch expression {
XStringLiteral : {
if (expectedType?.simpleName=='char')
@@ -34,14 +33,15 @@ class ConstantExpressionsInterpreter {
XBooleanLiteral : expression.isTrue
XNumberLiteral : {
switch expectedType {
- case expectedType?.simpleName == 'long' : Long::parseLong(expression.value)
- case expectedType?.simpleName == 'short' : Short::parseShort(expression.value)
- case expectedType?.simpleName == 'byte' : Byte::parseByte(expression.value)
- case expectedType?.simpleName == 'float' : Long::parseLong(expression.value)
- case expectedType?.simpleName == 'double' : Double::parseDouble(expression.value)
- default: Integer::parseInt(expression.value)
+ case expectedType?.simpleName == 'long' : Long::valueOf(expression.value)
+ case expectedType?.simpleName == 'short' : Short::valueOf(expression.value)
+ case expectedType?.simpleName == 'byte' : Byte::valueOf(expression.value)
+ case expectedType?.simpleName == 'float' : Float::valueOf(expression.value)
+ case expectedType?.simpleName == 'double' : Double::valueOf(expression.value)
+ default: Integer::valueOf(expression.value)
}
}
+ // arrays?
XTypeLiteral : expression.type
XAnnotation : expression
XListLiteral : {
@@ -253,7 +253,7 @@ protected TypeData mergeTypeData(final XExpression expression, Collection<TypeDa
}
private LightweightTypeReference refineMergedType(MergeData mergeData, LightweightTypeReference mergedType, boolean returnType, boolean useExpectation) {
- if (useExpectation && mergeData.expectation != null) {
+ if (useExpectation && mergeData.expectation != null && (returnType || !mergeData.allNoImplicitReturn)) {
LightweightTypeReference expectedType = mergeData.expectation.getExpectedType();
if (expectedType != null && expectedType.isResolved()) {
if (!expectedType.isAssignableFrom(mergedType)) {
@@ -56,23 +56,23 @@ public Object evaluate(final XExpression expression, final LightweightTypeRefere
final XBooleanLiteral _xBooleanLiteral = (XBooleanLiteral)expression;
_matched=true;
boolean _isIsTrue = _xBooleanLiteral.isIsTrue();
- _switchResult = _isIsTrue;
+ _switchResult = Boolean.valueOf(_isIsTrue);
}
}
if (!_matched) {
if (expression instanceof XNumberLiteral) {
final XNumberLiteral _xNumberLiteral = (XNumberLiteral)expression;
_matched=true;
- double _switchResult_1 = (double) 0;
+ Object _switchResult_1 = null;
boolean _matched_1 = false;
if (!_matched_1) {
String _simpleName = expectedType==null?(String)null:expectedType.getSimpleName();
boolean _equals = Objects.equal(_simpleName, "long");
if (_equals) {
_matched_1=true;
String _value = _xNumberLiteral.getValue();
- long _parseLong = Long.parseLong(_value);
- _switchResult_1 = _parseLong;
+ Long _valueOf = Long.valueOf(_value);
+ _switchResult_1 = _valueOf;
}
}
if (!_matched_1) {
@@ -81,8 +81,8 @@ public Object evaluate(final XExpression expression, final LightweightTypeRefere
if (_equals_1) {
_matched_1=true;
String _value_1 = _xNumberLiteral.getValue();
- short _parseShort = Short.parseShort(_value_1);
- _switchResult_1 = _parseShort;
+ Short _valueOf_1 = Short.valueOf(_value_1);
+ _switchResult_1 = _valueOf_1;
}
}
if (!_matched_1) {
@@ -91,8 +91,8 @@ public Object evaluate(final XExpression expression, final LightweightTypeRefere
if (_equals_2) {
_matched_1=true;
String _value_2 = _xNumberLiteral.getValue();
- byte _parseByte = Byte.parseByte(_value_2);
- _switchResult_1 = _parseByte;
+ Byte _valueOf_2 = Byte.valueOf(_value_2);
+ _switchResult_1 = _valueOf_2;
}
}
if (!_matched_1) {
@@ -101,8 +101,8 @@ public Object evaluate(final XExpression expression, final LightweightTypeRefere
if (_equals_3) {
_matched_1=true;
String _value_3 = _xNumberLiteral.getValue();
- long _parseLong_1 = Long.parseLong(_value_3);
- _switchResult_1 = _parseLong_1;
+ Float _valueOf_3 = Float.valueOf(_value_3);
+ _switchResult_1 = _valueOf_3;
}
}
if (!_matched_1) {
@@ -111,16 +111,16 @@ public Object evaluate(final XExpression expression, final LightweightTypeRefere
if (_equals_4) {
_matched_1=true;
String _value_4 = _xNumberLiteral.getValue();
- double _parseDouble = Double.parseDouble(_value_4);
- _switchResult_1 = _parseDouble;
+ Double _valueOf_4 = Double.valueOf(_value_4);
+ _switchResult_1 = _valueOf_4;
}
}
if (!_matched_1) {
String _value_5 = _xNumberLiteral.getValue();
- int _parseInt = Integer.parseInt(_value_5);
- _switchResult_1 = _parseInt;
+ Integer _valueOf_5 = Integer.valueOf(_value_5);
+ _switchResult_1 = _valueOf_5;
}
- _switchResult = _switchResult_1;
+ _switchResult = ((Number)_switchResult_1);
}
}
if (!_matched) {
@@ -309,7 +309,7 @@ public Integer apply(final Integer last, final Integer i) {
if (_greaterEqualsThan) {
_xifexpression = back;
} else {
- _xifexpression = 0;
+ _xifexpression = Integer.valueOf(0);
}
final Integer fiveLinesBackOffset = _xifexpression;
Integer _xifexpression_1 = null;
@@ -319,7 +319,7 @@ public Integer apply(final Integer last, final Integer i) {
} else {
String _document = this.getDocument();
int _length = _document.length();
- _xifexpression_1 = _length;
+ _xifexpression_1 = Integer.valueOf(_length);
}
final Integer fiveLinesForwardOffset = _xifexpression_1;
String _document_1 = this.getDocument();
@@ -5,11 +5,17 @@ import org.eclipse.xtend.core.tests.AbstractXtendTestCase
import org.eclipse.xtext.xbase.annotations.interpreter.ConstantExpressionsInterpreter
import org.junit.Test
import org.eclipse.xtext.common.types.JvmType
+import org.eclipse.xtext.xbase.XBlockExpression
+import org.eclipse.xtext.xbase.typesystem.util.CommonTypeComputationServices
+import org.eclipse.xtext.xbase.typesystem.legacy.StandardTypeReferenceOwner
+import org.eclipse.xtext.xbase.typesystem.references.UnknownTypeReference
class ConstantExpressionsInterpreterTest extends AbstractXtendTestCase {
@Inject ConstantExpressionsInterpreter interpreter
+ @Inject CommonTypeComputationServices services
+
@Test def void testBooleanLiteral() {
val function = function('@Foo(true) def void testFoo() {}')
assertTrue(interpreter.evaluate(function.annotations.head.value, null) as Boolean)
@@ -39,4 +45,31 @@ class ConstantExpressionsInterpreterTest extends AbstractXtendTestCase {
val function = function('@Foo(typeof(String)) def void testFoo() {}')
assertEquals('String', (interpreter.evaluate(function.annotations.head.value, null) as JvmType).simpleName)
}
+
+ @Test def void testNumberLiteral_long() {
+ val function = function('def void testFoo() { 1 }')
+ val numberLiteral = (function.expression as XBlockExpression).expressions.head
+ val owner = new StandardTypeReferenceOwner(services, function)
+ val pseudo = new UnknownTypeReference(owner, 'long')
+ val value = interpreter.evaluate(numberLiteral, pseudo)
+ assertEquals(typeof(Long), value.class)
+ }
+
+ @Test def void testNumberLiteral_double() {
+ val function = function('def void testFoo() { 1 }')
+ val numberLiteral = (function.expression as XBlockExpression).expressions.head
+ val owner = new StandardTypeReferenceOwner(services, function)
+ val pseudo = new UnknownTypeReference(owner, 'double')
+ val value = interpreter.evaluate(numberLiteral, pseudo)
+ assertEquals(typeof(Double), value.class)
+ }
+
+ @Test def void testNumberLiteral_float() {
+ val function = function('def void testFoo() { 1 }')
+ val numberLiteral = (function.expression as XBlockExpression).expressions.head
+ val owner = new StandardTypeReferenceOwner(services, function)
+ val pseudo = new UnknownTypeReference(owner, 'float')
+ val value = interpreter.evaluate(numberLiteral, pseudo)
+ assertEquals(typeof(Float), value.class)
+ }
}
@@ -951,4 +951,65 @@ class CompilerBug342021Test extends AbstractXtendCompilerTest {
}
''')
}
+
+ @Test
+ def testNestedIfInSwitchExpression_01() {
+ assertCompilesTo('''
+ class C {
+ def Iterable<Object> m(Object o, boolean b) {
+ switch o {
+ Boolean: [|<Object>newArrayList().iterator]
+ default: {
+ if (b)
+ return newArrayList('').toArray
+ else
+ return #{}
+ }
+ }
+ }
+ }
+ ''', '''
+ import com.google.common.collect.ImmutableSet;
+ import com.google.common.collect.ImmutableSet.Builder;
+ import java.util.ArrayList;
+ import java.util.Iterator;
+ import java.util.Set;
+ import org.eclipse.xtext.xbase.lib.CollectionLiterals;
+ import org.eclipse.xtext.xbase.lib.Conversions;
+
+ @SuppressWarnings("all")
+ public class C {
+ public Iterable<Object> m(final Object o, final boolean b) {
+ Iterable<Object> _switchResult = null;
+ boolean _matched = false;
+ if (!_matched) {
+ if (o instanceof Boolean) {
+ final Boolean _boolean = (Boolean)o;
+ _matched=true;
+ final Iterable<Object> _function = new Iterable<Object>() {
+ public Iterator<Object> iterator() {
+ ArrayList<Object> _newArrayList = CollectionLiterals.<Object>newArrayList();
+ Iterator<Object> _iterator = _newArrayList.iterator();
+ return _iterator;
+ }
+ };
+ _switchResult = _function;
+ }
+ }
+ if (!_matched) {
+ if (b) {
+ ArrayList<String> _newArrayList = CollectionLiterals.<String>newArrayList("");
+ return ((Iterable<Object>)Conversions.doWrapArray(_newArrayList.toArray()));
+ } else {
+ Set<Object> _xsetliteral = null;
+ Builder<Object> _builder = ImmutableSet.builder();
+ _xsetliteral = _builder.build();
+ return _xsetliteral;
+ }
+ }
+ return _switchResult;
+ }
+ }
+ ''')
+ }
}
@@ -15,6 +15,101 @@ import org.junit.Test
class CompilerBugTest extends AbstractXtendCompilerTest {
@Test
+ def testEarlyExitInBranch_01() {
+ assertCompilesTo('''
+ class C {
+ def m() {
+ switch 'a' {
+ case 'b': 'a'
+ case 'c': {
+ if (1==2)
+ return 'b'
+ else
+ return 'c'
+ }
+ }
+ }
+ }
+ ''', '''
+ import com.google.common.base.Objects;
+
+ @SuppressWarnings("all")
+ public class C {
+ public String m() {
+ String _switchResult = null;
+ final String _switchValue = "a";
+ boolean _matched = false;
+ if (!_matched) {
+ if (Objects.equal(_switchValue,"b")) {
+ _matched=true;
+ _switchResult = "a";
+ }
+ }
+ if (!_matched) {
+ if (Objects.equal(_switchValue,"c")) {
+ _matched=true;
+ boolean _equals = (1 == 2);
+ if (_equals) {
+ return "b";
+ } else {
+ return "c";
+ }
+ }
+ }
+ return _switchResult;
+ }
+ }
+ ''')
+ }
+
+ @Test
+ def testEarlyExitInBranch_02() {
+ assertCompilesTo('''
+ class C {
+ def String m(boolean b) {
+ switch 'a' {
+ case 'b': 'a'
+ case 'c': {
+ if (b)
+ return 'b'
+ else
+ return 'c'
+ }
+ }
+ }
+ }
+ ''', '''
+ import com.google.common.base.Objects;
+
+ @SuppressWarnings("all")
+ public class C {
+ public String m(final boolean b) {
+ String _switchResult = null;
+ final String _switchValue = "a";
+ boolean _matched = false;
+ if (!_matched) {
+ if (Objects.equal(_switchValue,"b")) {
+ _matched=true;
+ _switchResult = "a";
+ }
+ }
+ if (!_matched) {
+ if (Objects.equal(_switchValue,"c")) {
+ _matched=true;
+ if (b) {
+ return "b";
+ } else {
+ return "c";
+ }
+ }
+ }
+ return _switchResult;
+ }
+ }
+ ''')
+ }
+
+ @Test
def testOverloadWithSwitchExpression_01() {
assertCompilesTo('''
import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable
Oops, something went wrong.

0 comments on commit cdbfd77

Please sign in to comment.