Search in sources :

Example 1 with CombinedType

use of nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType in project magik-tools by StevenLooman.

the class LocalTypeReasoner method walkPostSuper.

@Override
protected void walkPostSuper(final AstNode node) {
    // Determine which type we are.
    final AbstractType methodOwnerType = this.getMethodOwnerType(node);
    if (methodOwnerType == UndefinedType.INSTANCE) {
        LOGGER.debug("Unknown type for node: {}", node);
        return;
    }
    // Find specified super, if given.
    final AstNode identifierNode = node.getFirstChild(MagikGrammar.IDENTIFIER);
    final String identifier = identifierNode != null ? identifierNode.getTokenValue() : null;
    final AbstractType superType;
    if (identifier != null) {
        superType = methodOwnerType.getParents().stream().filter(parentType -> {
            final String fullTypeName = parentType.getFullName();
            final String typeName = fullTypeName.split(":")[1];
            return identifier.equals(typeName);
        }).findAny().orElse(null);
    } else {
        superType = methodOwnerType.getParents().stream().reduce(CombinedType::combine).orElse(null);
    }
    if (superType == null) {
        return;
    }
    final ExpressionResult result = new ExpressionResult(superType);
    this.assignAtom(node, result);
}
Also used : Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) AstNode(com.sonar.sslr.api.AstNode) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Package(nl.ramsolutions.sw.magik.analysis.typing.types.Package) SelfType(nl.ramsolutions.sw.magik.analysis.typing.types.SelfType) ArrayList(java.util.ArrayList) UndefinedType(nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType) MagikKeyword(nl.ramsolutions.sw.magik.api.MagikKeyword) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Map(java.util.Map) LeaveStatementNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.LeaveStatementNodeHelper) URI(java.net.URI) Location(nl.ramsolutions.sw.magik.analysis.Location) AstQuery(nl.ramsolutions.sw.magik.analysis.AstQuery) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) EnumSet(java.util.EnumSet) Nullable(javax.annotation.Nullable) GlobalScope(nl.ramsolutions.sw.magik.analysis.scope.GlobalScope) MethodInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) Slot(nl.ramsolutions.sw.magik.analysis.typing.types.Slot) Logger(org.slf4j.Logger) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) Collection(java.util.Collection) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) Parameter(nl.ramsolutions.sw.magik.analysis.typing.types.Parameter) NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) ParameterNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ParameterNodeHelper) AstWalker(nl.ramsolutions.sw.magik.analysis.AstWalker) Objects(java.util.Objects) CombinedType(nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType) List(java.util.List) ProcedureInstance(nl.ramsolutions.sw.magik.analysis.typing.types.ProcedureInstance) ScopeEntry(nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry) Scope(nl.ramsolutions.sw.magik.analysis.scope.Scope) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) CombinedType(nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) AstNode(com.sonar.sslr.api.AstNode)

Example 2 with CombinedType

use of nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType in project magik-tools by StevenLooman.

the class LocalTypeReasonerTest method testLoopResultOptional.

@Test
void testLoopResultOptional() {
    final String code = "" + "_method object.test\n" + "    _return _for i _over 1.upto(10)\n" + "            _loop\n" + "                _if a\n" + "                _then\n" + "                    _leave _with i\n" + "                _endif\n" + "            _endloop\n" + "_endmethod\n";
    // Set up TypeKeeper/TypeReasoner.
    final TypeKeeper typeKeeper = new TypeKeeper();
    final MagikType integerType = (MagikType) typeKeeper.getType(GlobalReference.of("sw:integer"));
    integerType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "upto()", Collections.emptyList(), null, new ExpressionResult(), new ExpressionResult(integerType));
    // Do analysis.
    final MagikTypedFile magikFile = this.createMagikFile(code, typeKeeper);
    final LocalTypeReasoner reasoner = magikFile.getTypeReasoner();
    // Assert user:object.test type determined.
    final AstNode topNode = magikFile.getTopNode();
    final AstNode methodNode = topNode.getFirstChild(MagikGrammar.METHOD_DEFINITION);
    final ExpressionResult result = reasoner.getNodeType(methodNode);
    assertThat(result.size()).isEqualTo(1);
    final CombinedType resultType = (CombinedType) result.get(0, null);
    assertThat(resultType).isNotNull();
    assertThat(resultType.getFullName()).isEqualTo("sw:integer|sw:unset");
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) CombinedType(nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) AstNode(com.sonar.sslr.api.AstNode) Test(org.junit.jupiter.api.Test)

Example 3 with CombinedType

use of nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType in project magik-tools by StevenLooman.

the class TypeMatcherTest method testCombinedTypeNotMatchesCombinedType.

@Test
void testCombinedTypeNotMatchesCombinedType() {
    final AbstractType type = new CombinedType(new IntrinsicType(GlobalReference.of("sw:type1")), new IntrinsicType(GlobalReference.of("sw:type2")));
    final AbstractType criterium = new CombinedType(new IntrinsicType(GlobalReference.of("sw:type2")), new IntrinsicType(GlobalReference.of("sw:type3")));
    final boolean matches = TypeMatcher.typeMatches(type, criterium);
    assertThat(matches).isFalse();
}
Also used : IntrinsicType(nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType) CombinedType(nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Test(org.junit.jupiter.api.Test)

Example 4 with CombinedType

use of nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType in project magik-tools by StevenLooman.

the class TypeMatcherTest method testCombinedTypeMatchesCombinedType.

@Test
void testCombinedTypeMatchesCombinedType() {
    final AbstractType type = new CombinedType(new IntrinsicType(GlobalReference.of("sw:type1")), new IntrinsicType(GlobalReference.of("sw:type2")));
    final AbstractType criterium = new CombinedType(new IntrinsicType(GlobalReference.of("sw:type1")), new IntrinsicType(GlobalReference.of("sw:type2")), new IntrinsicType(GlobalReference.of("sw:type3")));
    final boolean matches = TypeMatcher.typeMatches(type, criterium);
    assertThat(matches).isTrue();
}
Also used : IntrinsicType(nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType) CombinedType(nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Test(org.junit.jupiter.api.Test)

Example 5 with CombinedType

use of nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType in project magik-tools by StevenLooman.

the class TypeMatcherTest method testTypeNotMatchesCombinedType.

@Test
void testTypeNotMatchesCombinedType() {
    final AbstractType type = new IntrinsicType(GlobalReference.of("sw:type3"));
    final AbstractType criterium = new CombinedType(new IntrinsicType(GlobalReference.of("sw:type1")), new IntrinsicType(GlobalReference.of("sw:type2")));
    final boolean matches = TypeMatcher.typeMatches(type, criterium);
    assertThat(matches).isFalse();
}
Also used : IntrinsicType(nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType) CombinedType(nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Test(org.junit.jupiter.api.Test)

Aggregations

CombinedType (nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType)7 AbstractType (nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)6 Test (org.junit.jupiter.api.Test)5 IntrinsicType (nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType)4 AstNode (com.sonar.sslr.api.AstNode)3 ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)3 MagikTypedFile (nl.ramsolutions.sw.magik.MagikTypedFile)2 PackageNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper)2 ParameterNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.ParameterNodeHelper)2 GlobalScope (nl.ramsolutions.sw.magik.analysis.scope.GlobalScope)2 Scope (nl.ramsolutions.sw.magik.analysis.scope.Scope)2 ScopeEntry (nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1