Search in sources :

Example 76 with BType

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

the class SymbolResolver method resolveTypeNode.

public BType resolveTypeNode(BLangType typeNode, SymbolEnv env, DiagnosticCode diagCode) {
    SymbolEnv prevEnv = this.env;
    DiagnosticCode preDiagCode = this.diagCode;
    this.env = env;
    this.diagCode = diagCode;
    typeNode.accept(this);
    this.env = prevEnv;
    this.diagCode = preDiagCode;
    // if it is not already a union type, JSON type, or any type
    if (typeNode.nullable && this.resultType.tag == TypeTags.UNION) {
        BUnionType unionType = (BUnionType) this.resultType;
        unionType.memberTypes.add(symTable.nullType);
        unionType.setNullable(true);
    } else if (typeNode.nullable && resultType.tag != TypeTags.JSON && resultType.tag != TypeTags.ANY) {
        Set<BType> memberTypes = new HashSet<BType>(2) {

            {
                add(resultType);
                add(symTable.nullType);
            }
        };
        this.resultType = new BUnionType(null, memberTypes, true);
    }
    typeNode.type = resultType;
    return resultType;
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) EnumSet(java.util.EnumSet) Set(java.util.Set) HashSet(java.util.HashSet) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) DiagnosticCode(org.ballerinalang.util.diagnostic.DiagnosticCode)

Example 77 with BType

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

the class SymbolResolver method resolveImplicitConversionOp.

public BSymbol resolveImplicitConversionOp(BType sourceType, BType targetType) {
    BSymbol symbol = resolveOperator(Names.CONVERSION_OP, Lists.of(sourceType, targetType));
    if (symbol == symTable.notFoundSymbol) {
        return symbol;
    }
    BConversionOperatorSymbol castSymbol = (BConversionOperatorSymbol) symbol;
    if (castSymbol.implicit) {
        return symbol;
    }
    return symTable.notFoundSymbol;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)

Example 78 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(BLangRecordLiteral recordLiteral) {
    BType actualType = symTable.errType;
    int expTypeTag = expTypes.get(0).tag;
    if (expTypeTag == TypeTags.NONE || expTypeTag == TypeTags.ANY) {
        // var a = {}
        // Change the expected type to map
        expTypes = Lists.of(symTable.mapType);
    }
    List<BType> matchedTypeList = getRecordCompatibleType(expTypes.get(0));
    if (matchedTypeList.isEmpty()) {
        dlog.error(recordLiteral.pos, DiagnosticCode.INVALID_LITERAL_FOR_TYPE, expTypes.get(0));
    } else if (matchedTypeList.size() > 1) {
        dlog.error(recordLiteral.pos, DiagnosticCode.AMBIGUOUS_TYPES, expTypes.get(0));
    } else {
        recordLiteral.keyValuePairs.forEach(keyValuePair -> checkRecLiteralKeyValue(keyValuePair, matchedTypeList.get(0)));
        actualType = matchedTypeList.get(0);
        // TODO Following check can be moved the code analyzer.
        if (expTypeTag == TypeTags.STRUCT) {
            validateStructInitalizer(recordLiteral.pos);
        }
    }
    resultTypes = types.checkTypes(recordLiteral, Lists.of(actualType), expTypes);
}
Also used : Arrays(java.util.Arrays) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) BLangXMLTextLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral) Map(java.util.Map) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr) Set(java.util.Set) SelectExpressionNode(org.ballerinalang.model.tree.clauses.SelectExpressionNode) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangLambdaFunction(org.wso2.ballerinalang.compiler.tree.expressions.BLangLambdaFunction) BLangSelectExpression(org.wso2.ballerinalang.compiler.tree.clauses.BLangSelectExpression) NamedArgNode(org.ballerinalang.model.tree.expressions.NamedArgNode) BLangXMLAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute) BLangNamedArgsExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangNamedArgsExpression) BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangXMLCommentLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLCommentLiteral) ArrayList(java.util.ArrayList) Flags(org.wso2.ballerinalang.util.Flags) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) TreeBuilder(org.ballerinalang.model.TreeBuilder) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangXMLProcInsLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLProcInsLiteral) BLangForever(org.wso2.ballerinalang.compiler.tree.statements.BLangForever) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangOrderBy(org.wso2.ballerinalang.compiler.tree.clauses.BLangOrderBy) BLangSelectClause(org.wso2.ballerinalang.compiler.tree.clauses.BLangSelectClause) SymTag(org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangTableQuery(org.wso2.ballerinalang.compiler.tree.clauses.BLangTableQuery) BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) BLangBracedOrTupleExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) BLangXMLAttributeAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttributeAccess) Lists(org.wso2.ballerinalang.util.Lists) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) OperatorKind(org.ballerinalang.model.tree.OperatorKind) BLangHaving(org.wso2.ballerinalang.compiler.tree.clauses.BLangHaving) BOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol) Names(org.wso2.ballerinalang.compiler.util.Names) BLangDiagnosticLog(org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLog) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) MultiReturnExpr(org.wso2.ballerinalang.compiler.tree.expressions.MultiReturnExpr) BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangRecordKey(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) Collectors(java.util.stream.Collectors) BLangFieldBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess) List(java.util.List) NodeKind(org.ballerinalang.model.tree.NodeKind) BLangTableQueryExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangTableQueryExpression) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) BLangStreamingInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangStreamingInput) BLangNodeVisitor(org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor) BLangTypeofExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeofExpr) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) HashSet(java.util.HashSet) IterableKind(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind) BLangStringTemplateLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangStringTemplateLiteral) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BLangTernaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr) BFutureType(org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType) BLangIntRangeExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangIntRangeExpression) BLangRecordKeyValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue) XMLConstants(javax.xml.XMLConstants) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BTransformerSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTransformerSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) RecordKind(org.wso2.ballerinalang.compiler.semantics.analyzer.Types.RecordKind) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangGroupBy(org.wso2.ballerinalang.compiler.tree.clauses.BLangGroupBy) Type(org.ballerinalang.model.types.Type) Name(org.wso2.ballerinalang.compiler.util.Name) BLangJoinStreamingInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinStreamingInput) BLangAwaitExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangAwaitExpr) BLangRestArgsExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangRestArgsExpression) BLangXMLElementLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral) FieldType(org.wso2.ballerinalang.compiler.util.FieldType) DiagnosticCode(org.ballerinalang.util.diagnostic.DiagnosticCode) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) Collections(java.util.Collections) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 79 with BType

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

the class TypeChecker method visit.

@Override
public void visit(BLangTableQueryExpression tableQueryExpression) {
    BType actualType = symTable.errType;
    int expTypeTag = expTypes.get(0).tag;
    if (expTypeTag == TypeTags.TABLE) {
        actualType = expTypes.get(0);
    } else if (expTypeTag != TypeTags.ERROR) {
        dlog.error(tableQueryExpression.pos, DiagnosticCode.INCOMPATIBLE_TYPES_CONVERSION, expTypes.get(0));
    }
    BLangTableQuery tableQuery = (BLangTableQuery) tableQueryExpression.getTableQuery();
    tableQuery.accept(this);
    resultTypes = types.checkTypes(tableQueryExpression, Lists.of(actualType), expTypes);
}
Also used : BLangTableQuery(org.wso2.ballerinalang.compiler.tree.clauses.BLangTableQuery) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 80 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(BLangUnaryExpr unaryExpr) {
    BType exprType = null;
    BType actualType = symTable.errType;
    if (OperatorKind.TYPEOF.equals(unaryExpr.operator)) {
        // Handle typeof operator separately
        if (unaryExpr.expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
            BLangSimpleVarRef varRef = (BLangSimpleVarRef) unaryExpr.expr;
            Name varRefName = names.fromIdNode((varRef).variableName);
            Name pkgAlias = names.fromIdNode((varRef).pkgAlias);
            // Resolve symbol for BLangSimpleVarRef
            BSymbol varRefSybmol = symResolver.lookupSymbolInPackage(unaryExpr.pos, env, pkgAlias, varRefName, SymTag.VARIABLE);
            if (varRefSybmol == symTable.notFoundSymbol) {
                // Resolve symbol for User Defined Type ( converted from BLangSimpleVarRef )
                BLangTypeofExpr typeAccessExpr = getTypeAccessExpression(varRef);
                unaryExpr.expr = typeAccessExpr;
                actualType = typeAccessExpr.type;
                resultTypes = types.checkTypes(unaryExpr, Lists.of(actualType), expTypes);
                return;
            } else {
                // Check type if resolved as BLangSimpleVarRef
                exprType = checkExpr(unaryExpr.expr, env).get(0);
            }
        } else {
            // Check type if resolved as non BLangSimpleVarRef Expression
            exprType = checkExpr(unaryExpr.expr, env).get(0);
        }
        if (exprType != symTable.errType) {
            unaryExpr.opSymbol = Symbols.createTypeofOperatorSymbol(exprType, types, symTable, names);
            actualType = unaryExpr.opSymbol.type.getReturnTypes().get(0);
        }
    } else {
        exprType = checkExpr(unaryExpr.expr, env).get(0);
        if (exprType != symTable.errType) {
            BSymbol symbol = symResolver.resolveUnaryOperator(unaryExpr.pos, unaryExpr.operator, exprType);
            if (symbol == symTable.notFoundSymbol) {
                dlog.error(unaryExpr.pos, DiagnosticCode.UNARY_OP_INCOMPATIBLE_TYPES, unaryExpr.operator, exprType);
            } else {
                unaryExpr.opSymbol = (BOperatorSymbol) symbol;
                actualType = symbol.type.getReturnTypes().get(0);
            }
        }
    }
    resultTypes = types.checkTypes(unaryExpr, Lists.of(actualType), expTypes);
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangTypeofExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeofExpr) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

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