use of org.wso2.ballerinalang.compiler.semantics.model.types.BStructType in project ballerina by ballerina-lang.
the class TypeChecker method checkFunctionInvocationExpr.
private void checkFunctionInvocationExpr(BLangInvocation iExpr, BStructType structType) {
String funcName = iExpr.name.value;
Name uniqueFuncName = names.fromString(Symbols.getAttachedFuncSymbolName(structType.tsymbol.name.value, funcName));
BPackageSymbol packageSymbol = (BPackageSymbol) structType.tsymbol.owner;
BSymbol funcSymbol = symResolver.lookupMemberSymbol(iExpr.pos, packageSymbol.scope, this.env, uniqueFuncName, SymTag.FUNCTION);
if (funcSymbol == symTable.notFoundSymbol) {
// Check functions defined within the struct.
Name functionName = names.fromString(Symbols.getAttachedFuncSymbolName(iExpr.expr.symbol.type.tsymbol.name.value, iExpr.name.value));
funcSymbol = symResolver.resolveStructField(iExpr.pos, env, functionName, iExpr.expr.symbol.type.tsymbol);
if (funcSymbol == symTable.notFoundSymbol) {
// Check, any function pointer in struct field with given name.
funcSymbol = symResolver.resolveStructField(iExpr.pos, env, names.fromIdNode(iExpr.name), iExpr.expr.symbol.type.tsymbol);
if (funcSymbol == symTable.notFoundSymbol || funcSymbol.type.tag != TypeTags.INVOKABLE) {
dlog.error(iExpr.pos, DiagnosticCode.UNDEFINED_FUNCTION_IN_STRUCT, funcName, structType);
resultTypes = getListWithErrorTypes(expTypes.size());
return;
}
if ((funcSymbol.flags & Flags.ATTACHED) != Flags.ATTACHED) {
iExpr.functionPointerInvocation = true;
}
}
} else {
// Attached function found
// Check for the explicit initializer function invocation
BStructSymbol.BAttachedFunction initializerFunc = ((BStructSymbol) structType.tsymbol).initializerFunc;
if (initializerFunc != null && initializerFunc.funcName.value.equals(funcName)) {
dlog.error(iExpr.pos, DiagnosticCode.STRUCT_INITIALIZER_INVOKED, structType.tsymbol.toString());
}
}
iExpr.symbol = funcSymbol;
checkInvocationParamAndReturnType(iExpr);
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BStructType 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.types.BStructType 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.types.BStructType in project ballerina by ballerina-lang.
the class Desugar method getReturnType.
private BLangStructLiteral getReturnType(BLangTableQueryExpression tableQueryExpression) {
// create a literal to represent the sql query.
BTableType tableType = (BTableType) tableQueryExpression.type;
BStructType structType = (BStructType) tableType.constraint;
return new BLangStructLiteral(new ArrayList<>(), structType);
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BStructType in project ballerina by ballerina-lang.
the class BLangMatchContextResolver method resolveItems.
@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
ArrayList<CompletionItem> completionItems = new ArrayList<>();
BLangNode symbolEnvNode = completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY);
List<SymbolInfo> visibleSymbols = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
if (!(symbolEnvNode instanceof BLangMatch)) {
return completionItems;
}
BLangMatch bLangMatch = (BLangMatch) symbolEnvNode;
if (bLangMatch.expr.type instanceof BUnionType) {
Set<BType> memberTypes = ((BUnionType) ((BLangSimpleVarRef) bLangMatch.expr).type).getMemberTypes();
memberTypes.forEach(bType -> {
completionItems.add(this.populateCompletionItem(bType.toString(), ItemResolverConstants.B_TYPE, bType.toString()));
});
} else if (bLangMatch.expr.type instanceof BJSONType) {
ArrayList<Integer> typeTagsList = new ArrayList<>(Arrays.asList(TypeTags.INT, TypeTags.FLOAT, TypeTags.BOOLEAN, TypeTags.STRING, TypeTags.NULL, TypeTags.JSON));
List<SymbolInfo> filteredBasicTypes = visibleSymbols.stream().filter(symbolInfo -> {
BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
return bSymbol instanceof BTypeSymbol && typeTagsList.contains(bSymbol.getType().tag);
}).collect(Collectors.toList());
this.populateCompletionItemList(filteredBasicTypes, completionItems);
} else {
if (bLangMatch.expr.type instanceof BStructType) {
List<SymbolInfo> structSymbols = visibleSymbols.stream().filter(symbolInfo -> {
BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
return bSymbol instanceof BStructSymbol && !bSymbol.getName().getValue().startsWith(UtilSymbolKeys.ANON_STRUCT_CHECKER);
}).collect(Collectors.toList());
this.populateCompletionItemList(structSymbols, completionItems);
}
}
return completionItems;
}
Aggregations