use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.
the class TypeChecker method visit.
public void visit(BLangIndexBasedAccess indexBasedAccessExpr) {
BType actualType = symTable.errType;
// First analyze the variable reference expression.
checkExpr(indexBasedAccessExpr.expr, this.env, Lists.of(symTable.noType));
BType indexExprType;
BType varRefType = indexBasedAccessExpr.expr.type;
BLangExpression indexExpr = indexBasedAccessExpr.indexExpr;
switch(varRefType.tag) {
case TypeTags.STRUCT:
indexExprType = checkIndexExprForStructFieldAccess(indexExpr);
if (indexExprType.tag == TypeTags.STRING) {
String fieldName = (String) ((BLangLiteral) indexExpr).value;
actualType = checkStructFieldAccess(indexBasedAccessExpr, names.fromString(fieldName), varRefType);
}
break;
case TypeTags.MAP:
indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.stringType)).get(0);
if (indexExprType.tag == TypeTags.STRING) {
actualType = ((BMapType) varRefType).getConstraint();
}
break;
case TypeTags.JSON:
BType constraintType = ((BJSONType) varRefType).constraint;
if (constraintType.tag == TypeTags.STRUCT) {
indexExprType = checkIndexExprForStructFieldAccess(indexExpr);
if (indexExprType.tag != TypeTags.STRING) {
break;
}
String fieldName = (String) ((BLangLiteral) indexExpr).value;
BType fieldType = checkStructFieldAccess(indexBasedAccessExpr, names.fromString(fieldName), constraintType);
// If the type of the field is struct, treat it as constraint JSON type.
if (fieldType.tag == TypeTags.STRUCT) {
actualType = new BJSONType(TypeTags.JSON, fieldType, symTable.jsonType.tsymbol);
break;
}
} else {
indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.noType)).get(0);
if (indexExprType.tag != TypeTags.STRING && indexExprType.tag != TypeTags.INT) {
dlog.error(indexExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES, symTable.stringType, indexExprType);
break;
}
}
actualType = symTable.jsonType;
break;
case TypeTags.ARRAY:
indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.intType)).get(0);
if (indexExprType.tag == TypeTags.INT) {
actualType = ((BArrayType) varRefType).getElementType();
}
break;
case TypeTags.XML:
if (indexBasedAccessExpr.lhsVar) {
dlog.error(indexBasedAccessExpr.pos, DiagnosticCode.CANNOT_UPDATE_XML_SEQUENCE);
break;
}
checkExpr(indexExpr, this.env).get(0);
actualType = symTable.xmlType;
break;
case TypeTags.ERROR:
// Do nothing
break;
default:
dlog.error(indexBasedAccessExpr.pos, DiagnosticCode.OPERATION_DOES_NOT_SUPPORT_INDEXING, indexBasedAccessExpr.expr.type);
}
resultTypes = types.checkTypes(indexBasedAccessExpr, Lists.of(actualType), this.expTypes);
}
use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.
the class CodeGenerator method visitXMLTagName.
/**
* Visit XML tag name and return the index of the tag name in the reference registry.
*
* @param tagName Tag name expression
* @param xmlElementEnv Environment of the XML element of the tag
* @param xmlElementLiteral XML element literal to which the tag name belongs to
* @return Index of the tag name, in the reference registry
*/
private RegIndex visitXMLTagName(BLangExpression tagName, SymbolEnv xmlElementEnv, BLangXMLElementLiteral xmlElementLiteral) {
genNode(tagName, xmlElementEnv);
RegIndex startTagNameRegIndex = tagName.regIndex;
// If this is a string representation of element name, generate the namespace lookup instructions
if (tagName.getKind() != NodeKind.XML_QNAME) {
RegIndex localNameRegIndex = getRegIndex(TypeTags.STRING);
RegIndex uriRegIndex = getRegIndex(TypeTags.STRING);
emit(InstructionCodes.S2QNAME, startTagNameRegIndex, localNameRegIndex, uriRegIndex);
startTagNameRegIndex = getRegIndex(TypeTags.XML);
generateURILookupInstructions(xmlElementLiteral.namespacesInScope, localNameRegIndex, uriRegIndex, startTagNameRegIndex, xmlElementLiteral.pos, xmlElementEnv);
tagName.regIndex = startTagNameRegIndex;
}
return startTagNameRegIndex;
}
use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
public void visit(BLangAssignment assignNode) {
List<BLangExpression> lhrExprs = assignNode.varRefs;
if (assignNode.declaredWithVar) {
lhrExprs.stream().filter(lhsExr -> lhsExr.type.tag != TypeTags.NONE).map(lhsExr -> (BLangVariableReference) lhsExr).forEach(varRef -> {
visitVarSymbol(varRef.symbol, lvIndexes, localVarAttrInfo);
});
}
// Calculate the register indexes of lhs expressions.
int nLHSExpr = lhrExprs.size();
RegIndex[] regIndexes = new RegIndex[nLHSExpr];
for (int i = 0; i < nLHSExpr; i++) {
BLangExpression lExpr = lhrExprs.get(i);
if (lExpr.type.tag == TypeTags.NONE) {
continue;
}
if (lExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF && lExpr instanceof BLangLocalVarRef) {
lExpr.regIndex = ((BLangVariableReference) lExpr).symbol.varIndex;
regIndexes[i] = lExpr.regIndex;
}
}
// Set calculated reg indexes and visit rhs expression
BLangExpression rhsExpr = assignNode.expr;
rhsExpr.regIndex = regIndexes[0];
genNode(rhsExpr, this.env);
// Set the reg indexes generated by visiting rhs expression to lhs expression
regIndexes[0] = rhsExpr.regIndex;
for (int i = 0; i < lhrExprs.size(); i++) {
BLangExpression lExpr = lhrExprs.get(i);
if (lExpr.type.tag == TypeTags.NONE) {
continue;
}
if (lExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF && lExpr instanceof BLangLocalVarRef) {
continue;
}
varAssignment = true;
lExpr.regIndex = regIndexes[i];
genNode(lExpr, this.env);
varAssignment = false;
}
}
use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.
the class ASTBuilderUtil method generateConversionExpr.
static BLangExpression generateConversionExpr(BLangExpression varRef, BType target, SymbolResolver symResolver) {
if (varRef.type.tag == target.tag || varRef.type.tag > TypeTags.TYPEDESC) {
return varRef;
}
// Box value using cast expression.
final BLangTypeConversionExpr conversion = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
conversion.pos = varRef.pos;
conversion.expr = varRef;
conversion.type = target;
conversion.targetType = target;
conversion.conversionSymbol = (BConversionOperatorSymbol) symResolver.resolveConversionOperator(varRef.type, target);
return conversion;
}
use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.
the class IterableCodeDesugar method desugar.
public void desugar(IterableContext ctx) {
// Gather required data for code generation.
processIterableContext(ctx);
// Generate Iterable Iteration.
generateIteratorFunction(ctx);
// Create invocation expression to invoke iterable operation.
final BLangInvocation iExpr = ASTBuilderUtil.createInvocationExpr(ctx.collectionExpr.pos, ctx.iteratorFuncSymbol, Collections.emptyList(), symResolver);
iExpr.requiredArgs.add(ctx.collectionExpr);
if (ctx.getLastOperation().expectedType == symTable.noType || ctx.getLastOperation().expectedType == symTable.voidType) {
ctx.iteratorCaller = iExpr;
} else {
ctx.iteratorCaller = ASTBuilderUtil.wrapToConversionExpr(ctx.getLastOperation().expectedType, iExpr, symTable, types);
}
}
Aggregations