Search in sources :

Example 1 with BLangXMLQName

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

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

the class TaintAnalyzer method visit.

public void visit(BLangXMLElementLiteral xmlElementLiteral) {
    SymbolEnv xmlElementEnv = SymbolEnv.getXMLElementEnv(xmlElementLiteral, env);
    // Visit in-line namespace declarations
    boolean inLineNamespaceTainted = false;
    for (BLangXMLAttribute attribute : xmlElementLiteral.attributes) {
        if (attribute.name.getKind() == NodeKind.XML_QNAME && ((BLangXMLQName) attribute.name).prefix.value.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
            attribute.accept(this);
            attribute.symbol.tainted = getObservedTaintedStatus();
            if (attribute.symbol.tainted) {
                inLineNamespaceTainted = true;
            }
        }
    }
    // Visit attributes.
    boolean attributesTainted = false;
    for (BLangXMLAttribute attribute : xmlElementLiteral.attributes) {
        if (attribute.name.getKind() == NodeKind.XML_QNAME && !((BLangXMLQName) attribute.name).prefix.value.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
            attribute.accept(this);
            attribute.symbol.tainted = getObservedTaintedStatus();
            if (attribute.symbol.tainted) {
                attributesTainted = true;
            }
        }
    }
    // Visit the tag names
    xmlElementLiteral.startTagName.accept(this);
    boolean startTagTaintedStatus = getObservedTaintedStatus();
    boolean endTagTaintedStatus = false;
    if (xmlElementLiteral.endTagName != null) {
        xmlElementLiteral.endTagName.accept(this);
        endTagTaintedStatus = getObservedTaintedStatus();
    }
    boolean tagNamesTainted = startTagTaintedStatus || endTagTaintedStatus;
    // Visit the children
    boolean childrenTainted = false;
    for (BLangExpression expr : xmlElementLiteral.children) {
        expr.accept(this);
        if (getObservedTaintedStatus()) {
            childrenTainted = true;
        }
    }
    setTaintedStatusList(inLineNamespaceTainted || attributesTainted || tagNamesTainted || childrenTainted);
}
Also used : BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangXMLAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 3 with BLangXMLQName

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

the class TypeChecker method visit.

public void visit(BLangXMLElementLiteral bLangXMLElementLiteral) {
    SymbolEnv xmlElementEnv = SymbolEnv.getXMLElementEnv(bLangXMLElementLiteral, env);
    // Visit in-line namespace declarations
    bLangXMLElementLiteral.attributes.forEach(attribute -> {
        if (attribute.name.getKind() == NodeKind.XML_QNAME && ((BLangXMLQName) attribute.name).prefix.value.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
            checkExpr((BLangExpression) attribute, xmlElementEnv, Lists.of(symTable.noType));
        }
    });
    // Visit attributes.
    bLangXMLElementLiteral.attributes.forEach(attribute -> {
        if (attribute.name.getKind() != NodeKind.XML_QNAME || !((BLangXMLQName) attribute.name).prefix.value.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
            checkExpr((BLangExpression) attribute, xmlElementEnv, Lists.of(symTable.noType));
        }
    });
    Map<Name, BXMLNSSymbol> namespaces = symResolver.resolveAllNamespaces(xmlElementEnv);
    Name defaultNs = names.fromString(XMLConstants.DEFAULT_NS_PREFIX);
    if (namespaces.containsKey(defaultNs)) {
        bLangXMLElementLiteral.defaultNsSymbol = namespaces.remove(defaultNs);
    }
    bLangXMLElementLiteral.namespacesInScope.putAll(namespaces);
    // Visit the tag names
    validateTags(bLangXMLElementLiteral, xmlElementEnv);
    // Visit the children
    bLangXMLElementLiteral.modifiedChildren = concatSimilarKindXMLNodes(bLangXMLElementLiteral.children, xmlElementEnv);
    resultTypes = Lists.of(types.checkType(bLangXMLElementLiteral, symTable.xmlType, expTypes.get(0)));
}
Also used : BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 4 with BLangXMLQName

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

the class TypeChecker method visit.

public void visit(BLangXMLAttributeAccess xmlAttributeAccessExpr) {
    BType actualType = symTable.errType;
    // First analyze the variable reference expression.
    checkExpr(xmlAttributeAccessExpr.expr, env, Lists.of(symTable.xmlType));
    // Then analyze the index expression.
    BLangExpression indexExpr = xmlAttributeAccessExpr.indexExpr;
    if (indexExpr == null) {
        if (xmlAttributeAccessExpr.lhsVar) {
            dlog.error(xmlAttributeAccessExpr.pos, DiagnosticCode.XML_ATTRIBUTE_MAP_UPDATE_NOT_ALLOWED);
        } else {
            actualType = symTable.xmlAttributesType;
        }
        resultTypes = types.checkTypes(xmlAttributeAccessExpr, Lists.of(actualType), expTypes);
        return;
    }
    checkExpr(indexExpr, env, Lists.of(symTable.stringType)).get(0);
    if (indexExpr.getKind() == NodeKind.XML_QNAME) {
        ((BLangXMLQName) indexExpr).isUsedInXML = true;
    }
    if (indexExpr.type.tag == TypeTags.STRING) {
        actualType = symTable.stringType;
    }
    xmlAttributeAccessExpr.namespaces.putAll(symResolver.resolveAllNamespaces(env));
    resultTypes = types.checkTypes(xmlAttributeAccessExpr, Lists.of(actualType), expTypes);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 5 with BLangXMLQName

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

the class Desugar method visit.

@Override
public void visit(BLangSimpleVarRef varRefExpr) {
    BLangSimpleVarRef genVarRefExpr = varRefExpr;
    // XML qualified name reference. e.g: ns0:foo
    if (varRefExpr.pkgSymbol != null && varRefExpr.pkgSymbol.tag == SymTag.XMLNS) {
        BLangXMLQName qnameExpr = new BLangXMLQName(varRefExpr.variableName);
        qnameExpr.nsSymbol = (BXMLNSSymbol) varRefExpr.pkgSymbol;
        qnameExpr.localname = varRefExpr.variableName;
        qnameExpr.prefix = varRefExpr.pkgAlias;
        qnameExpr.namespaceURI = qnameExpr.nsSymbol.namespaceURI;
        qnameExpr.isUsedInXML = false;
        qnameExpr.pos = varRefExpr.pos;
        qnameExpr.type = symTable.stringType;
        result = qnameExpr;
        return;
    }
    BSymbol ownerSymbol = varRefExpr.symbol.owner;
    if ((varRefExpr.symbol.tag & SymTag.FUNCTION) == SymTag.FUNCTION && varRefExpr.symbol.type.tag == TypeTags.INVOKABLE) {
        genVarRefExpr = new BLangFunctionVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.INVOKABLE) == SymTag.INVOKABLE) {
        // Local variable in a function/resource/action/worker
        genVarRefExpr = new BLangLocalVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.CONNECTOR) == SymTag.CONNECTOR) {
        // Field variable in a receiver
        genVarRefExpr = new BLangFieldVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.STRUCT) == SymTag.STRUCT) {
        genVarRefExpr = new BLangFieldVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.PACKAGE) == SymTag.PACKAGE || (ownerSymbol.tag & SymTag.SERVICE) == SymTag.SERVICE) {
        // Package variable | service variable
        // We consider both of them as package level variables
        genVarRefExpr = new BLangPackageVarRef(varRefExpr.symbol);
        // Only locking service level and package level variables
        if (!enclLocks.isEmpty()) {
            enclLocks.peek().addLockVariable(varRefExpr.symbol);
        }
    }
    genVarRefExpr.type = varRefExpr.type;
    result = genVarRefExpr;
}
Also used : BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangPackageVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangPackageVarRef) BLangFunctionVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFunctionVarRef) BLangLocalVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef) BLangFieldVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFieldVarRef)

Aggregations

BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)6 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)4 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)3 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)2 BXMLNSSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol)2 BLangXMLAttribute (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute)2 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)2 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)2 BXMLAttributeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLAttributeSymbol)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangLocalXMLNS (org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangLocalXMLNS)1 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)1 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)1 BLangFieldVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFieldVarRef)1 BLangFunctionVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFunctionVarRef)1 BLangLocalVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef)1 BLangPackageVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangPackageVarRef)1 Name (org.wso2.ballerinalang.compiler.util.Name)1