use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class IterableAnalyzer method validateIterableContext.
public void validateIterableContext(IterableContext context) {
final Operation lastOperation = context.operations.getLast();
final BType expectedType = lastOperation.expectedType;
final BType outputType = lastOperation.resultType;
if (expectedType.tag == TypeTags.VOID && outputType.tag == TypeTags.VOID) {
context.resultType = symTable.noType;
return;
}
if (expectedType.tag == TypeTags.VOID) {
// This error already logged.
return;
}
if (expectedType == symTable.errType) {
context.resultType = symTable.errType;
return;
}
if (outputType.tag == TypeTags.VOID) {
dlog.error(lastOperation.pos, DiagnosticCode.DOES_NOT_RETURN_VALUE, lastOperation.kind);
context.resultType = symTable.errType;
return;
}
// Calculate expected type, if this is an chained iterable operation.
if (outputType.tag == TypeTags.INTERMEDIATE_COLLECTION) {
BIntermediateCollectionType collectionType = (BIntermediateCollectionType) outputType;
final BTupleType tupleType = collectionType.tupleType;
if (expectedType.tag == TypeTags.ARRAY && tupleType.tupleTypes.size() == 1) {
// Convert result into an array.
context.resultType = new BArrayType(tupleType.tupleTypes.get(0));
return;
} else if (expectedType.tag == TypeTags.MAP && tupleType.tupleTypes.size() == 2 && tupleType.tupleTypes.get(0).tag == TypeTags.STRING) {
// Convert result into a map.
context.resultType = new BMapType(TypeTags.MAP, tupleType.tupleTypes.get(1), null);
return;
} else if (expectedType.tag == TypeTags.TABLE) {
// 3. Whether the returned struct is compatible with the constraint struct of the expected type(table)
if (tupleType.getTupleTypes().size() == 1 && tupleType.getTupleTypes().get(0).tag == TypeTags.STRUCT && types.isAssignable(tupleType.getTupleTypes().get(0), ((BTableType) expectedType).constraint)) {
context.resultType = symTable.tableType;
} else {
context.resultType = types.checkType(lastOperation.pos, outputType, ((BTableType) expectedType).constraint, DiagnosticCode.INCOMPATIBLE_TYPES);
}
return;
} else if (expectedType.tag == TypeTags.ANY) {
context.resultType = symTable.errType;
dlog.error(lastOperation.pos, DiagnosticCode.ITERABLE_RETURN_TYPE_MISMATCH, lastOperation.kind);
return;
} else if (expectedType.tag == TypeTags.NONE) {
context.resultType = symTable.noType;
return;
}
}
// Validate compatibility with calculated and expected type.
context.resultType = types.checkType(lastOperation.pos, outputType, expectedType, DiagnosticCode.INCOMPATIBLE_TYPES);
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class IterableAnalyzer method assignOutputAndResultType.
private void assignOutputAndResultType(Operation op, List<BType> argTypes, List<BType> supportedRetTypes) {
if (supportedRetTypes.isEmpty()) {
op.outputType = op.resultType = symTable.voidType;
return;
}
if (op.kind.isTerminal()) {
op.outputType = op.resultType = supportedRetTypes.get(0);
return;
}
if (op.kind == IterableKind.FILTER) {
op.outputType = new BTupleType(argTypes);
op.resultType = new BIntermediateCollectionType((BTupleType) op.outputType);
return;
}
if (supportedRetTypes.size() == 1) {
op.outputType = supportedRetTypes.get(0);
} else {
op.outputType = new BTupleType(supportedRetTypes);
}
op.resultType = new BIntermediateCollectionType(new BTupleType(supportedRetTypes));
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class IterableAnalyzer method calculatedGivenInputArgs.
private List<BType> calculatedGivenInputArgs(Operation operation) {
final BType inputParam = operation.lambdaType.getParameterTypes().get(0);
final List<BType> givenArgTypes;
if (inputParam.tag == TypeTags.TUPLE) {
final BTupleType bTupleType = (BTupleType) inputParam;
givenArgTypes = bTupleType.tupleTypes;
} else {
givenArgTypes = operation.lambdaType.getParameterTypes();
}
operation.arity = givenArgTypes.size();
return givenArgTypes;
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class SemanticAnalyzer method handleAssignNodeWithVar.
private void handleAssignNodeWithVar(BLangAssignment assignNode) {
int ignoredCount = 0;
int createdSymbolCount = 0;
List<Name> newVariables = new ArrayList<Name>();
List<BType> expTypes = new ArrayList<>();
// Check each LHS expression.
for (int i = 0; i < assignNode.varRefs.size(); i++) {
BLangExpression varRef = assignNode.varRefs.get(i);
// If the assignment is declared with "var", then lhs supports only simpleVarRef expressions only.
if (varRef.getKind() != NodeKind.SIMPLE_VARIABLE_REF) {
dlog.error(varRef.pos, DiagnosticCode.INVALID_VARIABLE_ASSIGNMENT, varRef);
expTypes.add(symTable.errType);
continue;
}
// Check variable symbol if exists.
BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) varRef;
((BLangVariableReference) varRef).lhsVar = true;
Name varName = names.fromIdNode(simpleVarRef.variableName);
if (varName == Names.IGNORE) {
ignoredCount++;
simpleVarRef.type = this.symTable.noType;
expTypes.add(symTable.noType);
typeChecker.checkExpr(simpleVarRef, env);
continue;
}
BSymbol symbol = symResolver.lookupSymbol(env, varName, SymTag.VARIABLE);
if (symbol == symTable.notFoundSymbol) {
createdSymbolCount++;
newVariables.add(varName);
expTypes.add(symTable.noType);
} else {
expTypes.add(symbol.type);
}
}
if (ignoredCount == assignNode.varRefs.size() || createdSymbolCount == 0) {
dlog.error(assignNode.pos, DiagnosticCode.NO_NEW_VARIABLES_VAR_ASSIGNMENT);
}
// Check RHS expressions with expected type list.
if (assignNode.getKind() == NodeKind.TUPLE_DESTRUCTURE) {
expTypes = Lists.of(symTable.noType);
}
List<BType> rhsTypes = typeChecker.checkExpr(assignNode.expr, this.env, expTypes);
if (assignNode.safeAssignment) {
rhsTypes = Lists.of(handleSafeAssignmentWithVarDeclaration(assignNode.pos, rhsTypes.get(0)));
}
if (assignNode.getKind() == NodeKind.TUPLE_DESTRUCTURE) {
if (rhsTypes.get(0) != symTable.errType && rhsTypes.get(0).tag == TypeTags.TUPLE) {
BTupleType tupleType = (BTupleType) rhsTypes.get(0);
rhsTypes = tupleType.tupleTypes;
} else if (rhsTypes.get(0) != symTable.errType && rhsTypes.get(0).tag != TypeTags.TUPLE) {
dlog.error(assignNode.pos, DiagnosticCode.INCOMPATIBLE_TYPES_EXP_TUPLE, rhsTypes.get(0));
rhsTypes = typeChecker.getListWithErrorTypes(assignNode.varRefs.size());
}
}
// visit all lhs expressions
for (int i = 0; i < assignNode.varRefs.size(); i++) {
BLangExpression varRef = assignNode.varRefs.get(i);
if (varRef.getKind() != NodeKind.SIMPLE_VARIABLE_REF) {
continue;
}
BType actualType = rhsTypes.get(i);
BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) varRef;
Name varName = names.fromIdNode(simpleVarRef.variableName);
if (newVariables.contains(varName)) {
// define new variables
this.symbolEnter.defineVarSymbol(simpleVarRef.pos, Collections.emptySet(), actualType, varName, env);
}
typeChecker.checkExpr(simpleVarRef, env);
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class SemanticAnalyzer method handleSafeAssignmentWithVarDeclaration.
private BType handleSafeAssignmentWithVarDeclaration(DiagnosticPos pos, BType rhsType) {
if (rhsType.tag != TypeTags.UNION && types.isAssignable(symTable.errStructType, rhsType)) {
dlog.error(pos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
return symTable.errType;
} else if (rhsType.tag != TypeTags.UNION) {
return rhsType;
}
// Collect all the rhs types from the union type
boolean isErrorFound = false;
BUnionType unionType = (BUnionType) rhsType;
Set<BType> rhsTypeSet = new HashSet<>(unionType.memberTypes);
for (BType type : unionType.memberTypes) {
if (types.isAssignable(type, symTable.errStructType)) {
rhsTypeSet.remove(type);
isErrorFound = true;
}
}
if (rhsTypeSet.isEmpty() || !isErrorFound) {
dlog.error(pos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
return symTable.noType;
} else if (rhsTypeSet.size() == 1) {
return rhsTypeSet.toArray(new BType[0])[0];
}
return new BUnionType(null, rhsTypeSet, rhsTypeSet.contains(symTable.nullType));
}
Aggregations