Search in sources :

Example 6 with TypeRefCPEntry

use of org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry 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 7 with TypeRefCPEntry

use of org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry in project ballerina by ballerina-lang.

the class ProgramFileReader method resolveConnectorMethodTables.

private void resolveConnectorMethodTables(PackageInfo packageInfo) {
    ConnectorInfo[] connectorInfoEntries = packageInfo.getConnectorInfoEntries();
    for (ConnectorInfo connectorInfo : connectorInfoEntries) {
        BConnectorType connectorType = connectorInfo.getType();
        VarTypeCountAttributeInfo attributeInfo = (VarTypeCountAttributeInfo) connectorInfo.getAttributeInfo(AttributeInfo.Kind.VARIABLE_TYPE_COUNT_ATTRIBUTE);
        connectorType.setFieldTypeCount(attributeInfo.getVarTypeCount());
        Map<Integer, Integer> methodTableInteger = connectorInfo.getMethodTableIndex();
        Map<BConnectorType, ConnectorInfo> methodTableType = new HashMap<>();
        for (Integer key : methodTableInteger.keySet()) {
            int keyType = methodTableInteger.get(key);
            TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) packageInfo.getCPEntry(key);
            StructureRefCPEntry structureRefCPEntry = (StructureRefCPEntry) packageInfo.getCPEntry(keyType);
            ConnectorInfo connectorInfoType = (ConnectorInfo) structureRefCPEntry.getStructureTypeInfo();
            methodTableType.put((BConnectorType) typeRefCPEntry.getType(), connectorInfoType);
        }
        connectorInfo.setMethodTableType(methodTableType);
        for (ActionInfo actionInfo : connectorInfo.getActionInfoEntries()) {
            setCallableUnitSignature(actionInfo, actionInfo.getSignature(), packageInfo);
        }
    }
}
Also used : VarTypeCountAttributeInfo(org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo) HashMap(java.util.HashMap) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BConnectorType(org.ballerinalang.model.types.BConnectorType) StructureRefCPEntry(org.ballerinalang.util.codegen.cpentries.StructureRefCPEntry)

Example 8 with TypeRefCPEntry

use of org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry in project ballerina by ballerina-lang.

the class ProgramFileReader method readCPEntry.

private ConstantPoolEntry readCPEntry(DataInputStream dataInStream, ConstantPool constantPool, ConstantPoolEntry.EntryType cpEntryType) throws IOException {
    int cpIndex;
    int pkgCPIndex;
    UTF8CPEntry utf8CPEntry;
    PackageRefCPEntry packageRefCPEntry;
    Optional<PackageInfo> packageInfoOptional;
    switch(cpEntryType) {
        case CP_ENTRY_UTF8:
            short length = dataInStream.readShort();
            String strValue = null;
            // Therefore we read the UTF value only if the length >= 0.
            if (length >= 0) {
                strValue = dataInStream.readUTF();
            }
            return new UTF8CPEntry(strValue);
        case CP_ENTRY_INTEGER:
            long longVal = dataInStream.readLong();
            return new IntegerCPEntry(longVal);
        case CP_ENTRY_FLOAT:
            double doubleVal = dataInStream.readDouble();
            return new FloatCPEntry(doubleVal);
        case CP_ENTRY_STRING:
            cpIndex = dataInStream.readInt();
            utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
            return new StringCPEntry(cpIndex, utf8CPEntry.getValue());
        case CP_ENTRY_PACKAGE:
            cpIndex = dataInStream.readInt();
            utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
            return new PackageRefCPEntry(cpIndex, utf8CPEntry.getValue());
        case CP_ENTRY_FUNCTION_REF:
            pkgCPIndex = dataInStream.readInt();
            packageRefCPEntry = (PackageRefCPEntry) constantPool.getCPEntry(pkgCPIndex);
            cpIndex = dataInStream.readInt();
            utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
            String funcName = utf8CPEntry.getValue();
            FunctionRefCPEntry functionRefCPEntry = new FunctionRefCPEntry(pkgCPIndex, packageRefCPEntry.getPackageName(), cpIndex, funcName);
            // Find the functionInfo
            packageInfoOptional = Optional.ofNullable(programFile.getPackageInfo(packageRefCPEntry.getPackageName()));
            Optional<FunctionInfo> funcInfoOptional = packageInfoOptional.map(packageInfo -> packageInfo.getFunctionInfo(funcName));
            if (!funcInfoOptional.isPresent()) {
                // This must reference to the current package and the current package is not been read yet.
                // Therefore we add this to the unresolved CP Entry list.
                unresolvedCPEntries.add(functionRefCPEntry);
                return functionRefCPEntry;
            }
            functionRefCPEntry.setFunctionInfo(funcInfoOptional.get());
            return functionRefCPEntry;
        case CP_ENTRY_TRANSFORMER_REF:
            pkgCPIndex = dataInStream.readInt();
            packageRefCPEntry = (PackageRefCPEntry) constantPool.getCPEntry(pkgCPIndex);
            cpIndex = dataInStream.readInt();
            utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
            String transformerName = utf8CPEntry.getValue();
            TransformerRefCPEntry transformerRefCPEntry = new TransformerRefCPEntry(pkgCPIndex, packageRefCPEntry.getPackageName(), cpIndex, transformerName);
            // Find the transformerInfo
            packageInfoOptional = Optional.ofNullable(programFile.getPackageInfo(packageRefCPEntry.getPackageName()));
            Optional<TransformerInfo> transInfoOptional = packageInfoOptional.map(packageInfo -> packageInfo.getTransformerInfo(transformerName));
            if (!transInfoOptional.isPresent()) {
                // This must reference to the current package and the current package is not been read yet.
                // Therefore we add this to the unresolved CP Entry list.
                unresolvedCPEntries.add(transformerRefCPEntry);
                return transformerRefCPEntry;
            }
            transformerRefCPEntry.setTransformerInfo(transInfoOptional.get());
            return transformerRefCPEntry;
        case CP_ENTRY_ACTION_REF:
            pkgCPIndex = dataInStream.readInt();
            packageRefCPEntry = (PackageRefCPEntry) constantPool.getCPEntry(pkgCPIndex);
            cpIndex = dataInStream.readInt();
            UTF8CPEntry nameCPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
            String actionName = nameCPEntry.getValue();
            return new ActionRefCPEntry(pkgCPIndex, packageRefCPEntry.getPackageName(), cpIndex, actionName);
        case CP_ENTRY_STRUCTURE_REF:
            pkgCPIndex = dataInStream.readInt();
            packageRefCPEntry = (PackageRefCPEntry) constantPool.getCPEntry(pkgCPIndex);
            cpIndex = dataInStream.readInt();
            utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
            StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, packageRefCPEntry.getPackageName(), cpIndex, utf8CPEntry.getValue());
            packageInfoOptional = Optional.ofNullable(programFile.getPackageInfo(packageRefCPEntry.getPackageName()));
            Optional<StructureTypeInfo> structInfoOptional = packageInfoOptional.map(packageInfo -> packageInfo.getStructureTypeInfo(utf8CPEntry.getValue()));
            if (!structInfoOptional.isPresent()) {
                // This must reference to the current package and the current package is not been read yet.
                // Therefore we add this to the unresolved CP Entry list.
                unresolvedCPEntries.add(structureRefCPEntry);
                return structureRefCPEntry;
            }
            structureRefCPEntry.setStructureTypeInfo(structInfoOptional.get());
            return structureRefCPEntry;
        case CP_ENTRY_TYPE_REF:
            int typeSigCPIndex = dataInStream.readInt();
            utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(typeSigCPIndex);
            TypeRefCPEntry typeRefCPEntry = new TypeRefCPEntry(typeSigCPIndex, utf8CPEntry.getValue());
            unresolvedCPEntries.add(typeRefCPEntry);
            return typeRefCPEntry;
        case CP_ENTRY_FORK_JOIN:
            int forkJoinCPIndex = dataInStream.readInt();
            return new ForkJoinCPEntry(forkJoinCPIndex);
        case CP_ENTRY_WRKR_DATA_CHNL_REF:
            int uniqueNameCPIndex = dataInStream.readInt();
            UTF8CPEntry wrkrDtChnlTypesSigCPEntry = (UTF8CPEntry) constantPool.getCPEntry(uniqueNameCPIndex);
            return new WorkerDataChannelRefCPEntry(uniqueNameCPIndex, wrkrDtChnlTypesSigCPEntry.getValue());
        default:
            throw new ProgramFileFormatException("invalid constant pool entry " + cpEntryType.getValue());
    }
}
Also used : StringCPEntry(org.ballerinalang.util.codegen.cpentries.StringCPEntry) ProgramFileFormatException(org.ballerinalang.util.exceptions.ProgramFileFormatException) UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) ForkJoinCPEntry(org.ballerinalang.util.codegen.cpentries.ForkJoinCPEntry) PackageRefCPEntry(org.ballerinalang.util.codegen.cpentries.PackageRefCPEntry) TransformerRefCPEntry(org.ballerinalang.util.codegen.cpentries.TransformerRefCPEntry) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) ActionRefCPEntry(org.ballerinalang.util.codegen.cpentries.ActionRefCPEntry) FloatCPEntry(org.ballerinalang.util.codegen.cpentries.FloatCPEntry) WorkerDataChannelRefCPEntry(org.ballerinalang.util.codegen.cpentries.WorkerDataChannelRefCPEntry) FunctionRefCPEntry(org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry) StructureRefCPEntry(org.ballerinalang.util.codegen.cpentries.StructureRefCPEntry) IntegerCPEntry(org.ballerinalang.util.codegen.cpentries.IntegerCPEntry)

Example 9 with TypeRefCPEntry

use of org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry in project ballerina by ballerina-lang.

the class ProgramFileReader method readFunctionInfo.

private void readFunctionInfo(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
    int funcNameCPIndex = dataInStream.readInt();
    UTF8CPEntry funcNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(funcNameCPIndex);
    String funcName = funcNameUTF8Entry.getValue();
    FunctionInfo functionInfo = new FunctionInfo(packageInfo.getPkgNameCPIndex(), packageInfo.getPkgPath(), funcNameCPIndex, funcName);
    functionInfo.setPackageInfo(packageInfo);
    int funcSigCPIndex = dataInStream.readInt();
    UTF8CPEntry funcSigUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(funcSigCPIndex);
    setCallableUnitSignature(functionInfo, funcSigUTF8Entry.getValue(), packageInfo);
    int flags = dataInStream.readInt();
    boolean nativeFunc = Flags.isFlagOn(flags, Flags.NATIVE);
    functionInfo.setNative(nativeFunc);
    String uniqueFuncName;
    boolean attached = Flags.isFlagOn(flags, Flags.ATTACHED);
    if (attached) {
        int attachedToTypeCPIndex = dataInStream.readInt();
        functionInfo.attachedToTypeCPIndex = attachedToTypeCPIndex;
        TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) packageInfo.getCPEntry(attachedToTypeCPIndex);
        functionInfo.attachedToType = typeRefCPEntry.getType();
        uniqueFuncName = AttachedFunctionInfo.getUniqueFuncName(typeRefCPEntry.getType().getName(), funcName);
        packageInfo.addFunctionInfo(uniqueFuncName, functionInfo);
        // Update the attachedFunctionInfo
        if (typeRefCPEntry.getType().getTag() == TypeTags.STRUCT_TAG) {
            BStructType structType = (BStructType) typeRefCPEntry.getType();
            AttachedFunctionInfo attachedFuncInfo = structType.structInfo.funcInfoEntries.get(funcName);
            attachedFuncInfo.functionInfo = functionInfo;
        }
    } else {
        uniqueFuncName = funcName;
        packageInfo.addFunctionInfo(uniqueFuncName, functionInfo);
    }
    int workerDataChannelsLength = dataInStream.readShort();
    for (int i = 0; i < workerDataChannelsLength; i++) {
        readWorkerDataChannelEntries(dataInStream, packageInfo, functionInfo);
    }
    // Read worker info entries
    readWorkerInfoEntries(dataInStream, packageInfo, functionInfo);
    if (nativeFunc) {
        NativeCallableUnit nativeFunction = NativeUnitLoader.getInstance().loadNativeFunction(functionInfo.getPkgPath(), uniqueFuncName);
        if (nativeFunction == null) {
            throw new BLangRuntimeException("native function not available " + functionInfo.getPkgPath() + ":" + uniqueFuncName);
        }
        functionInfo.setNativeCallableUnit(nativeFunction);
    }
    // Read attributes
    readAttributeInfoEntries(dataInStream, packageInfo, functionInfo);
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit)

Example 10 with TypeRefCPEntry

use of org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry in project ballerina by ballerina-lang.

the class ProgramFileReader method resolveCPEntries.

private void resolveCPEntries() {
    for (ConstantPoolEntry cpEntry : unresolvedCPEntries) {
        PackageInfo packageInfo;
        StructureRefCPEntry structureRefCPEntry;
        switch(cpEntry.getEntryType()) {
            case CP_ENTRY_FUNCTION_REF:
                FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) cpEntry;
                packageInfo = programFile.getPackageInfo(funcRefCPEntry.getPackagePath());
                FunctionInfo functionInfo = packageInfo.getFunctionInfo(funcRefCPEntry.getFunctionName());
                funcRefCPEntry.setFunctionInfo(functionInfo);
                break;
            case CP_ENTRY_STRUCTURE_REF:
                structureRefCPEntry = (StructureRefCPEntry) cpEntry;
                packageInfo = programFile.getPackageInfo(structureRefCPEntry.getPackagePath());
                StructureTypeInfo structureTypeInfo = packageInfo.getStructureTypeInfo(structureRefCPEntry.getStructureName());
                structureRefCPEntry.setStructureTypeInfo(structureTypeInfo);
                break;
            case CP_ENTRY_TYPE_REF:
                TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) cpEntry;
                String typeSig = typeRefCPEntry.getTypeSig();
                BType bType = getBTypeFromDescriptor(typeSig);
                typeRefCPEntry.setType(bType);
                break;
            case CP_ENTRY_TRANSFORMER_REF:
                TransformerRefCPEntry transformerRefCPEntry = (TransformerRefCPEntry) cpEntry;
                packageInfo = programFile.getPackageInfo(transformerRefCPEntry.getPackagePath());
                TransformerInfo transformerInfo = packageInfo.getTransformerInfo(transformerRefCPEntry.getTransformerName());
                transformerRefCPEntry.setTransformerInfo(transformerInfo);
                break;
            default:
                break;
        }
    }
}
Also used : FunctionRefCPEntry(org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry) TransformerRefCPEntry(org.ballerinalang.util.codegen.cpentries.TransformerRefCPEntry) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BType(org.ballerinalang.model.types.BType) StructureRefCPEntry(org.ballerinalang.util.codegen.cpentries.StructureRefCPEntry) ConstantPoolEntry(org.ballerinalang.util.codegen.cpentries.ConstantPoolEntry)

Aggregations

TypeRefCPEntry (org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry)12 BString (org.ballerinalang.model.values.BString)6 BJSON (org.ballerinalang.model.values.BJSON)5 BStructType (org.ballerinalang.model.types.BStructType)4 BType (org.ballerinalang.model.types.BType)4 FunctionRefCPEntry (org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry)4 BRefType (org.ballerinalang.model.values.BRefType)3 BValue (org.ballerinalang.model.values.BValue)3 StructureRefCPEntry (org.ballerinalang.util.codegen.cpentries.StructureRefCPEntry)3 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)3 BBlobArray (org.ballerinalang.model.values.BBlobArray)2 BBoolean (org.ballerinalang.model.values.BBoolean)2 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)2 BFloatArray (org.ballerinalang.model.values.BFloatArray)2 BIntArray (org.ballerinalang.model.values.BIntArray)2 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)2 BStringArray (org.ballerinalang.model.values.BStringArray)2 BStruct (org.ballerinalang.model.values.BStruct)2 InstructionACALL (org.ballerinalang.util.codegen.Instruction.InstructionACALL)2 InstructionCALL (org.ballerinalang.util.codegen.Instruction.InstructionCALL)2