root / trunk / compiler / ooasCompiler / src / org / momut / ooas / visitors / optimisation / OoaJoinSequentialCompositionBlocksVisitor.java @ 7
1 |
/**
|
---|---|
2 |
*
|
3 |
* OOAS Compiler
|
4 |
*
|
5 |
* Copyright 2015, AIT Austrian Institute of Technology.
|
6 |
* This code is based on the C# Version of the OOAS Compiler, which is
|
7 |
* copyright 2015 by the Institute of Software Technology, Graz University
|
8 |
* of Technology with portions copyright by the AIT Austrian Institute of
|
9 |
* Technology. All rights reserved.
|
10 |
*
|
11 |
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
|
12 |
*
|
13 |
* If you modify the file please update the list of contributors below to in-
|
14 |
* clude your name. Please also stick to the coding convention of using TABs
|
15 |
* to do the basic (block-level) indentation and spaces for anything after
|
16 |
* that. (Enable the display of special chars and it should be pretty obvious
|
17 |
* what this means.) Also, remove all trailing whitespace.
|
18 |
*
|
19 |
* Contributors:
|
20 |
* Willibald Krenn (AIT)
|
21 |
* Stephan Zimmerer (AIT)
|
22 |
* Markus Demetz (AIT)
|
23 |
* Christoph Czurda (AIT)
|
24 |
*
|
25 |
*/
|
26 |
|
27 |
|
28 |
package org.momut.ooas.visitors.optimisation; |
29 |
|
30 |
import java.util.LinkedList; |
31 |
|
32 |
import org.momut.ooas.ast.IScope; |
33 |
import org.momut.ooas.ast.statements.Block; |
34 |
import org.momut.ooas.ast.statements.NondetBlock; |
35 |
import org.momut.ooas.ast.statements.PrioBlock; |
36 |
import org.momut.ooas.ast.statements.SeqBlock; |
37 |
import org.momut.ooas.ast.statements.Statement; |
38 |
import org.momut.ooas.ast.statements.StatementKind; |
39 |
import org.momut.ooas.parser.ParserState; |
40 |
import org.momut.ooas.utils.exceptions.InternalCompilerException; |
41 |
import org.momut.ooas.visitors.OoaCompleteAstTraversalVisitor; |
42 |
|
43 |
public final class OoaJoinSequentialCompositionBlocksVisitor extends OoaCompleteAstTraversalVisitor { |
44 |
|
45 |
@Override
|
46 |
public void visit(SeqBlock seqBlock) { |
47 |
// do all the children first.
|
48 |
super.visit(seqBlock);
|
49 |
checkScope(seqBlock); |
50 |
|
51 |
// now see whether we can join a child-seq block
|
52 |
boolean didSomeWork = false; |
53 |
final LinkedList<Statement> newList = new LinkedList<>(); |
54 |
for (final Statement s: seqBlock.statements()) { |
55 |
if (s.kind() == StatementKind.SeqBlock && ((Block)s).isSimpleBlock()) {
|
56 |
final SeqBlock child = (SeqBlock)s;
|
57 |
if (child.GetParentScope() != seqBlock)
|
58 |
throw new InternalCompilerException("Internal Error: AST structure error."); |
59 |
// now add all the statements of seqblock to the current seqblock
|
60 |
for (final Statement childS: child.statements()) { |
61 |
if (childS instanceof IScope) |
62 |
((IScope)childS).SetParentScope(seqBlock); |
63 |
newList.add(childS); |
64 |
} |
65 |
didSomeWork = true;
|
66 |
} else
|
67 |
newList.add(s); |
68 |
} |
69 |
if (didSomeWork) {
|
70 |
m_changes = true;
|
71 |
seqBlock.SetStatements(newList); |
72 |
} |
73 |
} |
74 |
|
75 |
@Override
|
76 |
public void visit(NondetBlock nondetBlock) { |
77 |
super.visit(nondetBlock);
|
78 |
checkScope(nondetBlock); |
79 |
} |
80 |
|
81 |
@Override
|
82 |
public void visit(PrioBlock prioBlock) { |
83 |
super.visit(prioBlock);
|
84 |
checkScope(prioBlock); |
85 |
} |
86 |
|
87 |
|
88 |
private final void checkScope(Block aBlock) { |
89 |
for (final Statement element: aBlock.statements()) |
90 |
if (element instanceof IScope) { |
91 |
final IScope child = (IScope) element;
|
92 |
if (child.GetParentScope() != aBlock)
|
93 |
throw new InternalCompilerException("Internal Error: AST structure error."); |
94 |
} |
95 |
} |
96 |
|
97 |
private boolean m_changes = false; |
98 |
|
99 |
public OoaJoinSequentialCompositionBlocksVisitor(ParserState aState) {
|
100 |
super(aState);
|
101 |
} |
102 |
|
103 |
public boolean changedAst() { |
104 |
return m_changes;
|
105 |
} |
106 |
} |