Search in sources :

Example 1 with PerlValue

use of com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue in project Perl5-IDEA by Camelcade.

the class PerlImplicitDeclarationsService method registerAnonSub.

@NotNull
public PerlImplicitSubDefinition registerAnonSub(@NotNull String namespaceName, @NotNull String baseName, @Nullable PerlValue returnValue) {
    PerlImplicitSubDefinition subDefinition = new PerlImplicitSubDefinition(myPsiManager, baseName, namespaceName, Collections.emptyList(), returnValue, true);
    doRegister(mySubsMap, subDefinition);
    return subDefinition;
}
Also used : PerlImplicitSubDefinition(com.perl5.lang.perl.psi.impl.PerlImplicitSubDefinition) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PerlValue

use of com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue in project Perl5-IDEA by Camelcade.

the class PerlMooseAttributeHandler method createMojoAttributes.

@NotNull
private List<PerlDelegatingLightNamedElement<?>> createMojoAttributes(@NotNull PerlSubCallElement subCallElement, @NotNull Pair<List<PsiElement>, List<PsiElement>> lists) {
    List<PsiElement> arguments = lists.second.subList(1, lists.second.size());
    PsiElement argument = ContainerUtil.getFirstItem(arguments);
    PerlSubExpr subExpr = ObjectUtils.tryCast(argument, PerlSubExpr.class);
    List<PerlDelegatingLightNamedElement<?>> result = new ArrayList<>();
    String namespaceName = PerlPackageUtil.getContextNamespaceName(subCallElement);
    for (PsiElement identifier : lists.first) {
        AtomicNotNullLazyValue<PerlValue> valueProvider = AtomicNotNullLazyValue.createValue(() -> PerlSmartGetterValue.create(subExpr != null ? PerlResolveUtil.computeReturnValueFromControlFlow(subExpr) : argument != null ? PerlValuesManager.from(argument) : PerlScalarValue.create(namespaceName)));
        PerlLightMethodDefinitionElement<PerlSubCallElement> newMethod = new PerlLightMethodDefinitionElement<>(subCallElement, ElementManipulators.getValueText(identifier), LIGHT_METHOD_DEFINITION, identifier, namespaceName, Arrays.asList(PerlSubArgument.self(), PerlSubArgument.optionalScalar(PerlSubArgument.NEW_VALUE_VALUE)), PerlSubAnnotations.tryToFindAnnotations(identifier, subCallElement.getParent()), valueProvider, subExpr == null ? null : subExpr.getBlock());
        result.add(newMethod);
    }
    return result;
}
Also used : PerlLightMethodDefinitionElement(com.perl5.lang.perl.psi.light.PerlLightMethodDefinitionElement) PerlDelegatingLightNamedElement(com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement) PerlValue(com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue) PerlSubCallElement(com.perl5.lang.perl.psi.impl.PerlSubCallElement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PerlValue

use of com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue in project Perl5-IDEA by Camelcade.

the class PerlSub method getReturnValue.

/**
 * Calculates return value. By default - checks annotations
 *
 * @param contextPackage package this sub been invoked from, useful to return $self
 * @param arguments      invocation arguments
 * @return type of return value if can be calculated, or {@link PerlValues#UNKNOWN_VALUE}
 */
@NotNull
default PerlValue getReturnValue() {
    PerlValue valueFromCode = getReturnValueFromCode();
    PerlValue annotationsValue = getReturnValueFromAnnotations();
    if (valueFromCode.isUnknown()) {
        return annotationsValue;
    }
    if (annotationsValue.isUnknown()) {
        return valueFromCode;
    }
    return PerlOneOfValue.builder().addVariant(valueFromCode).addVariant(annotationsValue).build();
}
Also used : PerlValue(com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PerlValue

use of com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue in project Perl5-IDEA by Camelcade.

the class PerlValuesManager method from.

@NotNull
public static PerlValue from(@NotNull PsiElement target, @Nullable PerlAssignExpression.PerlAssignValueDescriptor assignValueDescriptor) {
    if (assignValueDescriptor == null) {
        return UNKNOWN_VALUE;
    }
    List<PsiElement> elements = assignValueDescriptor.getElements();
    PerlContextType targetContextType = PerlContextType.from(target);
    if (targetContextType == PerlContextType.SCALAR) {
        int startIndex = assignValueDescriptor.getStartIndex();
        if (elements.size() == 1 && (PerlContextType.isScalar(elements.get(0)) || startIndex == -1)) {
            return PerlScalarContextValue.create(from(elements.get(0)));
        } else if (elements.size() > 1 || PerlContextType.isList(ContainerUtil.getFirstItem(elements))) {
            return PerlArrayElementValue.create(PerlArrayValue.builder().addPsiElements(elements).build(), PerlScalarValue.create(startIndex));
        }
    } else if (targetContextType == PerlContextType.LIST && assignValueDescriptor.getStartIndex() == 0) {
        IElementType elementType = PsiUtilCore.getElementType(target);
        if (elementType == HASH_VARIABLE || elementType == HASH_CAST_EXPR) {
            return PerlMapValue.builder().addPsiElements(elements).build();
        }
        return PerlArrayValue.builder().addPsiElements(elements).build();
    }
    return UNKNOWN_VALUE;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiElement(com.intellij.psi.PsiElement) PerlContextType(com.perl5.lang.perl.psi.utils.PerlContextType) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PerlValue

use of com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue in project Perl5-IDEA by Camelcade.

the class PerlOneOfValue method getContextType.

@Override
@Nullable
protected PerlContextType getContextType() {
    if (myVariants.isEmpty()) {
        return null;
    }
    PerlContextType contextType = null;
    boolean first = true;
    for (PerlValue variant : myVariants) {
        if (first) {
            contextType = variant.getContextType();
            first = false;
            continue;
        }
        if (contextType != variant.getContextType()) {
            return null;
        }
    }
    return contextType;
}
Also used : PerlContextType(com.perl5.lang.perl.psi.utils.PerlContextType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

NotNull (org.jetbrains.annotations.NotNull)13 PerlValue (com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue)11 PsiElement (com.intellij.psi.PsiElement)8 PerlCallValue (com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlCallValue)3 Instruction (com.intellij.codeInsight.controlflow.Instruction)2 IElementType (com.intellij.psi.tree.IElementType)2 PerlAssignInstruction (com.perl5.lang.perl.idea.codeInsight.controlFlow.PerlAssignInstruction)2 PerlMutationInstruction (com.perl5.lang.perl.idea.codeInsight.controlFlow.PerlMutationInstruction)2 PerlOneOfValue (com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlOneOfValue)2 PerlBuiltInVariable (com.perl5.lang.perl.psi.impl.PerlBuiltInVariable)2 PerlSubCallElement (com.perl5.lang.perl.psi.impl.PerlSubCallElement)2 PerlDelegatingLightNamedElement (com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement)2 PerlLightMethodDefinitionElement (com.perl5.lang.perl.psi.light.PerlLightMethodDefinitionElement)2 PerlContextType (com.perl5.lang.perl.psi.utils.PerlContextType)2 PerlSubAnnotations (com.perl5.lang.perl.psi.utils.PerlSubAnnotations)2 PerlSubArgument (com.perl5.lang.perl.psi.utils.PerlSubArgument)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 VirtualFileWindow (com.intellij.injected.editor.VirtualFileWindow)1 Application (com.intellij.openapi.application.Application)1