0
|
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 }
|
72
|
40 override Type syntaxCopy()
|
0
|
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
|
72
|
51 override void toDecoBuffer(OutBuffer buf, int flag)
|
0
|
52 {
|
|
53 Type.toDecoBuffer(buf, flag);
|
|
54 string name = ident.toChars();
|
|
55 buf.printf("%d%s", name.length, name);
|
|
56 }
|
|
57
|
72
|
58 override void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod)
|
0
|
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 */
|
72
|
76 override void resolve(Loc loc, Scope sc, Expression* pe, Type* pt, Dsymbol* ps)
|
0
|
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 */
|
72
|
91 override Dsymbol toDsymbol(Scope sc)
|
0
|
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
|
72
|
117 override Type semantic(Loc loc, Scope sc)
|
0
|
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
|
72
|
159 override MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes)
|
0
|
160 {
|
51
|
161 // Extra check
|
|
162 if (tparam && tparam.ty == Tident)
|
|
163 {
|
|
164 TypeIdentifier tp = cast(TypeIdentifier)tparam;
|
|
165
|
|
166 for (int i = 0; i < idents.dim; i++)
|
|
167 {
|
|
168 Identifier id1 = cast(Identifier)idents.data[i];
|
|
169 Identifier id2 = cast(Identifier)tp.idents.data[i];
|
|
170
|
|
171 if (!id1.equals(id2))
|
|
172 return MATCHnomatch;
|
|
173 }
|
|
174 }
|
|
175 return Type.deduceType(sc, tparam, parameters, dedtypes);
|
0
|
176 }
|
|
177
|
72
|
178 override Type reliesOnTident()
|
0
|
179 {
|
|
180 return this;
|
|
181 }
|
|
182
|
72
|
183 override Expression toExpression()
|
0
|
184 {
|
|
185 Expression e = new IdentifierExp(loc, ident);
|
|
186 for (int i = 0; i < idents.dim; i++)
|
|
187 {
|
|
188 Identifier id = cast(Identifier)idents.data[i];
|
|
189 e = new DotIdExp(loc, e, id);
|
|
190 }
|
|
191
|
|
192 return e;
|
|
193 }
|
72
|
194 }
|