use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class SymbolResolver method resolveTypeNode.
public BType resolveTypeNode(BLangType typeNode, SymbolEnv env, DiagnosticCode diagCode) {
SymbolEnv prevEnv = this.env;
DiagnosticCode preDiagCode = this.diagCode;
this.env = env;
this.diagCode = diagCode;
typeNode.accept(this);
this.env = prevEnv;
this.diagCode = preDiagCode;
// if it is not already a union type, JSON type, or any type
if (typeNode.nullable && this.resultType.tag == TypeTags.UNION) {
BUnionType unionType = (BUnionType) this.resultType;
unionType.memberTypes.add(symTable.nullType);
unionType.setNullable(true);
} else if (typeNode.nullable && resultType.tag != TypeTags.JSON && resultType.tag != TypeTags.ANY) {
Set<BType> memberTypes = new HashSet<BType>(2) {
{
add(resultType);
add(symTable.nullType);
}
};
this.resultType = new BUnionType(null, memberTypes, true);
}
typeNode.type = resultType;
return resultType;
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class SymbolResolver method resolveImplicitConversionOp.
public BSymbol resolveImplicitConversionOp(BType sourceType, BType targetType) {
BSymbol symbol = resolveOperator(Names.CONVERSION_OP, Lists.of(sourceType, targetType));
if (symbol == symTable.notFoundSymbol) {
return symbol;
}
BConversionOperatorSymbol castSymbol = (BConversionOperatorSymbol) symbol;
if (castSymbol.implicit) {
return symbol;
}
return symTable.notFoundSymbol;
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class TypeChecker method visit.
public void visit(BLangRecordLiteral recordLiteral) {
BType actualType = symTable.errType;
int expTypeTag = expTypes.get(0).tag;
if (expTypeTag == TypeTags.NONE || expTypeTag == TypeTags.ANY) {
// var a = {}
// Change the expected type to map
expTypes = Lists.of(symTable.mapType);
}
List<BType> matchedTypeList = getRecordCompatibleType(expTypes.get(0));
if (matchedTypeList.isEmpty()) {
dlog.error(recordLiteral.pos, DiagnosticCode.INVALID_LITERAL_FOR_TYPE, expTypes.get(0));
} else if (matchedTypeList.size() > 1) {
dlog.error(recordLiteral.pos, DiagnosticCode.AMBIGUOUS_TYPES, expTypes.get(0));
} else {
recordLiteral.keyValuePairs.forEach(keyValuePair -> checkRecLiteralKeyValue(keyValuePair, matchedTypeList.get(0)));
actualType = matchedTypeList.get(0);
// TODO Following check can be moved the code analyzer.
if (expTypeTag == TypeTags.STRUCT) {
validateStructInitalizer(recordLiteral.pos);
}
}
resultTypes = types.checkTypes(recordLiteral, Lists.of(actualType), expTypes);
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class TypeChecker method visit.
@Override
public void visit(BLangTableQueryExpression tableQueryExpression) {
BType actualType = symTable.errType;
int expTypeTag = expTypes.get(0).tag;
if (expTypeTag == TypeTags.TABLE) {
actualType = expTypes.get(0);
} else if (expTypeTag != TypeTags.ERROR) {
dlog.error(tableQueryExpression.pos, DiagnosticCode.INCOMPATIBLE_TYPES_CONVERSION, expTypes.get(0));
}
BLangTableQuery tableQuery = (BLangTableQuery) tableQueryExpression.getTableQuery();
tableQuery.accept(this);
resultTypes = types.checkTypes(tableQueryExpression, Lists.of(actualType), expTypes);
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class TypeChecker method visit.
public void visit(BLangUnaryExpr unaryExpr) {
BType exprType = null;
BType actualType = symTable.errType;
if (OperatorKind.TYPEOF.equals(unaryExpr.operator)) {
// Handle typeof operator separately
if (unaryExpr.expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
BLangSimpleVarRef varRef = (BLangSimpleVarRef) unaryExpr.expr;
Name varRefName = names.fromIdNode((varRef).variableName);
Name pkgAlias = names.fromIdNode((varRef).pkgAlias);
// Resolve symbol for BLangSimpleVarRef
BSymbol varRefSybmol = symResolver.lookupSymbolInPackage(unaryExpr.pos, env, pkgAlias, varRefName, SymTag.VARIABLE);
if (varRefSybmol == symTable.notFoundSymbol) {
// Resolve symbol for User Defined Type ( converted from BLangSimpleVarRef )
BLangTypeofExpr typeAccessExpr = getTypeAccessExpression(varRef);
unaryExpr.expr = typeAccessExpr;
actualType = typeAccessExpr.type;
resultTypes = types.checkTypes(unaryExpr, Lists.of(actualType), expTypes);
return;
} else {
// Check type if resolved as BLangSimpleVarRef
exprType = checkExpr(unaryExpr.expr, env).get(0);
}
} else {
// Check type if resolved as non BLangSimpleVarRef Expression
exprType = checkExpr(unaryExpr.expr, env).get(0);
}
if (exprType != symTable.errType) {
unaryExpr.opSymbol = Symbols.createTypeofOperatorSymbol(exprType, types, symTable, names);
actualType = unaryExpr.opSymbol.type.getReturnTypes().get(0);
}
} else {
exprType = checkExpr(unaryExpr.expr, env).get(0);
if (exprType != symTable.errType) {
BSymbol symbol = symResolver.resolveUnaryOperator(unaryExpr.pos, unaryExpr.operator, exprType);
if (symbol == symTable.notFoundSymbol) {
dlog.error(unaryExpr.pos, DiagnosticCode.UNARY_OP_INCOMPATIBLE_TYPES, unaryExpr.operator, exprType);
} else {
unaryExpr.opSymbol = (BOperatorSymbol) symbol;
actualType = symbol.type.getReturnTypes().get(0);
}
}
}
resultTypes = types.checkTypes(unaryExpr, Lists.of(actualType), expTypes);
}
Aggregations