Search in sources :

Example 26 with BLangLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral in project ballerina by ballerina-lang.

the class CodeGenerator method getFloatLiteral.

private BLangLiteral getFloatLiteral(double value) {
    BLangLiteral literal = (BLangLiteral) TreeBuilder.createLiteralExpression();
    literal.value = value;
    literal.typeTag = TypeTags.FLOAT;
    literal.type = symTable.floatType;
    return literal;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)

Example 27 with BLangLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral 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)

Example 28 with BLangLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangBracedOrTupleExpr bracedOrTupleExpr) {
    // Emit create array instruction
    RegIndex exprRegIndex = calcAndGetExprRegIndex(bracedOrTupleExpr);
    Operand typeCPIndex = getTypeCPIndex(bracedOrTupleExpr.type);
    emit(InstructionCodes.RNEWARRAY, exprRegIndex, typeCPIndex);
    // Emit instructions populate initial array values;
    for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
        BLangExpression argExpr = bracedOrTupleExpr.expressions.get(i);
        genNode(argExpr, this.env);
        BLangLiteral indexLiteral = new BLangLiteral();
        indexLiteral.pos = argExpr.pos;
        indexLiteral.value = (long) i;
        indexLiteral.type = symTable.intType;
        genNode(indexLiteral, this.env);
        emit(InstructionCodes.RASTORE, exprRegIndex, indexLiteral.regIndex, argExpr.regIndex);
    }
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 29 with BLangLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral in project ballerina by ballerina-lang.

the class CodeGenerator method getBooleanLiteral.

private BLangLiteral getBooleanLiteral(boolean value) {
    BLangLiteral literal = (BLangLiteral) TreeBuilder.createLiteralExpression();
    literal.value = value;
    literal.typeTag = TypeTags.BOOLEAN;
    literal.type = symTable.booleanType;
    return literal;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)

Example 30 with BLangLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral in project ballerina by ballerina-lang.

the class CodeGenerator method getDefaultValue.

private DefaultValue getDefaultValue(BLangLiteral literalExpr) {
    String desc = literalExpr.type.getDesc();
    int typeDescCPIndex = addUTF8CPEntry(currentPkgInfo, desc);
    DefaultValue defaultValue = new DefaultValue(typeDescCPIndex, desc);
    int typeTag = literalExpr.type.tag;
    switch(typeTag) {
        case TypeTags.INT:
            defaultValue.intValue = (Long) literalExpr.value;
            defaultValue.valueCPIndex = currentPkgInfo.addCPEntry(new IntegerCPEntry(defaultValue.intValue));
            break;
        case TypeTags.FLOAT:
            defaultValue.floatValue = (Double) literalExpr.value;
            defaultValue.valueCPIndex = currentPkgInfo.addCPEntry(new FloatCPEntry(defaultValue.floatValue));
            break;
        case TypeTags.STRING:
            defaultValue.stringValue = (String) literalExpr.value;
            defaultValue.valueCPIndex = currentPkgInfo.addCPEntry(new UTF8CPEntry(defaultValue.stringValue));
            break;
        case TypeTags.BOOLEAN:
            defaultValue.booleanValue = (Boolean) literalExpr.value;
            break;
        default:
            defaultValue = null;
    }
    return defaultValue;
}
Also used : DefaultValue(org.wso2.ballerinalang.programfile.DefaultValue) UTF8CPEntry(org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) IntegerCPEntry(org.wso2.ballerinalang.programfile.cpentries.IntegerCPEntry) FloatCPEntry(org.wso2.ballerinalang.programfile.cpentries.FloatCPEntry) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Aggregations

BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)23 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)15 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)11 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)8 HashMap (java.util.HashMap)4 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)4 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)4 ArrayList (java.util.ArrayList)3 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)3 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)3 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)3 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)3 Path (java.nio.file.Path)2 NodeKind (org.ballerinalang.model.tree.NodeKind)2 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)2 BXMLNSSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol)2 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)2 DefaultValue (org.wso2.ballerinalang.programfile.DefaultValue)2 ParamDefaultValueAttributeInfo (org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo)2 FloatCPEntry (org.wso2.ballerinalang.programfile.cpentries.FloatCPEntry)2