Search in sources :

Example 1 with Type

use of org.ballerinalang.model.types.Type in project ballerina by ballerina-lang.

the class TypeChecker method visit.

public void visit(BLangRecordLiteral recordLiteral) {
    BType actualType = symTable.errType;
    int expTypeTag = expTypes.get(0).tag;
    if (expTypeTag == TypeTags.NONE || expTypeTag == TypeTags.ANY) {
        // var a = {}
        // Change the expected type to map
        expTypes = Lists.of(symTable.mapType);
    }
    List<BType> matchedTypeList = getRecordCompatibleType(expTypes.get(0));
    if (matchedTypeList.isEmpty()) {
        dlog.error(recordLiteral.pos, DiagnosticCode.INVALID_LITERAL_FOR_TYPE, expTypes.get(0));
    } else if (matchedTypeList.size() > 1) {
        dlog.error(recordLiteral.pos, DiagnosticCode.AMBIGUOUS_TYPES, expTypes.get(0));
    } else {
        recordLiteral.keyValuePairs.forEach(keyValuePair -> checkRecLiteralKeyValue(keyValuePair, matchedTypeList.get(0)));
        actualType = matchedTypeList.get(0);
        // TODO Following check can be moved the code analyzer.
        if (expTypeTag == TypeTags.STRUCT) {
            validateStructInitalizer(recordLiteral.pos);
        }
    }
    resultTypes = types.checkTypes(recordLiteral, Lists.of(actualType), expTypes);
}
Also used : Arrays(java.util.Arrays) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) BLangXMLTextLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral) Map(java.util.Map) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr) Set(java.util.Set) SelectExpressionNode(org.ballerinalang.model.tree.clauses.SelectExpressionNode) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangLambdaFunction(org.wso2.ballerinalang.compiler.tree.expressions.BLangLambdaFunction) BLangSelectExpression(org.wso2.ballerinalang.compiler.tree.clauses.BLangSelectExpression) NamedArgNode(org.ballerinalang.model.tree.expressions.NamedArgNode) BLangXMLAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute) BLangNamedArgsExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangNamedArgsExpression) BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangXMLCommentLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLCommentLiteral) ArrayList(java.util.ArrayList) Flags(org.wso2.ballerinalang.util.Flags) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) TreeBuilder(org.ballerinalang.model.TreeBuilder) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangXMLProcInsLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLProcInsLiteral) BLangForever(org.wso2.ballerinalang.compiler.tree.statements.BLangForever) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangOrderBy(org.wso2.ballerinalang.compiler.tree.clauses.BLangOrderBy) BLangSelectClause(org.wso2.ballerinalang.compiler.tree.clauses.BLangSelectClause) SymTag(org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangTableQuery(org.wso2.ballerinalang.compiler.tree.clauses.BLangTableQuery) BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) BLangBracedOrTupleExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) BLangXMLAttributeAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttributeAccess) Lists(org.wso2.ballerinalang.util.Lists) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) OperatorKind(org.ballerinalang.model.tree.OperatorKind) BLangHaving(org.wso2.ballerinalang.compiler.tree.clauses.BLangHaving) BOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol) Names(org.wso2.ballerinalang.compiler.util.Names) BLangDiagnosticLog(org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLog) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) MultiReturnExpr(org.wso2.ballerinalang.compiler.tree.expressions.MultiReturnExpr) BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangRecordKey(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) Collectors(java.util.stream.Collectors) BLangFieldBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess) List(java.util.List) NodeKind(org.ballerinalang.model.tree.NodeKind) BLangTableQueryExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangTableQueryExpression) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) BLangStreamingInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangStreamingInput) BLangNodeVisitor(org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor) BLangTypeofExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeofExpr) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) HashSet(java.util.HashSet) IterableKind(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind) BLangStringTemplateLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangStringTemplateLiteral) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BLangTernaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr) BFutureType(org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType) BLangIntRangeExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangIntRangeExpression) BLangRecordKeyValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue) XMLConstants(javax.xml.XMLConstants) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BTransformerSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTransformerSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) RecordKind(org.wso2.ballerinalang.compiler.semantics.analyzer.Types.RecordKind) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangGroupBy(org.wso2.ballerinalang.compiler.tree.clauses.BLangGroupBy) Type(org.ballerinalang.model.types.Type) Name(org.wso2.ballerinalang.compiler.util.Name) BLangJoinStreamingInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinStreamingInput) BLangAwaitExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangAwaitExpr) BLangRestArgsExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangRestArgsExpression) BLangXMLElementLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral) FieldType(org.wso2.ballerinalang.compiler.util.FieldType) DiagnosticCode(org.ballerinalang.util.diagnostic.DiagnosticCode) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) Collections(java.util.Collections) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Aggregations

ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 XMLConstants (javax.xml.XMLConstants)1 TreeBuilder (org.ballerinalang.model.TreeBuilder)1 NodeKind (org.ballerinalang.model.tree.NodeKind)1 OperatorKind (org.ballerinalang.model.tree.OperatorKind)1 SelectExpressionNode (org.ballerinalang.model.tree.clauses.SelectExpressionNode)1 ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)1 NamedArgNode (org.ballerinalang.model.tree.expressions.NamedArgNode)1 Type (org.ballerinalang.model.types.Type)1 DiagnosticCode (org.ballerinalang.util.diagnostic.DiagnosticCode)1 RecordKind (org.wso2.ballerinalang.compiler.semantics.analyzer.Types.RecordKind)1 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)1 SymbolTable (org.wso2.ballerinalang.compiler.semantics.model.SymbolTable)1