use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
@Override
public void visit(BLangArrayAccessExpr arrayIndexAccessExpr) {
boolean variableStore = this.varAssignment;
this.varAssignment = false;
genNode(arrayIndexAccessExpr.expr, this.env);
Operand varRefRegIndex = arrayIndexAccessExpr.expr.regIndex;
genNode(arrayIndexAccessExpr.indexExpr, this.env);
Operand indexRegIndex = arrayIndexAccessExpr.indexExpr.regIndex;
BArrayType arrayType = (BArrayType) arrayIndexAccessExpr.expr.type;
if (variableStore) {
int opcode = getOpcode(arrayType.eType.tag, InstructionCodes.IASTORE);
emit(opcode, varRefRegIndex, indexRegIndex, arrayIndexAccessExpr.regIndex);
} else {
int opcode = getOpcode(arrayType.eType.tag, InstructionCodes.IALOAD);
emit(opcode, varRefRegIndex, indexRegIndex, calcAndGetExprRegIndex(arrayIndexAccessExpr));
}
this.varAssignment = variableStore;
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.
the class Desugar method visit.
@Override
public void visit(BLangTupleDestructure stmt) {
// var (a, b) = (tuple)
//
// desugar once
// any[] x = (tuple);
// a = x[0];
final BLangBlockStmt blockStmt = ASTBuilderUtil.createBlockStmt(stmt.pos);
BType runTimeType = new BArrayType(symTable.anyType);
final BLangVariable tuple = ASTBuilderUtil.createVariable(stmt.pos, "", runTimeType, null, new BVarSymbol(0, names.fromString("tuple"), this.env.scope.owner.pkgID, runTimeType, this.env.scope.owner));
tuple.expr = stmt.expr;
final BLangVariableDef variableDef = ASTBuilderUtil.createVariableDefStmt(stmt.pos, blockStmt);
variableDef.var = tuple;
for (int index = 0; index < stmt.varRefs.size(); index++) {
BLangExpression varRef = stmt.varRefs.get(index);
BLangLiteral indexExpr = ASTBuilderUtil.createLiteral(stmt.pos, symTable.intType, (long) index);
BLangIndexBasedAccess arrayAccess = ASTBuilderUtil.createIndexBasesAccessExpr(stmt.pos, symTable.anyType, tuple.symbol, indexExpr);
final BLangExpression assignmentExpr;
if (types.isValueType(varRef.type)) {
BLangTypeConversionExpr castExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
castExpr.expr = arrayAccess;
castExpr.conversionSymbol = Symbols.createUnboxValueTypeOpSymbol(symTable.anyType, varRef.type);
castExpr.type = varRef.type;
assignmentExpr = castExpr;
} else {
assignmentExpr = arrayAccess;
}
final BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(stmt.pos, blockStmt);
assignmentStmt.declaredWithVar = stmt.declaredWithVar;
assignmentStmt.varRefs = Lists.of(varRef);
assignmentStmt.expr = assignmentExpr;
}
result = rewrite(blockStmt, env);
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType 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.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.
the class Types method checkArrayEquality.
public boolean checkArrayEquality(BType source, BType target) {
if (target.tag == TypeTags.ARRAY && source.tag == TypeTags.ARRAY) {
// Both types are array types
BArrayType lhrArrayType = (BArrayType) target;
BArrayType rhsArrayType = (BArrayType) source;
return checkArrayEquality(lhrArrayType.eType, rhsArrayType.eType);
}
// Now one or both types are not array types and they have to be equal
return isSameType(source, target);
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.
the class Types method checkForeachTypes.
List<BType> checkForeachTypes(BLangNode collection, int variableSize) {
BType collectionType = collection.type;
List<BType> errorTypes;
int maxSupportedTypes;
switch(collectionType.tag) {
case TypeTags.ARRAY:
BArrayType bArrayType = (BArrayType) collectionType;
if (variableSize == 1) {
return Lists.of(bArrayType.eType);
} else if (variableSize == 2) {
return Lists.of(symTable.intType, bArrayType.eType);
} else {
maxSupportedTypes = 2;
errorTypes = Lists.of(symTable.intType, bArrayType.eType);
}
break;
case TypeTags.MAP:
BMapType bMapType = (BMapType) collectionType;
if (variableSize == 1) {
return Lists.of(bMapType.constraint);
} else if (variableSize == 2) {
return Lists.of(symTable.stringType, bMapType.constraint);
} else {
maxSupportedTypes = 2;
errorTypes = Lists.of(symTable.stringType, bMapType.constraint);
}
break;
case TypeTags.JSON:
if (variableSize == 1) {
return Lists.of(symTable.jsonType);
} else {
maxSupportedTypes = 1;
errorTypes = Lists.of(symTable.jsonType);
}
break;
case TypeTags.XML:
if (variableSize == 1) {
return Lists.of(symTable.xmlType);
} else if (variableSize == 2) {
return Lists.of(symTable.intType, symTable.xmlType);
} else {
maxSupportedTypes = 2;
errorTypes = Lists.of(symTable.intType, symTable.xmlType);
}
break;
case TypeTags.TABLE:
BTableType tableType = (BTableType) collectionType;
if (variableSize == 1) {
return Lists.of(tableType.constraint);
} else {
maxSupportedTypes = 1;
errorTypes = Lists.of(tableType.constraint);
}
break;
case TypeTags.ERROR:
return Collections.nCopies(variableSize, symTable.errType);
default:
dlog.error(collection.pos, DiagnosticCode.ITERABLE_NOT_SUPPORTED_COLLECTION, collectionType);
return Collections.nCopies(variableSize, symTable.errType);
}
dlog.error(collection.pos, DiagnosticCode.ITERABLE_TOO_MANY_VARIABLES, collectionType);
errorTypes.addAll(Collections.nCopies(variableSize - maxSupportedTypes, symTable.errType));
return errorTypes;
}
Aggregations