Search in sources :

Example 1 with BXMLNSSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol 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 BXMLNSSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol 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 3 with BXMLNSSymbol

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

the class SymbolResolver method addNamespacesInScope.

private void addNamespacesInScope(Map<Name, BXMLNSSymbol> namespaces, SymbolEnv env) {
    if (env == null) {
        return;
    }
    env.scope.entries.forEach((name, scopeEntry) -> {
        if (scopeEntry.symbol.kind == SymbolKind.XMLNS) {
            BXMLNSSymbol nsSymbol = (BXMLNSSymbol) scopeEntry.symbol;
            // Skip if the namespace is already added, by a child scope. That means it has been overridden.
            if (!namespaces.containsKey(name)) {
                namespaces.put(name, nsSymbol);
            }
        }
    });
    addNamespacesInScope(namespaces, env.enclEnv);
}
Also used : BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol)

Example 4 with BXMLNSSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol 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 5 with BXMLNSSymbol

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

the class CodeGenerator method getNamespaceURIIndex.

private RegIndex getNamespaceURIIndex(BXMLNSSymbol namespaceSymbol, SymbolEnv env) {
    if (namespaceSymbol == null && env.node.getKind() == NodeKind.XML_ATTRIBUTE) {
        return createStringLiteral(XMLConstants.NULL_NS_URI, null, env);
    }
    if (namespaceSymbol == null) {
        return createStringLiteral(null, null, env);
    }
    // Otherwise get the URI index in the global var registry.
    if ((namespaceSymbol.owner.tag & SymTag.INVOKABLE) == SymTag.INVOKABLE) {
        return (RegIndex) namespaceSymbol.nsURIIndex;
    }
    RegIndex index = getRegIndex(TypeTags.STRING);
    emit(InstructionCodes.SGLOAD, namespaceSymbol.nsURIIndex, index);
    return index;
}
Also used : RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Aggregations

BXMLNSSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol)7 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)4 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)4 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)4 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)3 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)2 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)2 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)2 Stack (java.util.Stack)1 Name (org.ballerinalang.model.Name)1 BXMLAttributeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLAttributeSymbol)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)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 BLangXMLAttribute (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute)1