-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast_type.h
80 lines (64 loc) · 2.08 KB
/
ast_type.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
/* File: ast_type.h
* ----------------
* In our parse tree, Type nodes are used to represent and
* store type information. The base Type class is used
* for built-in types, the NamedType for classes and interfaces,
* and the ArrayType for arrays of other types.
*/
#ifndef _H_ast_type
#define _H_ast_type
#include "ast.h"
#include "list.h"
#include <iostream>
class Type : public Node
{
protected:
char *typeName;
public :
static Type *intType, *doubleType, *boolType, *voidType,
*nullType, *stringType, *errorType;
Type(yyltype loc) : Node(loc) {}
Type(const char *str);
virtual void PrintToStream(std::ostream& out) { out << typeName; }
friend std::ostream& operator<<(std::ostream& out, Type *t) { t->PrintToStream(out); return out; }
virtual bool IsEquivalentTo(Type *other) { return this == other; }
virtual bool IsArrayType() { return false; }
virtual bool IsNamedType() { return false; }
virtual bool IsCompatibleWith(Type *other);
virtual bool IsNumeric() { return this == Type::intType || this == Type::doubleType; }
virtual bool IsError() { return false;}
virtual Type *LesserType(Type *other);
void Emit(CodeGenerator *cg) {}
};
class NamedType : public Type
{
protected:
Identifier *id;
Decl *cachedDecl; // either class or inteface
bool isError;
public:
NamedType(Identifier *i);
void PrintToStream(std::ostream& out) { out << id; }
void Check();
Decl *GetDeclForType();
bool IsInterface();
bool IsClass();
Identifier *GetId() { return id; }
bool IsEquivalentTo(Type *other);
bool IsNamedType() { return true; }
bool IsCompatibleWith(Type *other);
bool IsError() { return isError;}
};
class ArrayType : public Type
{
protected:
Type *elemType;
public:
ArrayType(yyltype loc, Type *elemType);
void PrintToStream(std::ostream& out) { out << elemType << "[]"; }
void Check();
bool IsEquivalentTo(Type *other);
bool IsArrayType() { return true; }
Type *GetArrayElemType() { return elemType; }
};
#endif