Search in sources :

Example 21 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class Desugar method visit.

@Override
public void visit(BLangSimpleVarRef varRefExpr) {
    BLangSimpleVarRef genVarRefExpr = varRefExpr;
    // XML qualified name reference. e.g: ns0:foo
    if (varRefExpr.pkgSymbol != null && varRefExpr.pkgSymbol.tag == SymTag.XMLNS) {
        BLangXMLQName qnameExpr = new BLangXMLQName(varRefExpr.variableName);
        qnameExpr.nsSymbol = (BXMLNSSymbol) varRefExpr.pkgSymbol;
        qnameExpr.localname = varRefExpr.variableName;
        qnameExpr.prefix = varRefExpr.pkgAlias;
        qnameExpr.namespaceURI = qnameExpr.nsSymbol.namespaceURI;
        qnameExpr.isUsedInXML = false;
        qnameExpr.pos = varRefExpr.pos;
        qnameExpr.type = symTable.stringType;
        result = qnameExpr;
        return;
    }
    BSymbol ownerSymbol = varRefExpr.symbol.owner;
    if ((varRefExpr.symbol.tag & SymTag.FUNCTION) == SymTag.FUNCTION && varRefExpr.symbol.type.tag == TypeTags.INVOKABLE) {
        genVarRefExpr = new BLangFunctionVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.INVOKABLE) == SymTag.INVOKABLE) {
        // Local variable in a function/resource/action/worker
        genVarRefExpr = new BLangLocalVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.CONNECTOR) == SymTag.CONNECTOR) {
        // Field variable in a receiver
        genVarRefExpr = new BLangFieldVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.STRUCT) == SymTag.STRUCT) {
        genVarRefExpr = new BLangFieldVarRef(varRefExpr.symbol);
    } else if ((ownerSymbol.tag & SymTag.PACKAGE) == SymTag.PACKAGE || (ownerSymbol.tag & SymTag.SERVICE) == SymTag.SERVICE) {
        // Package variable | service variable
        // We consider both of them as package level variables
        genVarRefExpr = new BLangPackageVarRef(varRefExpr.symbol);
        // Only locking service level and package level variables
        if (!enclLocks.isEmpty()) {
            enclLocks.peek().addLockVariable(varRefExpr.symbol);
        }
    }
    genVarRefExpr.type = varRefExpr.type;
    result = genVarRefExpr;
}
Also used : BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangPackageVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangPackageVarRef) BLangFunctionVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFunctionVarRef) BLangLocalVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef) BLangFieldVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFieldVarRef)

Example 22 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class BLangParserListener method exitFieldVariableReference.

@Override
public void exitFieldVariableReference(BallerinaParser.FieldVariableReferenceContext ctx) {
    if (ctx.exception != null) {
        return;
    }
    FieldContext field = ctx.field();
    String fieldName;
    FieldType fieldType;
    if (field.Identifier() != null) {
        fieldName = field.Identifier().getText();
        fieldType = FieldType.SINGLE;
    } else {
        fieldName = field.MUL().getText();
        fieldType = FieldType.ALL;
    }
    this.pkgBuilder.createFieldBasedAccessNode(getCurrentPos(ctx), getWS(ctx), fieldName, fieldType);
}
Also used : FieldContext(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser.FieldContext) FieldType(org.wso2.ballerinalang.compiler.util.FieldType)

Example 23 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addFieldToObject.

void addFieldToObject(DiagnosticPos pos, Set<Whitespace> ws, String identifier, boolean exprAvailable, int annotCount, boolean isPrivate) {
    Set<Whitespace> wsForSemiColon = removeNthFromLast(ws, 0);
    BLangObject objectNode = (BLangObject) this.objectStack.peek();
    objectNode.addWS(wsForSemiColon);
    BLangVariable field = addVar(pos, ws, identifier, exprAvailable, annotCount);
    if (!isPrivate) {
        field.flagSet.add(Flag.PUBLIC);
    }
}
Also used : BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) Whitespace(org.ballerinalang.model.Whitespace) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 24 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class TreeVisitor method visit.

@Override
public void visit(BLangObject objectNode) {
    BSymbol objectSymbol = objectNode.symbol;
    SymbolEnv objectEnv = SymbolEnv.createPkgLevelSymbolEnv(objectNode, objectSymbol.scope, symbolEnv);
    blockOwnerStack.push(objectNode);
    this.cursorPositionResolver = ObjectTypeScopeResolver.class;
    objectNode.fields.forEach(field -> acceptNode(field, objectEnv));
    // TODO: visit annotation and doc attachments
    objectNode.functions.forEach(f -> acceptNode(f, objectEnv));
    blockOwnerStack.pop();
    this.cursorPositionResolver = TopLevelNodeScopeResolver.class;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 25 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project jaggery by wso2.

the class RegistryHostObject method search.

private AdvancedSearchResultsBean search(Registry configSystemRegistry, UserRegistry registry, CustomSearchParameterBean parameters) throws CarbonException {
    RegistryUtils.recordStatistics(parameters);
    AdvancedSearchResultsBean metaDataSearchResultsBean;
    ResourceData[] contentSearchResourceData;
    String[][] tempParameterValues = parameters.getParameterValues();
    //        Doing a validation of all the values sent
    boolean allEmpty = true;
    for (String[] tempParameterValue : tempParameterValues) {
        if (tempParameterValue[1] != null & tempParameterValue[1].trim().length() > 0) {
            allEmpty = false;
            //                Validating all the dates
            if (tempParameterValue[0].equals("createdAfter") || tempParameterValue[0].equals("createdBefore") || tempParameterValue[0].equals("updatedAfter") || tempParameterValue[0].equals("updatedBefore")) {
                if (!SearchUtils.validateDateInput(tempParameterValue[1])) {
                    String message = tempParameterValue[0] + " contains illegal characters";
                    return SearchUtils.getEmptyResultBeanWithErrorMsg(message);
                }
            } else if (tempParameterValue[0].equals("mediaType")) {
                if (SearchUtils.validateMediaTypeInput(tempParameterValue[1])) {
                    String message = tempParameterValue[0] + " contains illegal characters";
                    return SearchUtils.getEmptyResultBeanWithErrorMsg(message);
                }
            } else if (tempParameterValue[0].equals("content")) {
                if (SearchUtils.validateContentInput(tempParameterValue[1])) {
                    String message = tempParameterValue[0] + " contains illegal characters";
                    return SearchUtils.getEmptyResultBeanWithErrorMsg(message);
                }
            } else if (tempParameterValue[0].equals("tags")) {
                boolean containsTag = false;
                for (String str : tempParameterValue[1].split(",")) {
                    if (str.trim().length() > 0) {
                        containsTag = true;
                        break;
                    }
                }
                if (!containsTag) {
                    String message = tempParameterValue[0] + " contains illegal characters";
                    return SearchUtils.getEmptyResultBeanWithErrorMsg(message);
                }
                if (SearchUtils.validateTagsInput(tempParameterValue[1])) {
                    String message = tempParameterValue[0] + " contains illegal characters";
                    return SearchUtils.getEmptyResultBeanWithErrorMsg(message);
                }
            } else {
                if (SearchUtils.validatePathInput(tempParameterValue[1])) {
                    String message = tempParameterValue[0] + " contains illegal characters";
                    return SearchUtils.getEmptyResultBeanWithErrorMsg(message);
                }
            }
        }
    }
    if (allEmpty) {
        return SearchUtils.getEmptyResultBeanWithErrorMsg("At least one field must be filled");
    }
    boolean onlyContent = true;
    for (String[] tempParameterValue : tempParameterValues) {
        if (!tempParameterValue[0].equals("content") && !tempParameterValue[0].equals("leftOp") && !tempParameterValue[0].equals("rightOp") && tempParameterValue[1] != null && tempParameterValue[1].length() > 0) {
            onlyContent = false;
            break;
        }
    }
    for (String[] tempParameterValue : tempParameterValues) {
        if (tempParameterValue[0].equals("content") && tempParameterValue[1] != null && tempParameterValue[1].length() > 0) {
            try {
                contentSearchResourceData = search(registry, tempParameterValue[1]);
            } catch (Exception e) {
                metaDataSearchResultsBean = new AdvancedSearchResultsBean();
                metaDataSearchResultsBean.setErrorMessage(e.getMessage());
                return metaDataSearchResultsBean;
            }
            //                If there are no resource paths returned from content, then there is no point of searching for more
            if (contentSearchResourceData != null && contentSearchResourceData.length > 0) {
                //                    Map<String, ResourceData> resourceDataMap = new HashMap<String, ResourceData>();
                Map<String, ResourceData> aggregatedMap = new HashMap<String, ResourceData>();
                for (ResourceData resourceData : contentSearchResourceData) {
                    aggregatedMap.put(resourceData.getResourcePath(), resourceData);
                }
                metaDataSearchResultsBean = AdvancedSearchResultsBeanPopulator.populate(configSystemRegistry, registry, parameters);
                if (metaDataSearchResultsBean != null) {
                    ResourceData[] metaDataResourceData = metaDataSearchResultsBean.getResourceDataList();
                    if (metaDataResourceData != null && metaDataResourceData.length > 0) {
                        List<String> invalidKeys = new ArrayList<String>();
                        for (String key : aggregatedMap.keySet()) {
                            boolean keyFound = false;
                            for (ResourceData resourceData : metaDataResourceData) {
                                if (resourceData.getResourcePath().equals(key)) {
                                    keyFound = true;
                                    break;
                                }
                            }
                            if (!keyFound) {
                                invalidKeys.add(key);
                            }
                        }
                        for (String invalidKey : invalidKeys) {
                            aggregatedMap.remove(invalidKey);
                        }
                    } else if (!onlyContent) {
                        aggregatedMap.clear();
                    }
                }
                ArrayList<ResourceData> sortedList = new ArrayList<ResourceData>(aggregatedMap.values());
                SearchUtils.sortResourceDataList(sortedList);
                metaDataSearchResultsBean = new AdvancedSearchResultsBean();
                metaDataSearchResultsBean.setResourceDataList(sortedList.toArray(new ResourceData[sortedList.size()]));
                return metaDataSearchResultsBean;
            } else {
                metaDataSearchResultsBean = new AdvancedSearchResultsBean();
                metaDataSearchResultsBean.setResourceDataList(contentSearchResourceData);
                return metaDataSearchResultsBean;
            }
        }
    }
    return AdvancedSearchResultsBeanPopulator.populate(configSystemRegistry, registry, parameters);
}
Also used : ResourceData(org.wso2.carbon.registry.common.ResourceData) AdvancedSearchResultsBean(org.wso2.carbon.registry.search.beans.AdvancedSearchResultsBean) RegistryException(org.wso2.carbon.registry.api.RegistryException) IndexerException(org.wso2.carbon.registry.indexing.indexer.IndexerException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) CarbonException(org.wso2.carbon.CarbonException) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException)

Aggregations

BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)14 Test (org.testng.annotations.Test)11 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)10 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)10 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)10 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)9 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)8 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)8 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)8 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)8 Name (org.wso2.ballerinalang.compiler.util.Name)8 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)7 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)7 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)7 CompileResult (org.ballerinalang.launcher.util.CompileResult)6 PackageNode (org.ballerinalang.model.tree.PackageNode)6 BLangDocumentation (org.wso2.ballerinalang.compiler.tree.BLangDocumentation)6 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)5 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)5 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)5