Search in sources :

Example 16 with Type

use of kalang.core.Type in project kalang by kasonyang.

the class AstBuilder method varDecl.

protected VarInfo varDecl(VarDeclContext ctx, Type inferedType) {
    VarInfo vds = new VarInfo();
    String name = ctx.name.getText();
    TypeContext type = null;
    if (ctx.varType != null) {
        type = ctx.varType;
    } else if (ctx.type() != null) {
        type = ctx.type();
    }
    Type declType = type != null ? parseType(type) : inferedType;
    if (isDefindedId(name)) {
        diagnosisReporter.report(Diagnosis.Kind.ERROR, "the name is definded:" + name, ctx);
    }
    vds.name = name;
    vds.type = declType;
    vds.modifier = ctx.valToken != null ? Modifier.FINAL : 0;
    // }
    if (vds.type == null) {
        vds.type = Types.getRootType();
    }
    return vds;
}
Also used : WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) TypeContext(kalang.antlr.KalangParser.TypeContext)

Example 17 with Type

use of kalang.core.Type in project kalang by kasonyang.

the class AstBuilder method getObjectFieldExpr.

@Nullable
protected ExprNode getObjectFieldExpr(ExprNode expr, String fieldName, @Nullable ParserRuleContext rule) {
    ExprNode ret;
    Type type = expr.getType();
    if (!(type instanceof ObjectType)) {
        // AstBuilder.this.handleSyntaxError("unsupported type", rule==null ? ParserRuleContext.EMPTY : rule);
        return null;
    }
    ObjectType exprType = (ObjectType) type;
    if ((exprType instanceof ArrayType)) {
        return null;
    } else {
        try {
            ret = ObjectFieldExpr.create(expr, fieldName, exprType.getClassNode());
        } catch (FieldNotFoundException ex) {
            return null;
        }
    }
    if (rule != null)
        mapAst(ret, rule);
    return ret;
}
Also used : ExprNode(kalang.ast.ExprNode) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) FieldNotFoundException(kalang.FieldNotFoundException) Nullable(javax.annotation.Nullable)

Example 18 with Type

use of kalang.core.Type in project kalang by kasonyang.

the class AstBuilder method visitNewArrayExpr.

@Override
public ExprNode visitNewArrayExpr(KalangParser.NewArrayExprContext ctx) {
    Type type = parseType(ctx.type());
    ExprNode ret;
    if (ctx.size != null) {
        ExprNode size = visitExpression(ctx.size);
        ret = new NewArrayExpr(type, size);
    } else {
        ExprNode[] initExprs = new ExprNode[ctx.initExpr.size()];
        for (int i = 0; i < initExprs.length; i++) {
            initExprs[i] = visitExpression(ctx.initExpr.get(i));
        }
        ret = createInitializedArray(type, initExprs);
    }
    mapAst(ret, ctx);
    return ret;
}
Also used : ExprNode(kalang.ast.ExprNode) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) NewArrayExpr(kalang.ast.NewArrayExpr)

Example 19 with Type

use of kalang.core.Type in project kalang by kasonyang.

the class JvmClassNode method getDeclaredMethodNodes.

@Override
public MethodNode[] getDeclaredMethodNodes() {
    if (!this.methodsInitialized) {
        this.methodsInitialized = true;
        List<Executable> methods = new LinkedList();
        methods.addAll(Arrays.asList(clazz.getDeclaredMethods()));
        methods.addAll(Arrays.asList(clazz.getDeclaredConstructors()));
        for (Executable m : methods) {
            NullableKind nullable = getNullable(m.getAnnotations());
            Type mType;
            String mName;
            int mModifier;
            if (m instanceof Method) {
                mType = getType(((Method) m).getGenericReturnType(), getGenericTypeMap(), ((Method) m).getReturnType(), nullable);
                mName = m.getName();
                mModifier = m.getModifiers();
            } else if (m instanceof Constructor) {
                mName = "<init>";
                // getType(clz);
                mType = Types.VOID_TYPE;
                // | Modifier.STATIC;
                mModifier = m.getModifiers();
            } else {
                throw Exceptions.unexceptedValue(m);
            }
            MethodNode methodNode = createMethodNode(mType, mName, mModifier);
            for (Parameter p : m.getParameters()) {
                NullableKind pnullable = getNullable(p.getAnnotations());
                methodNode.createParameter(getType(p.getParameterizedType(), getGenericTypeMap(), p.getType(), pnullable), p.getName());
            }
            for (Class e : m.getExceptionTypes()) {
                methodNode.addExceptionType(getType(e, getGenericTypeMap(), e, NullableKind.NONNULL));
            }
        }
    }
    return super.getDeclaredMethodNodes();
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) WildcardType(kalang.core.WildcardType) ClassType(kalang.core.ClassType) MethodNode(kalang.ast.MethodNode) NullableKind(kalang.core.NullableKind) Constructor(java.lang.reflect.Constructor) Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method) Executable(java.lang.reflect.Executable) LinkedList(java.util.LinkedList)

Example 20 with Type

use of kalang.core.Type in project kalang by kasonyang.

the class JvmClassNode method transType.

// TODO why transType could be null?
@Nullable
private Type transType(java.lang.reflect.Type t, Map<TypeVariable, GenericType> genericTypes) {
    if (t instanceof TypeVariable) {
        GenericType vt = genericTypes.get((TypeVariable) t);
        // FIXME it may be null if TypeVariable comes from method
        if (vt != null) {
            return vt;
        }
        return null;
    } else if (t instanceof java.lang.reflect.ParameterizedType) {
        java.lang.reflect.ParameterizedType pt = (java.lang.reflect.ParameterizedType) t;
        Type rawType = transType(pt.getRawType(), genericTypes);
        if (!(rawType instanceof ObjectType)) {
            return null;
        }
        java.lang.reflect.Type[] typeArgs = pt.getActualTypeArguments();
        Type[] gTypes = transType(typeArgs, genericTypes);
        if (gTypes == null) {
            return null;
        }
        return Types.getClassType(((ObjectType) rawType).getClassNode(), gTypes);
    } else if (t instanceof java.lang.reflect.WildcardType) {
        java.lang.reflect.WildcardType wt = (java.lang.reflect.WildcardType) t;
        Type[] upperBounds = transType(wt.getUpperBounds(), genericTypes);
        if (upperBounds == null) {
            return null;
        }
        Type[] lowerBounds = transType(wt.getLowerBounds(), genericTypes);
        if (lowerBounds == null) {
            return null;
        }
        return new WildcardType(upperBounds, lowerBounds);
    } else if (t instanceof GenericArrayType) {
        GenericArrayType gt = (GenericArrayType) t;
        Type ct = transType(gt.getGenericComponentType(), genericTypes);
        if (ct == null) {
            return null;
        }
        return Types.getArrayType(ct, NullableKind.NONNULL);
    } else if (t instanceof Class) {
        Class type = (Class) t;
        if (type.isPrimitive()) {
            return Types.getPrimitiveType(type.getTypeName());
        } else if (type.isArray()) {
            Type ct = transType(type.getComponentType(), genericTypes);
            if (ct == null) {
                return null;
            }
            return Types.getArrayType(ct);
        } else {
            try {
                return Types.getClassType(this.astLoader.findAst(type));
            } catch (AstNotFoundException ex) {
                throw new RuntimeException(ex);
            }
        }
    } else {
        return null;
    }
}
Also used : GenericType(kalang.core.GenericType) GenericArrayType(java.lang.reflect.GenericArrayType) ObjectType(kalang.core.ObjectType) GenericArrayType(java.lang.reflect.GenericArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) WildcardType(kalang.core.WildcardType) ClassType(kalang.core.ClassType) WildcardType(kalang.core.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) AstNotFoundException(kalang.AstNotFoundException) Nullable(javax.annotation.Nullable)

Aggregations

ObjectType (kalang.core.ObjectType)44 Type (kalang.core.Type)44 ArrayType (kalang.core.ArrayType)37 ClassType (kalang.core.ClassType)36 GenericType (kalang.core.GenericType)36 PrimitiveType (kalang.core.PrimitiveType)33 WildcardType (kalang.core.WildcardType)30 ExprNode (kalang.ast.ExprNode)17 LinkedList (java.util.LinkedList)7 ExprStmt (kalang.ast.ExprStmt)7 VarExpr (kalang.ast.VarExpr)7 Nullable (javax.annotation.Nullable)6 AssignExpr (kalang.ast.AssignExpr)6 LocalVarNode (kalang.ast.LocalVarNode)6 BlockStmt (kalang.ast.BlockStmt)5 VarDeclStmt (kalang.ast.VarDeclStmt)5 AmbiguousMethodException (kalang.AmbiguousMethodException)4 MethodNotFoundException (kalang.MethodNotFoundException)4 ConstExpr (kalang.ast.ConstExpr)4 MethodNode (kalang.ast.MethodNode)4