Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / parser / visitors / ooaTypesVisitor.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
    /// <summary>
27
    /// Requires: ResolveOpaque
28
    /// 
29
    /// Checks whether types have been defined in a sane way (no self refs etc.)
30
    /// </summary>
31
    public sealed class OoaTypesVisitor : OoaCompleteAstTraversalVisitor
32
    {
33
        private void Error(UlyssesType aType, string p)
34
        {
35
            ParserError error = new ParserError(m_ParserState.filename,
36
                aType.identifier.line, aType.identifier.column, p);
37
            m_ParserState.AddErrorMessage(error);
38
        }
39

    
40
        C5.HashSet<UlyssesType> typesSeen = new C5.HashSet<UlyssesType>();
41

    
42

    
43
        public override void visit(MainModule mainModule)
44
        {
45
            foreach (var x in mainModule.symbolTable.symbolList)
46
            {
47
                if (x.kind == IdentifierKind.TypeIdentifier)
48
                    x.Accept(this);
49
            }
50
        }
51

    
52
        protected override void VisitAstElement(IAst element, IAst parent)
53
        {
54
            if (element.nodeType != AstNodeTypeEnum.type)
55
                m_visited.Add(element);
56

    
57
            base.VisitAstElement(element, parent);
58
        }
59

    
60

    
61
        public override void visit(TypeIdentifier typeIdentifier)
62
        {
63
            typesSeen.Clear();
64
            base.visit(typeIdentifier);
65
        }
66

    
67

    
68
        private delegate void Action<T>(T param) where T : UlyssesType;
69

    
70
        /* Does the check for recursion */
71
        private void CheckForRecursion<T>(T aType, string Message, Action<T> toCall) where T : UlyssesType
72
        {
73
            if (typesSeen.Contains(aType))
74
            {
75
                Error(aType, Message);
76
                return;
77
            }
78
            typesSeen.Add(aType);
79
            try
80
            {
81
                toCall(aType);
82
            }
83
            finally
84
            {
85
                typesSeen.Remove(aType);
86
            }
87
        }
88

    
89
        /* see whether a tuple-type includes itself */
90
        public void HackUntilCSharp4(TupleType t)
91
        { base.visit(t); }
92
        public override void visit(TupleType tupleType)
93
        {
94
            CheckForRecursion<TupleType>(tupleType,
95
                String.Format("Tuple contains itself: '{0}' (Recursive definitions not allowed.)",
96
                        tupleType.ToString()), (x) => HackUntilCSharp4(x));
97
        }
98

    
99
        /* see whether a list-type includes itself */
100
        public void HackUntilCSharp4(ListType t)
101
        { base.visit(t); }
102
        public override void visit(ListType listType)
103
        {
104
            CheckForRecursion<ListType>(listType,
105
                String.Format("List contains itself: '{0}' (Recursive definitions not allowed.)",
106
                        listType.ToString()), (x) => HackUntilCSharp4(x));
107
        }
108

    
109
        /* see whether a map-type includes itself */
110
        public void HackUntilCSharp4(MapType t)
111
        { base.visit(t); }
112
        public override void visit(MapType mapType)
113
        {
114
            CheckForRecursion<MapType>(mapType,
115
                String.Format("Map contains itself: '{0}' (Recursive definitions not allowed.)",
116
                        mapType.ToString()), (x) => HackUntilCSharp4(x));
117
        }
118

    
119
        public override void visit(OoActionSystemType ooActionSystemType)
120
        {
121
            // do not go into classes..
122
        }
123

    
124
        public OoaTypesVisitor(ParserState aState)
125
            : base(aState, true)
126
        {
127
            if (aState == null)
128
                throw new ArgumentException();
129
        }
130

    
131
    }
132
}