2013-12-23 01:26:24 -06:00
|
|
|
#include "Type.h"
|
|
|
|
|
|
|
|
|
|
Type::Type() {
|
|
|
|
|
indirection = 0;
|
|
|
|
|
baseType = none;
|
2015-05-18 04:46:03 -04:00
|
|
|
typeDefinition = nullptr;
|
|
|
|
|
templateDefinition = nullptr;
|
2015-05-18 17:14:12 -04:00
|
|
|
returnType = nullptr;
|
2015-06-28 14:27:48 -04:00
|
|
|
templateInstantiated = false;
|
2013-12-23 01:26:24 -06:00
|
|
|
}
|
|
|
|
|
|
2014-07-18 08:52:15 -07:00
|
|
|
Type::Type(ValueType typeIn, int indirectionIn) {
|
|
|
|
|
indirection = indirectionIn;
|
2013-12-23 01:26:24 -06:00
|
|
|
baseType = typeIn;
|
2015-05-18 04:46:03 -04:00
|
|
|
typeDefinition = nullptr;
|
|
|
|
|
templateDefinition = nullptr;
|
2015-05-18 17:14:12 -04:00
|
|
|
returnType = nullptr;
|
2015-06-28 14:27:48 -04:00
|
|
|
templateInstantiated = false;
|
2013-12-23 01:26:24 -06:00
|
|
|
}
|
|
|
|
|
|
2014-07-18 08:52:15 -07:00
|
|
|
Type::Type(ValueType typeIn, std::set<std::string> traitsIn) {
|
|
|
|
|
indirection = 0;
|
2013-12-23 01:26:24 -06:00
|
|
|
baseType = typeIn;
|
2014-07-18 08:52:15 -07:00
|
|
|
traits = traitsIn;
|
2015-05-18 04:46:03 -04:00
|
|
|
typeDefinition = nullptr;
|
|
|
|
|
templateDefinition = nullptr;
|
2015-05-18 17:14:12 -04:00
|
|
|
returnType = nullptr;
|
2015-06-28 14:27:48 -04:00
|
|
|
templateInstantiated = false;
|
2013-12-23 01:26:24 -06:00
|
|
|
}
|
|
|
|
|
|
2014-07-18 08:52:15 -07:00
|
|
|
Type::Type(NodeTree<ASTData>* typeDefinitionIn, int indirectionIn) {
|
|
|
|
|
indirection = indirectionIn;
|
2014-01-07 13:14:58 -05:00
|
|
|
baseType = none;
|
|
|
|
|
typeDefinition = typeDefinitionIn;
|
2015-05-18 04:46:03 -04:00
|
|
|
templateDefinition = nullptr;
|
2015-05-18 17:14:12 -04:00
|
|
|
returnType = nullptr;
|
2015-06-28 14:27:48 -04:00
|
|
|
templateInstantiated = false;
|
2014-01-07 13:14:58 -05:00
|
|
|
}
|
2014-07-18 08:52:15 -07:00
|
|
|
|
|
|
|
|
Type::Type(NodeTree<ASTData>* typeDefinitionIn, std::set<std::string> traitsIn) {
|
|
|
|
|
indirection = 0;
|
2014-01-07 13:14:58 -05:00
|
|
|
baseType = none;
|
|
|
|
|
typeDefinition = typeDefinitionIn;
|
2014-07-18 08:52:15 -07:00
|
|
|
traits = traitsIn;
|
2015-05-18 04:46:03 -04:00
|
|
|
templateDefinition = nullptr;
|
2015-05-18 17:14:12 -04:00
|
|
|
returnType = nullptr;
|
2015-06-28 14:27:48 -04:00
|
|
|
templateInstantiated = false;
|
2013-12-23 01:26:24 -06:00
|
|
|
}
|
|
|
|
|
|
2014-07-18 08:52:15 -07:00
|
|
|
Type::Type(ValueType typeIn, NodeTree<ASTData>* typeDefinitionIn, int indirectionIn, std::set<std::string> traitsIn) {
|
2014-01-07 13:14:58 -05:00
|
|
|
baseType = typeIn;
|
|
|
|
|
indirection = indirectionIn;
|
|
|
|
|
typeDefinition = typeDefinitionIn;
|
2014-07-18 08:52:15 -07:00
|
|
|
traits = traitsIn;
|
2015-05-18 04:46:03 -04:00
|
|
|
templateDefinition = nullptr;
|
2015-05-18 17:14:12 -04:00
|
|
|
returnType = nullptr;
|
2015-06-28 14:27:48 -04:00
|
|
|
templateInstantiated = false;
|
2015-05-18 04:46:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Type::Type(ValueType typeIn, NodeTree<ASTData>* typeDefinitionIn, int indirectionIn, std::set<std::string> traitsIn, std::vector<Type*> parameterTypesIn, Type* returnTypeIn) {
|
|
|
|
|
baseType = typeIn;
|
|
|
|
|
indirection = indirectionIn;
|
|
|
|
|
typeDefinition = typeDefinitionIn;
|
|
|
|
|
traits = traitsIn;
|
|
|
|
|
templateDefinition = nullptr;
|
|
|
|
|
parameterTypes = parameterTypesIn;
|
|
|
|
|
returnType = returnTypeIn;
|
2015-06-28 14:27:48 -04:00
|
|
|
templateInstantiated = false;
|
2015-05-18 04:46:03 -04:00
|
|
|
}
|
|
|
|
|
Type::Type(std::vector<Type*> parameterTypesIn, Type* returnTypeIn) {
|
|
|
|
|
baseType = function_type;
|
|
|
|
|
indirection = 0;
|
|
|
|
|
typeDefinition = nullptr;
|
|
|
|
|
templateDefinition = nullptr;
|
|
|
|
|
parameterTypes = parameterTypesIn;
|
|
|
|
|
returnType = returnTypeIn;
|
2015-06-28 14:27:48 -04:00
|
|
|
templateInstantiated = false;
|
2014-01-07 13:14:58 -05:00
|
|
|
}
|
2014-07-18 08:52:15 -07:00
|
|
|
|
|
|
|
|
Type::Type(ValueType typeIn, NodeTree<Symbol>* templateDefinitionIn, std::set<std::string> traitsIn) {
|
2014-05-09 02:56:55 -04:00
|
|
|
indirection = 0;
|
|
|
|
|
baseType = typeIn;
|
2015-05-18 04:46:03 -04:00
|
|
|
typeDefinition = nullptr;
|
2014-05-09 02:56:55 -04:00
|
|
|
templateDefinition = templateDefinitionIn;
|
2014-07-18 08:52:15 -07:00
|
|
|
traits = traitsIn;
|
2015-05-18 17:14:12 -04:00
|
|
|
returnType = nullptr;
|
2015-06-28 14:27:48 -04:00
|
|
|
templateInstantiated = false;
|
2014-05-09 02:56:55 -04:00
|
|
|
}
|
|
|
|
|
|
2013-12-23 01:26:24 -06:00
|
|
|
|
|
|
|
|
Type::~Type() {
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-06 13:13:40 -05:00
|
|
|
const bool Type::operator==(const Type &other) const {
|
2015-06-25 04:09:19 -04:00
|
|
|
bool first_part = ( baseType == other.baseType && indirection == other.indirection && typeDefinition == other.typeDefinition && templateDefinition == other.templateDefinition && other.traits == traits);
|
|
|
|
|
if (!first_part)
|
|
|
|
|
return false;
|
|
|
|
|
if ((returnType && !other.returnType) || (!returnType && other.returnType))
|
|
|
|
|
return false;
|
|
|
|
|
if (returnType && other.returnType)
|
|
|
|
|
if (*returnType != *other.returnType)
|
|
|
|
|
return false;
|
|
|
|
|
if (parameterTypes.size() != other.parameterTypes.size())
|
|
|
|
|
return false;
|
|
|
|
|
for (int i = 0; i < parameterTypes.size(); i++)
|
|
|
|
|
if (*parameterTypes[i] != *other.parameterTypes[i])
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
2014-03-06 13:13:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool Type::operator!=(const Type &other) const {
|
|
|
|
|
return(!this->operator==(other));
|
|
|
|
|
}
|
2015-06-25 04:09:19 -04:00
|
|
|
const bool Type::operator<(const Type &other) const {
|
|
|
|
|
if (baseType != other.baseType)
|
|
|
|
|
return baseType < other.baseType;
|
|
|
|
|
if (indirection != other.indirection)
|
|
|
|
|
return indirection < other.indirection;
|
|
|
|
|
if (typeDefinition != other.typeDefinition)
|
|
|
|
|
return typeDefinition < other.typeDefinition;
|
|
|
|
|
if (templateDefinition != other.templateDefinition)
|
|
|
|
|
return templateDefinition < other.templateDefinition;
|
|
|
|
|
if (traits != other.traits)
|
|
|
|
|
return traits < other.traits;
|
|
|
|
|
if ((returnType && !other.returnType) || (!returnType && other.returnType))
|
|
|
|
|
return returnType < other.returnType;
|
|
|
|
|
if (returnType && other.returnType)
|
|
|
|
|
if (*returnType != *other.returnType)
|
|
|
|
|
return *returnType < *other.returnType;
|
|
|
|
|
if (parameterTypes.size() != other.parameterTypes.size())
|
|
|
|
|
return parameterTypes.size() < other.parameterTypes.size();
|
|
|
|
|
for (int i = 0; i < parameterTypes.size(); i++)
|
|
|
|
|
if (*parameterTypes[i] != *other.parameterTypes[i])
|
|
|
|
|
return *parameterTypes[i] < *other.parameterTypes[i];
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-03-06 13:13:40 -05:00
|
|
|
|
2014-07-20 14:21:41 -07:00
|
|
|
std::string Type::toString(bool showTraits) {
|
2013-12-23 01:26:24 -06:00
|
|
|
std::string typeString;
|
|
|
|
|
switch (baseType) {
|
|
|
|
|
case none:
|
2014-01-07 13:14:58 -05:00
|
|
|
if (typeDefinition)
|
|
|
|
|
typeString = typeDefinition->getDataRef()->symbol.getName();
|
|
|
|
|
else
|
|
|
|
|
typeString = "none";
|
2013-12-23 01:26:24 -06:00
|
|
|
break;
|
2014-05-09 02:56:55 -04:00
|
|
|
case template_type:
|
|
|
|
|
typeString = "template: " + templateDefinition->getDataRef()->toString();
|
2014-05-19 20:00:35 -04:00
|
|
|
break;
|
|
|
|
|
case template_type_type:
|
|
|
|
|
typeString = "template_type_type";
|
|
|
|
|
break;
|
2013-12-23 01:26:24 -06:00
|
|
|
case void_type:
|
|
|
|
|
typeString = "void";
|
|
|
|
|
break;
|
|
|
|
|
case boolean:
|
|
|
|
|
typeString = "bool";
|
|
|
|
|
break;
|
|
|
|
|
case integer:
|
|
|
|
|
typeString = "int";
|
|
|
|
|
break;
|
|
|
|
|
case floating:
|
|
|
|
|
typeString = "float";
|
|
|
|
|
break;
|
|
|
|
|
case double_percision:
|
|
|
|
|
typeString = "double";
|
|
|
|
|
break;
|
|
|
|
|
case character:
|
|
|
|
|
typeString = "char";
|
|
|
|
|
break;
|
2015-05-18 04:46:03 -04:00
|
|
|
case function_type:
|
|
|
|
|
typeString = "function(";
|
|
|
|
|
for (Type *param : parameterTypes)
|
|
|
|
|
typeString += param->toString();
|
|
|
|
|
typeString += "): " + returnType->toString();
|
|
|
|
|
break;
|
2013-12-23 01:26:24 -06:00
|
|
|
default:
|
2014-01-07 13:14:58 -05:00
|
|
|
if (typeDefinition)
|
|
|
|
|
typeString = typeDefinition->getDataRef()->symbol.getName();
|
|
|
|
|
else
|
|
|
|
|
typeString = "unknown_type";
|
2013-12-23 01:26:24 -06:00
|
|
|
}
|
|
|
|
|
for (int i = 0; i < indirection; i++)
|
|
|
|
|
typeString += "*";
|
2015-06-19 17:13:06 -04:00
|
|
|
if (indirection < 0)
|
|
|
|
|
typeString += "negative indirection: " + intToString(indirection);
|
2014-07-20 14:21:41 -07:00
|
|
|
if (traits.size() && showTraits) {
|
2014-07-18 08:52:15 -07:00
|
|
|
typeString += "[ ";
|
|
|
|
|
for (auto i : traits)
|
|
|
|
|
typeString += i + " ";
|
|
|
|
|
typeString += "]";
|
|
|
|
|
}
|
2014-05-19 20:00:35 -04:00
|
|
|
//std::cout << "Extra components of " << typeString << " are " << indirection << " " << typeDefinition << " " << templateDefinition << std::endl;
|
2013-12-23 01:26:24 -06:00
|
|
|
return typeString;
|
|
|
|
|
}
|
2014-05-05 13:52:12 -04:00
|
|
|
|
|
|
|
|
Type* Type::clone() {
|
2015-05-18 04:46:03 -04:00
|
|
|
return new Type(baseType, typeDefinition, indirection, traits, parameterTypes, returnType);
|
2014-05-19 20:00:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Type::getIndirection() {
|
|
|
|
|
return indirection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Type::setIndirection(int indirectionIn) {
|
|
|
|
|
indirection = indirectionIn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Type::increaseIndirection() {
|
|
|
|
|
setIndirection(indirection+1);
|
|
|
|
|
}
|
|
|
|
|
void Type::decreaseIndirection() {
|
|
|
|
|
setIndirection(indirection-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Type::modifyIndirection(int mod) {
|
|
|
|
|
setIndirection(indirection + mod);
|
|
|
|
|
}
|
2015-06-05 00:34:24 -04:00
|
|
|
|
|
|
|
|
Type Type::withIncreasedIndirection() {
|
|
|
|
|
Type *newOne = clone();
|
|
|
|
|
newOne->increaseIndirection();
|
|
|
|
|
return *newOne;
|
|
|
|
|
}
|
|
|
|
|
Type Type::withDecreasedIndirection() {
|
|
|
|
|
Type *newOne = clone();
|
|
|
|
|
newOne->decreaseIndirection();
|
|
|
|
|
return *newOne;
|
|
|
|
|
}
|
|
|
|
|
|