Project

General

Profile

root / trunk / compiler / cppAst / ast / CompleteAstTraversalVisitor.cpp @ 7

1
/**
2
  *
3
  *                  OOAS Compiler - C++ AST
4
  *
5
  * Copyright 2015, AIT Austrian Institute of Technology.
6
  * All rights reserved.
7
  *
8
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
9
  *
10
  * If you modify the file please update the list of contributors below to in-
11
  * clude your name. Please also stick to the coding convention of using TABs
12
  * to do the basic (block-level) indentation and spaces for anything after
13
  * that. (Enable the display of special chars and it should be pretty obvious
14
  * what this means.) Also, remove all trailing whitespace.
15
  *
16
  * Contributors:
17
  *               Willibald Krenn (AIT)
18
  *               Stephan Zimmerer (AIT)
19
  *               Christoph Czurda (AIT)
20
  *
21
  */
22

    
23

    
24

    
25
/*
26
 * CompleteAstTraversalVisitor.cpp
27
 *
28
 *  Created on: Dec 28, 2013
29
 *      Author: willibald
30
 */
31

    
32
#include "CompleteAstTraversalVisitor.hpp"
33
#include <ast/identifiers/EnumIdentifier.hpp>
34
#include <ast/identifiers/AttributeIdentifier.hpp>
35
#include <ast/identifiers/ExpressionVariableIdentifier.hpp>
36
#include <ast/identifiers/MethodIdentifier.hpp>
37
#include <ast/identifiers/ConstantIdentifier.hpp>
38
#include <ast/identifiers/NamedActionIdentifier.hpp>
39
#include <ast/identifiers/MainModule.hpp>
40
#include <ast/identifiers/IdentifierBlock.hpp>
41
#include <ast/statements/NondetBlock.hpp>
42
#include <ast/statements/PrioBlock.hpp>
43
#include <ast/statements/SeqBlock.hpp>
44
#include <ast/statements/Assignment.hpp>
45
#include <ast/statements/GuardedCommand.hpp>
46
#include <ast/statements/Call.hpp>
47
#include <ast/expressions/IdentifierExpression.hpp>
48
#include <ast/expressions/TypeExpression.hpp>
49
#include <ast/expressions/ListConstructor.hpp>
50
#include <ast/expressions/SetConstructor.hpp>
51
#include <ast/expressions/TupleConstructor.hpp>
52
#include <ast/expressions/AccessExpression.hpp>
53
#include <ast/expressions/BinaryOperator.hpp>
54
#include <ast/expressions/TernaryOperator.hpp>
55
#include <ast/expressions/TupleMapAccessExpression.hpp>
56
#include <ast/expressions/CallExpression.hpp>
57
#include <ast/expressions/ForallQuantifier.hpp>
58
#include <ast/expressions/ExistsQuantifier.hpp>
59
#include <ast/expressions/UnaryOperator.hpp>
60
#include <ast/types/EnumType.hpp>
61
#include <ast/types/ListType.hpp>
62
#include <ast/types/TupleType.hpp>
63
#include <ast/types/FunctionType.hpp>
64
#include <ast/types/ActionSystemType.hpp>
65
#include <ast/expressions/ObjectConstructor.hpp>
66

    
67

    
68
namespace Ast {
69

    
70
CompleteAstTraversalVisitor::CompleteAstTraversalVisitor(CallContext* context, bool duplicateVisit):
71
                m_visited(),
72
                m_context(context),
73
                m_DuplicateVisit(duplicateVisit)
74
{}
75

    
76
CompleteAstTraversalVisitor::~CompleteAstTraversalVisitor()
77
{}
78

    
79

    
80
void CompleteAstTraversalVisitor::visitSub(AstElement* subElement, AstElement* parent) {
81
        if (subElement == nullptr)
82
                return;
83
        if (m_visited.count(subElement) > 0)
84
                return;
85
        if (!m_DuplicateVisit)
86
                m_visited[subElement] = true;
87
        visitAstElement(subElement, parent);
88
}
89

    
90
void CompleteAstTraversalVisitor::visitAstElement(AstElement* element, AstElement* parent){
91
        switch (element->nodeType()) {
92
        case AstNodeTypeEnum::type:
93
                visitType(static_cast<Type*>(element), parent);
94
                break;
95
        case AstNodeTypeEnum::identifier:
96
                visitIdentifier((Identifier*)element, parent);
97
                break;
98
        case AstNodeTypeEnum::expression:
99
                visitExpression((Expression*)element, parent);
100
                break;
101
        case AstNodeTypeEnum::statement:
102
                visitStatement((Statement*)element, parent);
103
                break;
104
        default:
105
                m_context->logError("Internal error. Unknown AST type.");
106
                abort();
107
        }
108
}
109

    
110

    
111
void CompleteAstTraversalVisitor::visitType(Type* element, AstElement* ) {
112
        element->accept(*this); // could be replaced by direct switch here..
113
}
114

    
115
void CompleteAstTraversalVisitor::visitIdentifier(Identifier* element, AstElement* ) {
116
        element->accept(*this); // could be replaced by direct switch here..
117
}
118

    
119
void CompleteAstTraversalVisitor::visitExpression(Expression* element, AstElement* ) {
120
        element->accept(*this); // could be replaced by direct switch here..
121
}
122

    
123
void CompleteAstTraversalVisitor::visitStatement(Statement* element, AstElement* ) {
124
        element->accept(*this); // could be replaced by direct switch here..
125
}
126

    
127
void CompleteAstTraversalVisitor::visit(TypeIdentifier* typeIdentifier) {
128
        visitSub(typeIdentifier->type(), typeIdentifier);
129
};
130

    
131
void CompleteAstTraversalVisitor::visit(EnumIdentifier* enumIdentifier) {
132
        visitSub(enumIdentifier->type(), enumIdentifier);
133
};
134

    
135
//void CompleteAstTraversalVisitor::visit(ValuedEnumIdentifier* enumIdentifier) {};
136

    
137
void CompleteAstTraversalVisitor::visit(AttributeIdentifier* attributeIdentifier) {
138
        visitSub(attributeIdentifier->type(), attributeIdentifier);
139
        visitSub(attributeIdentifier->initializer(), attributeIdentifier);
140
};
141

    
142
void CompleteAstTraversalVisitor::visit(LocalVariableIdentifier* localVariableIdentifier) {
143
        visitSub(localVariableIdentifier->type(), localVariableIdentifier);
144
};
145

    
146
void CompleteAstTraversalVisitor::visit(ParameterIdentifier* parameterIdentifier) {
147
        visitSub(parameterIdentifier->type(), parameterIdentifier);
148
};
149

    
150
void CompleteAstTraversalVisitor::visit(ExpressionVariableIdentifier* expressionVariableIdentifier) {
151
        visitSub(expressionVariableIdentifier->type(), expressionVariableIdentifier);
152
};
153

    
154
void CompleteAstTraversalVisitor::visit(MethodIdentifier* methodIdentifier) {
155
        visitSub(methodIdentifier->type(), methodIdentifier);
156
        for (auto& sym: *(methodIdentifier->symbolTable()))
157
                visitSub(sym.second, methodIdentifier);
158
        for (auto& par: methodIdentifier->parameter())
159
                visitSub(par, methodIdentifier);
160
        visitSub(methodIdentifier->body(), methodIdentifier);
161
};
162

    
163
void CompleteAstTraversalVisitor::visit(NamedActionIdentifier* namedActionIdentifier) {
164
        visitSub(namedActionIdentifier->type(), namedActionIdentifier);
165
        for (auto& sym : *namedActionIdentifier->symbolTable())
166
                visitSub(sym.second, namedActionIdentifier);
167
        for (auto& par: namedActionIdentifier->parameter())
168
                visitSub(par, namedActionIdentifier);
169
        visitSub(namedActionIdentifier->body(), namedActionIdentifier);
170
};
171

    
172
void CompleteAstTraversalVisitor::visit(Module* module) {
173
        for (auto& sym: *module->symbolTable())
174
                visitSub(sym.second, module);
175
};
176

    
177
void CompleteAstTraversalVisitor::visit(ConstantIdentifier* constantIdentifier) {
178
        visitSub(constantIdentifier->type(), constantIdentifier);
179
        visitSub(constantIdentifier->value(), constantIdentifier);
180
};
181

    
182
void CompleteAstTraversalVisitor::visit(UnspecIdentifierBlock* unspecIdentifierList) {
183
        for (auto& id: unspecIdentifierList->identifiers())
184
                visitSub(id, unspecIdentifierList);
185
};
186

    
187
void CompleteAstTraversalVisitor::visit(SeqIdentifierBlock* seqIdentifierList) {
188
        for (auto& id: seqIdentifierList->identifiers())
189
                visitSub(id, seqIdentifierList);
190
};
191

    
192
void CompleteAstTraversalVisitor::visit(NondetIdentifierBlock* nondetIdentifierList) {
193
        for (auto& id: nondetIdentifierList->identifiers())
194
                visitSub(id, nondetIdentifierList);
195
};
196

    
197
void CompleteAstTraversalVisitor::visit(PrioIdentifierBlock* prioIdentifierList) {
198
        for (auto& id: prioIdentifierList->identifiers())
199
                visitSub(id, prioIdentifierList);
200
};
201

    
202
void CompleteAstTraversalVisitor::visit(MainModule* mainModule) {
203
        for (auto& sym: *mainModule->symbolTable())
204
                visitSub(sym.second, mainModule);
205
        visitSub(mainModule->systemDescription(), mainModule);
206
};
207

    
208
void CompleteAstTraversalVisitor::visit(IntType* ) {};
209
void CompleteAstTraversalVisitor::visit(BoolType* ) {};
210

    
211
void CompleteAstTraversalVisitor::visit(EnumType* enumType) {
212
        for (auto& sym: enumType->listOfEnumSymbols())
213
                visitSub(sym, enumType);
214
};
215

    
216
void CompleteAstTraversalVisitor::visit(ListType* listType) {
217
        visitSub(listType->innerType(), listType);
218
};
219

    
220
void CompleteAstTraversalVisitor::visit(TupleType* tupleType) {
221
        for (auto& t: tupleType->innerTypes())
222
                visitSub(t, tupleType);
223
};
224

    
225
void CompleteAstTraversalVisitor::visit(NullType* ) {};
226

    
227
void CompleteAstTraversalVisitor::visit(FunctionType* functionType) {
228
        for (auto& a: functionType->parameter())
229
                visitSub(a, functionType);
230
        visitSub(functionType->returnType(), functionType);
231
};
232

    
233
void CompleteAstTraversalVisitor::visit(ActionSystemType* actionSystemType) {
234
        for (auto& s: *actionSystemType->symbols())
235
                visitSub(s.second, actionSystemType);
236

    
237
        visitSub(actionSystemType->doodBlock(), actionSystemType);
238
};
239

    
240
void CompleteAstTraversalVisitor::visit(GuardedCommand* guardedCommand) {
241
        visitSub(guardedCommand->guard(), guardedCommand);
242
        visitSub(guardedCommand->body(), guardedCommand);
243
};
244

    
245
void CompleteAstTraversalVisitor::visit(Call* call) {
246
        visitSub(call->callExpression(), call);
247
};
248

    
249
void CompleteAstTraversalVisitor::visit(Assignment* assignment) {
250
        for (auto& vexp: assignment->places())
251
                visitSub(vexp, assignment);
252
        for (auto& expr: assignment->values())
253
                visitSub(expr, assignment);
254

    
255
        visitSub(assignment->nondetExpression(), assignment);
256
};
257

    
258
void CompleteAstTraversalVisitor::visit(SeqBlock* seqBlock) {
259
        for (auto& id: *seqBlock->symbols())
260
                visitSub(id.second, seqBlock);
261

    
262
        if (seqBlock->filter() != nullptr)
263
                visitSub(seqBlock->filter(), seqBlock);
264

    
265
        for (auto& stmnt: seqBlock->statements())
266
                visitSub(stmnt, seqBlock);
267
};
268

    
269
void CompleteAstTraversalVisitor::visit(NondetBlock* nondetBlock) {
270
        for (auto& id: *nondetBlock->symbols())
271
                visitSub(id.second, nondetBlock);
272

    
273
        for (auto& stmnt: nondetBlock->statements())
274
                visitSub(stmnt, nondetBlock);
275
};
276

    
277
void CompleteAstTraversalVisitor::visit(PrioBlock* prioBlock) {
278
        for (auto& id: *prioBlock->symbols())
279
                visitSub(id.second, prioBlock);
280

    
281
        for (auto& stmnt: prioBlock->statements())
282
                visitSub(stmnt, prioBlock);
283
};
284

    
285
void CompleteAstTraversalVisitor::visit(Skip* ) {};
286
void CompleteAstTraversalVisitor::visit(Break* ) {};
287
void CompleteAstTraversalVisitor::visit(Abort* ) {};
288

    
289
void CompleteAstTraversalVisitor::visit(TernaryOperator* ternaryOperator) {
290
        visitSub(ternaryOperator->left(), ternaryOperator);
291
        visitSub(ternaryOperator->mid(), ternaryOperator);
292
        visitSub(ternaryOperator->right(), ternaryOperator);
293
};
294

    
295
void CompleteAstTraversalVisitor::visit(BinaryOperator* binaryOperator) {
296
        visitSub(binaryOperator->left(), binaryOperator);
297
        visitSub(binaryOperator->right(), binaryOperator);
298
};
299

    
300
void CompleteAstTraversalVisitor::visit(UnaryOperator* unaryOperator) {
301
        visitSub(unaryOperator->child(), unaryOperator);
302
        if (unaryOperator->kind() == ExpressionKind::Cast)
303
                visitSub(unaryOperator->type(), unaryOperator);
304
};
305

    
306
void CompleteAstTraversalVisitor::visit(ExistsQuantifier* quantifier) {
307
        for (auto& lvar: *quantifier->symbols())
308
                visitSub(lvar.second, quantifier);
309

    
310
        visitSub(quantifier->child(), quantifier);
311
};
312

    
313
void CompleteAstTraversalVisitor::visit(ForallQuantifier* quantifier) {
314
        for (auto& lvar: *quantifier->symbols())
315
                visitSub(lvar.second, quantifier);
316

    
317
        visitSub(quantifier->child(), quantifier);
318
};
319

    
320
void CompleteAstTraversalVisitor::visit(ListConstructor* listConstructor) {
321
        for (auto& lvar: *listConstructor->comprehensionVariables())
322
                visitSub(lvar.second, listConstructor);
323

    
324
        for (auto& elems: listConstructor->elements())
325
                visitSub(elems, listConstructor);
326

    
327
        visitSub(listConstructor->comprehension(), listConstructor);
328
};
329

    
330
void CompleteAstTraversalVisitor::visit(SetConstructor* setConstructor) {
331
        for (auto& svar: *setConstructor->comprehensionVariables())
332
                visitSub(svar.second, setConstructor);
333

    
334
        for (auto& elems: setConstructor->items())
335
                visitSub(elems, setConstructor);
336

    
337
        visitSub(setConstructor->comprehension(), setConstructor);
338
};
339

    
340
void CompleteAstTraversalVisitor::visit(TupleConstructor* tupleConstructor) {
341
        for (auto& elem: tupleConstructor->values())
342
                visitSub(elem, tupleConstructor);
343
};
344

    
345
void CompleteAstTraversalVisitor::visit(ObjectConstructor* objectConstructor) {
346
        visitSub(objectConstructor->type(), objectConstructor);
347
};
348

    
349
void CompleteAstTraversalVisitor::visit(BoolValueExpression* ) {};
350
void CompleteAstTraversalVisitor::visit(IntValueExpression* ) {};
351
void CompleteAstTraversalVisitor::visit(RefValueExpression* ) {};
352

    
353
void CompleteAstTraversalVisitor::visit(IdentifierExpression* identifierExpression) {
354
        visitSub(identifierExpression->type(), identifierExpression);
355
        visitSub(identifierExpression->identifier(), identifierExpression);
356
};
357

    
358
void CompleteAstTraversalVisitor::visit(TypeExpression* typeExpression) {
359
        visitSub(typeExpression->type(), typeExpression);
360
};
361

    
362
void CompleteAstTraversalVisitor::visit(TupleMapAccessExpression* tupleMapAccessExpression) {
363
        visitSub(tupleMapAccessExpression->argument(), tupleMapAccessExpression);
364
        visitSub(tupleMapAccessExpression->child(), tupleMapAccessExpression);
365
};
366

    
367
void CompleteAstTraversalVisitor::visit(CallExpression* callExpression) {
368
        for (auto& arg: callExpression->arguments())
369
                visitSub(arg, callExpression);
370
        visitSub(callExpression->child(), callExpression);
371
};
372

    
373
void CompleteAstTraversalVisitor::visit(AccessExpression* accessExpression) {
374
        visitSub(accessExpression->left(), accessExpression);
375
        visitSub(accessExpression->right(), accessExpression);
376
};
377

    
378
void CompleteAstTraversalVisitor::visit(PointerType* ) {
379

    
380
}
381

    
382
} /* namespace Ast */