Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / parser / visitors / ooaCloneVisitor.cs @ 3

1
/**
2
  *
3
  *                      OOAS Compiler (Deprecated)
4
  *
5
  * Copyright 2015, Institute for Software Technology, Graz University of
6
  * Technology. Portions are copyright 2015 by the AIT Austrian Institute
7
  * of Technology. All rights reserved.
8
  *
9
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
10
  *
11
  * Please notice that this version of the OOAS compiler is considered de-
12
  * precated. Only the Java version is maintained.
13
  *
14
  * Contributors:
15
  *               Willibald Krenn (TU Graz/AIT)
16
  *               Stefan Tiran (TU Graz/AIT)
17
  */
18

    
19
using System;
20
using System.Collections.Generic;
21
using System.Text;
22

    
23
namespace TUG.Mogentes
24
{
25

    
26
    
27

    
28
    public sealed class OoaDeepCloneVisitor : OoaCompleteAstTraversalVisitor
29
    {
30
        Dictionary<object, object> mapping;
31
        Identifier selfreplacement;
32
        //List<SymbolTable> symboltables = new List<SymbolTable>();
33

    
34

    
35
        public bool quantifierState = false;
36

    
37
        /* we do not clone types */
38
        public override void visit(AnyType anyType)
39
        { throw new NotImplementedException(); }
40
        public override void visit(BoolType boolType)
41
        { throw new NotImplementedException(); }
42
        public override void visit(CharType charType)
43
        { throw new NotImplementedException(); }
44
        public override void visit(EnumType enumType)
45
        { throw new NotImplementedException(); }
46
        public override void visit(FloatType floatType)
47
        { throw new NotImplementedException(); }
48
        public override void visit(FunctionType functionType)
49
        { throw new NotImplementedException(); }
50
        public override void visit(IntType intType)
51
        { throw new NotImplementedException(); }
52
        public override void visit(ListType listType)
53
        { throw new NotImplementedException(); }
54
        public override void visit(MapType mapType)
55
        { throw new NotImplementedException(); }
56
        public override void visit(NullType nullType)
57
        { throw new NotImplementedException(); }
58
        public override void visit(OoActionSystemType ooActionSystemType)
59
        { throw new NotImplementedException(); }
60
        public override void visit(TupleType tupleType)
61
        { throw new NotImplementedException(); }
62
        public override void visit(EnumIdentifier enumIdentifier)
63
        { throw new NotImplementedException(); }
64
        public override void visit(LandmarkIdentifier landmarkIdentifier)
65
        { throw new NotImplementedException(); }
66
        public override void visit(ExpressionVariableIdentifier expressionVariableIdentifier)
67
        { throw new NotImplementedException(); }
68

    
69

    
70
        public override void visit(LocalVariableIdentifier localVariableIdentifier)
71
        {
72
            throw new NotImplementedException();
73
        }
74

    
75
        public override void visit(MainModule mainModule)
76
        {
77
            throw new NotImplementedException();
78
        }
79

    
80

    
81
        public override void visit(MapConstructor mapConstructor)
82
        {
83
            throw new NotImplementedException();
84
        }
85

    
86
        public override void visit(MethodIdentifier methodIdentifier)
87
        {
88
            throw new NotImplementedException();
89
        }
90

    
91
        public override void visit(NamedActionIdentifier namedActionIdentifier)
92
        {
93
            Block newBlock = (Block)SClone(namedActionIdentifier.body);
94
            namedActionIdentifier.SetBody(newBlock);
95
            // we do not clone the parameters..
96
        }
97

    
98
        public override void visit(Module module)
99
        {
100
            throw new NotImplementedException();
101
        }
102

    
103
        public override void visit(NondetIdentifierList nondetIdentifierList)
104
        {
105
            throw new NotImplementedException();
106
        }
107

    
108

    
109
        public override void visit(OpaqueType opaqueType)
110
        {
111
            throw new NotImplementedException(); // must not happen
112
        }
113

    
114
        public override void visit(ParameterIdentifier parameterIdentifier)
115
        {
116
            throw new NotImplementedException();
117
        }
118

    
119
        public override void visit(PrioIdentifierList prioIdentifierList)
120
        {
121
            throw new NotImplementedException();
122
        }
123

    
124
        public override void visit(QrType qrType)
125
        {
126
            throw new NotImplementedException();
127
        }
128

    
129

    
130

    
131
        public override void visit(ObjectConstructor objectConstructor)
132
        {
133
            //base.visit(objectConstructor);
134
        }
135

    
136
        private void UpdateScope(IScope aScope)
137
        {
138
            if (aScope.GetParentScope() != null && !mapping.ContainsKey(aScope.GetParentScope()))
139
                throw new ArgumentException();
140
            else if (aScope.GetParentScope() != null)
141
            {
142
                aScope.SetParentScope((IScope)mapping[aScope.GetParentScope()]);
143
            }
144
        }
145

    
146

    
147
        private Expression EClone(Expression anExpr)
148
        {
149
            Expression result = null;
150
            if (anExpr != null)
151
            {
152
                result = anExpr.Clone();
153

    
154
                if (anExpr is IScope)
155
                {
156
                    mapping.Add(anExpr, result);
157
                    UpdateScope((IScope)result);
158
                }
159

    
160
                result.Accept(this);
161
            }
162
            return result;
163
        }
164

    
165
        private Statement SClone(Statement aStatement)
166
        {
167
            Statement result = null;
168
            if (aStatement != null)
169
            {
170
                result = aStatement.Clone();
171

    
172
                if (aStatement is IScope)
173
                {
174
                    mapping.Add(aStatement, result);
175
                    UpdateScope((IScope)result);
176
                }
177

    
178
                result.Accept(this);
179
            }
180
            return result;
181
        }
182

    
183
        // clones symbols found in symbol table
184
        private void SymTabClone(SymbolTable aTable)
185
        {
186
            if (aTable != null && aTable.symbolList.Count > 0)
187
            {
188
                List<Identifier> newList = new List<Identifier>();
189
                foreach (var x in aTable.symbolList)
190
                {
191
                    if (!mapping.ContainsKey(x))
192
                    {
193
                        Identifier newid = x.Clone();
194
                        newList.Add(newid);
195
                        mapping.Add(x, newid);
196
                    }
197
                    else
198
                        newList.Add((Identifier)mapping[x]);
199
                }
200
                aTable.SetSymbolList(newList);
201
            }
202
        }
203

    
204

    
205
        private void cloneExpressionList(LinkedListNode<Expression> anElem)
206
        {
207
            while (anElem != null)
208
            {
209
                anElem.Value = EClone(anElem.Value);
210
                anElem = anElem.Next;
211
            }
212
        }
213

    
214

    
215
        /*no need to clone leafs (they are already cloned and a shallow copy is sufficient*/
216
        public override void visit(AbortStatement abortStatement)
217
        { }
218
        public override void visit<T>(ValueExpression<T> valueExpression)
219
        { }
220

    
221

    
222
        /*clone a block*/
223
        public void cloneBlock(Block aBlock)
224
        {
225
            SymTabClone(aBlock.symbols);
226
            LinkedListNode<Statement> stmnt = aBlock.statements.First;
227
            while (stmnt != null)
228
            {
229
                stmnt.Value = SClone(stmnt.Value);
230
                stmnt = stmnt.Next;
231
            }
232
        }
233
        public override void visit(NondetBlock nondetBlock)
234
        {
235
            cloneBlock(nondetBlock);
236
        }
237
        public override void visit(SeqBlock seqBlock)
238
        {
239
            seqBlock.SetFilter(EClone(seqBlock.filter));
240
            cloneBlock(seqBlock);
241
        }
242
        public override void visit(PrioBlock prioBlock)
243
        {
244
            cloneBlock(prioBlock);
245
        }
246

    
247
        public override void visit(AccessExpression accessExpression)
248
        {
249
            visit((BinaryOperator)accessExpression);
250
        }
251
        public override void visit(BinaryOperator binaryOperator)
252
        {
253
            Expression left = EClone(binaryOperator.left);
254
            Expression right = EClone(binaryOperator.right);
255
            binaryOperator.SetLeftChild(left);
256
            binaryOperator.SetRightChild(right);
257
        }
258

    
259
        public override void visit(Assignment assignment)
260
        {
261
            SymTabClone(assignment.symbols);
262
            cloneExpressionList(assignment.places.First);
263
            cloneExpressionList(assignment.values.First);
264
            assignment.SetNondetExpression(EClone(assignment.nondetExpression));
265
            UpdateScope(assignment);
266
        }
267
        public override void visit(AttributeIdentifier attributeIdentifier)
268
        {
269
            Expression initializer = EClone(attributeIdentifier.initializer);
270
            attributeIdentifier.SetInitializer(initializer);
271
        }
272
        public override void visit(Call call)
273
        {
274
            Expression cExpr = EClone(call.callExpression);
275
            call.SetCallExpression(cExpr);
276
        }
277

    
278
        public override void visit(CallExpression callExpression)
279
        {
280
            List<Expression> newargs = new List<Expression>();
281
            foreach (var x in callExpression.arguments)
282
            {
283
                newargs.Add(EClone(x));
284
            }
285
            callExpression.SetArguments(newargs);
286

    
287
            visit((UnaryOperator)callExpression);
288
        }
289

    
290
        public void CloneQuantifier(Quantifier qf)
291
        {
292
            SymTabClone(qf.symbols);
293
            bool oldQunatifierState = quantifierState;
294
            quantifierState = true;
295
            visit((UnaryOperator)qf);
296
            quantifierState = oldQunatifierState;
297
        }
298
        public override void visit(ExistsQuantifier quantifier)
299
        {
300
            CloneQuantifier(quantifier);
301
        }
302
        public override void visit(ForallQuantifier quantifier)
303
        {
304
            CloneQuantifier(quantifier);
305
        }
306

    
307

    
308

    
309

    
310
        public override void visit(GuardedCommand guardedCommand)
311
        {
312
            Block newblock = (Block)SClone(guardedCommand.body);
313
            Expression newguard = EClone(guardedCommand.guard);
314
            guardedCommand.SetBody(newblock);
315
            guardedCommand.SetGuard(newguard);
316
        }
317

    
318
        public override void visit(IdentifierExpression identifierExpression)
319
        {
320
            
321
            if (identifierExpression.isSelf)
322
            {
323
                /*if (quantifierState) //FIXXME
324
                {
325
                    if (!selfreplacement.tokenText.StartsWith("attr"))
326
                    {
327
                        String oldTokenText = selfreplacement.tokenText;
328
                        selfreplacement.SetTokenText("attr____model_root_" + selfreplacement.tokenText);
329
                        identifierExpression.SetIdentifier(selfreplacement);
330
                        selfreplacement.SetTokenText(oldTokenText);
331
                    }
332
                }
333
                else
334
                */
335
                identifierExpression.SetIdentifier(selfreplacement);
336
                
337
               
338
            }
339
            else if (mapping.ContainsKey(identifierExpression.identifier) 
340
                && identifierExpression.identifier.kind != IdentifierKind.MethodIdentifier)
341

    
342
            /* we do NOT replace method calls here, since they are prefixed by self if it's a
343
             * method in the current object or by some field/method access if it's another 
344
             * object.
345
             */ 
346
                identifierExpression.SetIdentifier((Identifier)mapping[identifierExpression.identifier]);
347
            
348
        }
349

    
350
        public override void visit(KillStatement killStatement)
351
        {
352
            if (mapping.ContainsKey(killStatement.someOne))
353
                killStatement.SetIdentifier((Identifier)mapping[killStatement.someOne]);
354
        }
355

    
356

    
357
        public override void visit(ListConstructor listConstructor)
358
        {
359
            SymTabClone(listConstructor.comprehensionVariables);
360

    
361
            List<Expression> newelems = new List<Expression>();
362
            foreach (var x in listConstructor.elements)
363
                newelems.Add(EClone(x));
364

    
365
            listConstructor.SetElements(newelems);
366
            listConstructor.SetComprehension(EClone(listConstructor.comprehension));
367
        }
368

    
369
        public override void visit(QualitativeConstraintStatement qalitativeConstraintStatement)
370
        {
371
            if (qalitativeConstraintStatement.variable0 != null && mapping.ContainsKey(qalitativeConstraintStatement.variable0))
372
                qalitativeConstraintStatement.SetVariable0((Identifier)mapping[qalitativeConstraintStatement.variable0]);
373
            if (qalitativeConstraintStatement.variable1 != null && mapping.ContainsKey(qalitativeConstraintStatement.variable1))
374
                qalitativeConstraintStatement.SetVariable1((Identifier)mapping[qalitativeConstraintStatement.variable1]);
375
            if (qalitativeConstraintStatement.variable2 != null && mapping.ContainsKey(qalitativeConstraintStatement.variable2))
376
                qalitativeConstraintStatement.SetVariable2((Identifier)mapping[qalitativeConstraintStatement.variable2]);
377
        }
378

    
379

    
380
        public override void visit(QValConstructor qValConstructor)
381
        {
382
            List<Expression> values = new List<Expression>(qValConstructor.value);
383
            for (int i = 0; i < values.Count; i++)
384
                qValConstructor.value[i] = EClone(values[i]);
385
        }
386

    
387
        public override void visit(SeqIdentifierList seqIdentifierList)
388
        {
389
            throw new NotImplementedException();
390
        }
391

    
392

    
393
        public override void visit(SetConstructor setConstructor)
394
        {
395
            SymTabClone(setConstructor.comprehensionVariables);
396
            List<Expression> newelems = new List<Expression>();
397
            foreach (var x in setConstructor.items)
398
                newelems.Add(EClone(x));
399

    
400
            setConstructor.SetItems(newelems);
401
            setConstructor.SetComprehension(EClone(setConstructor.comprehension));
402
        }
403

    
404

    
405
        public override void visit(SkipStatement skipStatement)
406
        { }
407

    
408
        public override void visit(TernaryOperator ternaryOperator)
409
        {
410
            ternaryOperator.SetLeftChild(EClone(ternaryOperator.left));
411
            ternaryOperator.SetMidChild(EClone(ternaryOperator.mid));
412
            ternaryOperator.SetRightChild(EClone(ternaryOperator.right));
413
        }
414

    
415
        public override void visit(TupleConstructor tupleConstructor)
416
        {
417
            List<Expression> values = new List<Expression>();
418
            foreach (var x in tupleConstructor.values)
419
                values.Add(EClone(x));
420
            tupleConstructor.SetTupleValues(values);
421
        }
422

    
423
        public override void visit(TupleMapAccessExpression tupleMapAccessExpression)
424
        {
425
            tupleMapAccessExpression.SetArgument(EClone(tupleMapAccessExpression.argument));
426
            visit((UnaryOperator)tupleMapAccessExpression);
427
        }
428

    
429
        public override void visit(TypeExpression typeExpression)
430
        { /*we do not clone types*/ }
431

    
432

    
433
        public override void visit(TypeIdentifier typeIdentifier)
434
        {
435
            throw new NotImplementedException();
436
        }
437

    
438
        public override void visit(UnaryOperator unaryOperator)
439
        {
440
            Expression child = EClone(unaryOperator.child);
441
            unaryOperator.SetChild(child);
442
        }
443

    
444

    
445
        public override void visit(UnresolvedIdentifierExpression unresolvedIdentifierExpression)
446
        {
447
            throw new NotImplementedException(); // must not occur
448
        }
449

    
450
        public override void visit(UnspecIdentifierList unspecIdentifierList)
451
        {
452
            throw new NotImplementedException();
453
        }
454

    
455
        public OoaDeepCloneVisitor(Dictionary<object, object> initMapping, Identifier selfreplace)
456
            : base(null)
457
        {
458
            mapping = new Dictionary<object, object>(initMapping);
459
            selfreplacement = selfreplace;
460
        }
461
    }
462
    
463
}