Project

General

Profile

root / trunk / compiler / cppAst / ast / DeepCloneVisitor.hpp @ 3

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
#pragma once
26

    
27
#include <ast/CompleteAstTraversalVisitor.hpp>
28
#include <ast/Ast.hpp>
29
#include <unordered_map>
30
#include <ast/expressions/Quantifier.hpp>
31

    
32
namespace Ast {
33

    
34
/**
35
 * Based on the OoaDeepCloneVisitor from the Java frontend. Takes a shallow
36
 * copy of an AST element and makes it a deep copy.
37
 */
38
class DeepCloneVisitor final :
39
        public CompleteAstTraversalVisitor
40
{
41
protected:
42
        Ast* m_cloneAst;
43
        std::unordered_map<void*, void*> m_mapping;
44
        Identifier* m_selfreplacement;
45
        bool m_allowLinksToNonClonedParents;
46
        bool m_quantifierState;
47

    
48
        void updateScope(IScope* aScope);
49
        void checkScope(IScope* aScope);
50
        Expression* eClone(Expression* anExpr);
51
        Statement* sClone(Statement* aStatement);
52
        void symTabClone(SymbolTable* aTable);
53
        void cloneBlock(Block* aBlock);
54
        void cloneQuantifier(Quantifier* qf);
55
        void cloneExpressionList(ExpressionList& aList);
56

    
57
        void error() {
58
                m_context->logError("Internal error: Cannot clone a type");
59
                abort();
60
        }
61

    
62
public:
63
        DeepCloneVisitor(
64
                        CallContext* context,
65
                        Ast* cloneAst,
66
                        bool linksToNonClonedParents = true,
67
                        const std::unordered_map<void*, void*>* initMapping = nullptr,
68
                        Identifier* selfreplace = nullptr)
69
        :
70
                CompleteAstTraversalVisitor( context, false),
71
                m_cloneAst (cloneAst),
72
                m_mapping(),
73
                m_selfreplacement (selfreplace),
74
                m_allowLinksToNonClonedParents (linksToNonClonedParents),
75
                m_quantifierState (false)
76
        {
77
                if (initMapping != nullptr)
78
                        m_mapping.insert(initMapping->begin(), initMapping->end());
79
        }
80

    
81
        std::string returnVisitorName() override {return "DeepCloneVisitor";}
82

    
83
        void visit(BoolType* ) override         { error(); }
84
        void visit(EnumType* ) override         { error(); }
85
        void visit(FunctionType* ) override { error(); }
86
        void visit(IntType* ) override           { error(); }
87
        void visit(ListType* ) override         { error(); }
88
        void visit(NullType* ) override         { error(); }
89
        void visit(ActionSystemType* ) override       { error(); }
90
        void visit(TupleType* ) override                       { error(); }
91

    
92
        void visit(EnumIdentifier* ) override             { error(); }
93
        void visit(ExpressionVariableIdentifier* ) override { error(); }
94
        void visit(LocalVariableIdentifier* ) override           { error(); }
95
        void visit(MainModule* ) override                     { error(); }
96
        void visit(MethodIdentifier* ) override         { error(); }
97
        void visit(Module* ) override                             { error(); }
98
        void visit(NondetIdentifierBlock* ) override{ error(); }
99
        void visit(ParameterIdentifier* ) override   { error(); }
100
        void visit(PrioIdentifierBlock* ) override   { error(); }
101
        void visit(SeqIdentifierBlock* ) override      { error(); }
102
        void visit(TypeIdentifier* ) override             { error(); }
103
        void visit(UnspecIdentifierBlock* ) override { error(); }
104

    
105

    
106
        void visit(NamedActionIdentifier* namedActionIdentifier) override;
107
        void visit(ObjectConstructor* objectConstructor) override;
108

    
109

    
110
//        void visit(AbortStatement* abortStatement) override { }
111
        void visit(BoolValueExpression* ) override {};
112
        void visit(IntValueExpression* ) override  {};
113
        void visit(RefValueExpression* ) override  {};
114
        void visit(Skip* ) override { }
115
        void visit(Break* ) override { error(); }
116
        void visit(TypeExpression* ) override { /*we do not clone types*/ }
117

    
118

    
119
        void visit(NondetBlock* nondetBlock) override;
120
        void visit(SeqBlock* seqBlock) override;
121
        void visit(PrioBlock* prioBlock) override;
122
        void visit(AccessExpression* accessExpression) override;
123
        void visit(BinaryOperator* binaryOperator) override;
124
        void visit(Assignment* assignment) override;
125
        void visit(AttributeIdentifier* attributeIdentifier) override;
126
        void visit(Call* call) override;
127

    
128
        void visit(CallExpression* callExpression) override;
129
        void visit(ExistsQuantifier* quantifier) override;
130
        void visit(ForallQuantifier* quantifier) override;
131
        void visit(GuardedCommand* guardedCommand) override;
132
        void visit(IdentifierExpression* identifierExpression) override;
133
        void visit(ListConstructor* listConstructor) override;
134
        void visit(SetConstructor* setConstructor) override;
135
        void visit(TernaryOperator* ternaryOperator) override;
136
        void visit(TupleConstructor* tupleConstructor) override;
137
        void visit(TupleMapAccessExpression* tupleMapAccessExpression) override;
138
        void visit(UnaryOperator* unaryOperator) override;
139
};
140

    
141
} /* namespace Ast */