| 1 |
/** |
| 2 |
* Template semantics. |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 6 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/templatesem.d, _templatesem.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_templatesem.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/templatesem.d |
| 10 |
*/ |
| 11 |
|
| 12 |
module dmd.templatesem; |
| 13 |
|
| 14 |
import core.stdc.stdio; |
| 15 |
import core.stdc.string; |
| 16 |
import dmd.aggregate; |
| 17 |
import dmd.aliasthis; |
| 18 |
import dmd.arraytypes; |
| 19 |
import dmd.astenums; |
| 20 |
import dmd.ast_node; |
| 21 |
import dmd.attrib; |
| 22 |
import dmd.dcast; |
| 23 |
import dmd.dclass; |
| 24 |
import dmd.declaration; |
| 25 |
import dmd.dinterpret; |
| 26 |
import dmd.dmodule; |
| 27 |
import dmd.dscope; |
| 28 |
import dmd.dsymbol; |
| 29 |
import dmd.dsymbolsem; |
| 30 |
import dmd.dtemplate; |
| 31 |
import dmd.errors; |
| 32 |
import dmd.errorsink; |
| 33 |
import dmd.expression; |
| 34 |
import dmd.expressionsem; |
| 35 |
import dmd.func; |
| 36 |
import dmd.funcsem; |
| 37 |
import dmd.globals; |
| 38 |
import dmd.hdrgen; |
| 39 |
import dmd.id; |
| 40 |
import dmd.identifier; |
| 41 |
import dmd.impcnvtab; |
| 42 |
import dmd.init; |
| 43 |
import dmd.initsem; |
| 44 |
import dmd.location; |
| 45 |
import dmd.mtype; |
| 46 |
import dmd.opover; |
| 47 |
import dmd.optimize; |
| 48 |
import dmd.root.array; |
| 49 |
import dmd.common.outbuffer; |
| 50 |
import dmd.rootobject; |
| 51 |
import dmd.semantic2; |
| 52 |
import dmd.semantic3; |
| 53 |
import dmd.templateparamsem; |
| 54 |
import dmd.tokens; |
| 55 |
import dmd.typesem; |
| 56 |
import dmd.visitor; |
| 57 |
|
| 58 |
alias funcLeastAsSpecialized = dmd.funcsem.leastAsSpecialized; |
| 59 |
|
| 60 |
/************************************ |
| 61 |
* Perform semantic analysis on template. |
| 62 |
* Params: |
| 63 |
* sc = context |
| 64 |
* tempdecl = template declaration |
| 65 |
*/ |
| 66 |
void templateDeclarationSemantic(Scope* sc, TemplateDeclaration tempdecl) |
| 67 |
{ |
| 68 |
enum log = false; |
| 69 |
static if (log) |
| 70 |
{ |
| 71 |
printf("TemplateDeclaration.dsymbolSemantic(this = %p, id = '%s')\n", this, tempdecl.ident.toChars()); |
| 72 |
printf("sc.stc = %llx\n", sc.stc); |
| 73 |
printf("sc.module = %s\n", sc._module.toChars()); |
| 74 |
} |
| 75 |
if (tempdecl.semanticRun != PASS.initial) |
| 76 |
return; // semantic() already run |
| 77 |
|
| 78 |
if (tempdecl._scope) |
| 79 |
{ |
| 80 |
sc = tempdecl._scope; |
| 81 |
tempdecl._scope = null; |
| 82 |
} |
| 83 |
if (!sc) |
| 84 |
return; |
| 85 |
|
| 86 |
// Remember templates defined in module object that we need to know about |
| 87 |
if (sc._module && sc._module.ident == Id.object) |
| 88 |
{ |
| 89 |
if (tempdecl.ident == Id.RTInfo) |
| 90 |
Type.rtinfo = tempdecl; |
| 91 |
} |
| 92 |
|
| 93 |
/* Remember Scope for later instantiations, but make |
| 94 |
* a copy since attributes can change. |
| 95 |
*/ |
| 96 |
if (!tempdecl._scope) |
| 97 |
{ |
| 98 |
tempdecl._scope = sc.copy(); |
| 99 |
tempdecl._scope.setNoFree(); |
| 100 |
} |
| 101 |
|
| 102 |
tempdecl.semanticRun = PASS.semantic; |
| 103 |
|
| 104 |
tempdecl.parent = sc.parent; |
| 105 |
tempdecl.visibility = sc.visibility; |
| 106 |
tempdecl.userAttribDecl = sc.userAttribDecl; |
| 107 |
tempdecl.cppnamespace = sc.namespace; |
| 108 |
tempdecl.isstatic = tempdecl.toParent().isModule() || (tempdecl._scope.stc & STC.static_); |
| 109 |
tempdecl.deprecated_ = !!(sc.stc & STC.deprecated_); |
| 110 |
|
| 111 |
checkGNUABITag(tempdecl, sc.linkage); |
| 112 |
|
| 113 |
if (!tempdecl.isstatic) |
| 114 |
{ |
| 115 |
if (auto ad = tempdecl.parent.pastMixin().isAggregateDeclaration()) |
| 116 |
ad.makeNested(); |
| 117 |
} |
| 118 |
|
| 119 |
// Set up scope for parameters |
| 120 |
auto paramsym = new ScopeDsymbol(); |
| 121 |
paramsym.parent = tempdecl.parent; |
| 122 |
Scope* paramscope = sc.push(paramsym); |
| 123 |
paramscope.stc = STC.none; |
| 124 |
|
| 125 |
if (global.params.ddoc.doOutput) |
| 126 |
{ |
| 127 |
tempdecl.origParameters = new TemplateParameters(tempdecl.parameters.length); |
| 128 |
for (size_t i = 0; i < tempdecl.parameters.length; i++) |
| 129 |
{ |
| 130 |
TemplateParameter tp = (*tempdecl.parameters)[i]; |
| 131 |
(*tempdecl.origParameters)[i] = tp.syntaxCopy(); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
for (size_t i = 0; i < tempdecl.parameters.length; i++) |
| 136 |
{ |
| 137 |
TemplateParameter tp = (*tempdecl.parameters)[i]; |
| 138 |
if (!tp.declareParameter(paramscope)) |
| 139 |
{ |
| 140 |
error(tp.loc, "parameter `%s` multiply defined", tp.ident.toChars()); |
| 141 |
tempdecl.errors = true; |
| 142 |
} |
| 143 |
if (!tp.tpsemantic(paramscope, tempdecl.parameters)) |
| 144 |
{ |
| 145 |
tempdecl.errors = true; |
| 146 |
} |
| 147 |
if (i + 1 != tempdecl.parameters.length && tp.isTemplateTupleParameter()) |
| 148 |
{ |
| 149 |
.error(tempdecl.loc, "%s `%s` template sequence parameter must be the last one", tempdecl.kind, tempdecl.toPrettyChars); |
| 150 |
tempdecl.errors = true; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/* Calculate TemplateParameter.dependent |
| 155 |
*/ |
| 156 |
auto tparams = TemplateParameters(1); |
| 157 |
for (size_t i = 0; i < tempdecl.parameters.length; i++) |
| 158 |
{ |
| 159 |
TemplateParameter tp = (*tempdecl.parameters)[i]; |
| 160 |
tparams[0] = tp; |
| 161 |
|
| 162 |
for (size_t j = 0; j < tempdecl.parameters.length; j++) |
| 163 |
{ |
| 164 |
// Skip cases like: X(T : T) |
| 165 |
if (i == j) |
| 166 |
continue; |
| 167 |
|
| 168 |
if (TemplateTypeParameter ttp = (*tempdecl.parameters)[j].isTemplateTypeParameter()) |
| 169 |
{ |
| 170 |
if (reliesOnTident(ttp.specType, &tparams)) |
| 171 |
tp.dependent = true; |
| 172 |
} |
| 173 |
else if (TemplateAliasParameter tap = (*tempdecl.parameters)[j].isTemplateAliasParameter()) |
| 174 |
{ |
| 175 |
if (reliesOnTident(tap.specType, &tparams) || |
| 176 |
reliesOnTident(isType(tap.specAlias), &tparams)) |
| 177 |
{ |
| 178 |
tp.dependent = true; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
paramscope.pop(); |
| 185 |
|
| 186 |
// Compute again |
| 187 |
tempdecl.onemember = null; |
| 188 |
if (tempdecl.members) |
| 189 |
{ |
| 190 |
Dsymbol s; |
| 191 |
if (Dsymbol.oneMembers(tempdecl.members, s, tempdecl.ident) && s) |
| 192 |
{ |
| 193 |
tempdecl.onemember = s; |
| 194 |
s.parent = tempdecl; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/* BUG: should check: |
| 199 |
* 1. template functions must not introduce virtual functions, as they |
| 200 |
* cannot be accomodated in the vtbl[] |
| 201 |
* 2. templates cannot introduce non-static data members (i.e. fields) |
| 202 |
* as they would change the instance size of the aggregate. |
| 203 |
*/ |
| 204 |
|
| 205 |
tempdecl.semanticRun = PASS.semanticdone; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
/*************************************** |
| 210 |
* Given that ti is an instance of this TemplateDeclaration, |
| 211 |
* deduce the types of the parameters to this, and store |
| 212 |
* those deduced types in dedtypes[]. |
| 213 |
* Params: |
| 214 |
* sc = context |
| 215 |
* td = template |
| 216 |
* ti = instance of td |
| 217 |
* dedtypes = fill in with deduced types |
| 218 |
* argumentList = arguments to template instance |
| 219 |
* flag = 1 - don't do semantic() because of dummy types |
| 220 |
* 2 - don't change types in matchArg() |
| 221 |
* Returns: match level. |
| 222 |
*/ |
| 223 |
public |
| 224 |
MATCH matchWithInstance(Scope* sc, TemplateDeclaration td, TemplateInstance ti, ref Objects dedtypes, ArgumentList argumentList, int flag) |
| 225 |
{ |
| 226 |
enum LOGM = 0; |
| 227 |
static if (LOGM) |
| 228 |
{ |
| 229 |
printf("\n+TemplateDeclaration.matchWithInstance(td = %s, ti = %s, flag = %d)\n", td.toChars(), ti.toChars(), flag); |
| 230 |
} |
| 231 |
version (none) |
| 232 |
{ |
| 233 |
printf("dedtypes.length = %d, parameters.length = %d\n", dedtypes.length, parameters.length); |
| 234 |
if (ti.tiargs.length) |
| 235 |
printf("ti.tiargs.length = %d, [0] = %p\n", ti.tiargs.length, (*ti.tiargs)[0]); |
| 236 |
} |
| 237 |
MATCH nomatch() |
| 238 |
{ |
| 239 |
static if (LOGM) |
| 240 |
{ |
| 241 |
printf(" no match\n"); |
| 242 |
} |
| 243 |
return MATCH.nomatch; |
| 244 |
} |
| 245 |
MATCH m; |
| 246 |
size_t dedtypes_dim = dedtypes.length; |
| 247 |
|
| 248 |
dedtypes.zero(); |
| 249 |
|
| 250 |
if (td.errors) |
| 251 |
return MATCH.nomatch; |
| 252 |
|
| 253 |
size_t parameters_dim = td.parameters.length; |
| 254 |
const bool variadic = td.isVariadic() !is null; |
| 255 |
|
| 256 |
// If more arguments than parameters, no match |
| 257 |
if (ti.tiargs.length > parameters_dim && !variadic) |
| 258 |
{ |
| 259 |
static if (LOGM) |
| 260 |
{ |
| 261 |
printf(" no match: more arguments than parameters\n"); |
| 262 |
} |
| 263 |
return MATCH.nomatch; |
| 264 |
} |
| 265 |
|
| 266 |
assert(dedtypes_dim == parameters_dim); |
| 267 |
assert(dedtypes_dim >= ti.tiargs.length || variadic); |
| 268 |
|
| 269 |
assert(td._scope); |
| 270 |
|
| 271 |
// Set up scope for template parameters |
| 272 |
Scope* paramscope = createScopeForTemplateParameters(td, ti, sc); |
| 273 |
|
| 274 |
// Attempt type deduction |
| 275 |
m = MATCH.exact; |
| 276 |
for (size_t i = 0; i < dedtypes_dim; i++) |
| 277 |
{ |
| 278 |
MATCH m2; |
| 279 |
TemplateParameter tp = (*td.parameters)[i]; |
| 280 |
Declaration sparam; |
| 281 |
|
| 282 |
//printf("\targument [%d]\n", i); |
| 283 |
static if (LOGM) |
| 284 |
{ |
| 285 |
//printf("\targument [%d] is %s\n", i, oarg ? oarg.toChars() : "null"); |
| 286 |
TemplateTypeParameter ttp = tp.isTemplateTypeParameter(); |
| 287 |
if (ttp) |
| 288 |
printf("\tparameter[%d] is %s : %s\n", i, tp.ident.toChars(), ttp.specType ? ttp.specType.toChars() : ""); |
| 289 |
} |
| 290 |
|
| 291 |
m2 = tp.matchArg(ti.loc, paramscope, ti.tiargs, i, td.parameters, dedtypes, &sparam); |
| 292 |
//printf("\tm2 = %d\n", m2); |
| 293 |
if (m2 == MATCH.nomatch) |
| 294 |
{ |
| 295 |
version (none) |
| 296 |
{ |
| 297 |
printf("\tmatchArg() for parameter %i failed\n", i); |
| 298 |
} |
| 299 |
return nomatch(); |
| 300 |
} |
| 301 |
|
| 302 |
if (m2 < m) |
| 303 |
m = m2; |
| 304 |
|
| 305 |
if (!flag) |
| 306 |
sparam.dsymbolSemantic(paramscope); |
| 307 |
if (!paramscope.insert(sparam)) // TODO: This check can make more early |
| 308 |
{ |
| 309 |
// in TemplateDeclaration.semantic, and |
| 310 |
// then we don't need to make sparam if flags == 0 |
| 311 |
return nomatch(); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
if (!flag) |
| 316 |
{ |
| 317 |
/* Any parameter left without a type gets the type of |
| 318 |
* its corresponding arg |
| 319 |
*/ |
| 320 |
foreach (i, ref dedtype; dedtypes) |
| 321 |
{ |
| 322 |
if (!dedtype) |
| 323 |
{ |
| 324 |
assert(i < ti.tiargs.length); |
| 325 |
dedtype = cast(Type)(*ti.tiargs)[i]; |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
if (m > MATCH.nomatch && td.constraint && !flag) |
| 331 |
{ |
| 332 |
if (ti.hasNestedArgs(ti.tiargs, td.isstatic)) // TODO: should gag error |
| 333 |
ti.parent = ti.enclosing; |
| 334 |
else |
| 335 |
ti.parent = td.parent; |
| 336 |
|
| 337 |
// Similar to doHeaderInstantiation |
| 338 |
FuncDeclaration fd = td.onemember ? td.onemember.isFuncDeclaration() : null; |
| 339 |
if (fd) |
| 340 |
{ |
| 341 |
TypeFunction tf = fd.type.isTypeFunction().syntaxCopy(); |
| 342 |
|
| 343 |
fd = new FuncDeclaration(fd.loc, fd.endloc, fd.ident, fd.storage_class, tf); |
| 344 |
fd.parent = ti; |
| 345 |
fd.inferRetType = true; |
| 346 |
|
| 347 |
// Shouldn't run semantic on default arguments and return type. |
| 348 |
foreach (ref param; *tf.parameterList.parameters) |
| 349 |
param.defaultArg = null; |
| 350 |
|
| 351 |
tf.next = null; |
| 352 |
tf.incomplete = true; |
| 353 |
|
| 354 |
// Resolve parameter types and 'auto ref's. |
| 355 |
tf.inferenceArguments = argumentList; |
| 356 |
const olderrors = global.startGagging(); |
| 357 |
fd.type = tf.typeSemantic(td.loc, paramscope); |
| 358 |
global.endGagging(olderrors); |
| 359 |
if (fd.type.ty != Tfunction) |
| 360 |
return nomatch(); |
| 361 |
fd.originalType = fd.type; // for mangling |
| 362 |
} |
| 363 |
|
| 364 |
// TODO: dedtypes => ti.tiargs ? |
| 365 |
if (!evaluateConstraint(td, ti, sc, paramscope, &dedtypes, fd)) |
| 366 |
return nomatch(); |
| 367 |
} |
| 368 |
|
| 369 |
static if (LOGM) |
| 370 |
{ |
| 371 |
// Print out the results |
| 372 |
printf("--------------------------\n"); |
| 373 |
printf("template %s\n", toChars()); |
| 374 |
printf("instance %s\n", ti.toChars()); |
| 375 |
if (m > MATCH.nomatch) |
| 376 |
{ |
| 377 |
for (size_t i = 0; i < dedtypes_dim; i++) |
| 378 |
{ |
| 379 |
TemplateParameter tp = (*parameters)[i]; |
| 380 |
RootObject oarg; |
| 381 |
printf(" [%d]", i); |
| 382 |
if (i < ti.tiargs.length) |
| 383 |
oarg = (*ti.tiargs)[i]; |
| 384 |
else |
| 385 |
oarg = null; |
| 386 |
tp.print(oarg, (*dedtypes)[i]); |
| 387 |
} |
| 388 |
} |
| 389 |
else |
| 390 |
return nomatch(); |
| 391 |
} |
| 392 |
static if (LOGM) |
| 393 |
{ |
| 394 |
printf(" match = %d\n", m); |
| 395 |
} |
| 396 |
|
| 397 |
paramscope.pop(); |
| 398 |
static if (LOGM) |
| 399 |
{ |
| 400 |
printf("-TemplateDeclaration.matchWithInstance(td = %s, ti = %s) = %d\n", td.toChars(), ti.toChars(), m); |
| 401 |
} |
| 402 |
return m; |
| 403 |
} |
| 404 |
|
| 405 |
/**************************** |
| 406 |
* Check to see if constraint is satisfied. |
| 407 |
*/ |
| 408 |
bool evaluateConstraint(TemplateDeclaration td, TemplateInstance ti, Scope* sc, Scope* paramscope, Objects* dedargs, FuncDeclaration fd) |
| 409 |
{ |
| 410 |
/* Detect recursive attempts to instantiate this template declaration, |
| 411 |
* https://issues.dlang.org/show_bug.cgi?id=4072 |
| 412 |
* void foo(T)(T x) if (is(typeof(foo(x)))) { } |
| 413 |
* static assert(!is(typeof(foo(7)))); |
| 414 |
* Recursive attempts are regarded as a constraint failure. |
| 415 |
*/ |
| 416 |
/* There's a chicken-and-egg problem here. We don't know yet if this template |
| 417 |
* instantiation will be a local one (enclosing is set), and we won't know until |
| 418 |
* after selecting the correct template. Thus, function we're nesting inside |
| 419 |
* is not on the sc scope chain, and this can cause errors in FuncDeclaration.getLevel(). |
| 420 |
* Workaround the problem by setting a flag to relax the checking on frame errors. |
| 421 |
*/ |
| 422 |
|
| 423 |
for (TemplatePrevious* p = td.previous; p; p = p.prev) |
| 424 |
{ |
| 425 |
if (!arrayObjectMatch(*p.dedargs, *dedargs)) |
| 426 |
continue; |
| 427 |
//printf("recursive, no match p.sc=%p %p %s\n", p.sc, this, this.toChars()); |
| 428 |
/* It must be a subscope of p.sc, other scope chains are not recursive |
| 429 |
* instantiations. |
| 430 |
* the chain of enclosing scopes is broken by paramscope (its enclosing |
| 431 |
* scope is _scope, but paramscope.callsc is the instantiating scope). So |
| 432 |
* it's good enough to check the chain of callsc |
| 433 |
*/ |
| 434 |
for (Scope* scx = paramscope.callsc; scx; scx = scx.callsc) |
| 435 |
{ |
| 436 |
// The first scx might be identical for nested eponymeous templates, e.g. |
| 437 |
// template foo() { void foo()() {...} } |
| 438 |
if (scx == p.sc && scx !is paramscope.callsc) |
| 439 |
return false; |
| 440 |
} |
| 441 |
/* BUG: should also check for ref param differences |
| 442 |
*/ |
| 443 |
} |
| 444 |
|
| 445 |
TemplatePrevious pr; |
| 446 |
pr.prev = td.previous; |
| 447 |
pr.sc = paramscope.callsc; |
| 448 |
pr.dedargs = dedargs; |
| 449 |
td.previous = ≺ // add this to threaded list |
| 450 |
|
| 451 |
Scope* scx = paramscope.push(ti); |
| 452 |
scx.parent = ti; |
| 453 |
scx.tinst = null; |
| 454 |
scx.minst = null; |
| 455 |
// Set SCOPE.constraint before declaring function parameters for the static condition |
| 456 |
// (previously, this was immediately before calling evalStaticCondition), so the |
| 457 |
// semantic pass knows not to issue deprecation warnings for these throw-away decls. |
| 458 |
// https://issues.dlang.org/show_bug.cgi?id=21831 |
| 459 |
scx.inTemplateConstraint = true; |
| 460 |
|
| 461 |
assert(!ti.symtab); |
| 462 |
if (fd) |
| 463 |
{ |
| 464 |
/* Declare all the function parameters as variables and add them to the scope |
| 465 |
* Making parameters is similar to FuncDeclaration.semantic3 |
| 466 |
*/ |
| 467 |
auto tf = fd.type.isTypeFunction(); |
| 468 |
|
| 469 |
scx.parent = fd; |
| 470 |
|
| 471 |
Parameters* fparameters = tf.parameterList.parameters; |
| 472 |
const nfparams = tf.parameterList.length; |
| 473 |
foreach (i, fparam; tf.parameterList) |
| 474 |
{ |
| 475 |
fparam.storageClass &= (STC.IOR | STC.lazy_ | STC.final_ | STC.TYPECTOR | STC.nodtor); |
| 476 |
fparam.storageClass |= STC.parameter; |
| 477 |
if (tf.parameterList.varargs == VarArg.typesafe && i + 1 == nfparams) |
| 478 |
{ |
| 479 |
fparam.storageClass |= STC.variadic; |
| 480 |
/* Don't need to set STC.scope_ because this will only |
| 481 |
* be evaluated at compile time |
| 482 |
*/ |
| 483 |
} |
| 484 |
} |
| 485 |
foreach (fparam; *fparameters) |
| 486 |
{ |
| 487 |
if (!fparam.ident) |
| 488 |
continue; |
| 489 |
// don't add it, if it has no name |
| 490 |
auto v = new VarDeclaration(fparam.loc, fparam.type, fparam.ident, null); |
| 491 |
fparam.storageClass |= STC.parameter; |
| 492 |
v.storage_class = fparam.storageClass; |
| 493 |
v.dsymbolSemantic(scx); |
| 494 |
if (!ti.symtab) |
| 495 |
ti.symtab = new DsymbolTable(); |
| 496 |
if (!scx.insert(v)) |
| 497 |
.error(td.loc, "%s `%s` parameter `%s.%s` is already defined", td.kind, td.toPrettyChars, td.toChars(), v.toChars()); |
| 498 |
else |
| 499 |
v.parent = fd; |
| 500 |
} |
| 501 |
if (td.isstatic) |
| 502 |
fd.storage_class |= STC.static_; |
| 503 |
declareThis(fd, scx); |
| 504 |
} |
| 505 |
|
| 506 |
td.lastConstraint = td.constraint.syntaxCopy(); |
| 507 |
td.lastConstraintTiargs = ti.tiargs; |
| 508 |
td.lastConstraintNegs.setDim(0); |
| 509 |
|
| 510 |
import dmd.staticcond; |
| 511 |
|
| 512 |
assert(ti.inst is null); |
| 513 |
ti.inst = ti; // temporary instantiation to enable genIdent() |
| 514 |
bool errors; |
| 515 |
const bool result = evalStaticCondition(scx, td.constraint, td.lastConstraint, errors, &td.lastConstraintNegs); |
| 516 |
if (result || errors) |
| 517 |
{ |
| 518 |
td.lastConstraint = null; |
| 519 |
td.lastConstraintTiargs = null; |
| 520 |
td.lastConstraintNegs.setDim(0); |
| 521 |
} |
| 522 |
ti.inst = null; |
| 523 |
ti.symtab = null; |
| 524 |
scx = scx.pop(); |
| 525 |
td.previous = pr.prev; // unlink from threaded list |
| 526 |
if (errors) |
| 527 |
return false; |
| 528 |
return result; |
| 529 |
} |
| 530 |
|
| 531 |
/******************************************* |
| 532 |
* Append to buf a textual representation of template parameters with their arguments. |
| 533 |
* Params: |
| 534 |
* parameters = the template parameters |
| 535 |
* tiargs = the correspondeing template arguments |
| 536 |
* variadic = if it's a variadic argument list |
| 537 |
* buf = where the text output goes |
| 538 |
*/ |
| 539 |
void formatParamsWithTiargs(ref TemplateParameters parameters, ref Objects tiargs, bool variadic, ref OutBuffer buf) |
| 540 |
{ |
| 541 |
buf.writestring(" with `"); |
| 542 |
|
| 543 |
// write usual arguments line-by-line |
| 544 |
// skips trailing default ones - they are not present in `tiargs` |
| 545 |
const end = parameters.length - (variadic ? 1 : 0); |
| 546 |
size_t i; |
| 547 |
for (; i < tiargs.length && i < end; i++) |
| 548 |
{ |
| 549 |
if (i) |
| 550 |
{ |
| 551 |
buf.writeByte(','); |
| 552 |
buf.writenl(); |
| 553 |
buf.writestring(" "); |
| 554 |
} |
| 555 |
write(buf, parameters[i]); |
| 556 |
buf.writestring(" = "); |
| 557 |
write(buf, tiargs[i]); |
| 558 |
} |
| 559 |
// write remaining variadic arguments on the last line |
| 560 |
if (variadic) |
| 561 |
{ |
| 562 |
if (i) |
| 563 |
{ |
| 564 |
buf.writeByte(','); |
| 565 |
buf.writenl(); |
| 566 |
buf.writestring(" "); |
| 567 |
} |
| 568 |
write(buf, parameters[end]); |
| 569 |
buf.writestring(" = "); |
| 570 |
buf.writeByte('('); |
| 571 |
if (end < tiargs.length) |
| 572 |
{ |
| 573 |
write(buf, tiargs[end]); |
| 574 |
foreach (j; parameters.length .. tiargs.length) |
| 575 |
{ |
| 576 |
buf.writestring(", "); |
| 577 |
write(buf, tiargs[j]); |
| 578 |
} |
| 579 |
} |
| 580 |
buf.writeByte(')'); |
| 581 |
} |
| 582 |
buf.writeByte('`'); |
| 583 |
} |
| 584 |
|
| 585 |
/****************************** |
| 586 |
* Create a scope for the parameters of the TemplateInstance |
| 587 |
* `ti` in the parent scope sc from the ScopeDsymbol paramsym. |
| 588 |
* |
| 589 |
* If paramsym is null a new ScopeDsymbol is used in place of |
| 590 |
* paramsym. |
| 591 |
* Params: |
| 592 |
* td = template that ti is an instance of |
| 593 |
* ti = the TemplateInstance whose parameters to generate the scope for. |
| 594 |
* sc = the parent scope of ti |
| 595 |
* Returns: |
| 596 |
* new scope for the parameters of ti |
| 597 |
*/ |
| 598 |
Scope* createScopeForTemplateParameters(TemplateDeclaration td, TemplateInstance ti, Scope* sc) |
| 599 |
{ |
| 600 |
ScopeDsymbol paramsym = new ScopeDsymbol(); |
| 601 |
paramsym.parent = td._scope.parent; |
| 602 |
Scope* paramscope = td._scope.push(paramsym); |
| 603 |
paramscope.tinst = ti; |
| 604 |
paramscope.minst = sc.minst; |
| 605 |
paramscope.callsc = sc; |
| 606 |
paramscope.stc = STC.none; |
| 607 |
return paramscope; |
| 608 |
} |
| 609 |
|
| 610 |
/******************************************** |
| 611 |
* Determine partial specialization order of `td` vs `td2`. |
| 612 |
* Params: |
| 613 |
* sc = context |
| 614 |
* td = first template |
| 615 |
* td2 = second template |
| 616 |
* argumentList = arguments to template |
| 617 |
* Returns: |
| 618 |
* MATCH - td is at least as specialized as td2 |
| 619 |
* MATCH.nomatch - td2 is more specialized than td |
| 620 |
*/ |
| 621 |
MATCH leastAsSpecialized(Scope* sc, TemplateDeclaration td, TemplateDeclaration td2, ArgumentList argumentList) |
| 622 |
{ |
| 623 |
enum LOG_LEASTAS = 0; |
| 624 |
static if (LOG_LEASTAS) |
| 625 |
{ |
| 626 |
printf("%s.leastAsSpecialized(%s)\n", td.toChars(), td2.toChars()); |
| 627 |
} |
| 628 |
|
| 629 |
/* This works by taking the template parameters to this template |
| 630 |
* declaration and feeding them to td2 as if it were a template |
| 631 |
* instance. |
| 632 |
* If it works, then this template is at least as specialized |
| 633 |
* as td2. |
| 634 |
*/ |
| 635 |
|
| 636 |
// Set type arguments to dummy template instance to be types |
| 637 |
// generated from the parameters to this template declaration |
| 638 |
auto tiargs = new Objects(); |
| 639 |
tiargs.reserve(td.parameters.length); |
| 640 |
foreach (tp; *td.parameters) |
| 641 |
{ |
| 642 |
if (tp.dependent) |
| 643 |
break; |
| 644 |
RootObject p = tp.dummyArg(); |
| 645 |
if (!p) //TemplateTupleParameter |
| 646 |
break; |
| 647 |
|
| 648 |
tiargs.push(p); |
| 649 |
} |
| 650 |
scope TemplateInstance ti = new TemplateInstance(Loc.initial, td.ident, tiargs); // create dummy template instance |
| 651 |
|
| 652 |
// Temporary Array to hold deduced types |
| 653 |
Objects dedtypes = Objects(td2.parameters.length); |
| 654 |
|
| 655 |
// Attempt a type deduction |
| 656 |
MATCH m = matchWithInstance(sc, td2, ti, dedtypes, argumentList, 1); |
| 657 |
if (m > MATCH.nomatch) |
| 658 |
{ |
| 659 |
/* A non-variadic template is more specialized than a |
| 660 |
* variadic one. |
| 661 |
*/ |
| 662 |
TemplateTupleParameter tp = td.isVariadic(); |
| 663 |
if (tp && !tp.dependent && !td2.isVariadic()) |
| 664 |
goto L1; |
| 665 |
|
| 666 |
static if (LOG_LEASTAS) |
| 667 |
{ |
| 668 |
printf(" matches %d, so is least as specialized\n", m); |
| 669 |
} |
| 670 |
return m; |
| 671 |
} |
| 672 |
L1: |
| 673 |
static if (LOG_LEASTAS) |
| 674 |
{ |
| 675 |
printf(" doesn't match, so is not as specialized\n"); |
| 676 |
} |
| 677 |
return MATCH.nomatch; |
| 678 |
} |
| 679 |
|
| 680 |
/************************************************* |
| 681 |
* Match function arguments against a specific template function. |
| 682 |
* |
| 683 |
* Params: |
| 684 |
* td = template declaration for template instance |
| 685 |
* ti = template instance. `ti.tdtypes` will be set to Expression/Type deduced template arguments |
| 686 |
* sc = instantiation scope |
| 687 |
* fd = Partially instantiated function declaration, which is set to an instantiated function declaration |
| 688 |
* tthis = 'this' argument if !NULL |
| 689 |
* argumentList = arguments to function |
| 690 |
* |
| 691 |
* Returns: |
| 692 |
* match pair of initial and inferred template arguments |
| 693 |
*/ |
| 694 |
extern (D) MATCHpair deduceFunctionTemplateMatch(TemplateDeclaration td, TemplateInstance ti, Scope* sc, ref FuncDeclaration fd, Type tthis, ArgumentList argumentList) |
| 695 |
{ |
| 696 |
version (none) |
| 697 |
{ |
| 698 |
printf("\nTemplateDeclaration.deduceFunctionTemplateMatch() %s\n", td.toChars()); |
| 699 |
for (size_t i = 0; i < (fargs ? fargs.length : 0); i++) |
| 700 |
{ |
| 701 |
Expression e = (*fargs)[i]; |
| 702 |
printf("\tfarg[%d] is %s, type is %s\n", cast(int) i, e.toChars(), e.type.toChars()); |
| 703 |
} |
| 704 |
printf("fd = %s\n", fd.toChars()); |
| 705 |
printf("fd.type = %s\n", fd.type.toChars()); |
| 706 |
if (tthis) |
| 707 |
printf("tthis = %s\n", tthis.toChars()); |
| 708 |
} |
| 709 |
|
| 710 |
assert(td._scope); |
| 711 |
|
| 712 |
auto dedargs = new Objects(td.parameters.length); |
| 713 |
dedargs.zero(); |
| 714 |
|
| 715 |
Objects* dedtypes = &ti.tdtypes; // for T:T*, the dedargs is the T*, dedtypes is the T |
| 716 |
dedtypes.setDim(td.parameters.length); |
| 717 |
dedtypes.zero(); |
| 718 |
|
| 719 |
if (td.errors || fd.errors) |
| 720 |
return MATCHpair(MATCH.nomatch, MATCH.nomatch); |
| 721 |
|
| 722 |
// Set up scope for parameters |
| 723 |
Scope* paramscope = createScopeForTemplateParameters(td, ti,sc); |
| 724 |
|
| 725 |
MATCHpair nomatch() |
| 726 |
{ |
| 727 |
paramscope.pop(); |
| 728 |
//printf("\tnomatch\n"); |
| 729 |
return MATCHpair(MATCH.nomatch, MATCH.nomatch); |
| 730 |
} |
| 731 |
|
| 732 |
MATCHpair matcherror() |
| 733 |
{ |
| 734 |
// todo: for the future improvement |
| 735 |
paramscope.pop(); |
| 736 |
//printf("\terror\n"); |
| 737 |
return MATCHpair(MATCH.nomatch, MATCH.nomatch); |
| 738 |
} |
| 739 |
// Mark the parameter scope as deprecated if the templated |
| 740 |
// function is deprecated (since paramscope.enclosing is the |
| 741 |
// calling scope already) |
| 742 |
paramscope.stc |= fd.storage_class & STC.deprecated_; |
| 743 |
|
| 744 |
TemplateTupleParameter tp = td.isVariadic(); |
| 745 |
Tuple declaredTuple = null; |
| 746 |
|
| 747 |
version (none) |
| 748 |
{ |
| 749 |
for (size_t i = 0; i < dedargs.length; i++) |
| 750 |
{ |
| 751 |
printf("\tdedarg[%d] = ", i); |
| 752 |
RootObject oarg = (*dedargs)[i]; |
| 753 |
if (oarg) |
| 754 |
printf("%s", oarg.toChars()); |
| 755 |
printf("\n"); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
size_t ntargs = 0; // array size of tiargs |
| 760 |
size_t inferStart = 0; // index of first template parameter to infer from function argument |
| 761 |
const Loc instLoc = ti.loc; |
| 762 |
MATCH matchTiargs = MATCH.exact; |
| 763 |
|
| 764 |
if (auto tiargs = ti.tiargs) |
| 765 |
{ |
| 766 |
// Set initial template arguments |
| 767 |
ntargs = tiargs.length; |
| 768 |
size_t n = td.parameters.length; |
| 769 |
if (tp) |
| 770 |
n--; |
| 771 |
if (ntargs > n) |
| 772 |
{ |
| 773 |
if (!tp) |
| 774 |
return nomatch(); |
| 775 |
|
| 776 |
/* The extra initial template arguments |
| 777 |
* now form the tuple argument. |
| 778 |
*/ |
| 779 |
auto t = new Tuple(ntargs - n); |
| 780 |
assert(td.parameters.length); |
| 781 |
(*dedargs)[td.parameters.length - 1] = t; |
| 782 |
|
| 783 |
for (size_t i = 0; i < t.objects.length; i++) |
| 784 |
{ |
| 785 |
t.objects[i] = (*tiargs)[n + i]; |
| 786 |
} |
| 787 |
td.declareParameter(paramscope, tp, t); |
| 788 |
declaredTuple = t; |
| 789 |
} |
| 790 |
else |
| 791 |
n = ntargs; |
| 792 |
|
| 793 |
memcpy(dedargs.tdata(), tiargs.tdata(), n * (*dedargs.tdata()).sizeof); |
| 794 |
|
| 795 |
for (size_t i = 0; i < n; i++) |
| 796 |
{ |
| 797 |
assert(i < td.parameters.length); |
| 798 |
Declaration sparam = null; |
| 799 |
MATCH m = (*td.parameters)[i].matchArg(instLoc, paramscope, dedargs, i, td.parameters, *dedtypes, &sparam); |
| 800 |
//printf("\tdeduceType m = %d\n", m); |
| 801 |
if (m == MATCH.nomatch) |
| 802 |
return nomatch(); |
| 803 |
if (m < matchTiargs) |
| 804 |
matchTiargs = m; |
| 805 |
|
| 806 |
sparam.dsymbolSemantic(paramscope); |
| 807 |
if (!paramscope.insert(sparam)) |
| 808 |
return nomatch(); |
| 809 |
} |
| 810 |
if (n < td.parameters.length && !declaredTuple) |
| 811 |
{ |
| 812 |
inferStart = n; |
| 813 |
} |
| 814 |
else |
| 815 |
inferStart = td.parameters.length; |
| 816 |
//printf("tiargs matchTiargs = %d\n", matchTiargs); |
| 817 |
} |
| 818 |
version (none) |
| 819 |
{ |
| 820 |
for (size_t i = 0; i < dedargs.length; i++) |
| 821 |
{ |
| 822 |
printf("\tdedarg[%d] = ", i); |
| 823 |
RootObject oarg = (*dedargs)[i]; |
| 824 |
if (oarg) |
| 825 |
printf("%s", oarg.toChars()); |
| 826 |
printf("\n"); |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
ParameterList fparameters = fd.getParameterList(); // function parameter list |
| 831 |
const nfparams = fparameters.length; // number of function parameters |
| 832 |
|
| 833 |
/* Check for match of function arguments with variadic template |
| 834 |
* parameter, such as: |
| 835 |
* |
| 836 |
* void foo(T, A...)(T t, A a); |
| 837 |
* void main() { foo(1,2,3); } |
| 838 |
*/ |
| 839 |
size_t fptupindex = IDX_NOTFOUND; |
| 840 |
if (tp) // if variadic |
| 841 |
{ |
| 842 |
// TemplateTupleParameter always makes most lesser matching. |
| 843 |
matchTiargs = MATCH.convert; |
| 844 |
|
| 845 |
if (nfparams == 0 && argumentList.length != 0) // if no function parameters |
| 846 |
{ |
| 847 |
if (!declaredTuple) |
| 848 |
{ |
| 849 |
auto t = new Tuple(); |
| 850 |
//printf("t = %p\n", t); |
| 851 |
(*dedargs)[td.parameters.length - 1] = t; |
| 852 |
td.declareParameter(paramscope, tp, t); |
| 853 |
declaredTuple = t; |
| 854 |
} |
| 855 |
} |
| 856 |
else |
| 857 |
{ |
| 858 |
/* Figure out which of the function parameters matches |
| 859 |
* the tuple template parameter. Do this by matching |
| 860 |
* type identifiers. |
| 861 |
* Set the index of this function parameter to fptupindex. |
| 862 |
*/ |
| 863 |
for (fptupindex = 0; fptupindex < nfparams; fptupindex++) |
| 864 |
{ |
| 865 |
auto fparam = (*fparameters.parameters)[fptupindex]; // fparameters[fptupindex] ? |
| 866 |
if (fparam.type.ty != Tident) |
| 867 |
continue; |
| 868 |
TypeIdentifier tid = fparam.type.isTypeIdentifier(); |
| 869 |
if (!tp.ident.equals(tid.ident) || tid.idents.length) |
| 870 |
continue; |
| 871 |
|
| 872 |
if (fparameters.varargs != VarArg.none) // variadic function doesn't |
| 873 |
return nomatch(); // go with variadic template |
| 874 |
|
| 875 |
goto L1; |
| 876 |
} |
| 877 |
fptupindex = IDX_NOTFOUND; |
| 878 |
L1: |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
MATCH match = MATCH.exact; |
| 883 |
if (td.toParent().isModule()) |
| 884 |
tthis = null; |
| 885 |
if (tthis) |
| 886 |
{ |
| 887 |
bool hasttp = false; |
| 888 |
|
| 889 |
// Match 'tthis' to any TemplateThisParameter's |
| 890 |
foreach (param; *td.parameters) |
| 891 |
{ |
| 892 |
if (auto ttp = param.isTemplateThisParameter()) |
| 893 |
{ |
| 894 |
hasttp = true; |
| 895 |
|
| 896 |
Type t = new TypeIdentifier(Loc.initial, ttp.ident); |
| 897 |
MATCH m = deduceType(tthis, paramscope, t, *td.parameters, *dedtypes); |
| 898 |
if (m == MATCH.nomatch) |
| 899 |
return nomatch(); |
| 900 |
if (m < match) |
| 901 |
match = m; // pick worst match |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
// Match attributes of tthis against attributes of fd |
| 906 |
if (fd.type && !fd.isCtorDeclaration() && !(td._scope.stc & STC.static_)) |
| 907 |
{ |
| 908 |
STC stc = td._scope.stc | fd.storage_class2; |
| 909 |
// Propagate parent storage class, https://issues.dlang.org/show_bug.cgi?id=5504 |
| 910 |
Dsymbol p = td.parent; |
| 911 |
while (p.isTemplateDeclaration() || p.isTemplateInstance()) |
| 912 |
p = p.parent; |
| 913 |
AggregateDeclaration ad = p.isAggregateDeclaration(); |
| 914 |
if (ad) |
| 915 |
stc |= ad.storage_class; |
| 916 |
|
| 917 |
ubyte mod = fd.type.mod; |
| 918 |
if (stc & STC.immutable_) |
| 919 |
mod = MODFlags.immutable_; |
| 920 |
else |
| 921 |
{ |
| 922 |
if (stc & (STC.shared_ | STC.synchronized_)) |
| 923 |
mod |= MODFlags.shared_; |
| 924 |
if (stc & STC.const_) |
| 925 |
mod |= MODFlags.const_; |
| 926 |
if (stc & STC.wild) |
| 927 |
mod |= MODFlags.wild; |
| 928 |
} |
| 929 |
|
| 930 |
ubyte thismod = tthis.mod; |
| 931 |
if (hasttp) |
| 932 |
mod = MODmerge(thismod, mod); |
| 933 |
MATCH m = MODmethodConv(thismod, mod); |
| 934 |
if (m == MATCH.nomatch) |
| 935 |
return nomatch(); |
| 936 |
if (m < match) |
| 937 |
match = m; |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
// Loop through the function parameters |
| 942 |
{ |
| 943 |
//printf("%s\n\tnfargs = %d, nfparams = %d, tuple_dim = %d\n", toChars(), nfargs, nfparams, declaredTuple ? declaredTuple.objects.length : 0); |
| 944 |
//printf("\ttp = %p, fptupindex = %d, found = %d, declaredTuple = %s\n", tp, fptupindex, fptupindex != IDX_NOTFOUND, declaredTuple ? declaredTuple.toChars() : NULL); |
| 945 |
enum DEFAULT_ARGI = size_t.max - 10; // pseudo index signifying the parameter is expected to be assigned its default argument |
| 946 |
size_t argi = 0; // current argument index |
| 947 |
size_t argsConsumed = 0; // to ensure no excess arguments |
| 948 |
size_t nfargs2 = argumentList.length; // total number of arguments including applied defaultArgs |
| 949 |
uint inoutMatch = 0; // for debugging only |
| 950 |
Expression[] fargs = argumentList.arguments ? (*argumentList.arguments)[] : null; |
| 951 |
Identifier[] fnames = argumentList.names ? (*argumentList.names)[] : null; |
| 952 |
|
| 953 |
for (size_t parami = 0; parami < nfparams; parami++) |
| 954 |
{ |
| 955 |
Parameter fparam = fparameters[parami]; |
| 956 |
|
| 957 |
// Apply function parameter storage classes to parameter types |
| 958 |
Type prmtype = fparam.type.addStorageClass(fparam.storageClass); |
| 959 |
|
| 960 |
Expression farg; |
| 961 |
Identifier fname = argi < fnames.length ? fnames[argi] : null; |
| 962 |
bool foundName = false; |
| 963 |
if (fparam.ident) |
| 964 |
{ |
| 965 |
foreach (i; 0 .. fnames.length) |
| 966 |
{ |
| 967 |
if (fparam.ident == fnames[i]) |
| 968 |
{ |
| 969 |
argi = i; |
| 970 |
foundName = true; |
| 971 |
} |
| 972 |
} |
| 973 |
} |
| 974 |
if (fname && !foundName) |
| 975 |
{ |
| 976 |
argi = DEFAULT_ARGI; |
| 977 |
} |
| 978 |
|
| 979 |
/* See function parameters which wound up |
| 980 |
* as part of a template tuple parameter. |
| 981 |
*/ |
| 982 |
if (fptupindex != IDX_NOTFOUND && parami == fptupindex && argi != DEFAULT_ARGI) |
| 983 |
{ |
| 984 |
TypeIdentifier tid = prmtype.isTypeIdentifier(); |
| 985 |
assert(tid); |
| 986 |
if (!declaredTuple) |
| 987 |
{ |
| 988 |
/* The types of the function arguments |
| 989 |
* now form the tuple argument. |
| 990 |
*/ |
| 991 |
declaredTuple = new Tuple(); |
| 992 |
(*dedargs)[td.parameters.length - 1] = declaredTuple; |
| 993 |
|
| 994 |
/* Count function parameters with no defaults following a tuple parameter. |
| 995 |
* void foo(U, T...)(int y, T, U, double, int bar = 0) {} // rem == 2 (U, double) |
| 996 |
*/ |
| 997 |
size_t rem = 0; |
| 998 |
foreach (j; parami + 1 .. nfparams) |
| 999 |
{ |
| 1000 |
Parameter p = fparameters[j]; |
| 1001 |
if (p.defaultArg) |
| 1002 |
{ |
| 1003 |
break; |
| 1004 |
} |
| 1005 |
foreach(name; fnames) |
| 1006 |
{ |
| 1007 |
if (p.ident == name) |
| 1008 |
break; |
| 1009 |
} |
| 1010 |
if (!reliesOnTemplateParameters(p.type, (*td.parameters)[inferStart .. td.parameters.length])) |
| 1011 |
{ |
| 1012 |
Type pt = p.type.syntaxCopy().typeSemantic(fd.loc, paramscope); |
| 1013 |
if (auto ptt = pt.isTypeTuple()) |
| 1014 |
rem += ptt.arguments.length; |
| 1015 |
else |
| 1016 |
rem += 1; |
| 1017 |
} |
| 1018 |
else |
| 1019 |
{ |
| 1020 |
++rem; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
if (nfargs2 - argi < rem) |
| 1025 |
return nomatch(); |
| 1026 |
declaredTuple.objects.setDim(nfargs2 - argi - rem); |
| 1027 |
foreach (i; 0 .. declaredTuple.objects.length) |
| 1028 |
{ |
| 1029 |
farg = fargs[argi + i]; |
| 1030 |
|
| 1031 |
// Check invalid arguments to detect errors early. |
| 1032 |
if (farg.op == EXP.error || farg.type.ty == Terror) |
| 1033 |
return nomatch(); |
| 1034 |
|
| 1035 |
if (!fparam.isLazy() && farg.type.ty == Tvoid) |
| 1036 |
return nomatch(); |
| 1037 |
|
| 1038 |
Type tt; |
| 1039 |
MATCH m; |
| 1040 |
if (ubyte wm = deduceWildHelper(farg.type, &tt, tid)) |
| 1041 |
{ |
| 1042 |
inoutMatch |= wm; |
| 1043 |
m = MATCH.constant; |
| 1044 |
} |
| 1045 |
else |
| 1046 |
{ |
| 1047 |
m = deduceTypeHelper(farg.type, tt, tid); |
| 1048 |
} |
| 1049 |
if (m == MATCH.nomatch) |
| 1050 |
return nomatch(); |
| 1051 |
if (m < match) |
| 1052 |
match = m; |
| 1053 |
|
| 1054 |
/* Remove top const for dynamic array types and pointer types |
| 1055 |
*/ |
| 1056 |
if ((tt.ty == Tarray || tt.ty == Tpointer) && !tt.isMutable() && (!(fparam.storageClass & STC.ref_) || (fparam.storageClass & STC.auto_) && !farg.isLvalue())) |
| 1057 |
{ |
| 1058 |
tt = tt.mutableOf(); |
| 1059 |
} |
| 1060 |
declaredTuple.objects[i] = tt; |
| 1061 |
} |
| 1062 |
td.declareParameter(paramscope, tp, declaredTuple); |
| 1063 |
} |
| 1064 |
else |
| 1065 |
{ |
| 1066 |
// https://issues.dlang.org/show_bug.cgi?id=6810 |
| 1067 |
// If declared tuple is not a type tuple, |
| 1068 |
// it cannot be function parameter types. |
| 1069 |
for (size_t i = 0; i < declaredTuple.objects.length; i++) |
| 1070 |
{ |
| 1071 |
if (!isType(declaredTuple.objects[i])) |
| 1072 |
return nomatch(); |
| 1073 |
} |
| 1074 |
} |
| 1075 |
assert(declaredTuple); |
| 1076 |
argi += declaredTuple.objects.length; |
| 1077 |
argsConsumed += declaredTuple.objects.length; |
| 1078 |
continue; |
| 1079 |
} |
| 1080 |
|
| 1081 |
// If parameter type doesn't depend on inferred template parameters, |
| 1082 |
// semantic it to get actual type. |
| 1083 |
if (!reliesOnTemplateParameters(prmtype, (*td.parameters)[inferStart .. td.parameters.length])) |
| 1084 |
{ |
| 1085 |
// should copy prmtype to avoid affecting semantic result |
| 1086 |
prmtype = prmtype.syntaxCopy().typeSemantic(fd.loc, paramscope); |
| 1087 |
|
| 1088 |
if (TypeTuple tt = prmtype.isTypeTuple()) |
| 1089 |
{ |
| 1090 |
const tt_dim = tt.arguments.length; |
| 1091 |
for (size_t j = 0; j < tt_dim; j++, ++argi, ++argsConsumed) |
| 1092 |
{ |
| 1093 |
Parameter p = (*tt.arguments)[j]; |
| 1094 |
if (j == tt_dim - 1 && fparameters.varargs == VarArg.typesafe && |
| 1095 |
parami + 1 == nfparams && argi < fargs.length) |
| 1096 |
{ |
| 1097 |
prmtype = p.type; |
| 1098 |
goto Lvarargs; |
| 1099 |
} |
| 1100 |
if (argi >= fargs.length) |
| 1101 |
{ |
| 1102 |
if (p.defaultArg) |
| 1103 |
continue; |
| 1104 |
|
| 1105 |
// https://issues.dlang.org/show_bug.cgi?id=19888 |
| 1106 |
if (fparam.defaultArg) |
| 1107 |
break; |
| 1108 |
|
| 1109 |
return nomatch(); |
| 1110 |
} |
| 1111 |
farg = fargs[argi]; |
| 1112 |
if (!farg.implicitConvTo(p.type)) |
| 1113 |
return nomatch(); |
| 1114 |
} |
| 1115 |
continue; |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
if (argi >= fargs.length) // if not enough arguments |
| 1120 |
{ |
| 1121 |
if (!fparam.defaultArg) |
| 1122 |
goto Lvarargs; |
| 1123 |
|
| 1124 |
/* https://issues.dlang.org/show_bug.cgi?id=2803 |
| 1125 |
* Before the starting of type deduction from the function |
| 1126 |
* default arguments, set the already deduced parameters into paramscope. |
| 1127 |
* It's necessary to avoid breaking existing acceptable code. Cases: |
| 1128 |
* |
| 1129 |
* 1. Already deduced template parameters can appear in fparam.defaultArg: |
| 1130 |
* auto foo(A, B)(A a, B b = A.stringof); |
| 1131 |
* foo(1); |
| 1132 |
* // at fparam == 'B b = A.string', A is equivalent with the deduced type 'int' |
| 1133 |
* |
| 1134 |
* 2. If prmtype depends on default-specified template parameter, the |
| 1135 |
* default type should be preferred. |
| 1136 |
* auto foo(N = size_t, R)(R r, N start = 0) |
| 1137 |
* foo([1,2,3]); |
| 1138 |
* // at fparam `N start = 0`, N should be 'size_t' before |
| 1139 |
* // the deduction result from fparam.defaultArg. |
| 1140 |
*/ |
| 1141 |
if (argi == fargs.length) |
| 1142 |
{ |
| 1143 |
foreach (ref dedtype; *dedtypes) |
| 1144 |
{ |
| 1145 |
Type at = isType(dedtype); |
| 1146 |
if (at && at.ty == Tnone) |
| 1147 |
{ |
| 1148 |
TypeDeduced xt = cast(TypeDeduced)at; |
| 1149 |
dedtype = xt.tded; // 'unbox' |
| 1150 |
} |
| 1151 |
} |
| 1152 |
for (size_t i = ntargs; i < dedargs.length; i++) |
| 1153 |
{ |
| 1154 |
TemplateParameter tparam = (*td.parameters)[i]; |
| 1155 |
|
| 1156 |
RootObject oarg = (*dedargs)[i]; |
| 1157 |
RootObject oded = (*dedtypes)[i]; |
| 1158 |
if (oarg) |
| 1159 |
continue; |
| 1160 |
|
| 1161 |
if (oded) |
| 1162 |
{ |
| 1163 |
if (tparam.specialization() || !tparam.isTemplateTypeParameter()) |
| 1164 |
{ |
| 1165 |
/* The specialization can work as long as afterwards |
| 1166 |
* the oded == oarg |
| 1167 |
*/ |
| 1168 |
(*dedargs)[i] = oded; |
| 1169 |
MATCH m2 = tparam.matchArg(instLoc, paramscope, dedargs, i, td.parameters, *dedtypes, null); |
| 1170 |
//printf("m2 = %d\n", m2); |
| 1171 |
if (m2 == MATCH.nomatch) |
| 1172 |
return nomatch(); |
| 1173 |
if (m2 < matchTiargs) |
| 1174 |
matchTiargs = m2; // pick worst match |
| 1175 |
if (!(*dedtypes)[i].equals(oded)) |
| 1176 |
.error(td.loc, "%s `%s` specialization not allowed for deduced parameter `%s`", |
| 1177 |
td.kind, td.toPrettyChars, td.kind, td.toPrettyChars, tparam.ident.toChars()); |
| 1178 |
} |
| 1179 |
else |
| 1180 |
{ |
| 1181 |
if (MATCH.convert < matchTiargs) |
| 1182 |
matchTiargs = MATCH.convert; |
| 1183 |
} |
| 1184 |
(*dedargs)[i] = td.declareParameter(paramscope, tparam, oded); |
| 1185 |
} |
| 1186 |
else |
| 1187 |
{ |
| 1188 |
oded = tparam.defaultArg(instLoc, paramscope); |
| 1189 |
if (oded) |
| 1190 |
(*dedargs)[i] = td.declareParameter(paramscope, tparam, oded); |
| 1191 |
} |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
if (argi != DEFAULT_ARGI) |
| 1196 |
nfargs2 = argi + 1; |
| 1197 |
|
| 1198 |
/* If prmtype does not depend on any template parameters: |
| 1199 |
* |
| 1200 |
* auto foo(T)(T v, double x = 0); |
| 1201 |
* foo("str"); |
| 1202 |
* // at fparam == 'double x = 0' |
| 1203 |
* |
| 1204 |
* or, if all template parameters in the prmtype are already deduced: |
| 1205 |
* |
| 1206 |
* auto foo(R)(R range, ElementType!R sum = 0); |
| 1207 |
* foo([1,2,3]); |
| 1208 |
* // at fparam == 'ElementType!R sum = 0' |
| 1209 |
* |
| 1210 |
* Deducing prmtype from fparam.defaultArg is not necessary. |
| 1211 |
*/ |
| 1212 |
if (prmtype.deco || prmtype.syntaxCopy().trySemantic(td.loc, paramscope)) |
| 1213 |
{ |
| 1214 |
if (argi != DEFAULT_ARGI) |
| 1215 |
{ |
| 1216 |
++argi; |
| 1217 |
++argsConsumed; |
| 1218 |
} |
| 1219 |
continue; |
| 1220 |
} |
| 1221 |
|
| 1222 |
// Deduce prmtype from the defaultArg. |
| 1223 |
farg = fparam.defaultArg.syntaxCopy(); |
| 1224 |
farg = farg.expressionSemantic(paramscope); |
| 1225 |
farg = resolveProperties(paramscope, farg); |
| 1226 |
} |
| 1227 |
else |
| 1228 |
{ |
| 1229 |
farg = fargs[argi]; |
| 1230 |
} |
| 1231 |
{ |
| 1232 |
assert(farg); |
| 1233 |
// Check invalid arguments to detect errors early. |
| 1234 |
if (farg.op == EXP.error || farg.type.ty == Terror) |
| 1235 |
return nomatch(); |
| 1236 |
|
| 1237 |
Type att = null; |
| 1238 |
Lretry: |
| 1239 |
version (none) |
| 1240 |
{ |
| 1241 |
printf("\tfarg.type = %s\n", farg.type.toChars()); |
| 1242 |
printf("\tfparam.type = %s\n", prmtype.toChars()); |
| 1243 |
} |
| 1244 |
Type argtype = farg.type; |
| 1245 |
|
| 1246 |
if (!fparam.isLazy() && argtype.ty == Tvoid && farg.op != EXP.function_) |
| 1247 |
return nomatch(); |
| 1248 |
|
| 1249 |
// https://issues.dlang.org/show_bug.cgi?id=12876 |
| 1250 |
// Optimize argument to allow CT-known length matching |
| 1251 |
farg = farg.optimize(WANTvalue, fparam.isReference()); |
| 1252 |
//printf("farg = %s %s\n", farg.type.toChars(), farg.toChars()); |
| 1253 |
|
| 1254 |
RootObject oarg = farg; |
| 1255 |
|
| 1256 |
if (farg.isFuncExp()) |
| 1257 |
{ |
| 1258 |
// When assigning an untyped (void) lambda `x => y` to a `(F)(ref F)` parameter, |
| 1259 |
// we don't want to deduce type void creating a void parameter |
| 1260 |
} |
| 1261 |
else if ((fparam.storageClass & STC.ref_) && (!(fparam.storageClass & STC.auto_) || farg.isLvalue())) |
| 1262 |
{ |
| 1263 |
/* Allow expressions that have CT-known boundaries and type [] to match with [dim] |
| 1264 |
*/ |
| 1265 |
bool inferIndexType = (argtype.ty == Tarray) && (prmtype.ty == Tsarray || prmtype.ty == Taarray); |
| 1266 |
if (auto aaType = prmtype.isTypeAArray()) |
| 1267 |
{ |
| 1268 |
if (auto indexType = aaType.index.isTypeIdentifier()) |
| 1269 |
{ |
| 1270 |
inferIndexType = indexType.idents.length == 0; |
| 1271 |
} |
| 1272 |
} |
| 1273 |
if (inferIndexType) |
| 1274 |
{ |
| 1275 |
if (StringExp se = farg.isStringExp()) |
| 1276 |
{ |
| 1277 |
argtype = se.type.nextOf().sarrayOf(se.len); |
| 1278 |
} |
| 1279 |
else if (ArrayLiteralExp ae = farg.isArrayLiteralExp()) |
| 1280 |
{ |
| 1281 |
argtype = ae.type.nextOf().sarrayOf(ae.elements.length); |
| 1282 |
} |
| 1283 |
else if (SliceExp se = farg.isSliceExp()) |
| 1284 |
{ |
| 1285 |
if (Type tsa = toStaticArrayType(se)) |
| 1286 |
argtype = tsa; |
| 1287 |
} |
| 1288 |
} |
| 1289 |
|
| 1290 |
oarg = argtype; |
| 1291 |
} |
| 1292 |
else if ((fparam.storageClass & STC.out_) == 0 && |
| 1293 |
(argtype.ty == Tarray || argtype.ty == Tpointer) && |
| 1294 |
templateParameterLookup(prmtype, td.parameters) != IDX_NOTFOUND && |
| 1295 |
prmtype.isTypeIdentifier().idents.length == 0) |
| 1296 |
{ |
| 1297 |
/* The farg passing to the prmtype always make a copy. Therefore, |
| 1298 |
* we can shrink the set of the deduced type arguments for prmtype |
| 1299 |
* by adjusting top-qualifier of the argtype. |
| 1300 |
* |
| 1301 |
* prmtype argtype ta |
| 1302 |
* T <- const(E)[] const(E)[] |
| 1303 |
* T <- const(E[]) const(E)[] |
| 1304 |
* qualifier(T) <- const(E)[] const(E[]) |
| 1305 |
* qualifier(T) <- const(E[]) const(E[]) |
| 1306 |
*/ |
| 1307 |
Type ta = argtype.castMod(prmtype.mod ? argtype.nextOf().mod : 0); |
| 1308 |
if (ta != argtype) |
| 1309 |
{ |
| 1310 |
Expression ea = farg.copy(); |
| 1311 |
ea.type = ta; |
| 1312 |
oarg = ea; |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
if (fparameters.varargs == VarArg.typesafe && parami + 1 == nfparams && argi + 1 < fargs.length) |
| 1317 |
goto Lvarargs; |
| 1318 |
|
| 1319 |
uint im = 0; |
| 1320 |
MATCH m = deduceType(oarg, paramscope, prmtype, *td.parameters, *dedtypes, &im, inferStart); |
| 1321 |
//printf("\tL%d deduceType m = %d, im = x%x, inoutMatch = x%x\n", __LINE__, m, im, inoutMatch); |
| 1322 |
inoutMatch |= im; |
| 1323 |
|
| 1324 |
/* If no match, see if the argument can be matched by using |
| 1325 |
* implicit conversions. |
| 1326 |
*/ |
| 1327 |
if (m == MATCH.nomatch && prmtype.deco) |
| 1328 |
m = farg.implicitConvTo(prmtype); |
| 1329 |
|
| 1330 |
if (m == MATCH.nomatch) |
| 1331 |
{ |
| 1332 |
AggregateDeclaration ad = isAggregate(farg.type); |
| 1333 |
if (ad && ad.aliasthis && !isRecursiveAliasThis(att, argtype)) |
| 1334 |
{ |
| 1335 |
// https://issues.dlang.org/show_bug.cgi?id=12537 |
| 1336 |
// The isRecursiveAliasThis() call above |
| 1337 |
|
| 1338 |
/* If a semantic error occurs while doing alias this, |
| 1339 |
* eg purity(https://issues.dlang.org/show_bug.cgi?id=7295), |
| 1340 |
* just regard it as not a match. |
| 1341 |
* |
| 1342 |
* We also save/restore sc.func.flags to avoid messing up |
| 1343 |
* attribute inference in the evaluation. |
| 1344 |
*/ |
| 1345 |
const oldflags = sc.func ? sc.func.saveFlags : 0; |
| 1346 |
auto e = resolveAliasThis(sc, farg, true); |
| 1347 |
if (sc.func) |
| 1348 |
sc.func.restoreFlags(oldflags); |
| 1349 |
if (e) |
| 1350 |
{ |
| 1351 |
farg = e; |
| 1352 |
goto Lretry; |
| 1353 |
} |
| 1354 |
} |
| 1355 |
} |
| 1356 |
|
| 1357 |
if (m > MATCH.nomatch && (fparam.storageClass & (STC.ref_ | STC.auto_)) == STC.ref_) |
| 1358 |
{ |
| 1359 |
if (!farg.isLvalue()) |
| 1360 |
{ |
| 1361 |
if ((farg.op == EXP.string_ || farg.op == EXP.slice) && (prmtype.ty == Tsarray || prmtype.ty == Taarray)) |
| 1362 |
{ |
| 1363 |
// Allow conversion from T[lwr .. upr] to ref T[upr-lwr] |
| 1364 |
} |
| 1365 |
else if (sc.previews.rvalueRefParam) |
| 1366 |
{ |
| 1367 |
// Allow implicit conversion to ref |
| 1368 |
} |
| 1369 |
else |
| 1370 |
return nomatch(); |
| 1371 |
} |
| 1372 |
} |
| 1373 |
if (m > MATCH.nomatch && (fparam.storageClass & STC.out_)) |
| 1374 |
{ |
| 1375 |
if (!farg.isLvalue()) |
| 1376 |
return nomatch(); |
| 1377 |
if (!farg.type.isMutable()) // https://issues.dlang.org/show_bug.cgi?id=11916 |
| 1378 |
return nomatch(); |
| 1379 |
} |
| 1380 |
if (m == MATCH.nomatch && fparam.isLazy() && prmtype.ty == Tvoid && farg.type.ty != Tvoid) |
| 1381 |
m = MATCH.convert; |
| 1382 |
if (m != MATCH.nomatch) |
| 1383 |
{ |
| 1384 |
if (m < match) |
| 1385 |
match = m; // pick worst match |
| 1386 |
if (argi != DEFAULT_ARGI) |
| 1387 |
{ |
| 1388 |
argi++; |
| 1389 |
argsConsumed++; |
| 1390 |
} |
| 1391 |
continue; |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
Lvarargs: |
| 1396 |
/* The following code for variadic arguments closely |
| 1397 |
* matches TypeFunction.callMatch() |
| 1398 |
*/ |
| 1399 |
if (!(fparameters.varargs == VarArg.typesafe && parami + 1 == nfparams)) |
| 1400 |
return nomatch(); |
| 1401 |
|
| 1402 |
/* Check for match with function parameter T... |
| 1403 |
*/ |
| 1404 |
Type tb = prmtype.toBasetype(); |
| 1405 |
switch (tb.ty) |
| 1406 |
{ |
| 1407 |
// 6764 fix - TypeAArray may be TypeSArray have not yet run semantic(). |
| 1408 |
case Tsarray: |
| 1409 |
case Taarray: |
| 1410 |
{ |
| 1411 |
// Perhaps we can do better with this, see TypeFunction.callMatch() |
| 1412 |
if (TypeSArray tsa = tb.isTypeSArray()) |
| 1413 |
{ |
| 1414 |
dinteger_t sz = tsa.dim.toInteger(); |
| 1415 |
if (sz != fargs.length - argi) |
| 1416 |
return nomatch(); |
| 1417 |
} |
| 1418 |
else if (TypeAArray taa = tb.isTypeAArray()) |
| 1419 |
{ |
| 1420 |
Expression dim = new IntegerExp(instLoc, fargs.length - argi, Type.tsize_t); |
| 1421 |
|
| 1422 |
size_t i = templateParameterLookup(taa.index, td.parameters); |
| 1423 |
if (i == IDX_NOTFOUND) |
| 1424 |
{ |
| 1425 |
Expression e; |
| 1426 |
Type t; |
| 1427 |
Dsymbol s; |
| 1428 |
Scope* sco; |
| 1429 |
|
| 1430 |
const errors = global.startGagging(); |
| 1431 |
/* ref: https://issues.dlang.org/show_bug.cgi?id=11118 |
| 1432 |
* The parameter isn't part of the template |
| 1433 |
* ones, let's try to find it in the |
| 1434 |
* instantiation scope 'sc' and the one |
| 1435 |
* belonging to the template itself. */ |
| 1436 |
sco = sc; |
| 1437 |
taa.index.resolve(instLoc, sco, e, t, s); |
| 1438 |
if (!e) |
| 1439 |
{ |
| 1440 |
sco = paramscope; |
| 1441 |
taa.index.resolve(instLoc, sco, e, t, s); |
| 1442 |
} |
| 1443 |
global.endGagging(errors); |
| 1444 |
|
| 1445 |
if (!e) |
| 1446 |
return nomatch(); |
| 1447 |
|
| 1448 |
e = e.ctfeInterpret(); |
| 1449 |
e = e.implicitCastTo(sco, Type.tsize_t); |
| 1450 |
e = e.optimize(WANTvalue); |
| 1451 |
if (!dim.equals(e)) |
| 1452 |
return nomatch(); |
| 1453 |
} |
| 1454 |
else |
| 1455 |
{ |
| 1456 |
// This code matches code in TypeInstance.deduceType() |
| 1457 |
TemplateParameter tprm = (*td.parameters)[i]; |
| 1458 |
TemplateValueParameter tvp = tprm.isTemplateValueParameter(); |
| 1459 |
if (!tvp) |
| 1460 |
return nomatch(); |
| 1461 |
Expression e = cast(Expression)(*dedtypes)[i]; |
| 1462 |
if (e) |
| 1463 |
{ |
| 1464 |
if (!dim.equals(e)) |
| 1465 |
return nomatch(); |
| 1466 |
} |
| 1467 |
else |
| 1468 |
{ |
| 1469 |
Type vt = tvp.valType.typeSemantic(Loc.initial, sc); |
| 1470 |
MATCH m = dim.implicitConvTo(vt); |
| 1471 |
if (m == MATCH.nomatch) |
| 1472 |
return nomatch(); |
| 1473 |
(*dedtypes)[i] = dim; |
| 1474 |
} |
| 1475 |
} |
| 1476 |
} |
| 1477 |
goto case Tarray; |
| 1478 |
} |
| 1479 |
case Tarray: |
| 1480 |
{ |
| 1481 |
TypeArray ta = cast(TypeArray)tb; |
| 1482 |
Type tret = fparam.isLazyArray(); |
| 1483 |
for (; argi < fargs.length; argi++) |
| 1484 |
{ |
| 1485 |
Expression arg = fargs[argi]; |
| 1486 |
assert(arg); |
| 1487 |
|
| 1488 |
MATCH m; |
| 1489 |
/* If lazy array of delegates, |
| 1490 |
* convert arg(s) to delegate(s) |
| 1491 |
*/ |
| 1492 |
if (tret) |
| 1493 |
{ |
| 1494 |
if (ta.next.equals(arg.type)) |
| 1495 |
{ |
| 1496 |
m = MATCH.exact; |
| 1497 |
} |
| 1498 |
else |
| 1499 |
{ |
| 1500 |
m = arg.implicitConvTo(tret); |
| 1501 |
if (m == MATCH.nomatch) |
| 1502 |
{ |
| 1503 |
if (tret.toBasetype().ty == Tvoid) |
| 1504 |
m = MATCH.convert; |
| 1505 |
} |
| 1506 |
} |
| 1507 |
} |
| 1508 |
else |
| 1509 |
{ |
| 1510 |
uint wm = 0; |
| 1511 |
m = deduceType(arg, paramscope, ta.next, *td.parameters, *dedtypes, &wm, inferStart); |
| 1512 |
inoutMatch |= wm; |
| 1513 |
} |
| 1514 |
if (m == MATCH.nomatch) |
| 1515 |
return nomatch(); |
| 1516 |
if (m < match) |
| 1517 |
match = m; |
| 1518 |
} |
| 1519 |
goto Lmatch; |
| 1520 |
} |
| 1521 |
case Tclass: |
| 1522 |
case Tident: |
| 1523 |
goto Lmatch; |
| 1524 |
|
| 1525 |
default: |
| 1526 |
return nomatch(); |
| 1527 |
} |
| 1528 |
assert(0); |
| 1529 |
} |
| 1530 |
// printf(". argi = %d, nfargs = %d, nfargs2 = %d, argsConsumed = %d\n", cast(int) argi, cast(int) nfargs, cast(int) nfargs2, cast(int) argsConsumed); |
| 1531 |
if (argsConsumed != nfargs2 && fparameters.varargs == VarArg.none) |
| 1532 |
return nomatch(); |
| 1533 |
} |
| 1534 |
|
| 1535 |
Lmatch: |
| 1536 |
foreach (ref dedtype; *dedtypes) |
| 1537 |
{ |
| 1538 |
if (Type at = isType(dedtype)) |
| 1539 |
{ |
| 1540 |
if (at.ty == Tnone) |
| 1541 |
{ |
| 1542 |
TypeDeduced xt = cast(TypeDeduced)at; |
| 1543 |
at = xt.tded; // 'unbox' |
| 1544 |
} |
| 1545 |
dedtype = at.merge2(); |
| 1546 |
} |
| 1547 |
} |
| 1548 |
for (size_t i = ntargs; i < dedargs.length; i++) |
| 1549 |
{ |
| 1550 |
TemplateParameter tparam = (*td.parameters)[i]; |
| 1551 |
//printf("tparam[%d] = %s\n", i, tparam.ident.toChars()); |
| 1552 |
|
| 1553 |
/* For T:T*, the dedargs is the T*, dedtypes is the T |
| 1554 |
* But for function templates, we really need them to match |
| 1555 |
*/ |
| 1556 |
RootObject oarg = (*dedargs)[i]; |
| 1557 |
RootObject oded = (*dedtypes)[i]; |
| 1558 |
//printf("1dedargs[%d] = %p, dedtypes[%d] = %p\n", i, oarg, i, oded); |
| 1559 |
//if (oarg) printf("oarg: %s\n", oarg.toChars()); |
| 1560 |
//if (oded) printf("oded: %s\n", oded.toChars()); |
| 1561 |
if (oarg) |
| 1562 |
continue; |
| 1563 |
|
| 1564 |
if (oded) |
| 1565 |
{ |
| 1566 |
if (tparam.specialization() || !tparam.isTemplateTypeParameter()) |
| 1567 |
{ |
| 1568 |
/* The specialization can work as long as afterwards |
| 1569 |
* the oded == oarg |
| 1570 |
*/ |
| 1571 |
(*dedargs)[i] = oded; |
| 1572 |
MATCH m2 = tparam.matchArg(instLoc, paramscope, dedargs, i, td.parameters, *dedtypes, null); |
| 1573 |
//printf("m2 = %d\n", m2); |
| 1574 |
if (m2 == MATCH.nomatch) |
| 1575 |
return nomatch(); |
| 1576 |
if (m2 < matchTiargs) |
| 1577 |
matchTiargs = m2; // pick worst match |
| 1578 |
if (!(*dedtypes)[i].equals(oded)) |
| 1579 |
.error(td.loc, "%s `%s` specialization not allowed for deduced parameter `%s`", td.kind, td.toPrettyChars, tparam.ident.toChars()); |
| 1580 |
} |
| 1581 |
else |
| 1582 |
{ |
| 1583 |
// Discussion: https://issues.dlang.org/show_bug.cgi?id=16484 |
| 1584 |
if (MATCH.convert < matchTiargs) |
| 1585 |
matchTiargs = MATCH.convert; |
| 1586 |
} |
| 1587 |
} |
| 1588 |
else |
| 1589 |
{ |
| 1590 |
oded = tparam.defaultArg(instLoc, paramscope); |
| 1591 |
if (!oded) |
| 1592 |
{ |
| 1593 |
// if tuple parameter and |
| 1594 |
// tuple parameter was not in function parameter list and |
| 1595 |
// we're one or more arguments short (i.e. no tuple argument) |
| 1596 |
if (tparam == tp && |
| 1597 |
fptupindex == IDX_NOTFOUND && |
| 1598 |
ntargs <= dedargs.length - 1) |
| 1599 |
{ |
| 1600 |
// make tuple argument an empty tuple |
| 1601 |
oded = new Tuple(); |
| 1602 |
} |
| 1603 |
else |
| 1604 |
return nomatch(); |
| 1605 |
} |
| 1606 |
if (isError(oded)) |
| 1607 |
return matcherror(); |
| 1608 |
ntargs++; |
| 1609 |
|
| 1610 |
/* At the template parameter T, the picked default template argument |
| 1611 |
* X!int should be matched to T in order to deduce dependent |
| 1612 |
* template parameter A. |
| 1613 |
* auto foo(T : X!A = X!int, A...)() { ... } |
| 1614 |
* foo(); // T <-- X!int, A <-- (int) |
| 1615 |
*/ |
| 1616 |
if (tparam.specialization()) |
| 1617 |
{ |
| 1618 |
(*dedargs)[i] = oded; |
| 1619 |
MATCH m2 = tparam.matchArg(instLoc, paramscope, dedargs, i, td.parameters, *dedtypes, null); |
| 1620 |
//printf("m2 = %d\n", m2); |
| 1621 |
if (m2 == MATCH.nomatch) |
| 1622 |
return nomatch(); |
| 1623 |
if (m2 < matchTiargs) |
| 1624 |
matchTiargs = m2; // pick worst match |
| 1625 |
if (!(*dedtypes)[i].equals(oded)) |
| 1626 |
.error(td.loc, "%s `%s` specialization not allowed for deduced parameter `%s`", td.kind, td.toPrettyChars, tparam.ident.toChars()); |
| 1627 |
} |
| 1628 |
} |
| 1629 |
oded = td.declareParameter(paramscope, tparam, oded); |
| 1630 |
(*dedargs)[i] = oded; |
| 1631 |
} |
| 1632 |
|
| 1633 |
/* https://issues.dlang.org/show_bug.cgi?id=7469 |
| 1634 |
* As same as the code for 7469 in findBestMatch, |
| 1635 |
* expand a Tuple in dedargs to normalize template arguments. |
| 1636 |
*/ |
| 1637 |
if (auto d = dedargs.length) |
| 1638 |
{ |
| 1639 |
if (auto va = isTuple((*dedargs)[d - 1])) |
| 1640 |
{ |
| 1641 |
dedargs.setDim(d - 1); |
| 1642 |
dedargs.insert(d - 1, &va.objects); |
| 1643 |
} |
| 1644 |
} |
| 1645 |
ti.tiargs = dedargs; // update to the normalized template arguments. |
| 1646 |
|
| 1647 |
// Partially instantiate function for constraint and fd.leastAsSpecialized() |
| 1648 |
{ |
| 1649 |
assert(paramscope.scopesym); |
| 1650 |
Scope* sc2 = td._scope; |
| 1651 |
sc2 = sc2.push(paramscope.scopesym); |
| 1652 |
sc2 = sc2.push(ti); |
| 1653 |
sc2.parent = ti; |
| 1654 |
sc2.tinst = ti; |
| 1655 |
sc2.minst = sc.minst; |
| 1656 |
sc2.stc |= fd.storage_class & STC.deprecated_; |
| 1657 |
|
| 1658 |
fd = doHeaderInstantiation(td, ti, sc2, fd, tthis, argumentList); |
| 1659 |
sc2 = sc2.pop(); |
| 1660 |
sc2 = sc2.pop(); |
| 1661 |
|
| 1662 |
if (!fd) |
| 1663 |
return nomatch(); |
| 1664 |
} |
| 1665 |
|
| 1666 |
if (td.constraint) |
| 1667 |
{ |
| 1668 |
if (!evaluateConstraint(td, ti, sc, paramscope, dedargs, fd)) |
| 1669 |
return nomatch(); |
| 1670 |
} |
| 1671 |
|
| 1672 |
version (none) |
| 1673 |
{ |
| 1674 |
for (size_t i = 0; i < dedargs.length; i++) |
| 1675 |
{ |
| 1676 |
RootObject o = (*dedargs)[i]; |
| 1677 |
printf("\tdedargs[%d] = %d, %s\n", i, o.dyncast(), o.toChars()); |
| 1678 |
} |
| 1679 |
} |
| 1680 |
|
| 1681 |
paramscope.pop(); |
| 1682 |
//printf("\tmatch %d\n", match); |
| 1683 |
return MATCHpair(matchTiargs, match); |
| 1684 |
} |
| 1685 |
|
| 1686 |
/************************************************* |
| 1687 |
* Limited function template instantiation for using fd.leastAsSpecialized() |
| 1688 |
*/ |
| 1689 |
private |
| 1690 |
FuncDeclaration doHeaderInstantiation(TemplateDeclaration td, TemplateInstance ti, Scope* sc2, FuncDeclaration fd, Type tthis, ArgumentList inferenceArguments) |
| 1691 |
{ |
| 1692 |
assert(fd); |
| 1693 |
version (none) |
| 1694 |
{ |
| 1695 |
printf("doHeaderInstantiation this = %s\n", toChars()); |
| 1696 |
} |
| 1697 |
|
| 1698 |
// function body and contracts are not need |
| 1699 |
if (fd.isCtorDeclaration()) |
| 1700 |
fd = new CtorDeclaration(fd.loc, fd.endloc, fd.storage_class, fd.type.syntaxCopy()); |
| 1701 |
else |
| 1702 |
fd = new FuncDeclaration(fd.loc, fd.endloc, fd.ident, fd.storage_class, fd.type.syntaxCopy()); |
| 1703 |
fd.parent = ti; |
| 1704 |
|
| 1705 |
assert(fd.type.ty == Tfunction); |
| 1706 |
auto tf = fd.type.isTypeFunction(); |
| 1707 |
tf.inferenceArguments = inferenceArguments; |
| 1708 |
|
| 1709 |
if (tthis) |
| 1710 |
{ |
| 1711 |
// Match 'tthis' to any TemplateThisParameter's |
| 1712 |
bool hasttp = false; |
| 1713 |
foreach (tp; *td.parameters) |
| 1714 |
{ |
| 1715 |
TemplateThisParameter ttp = tp.isTemplateThisParameter(); |
| 1716 |
if (ttp) |
| 1717 |
hasttp = true; |
| 1718 |
} |
| 1719 |
if (hasttp) |
| 1720 |
{ |
| 1721 |
tf = tf.addSTC(ModToStc(tthis.mod)).isTypeFunction(); |
| 1722 |
assert(!tf.deco); |
| 1723 |
} |
| 1724 |
} |
| 1725 |
|
| 1726 |
Scope* scx = sc2.push(); |
| 1727 |
|
| 1728 |
// Shouldn't run semantic on default arguments and return type. |
| 1729 |
foreach (ref params; *tf.parameterList.parameters) |
| 1730 |
params.defaultArg = null; |
| 1731 |
tf.incomplete = true; |
| 1732 |
|
| 1733 |
if (fd.isCtorDeclaration()) |
| 1734 |
{ |
| 1735 |
// For constructors, emitting return type is necessary for |
| 1736 |
// isReturnIsolated() in functionResolve. |
| 1737 |
tf.isCtor = true; |
| 1738 |
|
| 1739 |
Dsymbol parent = td.toParentDecl(); |
| 1740 |
Type tret; |
| 1741 |
AggregateDeclaration ad = parent.isAggregateDeclaration(); |
| 1742 |
if (!ad || parent.isUnionDeclaration()) |
| 1743 |
{ |
| 1744 |
tret = Type.tvoid; |
| 1745 |
} |
| 1746 |
else |
| 1747 |
{ |
| 1748 |
tret = ad.handleType(); |
| 1749 |
assert(tret); |
| 1750 |
tret = tret.addStorageClass(fd.storage_class | scx.stc); |
| 1751 |
tret = tret.addMod(tf.mod); |
| 1752 |
} |
| 1753 |
tf.next = tret; |
| 1754 |
if (ad && ad.isStructDeclaration()) |
| 1755 |
tf.isRef = 1; |
| 1756 |
//printf("tf = %s\n", tf.toChars()); |
| 1757 |
} |
| 1758 |
else |
| 1759 |
tf.next = null; |
| 1760 |
fd.type = tf; |
| 1761 |
fd.type = fd.type.addSTC(scx.stc); |
| 1762 |
fd.type = fd.type.typeSemantic(fd.loc, scx); |
| 1763 |
scx = scx.pop(); |
| 1764 |
|
| 1765 |
if (fd.type.ty != Tfunction) |
| 1766 |
return null; |
| 1767 |
|
| 1768 |
fd.originalType = fd.type; // for mangling |
| 1769 |
//printf("\t[%s] fd.type = %s, mod = %x, ", loc.toChars(), fd.type.toChars(), fd.type.mod); |
| 1770 |
//printf("fd.needThis() = %d\n", fd.needThis()); |
| 1771 |
|
| 1772 |
return fd; |
| 1773 |
} |
| 1774 |
|
| 1775 |
/************************************************** |
| 1776 |
* Declare template parameter tp with value o, and install it in the scope sc. |
| 1777 |
*/ |
| 1778 |
extern (D) RootObject declareParameter(TemplateDeclaration td, Scope* sc, TemplateParameter tp, RootObject o) |
| 1779 |
{ |
| 1780 |
//printf("TemplateDeclaration.declareParameter('%s', o = %p)\n", tp.ident.toChars(), o); |
| 1781 |
Type ta = isType(o); |
| 1782 |
Expression ea = isExpression(o); |
| 1783 |
Dsymbol sa = isDsymbol(o); |
| 1784 |
Tuple va = isTuple(o); |
| 1785 |
|
| 1786 |
Declaration d; |
| 1787 |
VarDeclaration v = null; |
| 1788 |
|
| 1789 |
if (ea) |
| 1790 |
{ |
| 1791 |
if (ea.op == EXP.type) |
| 1792 |
ta = ea.type; |
| 1793 |
else if (auto se = ea.isScopeExp()) |
| 1794 |
sa = se.sds; |
| 1795 |
else if (auto te = ea.isThisExp()) |
| 1796 |
sa = te.var; |
| 1797 |
else if (auto se = ea.isSuperExp()) |
| 1798 |
sa = se.var; |
| 1799 |
else if (auto fe = ea.isFuncExp()) |
| 1800 |
{ |
| 1801 |
if (fe.td) |
| 1802 |
sa = fe.td; |
| 1803 |
else |
| 1804 |
sa = fe.fd; |
| 1805 |
} |
| 1806 |
} |
| 1807 |
|
| 1808 |
if (ta) |
| 1809 |
{ |
| 1810 |
//printf("type %s\n", ta.toChars()); |
| 1811 |
auto ad = new AliasDeclaration(Loc.initial, tp.ident, ta); |
| 1812 |
ad.storage_class |= STC.templateparameter; |
| 1813 |
d = ad; |
| 1814 |
} |
| 1815 |
else if (sa) |
| 1816 |
{ |
| 1817 |
//printf("Alias %s %s;\n", sa.ident.toChars(), tp.ident.toChars()); |
| 1818 |
auto ad = new AliasDeclaration(Loc.initial, tp.ident, sa); |
| 1819 |
ad.storage_class |= STC.templateparameter; |
| 1820 |
d = ad; |
| 1821 |
} |
| 1822 |
else if (ea) |
| 1823 |
{ |
| 1824 |
// tdtypes.data[i] always matches ea here |
| 1825 |
Initializer _init = new ExpInitializer(td.loc, ea); |
| 1826 |
TemplateValueParameter tvp = tp.isTemplateValueParameter(); |
| 1827 |
Type t = tvp ? tvp.valType : null; |
| 1828 |
v = new VarDeclaration(td.loc, t, tp.ident, _init); |
| 1829 |
v.storage_class = STC.manifest | STC.templateparameter; |
| 1830 |
d = v; |
| 1831 |
} |
| 1832 |
else if (va) |
| 1833 |
{ |
| 1834 |
//printf("\ttuple\n"); |
| 1835 |
d = new TupleDeclaration(td.loc, tp.ident, &va.objects); |
| 1836 |
} |
| 1837 |
else |
| 1838 |
{ |
| 1839 |
assert(0); |
| 1840 |
} |
| 1841 |
d.storage_class |= STC.templateparameter; |
| 1842 |
|
| 1843 |
if (ta) |
| 1844 |
{ |
| 1845 |
Type t = ta; |
| 1846 |
// consistent with Type.checkDeprecated() |
| 1847 |
while (t.ty != Tenum) |
| 1848 |
{ |
| 1849 |
if (!t.nextOf()) |
| 1850 |
break; |
| 1851 |
t = (cast(TypeNext)t).next; |
| 1852 |
} |
| 1853 |
if (Dsymbol s = t.toDsymbol(sc)) |
| 1854 |
{ |
| 1855 |
if (s.isDeprecated()) |
| 1856 |
d.storage_class |= STC.deprecated_; |
| 1857 |
} |
| 1858 |
} |
| 1859 |
else if (sa) |
| 1860 |
{ |
| 1861 |
if (sa.isDeprecated()) |
| 1862 |
d.storage_class |= STC.deprecated_; |
| 1863 |
} |
| 1864 |
|
| 1865 |
if (!sc.insert(d)) |
| 1866 |
.error(td.loc, "%s `%s` declaration `%s` is already defined", td.kind, td.toPrettyChars, tp.ident.toChars()); |
| 1867 |
d.dsymbolSemantic(sc); |
| 1868 |
/* So the caller's o gets updated with the result of semantic() being run on o |
| 1869 |
*/ |
| 1870 |
if (v) |
| 1871 |
o = v._init.initializerToExpression(); |
| 1872 |
return o; |
| 1873 |
} |
| 1874 |
|
| 1875 |
/************************************************* |
| 1876 |
* Given function arguments, figure out which template function |
| 1877 |
* to expand, and return matching result. |
| 1878 |
* Params: |
| 1879 |
* m = matching result |
| 1880 |
* dstart = the root of overloaded function templates |
| 1881 |
* loc = instantiation location |
| 1882 |
* sc = instantiation scope |
| 1883 |
* tiargs = initial list of template arguments |
| 1884 |
* tthis = if !NULL, the 'this' pointer argument |
| 1885 |
* argumentList= arguments to function |
| 1886 |
* errorHelper = delegate to send error message to if not null |
| 1887 |
*/ |
| 1888 |
void functionResolve(ref MatchAccumulator m, Dsymbol dstart, Loc loc, Scope* sc, Objects* tiargs, |
| 1889 |
Type tthis, ArgumentList argumentList, void delegate(const(char)*) scope errorHelper = null) |
| 1890 |
{ |
| 1891 |
version (none) |
| 1892 |
{ |
| 1893 |
printf("functionResolve() dstart: %s (", dstart.toChars()); |
| 1894 |
for (size_t i = 0; i < (tiargs ? (*tiargs).length : 0); i++) |
| 1895 |
{ |
| 1896 |
if (i) printf(", "); |
| 1897 |
RootObject arg = (*tiargs)[i]; |
| 1898 |
printf("%s", arg.toChars()); |
| 1899 |
} |
| 1900 |
printf(")("); |
| 1901 |
for (size_t i = 0; i < (argumentList.arguments ? (*argumentList.arguments).length : 0); i++) |
| 1902 |
{ |
| 1903 |
if (i) printf(", "); |
| 1904 |
Expression arg = (*argumentList.arguments)[i]; |
| 1905 |
printf("%s %s", arg.type.toChars(), arg.toChars()); |
| 1906 |
//printf("\tty = %d\n", arg.type.ty); |
| 1907 |
} |
| 1908 |
printf(")\n"); |
| 1909 |
//printf("stc = %llx\n", dstart._scope.stc); |
| 1910 |
//printf("match:t/f = %d/%d\n", ta_last, m.last); |
| 1911 |
} |
| 1912 |
|
| 1913 |
// results |
| 1914 |
int property = 0; // 0: uninitialized |
| 1915 |
// 1: seen @property |
| 1916 |
// 2: not @property |
| 1917 |
size_t ov_index = 0; |
| 1918 |
TemplateDeclaration td_best; |
| 1919 |
TemplateInstance ti_best; |
| 1920 |
MATCH ta_last = m.last != MATCH.nomatch ? MATCH.exact : MATCH.nomatch; |
| 1921 |
Type tthis_best; |
| 1922 |
|
| 1923 |
int applyFunction(FuncDeclaration fd) |
| 1924 |
{ |
| 1925 |
// skip duplicates |
| 1926 |
if (fd == m.lastf) |
| 1927 |
return 0; |
| 1928 |
// explicitly specified tiargs never match to non template function |
| 1929 |
if (tiargs && tiargs.length > 0) |
| 1930 |
return 0; |
| 1931 |
|
| 1932 |
// constructors need a valid scope in order to detect semantic errors |
| 1933 |
if (!fd.isCtorDeclaration && |
| 1934 |
fd.semanticRun < PASS.semanticdone) |
| 1935 |
{ |
| 1936 |
fd.ungagSpeculative(); |
| 1937 |
fd.dsymbolSemantic(null); |
| 1938 |
} |
| 1939 |
if (fd.semanticRun < PASS.semanticdone) |
| 1940 |
{ |
| 1941 |
.error(loc, "forward reference to template `%s`", fd.toChars()); |
| 1942 |
return 1; |
| 1943 |
} |
| 1944 |
//printf("fd = %s %s, fargs = %s\n", fd.toChars(), fd.type.toChars(), fargs.toChars()); |
| 1945 |
auto tf = fd.type.isTypeFunction(); |
| 1946 |
|
| 1947 |
int prop = tf.isProperty ? 1 : 2; |
| 1948 |
if (property == 0) |
| 1949 |
property = prop; |
| 1950 |
else if (property != prop) |
| 1951 |
error(fd.loc, "cannot overload both property and non-property functions"); |
| 1952 |
|
| 1953 |
/* For constructors, qualifier check will be opposite direction. |
| 1954 |
* Qualified constructor always makes qualified object, then will be checked |
| 1955 |
* that it is implicitly convertible to tthis. |
| 1956 |
*/ |
| 1957 |
Type tthis_fd = fd.needThis() ? tthis : null; |
| 1958 |
bool isCtorCall = tthis_fd && fd.isCtorDeclaration(); |
| 1959 |
if (isCtorCall) |
| 1960 |
{ |
| 1961 |
//printf("%s tf.mod = x%x tthis_fd.mod = x%x %d\n", tf.toChars(), |
| 1962 |
// tf.mod, tthis_fd.mod, fd.isReturnIsolated()); |
| 1963 |
if (MODimplicitConv(tf.mod, tthis_fd.mod) || |
| 1964 |
tf.isWild() && tf.isShared() == tthis_fd.isShared() || |
| 1965 |
fd.isReturnIsolated()) |
| 1966 |
{ |
| 1967 |
/* && tf.isShared() == tthis_fd.isShared()*/ |
| 1968 |
// Uniquely constructed object can ignore shared qualifier. |
| 1969 |
// TODO: Is this appropriate? |
| 1970 |
tthis_fd = null; |
| 1971 |
} |
| 1972 |
else |
| 1973 |
return 0; // MATCH.nomatch |
| 1974 |
} |
| 1975 |
/* Fix Issue 17970: |
| 1976 |
If a struct is declared as shared the dtor is automatically |
| 1977 |
considered to be shared, but when the struct is instantiated |
| 1978 |
the instance is no longer considered to be shared when the |
| 1979 |
function call matching is done. The fix makes it so that if a |
| 1980 |
struct declaration is shared, when the destructor is called, |
| 1981 |
the instantiated struct is also considered shared. |
| 1982 |
*/ |
| 1983 |
if (auto dt = fd.isDtorDeclaration()) |
| 1984 |
{ |
| 1985 |
auto dtmod = dt.type.toTypeFunction(); |
| 1986 |
auto shared_dtor = dtmod.mod & MODFlags.shared_; |
| 1987 |
auto shared_this = tthis_fd !is null ? |
| 1988 |
tthis_fd.mod & MODFlags.shared_ : 0; |
| 1989 |
if (shared_dtor && !shared_this) |
| 1990 |
tthis_fd = dtmod; |
| 1991 |
else if (shared_this && !shared_dtor && tthis_fd !is null) |
| 1992 |
tf.mod = tthis_fd.mod; |
| 1993 |
} |
| 1994 |
const(char)* failMessage; |
| 1995 |
MATCH mfa = callMatch(fd, tf, tthis_fd, argumentList, 0, errorHelper, sc); |
| 1996 |
//printf("test1: mfa = %d\n", mfa); |
| 1997 |
if (failMessage) |
| 1998 |
errorHelper(failMessage); |
| 1999 |
if (mfa == MATCH.nomatch) |
| 2000 |
return 0; |
| 2001 |
|
| 2002 |
int firstIsBetter() |
| 2003 |
{ |
| 2004 |
td_best = null; |
| 2005 |
ti_best = null; |
| 2006 |
ta_last = MATCH.exact; |
| 2007 |
m.last = mfa; |
| 2008 |
m.lastf = fd; |
| 2009 |
tthis_best = tthis_fd; |
| 2010 |
ov_index = 0; |
| 2011 |
m.count = 1; |
| 2012 |
return 0; |
| 2013 |
} |
| 2014 |
|
| 2015 |
if (mfa > m.last) return firstIsBetter(); |
| 2016 |
if (mfa < m.last) return 0; |
| 2017 |
|
| 2018 |
/* See if one of the matches overrides the other. |
| 2019 |
*/ |
| 2020 |
assert(m.lastf); |
| 2021 |
if (m.lastf.overrides(fd)) return 0; |
| 2022 |
if (fd.overrides(m.lastf)) return firstIsBetter(); |
| 2023 |
|
| 2024 |
/* Try to disambiguate using template-style partial ordering rules. |
| 2025 |
* In essence, if f() and g() are ambiguous, if f() can call g(), |
| 2026 |
* but g() cannot call f(), then pick f(). |
| 2027 |
* This is because f() is "more specialized." |
| 2028 |
*/ |
| 2029 |
{ |
| 2030 |
MATCH c1 = funcLeastAsSpecialized(fd, m.lastf, argumentList.names); |
| 2031 |
MATCH c2 = funcLeastAsSpecialized(m.lastf, fd, argumentList.names); |
| 2032 |
//printf("c1 = %d, c2 = %d\n", c1, c2); |
| 2033 |
if (c1 > c2) return firstIsBetter(); |
| 2034 |
if (c1 < c2) return 0; |
| 2035 |
} |
| 2036 |
|
| 2037 |
/* The 'overrides' check above does covariant checking only |
| 2038 |
* for virtual member functions. It should do it for all functions, |
| 2039 |
* but in order to not risk breaking code we put it after |
| 2040 |
* the 'leastAsSpecialized' check. |
| 2041 |
* In the future try moving it before. |
| 2042 |
* I.e. a not-the-same-but-covariant match is preferred, |
| 2043 |
* as it is more restrictive. |
| 2044 |
*/ |
| 2045 |
if (!m.lastf.type.equals(fd.type)) |
| 2046 |
{ |
| 2047 |
//printf("cov: %d %d\n", m.lastf.type.covariant(fd.type), fd.type.covariant(m.lastf.type)); |
| 2048 |
const lastCovariant = m.lastf.type.covariant(fd.type); |
| 2049 |
const firstCovariant = fd.type.covariant(m.lastf.type); |
| 2050 |
|
| 2051 |
if (lastCovariant == Covariant.yes || lastCovariant == Covariant.no) |
| 2052 |
{ |
| 2053 |
if (firstCovariant != Covariant.yes && firstCovariant != Covariant.no) |
| 2054 |
{ |
| 2055 |
return 0; |
| 2056 |
} |
| 2057 |
} |
| 2058 |
else if (firstCovariant == Covariant.yes || firstCovariant == Covariant.no) |
| 2059 |
{ |
| 2060 |
return firstIsBetter(); |
| 2061 |
} |
| 2062 |
} |
| 2063 |
|
| 2064 |
/* If the two functions are the same function, like: |
| 2065 |
* int foo(int); |
| 2066 |
* int foo(int x) { ... } |
| 2067 |
* then pick the one with the body. |
| 2068 |
* |
| 2069 |
* If none has a body then don't care because the same |
| 2070 |
* real function would be linked to the decl (e.g from object file) |
| 2071 |
*/ |
| 2072 |
if (tf.equals(m.lastf.type) && |
| 2073 |
fd.storage_class == m.lastf.storage_class && |
| 2074 |
fd.parent == m.lastf.parent && |
| 2075 |
fd.visibility == m.lastf.visibility && |
| 2076 |
fd._linkage == m.lastf._linkage) |
| 2077 |
{ |
| 2078 |
if (fd.fbody && !m.lastf.fbody) |
| 2079 |
return firstIsBetter(); |
| 2080 |
if (!fd.fbody) |
| 2081 |
return 0; |
| 2082 |
} |
| 2083 |
|
| 2084 |
// https://issues.dlang.org/show_bug.cgi?id=14450 |
| 2085 |
// Prefer exact qualified constructor for the creating object type |
| 2086 |
if (isCtorCall && tf.mod != m.lastf.type.mod) |
| 2087 |
{ |
| 2088 |
if (tthis.mod == tf.mod) return firstIsBetter(); |
| 2089 |
if (tthis.mod == m.lastf.type.mod) return 0; |
| 2090 |
} |
| 2091 |
|
| 2092 |
m.nextf = fd; |
| 2093 |
m.count++; |
| 2094 |
return 0; |
| 2095 |
} |
| 2096 |
|
| 2097 |
int applyTemplate(TemplateDeclaration td) |
| 2098 |
{ |
| 2099 |
//printf("applyTemplate(): td = %s\n", td.toChars()); |
| 2100 |
if (td == td_best) // skip duplicates |
| 2101 |
return 0; |
| 2102 |
|
| 2103 |
if (!sc) |
| 2104 |
sc = td._scope; // workaround for Type.aliasthisOf |
| 2105 |
|
| 2106 |
if (td.semanticRun == PASS.initial && td._scope) |
| 2107 |
{ |
| 2108 |
// Try to fix forward reference. Ungag errors while doing so. |
| 2109 |
td.ungagSpeculative(); |
| 2110 |
td.dsymbolSemantic(td._scope); |
| 2111 |
} |
| 2112 |
if (td.semanticRun == PASS.initial) |
| 2113 |
{ |
| 2114 |
.error(loc, "forward reference to template `%s`", td.toChars()); |
| 2115 |
Lerror: |
| 2116 |
m.lastf = null; |
| 2117 |
m.count = 0; |
| 2118 |
m.last = MATCH.nomatch; |
| 2119 |
return 1; |
| 2120 |
} |
| 2121 |
//printf("td = %s\n", td.toChars()); |
| 2122 |
|
| 2123 |
auto f = td.onemember ? td.onemember.isFuncDeclaration() : null; |
| 2124 |
if (!f) |
| 2125 |
{ |
| 2126 |
if (!tiargs) |
| 2127 |
tiargs = new Objects(); |
| 2128 |
auto ti = new TemplateInstance(loc, td, tiargs); |
| 2129 |
Objects dedtypes = Objects(td.parameters.length); |
| 2130 |
assert(td.semanticRun != PASS.initial); |
| 2131 |
MATCH mta = matchWithInstance(sc, td, ti, dedtypes, argumentList, 0); |
| 2132 |
//printf("matchWithInstance = %d\n", mta); |
| 2133 |
if (mta == MATCH.nomatch || mta < ta_last) // no match or less match |
| 2134 |
return 0; |
| 2135 |
|
| 2136 |
ti.templateInstanceSemantic(sc, argumentList); |
| 2137 |
if (!ti.inst) // if template failed to expand |
| 2138 |
return 0; |
| 2139 |
|
| 2140 |
Dsymbol s = ti.inst.toAlias(); |
| 2141 |
FuncDeclaration fd; |
| 2142 |
if (auto tdx = s.isTemplateDeclaration()) |
| 2143 |
{ |
| 2144 |
Objects dedtypesX; // empty tiargs |
| 2145 |
|
| 2146 |
// https://issues.dlang.org/show_bug.cgi?id=11553 |
| 2147 |
// Check for recursive instantiation of tdx. |
| 2148 |
for (TemplatePrevious* p = tdx.previous; p; p = p.prev) |
| 2149 |
{ |
| 2150 |
if (arrayObjectMatch(*p.dedargs, dedtypesX)) |
| 2151 |
{ |
| 2152 |
//printf("recursive, no match p.sc=%p %p %s\n", p.sc, this, this.toChars()); |
| 2153 |
/* It must be a subscope of p.sc, other scope chains are not recursive |
| 2154 |
* instantiations. |
| 2155 |
*/ |
| 2156 |
for (Scope* scx = sc; scx; scx = scx.enclosing) |
| 2157 |
{ |
| 2158 |
if (scx == p.sc) |
| 2159 |
{ |
| 2160 |
error(loc, "recursive template expansion while looking for `%s.%s`", ti.toChars(), tdx.toChars()); |
| 2161 |
goto Lerror; |
| 2162 |
} |
| 2163 |
} |
| 2164 |
} |
| 2165 |
/* BUG: should also check for ref param differences |
| 2166 |
*/ |
| 2167 |
} |
| 2168 |
|
| 2169 |
TemplatePrevious pr; |
| 2170 |
pr.prev = tdx.previous; |
| 2171 |
pr.sc = sc; |
| 2172 |
pr.dedargs = &dedtypesX; |
| 2173 |
tdx.previous = ≺ // add this to threaded list |
| 2174 |
|
| 2175 |
fd = resolveFuncCall(loc, sc, s, null, tthis, argumentList, FuncResolveFlag.quiet); |
| 2176 |
|
| 2177 |
tdx.previous = pr.prev; // unlink from threaded list |
| 2178 |
} |
| 2179 |
else if (s.isFuncDeclaration()) |
| 2180 |
{ |
| 2181 |
fd = resolveFuncCall(loc, sc, s, null, tthis, argumentList, FuncResolveFlag.quiet); |
| 2182 |
} |
| 2183 |
else |
| 2184 |
goto Lerror; |
| 2185 |
|
| 2186 |
if (!fd) |
| 2187 |
return 0; |
| 2188 |
|
| 2189 |
if (fd.type.ty != Tfunction) |
| 2190 |
{ |
| 2191 |
m.lastf = fd; // to propagate "error match" |
| 2192 |
m.count = 1; |
| 2193 |
m.last = MATCH.nomatch; |
| 2194 |
return 1; |
| 2195 |
} |
| 2196 |
|
| 2197 |
Type tthis_fd = fd.needThis() && !fd.isCtorDeclaration() ? tthis : null; |
| 2198 |
|
| 2199 |
auto tf = fd.type.isTypeFunction(); |
| 2200 |
MATCH mfa = callMatch(fd, tf, tthis_fd, argumentList, 0, null, sc); |
| 2201 |
if (mfa < m.last) |
| 2202 |
return 0; |
| 2203 |
|
| 2204 |
if (mta < ta_last) goto Ltd_best2; |
| 2205 |
if (mta > ta_last) goto Ltd2; |
| 2206 |
|
| 2207 |
if (mfa < m.last) goto Ltd_best2; |
| 2208 |
if (mfa > m.last) goto Ltd2; |
| 2209 |
|
| 2210 |
// td_best and td are ambiguous |
| 2211 |
//printf("Lambig2\n"); |
| 2212 |
m.nextf = fd; |
| 2213 |
m.count++; |
| 2214 |
return 0; |
| 2215 |
|
| 2216 |
Ltd_best2: |
| 2217 |
return 0; |
| 2218 |
|
| 2219 |
Ltd2: |
| 2220 |
// td is the new best match |
| 2221 |
assert(td._scope); |
| 2222 |
td_best = td; |
| 2223 |
ti_best = null; |
| 2224 |
property = 0; // (backward compatibility) |
| 2225 |
ta_last = mta; |
| 2226 |
m.last = mfa; |
| 2227 |
m.lastf = fd; |
| 2228 |
tthis_best = tthis_fd; |
| 2229 |
ov_index = 0; |
| 2230 |
m.nextf = null; |
| 2231 |
m.count = 1; |
| 2232 |
return 0; |
| 2233 |
} |
| 2234 |
|
| 2235 |
//printf("td = %s\n", td.toChars()); |
| 2236 |
for (size_t ovi = 0; f; f = f.overnext0, ovi++) |
| 2237 |
{ |
| 2238 |
if (f.type.ty != Tfunction || f.errors) |
| 2239 |
goto Lerror; |
| 2240 |
|
| 2241 |
/* This is a 'dummy' instance to evaluate constraint properly. |
| 2242 |
*/ |
| 2243 |
auto ti = new TemplateInstance(loc, td, tiargs); |
| 2244 |
ti.parent = td.parent; // Maybe calculating valid 'enclosing' is unnecessary. |
| 2245 |
|
| 2246 |
auto fd = f; |
| 2247 |
MATCHpair x = td.deduceFunctionTemplateMatch(ti, sc, fd, tthis, argumentList); |
| 2248 |
MATCH mta = x.mta; |
| 2249 |
MATCH mfa = x.mfa; |
| 2250 |
//printf("match:t/f = %d/%d\n", mta, mfa); |
| 2251 |
if (!fd || mfa == MATCH.nomatch) |
| 2252 |
continue; |
| 2253 |
|
| 2254 |
Type tthis_fd = fd.needThis() ? tthis : null; |
| 2255 |
|
| 2256 |
bool isCtorCall = tthis_fd && fd.isCtorDeclaration(); |
| 2257 |
if (isCtorCall) |
| 2258 |
{ |
| 2259 |
// Constructor call requires additional check. |
| 2260 |
auto tf = fd.type.isTypeFunction(); |
| 2261 |
assert(tf.next); |
| 2262 |
if (MODimplicitConv(tf.mod, tthis_fd.mod) || |
| 2263 |
tf.isWild() && tf.isShared() == tthis_fd.isShared() || |
| 2264 |
fd.isReturnIsolated()) |
| 2265 |
{ |
| 2266 |
tthis_fd = null; |
| 2267 |
} |
| 2268 |
else |
| 2269 |
continue; // MATCH.nomatch |
| 2270 |
|
| 2271 |
// need to check here whether the constructor is the member of a struct |
| 2272 |
// declaration that defines a copy constructor. This is already checked |
| 2273 |
// in the semantic of CtorDeclaration, however, when matching functions, |
| 2274 |
// the template instance is not expanded. |
| 2275 |
// https://issues.dlang.org/show_bug.cgi?id=21613 |
| 2276 |
auto ad = fd.isThis(); |
| 2277 |
auto sd = ad.isStructDeclaration(); |
| 2278 |
if (checkHasBothRvalueAndCpCtor(sd, fd.isCtorDeclaration(), ti)) |
| 2279 |
continue; |
| 2280 |
} |
| 2281 |
|
| 2282 |
if (mta < ta_last) goto Ltd_best; |
| 2283 |
if (mta > ta_last) goto Ltd; |
| 2284 |
|
| 2285 |
if (mfa < m.last) goto Ltd_best; |
| 2286 |
if (mfa > m.last) goto Ltd; |
| 2287 |
|
| 2288 |
if (td_best) |
| 2289 |
{ |
| 2290 |
// Disambiguate by picking the most specialized TemplateDeclaration |
| 2291 |
MATCH c1 = leastAsSpecialized(sc, td, td_best, argumentList); |
| 2292 |
MATCH c2 = leastAsSpecialized(sc, td_best, td, argumentList); |
| 2293 |
//printf("1: c1 = %d, c2 = %d\n", c1, c2); |
| 2294 |
if (c1 > c2) goto Ltd; |
| 2295 |
if (c1 < c2) goto Ltd_best; |
| 2296 |
} |
| 2297 |
assert(fd && m.lastf); |
| 2298 |
{ |
| 2299 |
// Disambiguate by tf.callMatch |
| 2300 |
auto tf1 = fd.type.isTypeFunction(); |
| 2301 |
auto tf2 = m.lastf.type.isTypeFunction(); |
| 2302 |
MATCH c1 = callMatch(fd, tf1, tthis_fd, argumentList, 0, null, sc); |
| 2303 |
MATCH c2 = callMatch(m.lastf, tf2, tthis_best, argumentList, 0, null, sc); |
| 2304 |
//printf("2: c1 = %d, c2 = %d\n", c1, c2); |
| 2305 |
if (c1 > c2) goto Ltd; |
| 2306 |
if (c1 < c2) goto Ltd_best; |
| 2307 |
} |
| 2308 |
{ |
| 2309 |
// Disambiguate by picking the most specialized FunctionDeclaration |
| 2310 |
MATCH c1 = funcLeastAsSpecialized(fd, m.lastf, argumentList.names); |
| 2311 |
MATCH c2 = funcLeastAsSpecialized(m.lastf, fd, argumentList.names); |
| 2312 |
//printf("3: c1 = %d, c2 = %d\n", c1, c2); |
| 2313 |
if (c1 > c2) goto Ltd; |
| 2314 |
if (c1 < c2) goto Ltd_best; |
| 2315 |
} |
| 2316 |
|
| 2317 |
// https://issues.dlang.org/show_bug.cgi?id=14450 |
| 2318 |
// Prefer exact qualified constructor for the creating object type |
| 2319 |
if (isCtorCall && fd.type.mod != m.lastf.type.mod) |
| 2320 |
{ |
| 2321 |
if (tthis.mod == fd.type.mod) goto Ltd; |
| 2322 |
if (tthis.mod == m.lastf.type.mod) goto Ltd_best; |
| 2323 |
} |
| 2324 |
|
| 2325 |
m.nextf = fd; |
| 2326 |
m.count++; |
| 2327 |
continue; |
| 2328 |
|
| 2329 |
Ltd_best: // td_best is the best match so far |
| 2330 |
//printf("Ltd_best\n"); |
| 2331 |
continue; |
| 2332 |
|
| 2333 |
Ltd: // td is the new best match |
| 2334 |
//printf("Ltd\n"); |
| 2335 |
assert(td._scope); |
| 2336 |
td_best = td; |
| 2337 |
ti_best = ti; |
| 2338 |
property = 0; // (backward compatibility) |
| 2339 |
ta_last = mta; |
| 2340 |
m.last = mfa; |
| 2341 |
m.lastf = fd; |
| 2342 |
tthis_best = tthis_fd; |
| 2343 |
ov_index = ovi; |
| 2344 |
m.nextf = null; |
| 2345 |
m.count = 1; |
| 2346 |
continue; |
| 2347 |
} |
| 2348 |
return 0; |
| 2349 |
} |
| 2350 |
|
| 2351 |
auto td = dstart.isTemplateDeclaration(); |
| 2352 |
if (td && td.funcroot) |
| 2353 |
dstart = td.funcroot; |
| 2354 |
overloadApply(dstart, (Dsymbol s) |
| 2355 |
{ |
| 2356 |
if (s.errors) |
| 2357 |
return 0; |
| 2358 |
if (auto fd = s.isFuncDeclaration()) |
| 2359 |
return applyFunction(fd); |
| 2360 |
if (auto td = s.isTemplateDeclaration()) |
| 2361 |
return applyTemplate(td); |
| 2362 |
return 0; |
| 2363 |
}, sc); |
| 2364 |
|
| 2365 |
//printf("td_best = %p, m.lastf = %p\n", td_best, m.lastf); |
| 2366 |
if (td_best && ti_best && m.count == 1) |
| 2367 |
{ |
| 2368 |
// Matches to template function |
| 2369 |
assert(td_best.onemember && td_best.onemember.isFuncDeclaration()); |
| 2370 |
/* The best match is td_best with arguments tdargs. |
| 2371 |
* Now instantiate the template. |
| 2372 |
*/ |
| 2373 |
assert(td_best._scope); |
| 2374 |
if (!sc) |
| 2375 |
sc = td_best._scope; // workaround for Type.aliasthisOf |
| 2376 |
|
| 2377 |
auto ti = new TemplateInstance(loc, td_best, ti_best.tiargs); |
| 2378 |
ti.templateInstanceSemantic(sc, argumentList); |
| 2379 |
|
| 2380 |
m.lastf = ti.toAlias().isFuncDeclaration(); |
| 2381 |
if (!m.lastf) |
| 2382 |
goto Lnomatch; |
| 2383 |
if (ti.errors) |
| 2384 |
{ |
| 2385 |
Lerror: |
| 2386 |
m.count = 1; |
| 2387 |
assert(m.lastf); |
| 2388 |
m.last = MATCH.nomatch; |
| 2389 |
return; |
| 2390 |
} |
| 2391 |
|
| 2392 |
// look forward instantiated overload function |
| 2393 |
// Dsymbol.oneMembers is alredy called in TemplateInstance.semantic. |
| 2394 |
// it has filled overnext0d |
| 2395 |
while (ov_index--) |
| 2396 |
{ |
| 2397 |
m.lastf = m.lastf.overnext0; |
| 2398 |
assert(m.lastf); |
| 2399 |
} |
| 2400 |
|
| 2401 |
tthis_best = m.lastf.needThis() && !m.lastf.isCtorDeclaration() ? tthis : null; |
| 2402 |
|
| 2403 |
if (m.lastf.type.ty == Terror) |
| 2404 |
goto Lerror; |
| 2405 |
auto tf = m.lastf.type.isTypeFunction(); |
| 2406 |
if (callMatch(m.lastf, tf, tthis_best, argumentList, 0, null, sc) == MATCH.nomatch) |
| 2407 |
goto Lnomatch; |
| 2408 |
|
| 2409 |
/* As https://issues.dlang.org/show_bug.cgi?id=3682 shows, |
| 2410 |
* a template instance can be matched while instantiating |
| 2411 |
* that same template. Thus, the function type can be incomplete. Complete it. |
| 2412 |
* |
| 2413 |
* https://issues.dlang.org/show_bug.cgi?id=9208 |
| 2414 |
* For auto function, completion should be deferred to the end of |
| 2415 |
* its semantic3. Should not complete it in here. |
| 2416 |
*/ |
| 2417 |
if (tf.next && !m.lastf.inferRetType) |
| 2418 |
{ |
| 2419 |
m.lastf.type = tf.typeSemantic(loc, sc); |
| 2420 |
} |
| 2421 |
} |
| 2422 |
else if (m.lastf) |
| 2423 |
{ |
| 2424 |
// Matches to non template function, |
| 2425 |
// or found matches were ambiguous. |
| 2426 |
assert(m.count >= 1); |
| 2427 |
} |
| 2428 |
else |
| 2429 |
{ |
| 2430 |
Lnomatch: |
| 2431 |
m.count = 0; |
| 2432 |
m.lastf = null; |
| 2433 |
m.last = MATCH.nomatch; |
| 2434 |
} |
| 2435 |
} |
| 2436 |
/* Create dummy argument based on parameter. |
| 2437 |
*/ |
| 2438 |
private RootObject dummyArg(TemplateParameter tp) |
| 2439 |
{ |
| 2440 |
scope v = new DummyArgVisitor(); |
| 2441 |
tp.accept(v); |
| 2442 |
return v.result; |
| 2443 |
} |
| 2444 |
private extern(C++) class DummyArgVisitor : Visitor |
| 2445 |
{ |
| 2446 |
RootObject result; |
| 2447 |
|
| 2448 |
alias visit = typeof(super).visit; |
| 2449 |
override void visit(TemplateTypeParameter ttp) |
| 2450 |
{ |
| 2451 |
Type t = ttp.specType; |
| 2452 |
if (t) |
| 2453 |
{ |
| 2454 |
result = t; |
| 2455 |
return; |
| 2456 |
} |
| 2457 |
// Use this for alias-parameter's too (?) |
| 2458 |
if (!ttp.tdummy) |
| 2459 |
ttp.tdummy = new TypeIdentifier(ttp.loc, ttp.ident); |
| 2460 |
result = ttp.tdummy; |
| 2461 |
} |
| 2462 |
|
| 2463 |
override void visit(TemplateValueParameter tvp) |
| 2464 |
{ |
| 2465 |
Expression e = tvp.specValue; |
| 2466 |
if (e) |
| 2467 |
{ |
| 2468 |
result = e; |
| 2469 |
return; |
| 2470 |
} |
| 2471 |
|
| 2472 |
// Create a dummy value |
| 2473 |
auto pe = cast(void*)tvp.valType in tvp.edummies; |
| 2474 |
if (pe) |
| 2475 |
{ |
| 2476 |
result = *pe; |
| 2477 |
return; |
| 2478 |
} |
| 2479 |
|
| 2480 |
e = tvp.valType.defaultInit(Loc.initial); |
| 2481 |
tvp.edummies[cast(void*)tvp.valType] = e; |
| 2482 |
result = e; |
| 2483 |
} |
| 2484 |
|
| 2485 |
override void visit(TemplateAliasParameter tap) |
| 2486 |
{ |
| 2487 |
RootObject s = tap.specAlias; |
| 2488 |
if (s) |
| 2489 |
{ |
| 2490 |
result = s; |
| 2491 |
return; |
| 2492 |
} |
| 2493 |
if (!tap.sdummy) |
| 2494 |
tap.sdummy = new Dsymbol(DSYM.dsymbol); |
| 2495 |
result = tap.sdummy; |
| 2496 |
} |
| 2497 |
|
| 2498 |
override void visit(TemplateTupleParameter tap) |
| 2499 |
{ |
| 2500 |
result = null; |
| 2501 |
} |
| 2502 |
} |