Search in sources :

Example 6 with BArrayType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangArrayAccessExpr arrayIndexAccessExpr) {
    boolean variableStore = this.varAssignment;
    this.varAssignment = false;
    genNode(arrayIndexAccessExpr.expr, this.env);
    Operand varRefRegIndex = arrayIndexAccessExpr.expr.regIndex;
    genNode(arrayIndexAccessExpr.indexExpr, this.env);
    Operand indexRegIndex = arrayIndexAccessExpr.indexExpr.regIndex;
    BArrayType arrayType = (BArrayType) arrayIndexAccessExpr.expr.type;
    if (variableStore) {
        int opcode = getOpcode(arrayType.eType.tag, InstructionCodes.IASTORE);
        emit(opcode, varRefRegIndex, indexRegIndex, arrayIndexAccessExpr.regIndex);
    } else {
        int opcode = getOpcode(arrayType.eType.tag, InstructionCodes.IALOAD);
        emit(opcode, varRefRegIndex, indexRegIndex, calcAndGetExprRegIndex(arrayIndexAccessExpr));
    }
    this.varAssignment = variableStore;
}
Also used : BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 7 with BArrayType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.

the class Desugar method visit.

@Override
public void visit(BLangTupleDestructure stmt) {
    // var (a, b) = (tuple)
    // 
    // desugar once
    // any[] x = (tuple);
    // a = x[0];
    final BLangBlockStmt blockStmt = ASTBuilderUtil.createBlockStmt(stmt.pos);
    BType runTimeType = new BArrayType(symTable.anyType);
    final BLangVariable tuple = ASTBuilderUtil.createVariable(stmt.pos, "", runTimeType, null, new BVarSymbol(0, names.fromString("tuple"), this.env.scope.owner.pkgID, runTimeType, this.env.scope.owner));
    tuple.expr = stmt.expr;
    final BLangVariableDef variableDef = ASTBuilderUtil.createVariableDefStmt(stmt.pos, blockStmt);
    variableDef.var = tuple;
    for (int index = 0; index < stmt.varRefs.size(); index++) {
        BLangExpression varRef = stmt.varRefs.get(index);
        BLangLiteral indexExpr = ASTBuilderUtil.createLiteral(stmt.pos, symTable.intType, (long) index);
        BLangIndexBasedAccess arrayAccess = ASTBuilderUtil.createIndexBasesAccessExpr(stmt.pos, symTable.anyType, tuple.symbol, indexExpr);
        final BLangExpression assignmentExpr;
        if (types.isValueType(varRef.type)) {
            BLangTypeConversionExpr castExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
            castExpr.expr = arrayAccess;
            castExpr.conversionSymbol = Symbols.createUnboxValueTypeOpSymbol(symTable.anyType, varRef.type);
            castExpr.type = varRef.type;
            assignmentExpr = castExpr;
        } else {
            assignmentExpr = arrayAccess;
        }
        final BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(stmt.pos, blockStmt);
        assignmentStmt.declaredWithVar = stmt.declaredWithVar;
        assignmentStmt.varRefs = Lists.of(varRef);
        assignmentStmt.expr = assignmentExpr;
    }
    result = rewrite(blockStmt, env);
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 8 with BArrayType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.

the class TypeChecker method visit.

public void visit(BLangArrayLiteral arrayLiteral) {
    // Check whether the expected type is an array type
    // var a = []; and var a = [1,2,3,4]; are illegal statements, because we cannot infer the type here.
    BType actualType = symTable.errType;
    int expTypeTag = expTypes.get(0).tag;
    if (expTypeTag == TypeTags.JSON || expTypeTag == TypeTags.ANY) {
        checkExprs(arrayLiteral.exprs, this.env, expTypes.get(0));
        actualType = expTypes.get(0);
    } else if (expTypeTag == TypeTags.ARRAY) {
        BArrayType arrayType = (BArrayType) expTypes.get(0);
        checkExprs(arrayLiteral.exprs, this.env, arrayType.eType);
        actualType = new BArrayType(arrayType.eType);
    } else if (expTypeTag != TypeTags.ERROR) {
        List<BType> resTypes = checkExprs(arrayLiteral.exprs, this.env, symTable.noType);
        Set<BType> arrayLitExprTypeSet = new HashSet<>(resTypes);
        BType[] uniqueExprTypes = arrayLitExprTypeSet.toArray(new BType[0]);
        if (uniqueExprTypes.length == 0) {
            actualType = symTable.anyType;
        } else if (uniqueExprTypes.length == 1) {
            actualType = resTypes.get(0);
        } else {
            BType superType = uniqueExprTypes[0];
            for (int i = 1; i < uniqueExprTypes.length; i++) {
                if (types.isAssignable(superType, uniqueExprTypes[i])) {
                    superType = uniqueExprTypes[i];
                } else if (!types.isAssignable(uniqueExprTypes[i], superType)) {
                    superType = symTable.anyType;
                    break;
                }
            }
            actualType = superType;
        }
        actualType = new BArrayType(actualType);
    }
    resultTypes = types.checkTypes(arrayLiteral, Lists.of(actualType), expTypes);
}
Also used : BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) HashSet(java.util.HashSet)

Example 9 with BArrayType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.

the class Types method checkArrayEquality.

public boolean checkArrayEquality(BType source, BType target) {
    if (target.tag == TypeTags.ARRAY && source.tag == TypeTags.ARRAY) {
        // Both types are array types
        BArrayType lhrArrayType = (BArrayType) target;
        BArrayType rhsArrayType = (BArrayType) source;
        return checkArrayEquality(lhrArrayType.eType, rhsArrayType.eType);
    }
    // Now one or both types are not array types and they have to be equal
    return isSameType(source, target);
}
Also used : BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)

Example 10 with BArrayType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.

the class Types method checkForeachTypes.

List<BType> checkForeachTypes(BLangNode collection, int variableSize) {
    BType collectionType = collection.type;
    List<BType> errorTypes;
    int maxSupportedTypes;
    switch(collectionType.tag) {
        case TypeTags.ARRAY:
            BArrayType bArrayType = (BArrayType) collectionType;
            if (variableSize == 1) {
                return Lists.of(bArrayType.eType);
            } else if (variableSize == 2) {
                return Lists.of(symTable.intType, bArrayType.eType);
            } else {
                maxSupportedTypes = 2;
                errorTypes = Lists.of(symTable.intType, bArrayType.eType);
            }
            break;
        case TypeTags.MAP:
            BMapType bMapType = (BMapType) collectionType;
            if (variableSize == 1) {
                return Lists.of(bMapType.constraint);
            } else if (variableSize == 2) {
                return Lists.of(symTable.stringType, bMapType.constraint);
            } else {
                maxSupportedTypes = 2;
                errorTypes = Lists.of(symTable.stringType, bMapType.constraint);
            }
            break;
        case TypeTags.JSON:
            if (variableSize == 1) {
                return Lists.of(symTable.jsonType);
            } else {
                maxSupportedTypes = 1;
                errorTypes = Lists.of(symTable.jsonType);
            }
            break;
        case TypeTags.XML:
            if (variableSize == 1) {
                return Lists.of(symTable.xmlType);
            } else if (variableSize == 2) {
                return Lists.of(symTable.intType, symTable.xmlType);
            } else {
                maxSupportedTypes = 2;
                errorTypes = Lists.of(symTable.intType, symTable.xmlType);
            }
            break;
        case TypeTags.TABLE:
            BTableType tableType = (BTableType) collectionType;
            if (variableSize == 1) {
                return Lists.of(tableType.constraint);
            } else {
                maxSupportedTypes = 1;
                errorTypes = Lists.of(tableType.constraint);
            }
            break;
        case TypeTags.ERROR:
            return Collections.nCopies(variableSize, symTable.errType);
        default:
            dlog.error(collection.pos, DiagnosticCode.ITERABLE_NOT_SUPPORTED_COLLECTION, collectionType);
            return Collections.nCopies(variableSize, symTable.errType);
    }
    dlog.error(collection.pos, DiagnosticCode.ITERABLE_TOO_MANY_VARIABLES, collectionType);
    errorTypes.addAll(Collections.nCopies(variableSize - maxSupportedTypes, symTable.errType));
    return errorTypes;
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)

Aggregations

BArrayType (org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)12 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)10 BMapType (org.wso2.ballerinalang.compiler.semantics.model.types.BMapType)5 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)5 BTableType (org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)4 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)3 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)3 SymbolKind (org.ballerinalang.model.symbols.SymbolKind)2 SymbolTable (org.wso2.ballerinalang.compiler.semantics.model.SymbolTable)2 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)2 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)2 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)2 BIntermediateCollectionType (org.wso2.ballerinalang.compiler.semantics.model.types.BIntermediateCollectionType)2 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)2