use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty in project intellij-community by JetBrains.
the class TypeInferenceHelper method getInitializerTypeFor.
@Nullable
public static PsiType getInitializerTypeFor(PsiElement element) {
final PsiElement parent = skipParentheses(element.getParent(), true);
if (parent instanceof GrAssignmentExpression) {
if (element instanceof GrIndexProperty) {
final GrExpression rvalue = ((GrAssignmentExpression) parent).getRValue();
//don't try to infer assignment type in case of index property because of infinite recursion (example: a[2]+=4)
return rvalue != null ? rvalue.getType() : null;
}
return ((GrAssignmentExpression) parent).getType();
}
if (parent instanceof GrTupleExpression) {
GrTupleExpression list = (GrTupleExpression) parent;
if (list.getParent() instanceof GrAssignmentExpression) {
// multiple assignment
final GrExpression rValue = ((GrAssignmentExpression) list.getParent()).getRValue();
int idx = list.indexOf(element);
if (idx >= 0 && rValue != null) {
PsiType rType = rValue.getType();
if (rType instanceof GrTupleType) {
PsiType[] componentTypes = ((GrTupleType) rType).getComponentTypes();
if (idx < componentTypes.length)
return componentTypes[idx];
return null;
}
return PsiUtil.extractIterableTypeParameter(rType, false);
}
}
}
if (parent instanceof GrUnaryExpression && TokenSets.POSTFIX_UNARY_OP_SET.contains(((GrUnaryExpression) parent).getOperationTokenType())) {
return ((GrUnaryExpression) parent).getType();
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitor method visitAssignmentExpression.
@Override
public void visitAssignmentExpression(@NotNull GrAssignmentExpression assignment) {
super.visitAssignmentExpression(assignment);
final GrExpression lValue = assignment.getLValue();
if (lValue instanceof GrIndexProperty)
return;
if (!PsiUtil.mightBeLValue(lValue))
return;
final IElementType opToken = assignment.getOperationTokenType();
if (opToken != GroovyTokenTypes.mASSIGN)
return;
final GrExpression rValue = assignment.getRValue();
if (rValue == null)
return;
if (lValue instanceof GrReferenceExpression && ((GrReferenceExpression) lValue).resolve() instanceof GrReferenceExpression) {
//lvalue is not-declared variable
return;
}
if (lValue instanceof GrTupleExpression) {
processTupleAssignment(((GrTupleExpression) lValue), rValue);
} else {
PsiType lValueNominalType = lValue.getNominalType();
final PsiType targetType = PsiImplUtil.isSpreadAssignment(lValue) ? extractIterableTypeParameter(lValueNominalType, false) : lValueNominalType;
if (targetType != null) {
processAssignment(targetType, rValue, lValue, "cannot.assign", assignment, ApplicableTo.ASSIGNMENT);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty in project intellij-community by JetBrains.
the class PsiUtil method getArgumentTypes.
@Nullable
public static PsiType[] getArgumentTypes(@Nullable PsiElement place, boolean nullAsBottom, @Nullable GrExpression stopAt) {
PsiElement parent = place instanceof GrEnumConstant ? place : place != null ? place.getParent() : null;
if (parent instanceof GrIndexProperty) {
GrIndexProperty index = (GrIndexProperty) parent;
PsiType[] argTypes = getArgumentTypes(index.getNamedArguments(), index.getExpressionArguments(), index.getClosureArguments(), nullAsBottom, stopAt);
if (isLValue(index) && argTypes != null) {
PsiType rawInitializer = TypeInferenceHelper.getInitializerTypeFor(index);
PsiType initializer = notNullizeType(rawInitializer, nullAsBottom, index);
return ArrayUtil.append(argTypes, initializer);
} else {
return argTypes;
}
}
if (parent instanceof GrCall) {
GrCall call = (GrCall) parent;
GrNamedArgument[] namedArgs = call.getNamedArguments();
GrExpression[] expressions = call.getExpressionArguments();
GrClosableBlock[] closures = call.getClosureArguments();
return getArgumentTypes(namedArgs, expressions, closures, nullAsBottom, stopAt);
} else if (parent instanceof GrAnonymousClassDefinition) {
final GrArgumentList argList = ((GrAnonymousClassDefinition) parent).getArgumentListGroovy();
if (argList == null) {
return getArgumentTypes(GrNamedArgument.EMPTY_ARRAY, GrExpression.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, nullAsBottom, stopAt);
} else {
return getArgumentTypes(argList.getNamedArguments(), argList.getExpressionArguments(), GrClosableBlock.EMPTY_ARRAY, nullAsBottom, stopAt);
}
} else if (parent instanceof GrBinaryExpression) {
GrExpression right = ((GrBinaryExpression) parent).getRightOperand();
PsiType type = right != null ? right.getType() : null;
return new PsiType[] { notNullizeType(type, nullAsBottom, parent) };
} else if (parent instanceof GrAssignmentExpression) {
PsiType type = ((GrAssignmentExpression) parent).getType();
return new PsiType[] { notNullizeType(type, nullAsBottom, parent) };
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty in project intellij-community by JetBrains.
the class IndexedExpressionConversionIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrIndexProperty arrayIndexExpression = (GrIndexProperty) element;
final GrArgumentList argList = (GrArgumentList) arrayIndexExpression.getLastChild();
assert argList != null;
final GrExpression[] arguments = argList.getExpressionArguments();
final PsiElement parent = element.getParent();
final GrExpression arrayExpression = arrayIndexExpression.getInvokedExpression();
if (!(parent instanceof GrAssignmentExpression)) {
rewriteAsGetAt(arrayIndexExpression, arrayExpression, arguments[0]);
return;
}
final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) parent;
final GrExpression rhs = assignmentExpression.getRValue();
if (rhs.equals(element)) {
rewriteAsGetAt(arrayIndexExpression, arrayExpression, arguments[0]);
} else {
rewriteAsSetAt(assignmentExpression, arrayExpression, arguments[0], rhs);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty in project intellij-community by JetBrains.
the class IndexedExpressionConversionPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrIndexProperty))
return false;
if (ErrorUtil.containsError(element))
return false;
final GrIndexProperty arrayIndexExpression = (GrIndexProperty) element;
final PsiElement lastChild = arrayIndexExpression.getLastChild();
if (!(lastChild instanceof GrArgumentList))
return false;
final GrArgumentList argList = (GrArgumentList) lastChild;
final GrExpression[] arguments = argList.getExpressionArguments();
if (arguments.length != 1)
return false;
final PsiElement parent = element.getParent();
if (!(parent instanceof GrAssignmentExpression)) {
return true;
}
final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) parent;
final GrExpression rvalue = assignmentExpression.getRValue();
if (rvalue == null)
return false;
if (rvalue.equals(element))
return true;
final IElementType operator = assignmentExpression.getOperationTokenType();
return GroovyTokenTypes.mASSIGN.equals(operator);
}
Aggregations