-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.h
91 lines (73 loc) · 1.78 KB
/
types.h
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
#ifndef _TYPES_H
#define _TYPES_H
#include "syntaxtree.h"
#include <map>
#include <list>
#include <string>
namespace Semantic {
enum BaseType {
TYPE_ERROR,
TYPE_VOID,
TYPE_NIL,
TYPE_INT,
TYPE_STRING,
TYPE_ARRAY,
TYPE_RECORD,
TYPE_NAMEREFERENCE
};
class Type {
public:
BaseType basetype;
bool is_loop_variable;
Type(BaseType _basetype) : basetype(_basetype), is_loop_variable(false) {}
Type *resolve();
};
class ForwardReferenceType: public Type {
public:
std::string name;
Type *meaning;
Syntax::Tree reference_position;
ForwardReferenceType(const std::string _name, Syntax::Tree _position) :
Type(TYPE_NAMEREFERENCE), name(_name), meaning(NULL),
reference_position(_position) {}
};
class ArrayType: public Type {
public:
/**
* Unique within the TypesEnvironment for all arrays and records
* Used for checking if two Type * are the same type
*/
ObjectId id;
Type *elemtype;
ArrayType(ObjectId _id, Type *_elemtype) :
Type(TYPE_ARRAY), id(_id), elemtype(_elemtype) {}
};
class RecordField {
public:
std::string name;
Type *type;
int offset;
RecordField(const std::string &_name, Type *_type) : name(_name), type(_type) {}
};
class RecordType: public Type {
public:
/**
* Unique within the TypesEnvironment for all arrays and records
* Used for checking if two Type * are the same type
*/
ObjectId id;
typedef std::list<RecordField> FieldsList;
typedef std::map<std::string, RecordField*> FieldsMap;
FieldsMap fields;
FieldsList field_list;
int data_size;
RecordType(ObjectId _id) : Type(TYPE_RECORD), id(_id), data_size(0) {}
RecordField *addField(const std::string &name, Type *type)
{
field_list.push_back(RecordField(name, type));
fields.insert(std::make_pair(name, &(field_list.back())));
return &(field_list.back());
}
};
}
#endif