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