use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class SemanticAnalyzer method handleForeachVariables.
// Private methods
private void handleForeachVariables(BLangForeach foreachStmt, List<BType> varTypes, SymbolEnv env) {
for (int i = 0; i < foreachStmt.varRefs.size(); i++) {
BLangExpression varRef = foreachStmt.varRefs.get(i);
// foreach variables supports only simpleVarRef expressions only.
if (varRef.getKind() != NodeKind.SIMPLE_VARIABLE_REF) {
dlog.error(varRef.pos, DiagnosticCode.INVALID_VARIABLE_ASSIGNMENT, varRef);
continue;
}
BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) varRef;
simpleVarRef.lhsVar = true;
Name varName = names.fromIdNode(simpleVarRef.variableName);
if (varName == Names.IGNORE) {
simpleVarRef.type = this.symTable.noType;
typeChecker.checkExpr(simpleVarRef, env);
continue;
}
// Check variable symbol for existence.
BSymbol symbol = symResolver.lookupSymbol(env, varName, SymTag.VARIABLE);
if (symbol == symTable.notFoundSymbol) {
symbolEnter.defineVarSymbol(simpleVarRef.pos, Collections.emptySet(), varTypes.get(i), varName, env);
typeChecker.checkExpr(simpleVarRef, env);
} else {
dlog.error(simpleVarRef.pos, DiagnosticCode.REDECLARED_SYMBOL, varName);
}
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class TypeChecker method getTypeOfExprInFieldAccess.
private BType getTypeOfExprInFieldAccess(BLangExpression expr) {
// First check whether variable expression is of type enum.
if (expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
BLangSimpleVarRef varRef = (BLangSimpleVarRef) expr;
BSymbol symbol = symResolver.lookupSymbolInPackage(varRef.pos, env, names.fromIdNode(varRef.pkgAlias), names.fromIdNode(varRef.variableName), SymTag.ENUM);
if (symbol != symTable.notFoundSymbol) {
expr.type = symbol.type;
return symbol.type;
}
}
checkExpr(expr, this.env, Lists.of(symTable.noType));
return expr.type;
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class TypeChecker method checkStructLiteralKeyExpr.
private BType checkStructLiteralKeyExpr(BLangRecordKey key, BType recordType, RecordKind recKind) {
Name fieldName;
BLangExpression keyExpr = key.expr;
if (keyExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
BLangSimpleVarRef varRef = (BLangSimpleVarRef) keyExpr;
fieldName = names.fromIdNode(varRef.variableName);
} else {
// keys of the struct literal can only be a varRef (identifier)
dlog.error(keyExpr.pos, DiagnosticCode.INVALID_STRUCT_LITERAL_KEY);
return symTable.errType;
}
// Check weather the struct field exists
BSymbol fieldSymbol = symResolver.resolveStructField(keyExpr.pos, this.env, fieldName, recordType.tsymbol);
if (fieldSymbol == symTable.notFoundSymbol) {
dlog.error(keyExpr.pos, DiagnosticCode.UNDEFINED_STRUCT_FIELD, fieldName, recordType.tsymbol);
return symTable.errType;
}
// Setting the struct field symbol for future use in Desugar and code generator.
key.fieldSymbol = (BVarSymbol) fieldSymbol;
return fieldSymbol.type;
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.
the class TypeChecker method visit.
// Expressions
public void visit(BLangLiteral literalExpr) {
BType literalType = symTable.getTypeFromTag(literalExpr.typeTag);
resultTypes = types.checkTypes(literalExpr, Lists.of(literalType), 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(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);
}
Aggregations