Search in sources :

Example 1 with DefaultValueAttributeInfo

use of org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo in project ballerina by ballerina-lang.

the class PackageInfoWriter method writeAttributeInfo.

private static void writeAttributeInfo(DataOutputStream dataOutStream, AttributeInfo attributeInfo) throws IOException {
    AttributeInfo.Kind attributeKind = attributeInfo.getKind();
    dataOutStream.writeInt(attributeInfo.getAttributeNameIndex());
    switch(attributeKind) {
        case CODE_ATTRIBUTE:
            CodeAttributeInfo codeAttributeInfo = (CodeAttributeInfo) attributeInfo;
            dataOutStream.writeInt(codeAttributeInfo.codeAddrs);
            dataOutStream.writeShort(codeAttributeInfo.maxLongLocalVars);
            dataOutStream.writeShort(codeAttributeInfo.maxDoubleLocalVars);
            dataOutStream.writeShort(codeAttributeInfo.maxStringLocalVars);
            dataOutStream.writeShort(codeAttributeInfo.maxIntLocalVars);
            dataOutStream.writeShort(codeAttributeInfo.maxByteLocalVars);
            dataOutStream.writeShort(codeAttributeInfo.maxRefLocalVars);
            dataOutStream.writeShort(codeAttributeInfo.maxLongRegs);
            dataOutStream.writeShort(codeAttributeInfo.maxDoubleRegs);
            dataOutStream.writeShort(codeAttributeInfo.maxStringRegs);
            dataOutStream.writeShort(codeAttributeInfo.maxIntRegs);
            dataOutStream.writeShort(codeAttributeInfo.maxByteRegs);
            dataOutStream.writeShort(codeAttributeInfo.maxRefRegs);
            break;
        case VARIABLE_TYPE_COUNT_ATTRIBUTE:
            VarTypeCountAttributeInfo varCountAttributeInfo = (VarTypeCountAttributeInfo) attributeInfo;
            dataOutStream.writeShort(varCountAttributeInfo.getMaxLongVars());
            dataOutStream.writeShort(varCountAttributeInfo.getMaxDoubleVars());
            dataOutStream.writeShort(varCountAttributeInfo.getMaxStringVars());
            dataOutStream.writeShort(varCountAttributeInfo.getMaxIntVars());
            dataOutStream.writeShort(varCountAttributeInfo.getMaxByteVars());
            dataOutStream.writeShort(varCountAttributeInfo.getMaxRefVars());
            break;
        case ERROR_TABLE:
            ErrorTableAttributeInfo errTable = (ErrorTableAttributeInfo) attributeInfo;
            ErrorTableEntry[] errorTableEntries = errTable.getErrorTableEntriesList().toArray(new ErrorTableEntry[0]);
            dataOutStream.writeShort(errorTableEntries.length);
            for (ErrorTableEntry errorTableEntry : errorTableEntries) {
                dataOutStream.writeInt(errorTableEntry.ipFrom);
                dataOutStream.writeInt(errorTableEntry.ipTo);
                dataOutStream.writeInt(errorTableEntry.ipTarget);
                dataOutStream.writeInt(errorTableEntry.priority);
                dataOutStream.writeInt(errorTableEntry.errorStructCPIndex);
            }
            break;
        case LOCAL_VARIABLES_ATTRIBUTE:
            LocalVariableAttributeInfo localVarAttrInfo = (LocalVariableAttributeInfo) attributeInfo;
            LocalVariableInfo[] localVarInfoArray = localVarAttrInfo.localVars.toArray(new LocalVariableInfo[0]);
            dataOutStream.writeShort(localVarInfoArray.length);
            for (LocalVariableInfo localVariableInfo : localVarInfoArray) {
                writeLocalVariableInfo(dataOutStream, localVariableInfo);
            }
            break;
        case LINE_NUMBER_TABLE_ATTRIBUTE:
            LineNumberTableAttributeInfo lnNoTblAttrInfo = (LineNumberTableAttributeInfo) attributeInfo;
            LineNumberInfo[] lineNumberInfoEntries = lnNoTblAttrInfo.getLineNumberInfoEntries();
            dataOutStream.writeShort(lineNumberInfoEntries.length);
            for (LineNumberInfo lineNumberInfo : lineNumberInfoEntries) {
                writeLineNumberInfo(dataOutStream, lineNumberInfo);
            }
            break;
        case DEFAULT_VALUE_ATTRIBUTE:
            DefaultValueAttributeInfo defaultValAttrInfo = (DefaultValueAttributeInfo) attributeInfo;
            writeDefaultValue(dataOutStream, defaultValAttrInfo.getDefaultValue());
            break;
        case PARAMETER_DEFAULTS_ATTRIBUTE:
            ParamDefaultValueAttributeInfo paramDefaultValAttrInfo = (ParamDefaultValueAttributeInfo) attributeInfo;
            DefaultValue[] defaultValues = paramDefaultValAttrInfo.getDefaultValueInfo();
            dataOutStream.writeShort(defaultValues.length);
            for (DefaultValue defaultValue : defaultValues) {
                writeDefaultValue(dataOutStream, defaultValue);
            }
            break;
    }
// TODO Support other types of attributes
}
Also used : VarTypeCountAttributeInfo(org.wso2.ballerinalang.programfile.attributes.VarTypeCountAttributeInfo) LineNumberTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.LineNumberTableAttributeInfo) CodeAttributeInfo(org.wso2.ballerinalang.programfile.attributes.CodeAttributeInfo) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) LineNumberTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.LineNumberTableAttributeInfo) ErrorTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo) AttributeInfo(org.wso2.ballerinalang.programfile.attributes.AttributeInfo) LocalVariableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.LocalVariableAttributeInfo) DefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo) VarTypeCountAttributeInfo(org.wso2.ballerinalang.programfile.attributes.VarTypeCountAttributeInfo) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) LocalVariableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.LocalVariableAttributeInfo) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) DefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo) ErrorTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo) CodeAttributeInfo(org.wso2.ballerinalang.programfile.attributes.CodeAttributeInfo)

Example 2 with DefaultValueAttributeInfo

use of org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo in project ballerina by ballerina-lang.

the class CodeGenerator method getDefaultValueAttributeInfo.

private DefaultValueAttributeInfo getDefaultValueAttributeInfo(BLangLiteral literalExpr) {
    DefaultValue defaultValue = getDefaultValue(literalExpr);
    UTF8CPEntry defaultValueAttribUTF8CPEntry = new UTF8CPEntry(AttributeInfo.Kind.DEFAULT_VALUE_ATTRIBUTE.toString());
    int defaultValueAttribNameIndex = currentPkgInfo.addCPEntry(defaultValueAttribUTF8CPEntry);
    return new DefaultValueAttributeInfo(defaultValueAttribNameIndex, defaultValue);
}
Also used : DefaultValue(org.wso2.ballerinalang.programfile.DefaultValue) UTF8CPEntry(org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) DefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 3 with DefaultValueAttributeInfo

use of org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo in project ballerina by ballerina-lang.

the class CodeGenerator method createStructInfoEntry.

private void createStructInfoEntry(BLangStruct structNode) {
    BStructSymbol structSymbol = (BStructSymbol) structNode.symbol;
    // Add Struct name as an UTFCPEntry to the constant pool
    int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
    StructInfo structInfo = new StructInfo(currentPackageRefCPIndex, structNameCPIndex, structSymbol.flags);
    currentPkgInfo.addStructInfo(structSymbol.name.value, structInfo);
    structInfo.structType = (BStructType) structSymbol.type;
    List<BLangVariable> structFields = structNode.fields;
    for (BLangVariable structField : structFields) {
        // Create StructFieldInfo Entry
        int fieldNameCPIndex = addUTF8CPEntry(currentPkgInfo, structField.name.value);
        int sigCPIndex = addUTF8CPEntry(currentPkgInfo, structField.type.getDesc());
        StructFieldInfo structFieldInfo = new StructFieldInfo(fieldNameCPIndex, sigCPIndex, structField.symbol.flags);
        structFieldInfo.fieldType = structField.type;
        // Populate default values
        if (structField.expr != null && structField.expr.getKind() == NodeKind.LITERAL) {
            DefaultValueAttributeInfo defaultVal = getDefaultValueAttributeInfo((BLangLiteral) structField.expr);
            structFieldInfo.addAttributeInfo(AttributeInfo.Kind.DEFAULT_VALUE_ATTRIBUTE, defaultVal);
        }
        structInfo.fieldInfoEntries.add(structFieldInfo);
        structField.symbol.varIndex = getFieldIndex(structField.symbol.type.tag);
    }
    // Create variable count attribute info
    prepareIndexes(fieldIndexes);
    int[] fieldCount = new int[] { fieldIndexes.tInt, fieldIndexes.tFloat, fieldIndexes.tString, fieldIndexes.tBoolean, fieldIndexes.tBlob, fieldIndexes.tRef };
    addVariableCountAttributeInfo(currentPkgInfo, structInfo, fieldCount);
    fieldIndexes = new VariableIndex(FIELD);
    // Create attached function info entries
    for (BAttachedFunction attachedFunc : structSymbol.attachedFuncs) {
        int funcNameCPIndex = addUTF8CPEntry(currentPkgInfo, attachedFunc.funcName.value);
        // Remove the first type. The first type is always the type to which the function is attached to
        BType[] paramTypes = attachedFunc.type.paramTypes.toArray(new BType[0]);
        if (paramTypes.length == 1) {
            paramTypes = new BType[0];
        } else {
            paramTypes = attachedFunc.type.paramTypes.toArray(new BType[0]);
            paramTypes = Arrays.copyOfRange(paramTypes, 1, paramTypes.length);
        }
        int sigCPIndex = addUTF8CPEntry(currentPkgInfo, generateFunctionSig(paramTypes, attachedFunc.type.retTypes.toArray(new BType[0])));
        int flags = attachedFunc.symbol.flags;
        structInfo.attachedFuncInfoEntries.add(new AttachedFunctionInfo(funcNameCPIndex, sigCPIndex, flags));
    }
}
Also used : BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) AttachedFunctionInfo(org.wso2.ballerinalang.programfile.AttachedFunctionInfo) StructInfo(org.wso2.ballerinalang.programfile.StructInfo) StructFieldInfo(org.wso2.ballerinalang.programfile.StructFieldInfo) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) DefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Aggregations

DefaultValueAttributeInfo (org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo)3 ParamDefaultValueAttributeInfo (org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo)3 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)1 BAttachedFunction (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)1 AttachedFunctionInfo (org.wso2.ballerinalang.programfile.AttachedFunctionInfo)1 DefaultValue (org.wso2.ballerinalang.programfile.DefaultValue)1 StructFieldInfo (org.wso2.ballerinalang.programfile.StructFieldInfo)1 StructInfo (org.wso2.ballerinalang.programfile.StructInfo)1 AttributeInfo (org.wso2.ballerinalang.programfile.attributes.AttributeInfo)1 CodeAttributeInfo (org.wso2.ballerinalang.programfile.attributes.CodeAttributeInfo)1 ErrorTableAttributeInfo (org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo)1 LineNumberTableAttributeInfo (org.wso2.ballerinalang.programfile.attributes.LineNumberTableAttributeInfo)1 LocalVariableAttributeInfo (org.wso2.ballerinalang.programfile.attributes.LocalVariableAttributeInfo)1 VarTypeCountAttributeInfo (org.wso2.ballerinalang.programfile.attributes.VarTypeCountAttributeInfo)1 UTF8CPEntry (org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry)1