use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class TypeChecker method checkNamedTransformerInvocation.
private List<BType> checkNamedTransformerInvocation(BLangTypeConversionExpr conversionExpr, BType sourceType, BType targetType) {
List<BType> actualTypes = getListWithErrorTypes(expTypes.size());
BLangInvocation transformerInvocation = conversionExpr.transformerInvocation;
BSymbol transformerSymbol = symResolver.lookupSymbolInPackage(transformerInvocation.pos, env, names.fromIdNode(transformerInvocation.pkgAlias), names.fromIdNode(transformerInvocation.name), SymTag.TRANSFORMER);
if (transformerSymbol == symTable.notFoundSymbol) {
dlog.error(conversionExpr.pos, DiagnosticCode.UNDEFINED_TRANSFORMER, transformerInvocation.name);
} else {
conversionExpr.conversionSymbol = (BConversionOperatorSymbol) (transformerInvocation.symbol = transformerSymbol);
// Check the transformer invocation. Expected type for the transformer is the target type
// of the cast conversion operator, but not the lhs type.
List<BType> prevExpType = expTypes;
expTypes = Lists.of(targetType);
checkInvocationParamAndReturnType(transformerInvocation);
expTypes = prevExpType;
if (transformerInvocation.type != symTable.errType) {
BInvokableType transformerSymType = (BInvokableType) transformerSymbol.type;
transformerInvocation.types = transformerSymType.retTypes;
actualTypes = getActualTypesOfConversionExpr(conversionExpr, targetType, sourceType, conversionExpr.conversionSymbol);
}
}
return actualTypes;
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class CodeAnalyzer method visit.
public void visit(BLangTryCatchFinally tryNode) {
this.checkStatementExecutionValidity(tryNode);
tryNode.tryBody.accept(this);
this.resetStatementReturns();
List<BType> caughtTypes = new ArrayList<>();
for (BLangCatch bLangCatch : tryNode.getCatchBlocks()) {
if (caughtTypes.contains(bLangCatch.getParameter().type)) {
dlog.error(bLangCatch.getParameter().pos, DiagnosticCode.DUPLICATED_ERROR_CATCH, bLangCatch.getParameter().type);
}
caughtTypes.add(bLangCatch.getParameter().type);
bLangCatch.body.accept(this);
this.resetStatementReturns();
}
if (tryNode.finallyBody != null) {
tryNode.finallyBody.accept(this);
this.resetStatementReturns();
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType 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.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class SemanticAnalyzer method handleSafeAssignment.
private void handleSafeAssignment(DiagnosticPos lhsPos, BType lhsType, BLangExpression rhsExpr, SymbolEnv env) {
// Collect all the lhs types
Set<BType> lhsTypes = lhsType.tag == TypeTags.UNION ? ((BUnionType) lhsType).memberTypes : new HashSet<BType>() {
{
add(lhsType);
}
};
// If there is at least one lhs type which assignable to the error type, then report an error.
for (BType type : lhsTypes) {
if (types.isAssignable(symTable.errStructType, type)) {
dlog.error(lhsPos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
typeChecker.checkExpr(rhsExpr, env, Lists.of(symTable.errType));
return;
}
}
// Create a new union type with the error type and continue the type checking process.
lhsTypes.add(symTable.errStructType);
BUnionType lhsUnionType = new BUnionType(null, lhsTypes, lhsTypes.contains(symTable.nullType));
typeChecker.checkExpr(rhsExpr, env, Lists.of(lhsUnionType));
if (rhsExpr.type.tag == TypeTags.UNION) {
BUnionType rhsUnionType = (BUnionType) rhsExpr.type;
for (BType type : rhsUnionType.memberTypes) {
if (types.isAssignable(symTable.errStructType, type)) {
return;
}
}
} else if (rhsExpr.type.tag != TypeTags.ERROR) {
dlog.error(rhsExpr.pos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class SemanticAnalyzer method visit.
public void visit(BLangVariable varNode) {
int ownerSymTag = env.scope.owner.tag;
if ((ownerSymTag & SymTag.INVOKABLE) == SymTag.INVOKABLE) {
// If the variable is parameter then the variable symbol is already defined
if (varNode.symbol == null) {
symbolEnter.defineNode(varNode, env);
}
}
BType lhsType = varNode.symbol.type;
varNode.type = lhsType;
// Here we validate annotation attachments for package level variables.
varNode.annAttachments.forEach(a -> {
a.attachmentPoint = new BLangAnnotationAttachmentPoint(BLangAnnotationAttachmentPoint.AttachmentPoint.CONST);
a.accept(this);
});
// Here we validate document attachments for package level variables.
varNode.docAttachments.forEach(doc -> {
doc.accept(this);
});
// Analyze the init expression
BLangExpression rhsExpr = varNode.expr;
if (rhsExpr == null) {
return;
}
// Here we create a new symbol environment to catch self references by keep the current
// variable symbol in the symbol environment
// e.g. int a = x + a;
SymbolEnv varInitEnv = SymbolEnv.createVarInitEnv(varNode, env, varNode.symbol);
// It will we done during the init-function of the respective construct is visited.
if ((ownerSymTag & SymTag.PACKAGE) == SymTag.PACKAGE || (ownerSymTag & SymTag.SERVICE) == SymTag.SERVICE || (ownerSymTag & SymTag.CONNECTOR) == SymTag.CONNECTOR) {
return;
}
// Return if this not a safe assignment
if (!varNode.safeAssignment) {
typeChecker.checkExpr(rhsExpr, varInitEnv, Lists.of(lhsType));
return;
}
handleSafeAssignment(varNode.pos, lhsType, rhsExpr, varInitEnv);
}
Aggregations