diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-03-09 18:11:17 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-03-09 18:11:17 +0000 |
| commit | 20dc1e124d606cf4298d5112b6d3c4f6c74e9b68 (patch) | |
| tree | 47914e8b0356b2f40e3723eae650d3bb6f100dd4 /src/main/java/org/zwobble/couscous/frontends | |
| parent | 8ac6cb16b727b5e3d28ab89fee1d169a4f74d9a1 (diff) | |
Introduce Type interface
Diffstat (limited to 'src/main/java/org/zwobble/couscous/frontends')
8 files changed, 87 insertions, 74 deletions
diff --git a/src/main/java/org/zwobble/couscous/frontends/java/GeneratedClosure.java b/src/main/java/org/zwobble/couscous/frontends/java/GeneratedClosure.java index 9386ff6..18f43be 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/GeneratedClosure.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/GeneratedClosure.java @@ -2,8 +2,7 @@ package org.zwobble.couscous.frontends.java; import org.zwobble.couscous.ast.ClassNode; import org.zwobble.couscous.ast.ReferenceNode; -import org.zwobble.couscous.ast.TypeName; -import org.zwobble.couscous.ast.VariableDeclaration; +import org.zwobble.couscous.ast.types.ScalarType; import java.util.List; @@ -20,7 +19,7 @@ public class GeneratedClosure { return classNode; } - public TypeName getType() { + public ScalarType getType() { return classNode.getName(); } diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java index 545b1c0..b08bd0c 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java @@ -5,8 +5,12 @@ import org.eclipse.jdt.core.dom.ExpressionMethodReference; import org.eclipse.jdt.core.dom.IMethodBinding; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.Modifier; -import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.ExpressionNode; +import org.zwobble.couscous.ast.FormalArgumentNode; +import org.zwobble.couscous.ast.VariableReferenceNode; import org.zwobble.couscous.ast.sugar.Lambda; +import org.zwobble.couscous.ast.types.Type; +import org.zwobble.couscous.ast.types.Types; import java.util.List; @@ -52,11 +56,11 @@ public class JavaExpressionMethodReferenceReader { IMethodBinding methodBinding = expression.resolveMethodBinding(); String methodName = expression.getName().getIdentifier(); List<ExpressionNode> arguments = eagerMap(formalArguments, VariableReferenceNode::reference); - TypeName type = typeOf(methodBinding.getReturnType()); + Type type = typeOf(methodBinding.getReturnType()); if (Modifier.isStatic(methodBinding.getModifiers())) { return staticMethodCall( - typeOf(methodBinding.getDeclaringClass()), + Types.erasure(typeOf(methodBinding.getDeclaringClass())), methodName, arguments, type); diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java index 3d8e1c0..98f2686 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java @@ -2,6 +2,8 @@ package org.zwobble.couscous.frontends.java; import org.eclipse.jdt.core.dom.*; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.Type; +import org.zwobble.couscous.ast.types.Types; import org.zwobble.couscous.values.BooleanValue; import org.zwobble.couscous.values.IntegerValue; import org.zwobble.couscous.values.ObjectValues; @@ -31,12 +33,12 @@ public class JavaExpressionReader { this.javaReader = javaReader; } - ExpressionNode readExpression(TypeName targetType, Expression expression) { + ExpressionNode readExpression(Type targetType, Expression expression) { ExpressionNode couscousExpression = readExpressionWithoutBoxing(expression); return coerceExpression(targetType, couscousExpression); } - static ExpressionNode coerceExpression(TypeName targetType, ExpressionNode couscousExpression) { + static ExpressionNode coerceExpression(Type targetType, ExpressionNode couscousExpression) { if (targetType.equals(couscousExpression.getType())) { return couscousExpression; } else { @@ -44,7 +46,7 @@ public class JavaExpressionReader { } } - private static TypeName unboxedType(TypeName type) { + private static Type unboxedType(Type type) { if (type.equals(ObjectValues.BOXED_INT)) { return IntegerValue.REF; } else if (type.equals(ObjectValues.BOXED_BOOLEAN)) { @@ -131,7 +133,7 @@ public class JavaExpressionReader { } private ExpressionNode readTypeLiteral(TypeLiteral expression) { - return literal(typeOf(expression.getType())); + return literal(Types.erasure(typeOf(expression.getType()))); } private ExpressionNode readSimpleName(SimpleName expression) { @@ -148,9 +150,9 @@ public class JavaExpressionReader { return scope.reference(binding.getKey()); } else { boolean isStatic = Modifier.isStatic(binding.getModifiers()); - TypeName classType = typeOf(binding.getDeclaringClass()); + Type classType = typeOf(binding.getDeclaringClass()); Receiver receiver = isStatic - ? new StaticReceiver(classType) + ? new StaticReceiver(Types.erasure(classType)) : new InstanceReceiver(ThisReferenceNode.thisReference(classType)); return FieldAccessNode.fieldAccess( receiver, @@ -167,7 +169,7 @@ public class JavaExpressionReader { IVariableBinding fieldBinding = expression.resolveFieldBinding(); boolean isStatic = Modifier.isStatic(fieldBinding.getModifiers()); Receiver receiver = isStatic - ? new StaticReceiver(typeOf(fieldBinding.getDeclaringClass())) + ? new StaticReceiver(Types.erasure(typeOf(fieldBinding.getDeclaringClass()))) : new InstanceReceiver(readExpressionWithoutBoxing(expression.getExpression())); return FieldAccessNode.fieldAccess( @@ -182,13 +184,13 @@ public class JavaExpressionReader { List<ExpressionNode> arguments = readArguments( expression.resolveMethodBinding(), expression.arguments()); - final TypeName type = typeOf(expression); + final Type type = typeOf(expression); IMethodBinding methodBinding = expression.resolveMethodBinding(); - TypeName receiverType = typeOf(methodBinding.getDeclaringClass()); + Type receiverType = typeOf(methodBinding.getDeclaringClass()); if ((Modifier.isStatic(methodBinding.getModifiers()))) { return staticMethodCall( - receiverType, + Types.erasure(receiverType), methodName, arguments, type); @@ -268,15 +270,15 @@ public class JavaExpressionReader { } private static boolean isPrimitiveOperation(InfixExpression expression) { - TypeName leftOperandType = typeOf(expression.getLeftOperand()); - TypeName rightOperandType = typeOf(expression.getRightOperand()); + Type leftOperandType = typeOf(expression.getLeftOperand()); + Type rightOperandType = typeOf(expression.getRightOperand()); return isPrimitive(leftOperandType) || isPrimitive(rightOperandType) || (expression.getOperator() != InfixExpression.Operator.NOT_EQUALS && expression.getOperator() != InfixExpression.Operator.EQUALS); } - private static boolean isPrimitive(TypeName type) { + private static boolean isPrimitive(Type type) { return type.equals(BooleanValue.REF) || type.equals(IntegerValue.REF); } @@ -297,7 +299,7 @@ public class JavaExpressionReader { } private ExpressionNode readConditionalExpression(ConditionalExpression expression) { - TypeName type = typeOf(expression); + Type type = typeOf(expression); return TernaryConditionalNode.ternaryConditional( readExpression(BooleanValue.REF, expression.getExpression()), readExpression(type, expression.getThenExpression()), diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java index ee4fc06..30dc2f8 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java @@ -3,13 +3,13 @@ package org.zwobble.couscous.frontends.java; import org.eclipse.jdt.core.dom.*; import org.zwobble.couscous.ast.FormalArgumentNode; import org.zwobble.couscous.ast.StatementNode; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.*; import org.zwobble.couscous.ast.sugar.Lambda; +import org.zwobble.couscous.ast.types.Type; import java.util.List; import java.util.Optional; -import static java.util.Arrays.asList; import static org.zwobble.couscous.ast.ReturnNode.returns; import static org.zwobble.couscous.ast.sugar.Lambda.lambda; import static org.zwobble.couscous.frontends.java.JavaTypes.typeOf; @@ -44,7 +44,7 @@ public class JavaLambdaExpressionReader { } private List<StatementNode> readLambdaExpressionBody(Scope scope, LambdaExpression expression) { - TypeName returnType = typeOf(expression.resolveTypeBinding().getFunctionalInterfaceMethod().getReturnType()); + Type returnType = typeOf(expression.resolveTypeBinding().getFunctionalInterfaceMethod().getReturnType()); if (expression.getBody() instanceof Block) { @SuppressWarnings("unchecked") List<Statement> statements = ((Block) expression.getBody()).statements(); diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java index 73cf749..950e678 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java @@ -8,6 +8,8 @@ import org.zwobble.couscous.ast.*; import org.zwobble.couscous.ast.sugar.AnonymousClass; import org.zwobble.couscous.ast.sugar.Lambda; import org.zwobble.couscous.ast.sugar.TypeDeclarationBody; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeTransformer; import org.zwobble.couscous.util.ExtraLists; @@ -30,6 +32,7 @@ import static org.zwobble.couscous.ast.InstanceReceiver.instanceReceiver; import static org.zwobble.couscous.ast.StaticReceiver.staticReceiver; import static org.zwobble.couscous.ast.ThisReferenceNode.thisReference; import static org.zwobble.couscous.ast.VariableReferenceNode.reference; +import static org.zwobble.couscous.ast.types.Types.erasure; import static org.zwobble.couscous.frontends.java.FreeVariables.findFreeVariables; import static org.zwobble.couscous.frontends.java.JavaTypes.*; import static org.zwobble.couscous.util.Casts.tryCast; @@ -62,7 +65,7 @@ public class JavaReader { private final ImmutableList.Builder<TypeNode> classes; private int anonymousClassCount = 0; private final Scope topScope = Scope.create(); - private final Map<TypeName, List<ReferenceNode>> nestedClasses = new HashMap<>(); + private final Map<ScalarType, List<ReferenceNode>> nestedClasses = new HashMap<>(); private JavaReader() { classes = ImmutableList.builder(); @@ -74,18 +77,18 @@ public class JavaReader { NodeTransformer transformer = NodeTransformer.builder() .transformExpression(expression -> tryCast(ConstructorCallNode.class, expression) - .flatMap(call -> lookup(nestedClasses, call.getType()).map(captures -> + .flatMap(call -> lookup(nestedClasses, erasure(call.getType())).map(captures -> new ConstructorCallNode(call.getType(), ExtraLists.concat(captures, call.getArguments()))))) .build(); return eagerMap(classes, classNode -> classNode.transform(transformer)); } private TypeNode readTypeDeclaration(TypeDeclaration type) { - TypeName name = typeOf(type.resolveBinding()); + ScalarType name = erasure(typeOf(type.resolveBinding())); Scope scope = topScope.enterClass(name); List<FormalTypeParameterNode> typeParameters = readTypeParameters(type); TypeDeclarationBody body = readTypeDeclarationBody(scope, name, type.bodyDeclarations()); - Set<TypeName> superTypes = superTypes(type); + Set<Type> superTypes = superTypes(type); if (type.isInterface()) { // TODO: throw exception if other parts of body are declared return InterfaceNode.declareInterface(name, typeParameters, superTypes, body.getMethods()); @@ -122,7 +125,7 @@ public class JavaReader { } private GeneratedClosure readLambda(Scope outerScope, Expression expression, Function<Scope, Lambda> lambda) { - TypeName name = generateAnonymousName(expression); + ScalarType name = generateAnonymousName(expression); Scope scope = outerScope.enterClass(name); IMethodBinding functionalInterfaceMethod = expression.resolveTypeBinding().getFunctionalInterfaceMethod(); return generateClosure(scope, name, toAnonymousClass(functionalInterfaceMethod, lambda.apply(scope))); @@ -144,7 +147,7 @@ public class JavaReader { } private List<StatementNode> replaceCaptureReferences( - TypeName className, + ScalarType className, List<StatementNode> body, List<CapturedVariable> freeVariables) { @@ -157,7 +160,7 @@ public class JavaReader { private ConstructorNode buildConstructor( Scope outerScope, - TypeName type, + ScalarType type, List<CapturedVariable> freeVariables, ConstructorNode existing) { @@ -178,13 +181,13 @@ public class JavaReader { return new ConstructorNode(arguments.build(), body.build()); } - private FieldAccessNode captureAccess(TypeName type, CapturedVariable freeVariable) { + private FieldAccessNode captureAccess(ScalarType type, CapturedVariable freeVariable) { FieldDeclarationNode field = freeVariable.field; return fieldAccess(thisReference(type), field.getName(), field.getType()); } GeneratedClosure readAnonymousClass(Scope outerScope, AnonymousClassDeclaration declaration) { - TypeName className = generateAnonymousName(declaration); + ScalarType className = generateAnonymousName(declaration); Scope scope = outerScope.enterClass(className); TypeDeclarationBody bodyDeclarations = readTypeDeclarationBody(scope, className, declaration.bodyDeclarations()); AnonymousClass anonymousClass = new AnonymousClass( @@ -194,7 +197,7 @@ public class JavaReader { return generateClosure(scope, className, anonymousClass); } - GeneratedClosure generateClosure(Scope scope, TypeName className, AnonymousClass anonymousClass) { + GeneratedClosure generateClosure(Scope scope, ScalarType className, AnonymousClass anonymousClass) { GeneratedClosure closure = classWithCapture(scope, className, anonymousClass); classes.add(closure.getClassNode()); return closure; @@ -245,7 +248,7 @@ public class JavaReader { return new GeneratedClosure(generatedClass, freeVariables); } - private boolean isThisReference(TypeName name, ReferenceNode reference) { + private boolean isThisReference(ScalarType name, ReferenceNode reference) { return tryCast(ThisReferenceNode.class, reference) .map(node -> node.getType().equals(name)) .orElse(false); @@ -253,7 +256,7 @@ public class JavaReader { private GeneratedClosure classWithCapture( Scope scope, - TypeName className, + ScalarType className, AnonymousClass anonymousClass ) { return classWithCapture(scope, ClassNode.declareClass( @@ -275,19 +278,19 @@ public class JavaReader { @Override public FieldDeclarationNode visit(ThisReferenceNode thisReference) { - TypeName type = thisReference.getType(); - String name = "this_" + type.getQualifiedName().replace(".", "__"); + Type type = thisReference.getType(); + String name = "this_" + erasure(type).getQualifiedName().replace(".", "__"); return field(name, type); } }); } - private TypeName generateAnonymousName(ASTNode node) { + private ScalarType generateAnonymousName(ASTNode node) { ITypeBinding type = findDeclaringClass(node); while (type.isAnonymous()) { type = type.getDeclaringClass(); } - return TypeName.of(type.getQualifiedName() + "_Anonymous_" + (anonymousClassCount++)); + return ScalarType.of(type.getQualifiedName() + "_Anonymous_" + (anonymousClassCount++)); } private ITypeBinding findDeclaringClass(ASTNode node) { @@ -297,7 +300,7 @@ public class JavaReader { return ((AbstractTypeDeclaration)node).resolveBinding(); } - private TypeDeclarationBody readTypeDeclarationBody(Scope scope, TypeName type, List<Object> bodyDeclarations) { + private TypeDeclarationBody readTypeDeclarationBody(Scope scope, ScalarType type, List<Object> bodyDeclarations) { TypeDeclarationBody.Builder body = TypeDeclarationBody.builder(); for (Object declaration : bodyDeclarations) { @@ -318,7 +321,7 @@ public class JavaReader { return body.build(); } - private TypeNode readNestedTypeDeclaration(TypeName outerType, TypeDeclaration typeDeclaration) { + private TypeNode readNestedTypeDeclaration(ScalarType outerType, TypeDeclaration typeDeclaration) { TypeNode typeNode = readTypeDeclaration(typeDeclaration); // TODO: can we remove duplication of scope creation with readTypeDeclaration()? Scope scope = topScope.enterClass(typeNode.getName()); @@ -332,10 +335,10 @@ public class JavaReader { .orElse(typeNode); } - private void readField(TypeDeclarationBody.Builder builder, Scope scope, TypeName declaringType, FieldDeclaration field) { + private void readField(TypeDeclarationBody.Builder builder, Scope scope, ScalarType declaringType, FieldDeclaration field) { @SuppressWarnings("unchecked") List<VariableDeclarationFragment> fragments = field.fragments(); - TypeName type = typeOf(field.getType()); + Type type = typeOf(field.getType()); fragments.forEach(fragment -> { builder.field(field(Modifier.isStatic(field.getModifiers()), fragment.getName().getIdentifier(), type)); if (fragment.getInitializer() != null) { @@ -358,7 +361,7 @@ public class JavaReader { List<FormalArgumentNode> formalArguments = readFormalArguments(scope, method); List<AnnotationNode> annotations = readAnnotations(method); - Optional<TypeName> returnType = Optional.ofNullable(method.getReturnType2()) + Optional<Type> returnType = Optional.ofNullable(method.getReturnType2()) .map(JavaTypes::typeOf); Optional<List<StatementNode>> body = readBody(scope, method, returnType); @@ -391,7 +394,7 @@ public class JavaReader { return eagerMap(parameters, parameter -> JavaVariableDeclarationReader.read(scope, parameter)); } - private Optional<List<StatementNode>> readBody(Scope scope, MethodDeclaration method, Optional<TypeName> returnType) { + private Optional<List<StatementNode>> readBody(Scope scope, MethodDeclaration method, Optional<Type> returnType) { if (method.getBody() == null) { return Optional.empty(); } else { @@ -406,12 +409,12 @@ public class JavaReader { return statementReader.readStatement(statement); } - List<StatementNode> readStatements(Scope scope, List<Statement> body, Optional<TypeName> returnType) { + List<StatementNode> readStatements(Scope scope, List<Statement> body, Optional<Type> returnType) { JavaStatementReader statementReader = new JavaStatementReader(scope, expressionReader(scope), returnType); return eagerFlatMap(body, statementReader::readStatement); } - ExpressionNode readExpression(Scope scope, TypeName targetType, Expression body) { + ExpressionNode readExpression(Scope scope, org.zwobble.couscous.ast.types.Type targetType, Expression body) { return expressionReader(scope).readExpression(targetType, body); } diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java index fb40ed3..ac039fb 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java @@ -3,6 +3,7 @@ package org.zwobble.couscous.frontends.java; import org.eclipse.jdt.core.dom.*; import org.zwobble.couscous.ast.*; import org.zwobble.couscous.ast.sugar.SwitchCaseNode; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.values.BooleanValue; import org.zwobble.couscous.values.ObjectValues; @@ -29,12 +30,12 @@ import static org.zwobble.couscous.util.UpToAndIncludingIterable.upToAndIncludin class JavaStatementReader { private final Scope scope; private final JavaExpressionReader expressionReader; - private final Optional<TypeName> returnType; + private final Optional<Type> returnType; JavaStatementReader( Scope scope, JavaExpressionReader expressionReader, - Optional<TypeName> returnType + Optional<org.zwobble.couscous.ast.types.Type> returnType ) { this.scope = scope; this.expressionReader = expressionReader; @@ -183,13 +184,13 @@ class JavaStatementReader { private List<StatementNode> readVariableDeclarationStatement(VariableDeclarationStatement statement) { @SuppressWarnings("unchecked") List<VariableDeclarationFragment> fragments = statement.fragments(); - TypeName type = typeOf(statement.getType()); + Type type = typeOf(statement.getType()); return readDeclarationFragments(fragments, type); } private List<StatementNode> readDeclarationFragments( List<VariableDeclarationFragment> fragments, - TypeName type + Type type ) { return eagerMap(fragments, fragment -> scope.localVariable( fragment.resolveBinding().getKey(), @@ -202,7 +203,7 @@ class JavaStatementReader { return expressionReader.readExpressionWithoutBoxing(expression); } - private ExpressionNode readExpression(TypeName targetType, Expression expression) { + private ExpressionNode readExpression(Type targetType, Expression expression) { return expressionReader.readExpression(targetType, expression); } } diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java index 117f3ca..fd59e8c 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java @@ -2,7 +2,9 @@ package org.zwobble.couscous.frontends.java; import com.google.common.collect.ImmutableSet; import org.eclipse.jdt.core.dom.*; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; +import org.zwobble.couscous.ast.types.Types; import org.zwobble.couscous.values.ObjectValues; import java.util.Optional; @@ -12,44 +14,44 @@ import static com.google.common.collect.Iterables.transform; import static java.util.Arrays.asList; class JavaTypes { - static TypeName typeOf(Expression expression) { + static Type typeOf(Expression expression) { return typeOf(expression.resolveTypeBinding()); } - static TypeName typeOf(IVariableBinding variableBinding) { + static Type typeOf(IVariableBinding variableBinding) { return typeOf(variableBinding.getType()); } - static TypeName typeOf(Type type) { + static Type typeOf(org.eclipse.jdt.core.dom.Type type) { return typeOf(type.resolveBinding()); } - static TypeName typeOf(ITypeBinding typeBinding) { + static Type typeOf(ITypeBinding typeBinding) { ITypeBinding outerClass = typeBinding.getDeclaringClass(); if (outerClass == null) { - return TypeName.of(typeBinding.getErasure().getQualifiedName()); + return ScalarType.of(typeBinding.getErasure().getQualifiedName()); } else { - return TypeName.of(typeOf(outerClass).getQualifiedName() + "__" + typeBinding.getName()); + return ScalarType.of(Types.erasure(typeOf(outerClass)).getQualifiedName() + "__" + typeBinding.getName()); } } - static Set<TypeName> superTypes(TypeDeclaration declaration) { + static Set<Type> superTypes(TypeDeclaration declaration) { return superTypes(declaration.resolveBinding()); } - static Set<TypeName> superTypes(AnonymousClassDeclaration declaration) { + static Set<Type> superTypes(AnonymousClassDeclaration declaration) { return superTypes(declaration.resolveBinding()); } - static Set<TypeName> superTypesAndSelf(ITypeBinding typeBinding) { - ImmutableSet.Builder<TypeName> superTypes = ImmutableSet.builder(); + static Set<Type> superTypesAndSelf(ITypeBinding typeBinding) { + ImmutableSet.Builder<Type> superTypes = ImmutableSet.builder(); superTypes.addAll(superTypes(typeBinding)); superTypes.add(typeOf(typeBinding)); return superTypes.build(); } - private static Set<TypeName> superTypes(ITypeBinding typeBinding) { - ImmutableSet.Builder<TypeName> superTypes = ImmutableSet.builder(); + private static Set<Type> superTypes(ITypeBinding typeBinding) { + ImmutableSet.Builder<Type> superTypes = ImmutableSet.builder(); superClass(typeBinding).ifPresent(superTypes::add); superTypes.addAll(transform( asList(typeBinding.getInterfaces()), @@ -57,7 +59,7 @@ class JavaTypes { return superTypes.build(); } - private static Optional<TypeName> superClass(ITypeBinding typeBinding) { + private static Optional<Type> superClass(ITypeBinding typeBinding) { return Optional.ofNullable(typeBinding.getSuperclass()) .map(JavaTypes::typeOf) .filter(type -> !type.equals(ObjectValues.OBJECT)); diff --git a/src/main/java/org/zwobble/couscous/frontends/java/Scope.java b/src/main/java/org/zwobble/couscous/frontends/java/Scope.java index 7ddc8ca..b02601a 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/Scope.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/Scope.java @@ -2,6 +2,8 @@ package org.zwobble.couscous.frontends.java; import org.zwobble.couscous.ast.*; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.util.NaturalNumbers; import java.util.*; @@ -28,7 +30,7 @@ public class Scope { this.temporaryCounter = temporaryCounter; } - public Scope enterClass(TypeName className) { + public Scope enterClass(ScalarType className) { return enter("class#" + className.getQualifiedName()); } @@ -45,12 +47,12 @@ public class Scope { return new Scope(variablesByKey, identifiers, identifier.extend(name), temporaryCounter); } - public FormalArgumentNode formalArgument(String name, TypeName type) { + public FormalArgumentNode formalArgument(String name, Type type) { VariableDeclaration declaration = generateVariable(name, type); return FormalArgumentNode.formalArg(declaration); } - public FormalArgumentNode formalArgument(String key, String name, TypeName type) { + public FormalArgumentNode formalArgument(String key, String name, Type type) { VariableDeclaration declaration = generateVariable(key, name, type); return FormalArgumentNode.formalArg(declaration); } @@ -58,7 +60,7 @@ public class Scope { public LocalVariableDeclarationNode localVariable( String key, String name, - TypeName type, + Type type, ExpressionNode initialValue ) { VariableDeclaration declaration = generateVariable(key, name, type); @@ -67,7 +69,7 @@ public class Scope { public LocalVariableDeclarationNode localVariable( String name, - TypeName type, + Type type, ExpressionNode initialValue ) { VariableDeclaration declaration = generateVariable(name, type); @@ -78,7 +80,7 @@ public class Scope { return temporaryVariable(initialValue.getType(), initialValue); } - public LocalVariableDeclarationNode temporaryVariable(TypeName type, ExpressionNode initialValue) { + public LocalVariableDeclarationNode temporaryVariable(Type type, ExpressionNode initialValue) { return localVariable("_couscous_tmp_" + temporaryCounter.next(), type, initialValue); } @@ -90,7 +92,7 @@ public class Scope { return VariableReferenceNode.reference(variable); } - private VariableDeclaration generateVariable(String key, String name, TypeName type) { + private VariableDeclaration generateVariable(String key, String name, Type type) { if (variablesByKey.containsKey(key)) { throw new IllegalArgumentException(key + " is already mapped"); } @@ -99,7 +101,7 @@ public class Scope { return variable; } - public VariableDeclaration generateVariable(String name, TypeName type) { + public VariableDeclaration generateVariable(String name, Type type) { Identifier identifier = generateIdentifier(name); return VariableDeclaration.var(identifier, name, type); } |
