Search in sources :

Example 1 with BLangIndexBasedAccess

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

the class TaintAnalyzer method visitAssignment.

private void visitAssignment(BLangExpression varRefExpr, boolean varTaintedStatus, DiagnosticPos pos) {
    // Generate error if a global variable has been assigned with a tainted value.
    if (varTaintedStatus && varRefExpr instanceof BLangVariableReference) {
        BLangVariableReference varRef = (BLangVariableReference) varRefExpr;
        if (varRef.symbol != null && varRef.symbol.owner != null) {
            if (varRef.symbol.owner instanceof BPackageSymbol || SymbolKind.SERVICE.equals(varRef.symbol.owner.kind) || SymbolKind.CONNECTOR.equals(varRef.symbol.owner.kind)) {
                addTaintError(pos, varRef.symbol.name.value, DiagnosticCode.TAINTED_VALUE_PASSED_TO_GLOBAL_VARIABLE);
                return;
            }
        }
    }
    // TODO: Re-evaluating the full data-set (array) when a change occur.
    if (varRefExpr instanceof BLangIndexBasedAccess) {
        nonOverridingAnalysis = true;
        updatedVarRefTaintedState((BLangIndexBasedAccess) varRefExpr, varTaintedStatus);
        nonOverridingAnalysis = false;
    } else if (varRefExpr instanceof BLangFieldBasedAccess) {
        BLangFieldBasedAccess fieldBasedAccessExpr = (BLangFieldBasedAccess) varRefExpr;
        // Propagate tainted status to fields, when field symbols are present (Example: structs).
        if (fieldBasedAccessExpr.symbol != null) {
            setTaintedStatus(fieldBasedAccessExpr, varTaintedStatus);
        }
        nonOverridingAnalysis = true;
        updatedVarRefTaintedState(fieldBasedAccessExpr, varTaintedStatus);
        nonOverridingAnalysis = false;
    } else {
        BLangVariableReference varRef = (BLangVariableReference) varRefExpr;
        setTaintedStatus(varRef, varTaintedStatus);
    }
}
Also used : BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) BLangFieldBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference)

Example 2 with BLangIndexBasedAccess

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

the class TypeChecker method visit.

public void visit(BLangIndexBasedAccess indexBasedAccessExpr) {
    BType actualType = symTable.errType;
    // First analyze the variable reference expression.
    checkExpr(indexBasedAccessExpr.expr, this.env, Lists.of(symTable.noType));
    BType indexExprType;
    BType varRefType = indexBasedAccessExpr.expr.type;
    BLangExpression indexExpr = indexBasedAccessExpr.indexExpr;
    switch(varRefType.tag) {
        case TypeTags.STRUCT:
            indexExprType = checkIndexExprForStructFieldAccess(indexExpr);
            if (indexExprType.tag == TypeTags.STRING) {
                String fieldName = (String) ((BLangLiteral) indexExpr).value;
                actualType = checkStructFieldAccess(indexBasedAccessExpr, names.fromString(fieldName), varRefType);
            }
            break;
        case TypeTags.MAP:
            indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.stringType)).get(0);
            if (indexExprType.tag == TypeTags.STRING) {
                actualType = ((BMapType) varRefType).getConstraint();
            }
            break;
        case TypeTags.JSON:
            BType constraintType = ((BJSONType) varRefType).constraint;
            if (constraintType.tag == TypeTags.STRUCT) {
                indexExprType = checkIndexExprForStructFieldAccess(indexExpr);
                if (indexExprType.tag != TypeTags.STRING) {
                    break;
                }
                String fieldName = (String) ((BLangLiteral) indexExpr).value;
                BType fieldType = checkStructFieldAccess(indexBasedAccessExpr, names.fromString(fieldName), constraintType);
                // If the type of the field is struct, treat it as constraint JSON type.
                if (fieldType.tag == TypeTags.STRUCT) {
                    actualType = new BJSONType(TypeTags.JSON, fieldType, symTable.jsonType.tsymbol);
                    break;
                }
            } else {
                indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.noType)).get(0);
                if (indexExprType.tag != TypeTags.STRING && indexExprType.tag != TypeTags.INT) {
                    dlog.error(indexExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES, symTable.stringType, indexExprType);
                    break;
                }
            }
            actualType = symTable.jsonType;
            break;
        case TypeTags.ARRAY:
            indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.intType)).get(0);
            if (indexExprType.tag == TypeTags.INT) {
                actualType = ((BArrayType) varRefType).getElementType();
            }
            break;
        case TypeTags.XML:
            if (indexBasedAccessExpr.lhsVar) {
                dlog.error(indexBasedAccessExpr.pos, DiagnosticCode.CANNOT_UPDATE_XML_SEQUENCE);
                break;
            }
            checkExpr(indexExpr, this.env).get(0);
            actualType = symTable.xmlType;
            break;
        case TypeTags.ERROR:
            // Do nothing
            break;
        default:
            dlog.error(indexBasedAccessExpr.pos, DiagnosticCode.OPERATION_DOES_NOT_SUPPORT_INDEXING, indexBasedAccessExpr.expr.type);
    }
    resultTypes = types.checkTypes(indexBasedAccessExpr, Lists.of(actualType), this.expTypes);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 3 with BLangIndexBasedAccess

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

the class Desugar method visit.

@Override
public void visit(BLangIndexBasedAccess indexAccessExpr) {
    BLangVariableReference targetVarRef = indexAccessExpr;
    indexAccessExpr.indexExpr = rewriteExpr(indexAccessExpr.indexExpr);
    indexAccessExpr.expr = rewriteExpr(indexAccessExpr.expr);
    BType varRefType = indexAccessExpr.expr.type;
    if (varRefType.tag == TypeTags.STRUCT) {
        targetVarRef = new BLangStructFieldAccessExpr(indexAccessExpr.pos, indexAccessExpr.expr, indexAccessExpr.symbol);
    } else if (varRefType.tag == TypeTags.MAP) {
        targetVarRef = new BLangMapAccessExpr(indexAccessExpr.pos, indexAccessExpr.expr, indexAccessExpr.indexExpr);
    } else if (varRefType.tag == TypeTags.JSON || getElementType(varRefType).tag == TypeTags.JSON) {
        targetVarRef = new BLangJSONAccessExpr(indexAccessExpr.pos, indexAccessExpr.expr, indexAccessExpr.indexExpr);
    } else if (varRefType.tag == TypeTags.ARRAY) {
        targetVarRef = new BLangArrayAccessExpr(indexAccessExpr.pos, indexAccessExpr.expr, indexAccessExpr.indexExpr);
    } else if (varRefType.tag == TypeTags.XML) {
        targetVarRef = new BLangXMLAccessExpr(indexAccessExpr.pos, indexAccessExpr.expr, indexAccessExpr.indexExpr);
    }
    targetVarRef.lhsVar = indexAccessExpr.lhsVar;
    targetVarRef.type = indexAccessExpr.type;
    result = targetVarRef;
}
Also used : BLangStructFieldAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess.BLangStructFieldAccessExpr) BLangArrayAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangArrayAccessExpr) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangMapAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangMapAccessExpr) BLangXMLAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangXMLAccessExpr) BLangJSONAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangJSONAccessExpr)

Example 4 with BLangIndexBasedAccess

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess 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 5 with BLangIndexBasedAccess

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

the class BLangPackageBuilder method createIndexBasedAccessNode.

public void createIndexBasedAccessNode(DiagnosticPos pos, Set<Whitespace> ws) {
    BLangIndexBasedAccess indexBasedAccess = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
    indexBasedAccess.pos = pos;
    indexBasedAccess.addWS(ws);
    indexBasedAccess.indexExpr = (BLangExpression) exprNodeStack.pop();
    indexBasedAccess.expr = (BLangVariableReference) exprNodeStack.pop();
    addExpressionNode(indexBasedAccess);
}
Also used : BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)

Aggregations

BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)10 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)5 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)3 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)3 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)2 BLangFieldBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess)2 BLangVariableReference (org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference)2 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)2 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)1 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)1 BArrayType (org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)1 BJSONType (org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangStructFieldAccessExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess.BLangStructFieldAccessExpr)1 BLangArrayAccessExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangArrayAccessExpr)1 BLangJSONAccessExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangJSONAccessExpr)1 BLangMapAccessExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangMapAccessExpr)1 BLangXMLAccessExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangXMLAccessExpr)1 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1