Search in sources :

Example 6 with BLangLiteral

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

the class SymbolEnter method visit.

public void visit(BLangXMLAttribute bLangXMLAttribute) {
    if (!(bLangXMLAttribute.name.getKind() == NodeKind.XML_QNAME)) {
        return;
    }
    BLangXMLQName qname = (BLangXMLQName) bLangXMLAttribute.name;
    // If no duplicates, then define this attribute symbol.
    if (!bLangXMLAttribute.isNamespaceDeclr) {
        BXMLAttributeSymbol attrSymbol = new BXMLAttributeSymbol(qname.localname.value, qname.namespaceURI, env.enclPkg.symbol.pkgID, env.scope.owner);
        if (symResolver.checkForUniqueMemberSymbol(bLangXMLAttribute.pos, env, attrSymbol)) {
            env.scope.define(attrSymbol.name, attrSymbol);
            bLangXMLAttribute.symbol = attrSymbol;
        }
        return;
    }
    List<BLangExpression> exprs = bLangXMLAttribute.value.textFragments;
    String nsURI = null;
    // TODO: find a better way to get the statically defined URI.
    if (exprs.size() == 1 && exprs.get(0).getKind() == NodeKind.LITERAL) {
        nsURI = (String) ((BLangLiteral) exprs.get(0)).value;
    }
    String symbolName = qname.localname.value;
    if (symbolName.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
        symbolName = XMLConstants.DEFAULT_NS_PREFIX;
    }
    BXMLNSSymbol xmlnsSymbol = new BXMLNSSymbol(names.fromString(symbolName), nsURI, env.enclPkg.symbol.pkgID, env.scope.owner);
    if (symResolver.checkForUniqueMemberSymbol(bLangXMLAttribute.pos, env, xmlnsSymbol)) {
        env.scope.define(xmlnsSymbol.name, xmlnsSymbol);
        bLangXMLAttribute.symbol = xmlnsSymbol;
    }
}
Also used : BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) BXMLAttributeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLAttributeSymbol) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 7 with BLangLiteral

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

the class SymbolEnter method visit.

@Override
public void visit(BLangXMLNS xmlnsNode) {
    String nsURI = (String) ((BLangLiteral) xmlnsNode.namespaceURI).value;
    if (xmlnsNode.prefix.value != null && nsURI.isEmpty()) {
        dlog.error(xmlnsNode.pos, DiagnosticCode.INVALID_NAMESPACE_DECLARATION, xmlnsNode.prefix);
    }
    // set the prefix of the default namespace
    if (xmlnsNode.prefix.value == null) {
        xmlnsNode.prefix.value = XMLConstants.DEFAULT_NS_PREFIX;
    }
    BXMLNSSymbol xmlnsSymbol = Symbols.createXMLNSSymbol(names.fromIdNode(xmlnsNode.prefix), nsURI, env.enclPkg.symbol.pkgID, env.scope.owner);
    xmlnsNode.symbol = xmlnsSymbol;
    // First check for package-imports with the same alias.
    // Here we do not check for owner equality, since package import is always at the package
    // level, but the namespace declaration can be at any level.
    BSymbol foundSym = symResolver.lookupSymbol(env, xmlnsSymbol.name, SymTag.PACKAGE);
    if (foundSym != symTable.notFoundSymbol) {
        dlog.error(xmlnsNode.pos, DiagnosticCode.REDECLARED_SYMBOL, xmlnsSymbol.name);
        return;
    }
    // Define it in the enclosing scope. Here we check for the owner equality,
    // to support overriding of namespace declarations defined at package level.
    defineSymbol(xmlnsNode.pos, xmlnsSymbol);
}
Also used : BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)

Example 8 with BLangLiteral

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

the class SemanticAnalyzer method checkRetryStmtValidity.

private void checkRetryStmtValidity(BLangExpression retryCountExpr) {
    boolean error = true;
    NodeKind retryKind = retryCountExpr.getKind();
    if (retryKind == NodeKind.LITERAL) {
        if (retryCountExpr.type.tag == TypeTags.INT) {
            int retryCount = Integer.parseInt(((BLangLiteral) retryCountExpr).getValue().toString());
            if (retryCount >= 0) {
                error = false;
            }
        }
    } else if (retryKind == NodeKind.SIMPLE_VARIABLE_REF) {
        if (((BLangSimpleVarRef) retryCountExpr).symbol.flags == Flags.CONST) {
            if (((BLangSimpleVarRef) retryCountExpr).symbol.type.tag == TypeTags.INT) {
                error = false;
            }
        }
    }
    if (error) {
        this.dlog.error(retryCountExpr.pos, DiagnosticCode.INVALID_RETRY_COUNT);
    }
}
Also used : NodeKind(org.ballerinalang.model.tree.NodeKind) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 9 with BLangLiteral

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

the class TypeChecker method visit.

public void visit(BLangIndexBasedAccess indexBasedAccessExpr) {
    BType actualType = symTable.errType;
    // First analyze the variable reference expression.
    checkExpr(indexBasedAccessExpr.expr, this.env, Lists.of(symTable.noType));
    BType indexExprType;
    BType varRefType = indexBasedAccessExpr.expr.type;
    BLangExpression indexExpr = indexBasedAccessExpr.indexExpr;
    switch(varRefType.tag) {
        case TypeTags.STRUCT:
            indexExprType = checkIndexExprForStructFieldAccess(indexExpr);
            if (indexExprType.tag == TypeTags.STRING) {
                String fieldName = (String) ((BLangLiteral) indexExpr).value;
                actualType = checkStructFieldAccess(indexBasedAccessExpr, names.fromString(fieldName), varRefType);
            }
            break;
        case TypeTags.MAP:
            indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.stringType)).get(0);
            if (indexExprType.tag == TypeTags.STRING) {
                actualType = ((BMapType) varRefType).getConstraint();
            }
            break;
        case TypeTags.JSON:
            BType constraintType = ((BJSONType) varRefType).constraint;
            if (constraintType.tag == TypeTags.STRUCT) {
                indexExprType = checkIndexExprForStructFieldAccess(indexExpr);
                if (indexExprType.tag != TypeTags.STRING) {
                    break;
                }
                String fieldName = (String) ((BLangLiteral) indexExpr).value;
                BType fieldType = checkStructFieldAccess(indexBasedAccessExpr, names.fromString(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;
                }
            } else {
                indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.noType)).get(0);
                if (indexExprType.tag != TypeTags.STRING && indexExprType.tag != TypeTags.INT) {
                    dlog.error(indexExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES, symTable.stringType, indexExprType);
                    break;
                }
            }
            actualType = symTable.jsonType;
            break;
        case TypeTags.ARRAY:
            indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.intType)).get(0);
            if (indexExprType.tag == TypeTags.INT) {
                actualType = ((BArrayType) varRefType).getElementType();
            }
            break;
        case TypeTags.XML:
            if (indexBasedAccessExpr.lhsVar) {
                dlog.error(indexBasedAccessExpr.pos, DiagnosticCode.CANNOT_UPDATE_XML_SEQUENCE);
                break;
            }
            checkExpr(indexExpr, this.env).get(0);
            actualType = symTable.xmlType;
            break;
        case TypeTags.ERROR:
            // Do nothing
            break;
        default:
            dlog.error(indexBasedAccessExpr.pos, DiagnosticCode.OPERATION_DOES_NOT_SUPPORT_INDEXING, indexBasedAccessExpr.expr.type);
    }
    resultTypes = types.checkTypes(indexBasedAccessExpr, Lists.of(actualType), this.expTypes);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 10 with BLangLiteral

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

the class CodeGenerator method visit.

@Override
public void visit(BLangJSONArrayLiteral arrayLiteral) {
    arrayLiteral.regIndex = calcAndGetExprRegIndex(arrayLiteral);
    List<BLangExpression> argExprs = arrayLiteral.exprs;
    BLangLiteral arraySizeLiteral = new BLangLiteral();
    arraySizeLiteral.pos = arrayLiteral.pos;
    arraySizeLiteral.value = (long) argExprs.size();
    arraySizeLiteral.type = symTable.intType;
    genNode(arraySizeLiteral, this.env);
    emit(InstructionCodes.JSONNEWARRAY, arrayLiteral.regIndex, arraySizeLiteral.regIndex);
    for (int i = 0; i < argExprs.size(); i++) {
        BLangExpression argExpr = argExprs.get(i);
        genNode(argExpr, this.env);
        BLangLiteral indexLiteral = new BLangLiteral();
        indexLiteral.pos = arrayLiteral.pos;
        indexLiteral.value = (long) i;
        indexLiteral.type = symTable.intType;
        genNode(indexLiteral, this.env);
        emit(InstructionCodes.JSONASTORE, arrayLiteral.regIndex, indexLiteral.regIndex, argExpr.regIndex);
    }
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Aggregations

BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)23 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)15 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)11 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)8 HashMap (java.util.HashMap)4 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)4 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)4 ArrayList (java.util.ArrayList)3 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)3 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)3 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)3 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)3 Path (java.nio.file.Path)2 NodeKind (org.ballerinalang.model.tree.NodeKind)2 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)2 BXMLNSSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol)2 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)2 DefaultValue (org.wso2.ballerinalang.programfile.DefaultValue)2 ParamDefaultValueAttributeInfo (org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo)2 FloatCPEntry (org.wso2.ballerinalang.programfile.cpentries.FloatCPEntry)2