Project

General

Profile

root / trunk / compiler / cppAst / ast / expressions / Expression.hpp @ 3

1
/**
2
  *
3
  *                  OOAS Compiler - C++ AST
4
  *
5
  * Copyright 2015, AIT Austrian Institute of Technology.
6
  * All rights reserved.
7
  *
8
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
9
  *
10
  * If you modify the file please update the list of contributors below to in-
11
  * clude your name. Please also stick to the coding convention of using TABs
12
  * to do the basic (block-level) indentation and spaces for anything after
13
  * that. (Enable the display of special chars and it should be pretty obvious
14
  * what this means.) Also, remove all trailing whitespace.
15
  *
16
  * Contributors:
17
  *               Willibald Krenn (AIT)
18
  *               Stephan Zimmerer (AIT)
19
  *               Christoph Czurda (AIT)
20
  *
21
  */
22

    
23

    
24

    
25

    
26
#pragma once
27

    
28
#include <base/TiresiasObject.hpp>
29
#include <ast/AstElement.hpp>
30
#include <ast/types/Type.hpp>
31
#include <cstdint>
32
#include <ast/Containers.hpp>
33
#include <ast/IAstVisitor.hpp>
34
#include <base/Exceptions.hpp>
35

    
36

    
37
namespace Ast {
38

    
39

    
40
/**
41
 * You must not change the assigned values as this
42
 * needs to be kept in sync with the definitions in Argos!
43
 */
44
enum class ExpressionKind : std::uint8_t
45
{
46
        /*tern.*/
47
        conditional = 0,
48
        /*map binary*/
49
        domresby = 1, domresto = 2, rngresby = 3, rngresto = 4, munion = 5,
50
        /*set/list binary*/
51
        conc = 6, diff = 7, inter = 8, elemin = 9, notelemin = 10, subset = 11, _union = 12,
52
        /*numeric binary*/
53
        div = 13, greater = 14, greaterequal = 15, idiv = 16, less = 17, lessequal = 18,
54
        minus = 19, mod = 20, pow = 21, prod = 22, sum = 23,
55
        /*bool binary*/
56
        _and = 24, biimplies = 25, implies = 26, _or = 27,
57
        /*other binary*/
58
        equal = 28, notequal = 29, seqmod_mapoverride = 30,
59
        /*map unary*/
60
        dom = 31, range = 32, merge = 33,
61
        /*set/list unary*/
62
        card = 34, dconc = 35, dinter = 36, dunion = 37, elems = 38, head = 39,
63
        inds = 40, len = 41, tail = 42,
64
        /*unary numberic*/
65
        unminus = 43, unplus = 44, _not = 45, abs = 46,
66
        /*unary quantors*/
67
        forall = 47, exists = 48,
68
        /*Constructors*/
69
        ListConstr = 49, SetConstr = 50, MapConstr = 51, TupleConstr = 52, ObjectConstr = 53, QValConstr = 54,
70
        /* identifier */
71
        Identifier = 55,
72
        UnresolvedIdentifier = 56,
73
        Type = 57,
74
        /* tuple access and method call */
75
        TupleMapAccess = 58, Call = 59,
76
        /* point operator */
77
        Access = 60,
78
        /* primed */
79
        Primed = 61,
80
        /* cast */
81
        Cast = 62,
82
        /* fold */
83
        foldLR = 63,
84
        foldRL = 64,
85

    
86
        /* values */
87
        Value_Integer = 65,
88
        Value_Bool = 66,
89
        Value_Reference = 67,
90

    
91
        EXPRESSIONKIND_LAST_ITEM = Value_Reference
92
};
93

    
94
#define EXPRESSIONKIND_NR_ITEMS ((std::uint8_t)ExpressionKind::EXPRESSIONKIND_LAST_ITEM + 1)
95

    
96

    
97
class Expression:
98
        public AstElement
99
{
100
protected:
101
        typedef Expression* (*ConstrPtr)(ExpressionKind k);
102
        typedef Expression* (*CopyConstrPtr) (const Expression& toCopy);
103

    
104
        ExpressionKind m_kind;
105
        Type* m_type;
106
        std::int32_t m_line;
107
        std::int32_t m_pos;
108
        SymbolTable* m_freeVariables;
109
        FunctionIdentifierList m_callTargets;
110

    
111
        Expression(ExpressionKind kind):
112
                AstElement(AstNodeTypeEnum::expression),
113
                m_kind(kind),
114
                m_type (nullptr),
115
                m_line (-1),
116
                m_pos  (-1),
117
                m_freeVariables (nullptr)
118
        {}
119
        Expression(const Expression& toCopy):
120
                AstElement(AstNodeTypeEnum::expression),
121
                m_kind (toCopy.m_kind),
122
                m_type (toCopy.m_type),
123
                m_line (toCopy.m_line),
124
                m_pos  (toCopy.m_pos),
125
                m_freeVariables (toCopy.m_freeVariables)
126
        {}
127
        ~Expression()
128
        {}
129

    
130
        static Base::FuncPtr s_exprVmt [2][EXPRESSIONKIND_NR_ITEMS];
131
        static Expression* createVirtual(ExpressionKind k);
132
        static Expression* createVirtual(const Expression& toCopy);
133
public:
134
        friend class Ast;
135

    
136
        void init(
137
                        std::int32_t line,
138
                        std::int32_t pos,
139
                        Type* typeRef,IdentifierList* callTargetsIdentifierListRef,
140
                        SymbolTable* symbTabRef)
141
        {
142
                m_line = line;
143
                m_pos = pos;
144
                m_type = typeRef;
145
                m_callTargets.clear();
146
                if (callTargetsIdentifierListRef != nullptr) {
147
                        for (auto& id: *callTargetsIdentifierListRef) {
148
                                IdentifierKind kind = id->kind();
149
                                if (kind != IdentifierKind::MethodIdentifier &&
150
                                    kind != IdentifierKind::NamedActionIdentifier)
151
                                {
152
                                        throw new Base::InvalidArgumentException("Calltargets must be methods or actions.");
153
                                }
154
                                m_callTargets.push_back((FunctionIdentifier*)id);
155
                        }
156
                }
157
                m_freeVariables = symbTabRef;
158
        }
159

    
160

    
161
        ExpressionKind kind() const { return m_kind; };
162
        Type* type() const { return m_type; };
163
        std::int32_t line() const { return m_line; };
164
        std::int32_t pos() const { return m_pos; };
165
        SymbolTable* freeVariables() { return m_freeVariables; };
166
        FunctionIdentifierList& callTargets() { return m_callTargets; };
167

    
168
        void accept(IAstVisitor& visitor) override;
169
};
170

    
171
}