Search in sources :

Example 91 with BType

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

the class SemanticAnalyzer method handleForeachVariables.

// Private methods
private void handleForeachVariables(BLangForeach foreachStmt, List<BType> varTypes, SymbolEnv env) {
    for (int i = 0; i < foreachStmt.varRefs.size(); i++) {
        BLangExpression varRef = foreachStmt.varRefs.get(i);
        // foreach variables supports only simpleVarRef expressions only.
        if (varRef.getKind() != NodeKind.SIMPLE_VARIABLE_REF) {
            dlog.error(varRef.pos, DiagnosticCode.INVALID_VARIABLE_ASSIGNMENT, varRef);
            continue;
        }
        BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) varRef;
        simpleVarRef.lhsVar = true;
        Name varName = names.fromIdNode(simpleVarRef.variableName);
        if (varName == Names.IGNORE) {
            simpleVarRef.type = this.symTable.noType;
            typeChecker.checkExpr(simpleVarRef, env);
            continue;
        }
        // Check variable symbol for existence.
        BSymbol symbol = symResolver.lookupSymbol(env, varName, SymTag.VARIABLE);
        if (symbol == symTable.notFoundSymbol) {
            symbolEnter.defineVarSymbol(simpleVarRef.pos, Collections.emptySet(), varTypes.get(i), varName, env);
            typeChecker.checkExpr(simpleVarRef, env);
        } else {
            dlog.error(simpleVarRef.pos, DiagnosticCode.REDECLARED_SYMBOL, varName);
        }
    }
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 92 with BType

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

the class TypeChecker method getTypeOfExprInFieldAccess.

private BType getTypeOfExprInFieldAccess(BLangExpression expr) {
    // First check whether variable expression is of type enum.
    if (expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
        BLangSimpleVarRef varRef = (BLangSimpleVarRef) expr;
        BSymbol symbol = symResolver.lookupSymbolInPackage(varRef.pos, env, names.fromIdNode(varRef.pkgAlias), names.fromIdNode(varRef.variableName), SymTag.ENUM);
        if (symbol != symTable.notFoundSymbol) {
            expr.type = symbol.type;
            return symbol.type;
        }
    }
    checkExpr(expr, this.env, Lists.of(symTable.noType));
    return expr.type;
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)

Example 93 with BType

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

the class TypeChecker method checkStructLiteralKeyExpr.

private BType checkStructLiteralKeyExpr(BLangRecordKey key, BType recordType, RecordKind recKind) {
    Name fieldName;
    BLangExpression keyExpr = key.expr;
    if (keyExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
        BLangSimpleVarRef varRef = (BLangSimpleVarRef) keyExpr;
        fieldName = names.fromIdNode(varRef.variableName);
    } else {
        // keys of the struct literal can only be a varRef (identifier)
        dlog.error(keyExpr.pos, DiagnosticCode.INVALID_STRUCT_LITERAL_KEY);
        return symTable.errType;
    }
    // Check weather the struct field exists
    BSymbol fieldSymbol = symResolver.resolveStructField(keyExpr.pos, this.env, fieldName, recordType.tsymbol);
    if (fieldSymbol == symTable.notFoundSymbol) {
        dlog.error(keyExpr.pos, DiagnosticCode.UNDEFINED_STRUCT_FIELD, fieldName, recordType.tsymbol);
        return symTable.errType;
    }
    // Setting the struct field symbol for future use in Desugar and code generator.
    key.fieldSymbol = (BVarSymbol) fieldSymbol;
    return fieldSymbol.type;
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 94 with BType

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

the class TypeChecker method visit.

// Expressions
public void visit(BLangLiteral literalExpr) {
    BType literalType = symTable.getTypeFromTag(literalExpr.typeTag);
    resultTypes = types.checkTypes(literalExpr, Lists.of(literalType), expTypes);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 95 with BType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BType 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)

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