Search in sources :

Example 1 with BTypeSymbol

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;
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) SymbolInformation(org.ballerinalang.composer.service.ballerina.parser.service.model.SymbolInformation)

Example 2 with BTypeSymbol

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);
}
Also used : BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)

Example 3 with BTypeSymbol

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();
}
Also used : StructInfo(org.wso2.ballerinalang.programfile.StructInfo) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) ArrayList(java.util.ArrayList) BLangCatch(org.wso2.ballerinalang.compiler.tree.statements.BLangCatch) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) Instruction(org.wso2.ballerinalang.programfile.Instruction) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ErrorTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo) StructureRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry) ErrorTableEntry(org.wso2.ballerinalang.programfile.ErrorTableEntry)

Example 4 with BTypeSymbol

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);
    }
}
Also used : EnumInfo(org.wso2.ballerinalang.programfile.EnumInfo) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BLangEnumerator(org.wso2.ballerinalang.compiler.tree.BLangEnum.BLangEnumerator) EnumeratorInfo(org.wso2.ballerinalang.programfile.EnumeratorInfo) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 5 with BTypeSymbol

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;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) ArrayList(java.util.ArrayList) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) CompletionItem(org.eclipse.lsp4j.CompletionItem) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)13 ArrayList (java.util.ArrayList)7 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)6 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)5 List (java.util.List)4 CompletionItem (org.eclipse.lsp4j.CompletionItem)4 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)4 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)4 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)4 Collectors (java.util.stream.Collectors)3 TokenStream (org.antlr.v4.runtime.TokenStream)3 DocumentServiceKeys (org.ballerinalang.langserver.DocumentServiceKeys)3 TextDocumentServiceContext (org.ballerinalang.langserver.TextDocumentServiceContext)3 CompletionKeys (org.ballerinalang.langserver.completions.CompletionKeys)3 ItemResolverConstants (org.ballerinalang.langserver.completions.util.ItemResolverConstants)3 BJSONType (org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType)3 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)3 Arrays (java.util.Arrays)2 Token (org.antlr.v4.runtime.Token)2 UtilSymbolKeys (org.ballerinalang.langserver.common.UtilSymbolKeys)2