Search in sources :

Example 26 with BVarSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol in project ballerina by ballerina-lang.

the class AssignmentStmtContextSorter method getVariableType.

/**
 * Get the variable type.
 *
 * @param ctx Document Service context (Completion context)
 * @return {@link String} type of the variable
 */
@Override
String getVariableType(TextDocumentServiceContext ctx) {
    String variableName = ctx.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY).getStart().getText();
    List<SymbolInfo> visibleSymbols = ctx.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
    SymbolInfo filteredSymbol = visibleSymbols.stream().filter(symbolInfo -> {
        BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
        String symbolName = symbolInfo.getSymbolName();
        return bSymbol instanceof BVarSymbol && symbolName.equals(variableName);
    }).findFirst().orElse(null);
    if (filteredSymbol != null) {
        return filteredSymbol.getScopeEntry().symbol.type.toString();
    }
    return "";
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo)

Example 27 with BVarSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol in project ballerina by ballerina-lang.

the class SymbolEnter method defineVarSymbol.

public BVarSymbol defineVarSymbol(DiagnosticPos pos, Set<Flag> flagSet, BType varType, Name varName, SymbolEnv env) {
    // Create variable symbol
    Scope enclScope = env.scope;
    BVarSymbol varSymbol;
    if (varType.tag == TypeTags.INVOKABLE) {
        varSymbol = new BInvokableSymbol(SymTag.VARIABLE, Flags.asMask(flagSet), varName, env.enclPkg.symbol.pkgID, varType, enclScope.owner);
    } else {
        varSymbol = new BVarSymbol(Flags.asMask(flagSet), varName, env.enclPkg.symbol.pkgID, varType, enclScope.owner);
    }
    // Find duplicates
    if (!symResolver.checkForUniqueSymbol(pos, env, varSymbol, SymTag.VARIABLE_NAME)) {
        varSymbol.type = symTable.errType;
    }
    enclScope.define(varSymbol.name, varSymbol);
    return varSymbol;
}
Also used : Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Example 28 with BVarSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol in project ballerina by ballerina-lang.

the class SymbolEnter method createAssignmentStmt.

private StatementNode createAssignmentStmt(BLangVariable variable, BVarSymbol varSym, BVarSymbol fieldVar) {
    // Create LHS reference variable
    BLangSimpleVarRef varRef = (BLangSimpleVarRef) TreeBuilder.createSimpleVariableReferenceNode();
    varRef.pos = variable.pos;
    varRef.variableName = (BLangIdentifier) createIdentifier(fieldVar.name.getValue());
    varRef.pkgAlias = (BLangIdentifier) TreeBuilder.createIdentifierNode();
    // Create RHS variable reference
    BLangSimpleVarRef exprVar = (BLangSimpleVarRef) TreeBuilder.createSimpleVariableReferenceNode();
    exprVar.pos = variable.pos;
    exprVar.variableName = (BLangIdentifier) createIdentifier(varSym.name.getValue());
    exprVar.pkgAlias = (BLangIdentifier) TreeBuilder.createIdentifierNode();
    // Create assignment statement
    BLangAssignment assignmentStmt = (BLangAssignment) TreeBuilder.createAssignmentNode();
    assignmentStmt.expr = exprVar;
    assignmentStmt.pos = variable.pos;
    assignmentStmt.addVariable(varRef);
    return assignmentStmt;
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)

Example 29 with BVarSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol in project ballerina by ballerina-lang.

the class SymbolEnter method visit.

@Override
public void visit(BLangVariable varNode) {
    // this is a field variable defined for object init function
    if (varNode.isField) {
        Name varName = names.fromIdNode(varNode.name);
        BVarSymbol fieldVar = (BVarSymbol) symResolver.resolveObjectField(varNode.pos, env, varName, env.enclObject.symbol.type.tsymbol);
        varNode.type = fieldVar.type;
        varName = getFieldSymbolName(((BLangFunction) env.enclInvokable).receiver, varNode);
        BVarSymbol varSymbol = defineVarSymbol(varNode.pos, varNode.flagSet, varNode.type, varName, env);
        env.enclObject.initFunction.initFunctionStmts.put(fieldVar, (BLangStatement) createAssignmentStmt(varNode, varSymbol, fieldVar));
        varSymbol.docTag = varNode.docTag;
        varNode.symbol = varSymbol;
        return;
    }
    // assign the type to var type node
    if (varNode.type == null) {
        varNode.type = symResolver.resolveTypeNode(varNode.typeNode, env);
    }
    Name varName = names.fromIdNode(varNode.name);
    if (varName == Names.EMPTY || varName == Names.IGNORE) {
        // e.g. function foo() (int);
        return;
    }
    BVarSymbol varSymbol = defineVarSymbol(varNode.pos, varNode.flagSet, varNode.type, varName, env);
    varSymbol.docTag = varNode.docTag;
    varNode.symbol = varSymbol;
}
Also used : BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 30 with BVarSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol in project ballerina by ballerina-lang.

the class SymbolEnter method visit.

@Override
public void visit(BLangEnum enumNode) {
    BTypeSymbol enumSymbol = Symbols.createEnumSymbol(Flags.asMask(enumNode.flagSet), names.fromIdNode(enumNode.name), env.enclPkg.symbol.pkgID, null, env.scope.owner);
    enumNode.symbol = enumSymbol;
    defineSymbol(enumNode.pos, enumSymbol);
    BEnumType enumType = new BEnumType(enumSymbol, null);
    enumSymbol.type = enumType;
    SymbolEnv enumEnv = SymbolEnv.createPkgLevelSymbolEnv(enumNode, enumSymbol.scope, this.env);
    for (int i = 0; i < enumNode.enumerators.size(); i++) {
        BLangEnumerator enumerator = enumNode.enumerators.get(i);
        BVarSymbol enumeratorSymbol = new BVarSymbol(Flags.PUBLIC, names.fromIdNode(enumerator.name), enumSymbol.pkgID, enumType, enumSymbol);
        enumeratorSymbol.docTag = DocTag.FIELD;
        enumerator.symbol = enumeratorSymbol;
        if (symResolver.checkForUniqueSymbol(enumerator.pos, enumEnv, enumeratorSymbol, enumeratorSymbol.tag)) {
            enumEnv.scope.define(enumeratorSymbol.name, enumeratorSymbol);
        }
    }
}
Also used : BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangEnumerator(org.wso2.ballerinalang.compiler.tree.BLangEnum.BLangEnumerator) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType)

Aggregations

BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)29 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)16 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)15 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)13 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)12 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)12 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)11 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)10 Name (org.wso2.ballerinalang.compiler.util.Name)10 ArrayList (java.util.ArrayList)9 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)9 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)8 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)8 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)8 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)7 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)7 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)7 BLangIdentifier (org.wso2.ballerinalang.compiler.tree.BLangIdentifier)6 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)6 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)6