Search in sources :

Example 1 with BFunctionType

use of org.ballerinalang.model.types.BFunctionType in project ballerina by ballerina-lang.

the class ProgramFileReader method resolveUserDefinedTypes.

private void resolveUserDefinedTypes(PackageInfo packageInfo) {
    // TODO Improve this. We should be able to this in a single pass.
    StructInfo[] structInfoEntries = packageInfo.getStructInfoEntries();
    for (StructInfo structInfo : structInfoEntries) {
        StructFieldInfo[] fieldInfoEntries = structInfo.getFieldInfoEntries();
        BStructType structType = structInfo.getType();
        BStructType.StructField[] structFields = new BStructType.StructField[fieldInfoEntries.length];
        for (int i = 0; i < fieldInfoEntries.length; i++) {
            // Get the BType from the type descriptor
            StructFieldInfo fieldInfo = fieldInfoEntries[i];
            String typeDesc = fieldInfo.getTypeDescriptor();
            BType fieldType = getBTypeFromDescriptor(typeDesc);
            fieldInfo.setFieldType(fieldType);
            // Create the StructField in the BStructType. This is required for the type equivalence algorithm
            BStructType.StructField structField = new BStructType.StructField(fieldType, fieldInfo.getName(), fieldInfo.flags);
            structFields[i] = structField;
        }
        VarTypeCountAttributeInfo attributeInfo = (VarTypeCountAttributeInfo) structInfo.getAttributeInfo(AttributeInfo.Kind.VARIABLE_TYPE_COUNT_ATTRIBUTE);
        structType.setFieldTypeCount(attributeInfo.getVarTypeCount());
        structType.setStructFields(structFields);
        // Resolve attached function signature
        int attachedFuncCount = structInfo.funcInfoEntries.size();
        BStructType.AttachedFunction[] attachedFunctions = new BStructType.AttachedFunction[attachedFuncCount];
        int count = 0;
        for (AttachedFunctionInfo attachedFuncInfo : structInfo.funcInfoEntries.values()) {
            BFunctionType funcType = getFunctionType(attachedFuncInfo.typeSignature, packageInfo);
            BStructType.AttachedFunction attachedFunction = new BStructType.AttachedFunction(attachedFuncInfo.name, funcType, attachedFuncInfo.flags);
            attachedFunctions[count++] = attachedFunction;
            if (structInfo.initializer == attachedFuncInfo) {
                structType.initializer = attachedFunction;
            } else if (structInfo.defaultsValuesInitFunc == attachedFuncInfo) {
                structType.defaultsValuesInitFunc = attachedFunction;
            }
        }
        structType.setAttachedFunctions(attachedFunctions);
    }
    for (ConstantPoolEntry cpEntry : unresolvedCPEntries) {
        switch(cpEntry.getEntryType()) {
            case CP_ENTRY_TYPE_REF:
                TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) cpEntry;
                String typeSig = typeRefCPEntry.getTypeSig();
                BType bType = getBTypeFromDescriptor(typeSig);
                typeRefCPEntry.setType(bType);
                break;
            default:
                break;
        }
    }
}
Also used : VarTypeCountAttributeInfo(org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BFunctionType(org.ballerinalang.model.types.BFunctionType) ConstantPoolEntry(org.ballerinalang.util.codegen.cpentries.ConstantPoolEntry) BStructType(org.ballerinalang.model.types.BStructType) BType(org.ballerinalang.model.types.BType)

Example 2 with BFunctionType

use of org.ballerinalang.model.types.BFunctionType in project ballerina by ballerina-lang.

the class ProgramFileReader method setCallableUnitSignature.

private void setCallableUnitSignature(CallableUnitInfo callableUnitInfo, String sig, PackageInfo packageInfo) {
    BFunctionType funcType = getFunctionType(sig, packageInfo);
    callableUnitInfo.setParamTypes(funcType.paramTypes);
    callableUnitInfo.setRetParamTypes(funcType.retParamTypes);
}
Also used : BFunctionType(org.ballerinalang.model.types.BFunctionType)

Example 3 with BFunctionType

use of org.ballerinalang.model.types.BFunctionType in project ballerina by ballerina-lang.

the class ProgramFileReader method createBTypeFromSig.

private int createBTypeFromSig(char[] chars, int index, Stack<BType> typeStack, PackageInfo packageInfo) {
    int nameIndex;
    char ch = chars[index];
    switch(ch) {
        case 'I':
            typeStack.push(BTypes.typeInt);
            return index + 1;
        case 'F':
            typeStack.push(BTypes.typeFloat);
            return index + 1;
        case 'S':
            typeStack.push(BTypes.typeString);
            return index + 1;
        case 'B':
            typeStack.push(BTypes.typeBoolean);
            return index + 1;
        case 'L':
            typeStack.push(BTypes.typeBlob);
            return index + 1;
        case 'Y':
            typeStack.push(BTypes.typeDesc);
            return index + 1;
        case 'A':
            typeStack.push(BTypes.typeAny);
            return index + 1;
        case 'R':
            // TODO Improve this logic
            index++;
            nameIndex = index;
            while (chars[nameIndex] != ';') {
                nameIndex++;
            }
            String typeName = new String(Arrays.copyOfRange(chars, index, nameIndex));
            typeStack.push(BTypes.getTypeFromName(typeName));
            return nameIndex + 1;
        case 'C':
        case 'J':
        case 'T':
        case 'E':
        case 'D':
        case 'H':
        case 'Z':
            char typeChar = chars[index];
            // TODO Improve this logic
            index++;
            nameIndex = index;
            int colonIndex = -1;
            while (chars[nameIndex] != ';') {
                if (chars[nameIndex] == ':') {
                    colonIndex = nameIndex;
                }
                nameIndex++;
            }
            String pkgPath;
            String name;
            PackageInfo packageInfoOfType;
            if (colonIndex != -1) {
                pkgPath = new String(Arrays.copyOfRange(chars, index, colonIndex));
                name = new String(Arrays.copyOfRange(chars, colonIndex + 1, nameIndex));
                packageInfoOfType = programFile.getPackageInfo(pkgPath);
            } else {
                name = new String(Arrays.copyOfRange(chars, index, nameIndex));
                // Setting the current package;
                packageInfoOfType = packageInfo;
            }
            if (typeChar == 'C') {
                typeStack.push(packageInfoOfType.getConnectorInfo(name).getType());
            } else if (typeChar == 'J') {
                if (name.isEmpty()) {
                    typeStack.push(BTypes.typeJSON);
                } else {
                    typeStack.push(new BJSONType(packageInfoOfType.getStructInfo(name).getType()));
                }
            } else if (typeChar == 'D') {
                if (name.isEmpty()) {
                    typeStack.push(BTypes.typeTable);
                } else {
                    typeStack.push(new BTableType(packageInfoOfType.getStructInfo(name).getType()));
                }
            } else if (typeChar == 'H') {
                if (name.isEmpty()) {
                    typeStack.push(BTypes.typeStream);
                } else {
                    typeStack.push(new BStreamType(packageInfoOfType.getStructInfo(name).getType()));
                }
            } else if (typeChar == 'E') {
                typeStack.push(packageInfoOfType.getEnumInfo(name).getType());
            } else {
                // This is a struct type
                typeStack.push(packageInfoOfType.getStructInfo(name).getType());
            }
            return nameIndex + 1;
        case '[':
            index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo);
            BType elemType = typeStack.pop();
            BArrayType arrayType = new BArrayType(elemType);
            typeStack.push(arrayType);
            return index;
        case 'M':
            index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo);
            BType constrainedType = typeStack.pop();
            BType mapType;
            if (constrainedType == BTypes.typeAny) {
                mapType = BTypes.typeMap;
            } else {
                mapType = new BMapType(constrainedType);
            }
            typeStack.push(mapType);
            return index;
        case 'U':
            // TODO : Fix this for type casting.
            typeStack.push(new BFunctionType());
            return index + 1;
        case 'O':
        case 'P':
            typeChar = chars[index];
            index++;
            nameIndex = index;
            while (chars[nameIndex] != ';') {
                nameIndex++;
            }
            List<BType> memberTypes = new ArrayList<>();
            int memberCount = Integer.parseInt(new String(Arrays.copyOfRange(chars, index, nameIndex)));
            index = nameIndex;
            for (int i = 0; i < memberCount; i++) {
                index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo) - 1;
                memberTypes.add(typeStack.pop());
            }
            if (typeChar == 'O') {
                typeStack.push(new BUnionType(memberTypes));
            } else if (typeChar == 'P') {
                typeStack.push(new BTupleType(memberTypes));
            }
            return index + 1;
        case 'N':
            typeStack.push(BTypes.typeNull);
            return index + 1;
        default:
            throw new ProgramFileFormatException("unsupported base type char: " + ch);
    }
}
Also used : BArrayType(org.ballerinalang.model.types.BArrayType) BJSONType(org.ballerinalang.model.types.BJSONType) BFunctionType(org.ballerinalang.model.types.BFunctionType) ArrayList(java.util.ArrayList) BTupleType(org.ballerinalang.model.types.BTupleType) ProgramFileFormatException(org.ballerinalang.util.exceptions.ProgramFileFormatException) BStreamType(org.ballerinalang.model.types.BStreamType) BUnionType(org.ballerinalang.model.types.BUnionType) BMapType(org.ballerinalang.model.types.BMapType) BType(org.ballerinalang.model.types.BType) BTableType(org.ballerinalang.model.types.BTableType)

Example 4 with BFunctionType

use of org.ballerinalang.model.types.BFunctionType in project ballerina by ballerina-lang.

the class ProgramFileReader method getBTypeFromDescriptor.

private BType getBTypeFromDescriptor(String desc) {
    char ch = desc.charAt(0);
    switch(ch) {
        case 'I':
            return BTypes.typeInt;
        case 'F':
            return BTypes.typeFloat;
        case 'S':
            return BTypes.typeString;
        case 'B':
            return BTypes.typeBoolean;
        case 'Y':
            return BTypes.typeDesc;
        case 'L':
            return BTypes.typeBlob;
        case 'A':
            return BTypes.typeAny;
        case 'R':
            return BTypes.getTypeFromName(desc.substring(1, desc.length() - 1));
        case 'M':
            BType constrainedType = getBTypeFromDescriptor(desc.substring(1));
            if (constrainedType == BTypes.typeAny) {
                return BTypes.typeMap;
            } else {
                return new BMapType(constrainedType);
            }
        case 'C':
        case 'X':
        case 'J':
        case 'T':
        case 'E':
        case 'H':
        case 'Z':
        case 'D':
            String typeName = desc.substring(1, desc.length() - 1);
            String[] parts = typeName.split(":");
            if (parts.length == 1) {
                if (ch == 'J') {
                    return BTypes.typeJSON;
                } else if (ch == 'D') {
                    return BTypes.typeTable;
                } else if (ch == 'H') {
                    // TODO:CHECK
                    return BTypes.typeStream;
                }
            }
            String pkgPath = parts[0];
            String name = parts[1];
            PackageInfo packageInfoOfType = programFile.getPackageInfo(pkgPath);
            if (ch == 'J') {
                return new BJSONType(packageInfoOfType.getStructInfo(name).getType());
            } else if (ch == 'C') {
                return packageInfoOfType.getConnectorInfo(name).getType();
            } else if (ch == 'X') {
                return packageInfoOfType.getServiceInfo(name).getType();
            } else if (ch == 'D') {
                return new BTableType(packageInfoOfType.getStructInfo(name).getType());
            } else if (ch == 'H') {
                return new BStreamType(packageInfoOfType.getStructInfo(name).getType());
            } else if (ch == 'E') {
                return packageInfoOfType.getEnumInfo(name).getType();
            } else {
                return packageInfoOfType.getStructInfo(name).getType();
            }
        case '[':
            BType elemType = getBTypeFromDescriptor(desc.substring(1));
            return new BArrayType(elemType);
        case 'U':
            // TODO : Fix this for type casting.
            return new BFunctionType();
        case 'O':
        case 'P':
            Stack<BType> typeStack = new Stack<BType>();
            createBTypeFromSig(desc.toCharArray(), 0, typeStack, null);
            return typeStack.pop();
        case 'N':
            return BTypes.typeNull;
        default:
            throw new ProgramFileFormatException("unsupported base type char: " + ch);
    }
}
Also used : BArrayType(org.ballerinalang.model.types.BArrayType) BJSONType(org.ballerinalang.model.types.BJSONType) BFunctionType(org.ballerinalang.model.types.BFunctionType) ProgramFileFormatException(org.ballerinalang.util.exceptions.ProgramFileFormatException) BStreamType(org.ballerinalang.model.types.BStreamType) Stack(java.util.Stack) BMapType(org.ballerinalang.model.types.BMapType) BType(org.ballerinalang.model.types.BType) BTableType(org.ballerinalang.model.types.BTableType)

Aggregations

BFunctionType (org.ballerinalang.model.types.BFunctionType)4 BType (org.ballerinalang.model.types.BType)3 BArrayType (org.ballerinalang.model.types.BArrayType)2 BJSONType (org.ballerinalang.model.types.BJSONType)2 BMapType (org.ballerinalang.model.types.BMapType)2 BStreamType (org.ballerinalang.model.types.BStreamType)2 BTableType (org.ballerinalang.model.types.BTableType)2 ProgramFileFormatException (org.ballerinalang.util.exceptions.ProgramFileFormatException)2 ArrayList (java.util.ArrayList)1 Stack (java.util.Stack)1 BStructType (org.ballerinalang.model.types.BStructType)1 BTupleType (org.ballerinalang.model.types.BTupleType)1 BUnionType (org.ballerinalang.model.types.BUnionType)1 VarTypeCountAttributeInfo (org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo)1 ConstantPoolEntry (org.ballerinalang.util.codegen.cpentries.ConstantPoolEntry)1 TypeRefCPEntry (org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry)1