Mercurial > projects > dang
annotate sema/Symbol.d @ 158:57b0b4464a0b
Parsing "new", putting it in AST and performs some tests on it. Eg. if the contructor exists and the params matches.
author | Anders Johnsen <skabet@gmail.com> |
---|---|
date | Tue, 22 Jul 2008 00:33:58 +0200 |
parents | 2be29b296081 |
children | 7982eb63c0eb |
rev | line source |
---|---|
129 | 1 module sema.Symbol; |
2 | |
3 import tango.text.convert.Integer : format; | |
4 import tango.io.Stdout; | |
5 | |
6 import sema.DType; | |
7 | |
136 | 8 import ast.Decl; |
9 | |
129 | 10 /// |
11 class Symbol | |
12 { | |
13 /// Create a root symbol - representing a module | |
14 this() { actual = this; } | |
15 | |
16 /// Get a simple human readable name (bar) | |
17 char[] getName() { return name; } | |
18 | |
19 /// Get a human readable name (foo.bar) | |
20 char[] getFQN() | |
21 { | |
22 char[] prefix; | |
23 if (parent !is null && parent.name !is null) | |
24 prefix ~= parent.getFQN() ~ "."; | |
25 return prefix ~ name; | |
26 } | |
27 | |
28 /// Get a machine readable name (_D3foo3barFZi) | |
29 char[] getMangledFQN() | |
30 { | |
31 char[] n = `_D`; | |
32 Symbol p = parent; | |
33 while (p !is null) { | |
34 n ~= p.internalFQN(); | |
35 p = p.parent; | |
36 } | |
37 n ~= internalFQN(); | |
38 n ~= type.mangle(); | |
39 return n; | |
40 } | |
41 | |
42 /** | |
43 Try to find a contained symbol with the given name - returns null if not | |
44 found | |
45 **/ | |
158
57b0b4464a0b
Parsing "new", putting it in AST and performs some tests on it. Eg. if the contructor exists and the params matches.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
46 Symbol[] findMembers(char[] member) |
129 | 47 { |
158
57b0b4464a0b
Parsing "new", putting it in AST and performs some tests on it. Eg. if the contructor exists and the params matches.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
48 Symbol[] res; |
131
3635fc5428b1
Try and make aliases work
Anders Halager <halager@gmail.com>
parents:
129
diff
changeset
|
49 foreach (possible; actual.contained) |
129 | 50 if (possible.name == member) |
158
57b0b4464a0b
Parsing "new", putting it in AST and performs some tests on it. Eg. if the contructor exists and the params matches.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
51 res ~= possible; |
57b0b4464a0b
Parsing "new", putting it in AST and performs some tests on it. Eg. if the contructor exists and the params matches.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
52 return res; |
129 | 53 } |
54 | |
136 | 55 /** |
56 Get the members of the symbol | |
57 **/ | |
58 Symbol[] getMembers() | |
59 { | |
60 return actual.contained; | |
61 } | |
62 | |
129 | 63 void dump() |
64 { | |
65 Stdout("Symbol: "); | |
66 Symbol p = parent; | |
67 while (p !is null) { | |
68 Stdout.format("{}.", p.name); | |
69 p = p.parent; | |
70 } | |
71 Stdout.formatln("{}", name); | |
72 } | |
73 | |
74 /// Create a member with the given name and type | |
136 | 75 Symbol createMember(char[] member, DType type, Decl decl) |
129 | 76 { |
77 auto res = new Symbol(member, type, this); | |
136 | 78 res.decl = decl; |
131
3635fc5428b1
Try and make aliases work
Anders Halager <halager@gmail.com>
parents:
129
diff
changeset
|
79 actual.contained ~= res; |
129 | 80 return res; |
81 } | |
82 | |
83 /** | |
84 Create an alias of another symbol with the given name. | |
85 | |
86 The target symbol can be a member of another symbol | |
87 **/ | |
136 | 88 Symbol createAlias(char[] aliasedName, Symbol target, Decl decl) |
129 | 89 { |
90 auto res = new Symbol(aliasedName, target, this); | |
136 | 91 res.decl = decl; |
131
3635fc5428b1
Try and make aliases work
Anders Halager <halager@gmail.com>
parents:
129
diff
changeset
|
92 actual.contained ~= res; |
129 | 93 return res; |
94 } | |
95 | |
96 // The type of this symbol | |
97 DType type; | |
136 | 98 // The declaration of this symbol |
99 Decl decl; | |
100 // If the symbol is an alias, this will point to the actual symbol | |
101 Symbol actual; | |
102 // If this symbol is contained within a struct or similar this will point | |
103 // to that symbol | |
104 Symbol parent; | |
129 | 105 |
106 private: | |
107 // Helper for getMangledFQN - gets the FQN without _D and the type | |
108 char[] internalFQN() | |
109 { | |
110 if (actual.name !is null && actual.name.length > 0) | |
111 { | |
112 char[32] len; | |
113 return format(len, actual.name.length) ~ actual.name; | |
114 } | |
115 return ""; | |
116 } | |
117 | |
118 this(char[] name, Symbol actual, Symbol parent) | |
119 { | |
120 this.name = name; | |
121 this.actual = actual; | |
122 this.parent = parent; | |
123 this.type = actual.type; | |
124 } | |
125 | |
126 this(char[] name, DType type, Symbol parent) | |
127 { | |
128 this.name = name; | |
129 this.actual = this; | |
130 this.parent = parent; | |
131 this.type = type; | |
132 } | |
133 | |
134 private: | |
135 char[] name; | |
136 | |
137 // All the symbols contained within this symbol | |
138 Symbol[] contained; | |
139 | |
140 // The module that contains this symbol (root of the parent-chain) | |
141 // DModule mod; | |
142 } | |
143 |