use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol in project ballerina by ballerina-lang.
the class TypeChecker method visit.
public void visit(BLangTypeInit cIExpr) {
BType actualType;
if (cIExpr.userDefinedType != null) {
actualType = symResolver.resolveTypeNode(cIExpr.userDefinedType, env);
} else {
actualType = expTypes.get(0);
}
if (actualType == symTable.errType) {
// TODO dlog error?
resultTypes = Lists.of(symTable.errType);
return;
}
if (actualType.tag != TypeTags.STRUCT) {
// TODO dlog error?
resultTypes = Lists.of(symTable.errType);
return;
}
cIExpr.objectInitInvocation.symbol = ((BStructSymbol) actualType.tsymbol).initializerFunc.symbol;
checkInvocationParam(cIExpr.objectInitInvocation);
resultTypes = types.checkTypes(cIExpr, Lists.of(actualType), expTypes);
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol in project ballerina by ballerina-lang.
the class Types method checkEquivalencyOfTwoPrivateStructs.
private boolean checkEquivalencyOfTwoPrivateStructs(BStructType lhsType, BStructType rhsType) {
for (int fieldCounter = 0; fieldCounter < lhsType.fields.size(); fieldCounter++) {
BStructField lhsField = lhsType.fields.get(fieldCounter);
BStructField rhsField = rhsType.fields.get(fieldCounter);
if (lhsField.name.equals(rhsField.name) && isSameType(rhsField.type, lhsField.type)) {
continue;
}
return false;
}
BStructSymbol lhsStructSymbol = (BStructSymbol) lhsType.tsymbol;
List<BAttachedFunction> lhsFuncs = lhsStructSymbol.attachedFuncs;
List<BAttachedFunction> rhsFuncs = ((BStructSymbol) rhsType.tsymbol).attachedFuncs;
int lhsAttachedFuncCount = lhsStructSymbol.initializerFunc != null ? lhsFuncs.size() - 1 : lhsFuncs.size();
if (lhsAttachedFuncCount > rhsFuncs.size()) {
return false;
}
for (BAttachedFunction lhsFunc : lhsFuncs) {
if (lhsFunc == lhsStructSymbol.initializerFunc || lhsFunc == lhsStructSymbol.defaultsValuesInitFunc) {
continue;
}
BAttachedFunction rhsFunc = getMatchingInvokableType(rhsFuncs, lhsFunc);
if (rhsFunc == null) {
return false;
}
}
return true;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol in project ballerina by ballerina-lang.
the class Types method checkEquivalencyOfPublicStructs.
private boolean checkEquivalencyOfPublicStructs(BStructType lhsType, BStructType rhsType) {
int fieldCounter = 0;
for (; fieldCounter < lhsType.fields.size(); fieldCounter++) {
BStructField lhsField = lhsType.fields.get(fieldCounter);
BStructField rhsField = rhsType.fields.get(fieldCounter);
if (Symbols.isPrivate(lhsField.symbol) || Symbols.isPrivate(rhsField.symbol)) {
return false;
}
if (lhsField.name.equals(rhsField.name) && isSameType(rhsField.type, lhsField.type)) {
continue;
}
return false;
}
// Check the rest of the fields in RHS type
for (; fieldCounter < rhsType.fields.size(); fieldCounter++) {
if (Symbols.isPrivate(rhsType.fields.get(fieldCounter).symbol)) {
return false;
}
}
BStructSymbol lhsStructSymbol = (BStructSymbol) lhsType.tsymbol;
List<BAttachedFunction> lhsFuncs = lhsStructSymbol.attachedFuncs;
List<BAttachedFunction> rhsFuncs = ((BStructSymbol) rhsType.tsymbol).attachedFuncs;
int lhsAttachedFuncCount = lhsStructSymbol.initializerFunc != null ? lhsFuncs.size() - 1 : lhsFuncs.size();
if (lhsAttachedFuncCount > rhsFuncs.size()) {
return false;
}
for (BAttachedFunction lhsFunc : lhsFuncs) {
if (lhsFunc == lhsStructSymbol.initializerFunc || lhsFunc == lhsStructSymbol.defaultsValuesInitFunc) {
continue;
}
if (Symbols.isPrivate(lhsFunc.symbol)) {
return false;
}
BAttachedFunction rhsFunc = getMatchingInvokableType(rhsFuncs, lhsFunc);
if (rhsFunc == null || Symbols.isPrivate(rhsFunc.symbol)) {
return false;
}
}
// Check for private attached function of the RHS type
for (BAttachedFunction rhsFunc : rhsFuncs) {
if (Symbols.isPrivate(rhsFunc.symbol)) {
return false;
}
}
return true;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol in project ballerina by ballerina-lang.
the class Desugar method getInitExpr.
private BLangExpression getInitExpr(BType type) {
// Don't need to create an empty init expressions if the type allows null.
if (type.isNullable()) {
return null;
}
switch(type.tag) {
case TypeTags.INT:
case TypeTags.FLOAT:
case TypeTags.BOOLEAN:
case TypeTags.STRING:
case TypeTags.BLOB:
// Int, float, boolean, string, blob types will get default values from VM side.
break;
case TypeTags.JSON:
return new BLangJSONLiteral(new ArrayList<>(), type);
case TypeTags.XML:
return new BLangXMLSequenceLiteral(type);
case TypeTags.TABLE:
return new BLangTableLiteral(type);
case TypeTags.STREAM:
return new BLangStreamLiteral(type, null);
case TypeTags.MAP:
return new BLangMapLiteral(new ArrayList<>(), type);
case TypeTags.STRUCT:
if (((BStructSymbol) type.tsymbol).isObject) {
return new BLangTypeInit();
}
return new BLangStructLiteral(new ArrayList<>(), type);
case TypeTags.ARRAY:
BLangArrayLiteral array = new BLangArrayLiteral();
array.exprs = new ArrayList<>();
array.type = type;
return rewriteExpr(array);
default:
break;
}
return null;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol in project ballerina by ballerina-lang.
the class TypeChecker method validateStructInitalizer.
private void validateStructInitalizer(DiagnosticPos pos) {
BStructType bStructType = (BStructType) expTypes.get(0);
BStructSymbol bStructSymbol = (BStructSymbol) bStructType.tsymbol;
if (bStructSymbol.initializerFunc == null) {
return;
}
boolean samePkg = this.env.enclPkg.symbol.pkgID == bStructSymbol.pkgID;
if (!samePkg && Symbols.isPrivate(bStructSymbol.initializerFunc.symbol)) {
dlog.error(pos, DiagnosticCode.ATTEMPT_CREATE_NON_PUBLIC_INITIALIZER);
}
}
Aggregations