Search in sources :

Example 66 with BLangVariable

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

the class CodeGenerator method createStructInfoEntry.

private void createStructInfoEntry(BLangStruct structNode) {
    BStructSymbol structSymbol = (BStructSymbol) structNode.symbol;
    // Add Struct name as an UTFCPEntry to the constant pool
    int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
    StructInfo structInfo = new StructInfo(currentPackageRefCPIndex, structNameCPIndex, structSymbol.flags);
    currentPkgInfo.addStructInfo(structSymbol.name.value, structInfo);
    structInfo.structType = (BStructType) structSymbol.type;
    List<BLangVariable> structFields = structNode.fields;
    for (BLangVariable structField : structFields) {
        // Create StructFieldInfo Entry
        int fieldNameCPIndex = addUTF8CPEntry(currentPkgInfo, structField.name.value);
        int sigCPIndex = addUTF8CPEntry(currentPkgInfo, structField.type.getDesc());
        StructFieldInfo structFieldInfo = new StructFieldInfo(fieldNameCPIndex, sigCPIndex, structField.symbol.flags);
        structFieldInfo.fieldType = structField.type;
        // Populate default values
        if (structField.expr != null && structField.expr.getKind() == NodeKind.LITERAL) {
            DefaultValueAttributeInfo defaultVal = getDefaultValueAttributeInfo((BLangLiteral) structField.expr);
            structFieldInfo.addAttributeInfo(AttributeInfo.Kind.DEFAULT_VALUE_ATTRIBUTE, defaultVal);
        }
        structInfo.fieldInfoEntries.add(structFieldInfo);
        structField.symbol.varIndex = getFieldIndex(structField.symbol.type.tag);
    }
    // Create variable count attribute info
    prepareIndexes(fieldIndexes);
    int[] fieldCount = new int[] { fieldIndexes.tInt, fieldIndexes.tFloat, fieldIndexes.tString, fieldIndexes.tBoolean, fieldIndexes.tBlob, fieldIndexes.tRef };
    addVariableCountAttributeInfo(currentPkgInfo, structInfo, fieldCount);
    fieldIndexes = new VariableIndex(FIELD);
    // Create attached function info entries
    for (BAttachedFunction attachedFunc : structSymbol.attachedFuncs) {
        int funcNameCPIndex = addUTF8CPEntry(currentPkgInfo, attachedFunc.funcName.value);
        // Remove the first type. The first type is always the type to which the function is attached to
        BType[] paramTypes = attachedFunc.type.paramTypes.toArray(new BType[0]);
        if (paramTypes.length == 1) {
            paramTypes = new BType[0];
        } else {
            paramTypes = attachedFunc.type.paramTypes.toArray(new BType[0]);
            paramTypes = Arrays.copyOfRange(paramTypes, 1, paramTypes.length);
        }
        int sigCPIndex = addUTF8CPEntry(currentPkgInfo, generateFunctionSig(paramTypes, attachedFunc.type.retTypes.toArray(new BType[0])));
        int flags = attachedFunc.symbol.flags;
        structInfo.attachedFuncInfoEntries.add(new AttachedFunctionInfo(funcNameCPIndex, sigCPIndex, flags));
    }
}
Also used : BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) AttachedFunctionInfo(org.wso2.ballerinalang.programfile.AttachedFunctionInfo) StructInfo(org.wso2.ballerinalang.programfile.StructInfo) StructFieldInfo(org.wso2.ballerinalang.programfile.StructFieldInfo) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) DefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 67 with BLangVariable

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

the class CodeGenerator method setParameterNames.

private void setParameterNames(BLangResource resourceNode, ResourceInfo resourceInfo) {
    int paramCount = resourceNode.requiredParams.size();
    resourceInfo.paramNameCPIndexes = new int[paramCount];
    for (int i = 0; i < paramCount; i++) {
        BLangVariable paramVar = resourceNode.requiredParams.get(i);
        String paramName = null;
        boolean isAnnotated = false;
        for (BLangAnnotationAttachment annotationAttachment : paramVar.annAttachments) {
            String attachmentName = annotationAttachment.getAnnotationName().getValue();
            if ("PathParam".equalsIgnoreCase(attachmentName) || "QueryParam".equalsIgnoreCase(attachmentName)) {
                // TODO:
                // paramName = annotationAttachment.getAttributeNameValuePairs().get("value")
                // .getLiteralValue().stringValue();
                isAnnotated = true;
                break;
            }
        }
        if (!isAnnotated) {
            paramName = paramVar.name.getValue();
        }
        int paramNameCPIndex = addUTF8CPEntry(currentPkgInfo, paramName);
        resourceInfo.paramNameCPIndexes[i] = paramNameCPIndex;
    }
}
Also used : BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 68 with BLangVariable

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

the class CodeGenerator method visit.

public void visit(BLangCatch bLangCatch) {
    // Define local variable index for Error.
    BLangVariable variable = bLangCatch.param;
    RegIndex lvIndex = getLVIndex(variable.symbol.type.tag);
    variable.symbol.varIndex = lvIndex;
    emit(InstructionFactory.get(InstructionCodes.ERRSTORE, lvIndex));
    // Visit Catch Block.
    genNode(bLangCatch.body, env);
}
Also used : BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 69 with BLangVariable

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

the class ASTBuilderUtil method createInvocationExpr.

static BLangInvocation createInvocationExpr(DiagnosticPos pos, BInvokableSymbol invokableSymbol, List<BLangVariable> requiredArgs, List<BLangVariable> namedArgs, List<BLangVariable> restArgs, SymbolResolver symResolver) {
    final BLangInvocation invokeLambda = (BLangInvocation) TreeBuilder.createInvocationNode();
    invokeLambda.pos = pos;
    invokeLambda.requiredArgs.addAll(generateArgExprs(pos, requiredArgs, invokableSymbol.params, symResolver));
    invokeLambda.namedArgs.addAll(generateArgExprs(pos, namedArgs, invokableSymbol.defaultableParams, symResolver));
    invokeLambda.restArgs.addAll(generateArgExprs(pos, restArgs, Lists.of(invokableSymbol.restParam), symResolver));
    invokeLambda.symbol = invokableSymbol;
    invokeLambda.types.addAll(((BInvokableType) invokableSymbol.type).retTypes);
    if (!invokeLambda.types.isEmpty()) {
        invokeLambda.type = invokeLambda.types.get(0);
    }
    return invokeLambda;
}
Also used : BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)

Example 70 with BLangVariable

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

the class ASTBuilderUtil method defineVariable.

static void defineVariable(BLangVariable variable, BSymbol targetSymbol, Names names) {
    variable.symbol = new BVarSymbol(0, names.fromIdNode(variable.name), targetSymbol.pkgID, variable.type, targetSymbol);
    targetSymbol.scope.define(variable.symbol.name, variable.symbol);
}
Also used : BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

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