Search in sources :

Example 81 with BType

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

the class TypeChecker method checkNamedTransformerInvocation.

private List<BType> checkNamedTransformerInvocation(BLangTypeConversionExpr conversionExpr, BType sourceType, BType targetType) {
    List<BType> actualTypes = getListWithErrorTypes(expTypes.size());
    BLangInvocation transformerInvocation = conversionExpr.transformerInvocation;
    BSymbol transformerSymbol = symResolver.lookupSymbolInPackage(transformerInvocation.pos, env, names.fromIdNode(transformerInvocation.pkgAlias), names.fromIdNode(transformerInvocation.name), SymTag.TRANSFORMER);
    if (transformerSymbol == symTable.notFoundSymbol) {
        dlog.error(conversionExpr.pos, DiagnosticCode.UNDEFINED_TRANSFORMER, transformerInvocation.name);
    } else {
        conversionExpr.conversionSymbol = (BConversionOperatorSymbol) (transformerInvocation.symbol = transformerSymbol);
        // Check the transformer invocation. Expected type for the transformer is the target type
        // of the cast conversion operator, but not the lhs type.
        List<BType> prevExpType = expTypes;
        expTypes = Lists.of(targetType);
        checkInvocationParamAndReturnType(transformerInvocation);
        expTypes = prevExpType;
        if (transformerInvocation.type != symTable.errType) {
            BInvokableType transformerSymType = (BInvokableType) transformerSymbol.type;
            transformerInvocation.types = transformerSymType.retTypes;
            actualTypes = getActualTypesOfConversionExpr(conversionExpr, targetType, sourceType, conversionExpr.conversionSymbol);
        }
    }
    return actualTypes;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)

Example 82 with BType

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

the class CodeAnalyzer method visit.

public void visit(BLangTryCatchFinally tryNode) {
    this.checkStatementExecutionValidity(tryNode);
    tryNode.tryBody.accept(this);
    this.resetStatementReturns();
    List<BType> caughtTypes = new ArrayList<>();
    for (BLangCatch bLangCatch : tryNode.getCatchBlocks()) {
        if (caughtTypes.contains(bLangCatch.getParameter().type)) {
            dlog.error(bLangCatch.getParameter().pos, DiagnosticCode.DUPLICATED_ERROR_CATCH, bLangCatch.getParameter().type);
        }
        caughtTypes.add(bLangCatch.getParameter().type);
        bLangCatch.body.accept(this);
        this.resetStatementReturns();
    }
    if (tryNode.finallyBody != null) {
        tryNode.finallyBody.accept(this);
        this.resetStatementReturns();
    }
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BLangCatch(org.wso2.ballerinalang.compiler.tree.statements.BLangCatch)

Example 83 with BType

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

the class IterableAnalyzer method validateLambdaReturnArgs.

private void validateLambdaReturnArgs(Operation operation, List<BType> supportedTypes, List<BType> givenTypes) {
    if (supportedTypes == givenTypes) {
        // Ignore this validation.
        return;
    }
    if (givenTypes.size() > supportedTypes.size()) {
        dlog.error(operation.pos, DiagnosticCode.ITERABLE_TOO_MANY_RETURN_VARIABLES, operation.kind);
        operation.outputType = operation.resultType = symTable.errType;
        return;
    } else if (givenTypes.size() < supportedTypes.size()) {
        dlog.error(operation.pos, DiagnosticCode.ITERABLE_NOT_ENOUGH_RETURN_VARIABLES, operation.kind);
        operation.outputType = operation.resultType = symTable.errType;
        return;
    }
    for (int i = 0; i < givenTypes.size(); i++) {
        if (givenTypes.get(i).tag == TypeTags.ERROR) {
            return;
        }
        BType result = types.checkType(operation.pos, givenTypes.get(i), supportedTypes.get(i), DiagnosticCode.ITERABLE_LAMBDA_INCOMPATIBLE_TYPES);
        if (result.tag == TypeTags.ERROR) {
            operation.outputType = operation.resultType = symTable.errType;
        }
    }
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 84 with BType

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

the class SemanticAnalyzer method handleSafeAssignment.

private void handleSafeAssignment(DiagnosticPos lhsPos, BType lhsType, BLangExpression rhsExpr, SymbolEnv env) {
    // Collect all the lhs types
    Set<BType> lhsTypes = lhsType.tag == TypeTags.UNION ? ((BUnionType) lhsType).memberTypes : new HashSet<BType>() {

        {
            add(lhsType);
        }
    };
    // If there is at least one lhs type which assignable to the error type, then report an error.
    for (BType type : lhsTypes) {
        if (types.isAssignable(symTable.errStructType, type)) {
            dlog.error(lhsPos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
            typeChecker.checkExpr(rhsExpr, env, Lists.of(symTable.errType));
            return;
        }
    }
    // Create a new union type with the error type and continue the type checking process.
    lhsTypes.add(symTable.errStructType);
    BUnionType lhsUnionType = new BUnionType(null, lhsTypes, lhsTypes.contains(symTable.nullType));
    typeChecker.checkExpr(rhsExpr, env, Lists.of(lhsUnionType));
    if (rhsExpr.type.tag == TypeTags.UNION) {
        BUnionType rhsUnionType = (BUnionType) rhsExpr.type;
        for (BType type : rhsUnionType.memberTypes) {
            if (types.isAssignable(symTable.errStructType, type)) {
                return;
            }
        }
    } else if (rhsExpr.type.tag != TypeTags.ERROR) {
        dlog.error(rhsExpr.pos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
    }
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 85 with BType

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

the class SemanticAnalyzer method visit.

public void visit(BLangVariable varNode) {
    int ownerSymTag = env.scope.owner.tag;
    if ((ownerSymTag & SymTag.INVOKABLE) == SymTag.INVOKABLE) {
        // If the variable is parameter then the variable symbol is already defined
        if (varNode.symbol == null) {
            symbolEnter.defineNode(varNode, env);
        }
    }
    BType lhsType = varNode.symbol.type;
    varNode.type = lhsType;
    // Here we validate annotation attachments for package level variables.
    varNode.annAttachments.forEach(a -> {
        a.attachmentPoint = new BLangAnnotationAttachmentPoint(BLangAnnotationAttachmentPoint.AttachmentPoint.CONST);
        a.accept(this);
    });
    // Here we validate document attachments for package level variables.
    varNode.docAttachments.forEach(doc -> {
        doc.accept(this);
    });
    // Analyze the init expression
    BLangExpression rhsExpr = varNode.expr;
    if (rhsExpr == null) {
        return;
    }
    // Here we create a new symbol environment to catch self references by keep the current
    // variable symbol in the symbol environment
    // e.g. int a = x + a;
    SymbolEnv varInitEnv = SymbolEnv.createVarInitEnv(varNode, env, varNode.symbol);
    // It will we done during the init-function of the respective construct is visited.
    if ((ownerSymTag & SymTag.PACKAGE) == SymTag.PACKAGE || (ownerSymTag & SymTag.SERVICE) == SymTag.SERVICE || (ownerSymTag & SymTag.CONNECTOR) == SymTag.CONNECTOR) {
        return;
    }
    // Return if this not a safe assignment
    if (!varNode.safeAssignment) {
        typeChecker.checkExpr(rhsExpr, varInitEnv, Lists.of(lhsType));
        return;
    }
    handleSafeAssignment(varNode.pos, lhsType, rhsExpr, varInitEnv);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint)

Aggregations

BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)97 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)34 ArrayList (java.util.ArrayList)30 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)30 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)25 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)23 Name (org.wso2.ballerinalang.compiler.util.Name)20 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)18 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)17 BArrayType (org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)17 BUnionType (org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType)17 List (java.util.List)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)16 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)16 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)15 Collectors (java.util.stream.Collectors)14 BConversionOperatorSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)13 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)13 Set (java.util.Set)12 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)12