Skip to content

Commit a57ce45

Browse files
[dtemplate.d] move match to templatesem.d (dlang/dmd!21735)
1 parent 9ba126b commit a57ce45

File tree

2 files changed

+136
-136
lines changed

2 files changed

+136
-136
lines changed

dmd/dtemplate.d

Lines changed: 1 addition & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import dmd.optimize;
7272
import dmd.root.array;
7373
import dmd.common.outbuffer;
7474
import dmd.rootobject;
75-
import dmd.templatesem : getExpression, TemplateInstance_semanticTiargs;
75+
import dmd.templatesem : getExpression, TemplateInstance_semanticTiargs, arrayObjectMatch;
7676
import dmd.tokens;
7777
import dmd.typesem : typeSemantic, isBaseOf, resolveNamedArgs;
7878
import dmd.visitor;
@@ -195,141 +195,6 @@ inout(Type) getType(inout RootObject o)
195195

196196
}
197197

198-
/******************************
199-
* See if two objects match
200-
* Params:
201-
* o1 = first object
202-
* o2 = second object
203-
* Returns: true if they match
204-
*/
205-
bool match(RootObject o1, RootObject o2)
206-
{
207-
enum log = false;
208-
209-
static if (log)
210-
{
211-
printf("match() o1 = %p %s (%d), o2 = %p %s (%d)\n",
212-
o1, o1.toChars(), o1.dyncast(), o2, o2.toChars(), o2.dyncast());
213-
}
214-
215-
bool yes()
216-
{
217-
static if (log)
218-
printf("\t. match\n");
219-
return true;
220-
}
221-
bool no()
222-
{
223-
static if (log)
224-
printf("\t. nomatch\n");
225-
return false;
226-
}
227-
/* A proper implementation of the various equals() overrides
228-
* should make it possible to just do o1.equals(o2), but
229-
* we'll do that another day.
230-
*/
231-
/* Manifest constants should be compared by their values,
232-
* at least in template arguments.
233-
*/
234-
235-
if (auto t1 = isType(o1))
236-
{
237-
auto t2 = isType(o2);
238-
if (!t2)
239-
return no();
240-
241-
static if (log)
242-
{
243-
printf("\tt1 = %s\n", t1.toChars());
244-
printf("\tt2 = %s\n", t2.toChars());
245-
}
246-
if (!t1.equals(t2))
247-
return no();
248-
249-
return yes();
250-
}
251-
if (auto e1 = getExpression(o1))
252-
{
253-
auto e2 = getExpression(o2);
254-
if (!e2)
255-
return no();
256-
257-
static if (log)
258-
{
259-
printf("\te1 = %s '%s' %s\n", e1.type ? e1.type.toChars() : "null", EXPtoString(e1.op).ptr, e1.toChars());
260-
printf("\te2 = %s '%s' %s\n", e2.type ? e2.type.toChars() : "null", EXPtoString(e2.op).ptr, e2.toChars());
261-
}
262-
263-
// two expressions can be equal although they do not have the same
264-
// type; that happens when they have the same value. So check type
265-
// as well as expression equality to ensure templates are properly
266-
// matched.
267-
if (!(e1.type && e2.type && e1.type.equals(e2.type)) || !e1.equals(e2))
268-
return no();
269-
270-
return yes();
271-
}
272-
if (auto s1 = isDsymbol(o1))
273-
{
274-
auto s2 = isDsymbol(o2);
275-
if (!s2)
276-
return no();
277-
278-
static if (log)
279-
{
280-
printf("\ts1 = %s \n", s1.kind(), s1.toChars());
281-
printf("\ts2 = %s \n", s2.kind(), s2.toChars());
282-
}
283-
if (!s1.equals(s2))
284-
return no();
285-
if (s1.parent != s2.parent && !s1.isFuncDeclaration() && !s2.isFuncDeclaration())
286-
return no();
287-
288-
return yes();
289-
}
290-
if (auto u1 = isTuple(o1))
291-
{
292-
auto u2 = isTuple(o2);
293-
if (!u2)
294-
return no();
295-
296-
static if (log)
297-
{
298-
printf("\tu1 = %s\n", u1.toChars());
299-
printf("\tu2 = %s\n", u2.toChars());
300-
}
301-
if (!arrayObjectMatch(u1.objects, u2.objects))
302-
return no();
303-
304-
return yes();
305-
}
306-
return yes();
307-
}
308-
309-
/************************************
310-
* Match an array of them.
311-
*/
312-
bool arrayObjectMatch(ref Objects oa1, ref Objects oa2)
313-
{
314-
if (&oa1 == &oa2)
315-
return true;
316-
if (oa1.length != oa2.length)
317-
return false;
318-
immutable oa1dim = oa1.length;
319-
auto oa1d = oa1[].ptr;
320-
auto oa2d = oa2[].ptr;
321-
foreach (j; 0 .. oa1dim)
322-
{
323-
RootObject o1 = oa1d[j];
324-
RootObject o2 = oa2d[j];
325-
if (!match(o1, o2))
326-
{
327-
return false;
328-
}
329-
}
330-
return true;
331-
}
332-
333198
/************************************
334199
* Return hash of Objects.
335200
*/

dmd/templatesem.d

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,141 @@ void templateDeclarationSemantic(Scope* sc, TemplateDeclaration tempdecl)
207207
tempdecl.semanticRun = PASS.semanticdone;
208208
}
209209

210+
/******************************
211+
* See if two objects match
212+
* Params:
213+
* o1 = first object
214+
* o2 = second object
215+
* Returns: true if they match
216+
*/
217+
bool match(RootObject o1, RootObject o2)
218+
{
219+
enum log = false;
220+
221+
static if (log)
222+
{
223+
printf("match() o1 = %p %s (%d), o2 = %p %s (%d)\n",
224+
o1, o1.toChars(), o1.dyncast(), o2, o2.toChars(), o2.dyncast());
225+
}
226+
227+
bool yes()
228+
{
229+
static if (log)
230+
printf("\t. match\n");
231+
return true;
232+
}
233+
bool no()
234+
{
235+
static if (log)
236+
printf("\t. nomatch\n");
237+
return false;
238+
}
239+
/* A proper implementation of the various equals() overrides
240+
* should make it possible to just do o1.equals(o2), but
241+
* we'll do that another day.
242+
*/
243+
/* Manifest constants should be compared by their values,
244+
* at least in template arguments.
245+
*/
246+
247+
if (auto t1 = isType(o1))
248+
{
249+
auto t2 = isType(o2);
250+
if (!t2)
251+
return no();
252+
253+
static if (log)
254+
{
255+
printf("\tt1 = %s\n", t1.toChars());
256+
printf("\tt2 = %s\n", t2.toChars());
257+
}
258+
if (!t1.equals(t2))
259+
return no();
260+
261+
return yes();
262+
}
263+
if (auto e1 = getExpression(o1))
264+
{
265+
auto e2 = getExpression(o2);
266+
if (!e2)
267+
return no();
268+
269+
static if (log)
270+
{
271+
printf("\te1 = %s '%s' %s\n", e1.type ? e1.type.toChars() : "null", EXPtoString(e1.op).ptr, e1.toChars());
272+
printf("\te2 = %s '%s' %s\n", e2.type ? e2.type.toChars() : "null", EXPtoString(e2.op).ptr, e2.toChars());
273+
}
274+
275+
// two expressions can be equal although they do not have the same
276+
// type; that happens when they have the same value. So check type
277+
// as well as expression equality to ensure templates are properly
278+
// matched.
279+
if (!(e1.type && e2.type && e1.type.equals(e2.type)) || !e1.equals(e2))
280+
return no();
281+
282+
return yes();
283+
}
284+
if (auto s1 = isDsymbol(o1))
285+
{
286+
auto s2 = isDsymbol(o2);
287+
if (!s2)
288+
return no();
289+
290+
static if (log)
291+
{
292+
printf("\ts1 = %s \n", s1.kind(), s1.toChars());
293+
printf("\ts2 = %s \n", s2.kind(), s2.toChars());
294+
}
295+
if (!s1.equals(s2))
296+
return no();
297+
if (s1.parent != s2.parent && !s1.isFuncDeclaration() && !s2.isFuncDeclaration())
298+
return no();
299+
300+
return yes();
301+
}
302+
if (auto u1 = isTuple(o1))
303+
{
304+
auto u2 = isTuple(o2);
305+
if (!u2)
306+
return no();
307+
308+
static if (log)
309+
{
310+
printf("\tu1 = %s\n", u1.toChars());
311+
printf("\tu2 = %s\n", u2.toChars());
312+
}
313+
if (!arrayObjectMatch(u1.objects, u2.objects))
314+
return no();
315+
316+
return yes();
317+
}
318+
return yes();
319+
}
320+
321+
/************************************
322+
* Match an array of them.
323+
*/
324+
bool arrayObjectMatch(ref Objects oa1, ref Objects oa2)
325+
{
326+
if (&oa1 == &oa2)
327+
return true;
328+
if (oa1.length != oa2.length)
329+
return false;
330+
immutable oa1dim = oa1.length;
331+
auto oa1d = oa1[].ptr;
332+
auto oa2d = oa2[].ptr;
333+
foreach (j; 0 .. oa1dim)
334+
{
335+
RootObject o1 = oa1d[j];
336+
RootObject o2 = oa2d[j];
337+
if (!match(o1, o2))
338+
{
339+
return false;
340+
}
341+
}
342+
return true;
343+
}
344+
210345
/*******************************************
211346
* Match to a particular TemplateParameter.
212347
* Input:

0 commit comments

Comments
 (0)