root / trunk / compiler / cppAst / ast / types / Type.cpp @ 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 |
#include "Type.hpp" |
26 |
#include <ast/types/IntType.hpp> |
27 |
#include <ast/types/BoolType.hpp> |
28 |
#include <ast/types/EnumType.hpp> |
29 |
#include <ast/types/ListType.hpp> |
30 |
#include <ast/types/TupleType.hpp> |
31 |
#include <ast/types/FunctionType.hpp> |
32 |
#include <ast/types/ActionSystemType.hpp> |
33 |
#include <ast/types/NullType.hpp> |
34 |
#include <ast/types/ValuedEnumType.hpp> |
35 |
#include <ast/types/PointerType.hpp> |
36 |
|
37 |
namespace Ast {
|
38 |
|
39 |
|
40 |
static void abstractError() { |
41 |
throw new Base::NotImplementedException(); |
42 |
} |
43 |
|
44 |
|
45 |
Base::FuncPtr Type::s_typeVmt [TYPEVMETHOD_NR_ITEMS][TYPEKIND_NR_ITEMS] = |
46 |
{ |
47 |
// pointers to create methods, index 0
|
48 |
{ /*IntType = 0*/ (Base::FuncPtr) &IntType::create,
|
49 |
/*BoolType = 1*/ (Base::FuncPtr) &BoolType::create,
|
50 |
/*FloatType = 2*/ &abstractError,
|
51 |
/*EnumeratedType = 3*/ (Base::FuncPtr) &EnumType::create,
|
52 |
/*ListType = 4*/ (Base::FuncPtr) &ListType::create,
|
53 |
/*MapType = 5*/ &abstractError,
|
54 |
/*QrType = 6*/ &abstractError,
|
55 |
/*TupleType = 7*/ (Base::FuncPtr) &TupleType::create,
|
56 |
/*FunctionType = 8*/ (Base::FuncPtr) &FunctionType::create,
|
57 |
/*ActionSystemType= 9*/(Base::FuncPtr) &ActionSystemType::create,
|
58 |
/*OpaqueType = 10*/ &abstractError,
|
59 |
/*Null = 11*/ (Base::FuncPtr) &NullType::create,
|
60 |
/*Any = 12*/ &abstractError,
|
61 |
/*ValuedEnumeratedType = 13*/(Base::FuncPtr) &ValuedEnumType::create,
|
62 |
/*Pointer = 14*/ (Base::FuncPtr) &PointerType::create
|
63 |
}, |
64 |
// pointers to copy constructors, index 1
|
65 |
{ /*IntType = 0*/ (Base::FuncPtr) &IntType::createCopy,
|
66 |
/*BoolType = 1*/ (Base::FuncPtr) &BoolType::createCopy,
|
67 |
/*FloatType = 2*/ &abstractError,
|
68 |
/*EnumeratedType = 3*/ (Base::FuncPtr) &EnumType::createCopy,
|
69 |
/*ListType = 4*/ (Base::FuncPtr) &ListType::createCopy,
|
70 |
/*MapType = 5*/ &abstractError,
|
71 |
/*QrType = 6*/ &abstractError,
|
72 |
/*TupleType = 7*/ (Base::FuncPtr) &TupleType::createCopy,
|
73 |
/*FunctionType = 8*/ (Base::FuncPtr) &FunctionType::createCopy,
|
74 |
/*ActionSystemType= 9*/(Base::FuncPtr) &ActionSystemType::createCopy,
|
75 |
/*OpaqueType = 10*/ &abstractError,
|
76 |
/*Null = 11*/ (Base::FuncPtr) &NullType::createCopy,
|
77 |
/*Any = 12*/ &abstractError,
|
78 |
/*ValuedEnumeratedType = 13*/(Base::FuncPtr) &ValuedEnumType::createCopy,
|
79 |
/*Pointer = 14*/ (Base::FuncPtr) &PointerType::createCopy
|
80 |
} |
81 |
}; |
82 |
|
83 |
|
84 |
void Type::accept(IAstVisitor& ) {
|
85 |
throw new Base::NotImplementedException(); |
86 |
}; |
87 |
|
88 |
std::string Type::toString() const { |
89 |
std::string result ("??"); |
90 |
if (m_identifier != nullptr) |
91 |
result = m_identifier->tokenText(); |
92 |
return result; // NRVO |
93 |
} |
94 |
|
95 |
Type* Type::createVirtual(TypeKind type) { |
96 |
return (std::uint8_t)type < TYPEKIND_NR_ITEMS ? ((ConstrPtr)s_typeVmt[0][(std::uint8_t)type])() : nullptr; |
97 |
} |
98 |
|
99 |
Type* Type::createVirtual(const Type& toCopy) {
|
100 |
std::uint8_t kind ((std::uint8_t)toCopy.kind()); |
101 |
return ((CopyConstrPtr)s_typeVmt[1][kind])(toCopy); |
102 |
} |
103 |
|
104 |
|
105 |
bool Type::equal(Type* type1, Type* type2)
|
106 |
{ |
107 |
if ((type1 == nullptr) || (type2 == nullptr)) |
108 |
return false; |
109 |
|
110 |
// if of different kind, then return false..
|
111 |
TypeKind tk1 = type1->kind(); |
112 |
TypeKind tk2 = type2->kind(); |
113 |
if (tk1 != tk2)
|
114 |
return false; |
115 |
|
116 |
// if same kind, make a rigorous check
|
117 |
switch (tk1)
|
118 |
{ |
119 |
case TypeKind::IntType: {
|
120 |
IntType* intt1 = (IntType*)type1; |
121 |
IntType* intt2 = (IntType*)type2; |
122 |
return intt1->rangeLow() == intt2->rangeLow() && intt1->rangeHigh() == intt2->rangeHigh();
|
123 |
} |
124 |
|
125 |
case TypeKind::BoolType:
|
126 |
return true; |
127 |
|
128 |
// case TypeKind::FloatType:
|
129 |
// FloatType* floatt1 = (FloatType*)type1;
|
130 |
// FloatType* floatt2 = (FloatType*)type2;
|
131 |
// return floatt1->low() == floatt2->low() &&
|
132 |
// floatt1->high() == floatt2->high() &&
|
133 |
// floatt1->precision() == floatt2->precision();
|
134 |
|
135 |
case TypeKind::ValuedEnumeratedType:
|
136 |
case TypeKind::EnumeratedType:
|
137 |
return type1 == type2; // ref equ |
138 |
|
139 |
case TypeKind::ListType: {
|
140 |
ListType* listt1 = (ListType*)type1; |
141 |
ListType* listt2 = (ListType*)type2; |
142 |
return equal(listt1->innerType(), listt2->innerType()) &&
|
143 |
listt1->maxNumberOfElements() == listt2->maxNumberOfElements(); // need this because of covertype
|
144 |
} |
145 |
|
146 |
// case TypeKind::MapType:
|
147 |
// MapType* mapt1 = (MapType*)type1;
|
148 |
// MapType* mapt2 = (MapType*)type2;
|
149 |
// return equal(mapt1->fromType(), mapt2->fromType()) &&
|
150 |
// equal(mapt1->toType(), mapt2->toType()) &&
|
151 |
// mapt1->maxNumberOfElements() == mapt2->maxNumberOfElements(); // need this because of covertype
|
152 |
|
153 |
case TypeKind::TupleType: {
|
154 |
TupleType* tuplet1 = (TupleType*)type1; |
155 |
TupleType* tuplet2 = (TupleType*)type2; |
156 |
if (tuplet1->innerTypes().size() != tuplet2->innerTypes().size())
|
157 |
return false; |
158 |
auto innert1 = tuplet1->innerTypes().begin();
|
159 |
auto innert2 = tuplet2->innerTypes().begin();
|
160 |
while (innert1 != tuplet1->innerTypes().end())
|
161 |
{ |
162 |
if (!equal(*innert1, *innert2))
|
163 |
return false; |
164 |
innert1++; |
165 |
innert2++; |
166 |
} |
167 |
return true; |
168 |
} |
169 |
|
170 |
case TypeKind::OoActionSystemType:
|
171 |
return type1 == type2;
|
172 |
|
173 |
case TypeKind::Null:
|
174 |
case TypeKind::Any:
|
175 |
case TypeKind::Pointer:
|
176 |
return true; |
177 |
|
178 |
default: {
|
179 |
std::cerr << "Unknown type in type comparison. Aborting. " << (int) tk1 << std::endl; |
180 |
abort(); |
181 |
} |
182 |
} |
183 |
} |
184 |
|
185 |
} |