use of org.wso2.siddhi.query.api.expression.Expression 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);
}
use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.
the class SemanticAnalyzer method visit.
public void visit(BLangBind bindNode) {
List<BType> expTypes = new ArrayList<>();
// Check each LHS expression.
BLangExpression varRef = bindNode.varRef;
((BLangVariableReference) varRef).lhsVar = true;
expTypes.add(typeChecker.checkExpr(varRef, env).get(0));
checkConstantAssignment(varRef);
typeChecker.checkExpr(bindNode.expr, this.env, expTypes);
}
use of org.wso2.siddhi.query.api.expression.Expression 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.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.
the class TypeChecker method checkRecLiteralKeyValue.
private void checkRecLiteralKeyValue(BLangRecordKeyValue keyValuePair, BType recType) {
BType fieldType = symTable.errType;
BLangExpression valueExpr = keyValuePair.valueExpr;
switch(recType.tag) {
case TypeTags.STRUCT:
fieldType = checkStructLiteralKeyExpr(keyValuePair.key, recType, RecordKind.STRUCT);
break;
case TypeTags.MAP:
fieldType = checkMapLiteralKeyExpr(keyValuePair.key.expr, recType, RecordKind.MAP);
break;
case TypeTags.JSON:
fieldType = checkJSONLiteralKeyExpr(keyValuePair.key, recType, RecordKind.JSON);
// If the field is again a struct, treat that literal expression as another constraint JSON.
if (fieldType.tag == TypeTags.STRUCT) {
fieldType = new BJSONType(TypeTags.JSON, fieldType, symTable.jsonType.tsymbol);
}
// First visit the expression having field type, as the expected type.
checkExpr(valueExpr, this.env, Lists.of(fieldType));
// Again check the type compatibility with JSON
if (valueExpr.impConversionExpr == null) {
types.checkTypes(valueExpr, Lists.of(valueExpr.type), Lists.of(symTable.jsonType));
} else {
BType valueType = valueExpr.type;
types.checkTypes(valueExpr, valueExpr.impConversionExpr.types, Lists.of(symTable.jsonType));
valueExpr.type = valueType;
}
resultTypes = Lists.of(valueExpr.type);
return;
}
checkExpr(valueExpr, this.env, Lists.of(fieldType));
}
use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.
the class TypeChecker method visit.
@Override
public void visit(BLangBracedOrTupleExpr bracedOrTupleExpr) {
// Handle Tuple Expression.
if (!expTypes.isEmpty() && expTypes.get(0).tag == TypeTags.TUPLE) {
BTupleType tupleType = (BTupleType) this.expTypes.get(0);
// Fix this.
List<BType> expTypes = getListWithErrorTypes(bracedOrTupleExpr.expressions.size());
if (tupleType.tupleTypes.size() != bracedOrTupleExpr.expressions.size()) {
dlog.error(bracedOrTupleExpr.pos, DiagnosticCode.SYNTAX_ERROR, "tuple and expression size does not match");
} else {
expTypes = tupleType.tupleTypes;
}
List<BType> results = new ArrayList<>();
for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
results.add(checkExpr(bracedOrTupleExpr.expressions.get(i), env, Lists.of(expTypes.get(i))).get(0));
}
resultTypes = Lists.of(new BTupleType(results));
} else if (bracedOrTupleExpr.expressions.size() > 1) {
// This is a tuple.
List<BType> results = new ArrayList<>();
for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
results.add(checkExpr(bracedOrTupleExpr.expressions.get(i), env, Lists.of(symTable.noType)).get(0));
}
resultTypes = Lists.of(new BTupleType(results));
} else {
// This is a braced expression.
bracedOrTupleExpr.isBracedExpr = true;
final BLangExpression expr = bracedOrTupleExpr.expressions.get(0);
final BType actualType = checkExpr(expr, env, Lists.of(symTable.noType)).get(0);
types.setImplicitCastExpr(expr, actualType, expTypes.get(0));
resultTypes = Lists.of(actualType);
}
}
Aggregations