use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol in project ballerina by ballerina-lang.
the class ParserUtils method getBuiltinTypes.
/**
* Get the builtin types.
*
* @return {@link List} list of builtin types
*/
public static List<SymbolInformation> getBuiltinTypes() {
CompilerContext context = prepareCompilerContext("", "");
SymbolTable symbolTable = SymbolTable.getInstance(context);
List<SymbolInformation> symbolInformationList = new ArrayList<>();
// TODO: Need to fill the default values
symbolTable.rootScope.entries.forEach((key, value) -> {
if (value.symbol instanceof BTypeSymbol) {
SymbolInformation symbolInfo = new SymbolInformation();
String symbolName = value.symbol.getName().getValue();
if (!symbolName.equals(BuiltInType.INVALID_TYPE)) {
symbolInfo.setName(symbolName);
setDefaultValuesForType(symbolName, symbolInfo);
symbolInformationList.add(symbolInfo);
}
}
});
return symbolInformationList;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol in project ballerina by ballerina-lang.
the class SymbolEnter method defineAttachedFunctions.
private void defineAttachedFunctions(BLangFunction funcNode, BInvokableSymbol funcSymbol, SymbolEnv invokableEnv, boolean isValidAttachedFunc) {
BInvokableType funcType = (BInvokableType) funcSymbol.type;
BTypeSymbol typeSymbol = funcNode.receiver.type.tsymbol;
// Check whether there exists a struct field with the same name as the function name.
if (isValidAttachedFunc) {
if (typeSymbol.tag == SymTag.STRUCT) {
validateFunctionsAttachedToStructs(funcNode, funcSymbol, invokableEnv);
} else if (typeSymbol.tag == SymTag.OBJECT) {
validateFunctionsAttachedToObject(funcNode, funcSymbol, invokableEnv);
}
}
defineNode(funcNode.receiver, invokableEnv);
funcSymbol.receiverSymbol = funcNode.receiver.symbol;
funcType.setReceiverType(funcNode.receiver.symbol.type);
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
public void visit(BLangTryCatchFinally tryNode) {
Operand gotoTryCatchEndAddr = getOperand(-1);
Instruction instructGotoTryCatchEnd = InstructionFactory.get(InstructionCodes.GOTO, gotoTryCatchEndAddr);
List<int[]> unhandledErrorRangeList = new ArrayList<>();
ErrorTableAttributeInfo errorTable = createErrorTableIfAbsent(currentPkgInfo);
// Handle try block.
int fromIP = nextIP();
genNode(tryNode.tryBody, env);
int toIP = nextIP() - 1;
// Append finally block instructions.
if (tryNode.finallyBody != null) {
genNode(tryNode.finallyBody, env);
}
emit(instructGotoTryCatchEnd);
unhandledErrorRangeList.add(new int[] { fromIP, toIP });
// Handle catch blocks.
int order = 0;
for (BLangCatch bLangCatch : tryNode.getCatchBlocks()) {
addLineNumberInfo(bLangCatch.pos);
int targetIP = nextIP();
genNode(bLangCatch, env);
unhandledErrorRangeList.add(new int[] { targetIP, nextIP() - 1 });
// Append finally block instructions.
if (tryNode.finallyBody != null) {
genNode(tryNode.finallyBody, env);
}
emit(instructGotoTryCatchEnd);
// Create Error table entry for this catch block
BTypeSymbol structSymbol = bLangCatch.param.symbol.type.tsymbol;
BPackageSymbol packageSymbol = (BPackageSymbol) bLangCatch.param.symbol.type.tsymbol.owner;
int pkgCPIndex = addPackageRefCPEntry(currentPkgInfo, packageSymbol.pkgID);
int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, structNameCPIndex);
int structCPEntryIndex = currentPkgInfo.addCPEntry(structureRefCPEntry);
StructInfo errorStructInfo = this.programFile.packageInfoMap.get(packageSymbol.pkgID.bvmAlias()).getStructInfo(structSymbol.name.value);
ErrorTableEntry errorTableEntry = new ErrorTableEntry(fromIP, toIP, targetIP, order++, structCPEntryIndex);
errorTableEntry.setError(errorStructInfo);
errorTable.addErrorTableEntry(errorTableEntry);
}
if (tryNode.finallyBody != null) {
// Create Error table entry for unhandled errors in try and catch(s) blocks
for (int[] range : unhandledErrorRangeList) {
ErrorTableEntry errorTableEntry = new ErrorTableEntry(range[0], range[1], nextIP(), order++, -1);
errorTable.addErrorTableEntry(errorTableEntry);
}
// Append finally block instruction.
genNode(tryNode.finallyBody, env);
emit(InstructionFactory.get(InstructionCodes.THROW, getOperand(-1)));
}
gotoTryCatchEndAddr.value = nextIP();
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol in project ballerina by ballerina-lang.
the class CodeGenerator method createEnumInfoEntry.
private void createEnumInfoEntry(BLangEnum enumNode) {
BTypeSymbol enumSymbol = (BTypeSymbol) enumNode.symbol;
// Add Enum name as an UTFCPEntry to the constant pool
int enumNameCPIndex = addUTF8CPEntry(currentPkgInfo, enumSymbol.name.value);
EnumInfo enumInfo = new EnumInfo(currentPackageRefCPIndex, enumNameCPIndex, enumSymbol.flags);
currentPkgInfo.addEnumInfo(enumSymbol.name.value, enumInfo);
enumInfo.enumType = (BEnumType) enumSymbol.type;
for (int i = 0; i < enumNode.enumerators.size(); i++) {
BLangEnumerator enumeratorNode = enumNode.enumerators.get(i);
enumeratorNode.symbol.varIndex = new RegIndex(i, enumSymbol.type.tag);
enumeratorNode.symbol.varIndex.isVarIndex = true;
int enumeratorNameCPIndex = addUTF8CPEntry(currentPkgInfo, enumeratorNode.symbol.name.toString());
EnumeratorInfo enumeratorInfo = new EnumeratorInfo(enumeratorNameCPIndex, i, enumInfo.enumType);
enumInfo.enumeratorInfoList.add(enumeratorInfo);
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol 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