use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement in project intellij-community by JetBrains.
the class InitialInfo method inferOutputType.
@Nullable
private PsiType inferOutputType(VariableInfo[] outputInfos, GrStatement[] statements, ArrayList<GrStatement> returnStatements, boolean hasReturnValue, StringPartInfo stringPartInfo) {
if (stringPartInfo != null) {
return stringPartInfo.getLiteral().getType();
}
PsiType outputType = PsiType.VOID;
if (outputInfos.length > 0) {
if (outputInfos.length == 1) {
outputType = outputInfos[0].getType();
} else {
outputType = JavaPsiFacade.getElementFactory(myProject).createTypeFromText(CommonClassNames.JAVA_UTIL_LIST, getContext());
}
} else if (ExtractUtil.isSingleExpression(statements)) {
outputType = ((GrExpression) statements[0]).getType();
} else if (hasReturnValue) {
assert !returnStatements.isEmpty();
List<PsiType> types = new ArrayList<>(returnStatements.size());
for (GrStatement statement : returnStatements) {
if (statement instanceof GrReturnStatement) {
GrExpression returnValue = ((GrReturnStatement) statement).getReturnValue();
if (returnValue != null) {
types.add(returnValue.getType());
}
} else if (statement instanceof GrExpression) {
types.add(((GrExpression) statement).getType());
}
}
outputType = TypesUtil.getLeastUpperBoundNullable(types, getContext().getManager());
}
return outputType;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement in project intellij-community by JetBrains.
the class GroovyMethodInliner method isOnExpressionOrReturnPlace.
/*
Method call is used as expression in some enclosing expression or
is method return result
*/
private static boolean isOnExpressionOrReturnPlace(@NotNull GrCallExpression call) {
PsiElement parent = call.getParent();
if (!(parent instanceof GrVariableDeclarationOwner)) {
return true;
}
// tail calls in methods and closures
GrVariableDeclarationOwner owner = (GrVariableDeclarationOwner) parent;
if (owner instanceof GrClosableBlock || owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) {
GrStatement[] statements = ((GrCodeBlock) owner).getStatements();
assert statements.length > 0;
GrStatement last = statements[statements.length - 1];
if (last == call)
return true;
if (last instanceof GrReturnStatement && call == ((GrReturnStatement) last).getReturnValue()) {
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement in project intellij-community by JetBrains.
the class GroovyInlineMethodUtil method isTailMethodCall.
/**
* Checks whether given method call is tail call of other method or closure
*
* @param call [tail?] Method call
* @return
*/
static boolean isTailMethodCall(GrCallExpression call) {
GrStatement stmt = call;
PsiElement parent = call.getParent();
// return statement
if (parent instanceof GrReturnStatement) {
stmt = ((GrReturnStatement) parent);
parent = parent.getParent();
}
// method body result
if (parent instanceof GrOpenBlock) {
if (parent.getParent() instanceof GrMethod) {
GrStatement[] statements = ((GrOpenBlock) parent).getStatements();
return statements.length > 0 && stmt == statements[statements.length - 1];
}
}
// closure result
if (parent instanceof GrClosableBlock) {
GrStatement[] statements = ((GrClosableBlock) parent).getStatements();
return statements.length > 0 && stmt == statements[statements.length - 1];
}
// todo test me!
if (stmt instanceof GrReturnStatement) {
GrMethod method = PsiTreeUtil.getParentOfType(stmt, GrMethod.class);
if (method != null) {
Collection<GrStatement> returnStatements = ControlFlowUtils.collectReturns(method.getBlock());
return returnStatements.contains(stmt) && !hasBadReturns(method);
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement in project intellij-community by JetBrains.
the class GroovyInlineMethodUtil method checkTailIfStatement.
public static boolean checkTailIfStatement(GrIfStatement ifStatement, Collection<GrStatement> returnStatements) {
GrStatement thenBranch = ifStatement.getThenBranch();
GrStatement elseBranch = ifStatement.getElseBranch();
if (elseBranch == null)
return false;
boolean tb = false;
boolean eb = false;
if (thenBranch instanceof GrReturnStatement) {
tb = returnStatements.remove(thenBranch);
} else if (thenBranch instanceof GrBlockStatement) {
tb = checkTailOpenBlock(((GrBlockStatement) thenBranch).getBlock(), returnStatements);
}
if (elseBranch instanceof GrReturnStatement) {
eb = returnStatements.remove(elseBranch);
} else if (elseBranch instanceof GrBlockStatement) {
eb = checkTailOpenBlock(((GrBlockStatement) elseBranch).getBlock(), returnStatements);
}
return tb && eb;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement in project intellij-community by JetBrains.
the class GroovyMethodInliner method getAloneResultExpression.
/**
* Get method result expression (if it is alone in method)
*
* @return null if method has more or less than one return statement or has void type
*/
@Nullable
static GrExpression getAloneResultExpression(@NotNull GrMethod method) {
GrOpenBlock body = method.getBlock();
assert body != null;
GrStatement[] statements = body.getStatements();
if (statements.length == 1) {
if (statements[0] instanceof GrExpression)
return (GrExpression) statements[0];
if (statements[0] instanceof GrReturnStatement) {
GrExpression value = ((GrReturnStatement) statements[0]).getReturnValue();
if (value == null && !PsiType.VOID.equals(PsiUtil.getSmartReturnType(method))) {
return GroovyPsiElementFactory.getInstance(method.getProject()).createExpressionFromText("null");
}
return value;
}
}
return null;
}
Aggregations