Project

General

Profile

root / trunk / compiler / ooasCompiler / src / org / momut / ooas / visitors / optimisation / OoaRemoveTrivialPrioritizedCompositionVisitor.java @ 9

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 org.momut.ooas.ast.AstNodeTypeEnum;
31
import org.momut.ooas.ast.IAst;
32
import org.momut.ooas.ast.identifiers.Identifier;
33
import org.momut.ooas.ast.identifiers.IdentifierKind;
34
import org.momut.ooas.ast.identifiers.IdentifierList;
35
import org.momut.ooas.ast.identifiers.MainModule;
36
import org.momut.ooas.ast.identifiers.MethodIdentifier;
37
import org.momut.ooas.ast.identifiers.NamedActionIdentifier;
38
import org.momut.ooas.ast.statements.Block;
39
import org.momut.ooas.ast.statements.GuardedCommand;
40
import org.momut.ooas.ast.statements.PrioBlock;
41
import org.momut.ooas.ast.statements.SeqBlock;
42
import org.momut.ooas.ast.statements.Statement;
43
import org.momut.ooas.ast.statements.StatementKind;
44
import org.momut.ooas.ast.types.OoActionSystemType;
45
import org.momut.ooas.ast.types.Type;
46
import org.momut.ooas.parser.ParserState;
47
import org.momut.ooas.utils.exceptions.NotImplementedException;
48
import org.momut.ooas.utils.exceptions.InternalCompilerException;
49
import org.momut.ooas.utils.exceptions.OoasCompilerRuntimeException;
50
import org.momut.ooas.visitors.OoaCompleteAstTraversalVisitor;
51

    
52
/// <summary>
53
/// removes trivial prioritized compositions.
54
/// </summary>
55
public final class OoaRemoveTrivialPrioritizedCompositionVisitor extends OoaCompleteAstTraversalVisitor
56
{
57
        /// <summary>
58
        /// replaces the prioBlock by its child (most likely a parBlock)
59
        /// </summary>
60
        private IAst ConvertPrioBlock(PrioBlock prioBlock, IAst parent)
61
        {
62
                if (prioBlock.statements().size() <= 1 && prioBlock.isSimpleBlock())
63
                {
64
                        final Block newBlock = (Block)prioBlock.statements().peekFirst();
65
                        newBlock.SetParentScope(prioBlock.GetParentScope());
66
                        switch (parent.nodeType())
67
                        {
68
                        case type:
69
                                switch (((Type)parent).kind())
70
                                {
71
                                case OoActionSystemType:
72
                                        final OoActionSystemType atype = (OoActionSystemType)parent;
73
                                        final Block ablock = atype.doOdBlock();
74
                                        assert(ablock == prioBlock); // ref equ.
75
                                        atype.SetDoOdBlock(newBlock);
76
                                        break;
77
                                default:
78
                                        throw new NotImplementedException();
79
                                }
80
                                break;
81
                        case statement:
82
                                switch (((Statement)parent).kind())
83
                                {
84
                                case SeqBlock:
85
                                        final SeqBlock aseqblock = (SeqBlock)parent;
86
                                        final int idx = aseqblock.statements().indexOf(prioBlock);
87
                                        if (idx == -1)
88
                                                throw new OoasCompilerRuntimeException(); // must not happen?!
89
                                        aseqblock.statements().set(idx, newBlock);
90
//                                        LinkedListNode<Statement> toChange = aseqblock.statements.Find(prioBlock);
91
//                                        toChange.Value = newBlock;
92
                                        break;
93
                                case GuardedCommand:
94
                                        final GuardedCommand gc = (GuardedCommand)parent;
95
                                        assert(gc.body() == prioBlock); // ref equ.
96
                                        gc.SetBody(newBlock);
97
                                        break;
98
                                default:
99
                                        throw new NotImplementedException();
100
                                }
101
                                break;
102
                        case identifier:
103
                                switch (((Identifier)parent).kind())
104
                                {
105
                                case NamedActionIdentifier:
106
                                        final NamedActionIdentifier action = (NamedActionIdentifier)parent;
107
                                        if (action.body() != prioBlock) // ref equ.
108
                                                throw new InternalCompilerException("Internal Error: Cannot replace action body.");
109
                                        action.SetBody(newBlock);
110
                                        break;
111
                                case MethodIdentifier:
112
                                        final MethodIdentifier method = (MethodIdentifier)parent;
113
                                        assert(method.body() == prioBlock); // ref equ.
114
                                        method.SetBody(newBlock);
115
                                        break;
116
                                default:
117
                                        throw new NotImplementedException();
118
                                }
119
                                break;
120
                        default:
121
                                throw new NotImplementedException();
122
                        }
123
                        m_changes = true;
124
                        return newBlock;
125
                }
126
                else
127
                        return prioBlock;
128
        }
129

    
130

    
131
        @Override
132
        protected void VisitAstElement(IAst element, IAst parent)
133
        {
134
                if ((element.nodeType() == AstNodeTypeEnum.statement) &&
135
                                (((Statement)element).kind() == StatementKind.PrioBlock) &&
136
                                // we're only interested in actionblock and actionbody composition, not in type composition
137
                                !(parent.nodeType() == AstNodeTypeEnum.identifier && ((Identifier)parent).kind() == IdentifierKind.List))
138
                {
139
                        final IAst newchild = ConvertPrioBlock((PrioBlock)element, parent);
140
                        super.VisitAstElement(newchild, parent);
141
                }
142
                else
143
                        super.VisitAstElement(element, parent);
144
        }
145

    
146

    
147
        @Override
148
        public void visit(MainModule mainModule)
149
        {
150
                // get rid of any top-level spurious block
151
                if (mainModule.systemDescription() != null)
152
                        while (mainModule.systemDescription().identifiers().size() == 1
153
                               && mainModule.systemDescription().identifiers().peekFirst().kind() == IdentifierKind.List)
154
                        {
155
                                mainModule.SetSystemDescription((IdentifierList)mainModule.systemDescription().identifiers().peekFirst());
156
                        }
157
                super.visit(mainModule);
158
        }
159

    
160
        private boolean m_changes = false;
161

    
162
        public OoaRemoveTrivialPrioritizedCompositionVisitor(ParserState aState)
163
        {
164
                super (aState);
165
        }
166

    
167

    
168
        public boolean changedAst() {
169
                return m_changes;
170
        }
171
}