root / trunk / compiler / ooasCompiler / src / org / momut / ooas / visitors / OoaMethodPureClassifierVisitor.java @ 7
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; |
29 |
|
30 |
import java.util.Stack; |
31 |
|
32 |
import org.momut.ooas.ast.expressions.Expression; |
33 |
import org.momut.ooas.ast.expressions.ExpressionKind; |
34 |
import org.momut.ooas.ast.expressions.IdentifierExpression; |
35 |
import org.momut.ooas.ast.identifiers.IdentifierKind; |
36 |
import org.momut.ooas.ast.identifiers.MethodIdentifier; |
37 |
import org.momut.ooas.ast.statements.Assignment; |
38 |
import org.momut.ooas.ast.types.FunctionType; |
39 |
import org.momut.ooas.ast.types.OoActionSystemType; |
40 |
import org.momut.ooas.parser.ParserMessage; |
41 |
import org.momut.ooas.parser.ParserState; |
42 |
|
43 |
/// <summary>
|
44 |
/// This visitor scans all methods and decides whether they are pure, i.e. there is no state-update.
|
45 |
/// </summary>
|
46 |
public final class OoaMethodPureClassifierVisitor extends OoaCompleteAstTraversalVisitor |
47 |
{ |
48 |
|
49 |
Stack<MethodIdentifier> currentMethod = new Stack<MethodIdentifier>(); |
50 |
|
51 |
@Override
|
52 |
public void visit(MethodIdentifier methodIdentifier) |
53 |
{ |
54 |
currentMethod.push(methodIdentifier); |
55 |
((FunctionType)currentMethod.peek().type()).SetIsPureFunction(true); // assume is true.. |
56 |
|
57 |
super.visit(currentMethod.peek());
|
58 |
|
59 |
if (((FunctionType)currentMethod.peek().type()).isPureFunction())
|
60 |
{ |
61 |
m_ParserState.AddMessage( |
62 |
new ParserMessage(m_ParserState.filename, methodIdentifier.line(),
|
63 |
methodIdentifier.column(), String.format("Found pure method: %s", |
64 |
methodIdentifier.tokenText()))); |
65 |
} else
|
66 |
m_ParserState.AddMessage( |
67 |
new ParserMessage(m_ParserState.filename, methodIdentifier.line(),
|
68 |
methodIdentifier.column(), String.format("Found extended method: %s", |
69 |
methodIdentifier.tokenText()))); |
70 |
|
71 |
currentMethod.pop(); |
72 |
} |
73 |
|
74 |
@Override
|
75 |
public void visit(OoActionSystemType systemType) |
76 |
{ |
77 |
boolean functionPure = false; //introduce variable with save value |
78 |
if (currentMethod.size() > 0 && currentMethod.peek() != null) //if we are determining the pureness... |
79 |
{ |
80 |
functionPure = ((FunctionType)currentMethod.peek().type()).isPureFunction(); //...save the current value
|
81 |
} |
82 |
super.visit(systemType); // go down in recursion |
83 |
if ( currentMethod.size() > 0 && currentMethod.peek() != null) //but restore old pureness! |
84 |
{ |
85 |
((FunctionType)currentMethod.peek().type()).SetIsPureFunction(functionPure); |
86 |
} |
87 |
} |
88 |
|
89 |
|
90 |
@Override
|
91 |
public void visit(Assignment assignment) |
92 |
{ |
93 |
|
94 |
boolean assignmentPure = true; |
95 |
if (currentMethod.size() > 0 && currentMethod.peek() != null) |
96 |
{ |
97 |
for (Expression x: assignment.places()) |
98 |
{ |
99 |
if (x.kind() != ExpressionKind.Identifier)
|
100 |
assignmentPure = false;
|
101 |
else if (((IdentifierExpression)x).identifier().kind() == IdentifierKind.AttributeIdentifier) |
102 |
assignmentPure = false;
|
103 |
} |
104 |
boolean functionPure = assignmentPure && ((FunctionType)currentMethod.peek().type()).isPureFunction();
|
105 |
((FunctionType)currentMethod.peek().type()).SetIsPureFunction(functionPure); |
106 |
} |
107 |
super.visit(assignment);
|
108 |
} |
109 |
|
110 |
|
111 |
|
112 |
public OoaMethodPureClassifierVisitor(ParserState aState)
|
113 |
{ |
114 |
super (aState);
|
115 |
} |
116 |
} |