use of org.codehaus.groovy.ast.stmt.SwitchStatement in project groovy by apache.
the class AntlrParserPlugin method switchStatement.
protected Statement switchStatement(AST switchNode) {
AST node = switchNode.getFirstChild();
Expression expression = expression(node);
Statement defaultStatement = EmptyStatement.INSTANCE;
List list = new ArrayList();
for (node = node.getNextSibling(); isType(CASE_GROUP, node); node = node.getNextSibling()) {
Statement tmpDefaultStatement;
AST child = node.getFirstChild();
if (isType(LITERAL_case, child)) {
List cases = new LinkedList();
// default statement can be grouped with previous case
tmpDefaultStatement = caseStatements(child, cases);
list.addAll(cases);
} else {
tmpDefaultStatement = statement(child.getNextSibling());
}
if (tmpDefaultStatement != EmptyStatement.INSTANCE) {
if (defaultStatement == EmptyStatement.INSTANCE) {
defaultStatement = tmpDefaultStatement;
} else {
throw new ASTRuntimeException(switchNode, "The default case is already defined.");
}
}
}
if (node != null) {
unknownAST(node);
}
SwitchStatement switchStatement = new SwitchStatement(expression, list, defaultStatement);
configureAST(switchStatement, switchNode);
return switchStatement;
}
use of org.codehaus.groovy.ast.stmt.SwitchStatement in project groovy by apache.
the class ReturnAdder method addReturnsIfNeeded.
private Statement addReturnsIfNeeded(final Statement statement, final VariableScope scope) {
if (statement instanceof ReturnStatement || statement instanceof ThrowStatement || statement instanceof EmptyStatement || statement instanceof BytecodeSequence) {
return statement;
}
if (statement == null) {
ReturnStatement returnStatement = new ReturnStatement(nullX());
listener.returnStatementAdded(returnStatement);
return returnStatement;
}
if (statement instanceof ExpressionStatement) {
Expression expression = ((ExpressionStatement) statement).getExpression();
ReturnStatement returnStatement = new ReturnStatement(expression);
returnStatement.copyStatementLabels(statement);
returnStatement.setSourcePosition(statement.getLineNumber() < 0 ? expression : statement);
listener.returnStatementAdded(returnStatement);
return returnStatement;
}
if (statement instanceof SynchronizedStatement) {
SynchronizedStatement syncStatement = (SynchronizedStatement) statement;
Statement code = addReturnsIfNeeded(syncStatement.getCode(), scope);
if (doAdd)
syncStatement.setCode(code);
return syncStatement;
}
if (statement instanceof IfStatement) {
IfStatement ifElseStatement = (IfStatement) statement;
Statement ifBlock = addReturnsIfNeeded(ifElseStatement.getIfBlock(), scope);
Statement elseBlock = addReturnsIfNeeded(ifElseStatement.getElseBlock(), scope);
if (doAdd) {
ifElseStatement.setIfBlock(ifBlock);
ifElseStatement.setElseBlock(elseBlock);
}
return ifElseStatement;
}
if (statement instanceof SwitchStatement) {
SwitchStatement switchStatement = (SwitchStatement) statement;
Statement defaultStatement = switchStatement.getDefaultStatement();
List<CaseStatement> caseStatements = switchStatement.getCaseStatements();
for (Iterator<CaseStatement> it = caseStatements.iterator(); it.hasNext(); ) {
CaseStatement caseStatement = it.next();
Statement code = adjustSwitchCaseCode(caseStatement.getCode(), scope, // GROOVY-9896: return if no default and last case lacks break
defaultStatement == EmptyStatement.INSTANCE && !it.hasNext());
if (doAdd)
caseStatement.setCode(code);
}
defaultStatement = adjustSwitchCaseCode(defaultStatement, scope, true);
if (doAdd)
switchStatement.setDefaultStatement(defaultStatement);
return switchStatement;
}
if (statement instanceof TryCatchStatement) {
TryCatchStatement tryCatchFinally = (TryCatchStatement) statement;
boolean[] missesReturn = new boolean[1];
new ReturnAdder(returnStatement -> missesReturn[0] = true).addReturnsIfNeeded(tryCatchFinally.getFinallyStatement(), scope);
boolean hasFinally = !(tryCatchFinally.getFinallyStatement() instanceof EmptyStatement);
// there is nothing to do
if (hasFinally && !missesReturn[0])
return tryCatchFinally;
// add returns to try and catch blocks
Statement tryStatement = addReturnsIfNeeded(tryCatchFinally.getTryStatement(), scope);
if (doAdd)
tryCatchFinally.setTryStatement(tryStatement);
for (CatchStatement catchStatement : tryCatchFinally.getCatchStatements()) {
Statement code = addReturnsIfNeeded(catchStatement.getCode(), scope);
if (doAdd)
catchStatement.setCode(code);
}
return tryCatchFinally;
}
if (statement instanceof BlockStatement) {
BlockStatement blockStatement = (BlockStatement) statement;
if (blockStatement.isEmpty()) {
ReturnStatement returnStatement = new ReturnStatement(nullX());
returnStatement.copyStatementLabels(blockStatement);
returnStatement.setSourcePosition(blockStatement);
listener.returnStatementAdded(returnStatement);
return returnStatement;
} else {
List<Statement> statements = blockStatement.getStatements();
int lastIndex = statements.size() - 1;
Statement last = addReturnsIfNeeded(statements.get(lastIndex), blockStatement.getVariableScope());
if (doAdd)
statements.set(lastIndex, last);
return blockStatement;
}
}
List<Statement> statements = new ArrayList<>(2);
statements.add(statement);
ReturnStatement returnStatement = new ReturnStatement(nullX());
listener.returnStatementAdded(returnStatement);
statements.add(returnStatement);
BlockStatement blockStatement = new BlockStatement(statements, new VariableScope(scope));
blockStatement.setSourcePosition(statement);
return blockStatement;
}
use of org.codehaus.groovy.ast.stmt.SwitchStatement in project groovy by apache.
the class FinalVariableAnalyzer method visitSwitch.
@Override
public void visitSwitch(SwitchStatement switchS) {
visitStatement(switchS);
switchS.getExpression().visit(this);
List<Statement> branches = new ArrayList<>(switchS.getCaseStatements());
if (!(switchS.getDefaultStatement() instanceof EmptyStatement)) {
branches.add(switchS.getDefaultStatement());
}
List<Map<Variable, VariableState>> afterStates = new ArrayList<>();
// collect after states
int lastIndex = branches.size() - 1;
for (int i = 0; i <= lastIndex; i++) {
pushState();
boolean done = false;
boolean returning = false;
for (int j = i; !done; j++) {
Statement branch = branches.get(j);
// default branch
Statement block = branch;
if (branch instanceof CaseStatement) {
CaseStatement caseS = (CaseStatement) branch;
block = caseS.getCode();
caseS.getExpression().visit(this);
}
block.visit(this);
done = j == lastIndex || !fallsThrough(block);
if (done) {
returning = returningBlock(block);
}
}
if (!returning) {
afterStates.add(getState());
}
popState();
}
if (afterStates.isEmpty()) {
return;
}
// merge branches
Map<Variable, VariableState> beforeState = getState();
Set<Variable> allVars = new HashSet<>(beforeState.keySet());
for (Map<Variable, VariableState> map : afterStates) {
allVars.addAll(map.keySet());
}
for (Variable var : allVars) {
VariableState beforeValue = beforeState.get(var);
if (beforeValue != null) {
final VariableState merged = afterStates.get(0).get(var);
if (merged != null) {
if (afterStates.stream().allMatch(state -> merged.equals(state.get(var)))) {
beforeState.put(var, merged);
} else {
VariableState different = beforeValue == VariableState.is_uninitialized ? VariableState.is_ambiguous : VariableState.is_var;
beforeState.put(var, different);
}
}
}
}
}
use of org.codehaus.groovy.ast.stmt.SwitchStatement in project groovy by apache.
the class AntlrParserPlugin method statement.
// Statements
//-------------------------------------------------------------------------
protected Statement statement(AST node) {
Statement statement = null;
int type = node.getType();
switch(type) {
case SLIST:
case LITERAL_finally:
statement = statementList(node);
break;
case METHOD_CALL:
statement = methodCall(node);
break;
case VARIABLE_DEF:
statement = variableDef(node);
break;
case LABELED_STAT:
return labelledStatement(node);
case LITERAL_assert:
statement = assertStatement(node);
break;
case LITERAL_break:
statement = breakStatement(node);
break;
case LITERAL_continue:
statement = continueStatement(node);
break;
case LITERAL_if:
statement = ifStatement(node);
break;
case LITERAL_for:
statement = forStatement(node);
break;
case LITERAL_return:
statement = returnStatement(node);
break;
case LITERAL_synchronized:
statement = synchronizedStatement(node);
break;
case LITERAL_switch:
statement = switchStatement(node);
break;
case LITERAL_try:
statement = tryStatement(node);
break;
case LITERAL_throw:
statement = throwStatement(node);
break;
case LITERAL_while:
statement = whileStatement(node);
break;
default:
statement = new ExpressionStatement(expression(node));
}
if (statement != null) {
configureAST(statement, node);
}
return statement;
}
use of org.codehaus.groovy.ast.stmt.SwitchStatement in project groovy by apache.
the class EnumTypeCheckingExtension method handleUnresolvedVariableExpression.
@Override
public boolean handleUnresolvedVariableExpression(VariableExpression vexp) {
SwitchStatement switchStatement = this.typeCheckingVisitor.typeCheckingContext.getEnclosingSwitchStatement();
if (null == switchStatement)
return false;
ClassNode type = switchStatement.getExpression().getNodeMetaData(StaticTypesMarker.TYPE);
if (null == type)
return false;
if (type.isEnum()) {
FieldNode fieldNode = type.redirect().getField(vexp.getName());
if (null != fieldNode) {
int modifiers = fieldNode.getModifiers();
if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers) && type.equals(fieldNode.getType())) {
vexp.putNodeMetaData(SWITCH_CONDITION_EXPRESSION_TYPE, type);
return true;
}
}
}
return false;
}
Aggregations