Search in sources :

Example 1 with AbstractType

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

the class HoverProvider method buildMethodDoc.

/**
 * Build hover text for method doc.
 * @param typeKeeper TypeKeeper.
 * @param reasoner LocalTypeReasoner.
 * @param node AstNode, METHOD_INVOCATION.
 * @param methodName Name of invoked method.
 * @param builder {{StringBuilder}} to fill.
 */
private void buildMethodDoc(final ITypeKeeper typeKeeper, final LocalTypeReasoner reasoner, final AstNode node, final String methodName, final StringBuilder builder) {
    // Get type from reasoner.
    final ExpressionResult result = reasoner.getNodeType(node);
    // We know what self is.
    AbstractType type = result.get(0, UndefinedType.INSTANCE);
    if (type == SelfType.INSTANCE) {
        final AstNode methodDefNode = node.getFirstAncestor(MagikGrammar.METHOD_DEFINITION);
        if (methodDefNode == null) {
            // This can happen in case of a procedure definition calling a method on _self.
            type = UndefinedType.INSTANCE;
        } else {
            final MethodDefinitionNodeHelper helper = new MethodDefinitionNodeHelper(methodDefNode);
            final GlobalReference globalRef = helper.getTypeGlobalReference();
            type = typeKeeper.getType(globalRef);
        }
    }
    // Get method info.
    final Collection<Method> methods = type.getMethods(methodName);
    if (methods.isEmpty()) {
        this.buildMethodUnknownDoc(type, methodName, builder);
        return;
    }
    methods.forEach(method -> this.buildMethodSignatureDoc(method, builder));
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) AstNode(com.sonar.sslr.api.AstNode)

Example 2 with AbstractType

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

the class HoverProvider method buildTypeDoc.

/**
 * Build hover text for type doc.
 * @param typeKeeper TypeKeeper to use.
 * @param globalReference Global reference to type.
 * @param builder {{StringBuilder}} to fill.
 */
private void buildTypeDoc(final ITypeKeeper typeKeeper, final GlobalReference globalReference, final StringBuilder builder) {
    final AbstractType type = typeKeeper.getType(globalReference);
    this.buildTypeSignatureDoc(type, builder);
}
Also used : AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)

Example 3 with AbstractType

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

the class HoverProvider method buildTypeDoc.

/**
 * Build hover text for type doc.
 * @param typeKeeper TypeKeeper.
 * @param reasoner {{LocalTypeReasoner}} which has reasoned over the AST.
 * @param node {{AstNode}} to get info from.
 * @param builder {{StringBuilder}} to fill.
 */
private void buildTypeDoc(final ITypeKeeper typeKeeper, final LocalTypeReasoner reasoner, final AstNode node, final StringBuilder builder) {
    // Get type from reasoner.
    final ExpressionResult result = reasoner.getNodeType(node);
    // We know what self is.
    AbstractType type = result.get(0, UndefinedType.INSTANCE);
    if (type == SelfType.INSTANCE) {
        final AstNode methodDefNode = node.getFirstAncestor(MagikGrammar.METHOD_DEFINITION);
        if (methodDefNode == null) {
            // This can happen in case of a procedure definition calling a method on _self.
            type = UndefinedType.INSTANCE;
        } else {
            final MethodDefinitionNodeHelper methodDefHelper = new MethodDefinitionNodeHelper(methodDefNode);
            final GlobalReference globalRef = methodDefHelper.getTypeGlobalReference();
            type = typeKeeper.getType(globalRef);
        }
    }
    this.buildTypeSignatureDoc(type, builder);
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) AstNode(com.sonar.sslr.api.AstNode)

Example 4 with AbstractType

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

the class LocalTypeReasoner method walkPostSlot.

// region: Atoms
@Override
protected void walkPostSlot(final AstNode node) {
    // Get class type.
    final AbstractType type = this.getMethodOwnerType(node);
    if (type == UndefinedType.INSTANCE) {
        LOGGER.debug("Unknown type for node: {}", node);
        return;
    }
    // Get slot type.
    final AstNode identifierNode = node.getFirstChild(MagikGrammar.IDENTIFIER);
    final String slotName = identifierNode.getTokenValue();
    final Slot slot = type.getSlot(slotName);
    final AbstractType slotType = slot != null ? slot.getType() : UndefinedType.INSTANCE;
    Objects.requireNonNull(slotType);
    this.assignAtom(node, slotType);
}
Also used : AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Slot(nl.ramsolutions.sw.magik.analysis.typing.types.Slot) AstNode(com.sonar.sslr.api.AstNode)

Example 5 with AbstractType

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

the class LocalTypeReasoner method walkPostBody.

@Override
protected void walkPostBody(final AstNode node) {
    // Get result from upper EXPRESSION.
    final AstNode expressionNode = node.getFirstAncestor(MagikGrammar.EXPRESSION);
    if (expressionNode == null) {
        // Happens with a return, don't do anything.
        return;
    }
    ExpressionResult result = this.getNodeType(expressionNode);
    // BODYs don't always have to result in something.
    // Find STATEMENT -> RETURN/EMIT/LEAVE
    // TODO: what about _loop _if .. _then _leave _with _endif _leave _with _endloop
    AstNode resultingNode = AstQuery.getFirstChildFromChain(node, MagikGrammar.STATEMENT, MagikGrammar.RETURN_STATEMENT);
    if (resultingNode == null) {
        resultingNode = AstQuery.getFirstChildFromChain(node, MagikGrammar.STATEMENT, MagikGrammar.EMIT_STATEMENT);
    }
    if (resultingNode == null) {
        resultingNode = AstQuery.getFirstChildFromChain(node, MagikGrammar.STATEMENT, MagikGrammar.LEAVE_STATEMENT);
    }
    if (resultingNode == null) {
        // Result can also be an unset, as no resulting statement was found.
        // TODO: but... "_block _block _return 1 _endblock _endblock"
        final ExpressionResult emptyResult = new ExpressionResult();
        final AbstractType unsetType = this.typeKeeper.getType(SW_UNSET);
        result = new ExpressionResult(result, emptyResult, unsetType);
    }
    // Set parent EXPRESSION result.
    this.setNodeType(expressionNode, result);
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) AstNode(com.sonar.sslr.api.AstNode)

Aggregations

AbstractType (nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)81 ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)46 AstNode (com.sonar.sslr.api.AstNode)45 Test (org.junit.jupiter.api.Test)34 MagikType (nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)21 Method (nl.ramsolutions.sw.magik.analysis.typing.types.Method)21 ITypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper)20 GlobalReference (nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference)20 MagikTypedFile (nl.ramsolutions.sw.magik.MagikTypedFile)19 IntrinsicType (nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType)14 List (java.util.List)13 MethodInvocationNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper)13 GlobalScope (nl.ramsolutions.sw.magik.analysis.scope.GlobalScope)13 Scope (nl.ramsolutions.sw.magik.analysis.scope.Scope)13 Parameter (nl.ramsolutions.sw.magik.analysis.typing.types.Parameter)12 Collectors (java.util.stream.Collectors)11 ScopeEntry (nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry)11 Collections (java.util.Collections)10 TypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.TypeKeeper)10 UndefinedType (nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType)10