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;
}
}
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);
}
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);
}
}
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);
}
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);
}
}
Aggregations