Search in sources :

Example 1 with BInvokableType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType in project ballerina by ballerina-lang.

the class SymbolEnter method validateFunctionsAttachedToStructs.

private void validateFunctionsAttachedToStructs(BLangFunction funcNode, BInvokableSymbol funcSymbol, SymbolEnv invokableEnv) {
    BInvokableType funcType = (BInvokableType) funcSymbol.type;
    BStructSymbol structSymbol = (BStructSymbol) funcNode.receiver.type.tsymbol;
    BSymbol symbol = symResolver.lookupMemberSymbol(funcNode.receiver.pos, structSymbol.scope, invokableEnv, names.fromIdNode(funcNode.name), SymTag.VARIABLE);
    if (symbol != symTable.notFoundSymbol) {
        dlog.error(funcNode.pos, DiagnosticCode.STRUCT_FIELD_AND_FUNC_WITH_SAME_NAME, funcNode.name.value, funcNode.receiver.type.toString());
        return;
    }
    BStructType structType = (BStructType) funcNode.receiver.type;
    BAttachedFunction attachedFunc = new BAttachedFunction(names.fromIdNode(funcNode.name), funcSymbol, funcType);
    structSymbol.attachedFuncs.add(attachedFunc);
    if (funcNode.name.value.equals(structType.tsymbol.name.value + Names.INIT_FUNCTION_SUFFIX.value)) {
        structSymbol.defaultsValuesInitFunc = attachedFunc;
        return;
    }
    // Check whether this attached function is a struct initializer.
    if (!structType.tsymbol.name.value.equals(funcNode.name.value)) {
        // Not a struct initializer.
        return;
    }
    if (!funcNode.requiredParams.isEmpty() || !funcNode.retParams.isEmpty()) {
        dlog.error(funcNode.pos, DiagnosticCode.INVALID_STRUCT_INITIALIZER_FUNCTION, funcNode.name.value, funcNode.receiver.type.toString());
    }
    structSymbol.initializerFunc = attachedFunc;
}
Also used : BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Example 2 with BInvokableType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType in project ballerina by ballerina-lang.

the class SymbolEnter method defineInvokableSymbolParams.

private void defineInvokableSymbolParams(BLangInvokableNode invokableNode, BInvokableSymbol symbol, SymbolEnv invokableEnv) {
    List<BVarSymbol> paramSymbols = invokableNode.requiredParams.stream().peek(varNode -> defineNode(varNode, invokableEnv)).map(varNode -> varNode.symbol).collect(Collectors.toList());
    List<BVarSymbol> namedParamSymbols = invokableNode.defaultableParams.stream().peek(varDefNode -> defineNode(varDefNode.var, invokableEnv)).map(varDefNode -> varDefNode.var.symbol).collect(Collectors.toList());
    List<BVarSymbol> retParamSymbols = invokableNode.retParams.stream().peek(varNode -> defineNode(varNode, invokableEnv)).filter(varNode -> varNode.symbol != null).map(varNode -> varNode.symbol).collect(Collectors.toList());
    symbol.params = paramSymbols;
    symbol.retParams = retParamSymbols;
    symbol.defaultableParams = namedParamSymbols;
    // Create function type
    List<BType> paramTypes = paramSymbols.stream().map(paramSym -> paramSym.type).collect(Collectors.toList());
    namedParamSymbols.forEach(paramSymbol -> paramTypes.add(paramSymbol.type));
    if (invokableNode.restParam != null) {
        defineNode(invokableNode.restParam, invokableEnv);
        symbol.restParam = invokableNode.restParam.symbol;
        paramTypes.add(symbol.restParam.type);
    }
    List<BType> retTypes = invokableNode.retParams.stream().map(varNode -> varNode.type != null ? varNode.type : varNode.typeNode.type).collect(Collectors.toList());
    symbol.type = new BInvokableType(paramTypes, retTypes, null);
}
Also used : BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) BConnectorType(org.wso2.ballerinalang.compiler.semantics.model.types.BConnectorType) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) Flag(org.ballerinalang.model.elements.Flag) IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) BAnnotationSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol) Names(org.wso2.ballerinalang.compiler.util.Names) BServiceSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BServiceSymbol) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType) EnumSet(java.util.EnumSet) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangDiagnosticLog(org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLog) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) BConnectorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConnectorSymbol) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) BXMLAttributeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLAttributeSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) PackageID(org.ballerinalang.model.elements.PackageID) Set(java.util.Set) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) BLangXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS) BLangAnnotAttribute(org.wso2.ballerinalang.compiler.tree.BLangAnnotAttribute) Collectors(java.util.stream.Collectors) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) List(java.util.List) IMPORT(org.ballerinalang.model.tree.NodeKind.IMPORT) Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) NodeKind(org.ballerinalang.model.tree.NodeKind) BAnnotationAttributeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationAttributeSymbol) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangWorker(org.wso2.ballerinalang.compiler.tree.BLangWorker) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangXMLNSStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangXMLNSStatement) BLangInvokableNode(org.wso2.ballerinalang.compiler.tree.BLangInvokableNode) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) BStructField(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType.BStructField) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) BAnnotationType(org.wso2.ballerinalang.compiler.semantics.model.types.BAnnotationType) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) BLangXMLAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute) BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) BLangNodeVisitor(org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor) PackageLoader(org.wso2.ballerinalang.compiler.PackageLoader) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) BServiceType(org.wso2.ballerinalang.compiler.semantics.model.types.BServiceType) Flags(org.wso2.ballerinalang.util.Flags) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) DocTag(org.ballerinalang.model.elements.DocTag) TreeBuilder(org.ballerinalang.model.TreeBuilder) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) XMLConstants(javax.xml.XMLConstants) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BTransformerSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTransformerSymbol) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Name(org.wso2.ballerinalang.compiler.util.Name) SymTag(org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag) DiagnosticCode(org.ballerinalang.util.diagnostic.DiagnosticCode) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangEnumerator(org.wso2.ballerinalang.compiler.tree.BLangEnum.BLangEnumerator) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Example 3 with BInvokableType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType in project ballerina by ballerina-lang.

the class SymbolEnter method visit.

@Override
public void visit(BLangAction actionNode) {
    BInvokableSymbol actionSymbol = Symbols.createActionSymbol(Flags.asMask(actionNode.flagSet), names.fromIdNode(actionNode.name), env.enclPkg.symbol.pkgID, null, env.scope.owner);
    SymbolEnv invokableEnv = SymbolEnv.createResourceActionSymbolEnv(actionNode, actionSymbol.scope, env);
    defineInvokableSymbol(actionNode, actionSymbol, invokableEnv);
    actionNode.endpoints.forEach(ep -> defineNode(ep, invokableEnv));
    // TODO check below as it create a new symbol for the connector
    BVarSymbol varSymbol = new BVarSymbol(Flags.asMask(EnumSet.noneOf(Flag.class)), names.fromIdNode((BLangIdentifier) createIdentifier(Names.CONNECTOR.getValue())), env.enclPkg.symbol.pkgID, actionSymbol.owner.type, invokableEnv.scope.owner);
    actionSymbol.receiverSymbol = varSymbol;
    ((BInvokableType) actionSymbol.type).setReceiverType(varSymbol.type);
}
Also used : BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Example 4 with BInvokableType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType 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 5 with BInvokableType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType in project ballerina by ballerina-lang.

the class SymbolEnter method validateFunctionsAttachedToObject.

private void validateFunctionsAttachedToObject(BLangFunction funcNode, BInvokableSymbol funcSymbol, SymbolEnv invokableEnv) {
    BInvokableType funcType = (BInvokableType) funcSymbol.type;
    BStructSymbol objectSymbol = (BStructSymbol) funcNode.receiver.type.tsymbol;
    BSymbol symbol = symResolver.lookupMemberSymbol(funcNode.receiver.pos, objectSymbol.scope, invokableEnv, names.fromIdNode(funcNode.name), SymTag.VARIABLE);
    if (symbol != symTable.notFoundSymbol) {
        dlog.error(funcNode.pos, DiagnosticCode.STRUCT_FIELD_AND_FUNC_WITH_SAME_NAME, funcNode.name.value, funcNode.receiver.type.toString());
        return;
    }
    BAttachedFunction attachedFunc = new BAttachedFunction(names.fromIdNode(funcNode.name), funcSymbol, funcType);
    objectSymbol.attachedFuncs.add(attachedFunc);
    // Check whether this attached function is a object initializer.
    if (!Names.OBJECT_INIT_SUFFIX.value.equals(funcNode.name.value)) {
        // Not a object initializer.
        return;
    }
    if (!funcNode.retParams.isEmpty()) {
        // TODO change message
        dlog.error(funcNode.pos, DiagnosticCode.INVALID_STRUCT_INITIALIZER_FUNCTION, funcNode.name.value, funcNode.receiver.type.toString());
    }
    objectSymbol.initializerFunc = attachedFunc;
}
Also used : BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Aggregations

BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)24 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)17 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)12 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)7 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)6 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)6 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)6 ArrayList (java.util.ArrayList)5 BEndpointVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol)4 List (java.util.List)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 SymbolTable (org.wso2.ballerinalang.compiler.semantics.model.SymbolTable)3 BOperatorSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol)3 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)3 BAttachedFunction (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction)3 BLangIdentifier (org.wso2.ballerinalang.compiler.tree.BLangIdentifier)3 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)3 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2