Search in sources :

Example 1 with BLangVariable

use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.

the class CommandUtil method getStructNodeDocumentation.

static DocAttachmentInfo getStructNodeDocumentation(BLangStruct bLangStruct, int replaceFrom) {
    List<String> attributes = new ArrayList<>();
    DiagnosticPos structPos = CommonUtil.toZeroBasedPosition(bLangStruct.getPosition());
    int offset = structPos.getStartColumn();
    bLangStruct.getFields().forEach(bLangVariable -> attributes.add(getDocAttributeFromBLangVariable(bLangVariable, offset)));
    return new DocAttachmentInfo(getDocumentationAttachment(attributes, structPos.getStartColumn()), replaceFrom);
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) ArrayList(java.util.ArrayList)

Example 2 with BLangVariable

use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.

the class ParserUtils method createNewStruct.

/**
 * Create new struct.
 *
 * @param name   name of the struct
 * @param fields field definiton statements
 * @return {Function} function
 */
private static Struct createNewStruct(String name, List<BLangVariable> fields, String fileName) {
    Struct struct = new Struct(name);
    fields.forEach((field) -> {
        String defaultValue = null;
        if (field.getInitialExpression() != null) {
            defaultValue = ((BLangLiteral) field.getInitialExpression()).getValue().toString();
        }
        StructField structField = createNewStructField(field.getName().getValue(), field.getTypeNode().type.toString(), defaultValue);
        struct.addStructField(structField);
    });
    struct.setFileName(fileName);
    return struct;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) StructField(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.StructField) Struct(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Struct) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct)

Example 3 with BLangVariable

use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.

the class SymbolEnter method createReceiver.

private BLangVariable createReceiver(BLangStruct struct) {
    BLangVariable receiver = (BLangVariable) TreeBuilder.createVariableNode();
    receiver.pos = struct.pos;
    IdentifierNode name = createIdentifier(Names.SELF.getValue());
    receiver.setName(name);
    receiver.docTag = DocTag.RECEIVER;
    BLangUserDefinedType structTypeNode = (BLangUserDefinedType) TreeBuilder.createUserDefinedTypeNode();
    structTypeNode.pkgAlias = new BLangIdentifier();
    structTypeNode.typeName = struct.name;
    receiver.setTypeNode(structTypeNode);
    return receiver;
}
Also used : IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 4 with BLangVariable

use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.

the class TaintAnalyzer method visitInvokable.

private void visitInvokable(BLangInvokableNode invNode, SymbolEnv symbolEnv) {
    if (invNode.symbol.taintTable == null) {
        if (Symbols.isNative(invNode.symbol)) {
            attachTaintTableBasedOnAnnotations(invNode);
            return;
        }
        Map<Integer, TaintRecord> taintTable = new HashMap<>();
        returnTaintedStatusList = null;
        // Check the tainted status of return values when no parameter is tainted.
        analyzeAllParamsUntaintedReturnTaintedStatus(taintTable, invNode, symbolEnv);
        boolean isBlocked = processBlockedNode(invNode);
        if (isBlocked) {
            return;
        }
        int requiredParamCount = invNode.requiredParams.size();
        int defaultableParamCount = invNode.defaultableParams.size();
        int totalParamCount = requiredParamCount + defaultableParamCount + (invNode.restParam == null ? 0 : 1);
        for (int paramIndex = 0; paramIndex < totalParamCount; paramIndex++) {
            BLangVariable param = getParam(invNode, paramIndex, requiredParamCount, defaultableParamCount);
            // If parameter is sensitive, it is invalid to have a case where tainted status of parameter is true.
            if (hasAnnotation(param, ANNOTATION_SENSITIVE)) {
                continue;
            }
            returnTaintedStatusList = null;
            // Set each parameter "tainted", then analyze the body to observe the outcome of the function.
            analyzeReturnTaintedStatus(taintTable, invNode, symbolEnv, paramIndex, requiredParamCount, defaultableParamCount);
        }
        invNode.symbol.taintTable = taintTable;
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) TaintRecord(org.wso2.ballerinalang.compiler.semantics.model.symbols.TaintRecord) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 5 with BLangVariable

use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.

the class TaintAnalyzer method analyzeLambdaExpressions.

private void analyzeLambdaExpressions(BLangInvocation invocationExpr, BLangExpression argExpr) {
    BLangFunction function = ((BLangLambdaFunction) argExpr).function;
    if (function.symbol.taintTable == null) {
        addToBlockedList(invocationExpr);
    } else {
        int requiredParamCount = function.requiredParams.size();
        int defaultableParamCount = function.defaultableParams.size();
        int totalParamCount = requiredParamCount + defaultableParamCount + (function.restParam == null ? 0 : 1);
        Map<Integer, TaintRecord> taintTable = function.symbol.taintTable;
        for (int paramIndex = 0; paramIndex < totalParamCount; paramIndex++) {
            TaintRecord taintRecord = taintTable.get(paramIndex);
            BLangVariable param = getParam(function, paramIndex, requiredParamCount, defaultableParamCount);
            if (taintRecord == null) {
                addTaintError(argExpr.pos, param.name.value, DiagnosticCode.TAINTED_VALUE_PASSED_TO_SENSITIVE_PARAMETER);
            } else if (taintRecord.taintError != null && taintRecord.taintError.size() > 0) {
                addTaintError(taintRecord.taintError);
            }
            if (stopAnalysis) {
                break;
            }
        }
    }
}
Also used : BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangLambdaFunction(org.wso2.ballerinalang.compiler.tree.expressions.BLangLambdaFunction) TaintRecord(org.wso2.ballerinalang.compiler.semantics.model.symbols.TaintRecord) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Aggregations

BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)77 ArrayList (java.util.ArrayList)21 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)20 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)18 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)18 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)16 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)15 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)14 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)11 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)10 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)10 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)10 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)9 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)8 Whitespace (org.ballerinalang.model.Whitespace)7 BLangObject (org.wso2.ballerinalang.compiler.tree.BLangObject)7 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)7 Name (org.wso2.ballerinalang.compiler.util.Name)7 HashMap (java.util.HashMap)6 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)6