Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / parser / visitors / ooaOpaqueVisitor.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
    /// Replaces all Opaque types (types we could not resolve in the first run of the parser)
27
    /// with the resolved ones. Note: The actual resolving has already been done, i.e., the 
28
    /// resolvedType member is already set to the correct value. Now we just want to get an
29
    /// AST that does not contain any OpaqueTypeNodes at all.
30
    /// </summary>
31
    public sealed class OoaReplaceOpaqueVisitor : OoaCompleteAstTraversalVisitor
32
    {
33

    
34
        private void ReplaceOpaque(IAst parent)
35
        {
36
            switch (parent.nodeType)
37
            {
38
                case AstNodeTypeEnum.type:
39
                    ReplaceOpaqueInType((UlyssesType)parent);
40
                    break;
41
                case AstNodeTypeEnum.identifier:
42
                    ReplaceOpaqueInIdentifier((Identifier)parent);
43
                    break;
44
                case AstNodeTypeEnum.expression:
45
                    ReplaceOpaqueInExpression((Expression)parent);
46
                    break;
47
                default:
48
                    throw new NotImplementedException();
49
            }
50
        }
51

    
52

    
53
        private void ReplaceOpaqueInExpression(Expression expression)
54
        {
55
            switch (expression.kind)
56
            {
57
                case ExpressionKind.ObjectConstr:
58
                    ObjectConstructor c = (ObjectConstructor)expression;
59
                    c.SetType(((OpaqueType)c.type).resolvedType);
60
                    break;
61
                case ExpressionKind.Cast:
62
                    UnaryOperator unop = (UnaryOperator)expression;
63
                    unop.SetType(((OpaqueType)unop.type).resolvedType);
64
                    break;
65
                default:
66
                    throw new NotImplementedException();
67
            }
68
        }
69

    
70

    
71
        private void ReplaceOpaqueInIdentifier(Identifier identifier)
72
        {
73
            switch (identifier.kind)
74
            {
75
                case IdentifierKind.AttributeIdentifier:
76
                    AttributeIdentifier id = (AttributeIdentifier)identifier;
77
                    System.Diagnostics.Debug.Assert(id.type is OpaqueType); // that's why we are here
78
                    id.SetType(((OpaqueType)id.type).resolvedType);
79
                    break;
80
                case IdentifierKind.ExpressionVariableIdentifier:
81
                    ExpressionVariableIdentifier exprvar = (ExpressionVariableIdentifier)identifier;
82
                    System.Diagnostics.Debug.Assert(exprvar.type is OpaqueType);
83
                    exprvar.SetType(((OpaqueType)exprvar.type).resolvedType);
84
                    break;
85
                case IdentifierKind.LocalVariableIdentifier:
86
                    LocalVariableIdentifier var = (LocalVariableIdentifier)identifier;
87
                    System.Diagnostics.Debug.Assert(var.type is OpaqueType);
88
                    var.SetType(((OpaqueType)var.type).resolvedType);
89
                    break;
90
                case IdentifierKind.ParameterIdentifier:
91
                    ParameterIdentifier param = (ParameterIdentifier)identifier;
92
                    System.Diagnostics.Debug.Assert(param.type is OpaqueType);
93
                    param.SetType(((OpaqueType)identifier.type).resolvedType);
94
                    break;
95
                default:
96
                    throw new NotImplementedException();
97
            }
98
        }
99

    
100
        private void ReplaceOpaqueInType(UlyssesType ulyssesType)
101
        {
102
            switch (ulyssesType.kind)
103
            {
104
                case TypeKind.FunctionType:
105
                    FunctionType func = (FunctionType)ulyssesType;
106
                    if (func.returnType is OpaqueType)
107
                        func.SetReturnType(((OpaqueType)func.returnType).resolvedType);
108

    
109
                    LinkedListNode<UlyssesType> item = func.parameter.First;
110
                    while (item != null)
111
                    {
112
                        if (item.Value.kind == TypeKind.OpaqueType)
113
                            item.Value = ((OpaqueType)item.Value).resolvedType;
114
                        item = item.Next;
115
                    }
116
                    break;
117
                case TypeKind.ListType:
118
                    ListType alist = (ListType)ulyssesType;
119
                    if (alist.innerType.kind == TypeKind.OpaqueType)
120
                        alist.SetInnerType(((OpaqueType)alist.innerType).resolvedType);
121
                    break;
122
                case TypeKind.MapType:
123
                    MapType aMap = (MapType)ulyssesType;
124
                    if (aMap.fromType.kind == TypeKind.OpaqueType)
125
                        aMap.SetFromType(((OpaqueType)aMap.fromType).resolvedType);
126
                    if (aMap.toType.kind == TypeKind.OpaqueType)
127
                        aMap.SetToType(((OpaqueType)aMap.toType).resolvedType);
128
                    break;
129
                /*case TypeKind.OoActionSystemType:
130
                   // handeled by the other cases...
131
                    break;*/
132
                case TypeKind.TupleType:
133
                    TupleType aTuple = (TupleType)ulyssesType;
134
                    LinkedListNode<UlyssesType> titem = aTuple.innerTypes.First;
135
                    while (titem != null)
136
                    {
137
                        if (titem.Value.kind == TypeKind.OpaqueType)
138
                            titem.Value = ((OpaqueType)titem.Value).resolvedType;
139
                        titem = titem.Next;
140
                    }
141
                    break;
142
                default:
143
                    throw new NotImplementedException();
144
            }
145
        }
146

    
147
        protected override void VisitAstElement(IAst subElement, IAst parent)
148
        {
149
            base.VisitAstElement(subElement, parent);
150

    
151
            /*see whether the subElement is an OpaqueType, we need
152
              to replace.*/
153
            if ((subElement != null)
154
              && (subElement.nodeType == AstNodeTypeEnum.type)
155
              && (((UlyssesType)subElement).kind == TypeKind.OpaqueType))
156
                ReplaceOpaque(parent);
157
        }
158

    
159
        public OoaReplaceOpaqueVisitor(ParserState aState)
160
            : base(aState)
161
        { }
162
    }
163
}