root / trunk / compiler / cppAst / ast / types / Type.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 <cstdint> |
29 |
#include <string> |
30 |
|
31 |
#include <base/TiresiasObject.hpp> |
32 |
#include <ast/identifiers/TypeIdentifier.hpp> |
33 |
#include <ast/AstElement.hpp> |
34 |
#include <base/Exceptions.hpp> |
35 |
|
36 |
namespace Ast {
|
37 |
|
38 |
/**
|
39 |
* You must not change the assigned values as this
|
40 |
* needs to be kept in sync with the definitions in Argos!
|
41 |
*/
|
42 |
enum class TypeKind : std::uint8_t |
43 |
{ |
44 |
IntType = 0,
|
45 |
BoolType = 1,
|
46 |
FloatType = 2,
|
47 |
EnumeratedType = 3,
|
48 |
ListType = 4,
|
49 |
MapType = 5,
|
50 |
QrType = 6,
|
51 |
TupleType = 7,
|
52 |
FunctionType = 8,
|
53 |
OoActionSystemType = 9,
|
54 |
OpaqueType = 10,
|
55 |
Null = 11,
|
56 |
Any = 12,
|
57 |
ValuedEnumeratedType = 13,
|
58 |
Pointer = 14, // used in backend only, to interface with the runtime |
59 |
TYPEKIND_LAST_ITEM = Pointer |
60 |
}; |
61 |
|
62 |
#define TYPEKIND_NR_ITEMS ((std::uint8_t)TypeKind::TYPEKIND_LAST_ITEM + 1) |
63 |
|
64 |
|
65 |
enum class TypeVMethod: std::uint8_t { |
66 |
Constr = 0,
|
67 |
CopyConstr = 1,
|
68 |
TYPEVMETHOD_LAST_ITEM = CopyConstr |
69 |
}; |
70 |
|
71 |
#define TYPEVMETHOD_NR_ITEMS ((std::uint8_t)TypeVMethod::TYPEVMETHOD_LAST_ITEM + 1) |
72 |
|
73 |
|
74 |
class Type |
75 |
: public AstElement
|
76 |
{ |
77 |
protected:
|
78 |
typedef Type* (*ConstrPtr)();
|
79 |
typedef Type* (*CopyConstrPtr)(const Type& toCopy); |
80 |
|
81 |
const TypeKind m_kind; // byte @ +2 byte |
82 |
bool m_anonymousType; // byte @ +3 |
83 |
TypeIdentifier* m_identifier = nullptr;
|
84 |
|
85 |
Type(TypeKind kind): |
86 |
AstElement(AstNodeTypeEnum::type), |
87 |
m_kind(kind), |
88 |
m_anonymousType (false),
|
89 |
m_identifier (nullptr)
|
90 |
{}; |
91 |
Type(const Type &toCopy):
|
92 |
AstElement(AstNodeTypeEnum::type), |
93 |
m_kind(toCopy.m_kind), |
94 |
m_anonymousType (toCopy.m_anonymousType), |
95 |
m_identifier (toCopy.m_identifier) |
96 |
{}; |
97 |
|
98 |
static Base::FuncPtr s_typeVmt [TYPEVMETHOD_NR_ITEMS][TYPEKIND_NR_ITEMS];
|
99 |
static Type* createVirtual(TypeKind aType); // virtual constructor used by Ast |
100 |
static Type* createVirtual(const Type& toCopy); // virtual copy constructor used by Ast |
101 |
public:
|
102 |
friend class Ast; |
103 |
|
104 |
void init(TypeIdentifier* id, bool anonymousType) { |
105 |
m_identifier = id; |
106 |
m_anonymousType = anonymousType; |
107 |
} |
108 |
|
109 |
TypeKind kind() const {
|
110 |
return m_kind;
|
111 |
} |
112 |
TypeIdentifier* identifier() const {
|
113 |
return m_identifier;
|
114 |
} |
115 |
std::string toString() const; |
116 |
|
117 |
void setTypeIdentifier(TypeIdentifier* anId) {
|
118 |
m_identifier = anId; |
119 |
} |
120 |
bool isAnonymousType() {
|
121 |
return m_anonymousType;
|
122 |
} |
123 |
|
124 |
void accept(IAstVisitor& visitor) override; |
125 |
|
126 |
static bool equal(Type* type1, Type* type2); |
127 |
|
128 |
virtual bool isSigned(){return false;} |
129 |
|
130 |
virtual uint32_t bitwidth(){throw new Base::NotImplementedException();} |
131 |
|
132 |
bool requiresCast(Type* targetType) {
|
133 |
return bitwidth() != targetType->bitwidth() || isSigned() != targetType->isSigned();
|
134 |
} |
135 |
|
136 |
}; |
137 |
} |