Search in sources :

Example 1 with SlottedType

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

the class MagikIndexer method handleDefinition.

private void handleDefinition(final MagikFile magikFile, final SlottedExemplarDefinition definition) {
    final AstNode node = definition.getNode();
    final GlobalReference globalRef = definition.getGlobalReference();
    final MagikType type = this.typeKeeper.getType(globalRef) instanceof SlottedType ? (MagikType) this.typeKeeper.getType(globalRef) : new SlottedType(globalRef);
    this.typeKeeper.addType(type);
    final NewDocParser docParser = new NewDocParser(node);
    final Map<String, String> slotTypes = docParser.getSlotTypes();
    // This needs a default value ("") due to https://bugs.openjdk.java.net/browse/JDK-8148463
    final Map<String, String> slots = definition.getSlots().stream().map(SlottedExemplarDefinition.Slot::getName).collect(Collectors.toMap(slotName -> slotName, slotName -> slotTypes.getOrDefault(slotName, "")));
    final List<String> parents = definition.getParents();
    final MagikType defaultParentType = (MagikType) this.typeKeeper.getType(GlobalReference.of("sw:slotted_format_mixin"));
    this.fillType(type, magikFile, node, globalRef.getPakkage(), slots, parents, defaultParentType);
    final Path path = Paths.get(magikFile.getUri());
    this.indexedTypes.get(path).add(type);
    LOGGER.debug("Indexed type: {}", type);
}
Also used : NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) AstNode(com.sonar.sslr.api.AstNode) SlottedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition) IndexedType(nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType) LoggerFactory(org.slf4j.LoggerFactory) SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) Package(nl.ramsolutions.sw.magik.analysis.typing.types.Package) AliasType(nl.ramsolutions.sw.magik.analysis.typing.types.AliasType) IndexedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.IndexedExemplarDefinition) UndefinedType(nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType) EnumerationDefinition(nl.ramsolutions.sw.magik.analysis.definitions.EnumerationDefinition) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) EnumSet(java.util.EnumSet) MethodInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper) MethodDefinition(nl.ramsolutions.sw.magik.analysis.definitions.MethodDefinition) TypeAnnotationHandler(nl.ramsolutions.sw.magik.analysis.typing.TypeAnnotationHandler) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ParameterDefinition(nl.ramsolutions.sw.magik.analysis.definitions.ParameterDefinition) GlobalDefinition(nl.ramsolutions.sw.magik.analysis.definitions.GlobalDefinition) HashMap(java.util.HashMap) SelfType(nl.ramsolutions.sw.magik.analysis.typing.types.SelfType) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) HashSet(java.util.HashSet) TypeParser(nl.ramsolutions.sw.magik.analysis.typing.TypeParser) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Location(nl.ramsolutions.sw.magik.analysis.Location) Nullable(javax.annotation.Nullable) GlobalScope(nl.ramsolutions.sw.magik.analysis.scope.GlobalScope) MagikCommentExtractor(nl.ramsolutions.sw.magik.parser.MagikCommentExtractor) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) Token(com.sonar.sslr.api.Token) Slot(nl.ramsolutions.sw.magik.analysis.typing.types.Slot) Logger(org.slf4j.Logger) MagikFile(nl.ramsolutions.sw.magik.MagikFile) IOException(java.io.IOException) Parameter(nl.ramsolutions.sw.magik.analysis.typing.types.Parameter) NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) BinaryOperatorDefinition(nl.ramsolutions.sw.magik.analysis.definitions.BinaryOperatorDefinition) MixinDefinition(nl.ramsolutions.sw.magik.analysis.definitions.MixinDefinition) IntrinsicType(nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType) PackageDefinition(nl.ramsolutions.sw.magik.analysis.definitions.PackageDefinition) Paths(java.nio.file.Paths) Definition(nl.ramsolutions.sw.magik.analysis.definitions.Definition) ScopeEntry(nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry) Scope(nl.ramsolutions.sw.magik.analysis.scope.Scope) Collections(java.util.Collections) BinaryOperator(nl.ramsolutions.sw.magik.analysis.typing.BinaryOperator) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) Path(java.nio.file.Path) SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) AstNode(com.sonar.sslr.api.AstNode) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) SlottedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition)

Example 2 with SlottedType

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

the class LocalTypeReasonerTest method testReasonVariable2.

@Test
void testReasonVariable2() {
    final String code = "" + "_method object.test\n" + "    _local (var1, var2) << rope.new(1)\n" + "    var1.add(:a)\n" + "    _return var1\n" + "_endmethod\n";
    // Set up TypeKeeper/TypeReasoner.
    final TypeKeeper typeKeeper = new TypeKeeper();
    final MagikType ropeType = new SlottedType(GlobalReference.of("sw:rope"));
    ropeType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "new()", Collections.emptyList(), null, new ExpressionResult(ropeType));
    ropeType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "add()", Collections.emptyList(), null, new ExpressionResult());
    typeKeeper.addType(ropeType);
    // 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 MagikType resultType = (MagikType) result.get(0, null);
    assertThat(resultType).isNotNull();
    assertThat(resultType.getFullName()).isEqualTo("sw:rope");
}
Also used : SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) 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 SlottedType

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

the class LocalTypeReasonerTest method testReasonVariable.

@Test
void testReasonVariable() {
    final String code = "" + "_method object.test\n" + "    _local var << rope.new(1)\n" + "    var.add(:a)\n" + "    _return var\n" + "_endmethod\n";
    // Set up TypeKeeper/TypeReasoner.
    final TypeKeeper typeKeeper = new TypeKeeper();
    final MagikType ropeType = new SlottedType(GlobalReference.of("sw:rope"));
    ropeType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "new()", Collections.emptyList(), null, new ExpressionResult(ropeType));
    ropeType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "add()", Collections.emptyList(), null, new ExpressionResult());
    typeKeeper.addType(ropeType);
    // 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 MagikType resultType = (MagikType) result.get(0, null);
    assertThat(resultType).isNotNull();
    assertThat(resultType.getFullName()).isEqualTo("sw:rope");
}
Also used : SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) 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 4 with SlottedType

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

the class LocalTypeReasonerTest method testSingleNamedSuperType.

@Test
void testSingleNamedSuperType() {
    final String code = "" + "_method t.m\n" + "  _super(r).m\n" + "_endmethod";
    // Set up TypeKeeper/TypeReasoner.
    final TypeKeeper typeKeeper = new TypeKeeper();
    final MagikType rType = new SlottedType(GlobalReference.of("sw", "r"));
    typeKeeper.addType(rType);
    final MagikType sType = new SlottedType(GlobalReference.of("sw", "s"));
    typeKeeper.addType(sType);
    final MagikType tType = new SlottedType(GlobalReference.of("sw", "t"));
    tType.addParent(rType);
    tType.addParent(sType);
    typeKeeper.addType(tType);
    // Do analysis.
    final MagikTypedFile magikFile = this.createMagikFile(code, typeKeeper);
    final LocalTypeReasoner reasoner = magikFile.getTypeReasoner();
    final AstNode topNode = magikFile.getTopNode();
    final AstNode superNode = topNode.getFirstDescendant(MagikGrammar.SUPER).getParent();
    final ExpressionResult result = reasoner.getNodeType(superNode);
    assertThat(result).isNotNull();
    final AbstractType resultType0 = result.get(0, null);
    assertThat(resultType0).isNotNull().isEqualTo(rType);
}
Also used : SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) 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 5 with SlottedType

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

the class LocalTypeReasonerTest method testReasonMethodReturn.

@Test
void testReasonMethodReturn() {
    final String code = "" + "_package sw\n" + "_method object.test\n" + "    _return rope.new()\n" + "_endmethod\n";
    // Set up TypeKeeper/TypeReasoner.
    final TypeKeeper typeKeeper = new TypeKeeper();
    final MagikType ropeType = new SlottedType(GlobalReference.of("sw:rope"));
    ropeType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "new()", Collections.emptyList(), null, new ExpressionResult(ropeType));
    typeKeeper.addType(ropeType);
    // 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 MagikType resultType = (MagikType) result.get(0, null);
    assertThat(resultType).isNotNull();
    assertThat(resultType.getFullName()).isEqualTo("sw:rope");
}
Also used : SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) 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)

Aggregations

SlottedType (nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType)16 MagikType (nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)15 AstNode (com.sonar.sslr.api.AstNode)9 ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)9 Test (org.junit.jupiter.api.Test)9 MagikTypedFile (nl.ramsolutions.sw.magik.MagikTypedFile)7 GlobalReference (nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference)7 ITypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper)5 AbstractType (nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)5 URI (java.net.URI)3 Path (java.nio.file.Path)3 Collections (java.util.Collections)3 EnumSet (java.util.EnumSet)3 List (java.util.List)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 IndexedType (nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType)3 IntrinsicType (nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType)3 Token (com.sonar.sslr.api.Token)2 IOException (java.io.IOException)2