Search in sources :

Example 6 with BLangSimpleVarRef

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

the class SemanticAnalyzer method checkConstantAssignment.

private void checkConstantAssignment(BLangExpression varRef) {
    if (varRef.type == symTable.errType) {
        return;
    }
    if (varRef.getKind() != NodeKind.SIMPLE_VARIABLE_REF) {
        return;
    }
    BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) varRef;
    if (simpleVarRef.pkgSymbol != null && simpleVarRef.pkgSymbol.tag == SymTag.XMLNS) {
        dlog.error(varRef.pos, DiagnosticCode.XML_QNAME_UPDATE_NOT_ALLOWED);
        return;
    }
    Name varName = names.fromIdNode(simpleVarRef.variableName);
    if (!Names.IGNORE.equals(varName) && simpleVarRef.symbol.flags == Flags.CONST && env.enclInvokable != env.enclPkg.initFunction) {
        dlog.error(varRef.pos, DiagnosticCode.CANNOT_ASSIGN_VALUE_CONSTANT, varRef);
    }
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 7 with BLangSimpleVarRef

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

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

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

the class ASTBuilderUtil method generateArgExprs.

static List<BLangExpression> generateArgExprs(DiagnosticPos pos, List<BLangVariable> args, List<BVarSymbol> formalParams, SymbolResolver symResolver) {
    List<BLangExpression> argsExpr = new ArrayList<>();
    final List<BLangSimpleVarRef> variableRefList = createVariableRefList(pos, args);
    for (int i = 0; i < variableRefList.size(); i++) {
        BLangSimpleVarRef varRef = variableRefList.get(i);
        BType target = formalParams.get(i).type;
        BType source = varRef.symbol.type;
        if (source != target) {
            argsExpr.add(generateConversionExpr(varRef, target, symResolver));
            continue;
        }
        argsExpr.add(varRef);
    }
    return argsExpr;
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 10 with BLangSimpleVarRef

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

the class Desugar method getJoinTableVarRef.

private BLangSimpleVarRef getJoinTableVarRef(BLangTableQueryExpression tableQueryExpression) {
    JoinStreamingInput joinStreamingInput = tableQueryExpression.getTableQuery().getJoinStreamingInput();
    BLangSimpleVarRef joinTable = null;
    if (joinStreamingInput != null) {
        joinTable = (BLangSimpleVarRef) joinStreamingInput.getStreamingInput().getStreamReference();
        joinTable = rewrite(joinTable, env);
    }
    return joinTable;
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) JoinStreamingInput(org.ballerinalang.model.tree.clauses.JoinStreamingInput)

Aggregations

BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)36 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)11 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)9 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)8 Name (org.wso2.ballerinalang.compiler.util.Name)8 ArrayList (java.util.ArrayList)7 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)6 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)5 BLangAnnotationAttachmentPoint (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint)4 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)4 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)3 BLangNameReference (org.wso2.ballerinalang.compiler.tree.BLangNameReference)3 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)3 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)3 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)3 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)2 BJSONType (org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType)2 BLangFieldBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess)2 BLangJSONLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangJSONLiteral)2 BLangMapLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangMapLiteral)2