Mercurial > projects > ddmd
comparison dmd/TypeIdentifier.d @ 0:10317f0c89a5
Initial commit
author | korDen |
---|---|
date | Sat, 24 Oct 2009 08:42:06 +0400 |
parents | |
children | b7d29f613539 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:10317f0c89a5 |
---|---|
1 module dmd.TypeIdentifier; | |
2 | |
3 import dmd.TypeQualified; | |
4 import dmd.MOD; | |
5 import dmd.Identifier; | |
6 import dmd.IdentifierExp; | |
7 import dmd.DotIdExp; | |
8 import dmd.TypeTypedef; | |
9 import dmd.Loc; | |
10 import dmd.OutBuffer; | |
11 import dmd.HdrGenState; | |
12 import dmd.Expression; | |
13 import dmd.Scope; | |
14 import dmd.Type; | |
15 import dmd.Dsymbol; | |
16 import dmd.MATCH; | |
17 import dmd.ArrayTypes; | |
18 import dmd.TY; | |
19 import dmd.Util; | |
20 | |
21 debug import dmd.Global; | |
22 | |
23 class TypeIdentifier : TypeQualified | |
24 { | |
25 Identifier ident; | |
26 | |
27 this(Loc loc, Identifier ident) | |
28 { | |
29 super(TY.Tident, loc); | |
30 this.ident = ident; | |
31 } | |
32 | |
33 version (DumbClone) { | |
34 } else { | |
35 Type clone() | |
36 { | |
37 assert(false); | |
38 } | |
39 } | |
40 Type syntaxCopy() | |
41 { | |
42 TypeIdentifier t = new TypeIdentifier(loc, ident); | |
43 t.syntaxCopyHelper(this); | |
44 t.mod = mod; | |
45 | |
46 return t; | |
47 } | |
48 | |
49 //char *toChars(); | |
50 | |
51 void toDecoBuffer(OutBuffer buf, int flag) | |
52 { | |
53 Type.toDecoBuffer(buf, flag); | |
54 string name = ident.toChars(); | |
55 buf.printf("%d%s", name.length, name); | |
56 } | |
57 | |
58 void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) | |
59 { | |
60 if (mod != this.mod) | |
61 { | |
62 toCBuffer3(buf, hgs, mod); | |
63 return; | |
64 } | |
65 buf.writestring(this.ident.toChars()); | |
66 toCBuffer2Helper(buf, hgs); | |
67 } | |
68 | |
69 /************************************* | |
70 * Takes an array of Identifiers and figures out if | |
71 * it represents a Type or an Expression. | |
72 * Output: | |
73 * if expression, *pe is set | |
74 * if type, *pt is set | |
75 */ | |
76 void resolve(Loc loc, Scope sc, Expression* pe, Type* pt, Dsymbol* ps) | |
77 { | |
78 Dsymbol scopesym; | |
79 | |
80 //printf("TypeIdentifier::resolve(sc = %p, idents = '%s')\n", sc, toChars()); | |
81 Dsymbol s = sc.search(loc, ident, &scopesym); | |
82 resolveHelper(loc, sc, s, scopesym, pe, pt, ps); | |
83 if (*pt) | |
84 (*pt) = (*pt).addMod(mod); | |
85 } | |
86 | |
87 /***************************************** | |
88 * See if type resolves to a symbol, if so, | |
89 * return that symbol. | |
90 */ | |
91 Dsymbol toDsymbol(Scope sc) | |
92 { | |
93 //printf("TypeIdentifier::toDsymbol('%s')\n", toChars()); | |
94 if (!sc) | |
95 return null; | |
96 //printf("ident = '%s'\n", ident.toChars()); | |
97 | |
98 Dsymbol scopesym; | |
99 Dsymbol s = sc.search(loc, ident, &scopesym); | |
100 if (s) | |
101 { | |
102 for (int i = 0; i < idents.dim; i++) | |
103 { | |
104 Identifier id = cast(Identifier)idents.data[i]; | |
105 s = s.searchX(loc, sc, id); | |
106 if (!s) // failed to find a symbol | |
107 { | |
108 //printf("\tdidn't find a symbol\n"); | |
109 break; | |
110 } | |
111 } | |
112 } | |
113 | |
114 return s; | |
115 } | |
116 | |
117 Type semantic(Loc loc, Scope sc) | |
118 { | |
119 Type t; | |
120 Expression e; | |
121 Dsymbol s; | |
122 | |
123 //printf("TypeIdentifier::semantic(%s)\n", toChars()); | |
124 resolve(loc, sc, &e, &t, &s); | |
125 if (t) | |
126 { | |
127 //printf("\tit's a type %d, %s, %s\n", t.ty, t.toChars(), t.deco); | |
128 | |
129 if (t.ty == TY.Ttypedef) | |
130 { | |
131 TypeTypedef tt = cast(TypeTypedef)t; | |
132 | |
133 if (tt.sym.sem == 1) | |
134 error(loc, "circular reference of typedef %s", tt.toChars()); | |
135 } | |
136 t = t.addMod(mod); | |
137 } | |
138 else | |
139 { | |
140 debug { | |
141 if (!global.gag) { | |
142 writef("1: "); | |
143 } | |
144 } | |
145 if (s) | |
146 { | |
147 s.error(loc, "is used as a type"); | |
148 //halt(); | |
149 } | |
150 else { | |
151 error(loc, "%s is used as a type", toChars()); | |
152 } | |
153 t = tvoid; | |
154 } | |
155 //t.print(); | |
156 return t; | |
157 } | |
158 | |
159 MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes) | |
160 { | |
161 assert(false); | |
162 } | |
163 | |
164 Type reliesOnTident() | |
165 { | |
166 return this; | |
167 } | |
168 | |
169 Expression toExpression() | |
170 { | |
171 Expression e = new IdentifierExp(loc, ident); | |
172 for (int i = 0; i < idents.dim; i++) | |
173 { | |
174 Identifier id = cast(Identifier)idents.data[i]; | |
175 e = new DotIdExp(loc, e, id); | |
176 } | |
177 | |
178 return e; | |
179 } | |
180 } |