Mercurial > projects > dang
annotate sema/DType.d @ 49:c7cde6af0095 new_gen
Seperated the AST from LLVM
* Changed SmallArray sligthly
* Added a NullAction that doesn't do anything
author | Anders Halager <halager@gmail.com> |
---|---|
date | Sat, 26 Apr 2008 13:15:37 +0200 |
parents | 1a7a308f75b2 |
children | 79cb0afafabe |
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 |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
58 static DInteger |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
59 Bool, |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
60 Byte, UByte, Short, UShort, |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
61 Int, UInt, Long, ULong; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
62 |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
63 static DType Void; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
64 |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
65 static this() |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
66 { |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
67 Void = new DType("void"); |
26 | 68 |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
69 Bool = new DInteger("bool", 1, false); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
70 Byte = new DInteger("byte", 8, false); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
71 UByte = new DInteger("ubyte", 8, true); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
72 Short = new DInteger("short", 16, false); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
73 UShort = new DInteger("ushort", 16, true); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
74 Int = new DInteger("int", 32, false); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
75 UInt = new DInteger("uint", 32, true); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
76 Long = new DInteger("long", 64, false); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
77 ULong = new DInteger("ulong", 64, true); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
78 } |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
79 } |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
80 |
49
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
81 /** |
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
82 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
|
83 */ |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
84 class DInteger : DType |
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 this(char[] name, int bits, bool unsigned) |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
87 { |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
88 super(name, null); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
89 this.bits = bits; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
90 this.unsigned = unsigned; |
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 |
39
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
93 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
|
94 |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
95 int bits; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
96 bool unsigned; |
26 | 97 } |
98 | |
99 class DStruct : DType | |
100 { | |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
101 this(Identifier id, DType actual = null) |
26 | 102 { |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
103 super(id, actual); |
26 | 104 } |
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
|
105 |
39
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
106 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
|
107 |
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
|
108 void setMembers(DType[char[]] members) |
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
|
109 { |
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
|
110 this.members = members; |
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
|
111 |
39
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
112 foreach (type; members) |
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
113 bytes_total += type.byteSize(); |
49
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
114 } |
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
|
115 |
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
|
116 DType[char[]] members; |
39
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
117 private int bytes_total; |
26 | 118 } |
119 | |
120 class DFunction : DType | |
121 { | |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
122 this(Identifier id, DType actual = null) |
26 | 123 { |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
124 super(id, actual); |
26 | 125 } |
49
c7cde6af0095
Seperated the AST from LLVM
Anders Halager <halager@gmail.com>
parents:
39
diff
changeset
|
126 |
26 | 127 DType[] params; |
128 DType return_type; | |
129 } | |
130 |