-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathType.h
More file actions
135 lines (115 loc) · 3.39 KB
/
Copy pathType.h
File metadata and controls
135 lines (115 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef __TYPE_H__
#define __TYPE_H__
#include <string>
#include <vector>
#include "SymbolTable.h"
#define INTSIZE 32
#define VALID_MAX 255 // arm汇编合适的立即数大小
class Type
{
private:
int kind;
protected:
enum { INT, VOID, FUNC, PTR, ARRAY, STRING };
int size;
public:
Type(int kind, int size = 0) : kind(kind), size(size){};
virtual ~Type(){};
virtual std::string toStr() = 0;
bool isInt() const { return kind == INT; };
bool isVoid() const { return kind == VOID; };
bool isFunc() const { return kind == FUNC; };
bool isPtr() const { return kind == PTR; };
bool isArray() const { return kind == ARRAY; };
bool isString() const { return kind == STRING; };
int getKind() const { return kind; };
int getSize() const { return size; };
};
class IntType : public Type
{
private:
bool constant;
public:
IntType(int size, bool constant = false)
: Type(Type::INT, size), constant(constant){};
std::string toStr();
bool isConst() const { return constant; };
};
class VoidType : public Type
{
public:
VoidType() : Type(Type::VOID){};
std::string toStr();
};
class FunctionType : public Type
{
private:
Type* returnType;
std::vector<Type*> paramsType;
std::vector<SymbolEntry*> paramsSe;
public:
FunctionType(Type* returnType, std::vector<Type*> paramsType, std::vector<SymbolEntry*> paramsSe)
: Type(Type::FUNC), returnType(returnType), paramsType(paramsType), paramsSe(paramsSe){};
void setParamsType(std::vector<Type*> paramsType)
{
this->paramsType = paramsType;
};
std::vector<Type*> getParamsType() { return paramsType; };
std::vector<SymbolEntry*> getParamsSe() { return paramsSe; };
Type* getRetType() { return returnType; };
std::string toStr();
};
class ArrayType : public Type
{
private:
Type* elementType; // 12/18 有可能是int(最低维度)有可能是array(高维度)
Type* arrayType = nullptr;
int length;
bool isConstant;
public:
ArrayType(Type* elementType, int length, bool isConstant = false) : Type(Type::ARRAY), elementType(elementType), length(length), isConstant(isConstant)
{
size = elementType->getSize() * length;
};
std::string toStr();
Type* getElementType() const { return elementType; };
Type* getArrayType() const { return arrayType; };
int getCurDimWidth() const { return length; };
void linkNextDim(Type* arrayType) { this->arrayType = arrayType; };
bool isConst() const { return isConstant; };
};
class StringType : public Type
{
private:
int length;
public:
StringType(int length) : Type(Type::STRING), length(length){};
int getCurDimWidth() const { return length; };
std::string toStr();
};
class PointerType : public Type
{
private:
Type *valueType;
public:
PointerType(Type* valueType) : Type(Type::PTR)
{
this->valueType = valueType;
};
std::string toStr();
Type* getType() const { return valueType; };
};
class TypeSystem
{
private:
static IntType commonInt;
static IntType commonBool;
static VoidType commonVoid;
static IntType commonConstInt;
public:
static Type* intType;
static Type* voidType;
static Type* boolType;
static Type* constIntType;
};
#endif