Mercurial > projects > dang
annotate sema/DType.d @ 58:fc62c5296a1c new_gen
Add types to our Exp
author | Anders Halager <halager@gmail.com> |
---|---|
date | Mon, 28 Apr 2008 21:51:39 +0200 |
parents | 79cb0afafabe |
children | 9f8131676242 |
rev | line source |
---|---|
26 | 1 module sema.DType; |
2 | |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
3 import lexer.Token, |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
4 ast.Exp; |
26 | 5 |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
6 import tango.io.Stdout; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
7 |
26 | 8 class DType |
9 { | |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
10 private char[] id; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
11 private Location loc; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
12 public DType actual; |
26 | 13 |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
14 this(Identifier id, DType actual = null) |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
15 { |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
16 Token temp = id.token; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
17 this.id = temp.get; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
18 this.loc = temp.location; |
49
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
19 this.actual = actual is null? this : actual; |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
20 } |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
21 |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
22 this(char[] id, DType actual = null) |
26 | 23 { |
24 this.id = id; | |
49
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
25 this.actual = actual is null? this : actual; |
26 | 26 } |
27 | |
28 int opEquals(Object o) | |
29 { | |
30 if (auto t = cast(DType)o) | |
31 return this.actual is t.actual; | |
32 return 0; | |
33 } | |
34 | |
35 int opCmp(Object o) | |
36 { | |
37 if (auto t = cast(DType)o) | |
38 return cast(void*)this.actual - cast(void*)t.actual; | |
39 return 0; | |
40 } | |
41 | |
49
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
42 /** |
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
43 Hashing is done by casting the reference to a void* and taking that |
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
44 value, but this gives a bad distribution of hash-values. |
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
45 |
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
46 Multiple DType's allocated close to each other will only have a |
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
47 difference in the lower bits of their hashes. |
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
48 */ |
26 | 49 hash_t toHash() |
50 { | |
51 return cast(hash_t)(cast(void*)this); | |
52 } | |
53 | |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
54 char[] name() { return id; } |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
55 Location getLoc() { return loc; } |
39
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
56 int byteSize() { return 0; } |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
57 |
58 | 58 /** |
59 Can this type legally be converted to that type with no casts? | |
60 True for short -> int etc. | |
61 */ | |
62 bool hasImplicitConversionTo(DType that) { return false; } | |
63 | |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
64 static DInteger |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
65 Bool, |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
66 Byte, UByte, Short, UShort, |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
67 Int, UInt, Long, ULong; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
68 |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
69 static DType Void; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
70 |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
71 static this() |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
72 { |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
73 Void = new DType("void"); |
26 | 74 |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
75 Bool = new DInteger("bool", 1, false); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
76 Byte = new DInteger("byte", 8, false); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
77 UByte = new DInteger("ubyte", 8, true); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
78 Short = new DInteger("short", 16, false); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
79 UShort = new DInteger("ushort", 16, true); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
80 Int = new DInteger("int", 32, false); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
81 UInt = new DInteger("uint", 32, true); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
82 Long = new DInteger("long", 64, false); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
83 ULong = new DInteger("ulong", 64, true); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
84 } |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
85 } |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
86 |
49
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
87 /** |
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
88 Class to represent the built-in integer types, from byte to long. |
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
89 */ |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
90 class DInteger : DType |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
91 { |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
92 this(char[] name, int bits, bool unsigned) |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
93 { |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
94 super(name, null); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
95 this.bits = bits; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
96 this.unsigned = unsigned; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
97 } |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
98 |
39
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
99 override int byteSize() { return bits / 8; } |
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
100 |
58 | 101 override bool hasImplicitConversionTo(DType that) |
102 { | |
103 if (auto o = cast(DInteger)that) | |
104 return this.bits >= o.bits; | |
105 return false; | |
106 } | |
107 | |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
108 int bits; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
109 bool unsigned; |
26 | 110 } |
111 | |
112 class DStruct : DType | |
113 { | |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
114 this(Identifier id, DType actual = null) |
26 | 115 { |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
116 super(id, actual); |
26 | 117 } |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
118 |
39
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
119 int byteSize() { return bytes_total; } |
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
120 |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
121 void addMember(DType type, char[] name) |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
122 { |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
123 auto s = DStructMember(type, members.length); |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
124 members[name] = s; |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
125 |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
126 bytes_total += type.byteSize(); |
49
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
127 } |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
128 |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
129 int indexOf(char[] name) |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
130 { |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
131 if(name in members) |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
132 return members[name].index; |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
133 |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
134 return -1; |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
135 } |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
136 |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
137 DType typeOf(char[] name) |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
138 { |
58 | 139 if (auto res = name in members) |
140 return res.type; | |
141 return null; | |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
142 } |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
143 |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
144 DStructMember[char[]] members; |
39
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
145 private int bytes_total; |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
146 |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
147 struct DStructMember |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
148 { |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
149 DType type; |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
150 int index; |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
49
diff
changeset
|
151 } |
26 | 152 } |
153 | |
154 class DFunction : DType | |
155 { | |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
156 this(Identifier id, DType actual = null) |
26 | 157 { |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
158 super(id, actual); |
26 | 159 } |
49
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
160 |
26 | 161 DType[] params; |
162 DType return_type; | |
163 } | |
164 |