1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
|
#include "stdafx.h"
#include "NameSet.h"
#include "Engine.h"
#include "Exception.h"
#include "Core/StrBuf.h"
#include "Core/Str.h"
#include <limits>
namespace storm {
NameOverloads::NameOverloads() :
items(new (this) Array<Named *>()),
templates(null) {}
Bool NameOverloads::empty() const {
return items->empty()
&& (!templates || templates->empty());
}
Nat NameOverloads::count() const {
return items->count();
}
Named *NameOverloads::operator [](Nat id) const {
return items->at(id);
}
Named *NameOverloads::at(Nat id) const {
return items->at(id);
}
static bool equals(Array<Value> *a, Array<Value> *b) {
if (a->count() != b->count())
return false;
for (Nat i = 0; i < a->count(); i++)
if (a->at(i) != b->at(i))
return false;
return true;
}
static bool equals(Array<Value> *a, Array<Value> *b, ReplaceContext *ctx) {
if (!ctx)
return equals(a, b);
if (a->count() != b->count())
return false;
for (Nat i = 0; i < a->count(); i++)
if (ctx->normalize(a->at(i)) != ctx->normalize(b->at(i)))
return false;
return true;
}
MAYBE(Named *) NameOverloads::has(Named *item) {
for (Nat i = 0; i < items->count(); i++) {
if (storm::equals(item->params, items->at(i)->params))
return items->at(i);
}
return null;
}
void NameOverloads::add(Named *item) {
for (Nat i = 0; i < items->count(); i++) {
Named *curr = items->at(i);
// If we try to re-insert the same object, we don't complain. This may happen when a
// template "helps" the implementation to find a suitable overload (e.g. modifying a
// parameter) which results in a duplicate. As we look for object identity here, we only
// exclude cases where this is intentional (i.e. as it requires the template to remember
// previously created objects).
if (item == curr)
return;
if (storm::equals(item->params, curr->params)) {
// If the item we found says that it is OK to replace it, then just replace it
// without asking questions.
if (curr->flags & namedAllowReplace) {
items->at(i) = item;
item->automaticReplace(curr);
return;
} else {
throw new (this) TypedefError(
item->pos,
TO_S(engine(), item << S(" is already defined at:\n@") << items->at(i)->pos << S(": here")));
}
}
}
items->push(item);
}
void NameOverloads::add(Template *item) {
// There is no way to validate templates at this stage.
if (!templates)
templates = new (this) Array<Template *>();
templates->push(item);
}
Bool NameOverloads::remove(Named *item) {
for (Nat i = 0; i < items->count(); i++) {
if (items->at(i) == item) {
items->remove(i);
return true;
}
}
return false;
}
Bool NameOverloads::remove(Template *item) {
if (!templates)
return false;
for (Nat i = 0; i < templates->count(); i++) {
if (templates->at(i) == item) {
templates->remove(i);
return true;
}
}
return false;
}
void NameOverloads::merge(NameOverloads *from) {
// Validate first.
for (Nat i = 0; i < from->items->count(); i++) {
Named *add = from->items->at(i);
for (Nat j = 0; j < items->count(); j++) {
if (storm::equals(add->params, items->at(j)->params)) {
throw new (this) TypedefError(
add->pos,
TO_S(engine(), add << S(" is already defined at:\n@") << items->at(j)->pos << S(": here")));
}
}
}
if (from->templates && from->templates->any()) {
if (!templates)
templates = new (this) Array<Template *>();
for (Nat i = 0; i < from->templates->count(); i++)
templates->push(from->templates->at(i));
}
for (Nat i = 0; i < from->items->count(); i++)
items->push(from->items->at(i));
}
void NameOverloads::diff(NameOverloads *with, NameDiff &callback, ReplaceContext *ctx) {
Array<Bool> *used = new (this) Array<Bool>(with->items->count(), false);
for (Nat i = 0; i < items->count(); i++) {
Bool found = false;
Named *here = items->at(i);
for (Nat j = 0; j < with->items->count(); j++) {
if (used->at(j))
continue;
Named *other = with->items->at(j);
if (storm::equals(here->params, other->params, ctx)) {
found = true;
used->at(j) = true;
callback.changed(here, other);
break;
}
}
if (!found)
callback.removed(here);
}
for (Nat j = 0; j < with->items->count(); j++) {
if (!used->at(j))
callback.added(with->items->at(j));
}
}
void NameOverloads::diffAdded(NameDiff &callback) {
for (Nat i = 0; i < items->count(); i++)
callback.added(items->at(i));
}
void NameOverloads::diffRemoved(NameDiff &callback) {
for (Nat i = 0; i < items->count(); i++)
callback.removed(items->at(i));
}
void NameOverloads::diffTemplatesAdded(NameDiff &callback) {
if (!templates)
return;
for (Nat i = 0; i < templates->count(); i++)
callback.added(templates->at(i));
}
void NameOverloads::diffTemplatesRemoved(NameDiff &callback) {
if (!templates)
return;
for (Nat i = 0; i < templates->count(); i++)
callback.removed(templates->at(i));
}
Bool NameOverloads::anyTemplates() const {
return templates && templates->any();
}
Named *NameOverloads::createTemplate(NameSet *owner, SimplePart *part, Scope source) {
if (!templates)
return null;
Named *found = null;
for (Nat i = 0; i < templates->count(); i++) {
Named *n = templates->at(i)->generate(part);
if (found != null && n != null) {
throw new (this) TypedefError(owner->pos, TO_S(engine(), S("Multiple template matches for: ") << part));
} else if (n) {
// Only pick it if it matches.
if (part->matches(n, source) >= 0) {
found = n;
}
}
}
return found;
}
void NameOverloads::toS(StrBuf *to) const {
for (Nat i = 0; i < items->count(); i++)
*to << items->at(i) << L"\n";
if (templates && templates->any())
*to << L"<" << templates->count() << L" templates>\n";
}
/**
* NameSet.
*/
NameSet::NameSet(Str *name) : Named(name) {
init();
}
NameSet::NameSet(Str *name, Array<Value> *params) : Named(name, params) {
init();
}
NameSet::NameSet(SrcPos pos, Str *name) : Named(pos, name) {
init();
}
NameSet::NameSet(SrcPos pos, Str *name, Array<Value> *params) : Named(pos, name, params) {
init();
}
void NameSet::init() {
loaded = false;
loading = false;
sourceDiscarded = false;
nextAnon = 0;
if (engine().has(bootTemplates))
lateInit();
}
void NameSet::lateInit() {
Named::lateInit();
if (!overloads)
overloads = new (this) Map<Str *, NameOverloads *>();
}
void NameSet::compile() {
forceLoad();
for (Iter i = begin(), e = end(); i != e; ++i)
i.v()->compile();
}
void NameSet::discardSource() {
sourceDiscarded = true;
for (Iter i = begin(), e = end(); i != e; ++i)
i.v()->discardSource();
}
void NameSet::stopDiscardSource() {
sourceDiscarded = false;
}
void NameSet::watchAdd(Named *notifyTo) {
if (!notify)
notify = new (this) WeakSet<Named>();
notify->put(notifyTo);
}
void NameSet::watchRemove(Named *notifyTo) {
if (!notify)
return;
notify->remove(notifyTo);
}
void NameSet::notifyAdd(Named *what) {
if (!notify)
return;
WeakSet<Named>::Iter i = notify->iter();
while (Named *n = i.next()) {
n->notifyAdded(this, what);
}
}
void NameSet::notifyRemove(Named *what) {
if (!notify)
return;
WeakSet<Named>::Iter i = notify->iter();
while (Named *n = i.next()) {
n->notifyRemoved(this, what);
}
}
MAYBE(Named *) NameSet::has(Named *item) const {
if (!overloads)
return null;
if (NameOverloads *o = overloads->get(item->name, null))
return o->has(item);
else
return null;
}
void NameSet::add(Named *item) {
if (!overloads)
overloads = new (this) Map<Str *, NameOverloads *>();
overloads->at(item->name)->add(item);
makeChild(item);
if (sourceDiscarded)
item->discardSource();
notifyAdd(item);
}
void NameSet::add(Template *item) {
if (!overloads)
overloads = new (this) Map<Str *, NameOverloads *>();
overloads->at(item->name)->add(item);
}
Bool NameSet::remove(Named *item) {
if (!overloads)
return false;
NameOverloads *o = overloads->at(item->name);
Bool ok = o->remove(item);
if (ok)
notifyRemove(item);
if (o->empty())
overloads->remove(item->name);
return ok;
}
Bool NameSet::remove(Template *item) {
if (!overloads)
return false;
NameOverloads *o = overloads->at(item->name);
Bool ok = o->remove(item);
if (o->empty())
overloads->remove(item->name);
return ok;
}
Str *NameSet::anonName() {
StrBuf *buf = new (this) StrBuf();
*buf << S("@ ") << (nextAnon++);
return buf->toS();
}
Array<Named *> *NameSet::content() {
Array<Named *> *r = new (this) Array<Named *>();
for (Overloads::Iter at = overloads->begin(); at != overloads->end(); ++at) {
NameOverloads &o = *at.v();
for (Nat i = 0; i < o.count(); i++)
r->push(o[i]);
}
return r;
}
Array<NameOverloads *> *NameSet::templateOverloads() {
Array<NameOverloads *> *result = new (this) Array<NameOverloads *>();
templateOverloads(result);
return result;
}
void NameSet::templateOverloads(Array<NameOverloads *> *result) {
if (!overloads)
return;
for (Overloads::Iter i = overloads->begin(), end = overloads->end(); i != end; ++i) {
NameOverloads *here = i.v();
if (here->anyTemplates())
result->push(here);
for (Nat i = 0; i < here->count(); i++) {
NameSet *r = as<NameSet>(here->at(i));
if (r)
r->templateOverloads(result);
}
}
}
MAYBE(NameOverloads *) NameSet::allOverloads(Str *name) {
Overloads::Iter found = overloads->find(name);
if (found == overloads->end())
return null;
else
return found.v();
}
void NameSet::forceLoad() {
if (loaded)
return;
if (loading) {
// This happens quite a lot...
// WARNING(L"Recursive loading attempted for " << name);
return;
}
loading = true;
try {
if (loadAll())
loaded = true;
} catch (...) {
loading = false;
throw;
}
loading = false;
}
Named *NameSet::find(SimplePart *part, Scope source) {
if (Named *found = tryFind(part, source))
return found;
if (loaded)
return null;
if (!loadName(part))
forceLoad();
return tryFind(part, source);
}
Named *NameSet::tryFind(SimplePart *part, Scope source) {
if (!overloads)
return null;
Overloads::Iter i = overloads->find(part->name);
if (i == overloads->end())
return null;
return tryFind(part, source, i.v());
}
Named *NameSet::tryFind(SimplePart *part, Scope source, NameOverloads *from) {
while (part) {
if (Named *found = tryFindSingle(part, source, from))
return found;
part = part->nextOption();
}
return null;
}
Named *NameSet::tryFindSingle(SimplePart *part, Scope source, NameOverloads *from) {
// Note: We do this in two steps. First, we find the best match, and keep track of whether
// or not there are multiple instances of that or not. If there are multiple instances of
// the best match, we need to produce an error. To do that, we loop through the candidates a
// second time (since the error path is generally not critical).
Named *bestCandidate = null;
Bool multipleBest = false;
Int best = std::numeric_limits<Int>::max();
for (Nat i = 0; i < from->count(); i++) {
Named *candidate = from->at(i);
// Ignore ones that are not visible. Note: We delegate this to the part, so that it may
// modify the default behavior.
if (!part->visible(candidate, source))
continue;
Int badness = part->matches(candidate, source);
if (badness < 0 || badness > best)
continue;
if (badness == best) {
// Multiple best matches so far. We can't keep track of them without allocating memory.
multipleBest = true;
} else {
multipleBest = false;
best = badness;
bestCandidate = candidate;
}
}
// If we have a badness above zero, we might need to create a template to get a better
// match.
if (best > 0) {
if (Named *created = from->createTemplate(this, part, source)) {
// Note: We always add the created template, even if it is not visible, or the best candidate.
add(created);
if (created && part->visible(created, source)) {
// Check suitability of the newly created template. Note: We more or less expect
// that the created template is a perfect match. If we get a higher badness,
// something is probably wrong with the implementation of the template.
Int badness = part->matches(created, source);
if (badness >= 0 && badness < best) {
bestCandidate = created;
best = badness;
multipleBest = false;
}
}
}
}
if (!multipleBest) {
// We have an answer!
return bestCandidate;
}
// Error case, we need to find all candidates.
StrBuf *msg = new (this) StrBuf();
*msg << S("Multiple possible matches for ") << this << S(", all with badness ") << best << S("\n");
for (Nat i = 0; i < from->count(); i++) {
Named *candidate = from->at(i);
if (!part->visible(candidate, source))
continue;
Int badness = part->matches(candidate, source);
if (badness == best)
*msg << S(" Could be: ") << candidate->identifier() << S("\n");
}
throw new (this) LookupError(msg->toS());
}
Bool NameSet::loadName(SimplePart *part) {
// Default implementation if the derived class does not support lazy-loading.
// Report some matches may be found using 'loadAll'.
return false;
}
Bool NameSet::loadAll() {
// Default implementation if the derived class does not support lazy-loading.
// Report done.
return true;
}
void NameSet::toS(StrBuf *to) const {
for (Overloads::Iter i = overloads->begin(); i != overloads->end(); ++i) {
*to << i.v();
}
}
void NameSet::merge(NameSet *from) {
if (!overloads)
overloads = new (this) Map<Str *, NameOverloads *>();
if (sourceDiscarded)
from->discardSource();
for (Overloads::Iter i = from->overloads->begin(), end = from->overloads->end(); i != end; ++i) {
overloads->at(i.k())->merge(i.v());
}
}
void NameSet::diff(NameSet *with, NameDiff &callback, ReplaceContext *ctx) {
if (!overloads && !with->overloads) {
// Nothing to do.
} else if (!overloads) {
// All entities were added.
for (Overloads::Iter i = with->overloads->begin(), end = with->overloads->end(); i != end; ++i) {
i.v()->diffAdded(callback);
i.v()->diffTemplatesAdded(callback);
}
} else if (!with->overloads) {
// All entities were removed.
for (Overloads::Iter i = overloads->begin(), end = overloads->end(); i != end; ++i) {
i.v()->diffRemoved(callback);
i.v()->diffTemplatesRemoved(callback);
}
} else {
Overloads::Iter end; // The 'end' iterator is always the same.
for (Overloads::Iter i = overloads->begin(); i != end; ++i) {
if (NameOverloads *o = with->overloads->get(i.k(), null))
i.v()->diff(o, callback, ctx);
else
i.v()->diffRemoved(callback);
i.v()->diffTemplatesRemoved(callback);
}
for (Overloads::Iter i = with->overloads->begin(); i != end; ++i) {
if (!overloads->has(i.k()))
i.v()->diffAdded(callback);
i.v()->diffTemplatesAdded(callback);
}
}
}
NameSet::Iter::Iter() : name(), pos(0), nextSet(null) {}
NameSet::Iter::Iter(Map<Str *, NameOverloads *> *c, NameSet *next) : name(c->begin()), pos(0), nextSet(next) {
advance();
}
Bool NameSet::Iter::operator ==(const Iter &o) const {
// Either both at end or none.
if (name == MapIter())
return o.name == MapIter();
if (name != o.name)
return false;
return pos == o.pos;
}
Bool NameSet::Iter::operator !=(const Iter &o) const {
return !(*this == o);
}
Named *NameSet::Iter::v() const {
return name.v()->at(pos);
}
void NameSet::Iter::advance() {
while (name != MapIter() && pos >= name.v()->count()) {
++name;
pos = 0;
}
if (nextSet && name == MapIter())
*this = nextSet->begin();
}
NameSet::Iter &NameSet::Iter::operator ++() {
pos++;
advance();
return *this;
}
NameSet::Iter NameSet::Iter::operator ++(int) {
Iter i(*this);
++*this;
return i;
}
NameSet::Iter NameSet::begin() const {
if (overloads)
return Iter(overloads, null);
else
return Iter();
}
NameSet::Iter NameSet::begin(NameSet *after) const {
if (overloads)
return Iter(overloads, after);
else
return after->begin();
}
NameSet::Iter NameSet::end() const {
return Iter();
}
Array<Named *> *NameSet::findName(Str *name) const {
Array<Named *> *result = new (this) Array<Named *>();
if (NameOverloads *f = overloads->get(name, null)) {
result->reserve(f->count());
for (Nat i = 0; i < f->count(); i++)
result->push(f->at(i));
}
return result;
}
void NameSet::dbg_dump() const {
PLN(L"Name set:");
for (Overloads::Iter i = overloads->begin(); i != overloads->end(); ++i) {
PLN(L" " << i.k() << L":");
NameOverloads *o = i.v();
for (Nat i = 0; i < o->count(); i++) {
PLN(L" " << o->at(i)->identifier());
}
}
}
}
|