use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression 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)));
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.
the class TypeChecker method checkInvocationArgs.
private List<BType> checkInvocationArgs(BLangInvocation iExpr, List<BType> paramTypes, int requiredParamsCount, BLangExpression vararg) {
List<BType> actualTypes = getListWithErrorTypes(expTypes.size());
BInvokableSymbol invocableSymbol = (BInvokableSymbol) iExpr.symbol;
// Check whether the expected param count and the actual args counts are matching.
if (requiredParamsCount > iExpr.requiredArgs.size()) {
dlog.error(iExpr.pos, DiagnosticCode.NOT_ENOUGH_ARGS_FUNC_CALL, iExpr.name.value);
return actualTypes;
} else if (invocableSymbol.restParam == null && (vararg != null || !iExpr.restArgs.isEmpty())) {
dlog.error(iExpr.pos, DiagnosticCode.TOO_MANY_ARGS_FUNC_CALL, iExpr.name.value);
return actualTypes;
}
// formal parameters of the outer function.
if (iExpr.argExprs.size() == 1 && iExpr.argExprs.get(0).getKind() == NodeKind.INVOCATION) {
checkExpr(iExpr.requiredArgs.get(0), this.env, ((BInvokableType) invocableSymbol.type).paramTypes);
} else {
checkRequiredArgs(iExpr.requiredArgs, paramTypes);
}
checkNamedArgs(iExpr.namedArgs, invocableSymbol.defaultableParams);
checkRestArgs(iExpr.restArgs, vararg, invocableSymbol.restParam);
if (iExpr.async) {
return Arrays.asList(this.generateFutureType(invocableSymbol));
} else {
return invocableSymbol.type.getReturnTypes();
}
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.
the class TypeChecker method setExprType.
private void setExprType(BLangExpression expr, List<BType> expTypes) {
int expected = expTypes.size();
if (expr instanceof MultiReturnExpr) {
MultiReturnExpr multiReturnExpr = (MultiReturnExpr) expr;
multiReturnExpr.setTypes(resultTypes);
} else {
if (expected > 1) {
dlog.error(expr.pos, DiagnosticCode.ASSIGNMENT_COUNT_MISMATCH, expected, 1);
resultTypes = getListWithErrorTypes(expected);
}
}
if (resultTypes.size() > 0) {
expr.type = resultTypes.get(0);
}
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression 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);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression 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);
}
Aggregations