Search in sources :

Example 1 with PerlValuableEntity

use of com.perl5.lang.perl.psi.properties.PerlValuableEntity in project Perl5-IDEA by Camelcade.

the class PerlLightTestCaseBase method doTestPerlValueWithoutInit.

protected void doTestPerlValueWithoutInit(@NotNull PerlValuableEntity element) {
    StringBuilder sb = new StringBuilder();
    PerlValue elementValue = PerlValuesManager.from(element);
    sb.append(getEditorTextWithCaretsAndSelections().trim()).append(SEPARATOR_NEWLINES).append(element.getText()).append("\n").append(serializePsiElement(element)).append("\n").append(elementValue.getPresentableText());
    sb.append(SEPARATOR_NEWLINES).append("Resolved").append(SEPARATOR_NEWLINES).append(elementValue.resolve(element).getPresentableText());
    UsefulTestCase.assertSameLinesWithFile(getTestResultsFilePath(), sb.toString());
}
Also used : PerlValue(com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue)

Example 2 with PerlValuableEntity

use of com.perl5.lang.perl.psi.properties.PerlValuableEntity in project Perl5-IDEA by Camelcade.

the class PerlValuesManager method computeValue.

@NotNull
private static PerlValue computeValue(@NotNull PsiElement element) {
    if (element instanceof PerlVariableDeclarationExpr) {
        if (((PerlVariableDeclarationExpr) element).isParenthesized()) {
            return PerlArrayValue.builder().addPsiElements(Arrays.asList(element.getChildren())).build();
        }
        PsiElement[] children = element.getChildren();
        return children.length == 1 ? from(children[0]) : UNKNOWN_VALUE;
    }
    if (element instanceof PerlReturnExpr) {
        PsiPerlExpr expr = ((PerlReturnExpr) element).getReturnValueExpr();
        return expr == null ? UNDEF_VALUE : from(expr);
    } else if (element instanceof PerlValuableEntity) {
        return ((PerlValuableEntity) element).computePerlValue();
    }
    IElementType elementType = PsiUtilCore.getElementType(element);
    if (elementType == UNDEF_EXPR) {
        return UNDEF_VALUE;
    } else if (elementType == SCALAR_EXPR) {
        PsiElement[] children = element.getChildren();
        return children.length == 0 ? UNKNOWN_VALUE : PerlScalarContextValue.create(from(children[0]));
    } else if (elementType == TERNARY_EXPR) {
        PerlOneOfValue.Builder builder = PerlOneOfValue.builder();
        PsiElement[] children = element.getChildren();
        for (int i = 1; i < children.length; i++) {
            builder.addVariant(children[i]);
        }
        return builder.build();
    } else if (ONE_OF_VALUES.contains(elementType)) {
        return PerlOneOfValue.builder().addVariants(element.getChildren()).build();
    } else if (LIST_VALUES.contains(elementType)) {
        return PerlArrayValue.builder().addPsiElements(PerlArrayUtil.collectListElements(element)).build();
    } else if (elementType == ANON_ARRAY) {
        return PerlReferenceValue.create(PerlArrayValue.builder().addPsiElements(Arrays.asList(element.getChildren())).build());
    } else if (elementType == ANON_HASH) {
        return PerlReferenceValue.create(PerlMapValue.builder().addPsiElements(Arrays.asList(element.getChildren())).build());
    } else if (elementType == NUMBER_CONSTANT) {
        return PerlScalarValue.create(element.getText());
    } else if (element instanceof PsiPerlHashIndex) {
        PsiElement parent = element.getParent();
        if (parent instanceof PsiPerlDerefExpr) {
            return PerlHashElementValue.create(PerlHashDereferenceValue.create(from(((PsiPerlDerefExpr) parent).getPreviousElement(element))), from(((PsiPerlHashIndex) element).getExpr()));
        }
    } else if (element instanceof PsiPerlArrayIndex) {
        PsiElement parent = element.getParent();
        if (parent instanceof PsiPerlDerefExpr) {
            return PerlArrayElementValue.create(PerlArrayDereferenceValue.create(from(((PsiPerlDerefExpr) parent).getPreviousElement(element))), from(((PsiPerlArrayIndex) element).getExpr()));
        }
    } else if (element instanceof PsiPerlPrefixUnaryExpr) {
        PsiPerlExpr target = ((PsiPerlPrefixUnaryExpr) element).getExpr();
        IElementType firstChildElementType = PsiUtilCore.getElementType(element.getFirstChild());
        if (firstChildElementType == OPERATOR_MINUS) {
            return PerlArithmeticNegationValue.create(from(target));
        } else if (firstChildElementType == OPERATOR_PLUS) {
            return from(target);
        }
    } else if (element instanceof PsiPerlRefExpr) {
        return PerlReferenceValue.create(from(((PsiPerlRefExpr) element).getExpr()));
    } else if (element instanceof PsiPerlHashElement) {
        return PerlHashElementValue.create(from(((PsiPerlHashElement) element).getExpr()), from(((PsiPerlHashElement) element).getHashIndex().getExpr()));
    } else if (element instanceof PsiPerlArrayElement) {
        return PerlArrayElementValue.create(from(((PsiPerlArrayElement) element).getExpr()), from(((PsiPerlArrayElement) element).getArrayIndex().getExpr()));
    } else if (element instanceof PsiPerlHashSlice) {
        return PerlHashSliceValue.create(from(((PsiPerlHashSlice) element).getExpr()), listContext(from(((PsiPerlHashSlice) element).getHashIndex().getExpr())));
    } else if (element instanceof PsiPerlArraySlice) {
        return PerlArraySliceValue.create(from(((PsiPerlArraySlice) element).getExpr()), listContext(from(((PsiPerlArraySlice) element).getArrayIndex().getExpr())));
    } else if (element instanceof PsiPerlScalarCastExpr) {
        PsiPerlExpr expr = ((PsiPerlScalarCastExpr) element).getExpr();
        if (expr == null) {
            expr = PerlPsiUtil.getSingleBlockExpression(((PsiPerlScalarCastExpr) element).getBlock());
        }
        return PerlScalarDereferenceValue.create(from(expr));
    } else if (element instanceof PsiPerlArrayCastExpr) {
        PsiPerlExpr expr = ((PsiPerlArrayCastExpr) element).getExpr();
        if (expr == null) {
            expr = PerlPsiUtil.getSingleBlockExpression(((PsiPerlArrayCastExpr) element).getBlock());
        }
        return PerlArrayDereferenceValue.create(from(expr));
    } else if (element instanceof PsiPerlHashCastExpr) {
        PsiPerlExpr expr = ((PsiPerlHashCastExpr) element).getExpr();
        if (expr == null) {
            expr = PerlPsiUtil.getSingleBlockExpression(((PsiPerlHashCastExpr) element).getBlock());
        }
        return PerlHashDereferenceValue.create(from(expr));
    } else if (element instanceof PsiPerlBlessExpr) {
        PerlValue targetValue = from(((PsiPerlBlessExpr) element).getReferenceExpression());
        PsiElement blessExpression = ((PsiPerlBlessExpr) element).getBlessExpression();
        PerlValue blessValue = blessExpression != null ? from(blessExpression) : PerlPackageUtil.getContextType(element);
        return PerlBlessedValue.create(targetValue, blessValue);
    } else if (element instanceof PsiPerlArrayUnshiftExpr) {
        return PerlScalarContextValue.create(PerlUnshiftValue.create((PsiPerlArrayUnshiftExpr) element));
    } else if (element instanceof PsiPerlArrayPushExpr) {
        return PerlScalarContextValue.create(PerlPushValue.create((PsiPerlArrayPushExpr) element));
    } else if (element instanceof PsiPerlArrayShiftExpr) {
        return createShiftPopValue((PsiPerlArrayShiftExpr) element, FIRST_ELEMENT_INDEX_VALUE);
    } else if (element instanceof PsiPerlArrayPopExpr) {
        return createShiftPopValue((PsiPerlArrayPopExpr) element, LAST_ELEMENT_INDEX_VALUE);
    }
    return UNKNOWN_VALUE;
}
Also used : PerlValuableEntity(com.perl5.lang.perl.psi.properties.PerlValuableEntity) PsiElement(com.intellij.psi.PsiElement) IElementType(com.intellij.psi.tree.IElementType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 IElementType (com.intellij.psi.tree.IElementType)1 PerlValue (com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue)1 PerlValuableEntity (com.perl5.lang.perl.psi.properties.PerlValuableEntity)1 NotNull (org.jetbrains.annotations.NotNull)1