use of org.wso2.carbon.apimgt.api.model.Tag in project ballerina by ballerina-lang.
the class IterableAnalyzer method validateLambdaReturnArgs.
private void validateLambdaReturnArgs(Operation operation, List<BType> supportedTypes, List<BType> givenTypes) {
if (supportedTypes == givenTypes) {
// Ignore this validation.
return;
}
if (givenTypes.size() > supportedTypes.size()) {
dlog.error(operation.pos, DiagnosticCode.ITERABLE_TOO_MANY_RETURN_VARIABLES, operation.kind);
operation.outputType = operation.resultType = symTable.errType;
return;
} else if (givenTypes.size() < supportedTypes.size()) {
dlog.error(operation.pos, DiagnosticCode.ITERABLE_NOT_ENOUGH_RETURN_VARIABLES, operation.kind);
operation.outputType = operation.resultType = symTable.errType;
return;
}
for (int i = 0; i < givenTypes.size(); i++) {
if (givenTypes.get(i).tag == TypeTags.ERROR) {
return;
}
BType result = types.checkType(operation.pos, givenTypes.get(i), supportedTypes.get(i), DiagnosticCode.ITERABLE_LAMBDA_INCOMPATIBLE_TYPES);
if (result.tag == TypeTags.ERROR) {
operation.outputType = operation.resultType = symTable.errType;
}
}
}
use of org.wso2.carbon.apimgt.api.model.Tag in project ballerina by ballerina-lang.
the class SemanticAnalyzer method visit.
public void visit(BLangMatch matchNode) {
List<BType> exprTypes = typeChecker.checkExpr(matchNode.expr, env, Lists.of(symTable.noType));
if (exprTypes.size() > 1) {
dlog.error(matchNode.expr.pos, DiagnosticCode.MULTI_VAL_EXPR_IN_SINGLE_VAL_CONTEXT);
return;
} else if (exprTypes.size() == 0) {
dlog.error(matchNode.expr.pos, DiagnosticCode.INVALID_EXPR_IN_MATCH_STMT);
return;
} else if (exprTypes.get(0).tag == TypeTags.UNION) {
BUnionType unionType = (BUnionType) exprTypes.get(0);
exprTypes = new ArrayList<>(unionType.memberTypes);
}
// visit patterns
matchNode.patternClauses.forEach(patternClause -> patternClause.accept(this));
matchNode.exprTypes = exprTypes;
}
use of org.wso2.carbon.apimgt.api.model.Tag in project ballerina by ballerina-lang.
the class TypeChecker method visit.
public void visit(BLangArrayLiteral arrayLiteral) {
// Check whether the expected type is an array type
// var a = []; and var a = [1,2,3,4]; are illegal statements, because we cannot infer the type here.
BType actualType = symTable.errType;
int expTypeTag = expTypes.get(0).tag;
if (expTypeTag == TypeTags.JSON || expTypeTag == TypeTags.ANY) {
checkExprs(arrayLiteral.exprs, this.env, expTypes.get(0));
actualType = expTypes.get(0);
} else if (expTypeTag == TypeTags.ARRAY) {
BArrayType arrayType = (BArrayType) expTypes.get(0);
checkExprs(arrayLiteral.exprs, this.env, arrayType.eType);
actualType = new BArrayType(arrayType.eType);
} else if (expTypeTag != TypeTags.ERROR) {
List<BType> resTypes = checkExprs(arrayLiteral.exprs, this.env, symTable.noType);
Set<BType> arrayLitExprTypeSet = new HashSet<>(resTypes);
BType[] uniqueExprTypes = arrayLitExprTypeSet.toArray(new BType[0]);
if (uniqueExprTypes.length == 0) {
actualType = symTable.anyType;
} else if (uniqueExprTypes.length == 1) {
actualType = resTypes.get(0);
} else {
BType superType = uniqueExprTypes[0];
for (int i = 1; i < uniqueExprTypes.length; i++) {
if (types.isAssignable(superType, uniqueExprTypes[i])) {
superType = uniqueExprTypes[i];
} else if (!types.isAssignable(uniqueExprTypes[i], superType)) {
superType = symTable.anyType;
break;
}
}
actualType = superType;
}
actualType = new BArrayType(actualType);
}
resultTypes = types.checkTypes(arrayLiteral, Lists.of(actualType), expTypes);
}
use of org.wso2.carbon.apimgt.api.model.Tag in project ballerina by ballerina-lang.
the class TypeChecker method visit.
@Override
public void visit(BLangBracedOrTupleExpr bracedOrTupleExpr) {
// Handle Tuple Expression.
if (!expTypes.isEmpty() && expTypes.get(0).tag == TypeTags.TUPLE) {
BTupleType tupleType = (BTupleType) this.expTypes.get(0);
// Fix this.
List<BType> expTypes = getListWithErrorTypes(bracedOrTupleExpr.expressions.size());
if (tupleType.tupleTypes.size() != bracedOrTupleExpr.expressions.size()) {
dlog.error(bracedOrTupleExpr.pos, DiagnosticCode.SYNTAX_ERROR, "tuple and expression size does not match");
} else {
expTypes = tupleType.tupleTypes;
}
List<BType> results = new ArrayList<>();
for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
results.add(checkExpr(bracedOrTupleExpr.expressions.get(i), env, Lists.of(expTypes.get(i))).get(0));
}
resultTypes = Lists.of(new BTupleType(results));
} else if (bracedOrTupleExpr.expressions.size() > 1) {
// This is a tuple.
List<BType> results = new ArrayList<>();
for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
results.add(checkExpr(bracedOrTupleExpr.expressions.get(i), env, Lists.of(symTable.noType)).get(0));
}
resultTypes = Lists.of(new BTupleType(results));
} else {
// This is a braced expression.
bracedOrTupleExpr.isBracedExpr = true;
final BLangExpression expr = bracedOrTupleExpr.expressions.get(0);
final BType actualType = checkExpr(expr, env, Lists.of(symTable.noType)).get(0);
types.setImplicitCastExpr(expr, actualType, expTypes.get(0));
resultTypes = Lists.of(actualType);
}
}
use of org.wso2.carbon.apimgt.api.model.Tag in project ballerina by ballerina-lang.
the class Types method isArrayTypesAssignable.
public boolean isArrayTypesAssignable(BType source, BType target) {
if (target.tag == TypeTags.ARRAY && source.tag == TypeTags.ARRAY) {
// Both types are array types
BArrayType lhsArrayType = (BArrayType) target;
BArrayType rhsArrayType = (BArrayType) source;
return isArrayTypesAssignable(rhsArrayType.eType, lhsArrayType.eType);
} else if (source.tag == TypeTags.ARRAY) {
// to JSON.
if (target.tag == TypeTags.JSON) {
return getElementType(source).tag == TypeTags.JSON;
}
// Then lhs type should 'any' type
return target.tag == TypeTags.ANY;
} else if (target.tag == TypeTags.ARRAY) {
// Only the left-hand side is an array type
return false;
}
// Now both types are not array types and they have to be equal
if (target == source) {
// TODO Figure out this.
return true;
}
// In this case, lhs type should be of type 'any' and the rhs type cannot be a value type
return target.tag == TypeTags.ANY && !isValueType(source);
}
Aggregations