use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.
the class SemanticAnalyzer method visit.
public void visit(BLangAssignment assignNode) {
if (assignNode.isDeclaredWithVar()) {
handleAssignNodeWithVar(assignNode);
return;
}
// Check each LHS expression.
List<BType> expTypes = new ArrayList<>();
for (BLangExpression expr : assignNode.varRefs) {
// In assignment, lhs supports only simpleVarRef, indexBasedAccess, filedBasedAccess expressions.
if (expr.getKind() != NodeKind.SIMPLE_VARIABLE_REF && expr.getKind() != NodeKind.INDEX_BASED_ACCESS_EXPR && expr.getKind() != NodeKind.FIELD_BASED_ACCESS_EXPR && expr.getKind() != NodeKind.XML_ATTRIBUTE_ACCESS_EXPR) {
dlog.error(expr.pos, DiagnosticCode.INVALID_VARIABLE_ASSIGNMENT, expr);
expTypes.add(symTable.errType);
continue;
}
// Evaluate the variable reference expression.
BLangVariableReference varRef = (BLangVariableReference) expr;
varRef.lhsVar = true;
typeChecker.checkExpr(varRef, env);
// Check whether we've got an enumerator access expression here.
if (varRef.getKind() == NodeKind.FIELD_BASED_ACCESS_EXPR && ((BLangFieldBasedAccess) varRef).expr.type.tag == TypeTags.ENUM) {
dlog.error(varRef.pos, DiagnosticCode.INVALID_VARIABLE_ASSIGNMENT, varRef);
expTypes.add(symTable.errType);
continue;
}
expTypes.add(varRef.type);
checkConstantAssignment(varRef);
}
if (assignNode.getKind() == NodeKind.TUPLE_DESTRUCTURE) {
BTupleType tupleType = new BTupleType(expTypes);
expTypes = Lists.of(tupleType);
}
if (!assignNode.safeAssignment) {
typeChecker.checkExpr(assignNode.expr, this.env, expTypes);
return;
}
// Assume that there is only one variable reference in LHS
// Continue the validate if this is a safe assignment operator
handleSafeAssignment(assignNode.pos, assignNode.varRefs.get(0).type, assignNode.expr, this.env);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.
the class SemanticAnalyzer method checkConstantAssignment.
private void checkConstantAssignment(BLangExpression varRef) {
if (varRef.type == symTable.errType) {
return;
}
if (varRef.getKind() != NodeKind.SIMPLE_VARIABLE_REF) {
return;
}
BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) varRef;
if (simpleVarRef.pkgSymbol != null && simpleVarRef.pkgSymbol.tag == SymTag.XMLNS) {
dlog.error(varRef.pos, DiagnosticCode.XML_QNAME_UPDATE_NOT_ALLOWED);
return;
}
Name varName = names.fromIdNode(simpleVarRef.variableName);
if (!Names.IGNORE.equals(varName) && simpleVarRef.symbol.flags == Flags.CONST && env.enclInvokable != env.enclPkg.initFunction) {
dlog.error(varRef.pos, DiagnosticCode.CANNOT_ASSIGN_VALUE_CONSTANT, varRef);
}
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.
the class TypeChecker method checkInvocationParam.
private List<BType> checkInvocationParam(BLangInvocation iExpr) {
List<BType> paramTypes = ((BInvokableType) iExpr.symbol.type).getParameterTypes();
int requiredParamsCount;
if (iExpr.symbol.tag == SymTag.VARIABLE) {
// Here we assume function pointers can have only required params.
// And assume that named params and rest params are not supported.
requiredParamsCount = paramTypes.size();
} else {
requiredParamsCount = ((BInvokableSymbol) iExpr.symbol).params.size();
}
// Split the different argument types: required args, named args and rest args
int i = 0;
BLangExpression vararg = null;
for (BLangExpression expr : iExpr.argExprs) {
switch(expr.getKind()) {
case NAMED_ARGS_EXPR:
iExpr.namedArgs.add((BLangNamedArgsExpression) expr);
break;
case REST_ARGS_EXPR:
vararg = expr;
break;
default:
if (i < requiredParamsCount) {
iExpr.requiredArgs.add(expr);
} else {
iExpr.restArgs.add(expr);
}
i++;
break;
}
}
return checkInvocationArgs(iExpr, paramTypes, requiredParamsCount, vararg);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.
the class TypeChecker method checkNamedArgs.
private void checkNamedArgs(List<BLangExpression> namedArgExprs, List<BVarSymbol> defaultableParams) {
for (BLangExpression expr : namedArgExprs) {
BLangIdentifier argName = ((NamedArgNode) expr).getName();
BVarSymbol varSym = defaultableParams.stream().filter(param -> param.getName().value.equals(argName.value)).findAny().orElse(null);
if (varSym == null) {
dlog.error(expr.pos, DiagnosticCode.UNDEFINED_PARAMETER, argName);
break;
}
checkExpr(expr, this.env, Lists.of(varSym.type));
}
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.
the class TypeChecker method checkExpr.
public List<BType> checkExpr(BLangExpression expr, SymbolEnv env, List<BType> expTypes, DiagnosticCode diagCode) {
// TODO Check the possibility of using a try/finally here
SymbolEnv prevEnv = this.env;
List<BType> preExpTypes = this.expTypes;
DiagnosticCode preDiagCode = this.diagCode;
this.env = env;
this.diagCode = diagCode;
this.expTypes = expTypes;
expr.accept(this);
setExprType(expr, expTypes);
this.env = prevEnv;
this.expTypes = preExpTypes;
this.diagCode = preDiagCode;
return resultTypes;
}
Aggregations