Project

General

Profile

root / trunk / compiler / cppAst / ast / types / ActionSystemType.hpp @ 7

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 <ast/Containers.hpp>
29
#include <ast/types/Type.hpp>
30
#include <ast/IScope.hpp>
31
#include <ast/statements/Block.hpp>
32
#include <ast/SymbolTable.hpp>
33
#include <deque>
34
#include <string>
35
#include <cstdint>
36
#include <ast/identifiers/Identifier.hpp>
37
#include <ast/identifiers/FunctionIdentifier.hpp>
38
#include <base/Exceptions.hpp>
39
#include <ast/IAstVisitor.hpp>
40

    
41
namespace Ast {
42

    
43
class ActionSystemType; // forward
44

    
45

    
46
class ActionSystemInstance final
47
        : public Base::TiresiasObject
48
{
49
protected:
50
        std::int32_t m_numberOfCreatedObjects;
51
        ActionSystemType* m_type;
52
        ActionSystemInstance* m_parentObject;
53
        string m_name;
54

    
55
        ActionSystemInstance():
56
                TiresiasObject(),
57
                m_numberOfCreatedObjects (0),
58
                m_type (nullptr),
59
                m_parentObject (nullptr),
60
                m_name ("")
61
        {};
62
        ActionSystemInstance(const ActionSystemInstance& toCopy):
63
                TiresiasObject(),
64
                m_numberOfCreatedObjects (toCopy.m_numberOfCreatedObjects),
65
                m_type (toCopy.m_type),
66
                m_parentObject (toCopy.m_parentObject),
67
                m_name (toCopy.m_name)
68
        {};
69
public:
70
        friend class Ast;
71

    
72
        void init(ActionSystemType* typeRef, const char* name, std::int32_t numOfCreatedObjs, ActionSystemInstance* parentInstanceRef)
73
        {
74
                m_type = typeRef;
75
                m_name = name;
76
                m_numberOfCreatedObjects = numOfCreatedObjs;
77
                m_parentObject = parentInstanceRef;
78
        }
79

    
80
        std::string getName() const {return m_name;};
81
        std::int32_t getNumberOfCreatedObjects() const {return m_numberOfCreatedObjects;};
82
        ActionSystemType* getType() const {return m_type;};
83
        ActionSystemInstance* getParentObject() const {return m_parentObject;};
84

    
85
        constexpr static std::uint8_t TypeId = 2;
86
        static ActionSystemInstance* create() {
87
                return new ActionSystemInstance();
88
        }
89
};
90

    
91

    
92
class ActionSystemType
93
        : public Type
94
        , public IScope
95
{
96
protected:
97
        bool m_autoCons;
98
        bool m_isInSystemDescription;
99
        ActionSystemType* m_baseType;
100
        Block* m_doodBlock;
101
        IScope* m_parentScope;
102
        SymbolTable* m_symbols;
103
        ActionSystemInstanceList m_objects;
104
        ActionSystemInstanceList m_derivedObjects;
105
        FunctionIdentifier* m_doodFunction; // only avail after Finalisation Visitor has run. Wraps doodBlock in function.
106

    
107
        ActionSystemType():
108
                Type(TypeKind::OoActionSystemType),
109
                m_autoCons (false),
110
                m_isInSystemDescription (false),
111
                m_baseType (nullptr),
112
                m_doodBlock(nullptr),
113
                m_parentScope(nullptr),
114
                m_symbols(nullptr),
115
                m_objects (),
116
                m_derivedObjects(),
117
                m_doodFunction (nullptr)
118
        {};
119

    
120
        ActionSystemType(const ActionSystemType& toCopy):
121
                Type(toCopy),
122
                m_autoCons (toCopy.m_autoCons),
123
                m_isInSystemDescription (toCopy.m_isInSystemDescription),
124
                m_baseType (toCopy.m_baseType),
125
                m_doodBlock(toCopy.m_doodBlock),
126
                m_parentScope(toCopy.m_parentScope),
127
                m_symbols(toCopy.m_symbols),
128
                m_objects (toCopy.m_objects),
129
                m_derivedObjects(toCopy.m_derivedObjects),
130
                m_doodFunction (toCopy.m_doodFunction)
131
        {};
132

    
133
        static constexpr const char* s_doodFunctionName = ".dood";
134
        static ActionSystemType* create() {return new ActionSystemType();}
135
        static ActionSystemType* createCopy(const ActionSystemType& toCopy) {return new ActionSystemType(toCopy);}
136
public:
137
        friend class Ast;
138
        friend class Type;
139

    
140

    
141
        void init(TypeIdentifier* identifierRef, bool anonymousType, ActionSystemType* baseTypeRef,
142
                        IScope* parentScopeRef, Block* doodBlockRef, SymbolTable* symTab,
143
                        ActionSystemInstanceList* objectsListRef,
144
                        ActionSystemInstanceList* derivedObjectsListRef, bool autoConstruction, bool isInSystemDescription)
145
        {
146
                Type::init(identifierRef,anonymousType);
147
                m_baseType = baseTypeRef;
148
                m_parentScope = parentScopeRef;
149
                m_doodBlock = doodBlockRef;
150
                m_symbols = symTab;
151
                m_objects = std::move(*objectsListRef);
152
                m_derivedObjects = std::move(*derivedObjectsListRef);
153
                m_autoCons = autoConstruction;
154
                m_isInSystemDescription = isInSystemDescription;
155
        }
156

    
157
        bool autoConstruction() {return m_autoCons;};
158
        bool isInSystemDescription() {return m_isInSystemDescription;};
159
        Block* doodBlock() {return m_doodBlock;};
160
        std::string doodFunctionName() const {return s_doodFunctionName;}
161
        FunctionIdentifier* doodFunction() {return m_doodFunction; }
162
        ActionSystemType* baseType() {return m_baseType;};
163
        SymbolTable* symbols() {return m_symbols;};
164
        ActionSystemInstanceList& objects() {return m_objects;};
165
        ActionSystemInstanceList& derivedObjects() {return m_derivedObjects;};
166
        IScope* getParentScope() const override {return m_parentScope;};
167
        std::string getScopeName() const override {return m_identifier->toString();}
168

    
169
        void setDoodBlock(Block* anArg) {m_doodBlock = anArg;};
170
        void setDoodFunction(FunctionIdentifier* id) {m_doodFunction = id;}
171
        void setParentScope(IScope* parentScope) override {m_parentScope = parentScope;};
172
        void setBaseType(ActionSystemType* newType) {m_baseType = newType;};
173
        void setIsInSystemDescription(bool p) {m_isInSystemDescription = p;};
174

    
175
        void accept(IAstVisitor& visitor) override {visitor.visit(this);};
176
        Identifier* resolveIdentifier(const std::string& aName) const override {return m_symbols->get(aName);}
177
        void addIdentifier(Identifier* anIdentifier, void* /*tag*/) override {m_symbols->addIdentifier(anIdentifier);};
178
        SymbolTable* getAllSymbols() {return m_symbols;};
179

    
180
        IScope* asScope() final {return (IScope*) this;}
181
        bool isSubTypeOf(ActionSystemType* other){
182
                if(other == this)
183
                        return true;
184
                else if(m_baseType == nullptr)
185
                        return false;
186
                else
187
                        return m_baseType->isSubTypeOf(other);
188
        }
189
        bool isDefined(const Identifier* id) {
190
                bool result = false;
191
                ActionSystemType* aType = this;
192
                while (!result && aType != nullptr) {
193
                        result = aType->m_symbols->defined(id);
194
                        aType = aType->m_baseType;
195
                }
196
                return result;
197
        }
198
};
199
}