use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.
the class Types method isArrayTypesAssignable.
public boolean isArrayTypesAssignable(BType source, BType target) {
if (target.tag == TypeTags.ARRAY && source.tag == TypeTags.ARRAY) {
// Both types are array types
BArrayType lhsArrayType = (BArrayType) target;
BArrayType rhsArrayType = (BArrayType) source;
return isArrayTypesAssignable(rhsArrayType.eType, lhsArrayType.eType);
} else if (source.tag == TypeTags.ARRAY) {
// to JSON.
if (target.tag == TypeTags.JSON) {
return getElementType(source).tag == TypeTags.JSON;
}
// Then lhs type should 'any' type
return target.tag == TypeTags.ANY;
} else if (target.tag == TypeTags.ARRAY) {
// Only the left-hand side is an array type
return false;
}
// Now both types are not array types and they have to be equal
if (target == source) {
// TODO Figure out this.
return true;
}
// In this case, lhs type should be of type 'any' and the rhs type cannot be a value type
return target.tag == TypeTags.ANY && !isValueType(source);
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
@Override
public void visit(BLangArrayLiteral arrayLiteral) {
BType etype;
if (arrayLiteral.type.tag == TypeTags.ANY) {
etype = arrayLiteral.type;
} else {
etype = ((BArrayType) arrayLiteral.type).eType;
}
// Emit create array instruction
int opcode = getOpcode(etype.tag, InstructionCodes.INEWARRAY);
Operand arrayVarRegIndex = calcAndGetExprRegIndex(arrayLiteral);
Operand typeCPIndex = getTypeCPIndex(arrayLiteral.type);
emit(opcode, arrayVarRegIndex, typeCPIndex);
// Emit instructions populate initial array values;
for (int i = 0; i < arrayLiteral.exprs.size(); i++) {
BLangExpression argExpr = arrayLiteral.exprs.get(i);
genNode(argExpr, this.env);
BLangLiteral indexLiteral = new BLangLiteral();
indexLiteral.pos = arrayLiteral.pos;
indexLiteral.value = (long) i;
indexLiteral.type = symTable.intType;
genNode(indexLiteral, this.env);
opcode = getOpcode(argExpr.type.tag, InstructionCodes.IASTORE);
emit(opcode, arrayVarRegIndex, indexLiteral.regIndex, argExpr.regIndex);
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.
the class CodeGenerator method getMainFunction.
private static BLangFunction getMainFunction(BLangPackage pkgNode) {
List<BLangFunction> functions = pkgNode.functions.stream().filter(f -> (f.name.value.equals(MAIN_FUNCTION_NAME) && f.symbol.params.size() == 1 && f.symbol.retParams.size() == 0)).collect(Collectors.toList());
if (functions.isEmpty()) {
return null;
}
for (BLangFunction f : functions) {
BType paramType = f.symbol.params.get(0).type;
if (paramType.tag != TypeTags.ARRAY) {
continue;
}
BArrayType arrayType = (BArrayType) paramType;
if (arrayType.eType.tag == TypeTags.STRING) {
return f;
}
}
return null;
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.
the class TestAnnotationProcessor method resolveFunctions.
/**
* Resolve function names to {@link TesterinaFunction}s.
*
* @param suite {@link TestSuite} whose functions to be resolved.
*/
private static void resolveFunctions(TestSuite suite) {
List<TesterinaFunction> functions = suite.getTestUtilityFunctions();
List<String> functionNames = functions.stream().map(testerinaFunction -> testerinaFunction.getName()).collect(Collectors.toList());
for (Test test : suite.getTests()) {
if (test.getTestName() != null && functionNames.contains(test.getTestName())) {
test.setTestFunction(functions.stream().filter(e -> e.getName().equals(test.getTestName())).findFirst().get());
}
if (test.getBeforeTestFunction() != null && functionNames.contains(test.getBeforeTestFunction())) {
test.setBeforeTestFunctionObj(functions.stream().filter(e -> e.getName().equals(test.getBeforeTestFunction())).findFirst().get());
}
if (test.getAfterTestFunction() != null && functionNames.contains(test.getAfterTestFunction())) {
test.setAfterTestFunctionObj(functions.stream().filter(e -> e.getName().equals(test.getAfterTestFunction())).findFirst().get());
}
if (test.getDataProvider() != null && functionNames.contains(test.getDataProvider())) {
String dataProvider = test.getDataProvider();
test.setDataProviderFunction(functions.stream().filter(e -> e.getName().equals(test.getDataProvider())).findFirst().map(func -> {
// TODO these validations are not working properly with the latest refactoring
if (func.getbFunction().getRetParamTypes().length == 1) {
BType bType = func.getbFunction().getRetParamTypes()[0];
if (bType.getTag() == TypeTags.ARRAY_TAG) {
BArrayType bArrayType = (BArrayType) bType;
if (bArrayType.getElementType().getTag() != TypeTags.ARRAY_TAG) {
String message = String.format("Data provider function [%s] should return an array of" + " arrays.", dataProvider);
throw new BallerinaException(message);
}
} else {
String message = String.format("Data provider function [%s] should return an array of " + "arrays.", dataProvider);
throw new BallerinaException(message);
}
} else {
String message = String.format("Data provider function [%s] should have only one return type" + ".", dataProvider);
throw new BallerinaException(message);
}
return func;
}).get());
if (test.getDataProviderFunction() == null) {
String message = String.format("Data provider function [%s] cannot be found.", dataProvider);
throw new BallerinaException(message);
}
}
for (String dependsOnFn : test.getDependsOnTestFunctions()) {
// TODO handle missing func case
test.addDependsOnTestFunction(functions.stream().filter(e -> e.getName().equals(dependsOnFn)).findFirst().get());
}
}
// resolve mock functions
suite.getMockFunctionNamesMap().forEach((id, functionName) -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addMockFunctionObj(id, function);
});
suite.getBeforeSuiteFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addBeforeSuiteFunctionObj(function);
});
suite.getAfterSuiteFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addAfterSuiteFunctionObj(function);
});
suite.getBeforeEachFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addBeforeEachFunctionObj(function);
});
suite.getAfterEachFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addAfterEachFunctionObj(function);
});
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType in project ballerina by ballerina-lang.
the class PackageActionFunctionAndTypesFilter method invocationsAndFieldsOnIdentifier.
/**
* Get the invocations and fields against an identifier (functions, struct fields and types including the enums).
* @param context Text Document Service context (Completion Context)
* @param delimiterIndex delimiter index (index of either . or :)
* @return {@link ArrayList} List of filtered symbol info
*/
private ArrayList<SymbolInfo> invocationsAndFieldsOnIdentifier(TextDocumentServiceContext context, int delimiterIndex) {
ArrayList<SymbolInfo> actionFunctionList = new ArrayList<>();
TokenStream tokenStream = context.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
List<SymbolInfo> symbols = context.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
SymbolTable symbolTable = context.get(DocumentServiceKeys.SYMBOL_TABLE_KEY);
String variableName = CommonUtil.getPreviousDefaultToken(tokenStream, delimiterIndex).getText();
SymbolInfo variable = this.getVariableByName(variableName, symbols);
String builtinPkgName = symbolTable.builtInPackageSymbol.name.getValue();
Map<Name, Scope.ScopeEntry> entries = new HashMap<>();
String currentPkgName = context.get(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY);
if (variable == null) {
return actionFunctionList;
}
String packageID;
BType bType = variable.getScopeEntry().symbol.getType();
String bTypeValue;
if (variable.getScopeEntry().symbol instanceof BEndpointVarSymbol) {
BType getClientFuncType = ((BEndpointVarSymbol) variable.getScopeEntry().symbol).getClientFunction.type;
if (!UtilSymbolKeys.ACTION_INVOCATION_SYMBOL_KEY.equals(tokenStream.get(delimiterIndex).getText()) || !(getClientFuncType instanceof BInvokableType)) {
return actionFunctionList;
}
BType boundType = ((BInvokableType) getClientFuncType).retTypes.get(0);
packageID = boundType.tsymbol.pkgID.toString();
bTypeValue = boundType.toString();
} else if (bType instanceof BArrayType) {
packageID = ((BArrayType) bType).eType.tsymbol.pkgID.toString();
bTypeValue = bType.toString();
} else {
packageID = bType.tsymbol.pkgID.toString();
bTypeValue = bType.toString();
}
// Extract the package symbol. This is used to extract the entries of the particular package
SymbolInfo packageSymbolInfo = symbols.stream().filter(item -> {
Scope.ScopeEntry scopeEntry = item.getScopeEntry();
return (scopeEntry.symbol instanceof BPackageSymbol) && scopeEntry.symbol.pkgID.toString().equals(packageID);
}).findFirst().orElse(null);
if (packageSymbolInfo == null && packageID.equals(builtinPkgName)) {
// If the packageID is ballerina.builtin, we extract entries of builtin package
entries = symbolTable.builtInPackageSymbol.scope.entries;
} else if (packageSymbolInfo == null && packageID.equals(currentPkgName)) {
entries = this.getScopeEntries(bType, context);
} else if (packageSymbolInfo != null) {
// If the package exist, we extract particular entries from package
entries = packageSymbolInfo.getScopeEntry().symbol.scope.entries;
}
entries.forEach((name, scopeEntry) -> {
if (scopeEntry.symbol instanceof BInvokableSymbol && ((BInvokableSymbol) scopeEntry.symbol).receiverSymbol != null) {
String symbolBoundedName = ((BInvokableSymbol) scopeEntry.symbol).receiverSymbol.getType().toString();
if (symbolBoundedName.equals(bTypeValue)) {
// TODO: Need to handle the name in a proper manner
String[] nameComponents = name.toString().split("\\.");
SymbolInfo actionFunctionSymbol = new SymbolInfo(nameComponents[nameComponents.length - 1], scopeEntry);
actionFunctionList.add(actionFunctionSymbol);
}
} else if ((scopeEntry.symbol instanceof BTypeSymbol) && bTypeValue.equals(scopeEntry.symbol.type.toString())) {
// Get the struct fields
Map<Name, Scope.ScopeEntry> fields = scopeEntry.symbol.scope.entries;
fields.forEach((fieldName, fieldScopeEntry) -> {
actionFunctionList.add(new SymbolInfo(fieldName.getValue(), fieldScopeEntry));
});
}
});
// Populate possible iterable operators over the variable
populateIterableOperations(variable, actionFunctionList);
return actionFunctionList;
}
Aggregations