use of org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType in project ballerina by ballerina-lang.
the class Desugar method getSafeAssignErrorPattern.
private BLangMatchStmtPatternClause getSafeAssignErrorPattern(DiagnosticPos pos, BSymbol invokableSymbol) {
// From here onwards we assume that this function has only one return type
// Owner of the variable symbol must be an invokable symbol
boolean noRetParams = ((BInvokableType) invokableSymbol.type).retTypes.isEmpty();
boolean returnErrorType = false;
if (!noRetParams) {
BType retType = ((BInvokableType) invokableSymbol.type).retTypes.get(0);
Set<BType> returnTypeSet = retType.tag == TypeTags.UNION ? ((BUnionType) retType).memberTypes : new HashSet<BType>() {
{
add(retType);
}
};
returnErrorType = returnTypeSet.stream().anyMatch(type -> types.isAssignable(type, symTable.errStructType));
}
// Create the pattern to match the error type
// 1) Create the pattern variable
String patternFailureCaseVarName = GEN_VAR_PREFIX.value + "t_failure";
BLangVariable patternFailureCaseVar = ASTBuilderUtil.createVariable(pos, patternFailureCaseVarName, symTable.errStructType, null, new BVarSymbol(0, names.fromString(patternFailureCaseVarName), this.env.scope.owner.pkgID, symTable.errStructType, this.env.scope.owner));
// 2) Create the pattern block
BLangVariableReference patternFailureCaseVarRef = ASTBuilderUtil.createVariableRef(pos, patternFailureCaseVar.symbol);
BLangBlockStmt patternBlockFailureCase = (BLangBlockStmt) TreeBuilder.createBlockNode();
patternBlockFailureCase.pos = pos;
if (noRetParams || !returnErrorType) {
// throw e
BLangThrow throwStmt = (BLangThrow) TreeBuilder.createThrowNode();
throwStmt.pos = pos;
throwStmt.expr = patternFailureCaseVarRef;
patternBlockFailureCase.stmts.add(throwStmt);
} else {
// return e;
BLangReturn returnStmt = (BLangReturn) TreeBuilder.createReturnNode();
returnStmt.pos = pos;
returnStmt.exprs = new ArrayList<BLangExpression>() {
{
add(patternFailureCaseVarRef);
}
};
patternBlockFailureCase.stmts.add(returnStmt);
}
return ASTBuilderUtil.createMatchStatementPattern(pos, patternFailureCaseVar, patternBlockFailureCase);
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType 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.types.BUnionType in project ballerina by ballerina-lang.
the class PositionTreeVisitor method visit.
@Override
public void visit(BLangUserDefinedType userDefinedType) {
userDefinedType.getPosition().sCol += (previousNode instanceof BLangEndpointTypeNode ? "endpoint<".length() : 0);
CommonUtil.calculateEndColumnOfGivenName(userDefinedType.getPosition(), userDefinedType.typeName.value, userDefinedType.pkgAlias.value);
if (userDefinedType.type instanceof BUnionType && HoverUtil.isMatchingPosition(userDefinedType.getPosition(), this.position)) {
try {
BUnionType bUnionType = (BUnionType) userDefinedType.type;
for (BType type : bUnionType.memberTypes) {
if (type.tsymbol != null && type.tsymbol.getName().getValue().equals(userDefinedType.typeName.getValue())) {
this.context.put(NodeContextKeys.NODE_KEY, userDefinedType);
this.context.put(NodeContextKeys.PREVIOUSLY_VISITED_NODE_KEY, this.previousNode);
this.context.put(NodeContextKeys.NAME_OF_NODE_KEY, userDefinedType.typeName.getValue());
this.context.put(NodeContextKeys.PACKAGE_OF_NODE_KEY, type.tsymbol.pkgID);
this.context.put(NodeContextKeys.SYMBOL_KIND_OF_NODE_PARENT_KEY, type.tsymbol.kind.name());
this.context.put(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY, type.tsymbol.kind.name());
this.context.put(NodeContextKeys.NODE_OWNER_KEY, type.tsymbol.owner.name.getValue());
this.context.put(NodeContextKeys.NODE_OWNER_PACKAGE_KEY, type.tsymbol.owner.pkgID);
this.context.put(NodeContextKeys.VAR_NAME_OF_NODE_KEY, userDefinedType.typeName.getValue());
setTerminateVisitor(true);
break;
}
}
} catch (ClassCastException e) {
// Ignores
}
} else if (userDefinedType.type.tsymbol != null && HoverUtil.isMatchingPosition(userDefinedType.getPosition(), this.position)) {
this.context.put(NodeContextKeys.NODE_KEY, userDefinedType);
this.context.put(NodeContextKeys.PREVIOUSLY_VISITED_NODE_KEY, this.previousNode);
this.context.put(NodeContextKeys.NAME_OF_NODE_KEY, userDefinedType.typeName.getValue());
this.context.put(NodeContextKeys.PACKAGE_OF_NODE_KEY, userDefinedType.type.tsymbol.pkgID);
this.context.put(NodeContextKeys.SYMBOL_KIND_OF_NODE_PARENT_KEY, userDefinedType.type.tsymbol.kind.name());
this.context.put(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY, userDefinedType.type.tsymbol.kind.name());
this.context.put(NodeContextKeys.NODE_OWNER_KEY, userDefinedType.type.tsymbol.owner.name.getValue());
this.context.put(NodeContextKeys.NODE_OWNER_PACKAGE_KEY, userDefinedType.type.tsymbol.owner.pkgID);
this.context.put(NodeContextKeys.VAR_NAME_OF_NODE_KEY, userDefinedType.typeName.getValue());
setTerminateVisitor(true);
}
}
Aggregations