Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / parser / visitors / ooaMethodPureClassifier.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
    /// <summary>
26
    /// This visitor scans all methods and decides whether they are pure, i.e. there is no state-update.
27
    /// </summary>
28
    public sealed class OoaMethodPureClassifierVisitor : OoaCompleteAstTraversalVisitor
29
    {
30

    
31
        Stack<MethodIdentifier> currentMethod = new Stack<MethodIdentifier>();
32

    
33
        public override void visit(MethodIdentifier methodIdentifier)
34
        {
35
            
36
            currentMethod.Push( methodIdentifier);
37
            ((FunctionType)currentMethod.Peek().type).SetIsPureFunction(true); // assume is true..
38

    
39
            base.visit(currentMethod.Peek());
40
          
41
            if (((FunctionType)currentMethod.Peek().type).isPureFunction)
42
            {
43
                m_ParserState.AddMessage(
44
                    new ParserMessage(m_ParserState.filename, methodIdentifier.line,
45
                        methodIdentifier.column, String.Format("Found pure method: {0}",
46
                         methodIdentifier.tokenText)));
47
            } else
48
                m_ParserState.AddMessage(                    new ParserMessage(m_ParserState.filename, methodIdentifier.line,
49
                        methodIdentifier.column, String.Format("Found extended method: {0}",
50
                         methodIdentifier.tokenText)));
51

    
52
            currentMethod.Pop();
53
        }
54

    
55
        public override void visit(OoActionSystemType systemType)
56
        {
57
            bool functionPure = false; //introduce variable with save value 
58
            if (currentMethod.Count > 0 && currentMethod.Peek() != null) //if we are determining the pureness...
59
            {
60
                functionPure = ((FunctionType)currentMethod.Peek().type).isPureFunction; //...save the current value
61
            }
62
            base.visit(systemType); // go down in recursion 
63
            if ( currentMethod.Count > 0 && currentMethod.Peek() != null) //but restore old pureness!
64
            {
65
                         
66
                ((FunctionType)currentMethod.Peek().type).SetIsPureFunction(functionPure);
67
                
68
            }
69
                    
70
        }
71

    
72

    
73
        public override void visit(Assignment assignment)
74
        {
75
            
76
            bool assignmentPure = true;
77
            if (currentMethod.Count > 0 && currentMethod.Peek() != null)
78
            {
79
                foreach (var x in assignment.places)
80
                {
81
                    if (x.kind != ExpressionKind.Identifier)
82
                        assignmentPure = false;
83
                    else if (((IdentifierExpression)x).identifier.kind == IdentifierKind.AttributeIdentifier)
84
                        assignmentPure = false;
85
                }
86
                bool functionPure = assignmentPure && ((FunctionType)currentMethod.Peek().type).isPureFunction;
87
                ((FunctionType)currentMethod.Peek().type).SetIsPureFunction(functionPure);
88
            }
89
            base.visit(assignment);
90
        }
91

    
92

    
93

    
94
        public OoaMethodPureClassifierVisitor(ParserState aState)
95
            : base(aState)
96
        { }
97
    }
98
}
99