annotate sema/DType.d @ 39:1a7a308f75b2 new_gen

Added some struct tests, and implemented a wrong struct assignment It assumes 8 bytes for all struct, we have no DType available at that point Slight improvement to an error message (Member access to unknown members)
author Anders Halager <halager@gmail.com>
date Mon, 21 Apr 2008 22:47:12 +0200
parents 858b9805843d
children c7cde6af0095
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
1 module sema.DType;
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
2
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
3 import tango.text.Util : jhash;
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
4
27
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
5 import LLVM = llvm.llvm;
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
6
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
7 import lexer.Token,
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
8 ast.Exp;
26
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
9
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
10 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
11
26
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
12 class DType
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
13 {
27
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
14 private char[] id;
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
15 private Location loc;
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
16 public DType actual;
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
17 private LLVM.Type llvmType;
26
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
18
27
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
19 this(Identifier id, DType actual = null)
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 Token temp = id.token;
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
22 this.id = temp.get;
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
23 this.loc = temp.location;
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
24 if (actual !is null)
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
25 this.actual = this;
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
26 }
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
27
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
28 this(char[] id, DType actual = null)
26
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
29 {
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
30 this.id = id;
27
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
31 if (actual !is null)
26
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
32 this.actual = this;
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
33 }
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
34
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
35 int opEquals(Object o)
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
36 {
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
37 if (auto t = cast(DType)o)
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
38 return this.actual is t.actual;
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
39 return 0;
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
40 }
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
41
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
42 int opCmp(Object o)
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
43 {
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
44 if (auto t = cast(DType)o)
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
45 return cast(void*)this.actual - cast(void*)t.actual;
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
46 return 0;
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
47 }
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
48
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
49 hash_t toHash()
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
50 {
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
51 return cast(hash_t)(cast(void*)this);
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
52 }
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
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; }
27
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
56 LLVM.Type llvm() { return llvmType; }
39
1a7a308f75b2 Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
57 int byteSize() { return 0; }
27
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
58
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
59 static DInteger
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
60 Bool,
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
61 Byte, UByte, Short, UShort,
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
62 Int, UInt, Long, ULong;
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
63
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
64 static DType Void;
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
65
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
66 static this()
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
67 {
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
68 Void = new DType("void");
37
858b9805843d Bug-fixes
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
69 Void.llvmType = LLVM.Type.Void;
26
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
70
27
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
71 Bool = new DInteger("bool", 1, false);
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
72 Byte = new DInteger("byte", 8, false);
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
73 UByte = new DInteger("ubyte", 8, true);
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
74 Short = new DInteger("short", 16, false);
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
75 UShort = new DInteger("ushort", 16, true);
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
76 Int = new DInteger("int", 32, false);
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
77 UInt = new DInteger("uint", 32, true);
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
78 Long = new DInteger("long", 64, false);
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
79 ULong = new DInteger("ulong", 64, true);
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
80 }
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
81 }
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
82
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
83 class DInteger : DType
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 this(char[] name, int bits, bool unsigned)
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
86 {
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
87 super(name, null);
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
88 this.bits = bits;
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
89 this.unsigned = unsigned;
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
90 llvmType = LLVM.IntegerType.Get(bits);
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
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
97 }
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
98
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
99 class DStruct : DType
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
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
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
102 {
27
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
103 super(id, actual);
26
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
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
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
112 LLVM.Type[] types;
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
113
39
1a7a308f75b2 Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
114 foreach (type; members)
1a7a308f75b2 Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
115 {
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
116 types ~= type.llvm;
39
1a7a308f75b2 Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
117 bytes_total += type.byteSize();
1a7a308f75b2 Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
118 }
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
119
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
120 this.llvmType = LLVM.StructType.Get(types);
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
121
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 }
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
123 DType[char[]] members;
39
1a7a308f75b2 Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
124 private int bytes_total;
26
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
125 }
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
126
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
127 class DFunction : DType
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
128 {
27
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
129 this(Identifier id, DType actual = null)
26
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
130 {
27
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
131 super(id, actual);
26
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
132 }
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
133 DType[] params;
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
134 DType return_type;
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
135 }
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents:
diff changeset
136