use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol in project ballerina by ballerina-lang.
the class EndpointSPIAnalyzer method populateEndpointSymbol.
public void populateEndpointSymbol(BStructSymbol structSymbol, BEndpointVarSymbol endpointVarSymbol) {
final Endpoint endpoint = validSPIs.get(structSymbol);
if (endpoint != null) {
final Endpoint endPoint = validSPIs.get(structSymbol);
endpointVarSymbol.initFunction = endPoint.initFunction;
endpointVarSymbol.interactable = endPoint.interactable;
endpointVarSymbol.getClientFunction = endPoint.getClientFunction;
endpointVarSymbol.clientSymbol = (BStructSymbol) endPoint.clientStruct.tsymbol;
endpointVarSymbol.startFunction = endPoint.startFunction;
endpointVarSymbol.stopFunction = endPoint.stopFunction;
endpointVarSymbol.registrable = endPoint.registrable;
endpointVarSymbol.registerFunction = endPoint.registerFunction;
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
@Override
public void visit(BLangStructLiteral structLiteral) {
BStructSymbol structSymbol = (BStructSymbol) structLiteral.type.tsymbol;
int pkgCPIndex = addPackageRefCPEntry(currentPkgInfo, structSymbol.pkgID);
int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, structNameCPIndex);
Operand structCPIndex = getOperand(currentPkgInfo.addCPEntry(structureRefCPEntry));
// Emit an instruction to create a new struct.
RegIndex structRegIndex = calcAndGetExprRegIndex(structLiteral);
emit(InstructionCodes.NEWSTRUCT, structCPIndex, structRegIndex);
// Invoke the struct default values init function here.
if (structSymbol.defaultsValuesInitFunc != null) {
int funcRefCPIndex = getFuncRefCPIndex(structSymbol.defaultsValuesInitFunc.symbol);
// call funcRefCPIndex 1 structRegIndex 0
Operand[] operands = new Operand[5];
operands[0] = getOperand(funcRefCPIndex);
operands[1] = getOperand(false);
operands[2] = getOperand(1);
operands[3] = structRegIndex;
operands[4] = getOperand(0);
emit(InstructionCodes.CALL, operands);
}
// Invoke the struct initializer here.
if (structLiteral.initializer != null) {
int funcRefCPIndex = getFuncRefCPIndex(structLiteral.initializer.symbol);
// call funcRefCPIndex 1 structRegIndex 0
Operand[] operands = new Operand[5];
operands[0] = getOperand(funcRefCPIndex);
operands[1] = getOperand(false);
operands[2] = getOperand(1);
operands[3] = structRegIndex;
operands[4] = getOperand(0);
emit(InstructionCodes.CALL, operands);
}
// Generate code the struct literal.
for (BLangRecordKeyValue keyValue : structLiteral.keyValuePairs) {
BLangRecordKey key = keyValue.key;
Operand fieldIndex = key.fieldSymbol.varIndex;
genNode(keyValue.valueExpr, this.env);
int opcode = getOpcode(key.fieldSymbol.type.tag, InstructionCodes.IFIELDSTORE);
emit(opcode, structRegIndex, fieldIndex, keyValue.valueExpr.regIndex);
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol 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));
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol in project ballerina by ballerina-lang.
the class ParserRuleMatchStatementContextResolver method resolveItems.
@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
ArrayList<CompletionItem> completionItems = new ArrayList<>();
int currentTokenIndex = completionContext.get(DocumentServiceKeys.TOKEN_INDEX_KEY);
List<SymbolInfo> visibleSymbols = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
TokenStream tokenStream = completionContext.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
while (true) {
if (currentTokenIndex < 0) {
// Ideally should not come to this point
return completionItems;
}
Token token = CommonUtil.getPreviousDefaultToken(tokenStream, currentTokenIndex);
if (token.getText().equals(UtilSymbolKeys.MATCH_KEYWORD_KEY)) {
currentTokenIndex = token.getTokenIndex();
break;
} else {
currentTokenIndex = token.getTokenIndex();
}
}
String identifierMatched = CommonUtil.getNextDefaultToken(tokenStream, currentTokenIndex).getText();
SymbolInfo identifierSymbol = visibleSymbols.stream().filter(symbolInfo -> symbolInfo.getScopeEntry().symbol.getName().getValue().equals(identifierMatched)).findFirst().orElseGet(null);
if (identifierSymbol == null) {
return completionItems;
} else if (identifierSymbol.getScopeEntry().symbol.type instanceof BUnionType) {
Set<BType> memberTypes = ((BUnionType) identifierSymbol.getScopeEntry().symbol.type).getMemberTypes();
memberTypes.forEach(bType -> {
completionItems.add(this.populateCompletionItem(bType.toString(), ItemResolverConstants.B_TYPE, bType.toString()));
});
} else if (identifierSymbol.getScopeEntry().symbol.type instanceof BJSONType) {
ArrayList<Integer> typeTagsList = new ArrayList<>(Arrays.asList(TypeTags.INT, TypeTags.FLOAT, TypeTags.BOOLEAN, TypeTags.STRING, TypeTags.NULL, TypeTags.JSON));
List<SymbolInfo> filteredBasicTypes = visibleSymbols.stream().filter(symbolInfo -> {
BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
return bSymbol instanceof BTypeSymbol && typeTagsList.contains(bSymbol.getType().tag);
}).collect(Collectors.toList());
this.populateCompletionItemList(filteredBasicTypes, completionItems);
} else if (identifierSymbol.getScopeEntry().symbol.type instanceof BStructType) {
List<SymbolInfo> structSymbols = visibleSymbols.stream().filter(symbolInfo -> {
BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
return bSymbol instanceof BStructSymbol && !bSymbol.getName().getValue().startsWith(UtilSymbolKeys.ANON_STRUCT_CHECKER);
}).collect(Collectors.toList());
this.populateCompletionItemList(structSymbols, completionItems);
}
return completionItems;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol in project ballerina by ballerina-lang.
the class BLangEndpointContextResolver method resolveItems.
@Override
@SuppressWarnings("unchecked")
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
BLangNode bLangEndpoint = completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY);
ArrayList<CompletionItem> completionItems = new ArrayList<>();
ArrayList<SymbolInfo> configurationFields = new ArrayList<>();
List<BStructSymbol.BAttachedFunction> attachedFunctions = new ArrayList<>();
if (((BLangEndpoint) bLangEndpoint).type.tsymbol instanceof BStructSymbol) {
attachedFunctions.addAll(((BStructSymbol) ((BLangEndpoint) bLangEndpoint).type.tsymbol).attachedFuncs);
}
BStructSymbol.BAttachedFunction initFunction = attachedFunctions.stream().filter(bAttachedFunction -> bAttachedFunction.funcName.getValue().equals(INIT)).findFirst().orElseGet(null);
BVarSymbol configSymbol = initFunction.symbol.getParameters().get(0);
BType configSymbolType = configSymbol.getType();
if (configSymbolType instanceof BStructType) {
((BStructType) configSymbolType).getFields().forEach(bStructField -> configurationFields.add(new SymbolInfo(bStructField.getName().getValue(), new Scope.ScopeEntry(bStructField.symbol, null))));
}
this.populateCompletionItemList(configurationFields, completionItems);
return completionItems;
}
Aggregations