annotate dmd/TemplateInstance.d @ 135:af1bebfd96a4 dmd2037

dmd 2.038
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Mon, 13 Sep 2010 22:19:42 +0100
parents 206db751bd4c
children e7769d53e750
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.TemplateInstance;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 113
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.ScopeDsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.TupleDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.TemplateParameter;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.AliasDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.TemplateDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.TupleExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.WithScopeSymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.Module;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.Global;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.Tuple;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.STC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.TypeTuple;
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
27 import dmd.Parameter;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.ExpInitializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.Array;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 import dmd.DsymbolTable;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 import dmd.VarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 import dmd.FuncExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 import dmd.Declaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 import dmd.MATCH;
131
206db751bd4c dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
40 import dmd.TypeFunction;
206db751bd4c dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
41 import dmd.TemplateTupleParameter;
206db751bd4c dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
42 import dmd.FuncDeclaration;
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
43 import dmd.OverloadSet;
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
44 import dmd.templates.Util;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 import dmd.backend.glue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 Tuple isTuple(Object o)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 //return dynamic_cast<Tuple *>(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 ///if (!o || o.dyncast() != DYNCAST_TUPLE)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 /// return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 return cast(Tuple)o;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 /******************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 * If o1 matches o2, return 1.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 * Else, return 0.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 bool match(Object o1, Object o2, TemplateDeclaration tempdecl, Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 Type t1 = isType(o1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 Type t2 = isType(o2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 Expression e1 = isExpression(o1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 Expression e2 = isExpression(o2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 Dsymbol s1 = isDsymbol(o1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 Dsymbol s2 = isDsymbol(o2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 Tuple v1 = isTuple(o1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 Tuple v2 = isTuple(o2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 //printf("\t match t1 %p t2 %p, e1 %p e2 %p, s1 %p s2 %p, v1 %p v2 %p\n", t1,t2,e1,e2,s1,s2,v1,v2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 /* A proper implementation of the various equals() overrides
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 * should make it possible to just do o1.equals(o2), but
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 * we'll do that another day.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 if (t1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 /* if t1 is an instance of ti, then give error
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 * about recursive expansions.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 Dsymbol s = t1.toDsymbol(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 if (s && s.parent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 TemplateInstance ti1 = s.parent.isTemplateInstance();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 if (ti1 && ti1.tempdecl == tempdecl)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 for (Scope sc1 = sc; sc1; sc1 = sc1.enclosing)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 if (sc1.scopesym == ti1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 error("recursive template expansion for template argument %s", t1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 return true; // fake a match
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 //printf("t1 = %s\n", t1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 //printf("t2 = %s\n", t2.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 if (!t2 || !t1.equals(t2))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 else if (e1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 if (e1 && e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 printf("match %d\n", e1.equals(e2));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 e1.print();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 e2.print();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 e1.type.print();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 e2.type.print();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 if (!e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 if (!e1.equals(e2))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 else if (s1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 //printf("%p %s, %p %s\n", s1, s1.toChars(), s2, s2.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 if (!s2 || !s1.equals(s2) || s1.parent != s2.parent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 VarDeclaration vv1 = s1.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 VarDeclaration vv2 = s2.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 if (vv1 && vv2 && vv1.storage_class & vv2.storage_class & STCmanifest)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 ExpInitializer ei1 = vv1.init.isExpInitializer();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 ExpInitializer ei2 = vv2.init.isExpInitializer();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 if (ei1 && ei2 && !ei1.exp.equals(ei2.exp))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 else if (v1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 if (!v2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 if (v1.objects.dim != v2.objects.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 for (size_t i = 0; i < v1.objects.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 {
113
3482c73a991b More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 110
diff changeset
152 if (!match(v1.objects[i], v2.objects[i], tempdecl, sc))
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 //printf("match\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 return true; // match
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 Lnomatch:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 //printf("nomatch\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 return false; // nomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 class TemplateInstance : ScopeDsymbol
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 /* Given:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 * foo!(args) =>
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 * name = foo
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 * tiargs = args
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 Identifier name;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 //Array idents;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 Objects tiargs; // Array of Types/Expressions of template
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 // instance arguments [int*, char, 10*10]
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 Objects tdtypes; // Array of Types/Expressions corresponding
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 // to TemplateDeclaration.parameters
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 // [int, char, 100]
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 TemplateDeclaration tempdecl; // referenced by foo.bar.abc
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 TemplateInstance inst; // refer to existing instance
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 TemplateInstance tinst; // enclosing template instance
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 ScopeDsymbol argsym; // argument symbol table
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 AliasDeclaration aliasdecl; // !=null if instance is an alias for its
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 // sole member
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 WithScopeSymbol withsym; // if a member of a with statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 int semanticRun; // has semantic() been done?
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 int semantictiargsdone; // has semanticTiargs() been done?
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 int nest; // for recursion detection
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 int havetempdecl; // 1 if used second constructor
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 Dsymbol isnested; // if referencing local symbols, this is the context
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 int errors; // 1 if compiled with errors
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 version (IN_GCC) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 /* On some targets, it is necessary to know whether a symbol
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 will be emitted in the output or not before the symbol
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 is used. This can be different from getModule(). */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 Module objFileModule;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 this(Loc loc, Identifier ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 super(null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 printf("TemplateInstance(this = %p, ident = '%s')\n", this, ident ? ident.toChars() : "null");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 this.loc = loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 this.name = ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 tdtypes = new Objects();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 /*****************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 * This constructor is only called when we figured out which function
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 * template to instantiate.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 this(Loc loc, TemplateDeclaration td, Objects tiargs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 super(null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 printf("TemplateInstance(this = %p, tempdecl = '%s')\n", this, td.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 this.loc = loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225 this.name = td.ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226 this.tiargs = tiargs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 this.tempdecl = td;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 this.semantictiargsdone = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 this.havetempdecl = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 assert(cast(size_t)cast(void*)tempdecl.scope_ > 0x10000);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 tdtypes = new Objects();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236 static Objects arraySyntaxCopy(Objects objs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 {
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
238 Objects a = null;
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
239 if (objs)
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
240 { a = new Objects();
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
241 a.setDim(objs.dim);
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
242 for (size_t i = 0; i < objs.dim; i++)
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
243 {
94
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 91
diff changeset
244 a[i] = objectSyntaxCopy(objs[i]);
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
245 }
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
246 }
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
247 return a;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
250 override Dsymbol syntaxCopy(Dsymbol s)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 {
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
252 TemplateInstance ti;
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
253
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
254 if (s)
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
255 ti = cast(TemplateInstance)s;
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
256 else
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
257 ti = new TemplateInstance(loc, name);
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
258
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
259 ti.tiargs = arraySyntaxCopy(tiargs);
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
260
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
261 ScopeDsymbol.syntaxCopy(ti);
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
262 return ti;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
265 override void semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
266 {
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
267 semantic(sc, null);
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
268 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
269
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
270 void semantic(Scope sc, Expressions fargs)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
271 {
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 if (global.errors)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274 if (!global.gag)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 /* Trying to soldier on rarely generates useful messages
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277 * at this point.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279 fatal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 printf("\n+TemplateInstance.semantic('%s', this=%p)\n", toChars(), this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 if (inst) // if semantic() was already run
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 printf("-TemplateInstance.semantic('%s', this=%p) already run\n", inst.toChars(), inst);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296 // get the enclosing template instance from the scope tinst
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 tinst = sc.tinst;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 if (semanticRun != 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301 error(loc, "recursive template expansion");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302 // inst = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306 semanticRun = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 printf("\tdo semantic\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 if (havetempdecl)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 assert(cast(size_t)cast(void*)tempdecl.scope_ > 0x10000);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 // Deduce tdtypes
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316 tdtypes.setDim(tempdecl.parameters.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317 if (!tempdecl.matchWithInstance(this, tdtypes, 2))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319 error("incompatible arguments for template instantiation");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320 inst = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
323 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
326 /* Run semantic on each argument, place results in tiargs[]
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
327 * (if we havetempdecl, then tiargs is already evaluated)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329 semanticTiargs(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
330
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
331 tempdecl = findTemplateDeclaration(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332 if (tempdecl)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
333 tempdecl = findBestMatch(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 if (!tempdecl || global.errors)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337 inst = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338 //printf("error return %p, %d\n", tempdecl, global.errors);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339 return; // error recovery
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
343 hasNestedArgs(tiargs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
344
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
345 /* See if there is an existing TemplateInstantiation that already
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 * implements the typeargs. If so, just refer to that one instead.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348
128
e6e542f37b94 Some more Array -> Vector conversions
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 126
diff changeset
349 foreach (ti; tempdecl.instances)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
351 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352 printf("\t%s: checking for match with instance %d (%p): '%s'\n", toChars(), i, ti, ti.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
353 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
354 assert(tdtypes.dim == ti.tdtypes.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
355
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
356 // Nesting must match
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
357 if (isnested !is ti.isnested)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
358 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
359 //printf("test2 isnested %s ti.isnested %s\n", isnested ? isnested.toChars() : "", ti.isnested ? ti.isnested.toChars() : "");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
360 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
361 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
362 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
363 if (isnested && sc.parent != ti.parent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
364 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
365 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
366 for (size_t j = 0; j < tdtypes.dim; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
367 {
113
3482c73a991b More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 110
diff changeset
368 Object o1 = tdtypes[j];
3482c73a991b More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 110
diff changeset
369 Object o2 = ti.tdtypes[j];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
370 if (!match(o1, o2, tempdecl, sc))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
371 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
372 goto L1;
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
373 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
374 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
375
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
376 /* Template functions may have different instantiations based on
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
377 * "auto ref" parameters.
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
378 */
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
379 if (fargs)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
380 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
381 FuncDeclaration fd = ti.toAlias().isFuncDeclaration();
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
382 if (fd)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
383 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
384 auto fparameters = fd.getParameters(null);
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
385 size_t nfparams = Parameter.dim(fparameters); // Num function parameters
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
386 for (int i = 0; i < nfparams && i < fargs.dim; i++)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
387 { auto fparam = Parameter.getNth(fparameters, i);
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
388 auto farg = fargs[i];
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
389 if (fparam.storageClass & STCauto) // if "auto ref"
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
390 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
391 if (farg.isLvalue())
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
392 { if (!(fparam.storageClass & STC.STCref))
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
393 goto L1; // auto ref's don't match
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
394 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
395 else
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
396 { if (fparam.storageClass & STC.STCref)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
397 goto L1; // auto ref's don't match
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
398 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
399 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
400 }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
401 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
402 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
403
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
404 // It's a match
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
405 inst = ti;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
406 parent = ti.parent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
407 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
408 printf("\tit's a match with instance %p\n", inst);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
409 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
410 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
411
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
412 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
413 ;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
414 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
415
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
416 /* So, we need to implement 'this' instance.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
417 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
418 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
419 printf("\timplement template instance '%s'\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
420 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
421 uint errorsave = global.errors;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
422 inst = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
423 int tempdecl_instance_idx = tempdecl.instances.dim;
128
e6e542f37b94 Some more Array -> Vector conversions
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 126
diff changeset
424 tempdecl.instances.push(this);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
425 parent = tempdecl.parent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
426 //printf("parent = '%s'\n", parent.kind());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
427
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
428 ident = genIdent(); // need an identifier for name mangling purposes.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
429
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
430 static if (true) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
431 if (isnested)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
432 parent = isnested;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
433 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
434 //printf("parent = '%s'\n", parent.kind());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
435
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
436 // Add 'this' to the enclosing scope's members[] so the semantic routines
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
437 // will get called on the instance members
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
438 static if (true) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
439 int dosemantic3 = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
440 {
74
7e0d548de9e6 Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
441 Dsymbols a;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
442
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
443 Scope scx = sc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
444 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
445 for (scx = sc; scx; scx = scx.enclosing)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
446 if (scx.scopesym)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
447 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
448 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
449
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
450 //if (scx && scx.scopesym) printf("3: scx is %s %s\n", scx.scopesym.kind(), scx.scopesym.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
451 if (scx && scx.scopesym &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
452 scx.scopesym.members && !scx.scopesym.isTemplateMixin()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
453
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
454 /// static if (false) { // removed because it bloated compile times
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
455 /// /* The problem is if A imports B, and B imports A, and both A
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
456 /// * and B instantiate the same template, does the compilation of A
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
457 /// * or the compilation of B do the actual instantiation?
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
458 /// *
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
459 /// * see bugzilla 2500.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
460 /// */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
461 /// && !scx.module.selfImports()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
462 /// }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
463 )
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
464 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
465 //printf("\t1: adding to %s %s\n", scx.scopesym.kind(), scx.scopesym.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
466 a = scx.scopesym.members;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
467 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
468 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
469 { Module m = sc.module_.importedFrom;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
470 //printf("\t2: adding to module %s instead of module %s\n", m.toChars(), sc.module.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
471 a = m.members;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
472 if (m.semanticRun >= 3)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
473 dosemantic3 = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
474 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
475
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
476 for (int i = 0; 1; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
477 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
478 if (i == a.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
479 {
74
7e0d548de9e6 Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
480 a.push(this);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
481 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
482 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
483
74
7e0d548de9e6 Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
484 if (this is a[i]) // if already in Array
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
485 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
486 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
487 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
488 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
489
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
490 // Copy the syntax trees from the TemplateDeclaration
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
491 members = Dsymbol.arraySyntaxCopy(tempdecl.members);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
492
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
493 // Create our own scope for the template parameters
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
494 Scope scope_ = tempdecl.scope_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
495 if (!tempdecl.semanticRun)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
496 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
497 error("template instantiation %s forward references template declaration %s\n", toChars(), tempdecl.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
498 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
499 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
500
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
501 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
502 printf("\tcreate scope for template parameters '%s'\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
503 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
504 argsym = new ScopeDsymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
505 argsym.parent = scope_.parent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
506 scope_ = scope_.push(argsym);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
507 // scope.stc = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
508
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
509 // Declare each template parameter as an alias for the argument type
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
510 Scope paramscope = scope_.push();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
511 paramscope.stc = STCundefined;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
512 declareParameters(paramscope);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
513 paramscope.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
514
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
515 // Add members of template instance to template instance symbol table
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
516 // parent = scope.scopesym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
517 symtab = new DsymbolTable();
13
427f8aa74d28 On the road to make Phobos compilable
korDen
parents: 0
diff changeset
518 bool memnum = false;
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 74
diff changeset
519 foreach(Dsymbol s; members)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
520 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
521 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
522 printf("\t[%d] adding member '%s' %p kind %s to '%s', memnum = %d\n", i, s.toChars(), s, s.kind(), this.toChars(), memnum);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
523 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
524 memnum |= s.addMember(scope_, this, memnum);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
525 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
526
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
527 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
528 printf("adding members done\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
529 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
530
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
531 /* See if there is only one member of template instance, and that
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
532 * member has the same name as the template instance.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
533 * If so, this template instance becomes an alias for that member.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
534 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
535 //printf("members.dim = %d\n", members.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
536 if (members.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
537 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
538 Dsymbol s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
539 if (Dsymbol.oneMembers(members, &s) && s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
540 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
541 //printf("s.kind = '%s'\n", s.kind());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
542 //s.print();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
543 //printf("'%s', '%s'\n", s.ident.toChars(), tempdecl.ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
544 if (s.ident && s.ident.equals(tempdecl.ident))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
545 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
546 //printf("setting aliasdecl\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
547 aliasdecl = new AliasDeclaration(loc, s.ident, s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
548 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
549 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
550 }
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
551
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
552 /* If function template declaration
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
553 */
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
554 if (fargs && aliasdecl)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
555 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
556 FuncDeclaration fd = aliasdecl.toAlias().isFuncDeclaration();
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
557 if (fd)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
558 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
559 /* Transmit fargs to type so that TypeFunction::semantic() can
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
560 * resolve any "auto ref" storage classes.
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
561 */
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
562 auto tf = cast(TypeFunction)fd.type;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
563 if (tf && tf.ty == TY.Tfunction)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
564 tf.fargs = fargs;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
565 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
566 }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
567
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
568 // Do semantic() analysis on template instance members
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
569 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
570 printf("\tdo semantic() on template instance members '%s'\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
571 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
572 Scope sc2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
573 sc2 = scope_.push(this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
574 //printf("isnested = %d, sc.parent = %s\n", isnested, sc.parent.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
575 sc2.parent = /*isnested ? sc.parent :*/ this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
576 sc2.tinst = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
577
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
578 try
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
579 {
79
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
580 // static int nest;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
581 //printf("%d\n", nest);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
582 if (++nest > 500)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
583 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
584 global.gag = 0; // ensure error message gets printed
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
585 error("recursive expansion");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
586 fatal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
587 }
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 74
diff changeset
588 foreach(Dsymbol s; members)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
589 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
590 //printf("\t[%d] semantic on '%s' %p kind %s in '%s'\n", i, s.toChars(), s, s.kind(), this.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
591 //printf("test: isnested = %d, sc2.parent = %s\n", isnested, sc2.parent.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
592 // if (isnested)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
593 // s.parent = sc.parent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
594 //printf("test3: isnested = %d, s.parent = %s\n", isnested, s.parent.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
595 s.semantic(sc2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
596 //printf("test4: isnested = %d, s.parent = %s\n", isnested, s.parent.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
597 sc2.module_.runDeferredSemantic();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
598 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
599 --nest;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
600 }
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
601 catch (Exception e)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
602 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
603 global.gag = 0; // ensure error message gets printed
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
604 error("recursive expansion");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
605 fatal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
606 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
607
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
608 /* If any of the instantiation members didn't get semantic() run
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
609 * on them due to forward references, we cannot run semantic2()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
610 * or semantic3() yet.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
611 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
612 for (size_t i = 0; i < Module.deferred.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
613 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
614 Dsymbol sd = cast(Dsymbol)Module.deferred.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
615
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
616 if (sd.parent is this)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
617 goto Laftersemantic;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
618 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
619
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
620 /* The problem is when to parse the initializer for a variable.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
621 * Perhaps VarDeclaration.semantic() should do it like it does
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
622 * for initializers inside a function.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
623 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
624 // if (sc.parent.isFuncDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
625
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
626 /* BUG 782: this has problems if the classes this depends on
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
627 * are forward referenced. Find a way to defer semantic()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
628 * on this template.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
629 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
630 semantic2(sc2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
631
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
632 if (sc.func || dosemantic3)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
633 {
79
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
634 try
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
635 {
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
636 // static int nest; // TODO:
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
637 if (++nest > 300)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
638 {
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
639 global.gag = 0; // ensure error message gets printed
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
640 error("recursive expansion");
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
641 fatal();
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
642 }
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
643 semantic3(sc2);
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
644 --nest;
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
645 }
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
646 catch (Exception e)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
647 {
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
648 global.gag = 0; // ensure error message gets printed
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
649 error("recursive expansion");
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
650 fatal();
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
651 }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
652 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
653
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
654 Laftersemantic:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
655 sc2.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
656
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
657 scope_.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
658
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
659 // Give additional context info if error occurred during instantiation
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
660 if (global.errors != errorsave)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
661 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
662 error("error instantiating");
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
663 if (tinst)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
664 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
665 tinst.printInstantiationTrace();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
666 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
667 errors = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
668 if (global.gag)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
669 tempdecl.instances.remove(tempdecl_instance_idx);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
670 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
671
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
672 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
673 printf("-TemplateInstance.semantic('%s', this=%p)\n", toChars(), this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
674 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
675 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
676
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
677 override void semantic2(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
678 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
679 if (semanticRun >= 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
680 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
681
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
682 semanticRun = 2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
683 version (LOG) {
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
684 printf("+TemplateInstance.semantic2('%s')\n", toChars());
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
685 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
686
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
687 if (!errors && members)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
688 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
689 sc = tempdecl.scope_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
690 assert(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
691 sc = sc.push(argsym);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
692 sc = sc.push(this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
693 sc.tinst = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
694
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 74
diff changeset
695 foreach(Dsymbol s; members)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
696 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
697 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
698 printf("\tmember '%s', kind = '%s'\n", s.toChars(), s.kind());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
699 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
700 s.semantic2(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
701 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
702
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
703 sc = sc.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
704 sc.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
705 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
706
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
707 version (LOG) {
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
708 printf("-TemplateInstance.semantic2('%s')\n", toChars());
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
709 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
710 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
711
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
712 override void semantic3(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
713 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
714 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
715 printf("TemplateInstance.semantic3('%s'), semanticRun = %d\n", toChars(), semanticRun);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
716 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
717 //if (toChars()[0] == 'D') *(char*)0=0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
718 if (semanticRun >= 3)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
719 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
720 semanticRun = 3;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
721 if (!errors && members)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
722 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
723 sc = tempdecl.scope_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
724 sc = sc.push(argsym);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
725 sc = sc.push(this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
726 sc.tinst = this;
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 74
diff changeset
727 foreach(Dsymbol s; members)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
728 s.semantic3(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
729 sc = sc.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
730 sc.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
731 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
732 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
733
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
734 override void inlineScan()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
735 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
736 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
737 printf("TemplateInstance.inlineScan('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
738 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
739 if (!errors && members)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
740 {
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 74
diff changeset
741 foreach(Dsymbol s; members)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
742 s.inlineScan();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
743 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
744 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
745
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
746 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
747 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
748 int i;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
749
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
750 Identifier id = name;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
751 buf.writestring(id.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
752 buf.writestring("!(");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
753 if (nest)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
754 buf.writestring("...");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
755 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
756 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
757 nest++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
758 Objects args = tiargs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
759 for (i = 0; i < args.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
760 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
761 if (i)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
762 buf.writeByte(',');
113
3482c73a991b More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 110
diff changeset
763 Object oarg = args[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
764 ObjectToCBuffer(buf, hgs, oarg);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
765 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
766 nest--;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
767 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
768 buf.writeByte(')');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
769 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
770
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
771 override Dsymbol toAlias() // resolve real symbol
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
772 {
79
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
773 version (LOG)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
774 {
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
775 writef("TemplateInstance.toAlias()\n");
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
776 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
777 if (!inst)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
778 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
779 error("cannot resolve forward reference");
79
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
780 errors = 1;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
781 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
782 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
783
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
784 if (inst !is this)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
785 return inst.toAlias();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
786
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
787 if (aliasdecl)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
788 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
789 return aliasdecl.toAlias();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
790 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
791
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
792 return inst;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
793 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
794
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
795 override string kind()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
796 {
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
797 return "template instance";
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
798 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
799
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
800 override bool oneMember(Dsymbol* ps)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
801 {
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
802 *ps = null;
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
803 return true;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
804 }
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
805
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
806 /*****************************************************
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
807 * Determine if template instance is really a template function,
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
808 * and that template function needs to infer types from the function
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
809 * arguments.
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
810 */
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
811
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
812 bool needsTypeInference(Scope sc)
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
813 {
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
814 //printf("TemplateInstance::needsTypeInference() %s\n", toChars());
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
815 if (!tempdecl)
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
816 tempdecl = findTemplateDeclaration(sc);
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
817 for (TemplateDeclaration td = tempdecl; td; td = td.overnext)
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
818 {
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
819 /* If any of the overloaded template declarations need inference,
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
820 * then return TRUE
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
821 */
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
822 FuncDeclaration fd;
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
823 if (!td.onemember ||
131
206db751bd4c dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
824 (fd = td.onemember.toAlias().isFuncDeclaration()) is null ||
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
825 fd.type.ty != TY.Tfunction)
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
826 {
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
827 /* Not a template function, therefore type inference is not possible.
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
828 */
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
829 //printf("false\n");
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
830 return false;
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
831 }
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
832
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
833 /* Determine if the instance arguments, tiargs, are all that is necessary
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
834 * to instantiate the template.
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
835 */
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
836 TemplateTupleParameter tp = td.isVariadic();
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
837 //printf("tp = %p, td->parameters->dim = %d, tiargs->dim = %d\n", tp, td->parameters->dim, tiargs->dim);
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
838 TypeFunction fdtype = cast(TypeFunction)fd.type;
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
839 if (Parameter.dim(fdtype.parameters) &&
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
840 (tp || tiargs.dim < td.parameters.dim))
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
841 return true;
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
842 }
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
843 //printf("false\n");
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
844 return false;
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
845 }
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 128
diff changeset
846
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
847 override string toChars()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
848 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
849 scope OutBuffer buf = new OutBuffer();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
850 HdrGenState hgs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
851
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
852 toCBuffer(buf, &hgs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
853 return buf.extractString();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
854 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
855
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
856 override string mangle()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
857 {
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
858 OutBuffer buf = new OutBuffer();
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
859 string id;
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
860
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
861 static if (0) {
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
862 printf("TemplateInstance.mangle() %s", toChars());
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
863 if (parent)
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
864 printf(" parent = %s %s", parent.kind(), parent.toChars());
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
865 printf("\n");
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
866 }
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
867 id = ident ? ident.toChars() : toChars();
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
868 if (!tempdecl)
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
869 error("is not defined");
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
870 else if (tempdecl.parent)
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
871 {
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
872 string p = tempdecl.parent.mangle();
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
873 if (p[0] == '_' && p[1] == 'D')
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
874 p = p[2..$];
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
875 buf.writestring(p);
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
876 }
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
877 buf.printf("%d%s", id.length, id);
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
878 id = buf.toChars();
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
879 buf.data = null;
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
880 //printf("TemplateInstance.mangle() %s = %s\n", toChars(), id);
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
881 return id;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
882 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
883
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
884 /**************************************
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
885 * Given an error instantiating the TemplateInstance,
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
886 * give the nested TemplateInstance instantiations that got
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
887 * us here. Those are a list threaded into the nested scopes.
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
888 */
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
889 void printInstantiationTrace()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
890 {
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
891 if (global.gag)
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
892 return;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
893 /+
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
894 const int max_shown = 6;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
895 const string format = "%s: instantiated from here: %s\n";
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
896
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
897 // determine instantiation depth and number of recursive instantiations
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
898 int n_instantiations = 1;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
899 int n_totalrecursions = 0;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
900 for (TemplateInstance cur = this; cur; cur = cur.tinst)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
901 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
902 ++n_instantiations;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
903 // If two instantiations use the same declaration, they are recursive.
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
904 // (this works even if they are instantiated from different places in the
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
905 // same template).
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
906 // In principle, we could also check for multiple-template recursion, but it's
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
907 // probably not worthwhile.
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
908 if (cur.tinst && cur.tempdecl && cur.tinst.tempdecl
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
909 && cur.tempdecl.loc.equals(cur.tinst.tempdecl.loc))
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
910 ++n_totalrecursions;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
911 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
912
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
913 // show full trace only if it's short or verbose is on
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
914 if (n_instantiations <= max_shown || global.params.verbose)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
915 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
916 for (TemplateInstance cur = this; cur; cur = cur.tinst)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
917 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
918 fprintf(stdmsg, format, cur.loc.toChars(), cur.toChars());
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
919 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
920 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
921 else if (n_instantiations - n_totalrecursions <= max_shown)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
922 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
923 // By collapsing recursive instantiations into a single line,
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
924 // we can stay under the limit.
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
925 int recursionDepth=0;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
926 for (TemplateInstance cur = this; cur; cur = cur.tinst)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
927 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
928 if (cur.tinst && cur.tempdecl && cur.tinst.tempdecl
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
929 && cur.tempdecl.loc.equals(cur.tinst.tempdecl.loc))
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
930 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
931 ++recursionDepth;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
932 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
933 else
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
934 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
935 if (recursionDepth)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
936 fprintf(stdmsg, "%s: %d recursive instantiations from here: %s\n", cur.loc.toChars(), recursionDepth+2, cur.toChars());
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
937 else
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
938 fprintf(stdmsg,format, cur.loc.toChars(), cur.toChars());
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
939 recursionDepth = 0;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
940 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
941 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
942 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
943 else
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
944 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
945 // Even after collapsing the recursions, the depth is too deep.
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
946 // Just display the first few and last few instantiations.
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
947 size_t i = 0;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
948 for (TemplateInstance cur = this; cur; cur = cur.tinst)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
949 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
950 if (i == max_shown / 2)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
951 fprintf(stdmsg," ... (%d instantiations, -v to show) ...\n", n_instantiations - max_shown);
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
952
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
953 if (i < max_shown / 2 ||
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
954 i >= n_instantiations - max_shown + max_shown / 2)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
955 fprintf(stdmsg, format, cur.loc.toChars(), cur.toChars());
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
956 ++i;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
957 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
958 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
959 +/
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
960 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
961
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
962 override void toObjFile(int multiobj) // compile to .obj file
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
963 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
964 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
965 printf("TemplateInstance.toObjFile('%s', this = %p)\n", toChars(), this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
966 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
967 if (!errors && members)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
968 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
969 if (multiobj)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
970 // Append to list of object files to be written later
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
971 obj_append(this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
972 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
973 {
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 74
diff changeset
974 foreach(Dsymbol s; members)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
975 s.toObjFile(multiobj);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
976 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
977 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
978 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
979
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
980 // Internal
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
981 /**********************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
982 * Input:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
983 * flags 1: replace const variables with their initializers
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
984 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
985 static void semanticTiargs(Loc loc, Scope sc, Objects tiargs, int flags)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
986 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
987 // Run semantic on each argument, place results in tiargs[]
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
988 //printf("+TemplateInstance.semanticTiargs() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
989 if (!tiargs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
990 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
991 for (size_t j = 0; j < tiargs.dim; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
992 {
113
3482c73a991b More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 110
diff changeset
993 Object o = tiargs[j];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
994 Type ta = isType(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
995 Expression ea = isExpression(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
996 Dsymbol sa = isDsymbol(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
997
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
998 //printf("1: tiargs.data[%d] = %p, %p, %p, ea=%p, ta=%p\n", j, o, isDsymbol(o), isTuple(o), ea, ta);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
999 if (ta)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1000 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1001 //printf("type %s\n", ta.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1002 // It might really be an Expression or an Alias
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1003 ta.resolve(loc, sc, &ea, &ta, &sa);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1004 if (ea)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1005 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1006 ea = ea.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1007 /* This test is to skip substituting a const var with
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1008 * its initializer. The problem is the initializer won't
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1009 * match with an 'alias' parameter. Instead, do the
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1010 * const substitution in TemplateValueParameter.matchArg().
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1011 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1012 if (ea.op != TOKvar || flags & 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1013 ea = ea.optimize(WANTvalue | WANTinterpret);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1014
94
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 91
diff changeset
1015 tiargs[j] = ea;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1016 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1017 else if (sa)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1018 {
94
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 91
diff changeset
1019 tiargs[j] = sa;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1020 TupleDeclaration d = sa.toAlias().isTupleDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1021 if (d)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1022 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1023 size_t dim = d.objects.dim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1024 tiargs.remove(j);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1025 tiargs.insert(j, d.objects);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1026 j--;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1027 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1028 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1029 else if (ta)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1030 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1031 Ltype:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1032 if (ta.ty == Ttuple)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1033 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1034 // Expand tuple
126
1765f3ef917d ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 121
diff changeset
1035 auto tt = cast(TypeTuple)ta;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1036 size_t dim = tt.arguments.dim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1037 tiargs.remove(j);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1038 if (dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1039 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1040 tiargs.reserve(dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1041 for (size_t i = 0; i < dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1042 {
126
1765f3ef917d ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 121
diff changeset
1043 auto arg = tt.arguments[i];
94
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 91
diff changeset
1044 tiargs.insert(j + i, arg.type);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1045 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1046 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1047 j--;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1048 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1049 else
94
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 91
diff changeset
1050 tiargs[j] = ta;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1051 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1052 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1053 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1054 assert(global.errors);
94
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 91
diff changeset
1055 tiargs[j] = Type.terror;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1056 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1057 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1058 else if (ea)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1059 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1060 if (!ea)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1061 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1062 assert(global.errors);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1063 ea = new IntegerExp(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1064 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1065 assert(ea);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1066 ea = ea.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1067 if (ea.op != TOKvar || flags & 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1068 ea = ea.optimize(WANTvalue | WANTinterpret);
94
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 91
diff changeset
1069 tiargs[j] = ea;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1070 if (ea.op == TOKtype)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1071 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1072 ta = ea.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1073 goto Ltype;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1074 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1075 if (ea.op == TOKtuple)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1076 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1077 // Expand tuple
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
1078 auto te = cast(TupleExp)ea;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1079 size_t dim = te.exps.dim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1080 tiargs.remove(j);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1081 if (dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1082 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1083 tiargs.reserve(dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1084 for (size_t i = 0; i < dim; i++)
94
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 91
diff changeset
1085 tiargs.insert(j + i, te.exps[i]);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1086 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1087 j--;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1088 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1089 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1090 else if (sa)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1091 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1092 TemplateDeclaration td = sa.isTemplateDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1093 if (td && !td.semanticRun && td.literal)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1094 td.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1095 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1096 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1097 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1098 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1099 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1100 //printf("1: tiargs.data[%d] = %p\n", j, tiargs.data[j]);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1101 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1102
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1103 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1104 printf("-TemplateInstance.semanticTiargs('%s', this=%p)\n", toChars(), this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1105 for (size_t j = 0; j < tiargs.dim; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1106 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1107 Object o = cast(Object)tiargs.data[j];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1108 Type ta = isType(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1109 Expression ea = isExpression(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1110 Dsymbol sa = isDsymbol(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1111 Tuple va = isTuple(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1112
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1113 printf("\ttiargs[%d] = ta %p, ea %p, sa %p, va %p\n", j, ta, ea, sa, va);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1114 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1115 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1116 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1117
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1118 void semanticTiargs(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1119 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1120 //printf("+TemplateInstance.semanticTiargs() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1121 if (semantictiargsdone)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1122 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1123
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1124 semantictiargsdone = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1125 semanticTiargs(loc, sc, tiargs, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1126 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1127
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1128 /**********************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1129 * Find template declaration corresponding to template instance.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1130 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1131 TemplateDeclaration findTemplateDeclaration(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1132 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1133 //printf("TemplateInstance.findTemplateDeclaration() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1134 if (!tempdecl)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1135 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1136 /* Given:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1137 * foo!( ... )
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1138 * figure out which TemplateDeclaration foo refers to.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1139 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1140 Dsymbol s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1141 Dsymbol scopesym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1142 int i;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1143
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1144 Identifier id = name;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1145 s = sc.search(loc, id, &scopesym);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1146 if (!s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1147 {
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1148 error("template '%s' is not defined", id.toChars());
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1149 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1150 }
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1151
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1152 /* If an OverloadSet, look for a unique member that is a template declaration
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1153 */
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1154 OverloadSet os = s.isOverloadSet();
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1155 if (os)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1156 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1157 s = null;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1158 foreach (s2; os.a)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1159 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1160 if (s2.isTemplateDeclaration())
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1161 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1162 if (s)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1163 error("ambiguous template declaration %s and %s", s.toPrettyChars(), s2.toPrettyChars());
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1164 s = s2;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1165 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1166 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1167 if (!s)
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1168 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1169 error("template '%s' is not defined", id.toChars());
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1170 return null;
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1171 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1172 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 131
diff changeset
1173
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1174 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1175 printf("It's an instance of '%s' kind '%s'\n", s.toChars(), s.kind());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1176 if (s.parent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1177 printf("s.parent = '%s'\n", s.parent.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1178 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1179 withsym = scopesym.isWithScopeSymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1180
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1181 /* We might have found an alias within a template when
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1182 * we really want the template.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1183 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1184 TemplateInstance ti;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1185 if (s.parent &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1186 (ti = s.parent.isTemplateInstance()) !is null)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1187 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1188 if (
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1189 (ti.name == id ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1190 ti.toAlias().ident == id)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1191 &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1192 ti.tempdecl
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1193 )
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1194 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1195 /* This is so that one can refer to the enclosing
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1196 * template, even if it has the same name as a member
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1197 * of the template, if it has a !(arguments)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1198 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1199 tempdecl = ti.tempdecl;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1200 if (tempdecl.overroot) // if not start of overloaded list of TemplateDeclaration's
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1201 tempdecl = tempdecl.overroot; // then get the start
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1202
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1203 s = tempdecl;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1204 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1205 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1206
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1207 s = s.toAlias();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1208
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1209 /* It should be a TemplateDeclaration, not some other symbol
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1210 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1211 tempdecl = s.isTemplateDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1212 if (!tempdecl)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1213 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1214 if (!s.parent && global.errors)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1215 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1216 if (!s.parent && s.getType())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1217 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1218 Dsymbol s2 = s.getType().toDsymbol(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1219 if (!s2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1220 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1221 error("%s is not a template declaration, it is a %s", id.toChars(), s.kind());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1222 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1223 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1224 s = s2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1225 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1226 debug {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1227 //if (!s.parent) printf("s = %s %s\n", s.kind(), s.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1228 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1229 //assert(s.parent);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1230 TemplateInstance ti2 = s.parent ? s.parent.isTemplateInstance() : null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1231 if (ti2 &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1232 (ti2.name == id ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1233 ti2.toAlias().ident == id)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1234 &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1235 ti2.tempdecl
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1236 )
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1237 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1238 /* This is so that one can refer to the enclosing
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1239 * template, even if it has the same name as a member
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1240 * of the template, if it has a !(arguments)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1241 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1242 tempdecl = ti2.tempdecl;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1243 if (tempdecl.overroot) // if not start of overloaded list of TemplateDeclaration's
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1244 tempdecl = tempdecl.overroot; // then get the start
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1245 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1246 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1247 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1248 error("%s is not a template declaration, it is a %s", id.toChars(), s.kind());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1249 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1250 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1251 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1252 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1253 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1254 assert(tempdecl.isTemplateDeclaration());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1255
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1256 return tempdecl;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1257 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1258
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1259 TemplateDeclaration findBestMatch(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1260 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1261 /* Since there can be multiple TemplateDeclaration's with the same
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1262 * name, look for the best match.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1263 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1264 TemplateDeclaration td_ambig = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1265 TemplateDeclaration td_best = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1266 MATCH m_best = MATCHnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1267 scope Objects dedtypes = new Objects();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1268
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1269 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1270 printf("TemplateInstance.findBestMatch()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1271 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1272 // First look for forward references
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1273 for (TemplateDeclaration td = tempdecl; td; td = td.overnext)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1274 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1275 if (!td.semanticRun)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1276 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1277 if (td.scope_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1278 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1279 // Try to fix forward reference
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1280 td.semantic(td.scope_);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1281 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1282 if (!td.semanticRun)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1283 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1284 error("%s forward references template declaration %s\n", toChars(), td.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1285 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1286 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1287 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1288 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1289
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1290 for (TemplateDeclaration td = tempdecl; td; td = td.overnext)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1291 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1292 MATCH m;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1293
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1294 //if (tiargs.dim) printf("2: tiargs.dim = %d, data[0] = %p\n", tiargs.dim, tiargs.data[0]);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1295
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1296 // If more arguments than parameters,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1297 // then this is no match.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1298 if (td.parameters.dim < tiargs.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1299 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1300 if (!td.isVariadic())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1301 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1302 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1303
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1304 dedtypes.setDim(td.parameters.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1305 dedtypes.zero();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1306 assert(td.semanticRun);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1307 m = td.matchWithInstance(this, dedtypes, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1308 //printf("matchWithInstance = %d\n", m);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1309 if (!m) // no match at all
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1310 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1311
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1312 if (m < m_best)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1313 goto Ltd_best;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1314 if (m > m_best)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1315 goto Ltd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1316
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1317 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1318 // Disambiguate by picking the most specialized TemplateDeclaration
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1319 MATCH c1 = td.leastAsSpecialized(td_best);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1320 MATCH c2 = td_best.leastAsSpecialized(td);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1321 //printf("c1 = %d, c2 = %d\n", c1, c2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1322
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1323 if (c1 > c2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1324 goto Ltd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1325 else if (c1 < c2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1326 goto Ltd_best;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1327 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1328 goto Lambig;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1329 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1330
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1331 Lambig: // td_best and td are ambiguous
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1332 td_ambig = td;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1333 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1334
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1335 Ltd_best: // td_best is the best match so far
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1336 td_ambig = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1337 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1338
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1339 Ltd: // td is the new best match
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1340 td_ambig = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1341 td_best = td;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1342 m_best = m;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1343 tdtypes.setDim(dedtypes.dim);
113
3482c73a991b More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 110
diff changeset
1344 memcpy(tdtypes.ptr, dedtypes.ptr, tdtypes.dim * (void*).sizeof);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1345 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1346 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1347
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1348 if (!td_best)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1349 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1350 if (tempdecl && !tempdecl.overnext)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1351 // Only one template, so we can give better error message
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1352 error("%s does not match template declaration %s", toChars(), tempdecl.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1353 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1354 error("%s does not match any template declaration", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1355 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1356 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1357
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1358 if (td_ambig)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1359 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1360 error("%s matches more than one template declaration, %s and %s",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1361 toChars(), td_best.toChars(), td_ambig.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1362 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1363
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1364 /* The best match is td_best
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1365 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1366 tempdecl = td_best;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1367
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1368 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1369 /* Cast any value arguments to be same type as value parameter
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1370 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1371 for (size_t i = 0; i < tiargs.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1372 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1373 Object o = cast(Object)tiargs.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1374 Expression ea = isExpression(o); // value argument
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1375 TemplateParameter tp = cast(TemplateParameter)tempdecl.parameters.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1376 assert(tp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1377 TemplateValueParameter tvp = tp.isTemplateValueParameter();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1378 if (tvp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1379 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1380 assert(ea);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1381 ea = ea.castTo(tvp.valType);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1382 ea = ea.optimize(WANTvalue | WANTinterpret);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1383 tiargs.data[i] = cast(Object)ea;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1384 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1385 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1386 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1387
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1388 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1389 printf("\tIt's a match with template declaration '%s'\n", tempdecl.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1390 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1391 return tempdecl;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1392 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1393
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1394 /****************************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1395 * Declare parameters of template instance, initialize them with the
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1396 * template instance arguments.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1397 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1398 void declareParameters(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1399 {
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
1400 //printf("TemplateInstance.declareParameters()\n");
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1401 for (int i = 0; i < tdtypes.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1402 {
121
347de076ad34 TemplateParameters -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
1403 auto tp = tempdecl.parameters[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1404 //Object o = cast(Object)tiargs.data[i];
113
3482c73a991b More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 110
diff changeset
1405 Object o = tdtypes[i]; // initializer for tp
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1406
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1407 //printf("\ttdtypes[%d] = %p\n", i, o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1408 tempdecl.declareParameter(sc, tp, o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1409 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1410 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1411
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1412 /*****************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1413 * Determines if a TemplateInstance will need a nested
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1414 * generation of the TemplateDeclaration.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1415 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1416 bool hasNestedArgs(Objects args)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1417 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1418 bool nested = false;
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
1419 //printf("TemplateInstance.hasNestedArgs('%s')\n", tempdecl.ident.toChars());
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1420
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1421 /* A nested instance happens when an argument references a local
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1422 * symbol that is on the stack.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1423 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1424 for (size_t i = 0; i < args.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1425 {
113
3482c73a991b More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 110
diff changeset
1426 Object o = args[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1427 Expression ea = isExpression(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1428 Dsymbol sa = isDsymbol(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1429 Tuple va = isTuple(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1430 if (ea)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1431 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1432 if (ea.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1433 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1434 sa = (cast(VarExp)ea).var;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1435 goto Lsa;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1436 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1437 if (ea.op == TOKfunction)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1438 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1439 sa = (cast(FuncExp)ea).fd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1440 goto Lsa;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1441 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1442 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1443 else if (sa)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1444 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1445 Lsa:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1446 TemplateDeclaration td = sa.isTemplateDeclaration();
96
acd69f84627e further work
Trass3r
parents: 91
diff changeset
1447 Declaration d = sa.isDeclaration();
acd69f84627e further work
Trass3r
parents: 91
diff changeset
1448 if ((td && td.literal) ||
acd69f84627e further work
Trass3r
parents: 91
diff changeset
1449 (d && !d.isDataseg() &&
acd69f84627e further work
Trass3r
parents: 91
diff changeset
1450
acd69f84627e further work
Trass3r
parents: 91
diff changeset
1451 /// version (DMDV2) { // TODO:
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1452 !(d.storage_class & STCmanifest) &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1453 /// }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1454 (!d.isFuncDeclaration() || d.isFuncDeclaration().isNested()) &&
96
acd69f84627e further work
Trass3r
parents: 91
diff changeset
1455 !isTemplateMixin()
acd69f84627e further work
Trass3r
parents: 91
diff changeset
1456 ))
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1457 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1458 // if module level template
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1459 if (tempdecl.toParent().isModule())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1460 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1461 Dsymbol dparent = sa.toParent();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1462 if (!isnested)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1463 isnested = dparent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1464 else if (isnested != dparent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1465 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1466 /* Select the more deeply nested of the two.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1467 * Error if one is not nested inside the other.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1468 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1469 for (Dsymbol p = isnested; p; p = p.parent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1470 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1471 if (p == dparent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1472 goto L1; // isnested is most nested
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1473 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1474 for (Dsymbol p = dparent; p; p = p.parent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1475 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1476 if (p == isnested)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1477 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1478 isnested = dparent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1479 goto L1; // dparent is most nested
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1480 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1481 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1482 error("%s is nested in both %s and %s",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1483 toChars(), isnested.toChars(), dparent.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1484 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1485 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1486 //printf("\tnested inside %s\n", isnested.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1487 nested |= 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1488 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1489 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1490 error("cannot use local '%s' as parameter to non-global template %s", d.toChars(), tempdecl.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1491 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1492 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1493 else if (va)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1494 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1495 nested |= hasNestedArgs(va.objects);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1496 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1497 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1498 return nested;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1499 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1500
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1501 /****************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1502 * This instance needs an identifier for name mangling purposes.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1503 * Create one by taking the template declaration name and adding
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1504 * the type signature for it.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1505 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1506 Identifier genIdent()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1507 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1508 scope OutBuffer buf = new OutBuffer();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1509
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
1510 //printf("TemplateInstance.genIdent('%s')\n", tempdecl.ident.toChars());
96
acd69f84627e further work
Trass3r
parents: 91
diff changeset
1511 string id = tempdecl.ident.toChars();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1512 buf.printf("__T%d%s", id.length, id); ///!
96
acd69f84627e further work
Trass3r
parents: 91
diff changeset
1513 Objects args = tiargs;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1514 for (int i = 0; i < args.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1515 {
113
3482c73a991b More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 110
diff changeset
1516 Object o = args[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1517 Type ta = isType(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1518 Expression ea = isExpression(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1519 Dsymbol sa = isDsymbol(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1520 Tuple va = isTuple(o);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1521 //printf("\to [%d] %p ta %p ea %p sa %p va %p\n", i, o, ta, ea, sa, va);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1522 if (ta)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1523 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1524 buf.writeByte('T');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1525 if (ta.deco)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1526 buf.writestring(ta.deco);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1527 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1528 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1529 debug writef("ta = %d, %s\n", ta.ty, ta.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1530 assert(global.errors);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1531 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1532 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1533 else if (ea)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1534 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1535 Lea:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1536 long v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1537 real r;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1538
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1539 ea = ea.optimize(WANTvalue | WANTinterpret);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1540 if (ea.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1541 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1542 sa = (cast(VarExp)ea).var;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1543 ea = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1544 goto Lsa;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1545 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1546 if (ea.op == TOKfunction)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1547 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1548 sa = (cast(FuncExp)ea).fd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1549 ea = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1550 goto Lsa;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1551 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1552 buf.writeByte('V');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1553 if (ea.op == TOKtuple)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1554 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1555 ea.error("tuple is not a valid template value argument");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1556 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1557 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1558 static if (true) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1559 /* Use deco that matches what it would be for a function parameter
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1560 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1561 buf.writestring(ea.type.deco);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1562 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1563 // Use type of parameter, not type of argument
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1564 TemplateParameter tp = cast(TemplateParameter)tempdecl.parameters.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1565 assert(tp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1566 TemplateValueParameter tvp = tp.isTemplateValueParameter();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1567 assert(tvp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1568 buf.writestring(tvp.valType.deco);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1569 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1570 ea.toMangleBuffer(buf);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1571 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1572 else if (sa)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1573 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1574 Lsa:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1575 buf.writeByte('S');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1576 Declaration d = sa.isDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1577 if (d && (!d.type || !d.type.deco))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1578 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1579 error("forward reference of %s", d.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1580 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1581 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1582 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1583 VarDeclaration v = sa.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1584 if (v && v.storage_class & STCmanifest)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1585 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1586 ExpInitializer ei = v.init.isExpInitializer();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1587 if (ei)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1588 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1589 ea = ei.exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1590 goto Lea;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1591 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1592 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1593 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1594 string p = sa.mangle();
62
6557375aff35 InExp.opId_r
korDen
parents: 23
diff changeset
1595 ///buf.printf("%zu%s", p.length, p);
6557375aff35 InExp.opId_r
korDen
parents: 23
diff changeset
1596 buf.printf("%su%s", p.length, p);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1597 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1598 else if (va)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1599 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1600 assert(i + 1 == args.dim); // must be last one
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1601 args = va.objects;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1602 i = -1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1603 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1604 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1605 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1606 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1607 buf.writeByte('Z');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1608 id = buf.toChars();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1609 buf.data = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1610 //printf("\tgenIdent = %s\n", id);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1611 return new Identifier(id, TOKidentifier);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1612 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1613
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
1614 override TemplateInstance isTemplateInstance() { return this; }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1615
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 62
diff changeset
1616 override AliasDeclaration isAliasDeclaration()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1617 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1618 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1619 }
20
1628b221808d Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
1620 }