Search in sources :

Example 6 with BVarSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol in project ballerina by ballerina-lang.

the class TypeChecker method checkRestArgs.

private void checkRestArgs(List<BLangExpression> restArgExprs, BLangExpression vararg, BVarSymbol restParam) {
    if (vararg != null && !restArgExprs.isEmpty()) {
        dlog.error(vararg.pos, DiagnosticCode.INVALID_REST_ARGS);
        return;
    }
    if (vararg != null) {
        checkExpr(vararg, this.env, Lists.of(restParam.type));
        restArgExprs.add(vararg);
        return;
    }
    for (BLangExpression arg : restArgExprs) {
        checkExpr(arg, this.env, Lists.of(((BArrayType) restParam.type).eType));
    }
}
Also used : BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 7 with BVarSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol in project ballerina by ballerina-lang.

the class TypeChecker method checkNamedArgs.

private void checkNamedArgs(List<BLangExpression> namedArgExprs, List<BVarSymbol> defaultableParams) {
    for (BLangExpression expr : namedArgExprs) {
        BLangIdentifier argName = ((NamedArgNode) expr).getName();
        BVarSymbol varSym = defaultableParams.stream().filter(param -> param.getName().value.equals(argName.value)).findAny().orElse(null);
        if (varSym == null) {
            dlog.error(expr.pos, DiagnosticCode.UNDEFINED_PARAMETER, argName);
            break;
        }
        checkExpr(expr, this.env, Lists.of(varSym.type));
    }
}
Also used : BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) NamedArgNode(org.ballerinalang.model.tree.expressions.NamedArgNode)

Example 8 with BVarSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol in project ballerina by ballerina-lang.

the class TypeChecker method visit.

public void visit(BLangFieldBasedAccess fieldAccessExpr) {
    // First analyze the variable reference expression.
    BType actualType = symTable.errType;
    BType varRefType = getTypeOfExprInFieldAccess(fieldAccessExpr.expr);
    if (fieldAccessExpr.fieldType == FieldType.ALL && varRefType.tag != TypeTags.XML) {
        dlog.error(fieldAccessExpr.pos, DiagnosticCode.CANNOT_GET_ALL_FIELDS, varRefType);
    }
    Name fieldName = names.fromIdNode(fieldAccessExpr.field);
    switch(varRefType.tag) {
        case TypeTags.STRUCT:
            actualType = checkStructFieldAccess(fieldAccessExpr, fieldName, varRefType);
            break;
        case TypeTags.MAP:
            actualType = ((BMapType) varRefType).getConstraint();
            break;
        case TypeTags.JSON:
            BType constraintType = ((BJSONType) varRefType).constraint;
            if (constraintType.tag == TypeTags.STRUCT) {
                BType fieldType = checkStructFieldAccess(fieldAccessExpr, 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;
                }
            }
            actualType = symTable.jsonType;
            break;
        case TypeTags.ENUM:
            // Enumerator access expressions only allow enum type name as the first part e.g state.INSTALLED,
            BEnumType enumType = (BEnumType) varRefType;
            if (fieldAccessExpr.expr.getKind() != NodeKind.SIMPLE_VARIABLE_REF || !((BLangSimpleVarRef) fieldAccessExpr.expr).variableName.value.equals(enumType.tsymbol.name.value)) {
                dlog.error(fieldAccessExpr.pos, DiagnosticCode.INVALID_ENUM_EXPR, enumType.tsymbol.name.value);
                break;
            }
            BSymbol symbol = symResolver.lookupMemberSymbol(fieldAccessExpr.pos, enumType.tsymbol.scope, this.env, fieldName, SymTag.VARIABLE);
            if (symbol == symTable.notFoundSymbol) {
                dlog.error(fieldAccessExpr.pos, DiagnosticCode.UNDEFINED_SYMBOL, fieldName.value);
                break;
            }
            fieldAccessExpr.symbol = (BVarSymbol) symbol;
            actualType = fieldAccessExpr.expr.type;
            break;
        case TypeTags.XML:
            if (fieldAccessExpr.lhsVar) {
                dlog.error(fieldAccessExpr.pos, DiagnosticCode.CANNOT_UPDATE_XML_SEQUENCE);
                break;
            }
            actualType = symTable.xmlType;
            break;
        case TypeTags.ERROR:
            // Do nothing
            break;
        default:
            dlog.error(fieldAccessExpr.pos, DiagnosticCode.OPERATION_DOES_NOT_SUPPORT_FIELD_ACCESS, varRefType);
    }
    resultTypes = types.checkTypes(fieldAccessExpr, Lists.of(actualType), this.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) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType)

Example 9 with BVarSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol in project ballerina by ballerina-lang.

the class TypeChecker method visit.

public void visit(BLangSimpleVarRef varRefExpr) {
    // Set error type as the actual type.
    BType actualType = symTable.errType;
    Name varName = names.fromIdNode(varRefExpr.variableName);
    if (varName == Names.IGNORE) {
        if (varRefExpr.lhsVar) {
            varRefExpr.type = this.symTable.noType;
        } else {
            varRefExpr.type = this.symTable.errType;
            dlog.error(varRefExpr.pos, DiagnosticCode.UNDERSCORE_NOT_ALLOWED);
        }
        varRefExpr.symbol = new BVarSymbol(0, varName, env.enclPkg.symbol.pkgID, actualType, env.scope.owner);
        resultTypes = Lists.of(varRefExpr.type);
        return;
    }
    varRefExpr.pkgSymbol = symResolver.resolveImportSymbol(varRefExpr.pos, env, names.fromIdNode(varRefExpr.pkgAlias));
    if (varRefExpr.pkgSymbol.tag == SymTag.XMLNS) {
        actualType = symTable.stringType;
    } else if (varRefExpr.pkgSymbol != symTable.notFoundSymbol) {
        BSymbol symbol = symResolver.lookupSymbolInPackage(varRefExpr.pos, env, names.fromIdNode(varRefExpr.pkgAlias), varName, SymTag.VARIABLE_NAME);
        if ((symbol.tag & SymTag.VARIABLE) == SymTag.VARIABLE) {
            BVarSymbol varSym = (BVarSymbol) symbol;
            checkSefReferences(varRefExpr.pos, env, varSym);
            varRefExpr.symbol = varSym;
            actualType = varSym.type;
        } else {
            dlog.error(varRefExpr.pos, DiagnosticCode.UNDEFINED_SYMBOL, varName.toString());
        }
    }
    // Check type compatibility
    resultTypes = types.checkTypes(varRefExpr, Lists.of(actualType), expTypes);
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 10 with BVarSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol in project ballerina by ballerina-lang.

the class TypeChecker method checkStructFieldAccess.

private BType checkStructFieldAccess(BLangVariableReference varReferExpr, Name fieldName, BType structType) {
    BSymbol fieldSymbol = symResolver.resolveStructField(varReferExpr.pos, this.env, fieldName, structType.tsymbol);
    if (fieldSymbol == symTable.notFoundSymbol) {
        dlog.error(varReferExpr.pos, DiagnosticCode.UNDEFINED_STRUCT_FIELD, fieldName, structType.tsymbol);
        return symTable.errType;
    }
    // Setting the field symbol. This is used during the code generation phase
    varReferExpr.symbol = (BVarSymbol) fieldSymbol;
    return fieldSymbol.type;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)

Aggregations

BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)29 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)16 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)15 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)13 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)12 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)12 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)11 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)10 Name (org.wso2.ballerinalang.compiler.util.Name)10 ArrayList (java.util.ArrayList)9 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)9 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)8 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)8 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)8 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)7 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)7 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)7 BLangIdentifier (org.wso2.ballerinalang.compiler.tree.BLangIdentifier)6 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)6 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)6