node-translator.c++ 117 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
Kenton Varda's avatar
Kenton Varda committed
3
//
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
Kenton Varda's avatar
Kenton Varda committed
10
//
Kenton Varda's avatar
Kenton Varda committed
11 12
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
Kenton Varda's avatar
Kenton Varda committed
13
//
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Kenton Varda's avatar
Kenton Varda committed
21 22

#include "node-translator.h"
23
#include "parser.h"      // only for generateGroupId()
24
#include <capnp/serialize.h>
Kenton Varda's avatar
Kenton Varda committed
25 26 27 28
#include <kj/debug.h>
#include <kj/arena.h>
#include <set>
#include <map>
29
#include <stdlib.h>
Kenton Varda's avatar
Kenton Varda committed
30 31 32 33

namespace capnp {
namespace compiler {

34 35 36 37
bool shouldDetectIssue344() {
  return getenv("CAPNP_IGNORE_ISSUE_344") == nullptr;
}

Kenton Varda's avatar
Kenton Varda committed
38 39 40 41 42 43 44
class NodeTranslator::StructLayout {
  // Massive, disgusting class which implements the layout algorithm, which decides the offset
  // for each field.

public:
  template <typename UIntType>
  struct HoleSet {
Kenton Varda's avatar
Kenton Varda committed
45
    inline HoleSet(): holes{0, 0, 0, 0, 0, 0} {}
Kenton Varda's avatar
Kenton Varda committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

    // Represents a set of "holes" within a segment of allocated space, up to one hole of each
    // power-of-two size between 1 bit and 32 bits.
    //
    // The amount of "used" space in a struct's data segment can always be represented as a
    // combination of a word count and a HoleSet.  The HoleSet represents the space lost to
    // "padding".
    //
    // There can never be more than one hole of any particular size.  Why is this?  Well, consider
    // that every data field has a power-of-two size, every field must be aligned to a multiple of
    // its size, and the maximum size of a single field is 64 bits.  If we need to add a new field
    // of N bits, there are two possibilities:
    // 1. A hole of size N or larger exists.  In this case, we find the smallest hole that is at
    //    least N bits.  Let's say that that hole has size M.  We allocate the first N bits of the
    //    hole to the new field.  The remaining M - N bits become a series of holes of sizes N*2,
    //    N*4, ..., M / 2.  We know no holes of these sizes existed before because we chose M to be
    //    the smallest available hole larger than N.  So, there is still no more than one hole of
    //    each size, and no hole larger than any hole that existed previously.
    // 2. No hole equal or larger N exists.  In that case we extend the data section's size by one
    //    word, creating a new 64-bit hole at the end.  We then allocate N bits from it, creating
    //    a series of holes between N and 64 bits, as described in point (1).  Thus, again, there
    //    is still at most one hole of each size, and the largest hole is 32 bits.

Kenton Varda's avatar
Kenton Varda committed
69
    UIntType holes[6];
Kenton Varda's avatar
Kenton Varda committed
70 71 72 73 74 75 76
    // The offset of each hole as a multiple of its size.  A value of zero indicates that no hole
    // exists.  Notice that it is impossible for any actual hole to have an offset of zero, because
    // the first field allocated is always placed at the very beginning of the section.  So either
    // the section has a size of zero (in which case there are no holes), or offset zero is
    // already allocated and therefore cannot be a hole.

    kj::Maybe<UIntType> tryAllocate(UIntType lgSize) {
77
      // Try to find space for a field of size 2^lgSize within the set of holes.  If found,
Kenton Varda's avatar
Kenton Varda committed
78 79 80
      // remove it from the holes, and return its offset (as a multiple of its size).  If there
      // is no such space, returns zero (no hole can be at offset zero, as explained above).

81
      if (lgSize >= kj::size(holes)) {
82 83
        return nullptr;
      } else if (holes[lgSize] != 0) {
Kenton Varda's avatar
Kenton Varda committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        UIntType result = holes[lgSize];
        holes[lgSize] = 0;
        return result;
      } else {
        KJ_IF_MAYBE(next, tryAllocate(lgSize + 1)) {
          UIntType result = *next * 2;
          holes[lgSize] = result + 1;
          return result;
        } else {
          return nullptr;
        }
      }
    }

    uint assertHoleAndAllocate(UIntType lgSize) {
      KJ_ASSERT(holes[lgSize] != 0);
      uint result = holes[lgSize];
      holes[lgSize] = 0;
      return result;
    }

    void addHolesAtEnd(UIntType lgSize, UIntType offset,
106
                       UIntType limitLgSize = sizeof(HoleSet::holes) / sizeof(HoleSet::holes[0])) {
Kenton Varda's avatar
Kenton Varda committed
107 108 109 110
      // Add new holes of progressively larger sizes in the range [lgSize, limitLgSize) starting
      // from the given offset.  The idea is that you just allocated an lgSize-sized field from
      // an limitLgSize-sized space, such as a newly-added word on the end of the data segment.

111
      KJ_DREQUIRE(limitLgSize <= kj::size(holes));
Kenton Varda's avatar
Kenton Varda committed
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

      while (lgSize < limitLgSize) {
        KJ_DREQUIRE(holes[lgSize] == 0);
        KJ_DREQUIRE(offset % 2 == 1);
        holes[lgSize] = offset;
        ++lgSize;
        offset = (offset + 1) / 2;
      }
    }

    bool tryExpand(UIntType oldLgSize, uint oldOffset, uint expansionFactor) {
      // Try to expand the value at the given location by combining it with subsequent holes, so
      // as to expand the location to be 2^expansionFactor times the size that it started as.
      // (In other words, the new lgSize is oldLgSize + expansionFactor.)

      if (expansionFactor == 0) {
        // No expansion requested.
        return true;
      }
      if (holes[oldLgSize] != oldOffset + 1) {
        // The space immediately after the location is not a hole.
        return false;
      }

      // We can expand the location by one factor by combining it with a hole.  Try to further
      // expand from there to the number of factors requested.
      if (tryExpand(oldLgSize + 1, oldOffset >> 1, expansionFactor - 1)) {
        // Success.  Consume the hole.
        holes[oldLgSize] = 0;
        return true;
      } else {
        return false;
      }
    }

Kenton Varda's avatar
Kenton Varda committed
147
    kj::Maybe<uint> smallestAtLeast(uint size) {
Kenton Varda's avatar
Kenton Varda committed
148 149
      // Return the size of the smallest hole that is equal to or larger than the given size.

150
      for (uint i = size; i < kj::size(holes); i++) {
Kenton Varda's avatar
Kenton Varda committed
151 152 153 154 155 156
        if (holes[i] != 0) {
          return i;
        }
      }
      return nullptr;
    }
Kenton Varda's avatar
Kenton Varda committed
157 158 159 160 161 162 163

    uint getFirstWordUsed() {
      // Computes the lg of the amount of space used in the first word of the section.

      // If there is a 32-bit hole with a 32-bit offset, no more than the first 32 bits are used.
      // If no more than the first 32 bits are used, and there is a 16-bit hole with a 16-bit
      // offset, then no more than the first 16 bits are used.  And so on.
164
      for (uint i = kj::size(holes); i > 0; i--) {
Kenton Varda's avatar
Kenton Varda committed
165 166 167 168 169 170
        if (holes[i - 1] != 1) {
          return i;
        }
      }
      return 0;
    }
Kenton Varda's avatar
Kenton Varda committed
171 172 173 174 175
  };

  struct StructOrGroup {
    // Abstract interface for scopes in which fields can be added.

176
    virtual void addVoid() = 0;
Kenton Varda's avatar
Kenton Varda committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    virtual uint addData(uint lgSize) = 0;
    virtual uint addPointer() = 0;
    virtual bool tryExpandData(uint oldLgSize, uint oldOffset, uint expansionFactor) = 0;
    // Try to expand the given previously-allocated space by 2^expansionFactor.  Succeeds --
    // returning true -- if the following space happens to be empty, making this expansion possible.
    // Otherwise, returns false.
  };

  struct Top: public StructOrGroup {
    uint dataWordCount = 0;
    uint pointerCount = 0;
    // Size of the struct so far.

    HoleSet<uint> holes;

192 193
    void addVoid() override {}

Kenton Varda's avatar
Kenton Varda committed
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
    uint addData(uint lgSize) override {
      KJ_IF_MAYBE(hole, holes.tryAllocate(lgSize)) {
        return *hole;
      } else {
        uint offset = dataWordCount++ << (6 - lgSize);
        holes.addHolesAtEnd(lgSize, offset + 1);
        return offset;
      }
    }

    uint addPointer() override {
      return pointerCount++;
    }

    bool tryExpandData(uint oldLgSize, uint oldOffset, uint expansionFactor) override {
      return holes.tryExpand(oldLgSize, oldOffset, expansionFactor);
    }

    Top() = default;
    KJ_DISALLOW_COPY(Top);
  };

  struct Union {
    struct DataLocation {
      uint lgSize;
      uint offset;

      bool tryExpandTo(Union& u, uint newLgSize) {
        if (newLgSize <= lgSize) {
          return true;
        } else if (u.parent.tryExpandData(lgSize, offset, newLgSize - lgSize)) {
          offset >>= (newLgSize - lgSize);
          lgSize = newLgSize;
          return true;
        } else {
          return false;
        }
      }
    };

    StructOrGroup& parent;
    uint groupCount = 0;
236
    kj::Maybe<uint> discriminantOffset;
Kenton Varda's avatar
Kenton Varda committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    kj::Vector<DataLocation> dataLocations;
    kj::Vector<uint> pointerLocations;

    inline Union(StructOrGroup& parent): parent(parent) {}
    KJ_DISALLOW_COPY(Union);

    uint addNewDataLocation(uint lgSize) {
      // Add a whole new data location to the union with the given size.

      uint offset = parent.addData(lgSize);
      dataLocations.add(DataLocation { lgSize, offset });
      return offset;
    }

    uint addNewPointerLocation() {
      // Add a whole new pointer location to the union with the given size.

      return pointerLocations.add(parent.addPointer());
    }

257
    void newGroupAddingFirstMember() {
Kenton Varda's avatar
Kenton Varda committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
      if (++groupCount == 2) {
        addDiscriminant();
      }
    }

    bool addDiscriminant() {
      if (discriminantOffset == nullptr) {
        discriminantOffset = parent.addData(4);  // 2^4 = 16 bits
        return true;
      } else {
        return false;
      }
    }
  };

273
  struct Group final: public StructOrGroup {
Kenton Varda's avatar
Kenton Varda committed
274 275 276 277 278 279 280 281 282
  public:
    class DataLocationUsage {
    public:
      DataLocationUsage(): isUsed(false) {}
      explicit DataLocationUsage(uint lgSize): isUsed(true), lgSizeUsed(lgSize) {}

      kj::Maybe<uint> smallestHoleAtLeast(Union::DataLocation& location, uint lgSize) {
        // Find the smallest single hole that is at least the given size.  This is used to find the
        // optimal place to allocate each field -- it is placed in the smallest slot where it fits,
283
        // to reduce fragmentation.  Returns the size of the hole, if found.
Kenton Varda's avatar
Kenton Varda committed
284 285 286

        if (!isUsed) {
          // The location is effectively one big hole.
287 288 289 290 291
          if (lgSize <= location.lgSize) {
            return location.lgSize;
          } else {
            return nullptr;
          }
Kenton Varda's avatar
Kenton Varda committed
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
        } else if (lgSize >= lgSizeUsed) {
          // Requested size is at least our current usage, so clearly won't fit in any current
          // holes, but if the location's size is larger than what we're using, we'd be able to
          // expand.
          if (lgSize < location.lgSize) {
            return lgSize;
          } else {
            return nullptr;
          }
        } else KJ_IF_MAYBE(result, holes.smallestAtLeast(lgSize)) {
          // There's a hole.
          return *result;
        } else {
          // The requested size is smaller than what we're already using, but there are no holes
          // available.  If we could double our size, then we could allocate in the new space.

          if (lgSizeUsed < location.lgSize) {
            // We effectively create a new hole the same size as the current usage.
            return lgSizeUsed;
          } else {
            return nullptr;
          }
        }
      }

      uint allocateFromHole(Group& group, Union::DataLocation& location, uint lgSize) {
        // Allocate the given space from an existing hole, given smallestHoleAtLeast() already
        // returned non-null indicating such a hole exists.

        uint result;

        if (!isUsed) {
          // The location is totally unused, so just allocate from the beginning.
          KJ_DASSERT(lgSize <= location.lgSize, "Did smallestHoleAtLeast() really find a hole?");
          result = 0;
          isUsed = true;
          lgSizeUsed = lgSize;
        } else if (lgSize >= lgSizeUsed) {
          // Requested size is at least our current usage, so clearly won't fit in any holes.
          // We must expand to double the requested size, and return the second half.
          KJ_DASSERT(lgSize < location.lgSize, "Did smallestHoleAtLeast() really find a hole?");
          holes.addHolesAtEnd(lgSizeUsed, 1, lgSize);
          lgSizeUsed = lgSize + 1;
          result = 1;
        } else KJ_IF_MAYBE(hole, holes.tryAllocate(lgSize)) {
          // Found a hole.
          result = *hole;
        } else {
          // The requested size is smaller than what we're using so far, but didn't fit in a
          // hole.  We should double our "used" size, then allocate from the new space.
          KJ_DASSERT(lgSizeUsed < location.lgSize,
                     "Did smallestHoleAtLeast() really find a hole?");
          result = 1 << (lgSizeUsed - lgSize);
          holes.addHolesAtEnd(lgSize, result + 1, lgSizeUsed);
          lgSizeUsed += 1;
        }

        // Adjust the offset according to the location's offset before returning.
        uint locationOffset = location.offset << (location.lgSize - lgSize);
        return locationOffset + result;
      }

      kj::Maybe<uint> tryAllocateByExpanding(
          Group& group, Union::DataLocation& location, uint lgSize) {
        // Attempt to allocate the given size by requesting that the parent union expand this
        // location to fit.  This is used if smallestHoleAtLeast() already determined that there
        // are no holes that would fit, so we don't bother checking that.

        if (!isUsed) {
          if (location.tryExpandTo(group.parent, lgSize)) {
            isUsed = true;
            lgSizeUsed = lgSize;
364
            return location.offset << (location.lgSize - lgSize);
Kenton Varda's avatar
Kenton Varda committed
365 366 367 368 369
          } else {
            return nullptr;
          }
        } else {
          uint newSize = kj::max(lgSizeUsed, lgSize) + 1;
370
          if (tryExpandUsage(group, location, newSize, true)) {
371
            uint result = KJ_ASSERT_NONNULL(holes.tryAllocate(lgSize));
372 373
            uint locationOffset = location.offset << (location.lgSize - lgSize);
            return locationOffset + result;
Kenton Varda's avatar
Kenton Varda committed
374 375 376 377 378 379 380 381 382 383
          } else {
            return nullptr;
          }
        }
      }

      bool tryExpand(Group& group, Union::DataLocation& location,
                     uint oldLgSize, uint oldOffset, uint expansionFactor) {
        if (oldOffset == 0 && lgSizeUsed == oldLgSize) {
          // This location contains exactly the requested data, so just expand the whole thing.
384
          return tryExpandUsage(group, location, oldLgSize + expansionFactor, false);
Kenton Varda's avatar
Kenton Varda committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
        } else {
          // This location contains the requested data plus other stuff.  Therefore the data cannot
          // possibly expand past the end of the space we've already marked used without either
          // overlapping with something else or breaking alignment rules.  We only have to combine
          // it with holes.
          return holes.tryExpand(oldLgSize, oldOffset, expansionFactor);
        }
      }

    private:
      bool isUsed;
      // Whether or not this location has been used at all by the group.

      uint8_t lgSizeUsed;
      // Amount of space from the location which is "used".  This is the minimum size needed to
      // cover all allocated space.  Only meaningful if `isUsed` is true.

      HoleSet<uint8_t> holes;
      // Indicates holes present in the space designated by `lgSizeUsed`.  The offsets in this
      // HoleSet are relative to the beginning of this particular data location, not the beginning
      // of the struct.

407 408
      bool tryExpandUsage(Group& group, Union::DataLocation& location, uint desiredUsage,
                          bool newHoles) {
Kenton Varda's avatar
Kenton Varda committed
409 410 411 412 413 414 415 416
        if (desiredUsage > location.lgSize) {
          // Need to expand the underlying slot.
          if (!location.tryExpandTo(group.parent, desiredUsage)) {
            return false;
          }
        }

        // Underlying slot is big enough, so expand our size and update holes.
417 418
        if (newHoles) {
          holes.addHolesAtEnd(lgSizeUsed, 1, desiredUsage);
419
        } else if (shouldDetectIssue344()) {
420 421 422 423 424 425 426 427 428 429 430
          // Unfortunately, Cap'n Proto 0.5.x and below would always call addHolesAtEnd(), which
          // was the wrong thing to do when called from tryExpand(), which itself is only called
          // in cases involving unions nested in other unions. The bug could lead to multiple
          // fields in a group incorrectly being assigned overlapping offsets. Although the bug
          // is now fixed by adding the `newHoles` parameter, this silently breaks
          // backwards-compatibilty with affected schemas. Therefore, for now, we throw an
          // exception to alert developers of the problem.
          //
          // TODO(cleanup): Once sufficient time has elapsed, remove this assert.
          KJ_FAIL_ASSERT("Bad news: Cap'n Proto 0.5.x and previous contained a bug which would cause this schema to be compiled incorrectly. Please see: https://github.com/sandstorm-io/capnproto/issues/344");
        }
Kenton Varda's avatar
Kenton Varda committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444
        lgSizeUsed = desiredUsage;
        return true;
      }
    };

    Union& parent;

    kj::Vector<DataLocationUsage> parentDataLocationUsage;
    // Vector corresponding to the parent union's `dataLocations`, indicating how much of each
    // location has already been allocated.

    uint parentPointerLocationUsage = 0;
    // Number of parent's pointer locations that have been used by this group.

445 446 447
    bool hasMembers = false;

    inline Group(Union& parent): parent(parent) {}
Kenton Varda's avatar
Kenton Varda committed
448 449
    KJ_DISALLOW_COPY(Group);

450
    void addMember() {
451 452 453 454 455 456
      if (!hasMembers) {
        hasMembers = true;
        parent.newGroupAddingFirstMember();
      }
    }

457 458 459 460 461 462 463 464 465 466
    void addVoid() override {
      addMember();

      // Make sure that if this is a member of a union which is in turn a member of another union,
      // that we let the outer union know that a field is being added, even though it is a
      // zero-size field. This is important because the union needs to allocate its discriminant
      // just before its second member is added.
      parent.parent.addVoid();
    }

Kenton Varda's avatar
Kenton Varda committed
467
    uint addData(uint lgSize) override {
468
      addMember();
469

470
      uint bestSize = kj::maxValue;
Kenton Varda's avatar
Kenton Varda committed
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
      kj::Maybe<uint> bestLocation = nullptr;

      for (uint i = 0; i < parent.dataLocations.size(); i++) {
        // If we haven't seen this DataLocation yet, add a corresponding DataLocationUsage.
        if (parentDataLocationUsage.size() == i) {
          parentDataLocationUsage.add();
        }

        auto& usage = parentDataLocationUsage[i];
        KJ_IF_MAYBE(hole, usage.smallestHoleAtLeast(parent.dataLocations[i], lgSize)) {
          if (*hole < bestSize) {
            bestSize = *hole;
            bestLocation = i;
          }
        }
      }

      KJ_IF_MAYBE(best, bestLocation) {
        return parentDataLocationUsage[*best].allocateFromHole(
            *this, parent.dataLocations[*best], lgSize);
      }

      // There are no holes at all in the union big enough to fit this field.  Go back through all
      // of the locations and attempt to expand them to fit.
      for (uint i = 0; i < parent.dataLocations.size(); i++) {
        KJ_IF_MAYBE(result, parentDataLocationUsage[i].tryAllocateByExpanding(
            *this, parent.dataLocations[i], lgSize)) {
          return *result;
        }
      }

      // Couldn't find any space in the existing locations, so add a new one.
      uint result = parent.addNewDataLocation(lgSize);
      parentDataLocationUsage.add(lgSize);
      return result;
    }

    uint addPointer() override {
509
      addMember();
510

Kenton Varda's avatar
Kenton Varda committed
511 512 513 514 515 516 517 518 519
      if (parentPointerLocationUsage < parent.pointerLocations.size()) {
        return parent.pointerLocations[parentPointerLocationUsage++];
      } else {
        parentPointerLocationUsage++;
        return parent.addNewPointerLocation();
      }
    }

    bool tryExpandData(uint oldLgSize, uint oldOffset, uint expansionFactor) override {
520
      bool mustFail = false;
Kenton Varda's avatar
Kenton Varda committed
521 522 523 524
      if (oldLgSize + expansionFactor > 6 ||
          (oldOffset & ((1 << expansionFactor) - 1)) != 0) {
        // Expansion is not possible because the new size is too large or the offset is not
        // properly-aligned.
525 526 527 528 529 530 531 532 533

        // Unfortunately, Cap'n Proto 0.5.x and prior forgot to "return false" here, instead
        // continuing to execute the rest of the method. In most cases, the method failed later
        // on, causing no harm. But, in cases where the method later succeeded, it probably
        // led to bogus layouts. We cannot simply add the return statement now as this would
        // silently break backwards-compatibility with affected schemas. Instead, we detect the
        // problem and throw an exception.
        //
        // TODO(cleanup): Once sufficient time has elapsed, switch to "return false;" here.
534 535 536 537 538
        if (shouldDetectIssue344()) {
          mustFail = true;
        } else {
          return false;
        }
Kenton Varda's avatar
Kenton Varda committed
539 540 541 542 543 544 545 546 547 548 549 550 551
      }

      for (uint i = 0; i < parentDataLocationUsage.size(); i++) {
        auto& location = parent.dataLocations[i];
        if (location.lgSize >= oldLgSize &&
            oldOffset >> (location.lgSize - oldLgSize) == location.offset) {
          // The location we're trying to expand is a subset of this data location.
          auto& usage = parentDataLocationUsage[i];

          // Adjust the offset to be only within this location.
          uint localOldOffset = oldOffset - (location.offset << (location.lgSize - oldLgSize));

          // Try to expand.
552 553 554 555 556 557
          bool result = usage.tryExpand(
              *this, location, oldLgSize, localOldOffset, expansionFactor);
          if (mustFail && result) {
            KJ_FAIL_ASSERT("Bad news: Cap'n Proto 0.5.x and previous contained a bug which would cause this schema to be compiled incorrectly. Please see: https://github.com/sandstorm-io/capnproto/issues/344");
          }
          return result;
Kenton Varda's avatar
Kenton Varda committed
558 559 560 561 562 563 564 565
        }
      }

      KJ_FAIL_ASSERT("Tried to expand field that was never allocated.");
      return false;
    }
  };

566
  Top& getTop() { return top; }
Kenton Varda's avatar
Kenton Varda committed
567 568 569 570 571 572 573

private:
  Top top;
};

// =======================================================================================

574
class NodeTranslator::BrandedDecl {
Kenton Varda's avatar
Kenton Varda committed
575
  // Represents a declaration possibly with generic parameter bindings.
576 577 578
  //
  // TODO(cleaup): This is too complicated to live here. We should refactor this class and
  //   BrandScope out into their own file, independent of NodeTranslator.
Kenton Varda's avatar
Kenton Varda committed
579 580

public:
581 582 583
  inline BrandedDecl(Resolver::ResolvedDecl decl,
                     kj::Own<NodeTranslator::BrandScope>&& brand,
                     Expression::Reader source)
584 585 586
      : brand(kj::mv(brand)), source(source) {
    body.init<Resolver::ResolvedDecl>(kj::mv(decl));
  }
587
  inline BrandedDecl(Resolver::ResolvedParameter variable, Expression::Reader source)
588 589 590
      : source(source) {
    body.init<Resolver::ResolvedParameter>(kj::mv(variable));
  }
591
  inline BrandedDecl(decltype(nullptr)) {}
Kenton Varda's avatar
Kenton Varda committed
592

593 594 595 596 597 598
  static BrandedDecl implicitMethodParam(uint index) {
    // Get a BrandedDecl referring to an implicit method parameter.
    // (As a hack, we internally represent this as a ResolvedParameter. Sorry.)
    return BrandedDecl(Resolver::ResolvedParameter { 0, index }, Expression::Reader());
  }

599 600
  BrandedDecl(BrandedDecl& other);
  BrandedDecl(BrandedDecl&& other) = default;
Kenton Varda's avatar
Kenton Varda committed
601

602 603
  BrandedDecl& operator=(BrandedDecl& other);
  BrandedDecl& operator=(BrandedDecl&& other) = default;
Kenton Varda's avatar
Kenton Varda committed
604

605 606 607 608
  // TODO(cleanup): A lot of the methods below are actually only called within compileAsType(),
  //   which was originally a method on NodeTranslator, but now is a method here and thus doesn't
  //   need these to be public. We should privatize most of these.

609
  kj::Maybe<BrandedDecl> applyParams(kj::Array<BrandedDecl> params, Expression::Reader subSource);
Kenton Varda's avatar
Kenton Varda committed
610 611
  // Treat the declaration as a generic and apply it to the given parameter list.

612
  kj::Maybe<BrandedDecl> getMember(kj::StringPtr memberName, Expression::Reader subSource);
Kenton Varda's avatar
Kenton Varda committed
613 614 615 616 617
  // Get a member of this declaration.

  kj::Maybe<Declaration::Which> getKind();
  // Returns the kind of declaration, or null if this is an unbound generic variable.

618
  template <typename InitBrandFunc>
619
  uint64_t getIdAndFillBrand(InitBrandFunc&& initBrand);
620 621 622
  // Returns the type ID of this node. `initBrand` is a zero-arg functor which returns
  // schema::Brand::Builder; this will be called if this decl has brand bindings, and
  // the returned builder filled in to reflect those bindings.
Kenton Varda's avatar
Kenton Varda committed
623 624 625
  //
  // It is an error to call this when `getKind()` returns null.

626
  kj::Maybe<BrandedDecl&> getListParam();
Kenton Varda's avatar
Kenton Varda committed
627 628 629 630 631 632 633 634
  // Only if the kind is BUILTIN_LIST: Get the list's type parameter.

  Resolver::ResolvedParameter asVariable();
  // If this is an unbound generic variable (i.e. `getKind()` returns null), return information
  // about the variable.
  //
  // It is an error to call this when `getKind()` does not return null.

635 636 637
  bool compileAsType(ErrorReporter& errorReporter, schema::Type::Builder target);
  // Compile this decl to a schema::Type.

Kenton Varda's avatar
Kenton Varda committed
638 639 640 641
  inline void addError(ErrorReporter& errorReporter, kj::StringPtr message) {
    errorReporter.addErrorOn(source, message);
  }

642 643 644 645
  Resolver::ResolveResult asResolveResult(uint64_t scopeId, schema::Brand::Builder brandBuilder);
  // Reverse this into a ResolveResult. If necessary, use `brandBuilder` to fill in
  // ResolvedDecl.brand.

Kenton Varda's avatar
Kenton Varda committed
646 647 648 649
  kj::String toString();
  kj::String toDebugString();

private:
650 651
  Resolver::ResolveResult body;
  kj::Own<NodeTranslator::BrandScope> brand;  // null if parameter
Kenton Varda's avatar
Kenton Varda committed
652 653 654
  Expression::Reader source;
};

655
class NodeTranslator::BrandScope: public kj::Refcounted {
656
  // Tracks the brand parameter bindings affecting the current scope. For example, if we are
David Renshaw's avatar
David Renshaw committed
657
  // interpreting the type expression "Foo(Text).Bar", we would start with the current scopes
658 659 660 661
  // BrandScope, create a new child BrandScope representing "Foo", add the "(Text)" parameter
  // bindings to it, then create a further child scope for "Bar". Thus the BrandScope for Bar
  // knows that Foo's parameter list has been bound to "(Text)".
  //
David Renshaw's avatar
David Renshaw committed
662
  // TODO(cleanup): This is too complicated to live here. We should refactor this class and
663 664
  //   BrandedDecl out into their own file, independent of NodeTranslator.

Kenton Varda's avatar
Kenton Varda committed
665
public:
666 667
  BrandScope(ErrorReporter& errorReporter, uint64_t startingScopeId,
             uint startingScopeParamCount, Resolver& startingScope)
668 669
      : errorReporter(errorReporter), parent(nullptr), leafId(startingScopeId),
        leafParamCount(startingScopeParamCount), inherited(true) {
670
    // Create all lexical parent scopes, all with no brand bindings.
671
    KJ_IF_MAYBE(p, startingScope.getParent()) {
672
      parent = kj::refcounted<BrandScope>(
673 674 675
          errorReporter, p->id, p->genericParamCount, *p->resolver);
    }
  }
Kenton Varda's avatar
Kenton Varda committed
676

677 678 679 680 681 682 683 684 685 686
  bool isGeneric() {
    if (leafParamCount > 0) return true;

    KJ_IF_MAYBE(p, parent) {
      return p->get()->isGeneric();
    } else {
      return false;
    }
  }

687 688
  kj::Own<BrandScope> push(uint64_t typeId, uint paramCount) {
    return kj::refcounted<BrandScope>(kj::addRef(*this), typeId, paramCount);
Kenton Varda's avatar
Kenton Varda committed
689 690
  }

691 692
  kj::Maybe<kj::Own<BrandScope>> setParams(
      kj::Array<BrandedDecl> params, Declaration::Which genericType, Expression::Reader source) {
Kenton Varda's avatar
Kenton Varda committed
693
    if (this->params.size() != 0) {
694 695 696
      errorReporter.addErrorOn(source, "Double-application of generic parameters.");
      return nullptr;
    } else if (params.size() > leafParamCount) {
697 698 699 700 701
      if (leafParamCount == 0) {
        errorReporter.addErrorOn(source, "Declaration does not accept generic parameters.");
      } else {
        errorReporter.addErrorOn(source, "Too many generic parameters.");
      }
Kenton Varda's avatar
Kenton Varda committed
702
      return nullptr;
703
    } else if (params.size() < leafParamCount) {
704
      errorReporter.addErrorOn(source, "Not enough generic parameters.");
Kenton Varda's avatar
Kenton Varda committed
705 706
      return nullptr;
    } else {
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
      if (genericType != Declaration::BUILTIN_LIST) {
        for (auto& param: params) {
          KJ_IF_MAYBE(kind, param.getKind()) {
            switch (*kind) {
              case Declaration::BUILTIN_LIST:
              case Declaration::BUILTIN_TEXT:
              case Declaration::BUILTIN_DATA:
              case Declaration::BUILTIN_ANY_POINTER:
              case Declaration::STRUCT:
              case Declaration::INTERFACE:
                break;

              default:
                param.addError(errorReporter,
                    "Sorry, only pointer types can be used as generic parameters.");
                break;
            }
          }
        }
      }

728
      return kj::refcounted<BrandScope>(*this, kj::mv(params));
Kenton Varda's avatar
Kenton Varda committed
729 730 731
    }
  }

732
  kj::Own<BrandScope> pop(uint64_t newLeafId) {
Kenton Varda's avatar
Kenton Varda committed
733
    if (leafId == newLeafId) {
734
      return kj::addRef(*this);
Kenton Varda's avatar
Kenton Varda committed
735 736 737 738
    }
    KJ_IF_MAYBE(p, parent) {
      return (*p)->pop(newLeafId);
    } else {
739
      // Looks like we're moving into a whole top-level scope.
740
      return kj::refcounted<BrandScope>(errorReporter, newLeafId);
Kenton Varda's avatar
Kenton Varda committed
741 742 743
    }
  }

744 745 746
  kj::Maybe<BrandedDecl> lookupParameter(Resolver& resolver, uint64_t scopeId, uint index) {
    // Returns null if the param should be inherited from the client scope.

Kenton Varda's avatar
Kenton Varda committed
747 748 749
    if (scopeId == leafId) {
      if (index < params.size()) {
        return params[index];
750
      } else if (inherited) {
Kenton Varda's avatar
Kenton Varda committed
751
        return nullptr;
752 753 754 755 756 757
      } else {
        // Unbound and not inherited, so return AnyPointer.
        auto decl = resolver.resolveBuiltin(Declaration::BUILTIN_ANY_POINTER);
        return BrandedDecl(decl,
            evaluateBrand(resolver, decl, List<schema::Brand::Scope>::Reader()),
            Expression::Reader());
Kenton Varda's avatar
Kenton Varda committed
758 759
      }
    } else KJ_IF_MAYBE(p, parent) {
760
      return p->get()->lookupParameter(resolver, scopeId, index);
Kenton Varda's avatar
Kenton Varda committed
761
    } else {
762
      KJ_FAIL_REQUIRE("scope is not a parent");
Kenton Varda's avatar
Kenton Varda committed
763 764 765
    }
  }

766 767 768
  kj::Maybe<kj::ArrayPtr<BrandedDecl>> getParams(uint64_t scopeId) {
    // Returns null if params at the requested scope should be inherited from the client scope.

Kenton Varda's avatar
Kenton Varda committed
769
    if (scopeId == leafId) {
770 771 772 773 774
      if (inherited) {
        return nullptr;
      } else {
        return params.asPtr();
      }
Kenton Varda's avatar
Kenton Varda committed
775 776 777
    } else KJ_IF_MAYBE(p, parent) {
      return p->get()->getParams(scopeId);
    } else {
778
      KJ_FAIL_REQUIRE("scope is not a parent");
Kenton Varda's avatar
Kenton Varda committed
779 780 781
    }
  }

782
  template <typename InitBrandFunc>
783
  void compile(InitBrandFunc&& initBrand) {
784 785
    kj::Vector<BrandScope*> levels;
    BrandScope* ptr = this;
Kenton Varda's avatar
Kenton Varda committed
786
    for (;;) {
787 788 789
      if (ptr->params.size() > 0 || (ptr->inherited && ptr->leafParamCount > 0)) {
        levels.add(ptr);
      }
Kenton Varda's avatar
Kenton Varda committed
790 791 792 793 794 795 796 797
      KJ_IF_MAYBE(p, ptr->parent) {
        ptr = *p;
      } else {
        break;
      }
    }

    if (levels.size() > 0) {
798
      auto scopes = initBrand().initScopes(levels.size());
Kenton Varda's avatar
Kenton Varda committed
799 800 801
      for (uint i: kj::indices(levels)) {
        auto scope = scopes[i];
        scope.setScopeId(levels[i]->leafId);
802 803 804 805 806 807

        if (levels[i]->inherited) {
          scope.setInherit();
        } else {
          auto bindings = scope.initBind(levels[i]->params.size());
          for (uint j: kj::indices(bindings)) {
808
            levels[i]->params[j].compileAsType(errorReporter, bindings[j].initType());
809
          }
Kenton Varda's avatar
Kenton Varda committed
810 811 812 813 814
        }
      }
    }
  }

815
  kj::Maybe<NodeTranslator::BrandedDecl> compileDeclExpression(
816 817
      Expression::Reader source, Resolver& resolver,
      ImplicitParams implicitMethodParams);
818 819 820 821 822 823 824 825 826 827 828 829

  NodeTranslator::BrandedDecl interpretResolve(
      Resolver& resolver, Resolver::ResolveResult& result, Expression::Reader source);

  kj::Own<NodeTranslator::BrandScope> evaluateBrand(
      Resolver& resolver, Resolver::ResolvedDecl decl,
      List<schema::Brand::Scope>::Reader brand, uint index = 0);

  BrandedDecl decompileType(Resolver& resolver, schema::Type::Reader type);

  inline uint64_t getScopeId() { return leafId; }

Kenton Varda's avatar
Kenton Varda committed
830
private:
831
  ErrorReporter& errorReporter;
832
  kj::Maybe<kj::Own<NodeTranslator::BrandScope>> parent;
Kenton Varda's avatar
Kenton Varda committed
833 834
  uint64_t leafId;                     // zero = this is the root
  uint leafParamCount;                 // number of generic parameters on this leaf
835
  bool inherited;
836
  kj::Array<BrandedDecl> params;
Kenton Varda's avatar
Kenton Varda committed
837

838
  BrandScope(kj::Own<NodeTranslator::BrandScope> parent, uint64_t leafId, uint leafParamCount)
839 840 841
      : errorReporter(parent->errorReporter),
        parent(kj::mv(parent)), leafId(leafId), leafParamCount(leafParamCount),
        inherited(false) {}
842
  BrandScope(BrandScope& base, kj::Array<BrandedDecl> params)
843 844 845
      : errorReporter(base.errorReporter),
        leafId(base.leafId), leafParamCount(base.leafParamCount),
        inherited(false), params(kj::mv(params)) {
Kenton Varda's avatar
Kenton Varda committed
846 847 848 849
    KJ_IF_MAYBE(p, base.parent) {
      parent = kj::addRef(**p);
    }
  }
850
  BrandScope(ErrorReporter& errorReporter, uint64_t scopeId)
851
      : errorReporter(errorReporter), leafId(scopeId), leafParamCount(0), inherited(false) {}
Kenton Varda's avatar
Kenton Varda committed
852 853 854 855 856

  template <typename T, typename... Params>
  friend kj::Own<T> kj::refcounted(Params&&... params);
};

857
NodeTranslator::BrandedDecl::BrandedDecl(BrandedDecl& other)
858
    : body(other.body),
Kenton Varda's avatar
Kenton Varda committed
859
      source(other.source) {
860
  if (body.is<Resolver::ResolvedDecl>()) {
861
    brand = kj::addRef(*other.brand);
Kenton Varda's avatar
Kenton Varda committed
862 863 864
  }
}

865
NodeTranslator::BrandedDecl& NodeTranslator::BrandedDecl::operator=(BrandedDecl& other) {
866
  body = other.body;
867
  source = other.source;
868
  if (body.is<Resolver::ResolvedDecl>()) {
869
    brand = kj::addRef(*other.brand);
Kenton Varda's avatar
Kenton Varda committed
870 871 872 873
  }
  return *this;
}

874 875
kj::Maybe<NodeTranslator::BrandedDecl> NodeTranslator::BrandedDecl::applyParams(
    kj::Array<BrandedDecl> params, Expression::Reader subSource) {
876
  if (body.is<Resolver::ResolvedParameter>()) {
Kenton Varda's avatar
Kenton Varda committed
877 878
    return nullptr;
  } else {
879
    return brand->setParams(kj::mv(params), body.get<Resolver::ResolvedDecl>().kind, subSource)
880
        .map([&](kj::Own<BrandScope>&& scope) {
881
      BrandedDecl result = *this;
882
      result.brand = kj::mv(scope);
Kenton Varda's avatar
Kenton Varda committed
883 884 885 886 887 888
      result.source = subSource;
      return result;
    });
  }
}

889
kj::Maybe<NodeTranslator::BrandedDecl> NodeTranslator::BrandedDecl::getMember(
890 891
    kj::StringPtr memberName, Expression::Reader subSource) {
  if (body.is<Resolver::ResolvedParameter>()) {
Kenton Varda's avatar
Kenton Varda committed
892
    return nullptr;
893 894
  } else KJ_IF_MAYBE(r, body.get<Resolver::ResolvedDecl>().resolver->resolveMember(memberName)) {
    return brand->interpretResolve(*body.get<Resolver::ResolvedDecl>().resolver, *r, subSource);
Kenton Varda's avatar
Kenton Varda committed
895 896 897 898 899
  } else {
    return nullptr;
  }
}

900
kj::Maybe<Declaration::Which> NodeTranslator::BrandedDecl::getKind() {
901
  if (body.is<Resolver::ResolvedParameter>()) {
Kenton Varda's avatar
Kenton Varda committed
902 903
    return nullptr;
  } else {
904
    return body.get<Resolver::ResolvedDecl>().kind;
Kenton Varda's avatar
Kenton Varda committed
905 906 907
  }
}

908
template <typename InitBrandFunc>
909 910
uint64_t NodeTranslator::BrandedDecl::getIdAndFillBrand(InitBrandFunc&& initBrand) {
  KJ_REQUIRE(body.is<Resolver::ResolvedDecl>());
Kenton Varda's avatar
Kenton Varda committed
911

912 913
  brand->compile(kj::fwd<InitBrandFunc>(initBrand));
  return body.get<Resolver::ResolvedDecl>().id;
Kenton Varda's avatar
Kenton Varda committed
914 915
}

916
kj::Maybe<NodeTranslator::BrandedDecl&> NodeTranslator::BrandedDecl::getListParam() {
917 918 919
  KJ_REQUIRE(body.is<Resolver::ResolvedDecl>());

  auto& decl = body.get<Resolver::ResolvedDecl>();
Kenton Varda's avatar
Kenton Varda committed
920 921
  KJ_REQUIRE(decl.kind == Declaration::BUILTIN_LIST);

922
  auto params = KJ_ASSERT_NONNULL(brand->getParams(decl.id));
923 924 925 926 927
  if (params.size() != 1) {
    return nullptr;
  } else {
    return params[0];
  }
Kenton Varda's avatar
Kenton Varda committed
928 929
}

930
NodeTranslator::Resolver::ResolvedParameter NodeTranslator::BrandedDecl::asVariable() {
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
  KJ_REQUIRE(body.is<Resolver::ResolvedParameter>());

  return body.get<Resolver::ResolvedParameter>();
}

bool NodeTranslator::BrandedDecl::compileAsType(
    ErrorReporter& errorReporter, schema::Type::Builder target) {
  KJ_IF_MAYBE(kind, getKind()) {
    switch (*kind) {
      case Declaration::ENUM: {
        auto enum_ = target.initEnum();
        enum_.setTypeId(getIdAndFillBrand([&]() { return enum_.initBrand(); }));
        return true;
      }

      case Declaration::STRUCT: {
        auto struct_ = target.initStruct();
        struct_.setTypeId(getIdAndFillBrand([&]() { return struct_.initBrand(); }));
        return true;
      }

      case Declaration::INTERFACE: {
        auto interface = target.initInterface();
        interface.setTypeId(getIdAndFillBrand([&]() { return interface.initBrand(); }));
        return true;
      }

      case Declaration::BUILTIN_LIST: {
        auto elementType = target.initList().initElementType();
960 961 962 963 964 965 966

        KJ_IF_MAYBE(param, getListParam()) {
          if (!param->compileAsType(errorReporter, elementType)) {
            return false;
          }
        } else {
          addError(errorReporter, "'List' requires exactly one parameter.");
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
          return false;
        }

        if (elementType.isAnyPointer()) {
          addError(errorReporter, "'List(AnyPointer)' is not supported.");
          // Seeing List(AnyPointer) later can mess things up, so change the type to Void.
          elementType.setVoid();
          return false;
        }

        return true;
      }

      case Declaration::BUILTIN_VOID: target.setVoid(); return true;
      case Declaration::BUILTIN_BOOL: target.setBool(); return true;
      case Declaration::BUILTIN_INT8: target.setInt8(); return true;
      case Declaration::BUILTIN_INT16: target.setInt16(); return true;
      case Declaration::BUILTIN_INT32: target.setInt32(); return true;
      case Declaration::BUILTIN_INT64: target.setInt64(); return true;
      case Declaration::BUILTIN_U_INT8: target.setUint8(); return true;
      case Declaration::BUILTIN_U_INT16: target.setUint16(); return true;
      case Declaration::BUILTIN_U_INT32: target.setUint32(); return true;
      case Declaration::BUILTIN_U_INT64: target.setUint64(); return true;
      case Declaration::BUILTIN_FLOAT32: target.setFloat32(); return true;
      case Declaration::BUILTIN_FLOAT64: target.setFloat64(); return true;
      case Declaration::BUILTIN_TEXT: target.setText(); return true;
      case Declaration::BUILTIN_DATA: target.setData(); return true;

      case Declaration::BUILTIN_OBJECT:
        addError(errorReporter,
            "As of Cap'n Proto 0.4, 'Object' has been renamed to 'AnyPointer'.  Sorry for the "
            "inconvenience, and thanks for being an early adopter.  :)");
        // no break
      case Declaration::BUILTIN_ANY_POINTER:
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
        target.initAnyPointer().initUnconstrained().setAnyKind();
        return true;
      case Declaration::BUILTIN_ANY_STRUCT:
        target.initAnyPointer().initUnconstrained().setStruct();
        return true;
      case Declaration::BUILTIN_ANY_LIST:
        target.initAnyPointer().initUnconstrained().setList();
        return true;
      case Declaration::BUILTIN_CAPABILITY:
        target.initAnyPointer().initUnconstrained().setCapability();
1011
        return true;
Kenton Varda's avatar
Kenton Varda committed
1012

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
      case Declaration::FILE:
      case Declaration::USING:
      case Declaration::CONST:
      case Declaration::ENUMERANT:
      case Declaration::FIELD:
      case Declaration::UNION:
      case Declaration::GROUP:
      case Declaration::METHOD:
      case Declaration::ANNOTATION:
      case Declaration::NAKED_ID:
      case Declaration::NAKED_ANNOTATION:
        addError(errorReporter, kj::str("'", toString(), "' is not a type."));
        return false;
    }

    KJ_UNREACHABLE;
  } else {
    // Oh, this is a type variable.
    auto var = asVariable();
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    if (var.id == 0) {
      // This is actually a method implicit parameter.
      auto builder = target.initAnyPointer().initImplicitMethodParameter();
      builder.setParameterIndex(var.index);
      return true;
    } else {
      auto builder = target.initAnyPointer().initParameter();
      builder.setScopeId(var.id);
      builder.setParameterIndex(var.index);
      return true;
    }
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
  }
}

NodeTranslator::Resolver::ResolveResult NodeTranslator::BrandedDecl::asResolveResult(
    uint64_t scopeId, schema::Brand::Builder brandBuilder) {
  auto result = body;
  if (result.is<Resolver::ResolvedDecl>()) {
    // May need to compile our context as the "brand".

    result.get<Resolver::ResolvedDecl>().scopeId = scopeId;

    getIdAndFillBrand([&]() {
      result.get<Resolver::ResolvedDecl>().brand = brandBuilder.asReader();
      return brandBuilder;
    });
  }
  return result;
Kenton Varda's avatar
Kenton Varda committed
1060 1061 1062 1063
}

static kj::String expressionString(Expression::Reader name);  // defined later

1064
kj::String NodeTranslator::BrandedDecl::toString() {
Kenton Varda's avatar
Kenton Varda committed
1065 1066 1067
  return expressionString(source);
}

1068
kj::String NodeTranslator::BrandedDecl::toDebugString() {
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
  if (body.is<Resolver::ResolvedParameter>()) {
    auto variable = body.get<Resolver::ResolvedParameter>();
    return kj::str("varibale(", variable.id, ", ", variable.index, ")");
  } else {
    auto decl = body.get<Resolver::ResolvedDecl>();
    return kj::str("decl(", decl.id, ", ", (uint)decl.kind, "')");
  }
}

NodeTranslator::BrandedDecl NodeTranslator::BrandScope::interpretResolve(
    Resolver& resolver, Resolver::ResolveResult& result, Expression::Reader source) {
  if (result.is<Resolver::ResolvedDecl>()) {
    auto& decl = result.get<Resolver::ResolvedDecl>();

    auto scope = pop(decl.scopeId);
    KJ_IF_MAYBE(brand, decl.brand) {
      scope = scope->evaluateBrand(resolver, decl, brand->getScopes());
    } else {
      scope = scope->push(decl.id, decl.genericParamCount);
    }

    return BrandedDecl(decl, kj::mv(scope), source);
  } else {
    auto& param = result.get<Resolver::ResolvedParameter>();
    KJ_IF_MAYBE(p, lookupParameter(resolver, param.id, param.index)) {
      return *p;
    } else {
      return BrandedDecl(param, source);
    }
  }
}

kj::Own<NodeTranslator::BrandScope> NodeTranslator::BrandScope::evaluateBrand(
    Resolver& resolver, Resolver::ResolvedDecl decl,
    List<schema::Brand::Scope>::Reader brand, uint index) {
  auto result = kj::refcounted<BrandScope>(errorReporter, decl.id);
  result->leafParamCount = decl.genericParamCount;

  // Fill in `params`.
  if (index < brand.size()) {
    auto nextScope = brand[index];
    if (decl.id == nextScope.getScopeId()) {
      // Initialize our parameters.

      switch (nextScope.which()) {
        case schema::Brand::Scope::BIND: {
          auto bindings = nextScope.getBind();
          auto params = kj::heapArrayBuilder<BrandedDecl>(bindings.size());
          for (auto binding: bindings) {
            switch (binding.which()) {
              case schema::Brand::Binding::UNBOUND: {
                // Build an AnyPointer-equivalent.
                auto anyPointerDecl = resolver.resolveBuiltin(Declaration::BUILTIN_ANY_POINTER);
                params.add(BrandedDecl(anyPointerDecl,
                    kj::refcounted<BrandScope>(errorReporter, anyPointerDecl.scopeId),
                    Expression::Reader()));
                break;
              }

              case schema::Brand::Binding::TYPE:
                // Reverse this schema::Type back into a BrandedDecl.
                params.add(decompileType(resolver, binding.getType()));
                break;
            }
          }
          result->params = params.finish();
          break;
        }

        case schema::Brand::Scope::INHERIT:
          KJ_IF_MAYBE(p, getParams(decl.id)) {
            result->params = kj::heapArray(*p);
          } else {
            result->inherited = true;
          }
          break;
      }

      // Parent should start one level deeper in the list.
      ++index;
    }
  }

  // Fill in `parent`.
  KJ_IF_MAYBE(parent, decl.resolver->getParent()) {
    result->parent = evaluateBrand(resolver, *parent, brand, index);
  }

  return result;
}

NodeTranslator::BrandedDecl NodeTranslator::BrandScope::decompileType(
    Resolver& resolver, schema::Type::Reader type) {
  auto builtin = [&](Declaration::Which which) -> BrandedDecl {
    auto decl = resolver.resolveBuiltin(which);
    return BrandedDecl(decl,
        evaluateBrand(resolver, decl, List<schema::Brand::Scope>::Reader()),
        Expression::Reader());
  };

  switch (type.which()) {
    case schema::Type::VOID:    return builtin(Declaration::BUILTIN_VOID);
    case schema::Type::BOOL:    return builtin(Declaration::BUILTIN_BOOL);
    case schema::Type::INT8:    return builtin(Declaration::BUILTIN_INT8);
    case schema::Type::INT16:   return builtin(Declaration::BUILTIN_INT16);
    case schema::Type::INT32:   return builtin(Declaration::BUILTIN_INT32);
    case schema::Type::INT64:   return builtin(Declaration::BUILTIN_INT64);
    case schema::Type::UINT8:   return builtin(Declaration::BUILTIN_U_INT8);
    case schema::Type::UINT16:  return builtin(Declaration::BUILTIN_U_INT16);
    case schema::Type::UINT32:  return builtin(Declaration::BUILTIN_U_INT32);
    case schema::Type::UINT64:  return builtin(Declaration::BUILTIN_U_INT64);
    case schema::Type::FLOAT32: return builtin(Declaration::BUILTIN_FLOAT32);
    case schema::Type::FLOAT64: return builtin(Declaration::BUILTIN_FLOAT64);
    case schema::Type::TEXT:    return builtin(Declaration::BUILTIN_TEXT);
    case schema::Type::DATA:    return builtin(Declaration::BUILTIN_DATA);

    case schema::Type::ENUM: {
      auto enumType = type.getEnum();
      Resolver::ResolvedDecl decl = resolver.resolveId(enumType.getTypeId());
      return BrandedDecl(decl,
          evaluateBrand(resolver, decl, enumType.getBrand().getScopes()),
          Expression::Reader());
    }

    case schema::Type::INTERFACE: {
      auto interfaceType = type.getInterface();
      Resolver::ResolvedDecl decl = resolver.resolveId(interfaceType.getTypeId());
      return BrandedDecl(decl,
          evaluateBrand(resolver, decl, interfaceType.getBrand().getScopes()),
          Expression::Reader());
    }

    case schema::Type::STRUCT: {
      auto structType = type.getStruct();
      Resolver::ResolvedDecl decl = resolver.resolveId(structType.getTypeId());
      return BrandedDecl(decl,
          evaluateBrand(resolver, decl, structType.getBrand().getScopes()),
          Expression::Reader());
    }

    case schema::Type::LIST: {
      auto elementType = decompileType(resolver, type.getList().getElementType());
      return KJ_ASSERT_NONNULL(builtin(Declaration::BUILTIN_LIST)
          .applyParams(kj::heapArray(&elementType, 1), Expression::Reader()));
    }

    case schema::Type::ANY_POINTER: {
      auto anyPointer = type.getAnyPointer();
      switch (anyPointer.which()) {
        case schema::Type::AnyPointer::UNCONSTRAINED:
          return builtin(Declaration::BUILTIN_ANY_POINTER);

        case schema::Type::AnyPointer::PARAMETER: {
          auto param = anyPointer.getParameter();
          auto id = param.getScopeId();
          uint index = param.getParameterIndex();
          KJ_IF_MAYBE(binding, lookupParameter(resolver, id, index)) {
            return *binding;
          } else {
            return BrandedDecl(Resolver::ResolvedParameter {id, index}, Expression::Reader());
          }
        }
1231 1232 1233

        case schema::Type::AnyPointer::IMPLICIT_METHOD_PARAMETER:
          KJ_FAIL_ASSERT("Alias pointed to implicit method type parameter?");
1234 1235 1236 1237 1238
      }

      KJ_UNREACHABLE;
    }
  }
Kenton Varda's avatar
Kenton Varda committed
1239 1240

  KJ_UNREACHABLE;
1241 1242 1243
}

kj::Maybe<NodeTranslator::BrandedDecl> NodeTranslator::BrandScope::compileDeclExpression(
1244 1245
    Expression::Reader source, Resolver& resolver,
    ImplicitParams implicitMethodParams) {
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
  switch (source.which()) {
    case Expression::UNKNOWN:
      // Error reported earlier.
      return nullptr;

    case Expression::POSITIVE_INT:
    case Expression::NEGATIVE_INT:
    case Expression::FLOAT:
    case Expression::STRING:
    case Expression::BINARY:
    case Expression::LIST:
    case Expression::TUPLE:
1258
    case Expression::EMBED:
1259 1260 1261 1262 1263
      errorReporter.addErrorOn(source, "Expected name.");
      return nullptr;

    case Expression::RELATIVE_NAME: {
      auto name = source.getRelativeName();
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
      auto nameValue = name.getValue();

      // Check implicit method params first.
      for (auto i: kj::indices(implicitMethodParams.params)) {
        if (implicitMethodParams.params[i].getName() == nameValue) {
          if (implicitMethodParams.scopeId == 0) {
            return BrandedDecl::implicitMethodParam(i);
          } else {
            return BrandedDecl(Resolver::ResolvedParameter {
                implicitMethodParams.scopeId, static_cast<uint16_t>(i) },
                Expression::Reader());
          }
        }
      }

      KJ_IF_MAYBE(r, resolver.resolve(nameValue)) {
1280 1281
        return interpretResolve(resolver, *r, source);
      } else {
1282
        errorReporter.addErrorOn(name, kj::str("Not defined: ", nameValue));
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
        return nullptr;
      }
    }

    case Expression::ABSOLUTE_NAME: {
      auto name = source.getAbsoluteName();
      KJ_IF_MAYBE(r, resolver.getTopScope().resolver->resolveMember(name.getValue())) {
        return interpretResolve(resolver, *r, source);
      } else {
        errorReporter.addErrorOn(name, kj::str("Not defined: ", name.getValue()));
        return nullptr;
      }
    }

    case Expression::IMPORT: {
      auto filename = source.getImport();
      KJ_IF_MAYBE(decl, resolver.resolveImport(filename.getValue())) {
1300
        // Import is always a root scope, so create a fresh BrandScope.
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
        return BrandedDecl(*decl, kj::refcounted<BrandScope>(
            errorReporter, decl->id, decl->genericParamCount, *decl->resolver), source);
      } else {
        errorReporter.addErrorOn(filename, kj::str("Import failed: ", filename.getValue()));
        return nullptr;
      }
    }

    case Expression::APPLICATION: {
      auto app = source.getApplication();
1311
      KJ_IF_MAYBE(decl, compileDeclExpression(app.getFunction(), resolver, implicitMethodParams)) {
1312 1313 1314 1315 1316 1317 1318 1319 1320
        // Compile all params.
        auto params = app.getParams();
        auto compiledParams = kj::heapArrayBuilder<BrandedDecl>(params.size());
        bool paramFailed = false;
        for (auto param: params) {
          if (param.isNamed()) {
            errorReporter.addErrorOn(param.getNamed(), "Named parameter not allowed here.");
          }

1321
          KJ_IF_MAYBE(d, compileDeclExpression(param.getValue(), resolver, implicitMethodParams)) {
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
            compiledParams.add(kj::mv(*d));
          } else {
            // Param failed to compile. Error was already reported.
            paramFailed = true;
          }
        };

        if (paramFailed) {
          return kj::mv(*decl);
        }

        // Add the parameters to the brand.
        KJ_IF_MAYBE(applied, decl->applyParams(compiledParams.finish(), source)) {
          return kj::mv(*applied);
        } else {
          // Error already reported. Ignore parameters.
          return kj::mv(*decl);
        }
      } else {
        // error already reported
        return nullptr;
      }
    }

    case Expression::MEMBER: {
      auto member = source.getMember();
1348
      KJ_IF_MAYBE(decl, compileDeclExpression(member.getParent(), resolver, implicitMethodParams)) {
1349 1350 1351 1352 1353
        auto name = member.getName();
        KJ_IF_MAYBE(memberDecl, decl->getMember(name.getValue(), source)) {
          return kj::mv(*memberDecl);
        } else {
          errorReporter.addErrorOn(name, kj::str(
1354 1355
              "'", expressionString(member.getParent()),
              "' has no member named '", name.getValue(), "'"));
1356 1357 1358 1359 1360 1361 1362 1363
          return nullptr;
        }
      } else {
        // error already reported
        return nullptr;
      }
    }
  }
Kenton Varda's avatar
Kenton Varda committed
1364 1365

  KJ_UNREACHABLE;
Kenton Varda's avatar
Kenton Varda committed
1366 1367 1368 1369
}

// =======================================================================================

Kenton Varda's avatar
Kenton Varda committed
1370
NodeTranslator::NodeTranslator(
1371
    Resolver& resolver, ErrorReporter& errorReporter,
Kenton Varda's avatar
Kenton Varda committed
1372
    const Declaration::Reader& decl, Orphan<schema::Node> wipNodeParam,
1373
    bool compileAnnotations)
Kenton Varda's avatar
Kenton Varda committed
1374
    : resolver(resolver), errorReporter(errorReporter),
1375
      orphanage(Orphanage::getForMessageContaining(wipNodeParam.get())),
1376
      compileAnnotations(compileAnnotations),
1377
      localBrand(kj::refcounted<BrandScope>(
1378 1379 1380
          errorReporter, wipNodeParam.getReader().getId(),
          decl.getParameters().size(), resolver)),
      wipNode(kj::mv(wipNodeParam)) {
Kenton Varda's avatar
Kenton Varda committed
1381 1382 1383
  compileNode(decl, wipNode.get());
}

Kenton Varda's avatar
Kenton Varda committed
1384
NodeTranslator::~NodeTranslator() noexcept(false) {}
1385

Kenton Varda's avatar
Kenton Varda committed
1386
NodeTranslator::NodeSet NodeTranslator::getBootstrapNode() {
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
  auto nodeReader = wipNode.getReader();
  if (nodeReader.isInterface()) {
    return NodeSet {
      nodeReader,
      KJ_MAP(g, paramStructs) { return g.getReader(); }
    };
  } else {
    return NodeSet {
      nodeReader,
      KJ_MAP(g, groups) { return g.getReader(); }
    };
  }
Kenton Varda's avatar
Kenton Varda committed
1399 1400 1401
}

NodeTranslator::NodeSet NodeTranslator::finish() {
Kenton Varda's avatar
Kenton Varda committed
1402 1403 1404 1405
  // Careful about iteration here:  compileFinalValue() may actually add more elements to
  // `unfinishedValues`, invalidating iterators in the process.
  for (size_t i = 0; i < unfinishedValues.size(); i++) {
    auto& value = unfinishedValues[i];
1406
    compileValue(value.source, value.type, value.typeScope, value.target, false);
Kenton Varda's avatar
Kenton Varda committed
1407 1408
  }

Kenton Varda's avatar
Kenton Varda committed
1409
  return getBootstrapNode();
Kenton Varda's avatar
Kenton Varda committed
1410 1411
}

1412 1413
class NodeTranslator::DuplicateNameDetector {
public:
1414
  inline explicit DuplicateNameDetector(ErrorReporter& errorReporter)
1415
      : errorReporter(errorReporter) {}
1416
  void check(List<Declaration>::Reader nestedDecls, Declaration::Which parentKind);
1417 1418

private:
1419
  ErrorReporter& errorReporter;
1420 1421 1422
  std::map<kj::StringPtr, LocatedText::Reader> names;
};

Kenton Varda's avatar
Kenton Varda committed
1423
void NodeTranslator::compileNode(Declaration::Reader decl, schema::Node::Builder builder) {
1424
  DuplicateNameDetector(errorReporter)
1425
      .check(decl.getNestedDecls(), decl.which());
Kenton Varda's avatar
Kenton Varda committed
1426

Kenton Varda's avatar
Kenton Varda committed
1427 1428 1429 1430 1431 1432 1433 1434
  auto genericParams = decl.getParameters();
  if (genericParams.size() > 0) {
    auto paramsBuilder = builder.initParameters(genericParams.size());
    for (auto i: kj::indices(genericParams)) {
      paramsBuilder[i].setName(genericParams[i].getName());
    }
  }

1435 1436
  builder.setIsGeneric(localBrand->isGeneric());

Kenton Varda's avatar
Kenton Varda committed
1437 1438
  kj::StringPtr targetsFlagName;

1439 1440
  switch (decl.which()) {
    case Declaration::FILE:
Kenton Varda's avatar
Kenton Varda committed
1441
      targetsFlagName = "targetsFile";
Kenton Varda's avatar
Kenton Varda committed
1442
      break;
1443 1444
    case Declaration::CONST:
      compileConst(decl.getConst(), builder.initConst());
Kenton Varda's avatar
Kenton Varda committed
1445
      targetsFlagName = "targetsConst";
Kenton Varda's avatar
Kenton Varda committed
1446
      break;
1447 1448
    case Declaration::ANNOTATION:
      compileAnnotation(decl.getAnnotation(), builder.initAnnotation());
Kenton Varda's avatar
Kenton Varda committed
1449
      targetsFlagName = "targetsAnnotation";
Kenton Varda's avatar
Kenton Varda committed
1450
      break;
1451 1452
    case Declaration::ENUM:
      compileEnum(decl.getEnum(), decl.getNestedDecls(), builder);
Kenton Varda's avatar
Kenton Varda committed
1453
      targetsFlagName = "targetsEnum";
Kenton Varda's avatar
Kenton Varda committed
1454
      break;
1455 1456
    case Declaration::STRUCT:
      compileStruct(decl.getStruct(), decl.getNestedDecls(), builder);
Kenton Varda's avatar
Kenton Varda committed
1457
      targetsFlagName = "targetsStruct";
Kenton Varda's avatar
Kenton Varda committed
1458
      break;
1459 1460
    case Declaration::INTERFACE:
      compileInterface(decl.getInterface(), decl.getNestedDecls(), builder);
Kenton Varda's avatar
Kenton Varda committed
1461
      targetsFlagName = "targetsInterface";
Kenton Varda's avatar
Kenton Varda committed
1462 1463 1464 1465 1466 1467 1468
      break;

    default:
      KJ_FAIL_REQUIRE("This Declaration is not a node.");
      break;
  }

Kenton Varda's avatar
Kenton Varda committed
1469
  builder.adoptAnnotations(compileAnnotationApplications(decl.getAnnotations(), targetsFlagName));
Kenton Varda's avatar
Kenton Varda committed
1470 1471
}

1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
static kj::StringPtr getExpressionTargetName(Expression::Reader exp) {
  kj::StringPtr targetName;
  switch (exp.which()) {
    case Expression::ABSOLUTE_NAME:
      return exp.getAbsoluteName().getValue();
    case Expression::RELATIVE_NAME:
      return exp.getRelativeName().getValue();
    case Expression::APPLICATION:
      return getExpressionTargetName(exp.getApplication().getFunction());
    case Expression::MEMBER:
      return exp.getMember().getName().getValue();
    default:
      return nullptr;
  }
}

1488
void NodeTranslator::DuplicateNameDetector::check(
1489
    List<Declaration>::Reader nestedDecls, Declaration::Which parentKind) {
Kenton Varda's avatar
Kenton Varda committed
1490 1491 1492 1493 1494 1495
  for (auto decl: nestedDecls) {
    {
      auto name = decl.getName();
      auto nameText = name.getValue();
      auto insertResult = names.insert(std::make_pair(nameText, name));
      if (!insertResult.second) {
1496
        if (nameText.size() == 0 && decl.isUnion()) {
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
          errorReporter.addErrorOn(
              name, kj::str("An unnamed union is already defined in this scope."));
          errorReporter.addErrorOn(
              insertResult.first->second, kj::str("Previously defined here."));
        } else {
          errorReporter.addErrorOn(
              name, kj::str("'", nameText, "' is already defined in this scope."));
          errorReporter.addErrorOn(
              insertResult.first->second, kj::str("'", nameText, "' previously defined here."));
        }
Kenton Varda's avatar
Kenton Varda committed
1507
      }
Kenton Varda's avatar
Kenton Varda committed
1508 1509

      switch (decl.which()) {
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
        case Declaration::USING: {
          kj::StringPtr targetName = getExpressionTargetName(decl.getUsing().getTarget());
          if (targetName.size() > 0 && targetName[0] >= 'a' && targetName[0] <= 'z') {
            // Target starts with lower-case letter, so alias should too.
            if (nameText.size() > 0 && (nameText[0] < 'a' || nameText[0] > 'z')) {
              errorReporter.addErrorOn(name,
                  "Non-type names must begin with a lower-case letter.");
            }
          } else {
            // Target starts with capital or is not named (probably, an import). Require
            // capitalization.
            if (nameText.size() > 0 && (nameText[0] < 'A' || nameText[0] > 'Z')) {
              errorReporter.addErrorOn(name,
                  "Type names must begin with a capital letter.");
            }
          }
          break;
        }

Kenton Varda's avatar
Kenton Varda committed
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
        case Declaration::ENUM:
        case Declaration::STRUCT:
        case Declaration::INTERFACE:
          if (nameText.size() > 0 && (nameText[0] < 'A' || nameText[0] > 'Z')) {
            errorReporter.addErrorOn(name,
                "Type names must begin with a capital letter.");
          }
          break;

        case Declaration::CONST:
        case Declaration::ANNOTATION:
        case Declaration::ENUMERANT:
        case Declaration::METHOD:
        case Declaration::FIELD:
        case Declaration::UNION:
        case Declaration::GROUP:
          if (nameText.size() > 0 && (nameText[0] < 'a' || nameText[0] > 'z')) {
            errorReporter.addErrorOn(name,
                "Non-type names must begin with a lower-case letter.");
          }
          break;

        default:
          KJ_ASSERT(nameText.size() == 0, "Don't know what naming rules to enforce for node type.",
                    (uint)decl.which());
          break;
      }

      if (nameText.findFirst('_') != nullptr) {
        errorReporter.addErrorOn(name,
            "Cap'n Proto declaration names should use camelCase and must not contain "
            "underscores. (Code generators may convert names to the appropriate style for the "
            "target language.)");
      }
Kenton Varda's avatar
Kenton Varda committed
1563 1564
    }

1565 1566 1567 1568 1569 1570 1571
    switch (decl.which()) {
      case Declaration::USING:
      case Declaration::CONST:
      case Declaration::ENUM:
      case Declaration::STRUCT:
      case Declaration::INTERFACE:
      case Declaration::ANNOTATION:
Kenton Varda's avatar
Kenton Varda committed
1572
        switch (parentKind) {
1573 1574 1575
          case Declaration::FILE:
          case Declaration::STRUCT:
          case Declaration::INTERFACE:
Kenton Varda's avatar
Kenton Varda committed
1576 1577 1578 1579 1580 1581 1582 1583
            // OK.
            break;
          default:
            errorReporter.addErrorOn(decl, "This kind of declaration doesn't belong here.");
            break;
        }
        break;

1584 1585
      case Declaration::ENUMERANT:
        if (parentKind != Declaration::ENUM) {
Kenton Varda's avatar
Kenton Varda committed
1586 1587 1588
          errorReporter.addErrorOn(decl, "Enumerants can only appear in enums.");
        }
        break;
1589 1590
      case Declaration::METHOD:
        if (parentKind != Declaration::INTERFACE) {
Kenton Varda's avatar
Kenton Varda committed
1591 1592 1593
          errorReporter.addErrorOn(decl, "Methods can only appear in interfaces.");
        }
        break;
1594 1595 1596
      case Declaration::FIELD:
      case Declaration::UNION:
      case Declaration::GROUP:
Kenton Varda's avatar
Kenton Varda committed
1597
        switch (parentKind) {
1598 1599 1600
          case Declaration::STRUCT:
          case Declaration::UNION:
          case Declaration::GROUP:
Kenton Varda's avatar
Kenton Varda committed
1601 1602 1603 1604 1605 1606
            // OK.
            break;
          default:
            errorReporter.addErrorOn(decl, "This declaration can only appear in structs.");
            break;
        }
1607 1608 1609 1610 1611

        // Struct members may have nested decls.  We need to check those here, because no one else
        // is going to do it.
        if (decl.getName().getValue().size() == 0) {
          // Unnamed union.  Check members as if they are in the same scope.
1612
          check(decl.getNestedDecls(), decl.which());
1613 1614 1615
        } else {
          // Children are in their own scope.
          DuplicateNameDetector(errorReporter)
1616
              .check(decl.getNestedDecls(), decl.which());
1617 1618
        }

Kenton Varda's avatar
Kenton Varda committed
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
        break;

      default:
        errorReporter.addErrorOn(decl, "This kind of declaration doesn't belong here.");
        break;
    }
  }
}

void NodeTranslator::compileConst(Declaration::Const::Reader decl,
Kenton Varda's avatar
Kenton Varda committed
1629
                                  schema::Node::Const::Builder builder) {
Kenton Varda's avatar
Kenton Varda committed
1630
  auto typeBuilder = builder.initType();
1631
  if (compileType(decl.getType(), typeBuilder, noImplicitParams())) {
Kenton Varda's avatar
Kenton Varda committed
1632 1633 1634 1635 1636
    compileBootstrapValue(decl.getValue(), typeBuilder.asReader(), builder.initValue());
  }
}

void NodeTranslator::compileAnnotation(Declaration::Annotation::Reader decl,
Kenton Varda's avatar
Kenton Varda committed
1637
                                       schema::Node::Annotation::Builder builder) {
1638
  compileType(decl.getType(), builder.initType(), noImplicitParams());
Kenton Varda's avatar
Kenton Varda committed
1639 1640 1641 1642

  // Dynamically copy over the values of all of the "targets" members.
  DynamicStruct::Reader src = decl;
  DynamicStruct::Builder dst = builder;
1643 1644 1645 1646 1647
  for (auto srcField: src.getSchema().getFields()) {
    kj::StringPtr fieldName = srcField.getProto().getName();
    if (fieldName.startsWith("targets")) {
      auto dstField = dst.getSchema().getFieldByName(fieldName);
      dst.set(dstField, src.get(srcField));
Kenton Varda's avatar
Kenton Varda committed
1648 1649 1650 1651 1652 1653
    }
  }
}

class NodeTranslator::DuplicateOrdinalDetector {
public:
1654
  DuplicateOrdinalDetector(ErrorReporter& errorReporter): errorReporter(errorReporter) {}
Kenton Varda's avatar
Kenton Varda committed
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668

  void check(LocatedInteger::Reader ordinal) {
    if (ordinal.getValue() < expectedOrdinal) {
      errorReporter.addErrorOn(ordinal, "Duplicate ordinal number.");
      KJ_IF_MAYBE(last, lastOrdinalLocation) {
        errorReporter.addErrorOn(
            *last, kj::str("Ordinal @", last->getValue(), " originally used here."));
        // Don't report original again.
        lastOrdinalLocation = nullptr;
      }
    } else if (ordinal.getValue() > expectedOrdinal) {
      errorReporter.addErrorOn(ordinal,
          kj::str("Skipped ordinal @", expectedOrdinal, ".  Ordinals must be sequential with no "
                  "holes."));
1669
      expectedOrdinal = ordinal.getValue() + 1;
Kenton Varda's avatar
Kenton Varda committed
1670 1671 1672 1673 1674 1675 1676
    } else {
      ++expectedOrdinal;
      lastOrdinalLocation = ordinal;
    }
  }

private:
1677
  ErrorReporter& errorReporter;
Kenton Varda's avatar
Kenton Varda committed
1678 1679 1680 1681
  uint expectedOrdinal = 0;
  kj::Maybe<LocatedInteger::Reader> lastOrdinalLocation;
};

1682
void NodeTranslator::compileEnum(Void decl,
Kenton Varda's avatar
Kenton Varda committed
1683
                                 List<Declaration>::Reader members,
Kenton Varda's avatar
Kenton Varda committed
1684
                                 schema::Node::Builder builder) {
Kenton Varda's avatar
Kenton Varda committed
1685 1686 1687 1688 1689
  // maps ordinal -> (code order, declaration)
  std::multimap<uint, std::pair<uint, Declaration::Reader>> enumerants;

  uint codeOrder = 0;
  for (auto member: members) {
1690
    if (member.isEnumerant()) {
Kenton Varda's avatar
Kenton Varda committed
1691 1692 1693 1694 1695 1696
      enumerants.insert(
          std::make_pair(member.getId().getOrdinal().getValue(),
                         std::make_pair(codeOrder++, member)));
    }
  }

1697
  auto list = builder.initEnum().initEnumerants(enumerants.size());
Kenton Varda's avatar
Kenton Varda committed
1698 1699 1700 1701 1702 1703 1704 1705 1706
  uint i = 0;
  DuplicateOrdinalDetector dupDetector(errorReporter);

  for (auto& entry: enumerants) {
    uint codeOrder = entry.second.first;
    Declaration::Reader enumerantDecl = entry.second.second;

    dupDetector.check(enumerantDecl.getId().getOrdinal());

1707
    auto enumerantBuilder = list[i++];
Kenton Varda's avatar
Kenton Varda committed
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
    enumerantBuilder.setName(enumerantDecl.getName().getValue());
    enumerantBuilder.setCodeOrder(codeOrder);
    enumerantBuilder.adoptAnnotations(compileAnnotationApplications(
        enumerantDecl.getAnnotations(), "targetsEnumerant"));
  }
}

// -------------------------------------------------------------------

class NodeTranslator::StructTranslator {
public:
1719 1720 1721
  explicit StructTranslator(NodeTranslator& translator, ImplicitParams implicitMethodParams)
      : translator(translator), errorReporter(translator.errorReporter),
        implicitMethodParams(implicitMethodParams) {}
Kenton Varda's avatar
Kenton Varda committed
1722 1723
  KJ_DISALLOW_COPY(StructTranslator);

Kenton Varda's avatar
Kenton Varda committed
1724
  void translate(Void decl, List<Declaration>::Reader members, schema::Node::Builder builder) {
Kenton Varda's avatar
Kenton Varda committed
1725 1726 1727
    // Build the member-info-by-ordinal map.
    MemberInfo root(builder);
    traverseTopOrGroup(members, root, layout.getTop());
1728 1729
    translateInternal(root, builder);
  }
Kenton Varda's avatar
Kenton Varda committed
1730

1731 1732 1733 1734 1735
  void translate(List<Declaration::Param>::Reader params, schema::Node::Builder builder) {
    // Build a struct from a method param / result list.
    MemberInfo root(builder);
    traverseParams(params, root, layout.getTop());
    translateInternal(root, builder);
Kenton Varda's avatar
Kenton Varda committed
1736 1737 1738 1739
  }

private:
  NodeTranslator& translator;
1740
  ErrorReporter& errorReporter;
1741
  ImplicitParams implicitMethodParams;
Kenton Varda's avatar
Kenton Varda committed
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751
  StructLayout layout;
  kj::Arena arena;

  struct MemberInfo {
    MemberInfo* parent;
    // The MemberInfo for the parent scope.

    uint codeOrder;
    // Code order within the parent.

1752 1753 1754
    uint index = 0;
    // Index within the parent.

Kenton Varda's avatar
Kenton Varda committed
1755 1756 1757
    uint childCount = 0;
    // Number of children this member has.

1758 1759 1760
    uint childInitializedCount = 0;
    // Number of children whose `schema` member has been initialized.  This initialization happens
    // while walking the fields in ordinal order.
Kenton Varda's avatar
Kenton Varda committed
1761

Kenton Varda's avatar
Kenton Varda committed
1762 1763 1764 1765 1766 1767 1768
    uint unionDiscriminantCount = 0;
    // Number of children who are members of the scope's union and have had their discriminant
    // value decided.

    bool isInUnion;
    // Whether or not this field is in the parent's union.

1769 1770 1771 1772 1773
    kj::StringPtr name;
    Declaration::Id::Reader declId;
    Declaration::Which declKind;
    bool isParam = false;
    bool hasDefaultValue = false;               // if declKind == FIELD
Kenton Varda's avatar
Kenton Varda committed
1774 1775
    Expression::Reader fieldType;               // if declKind == FIELD
    Expression::Reader fieldDefaultValue;       // if declKind == FIELD && hasDefaultValue
1776 1777 1778 1779 1780
    List<Declaration::AnnotationApplication>::Reader declAnnotations;
    uint startByte = 0;
    uint endByte = 0;
    // Information about the field declaration.  We don't use Declaration::Reader because it might
    // have come from a Declaration::Param instead.
Kenton Varda's avatar
Kenton Varda committed
1781

Kenton Varda's avatar
Kenton Varda committed
1782
    kj::Maybe<schema::Field::Builder> schema;
Kenton Varda's avatar
Kenton Varda committed
1783
    // Schema for the field.  Initialized when getSchema() is first called.
Kenton Varda's avatar
Kenton Varda committed
1784

Kenton Varda's avatar
Kenton Varda committed
1785
    schema::Node::Builder node;
Kenton Varda's avatar
Kenton Varda committed
1786
    // If it's a group, or the top-level struct.
1787

Kenton Varda's avatar
Kenton Varda committed
1788 1789 1790 1791 1792 1793
    union {
      StructLayout::StructOrGroup* fieldScope;
      // If this member is a field, the scope of that field.  This will be used to assign an
      // offset for the field when going through in ordinal order.

      StructLayout::Union* unionScope;
Kenton Varda's avatar
Kenton Varda committed
1794 1795 1796 1797
      // If this member is a union, or it is a group or top-level struct containing an unnamed
      // union, this is the union.  This will be used to assign a discriminant offset when the
      // union's ordinal comes up (if the union has an explicit ordinal), as well as to finally
      // copy over the discriminant offset to the schema.
Kenton Varda's avatar
Kenton Varda committed
1798 1799
    };

Kenton Varda's avatar
Kenton Varda committed
1800
    inline explicit MemberInfo(schema::Node::Builder node)
Kenton Varda's avatar
Kenton Varda committed
1801
        : parent(nullptr), codeOrder(0), isInUnion(false), node(node), unionScope(nullptr) {}
Kenton Varda's avatar
Kenton Varda committed
1802 1803
    inline MemberInfo(MemberInfo& parent, uint codeOrder,
                      const Declaration::Reader& decl,
Kenton Varda's avatar
Kenton Varda committed
1804 1805 1806
                      StructLayout::StructOrGroup& fieldScope,
                      bool isInUnion)
        : parent(&parent), codeOrder(codeOrder), isInUnion(isInUnion),
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
          name(decl.getName().getValue()), declId(decl.getId()), declKind(Declaration::FIELD),
          declAnnotations(decl.getAnnotations()),
          startByte(decl.getStartByte()), endByte(decl.getEndByte()),
          node(nullptr), fieldScope(&fieldScope) {
      KJ_REQUIRE(decl.which() == Declaration::FIELD);
      auto fieldDecl = decl.getField();
      fieldType = fieldDecl.getType();
      if (fieldDecl.getDefaultValue().isValue()) {
        hasDefaultValue = true;
        fieldDefaultValue = fieldDecl.getDefaultValue().getValue();
      }
    }
    inline MemberInfo(MemberInfo& parent, uint codeOrder,
                      const Declaration::Param::Reader& decl,
                      StructLayout::StructOrGroup& fieldScope,
                      bool isInUnion)
        : parent(&parent), codeOrder(codeOrder), isInUnion(isInUnion),
          name(decl.getName().getValue()), declKind(Declaration::FIELD), isParam(true),
          declAnnotations(decl.getAnnotations()),
          startByte(decl.getStartByte()), endByte(decl.getEndByte()),
          node(nullptr), fieldScope(&fieldScope) {
      fieldType = decl.getType();
      if (decl.getDefaultValue().isValue()) {
        hasDefaultValue = true;
        fieldDefaultValue = decl.getDefaultValue().getValue();
      }
    }
Kenton Varda's avatar
Kenton Varda committed
1834
    inline MemberInfo(MemberInfo& parent, uint codeOrder,
Kenton Varda's avatar
Kenton Varda committed
1835
                      const Declaration::Reader& decl,
Kenton Varda's avatar
Kenton Varda committed
1836
                      schema::Node::Builder node,
Kenton Varda's avatar
Kenton Varda committed
1837 1838
                      bool isInUnion)
        : parent(&parent), codeOrder(codeOrder), isInUnion(isInUnion),
1839 1840 1841 1842 1843 1844
          name(decl.getName().getValue()), declId(decl.getId()), declKind(decl.which()),
          declAnnotations(decl.getAnnotations()),
          startByte(decl.getStartByte()), endByte(decl.getEndByte()),
          node(node), unionScope(nullptr) {
      KJ_REQUIRE(decl.which() != Declaration::FIELD);
    }
Kenton Varda's avatar
Kenton Varda committed
1845

Kenton Varda's avatar
Kenton Varda committed
1846
    schema::Field::Builder getSchema() {
1847 1848 1849
      KJ_IF_MAYBE(result, schema) {
        return *result;
      } else {
1850
        index = parent->childInitializedCount;
Kenton Varda's avatar
Kenton Varda committed
1851 1852 1853 1854
        auto builder = parent->addMemberSchema();
        if (isInUnion) {
          builder.setDiscriminantValue(parent->unionDiscriminantCount++);
        }
1855
        builder.setName(name);
Kenton Varda's avatar
Kenton Varda committed
1856
        builder.setCodeOrder(codeOrder);
1857 1858 1859 1860 1861
        schema = builder;
        return builder;
      }
    }

Kenton Varda's avatar
Kenton Varda committed
1862
    schema::Field::Builder addMemberSchema() {
Kenton Varda's avatar
Kenton Varda committed
1863 1864 1865
      // Get the schema builder for the child member at the given index.  This lazily/dynamically
      // builds the builder tree.

Kenton Varda's avatar
Kenton Varda committed
1866
      KJ_REQUIRE(childInitializedCount < childCount);
Kenton Varda's avatar
Kenton Varda committed
1867

Kenton Varda's avatar
Kenton Varda committed
1868 1869
      auto structNode = node.getStruct();
      if (!structNode.hasFields()) {
Kenton Varda's avatar
Kenton Varda committed
1870 1871 1872
        if (parent != nullptr) {
          getSchema();  // Make sure field exists in parent once the first child is added.
        }
Kenton Varda's avatar
Kenton Varda committed
1873 1874 1875 1876 1877 1878
        return structNode.initFields(childCount)[childInitializedCount++];
      } else {
        return structNode.getFields()[childInitializedCount++];
      }
    }

Kenton Varda's avatar
Kenton Varda committed
1879
    void finishGroup() {
Kenton Varda's avatar
Kenton Varda committed
1880 1881 1882 1883 1884
      if (unionScope != nullptr) {
        unionScope->addDiscriminant();  // if it hasn't happened already
        auto structNode = node.getStruct();
        structNode.setDiscriminantCount(unionDiscriminantCount);
        structNode.setDiscriminantOffset(KJ_ASSERT_NONNULL(unionScope->discriminantOffset));
Kenton Varda's avatar
Kenton Varda committed
1885
      }
Kenton Varda's avatar
Kenton Varda committed
1886 1887 1888 1889

      if (parent != nullptr) {
        uint64_t groupId = generateGroupId(parent->node.getId(), index);
        node.setId(groupId);
1890
        node.setScopeId(parent->node.getId());
1891
        getSchema().initGroup().setTypeId(groupId);
Kenton Varda's avatar
Kenton Varda committed
1892
      }
Kenton Varda's avatar
Kenton Varda committed
1893 1894 1895 1896
    }
  };

  std::multimap<uint, MemberInfo*> membersByOrdinal;
Kenton Varda's avatar
Kenton Varda committed
1897 1898
  // Every member that has an explicit ordinal goes into this map.  We then iterate over the map
  // to assign field offsets (or discriminant offsets for unions).
Kenton Varda's avatar
Kenton Varda committed
1899

Kenton Varda's avatar
Kenton Varda committed
1900 1901
  kj::Vector<MemberInfo*> allMembers;
  // All members, including ones that don't have ordinals.
Kenton Varda's avatar
Kenton Varda committed
1902

1903 1904
  void traverseUnion(const Declaration::Reader& decl,
                     List<Declaration>::Reader members, MemberInfo& parent,
Kenton Varda's avatar
Kenton Varda committed
1905
                     StructLayout::Union& layout, uint& codeOrder) {
Kenton Varda's avatar
Kenton Varda committed
1906
    if (members.size() < 2) {
1907
      errorReporter.addErrorOn(decl, "Union must have at least two members.");
Kenton Varda's avatar
Kenton Varda committed
1908 1909 1910
    }

    for (auto member: members) {
Kenton Varda's avatar
Kenton Varda committed
1911
      kj::Maybe<uint> ordinal;
Kenton Varda's avatar
Kenton Varda committed
1912 1913
      MemberInfo* memberInfo = nullptr;

1914 1915
      switch (member.which()) {
        case Declaration::FIELD: {
Kenton Varda's avatar
Kenton Varda committed
1916 1917
          parent.childCount++;
          // For layout purposes, pretend this field is enclosed in a one-member group.
Kenton Varda's avatar
Kenton Varda committed
1918
          StructLayout::Group& singletonGroup =
Kenton Varda's avatar
Kenton Varda committed
1919 1920 1921 1922
              arena.allocate<StructLayout::Group>(layout);
          memberInfo = &arena.allocate<MemberInfo>(parent, codeOrder++, member, singletonGroup,
                                                   true);
          allMembers.add(memberInfo);
Kenton Varda's avatar
Kenton Varda committed
1923 1924 1925 1926
          ordinal = member.getId().getOrdinal().getValue();
          break;
        }

1927
        case Declaration::UNION:
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
          if (member.getName().getValue() == "") {
            errorReporter.addErrorOn(member, "Unions cannot contain unnamed unions.");
          } else {
            parent.childCount++;

            // For layout purposes, pretend this union is enclosed in a one-member group.
            StructLayout::Group& singletonGroup =
                arena.allocate<StructLayout::Group>(layout);
            StructLayout::Union& unionLayout = arena.allocate<StructLayout::Union>(singletonGroup);

            memberInfo = &arena.allocate<MemberInfo>(
                parent, codeOrder++, member,
                newGroupNode(parent.node, member.getName().getValue()),
                true);
            allMembers.add(memberInfo);
            memberInfo->unionScope = &unionLayout;
            uint subCodeOrder = 0;
1945
            traverseUnion(member, member.getNestedDecls(), *memberInfo, unionLayout, subCodeOrder);
1946 1947 1948 1949
            if (member.getId().isOrdinal()) {
              ordinal = member.getId().getOrdinal().getValue();
            }
          }
Kenton Varda's avatar
Kenton Varda committed
1950 1951
          break;

1952
        case Declaration::GROUP: {
Kenton Varda's avatar
Kenton Varda committed
1953 1954 1955 1956 1957 1958 1959 1960
          parent.childCount++;
          StructLayout::Group& group = arena.allocate<StructLayout::Group>(layout);
          memberInfo = &arena.allocate<MemberInfo>(
              parent, codeOrder++, member,
              newGroupNode(parent.node, member.getName().getValue()),
              true);
          allMembers.add(memberInfo);
          traverseGroup(member.getNestedDecls(), *memberInfo, group);
Kenton Varda's avatar
Kenton Varda committed
1961 1962 1963 1964 1965 1966 1967 1968
          break;
        }

        default:
          // Ignore others.
          break;
      }

Kenton Varda's avatar
Kenton Varda committed
1969 1970
      KJ_IF_MAYBE(o, ordinal) {
        membersByOrdinal.insert(std::make_pair(*o, memberInfo));
Kenton Varda's avatar
Kenton Varda committed
1971 1972 1973 1974
      }
    }
  }

Kenton Varda's avatar
Kenton Varda committed
1975 1976 1977
  void traverseGroup(List<Declaration>::Reader members, MemberInfo& parent,
                     StructLayout::StructOrGroup& layout) {
    if (members.size() < 1) {
1978 1979
      errorReporter.addError(parent.startByte, parent.endByte,
                             "Group must have at least one member.");
Kenton Varda's avatar
Kenton Varda committed
1980 1981
    }

Kenton Varda's avatar
Kenton Varda committed
1982
    traverseTopOrGroup(members, parent, layout);
1983 1984
  }

Kenton Varda's avatar
Kenton Varda committed
1985 1986
  void traverseTopOrGroup(List<Declaration>::Reader members, MemberInfo& parent,
                          StructLayout::StructOrGroup& layout) {
1987 1988
    uint codeOrder = 0;

Kenton Varda's avatar
Kenton Varda committed
1989
    for (auto member: members) {
Kenton Varda's avatar
Kenton Varda committed
1990
      kj::Maybe<uint> ordinal;
Kenton Varda's avatar
Kenton Varda committed
1991 1992
      MemberInfo* memberInfo = nullptr;

1993 1994
      switch (member.which()) {
        case Declaration::FIELD: {
Kenton Varda's avatar
Kenton Varda committed
1995
          parent.childCount++;
Kenton Varda's avatar
Kenton Varda committed
1996
          memberInfo = &arena.allocate<MemberInfo>(
Kenton Varda's avatar
Kenton Varda committed
1997 1998
              parent, codeOrder++, member, layout, false);
          allMembers.add(memberInfo);
1999
          ordinal = member.getId().getOrdinal().getValue();
Kenton Varda's avatar
Kenton Varda committed
2000 2001 2002
          break;
        }

2003
        case Declaration::UNION: {
Kenton Varda's avatar
Kenton Varda committed
2004 2005
          StructLayout::Union& unionLayout = arena.allocate<StructLayout::Union>(layout);

Kenton Varda's avatar
Kenton Varda committed
2006 2007
          uint independentSubCodeOrder = 0;
          uint* subCodeOrder = &independentSubCodeOrder;
Kenton Varda's avatar
Kenton Varda committed
2008 2009
          if (member.getName().getValue() == "") {
            memberInfo = &parent;
Kenton Varda's avatar
Kenton Varda committed
2010
            subCodeOrder = &codeOrder;
Kenton Varda's avatar
Kenton Varda committed
2011 2012 2013 2014 2015 2016 2017 2018 2019
          } else {
            parent.childCount++;
            memberInfo = &arena.allocate<MemberInfo>(
                parent, codeOrder++, member,
                newGroupNode(parent.node, member.getName().getValue()),
                false);
            allMembers.add(memberInfo);
          }
          memberInfo->unionScope = &unionLayout;
2020
          traverseUnion(member, member.getNestedDecls(), *memberInfo, unionLayout, *subCodeOrder);
2021
          if (member.getId().isOrdinal()) {
Kenton Varda's avatar
Kenton Varda committed
2022 2023 2024 2025 2026
            ordinal = member.getId().getOrdinal().getValue();
          }
          break;
        }

2027
        case Declaration::GROUP:
Kenton Varda's avatar
Kenton Varda committed
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
          parent.childCount++;
          memberInfo = &arena.allocate<MemberInfo>(
              parent, codeOrder++, member,
              newGroupNode(parent.node, member.getName().getValue()),
              false);
          allMembers.add(memberInfo);

          // Members of the group are laid out just like they were members of the parent, so we
          // just pass along the parent layout.
          traverseGroup(member.getNestedDecls(), *memberInfo, layout);

          // No ordinal for groups.
Kenton Varda's avatar
Kenton Varda committed
2040 2041 2042 2043 2044 2045 2046
          break;

        default:
          // Ignore others.
          break;
      }

Kenton Varda's avatar
Kenton Varda committed
2047 2048
      KJ_IF_MAYBE(o, ordinal) {
        membersByOrdinal.insert(std::make_pair(*o, memberInfo));
Kenton Varda's avatar
Kenton Varda committed
2049 2050
      }
    }
Kenton Varda's avatar
Kenton Varda committed
2051 2052
  }

2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
  void traverseParams(List<Declaration::Param>::Reader params, MemberInfo& parent,
                      StructLayout::StructOrGroup& layout) {
    for (uint i: kj::indices(params)) {
      auto param = params[i];
      parent.childCount++;
      MemberInfo* memberInfo = &arena.allocate<MemberInfo>(parent, i, param, layout, false);
      allMembers.add(memberInfo);
      membersByOrdinal.insert(std::make_pair(i, memberInfo));
    }
  }

Kenton Varda's avatar
Kenton Varda committed
2064
  schema::Node::Builder newGroupNode(schema::Node::Reader parent, kj::StringPtr name) {
2065
    auto orphan = translator.orphanage.newOrphan<schema::Node>();
Kenton Varda's avatar
Kenton Varda committed
2066
    auto node = orphan.get();
Kenton Varda's avatar
Kenton Varda committed
2067

2068
    // We'll set the ID and scope ID later.
Kenton Varda's avatar
Kenton Varda committed
2069 2070
    node.setDisplayName(kj::str(parent.getDisplayName(), '.', name));
    node.setDisplayNamePrefixLength(node.getDisplayName().size() - name.size());
2071
    node.setIsGeneric(parent.getIsGeneric());
Kenton Varda's avatar
Kenton Varda committed
2072 2073 2074 2075 2076 2077
    node.initStruct().setIsGroup(true);

    // The remaining contents of node.struct will be filled in later.

    translator.groups.add(kj::mv(orphan));
    return node;
Kenton Varda's avatar
Kenton Varda committed
2078
  }
2079 2080 2081 2082 2083 2084 2085 2086 2087

  void translateInternal(MemberInfo& root, schema::Node::Builder builder) {
    auto structBuilder = builder.initStruct();

    // Go through each member in ordinal order, building each member schema.
    DuplicateOrdinalDetector dupDetector(errorReporter);
    for (auto& entry: membersByOrdinal) {
      MemberInfo& member = *entry.second;

2088 2089 2090 2091
      // Make sure the exceptions added relating to
      // https://github.com/sandstorm-io/capnproto/issues/344 identify the affected field.
      KJ_CONTEXT(member.name);

2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
      if (member.declId.isOrdinal()) {
        dupDetector.check(member.declId.getOrdinal());
      }

      schema::Field::Builder fieldBuilder = member.getSchema();
      fieldBuilder.getOrdinal().setExplicit(entry.first);

      switch (member.declKind) {
        case Declaration::FIELD: {
          auto slot = fieldBuilder.initSlot();
          auto typeBuilder = slot.initType();
2103
          if (translator.compileType(member.fieldType, typeBuilder, implicitMethodParams)) {
2104 2105 2106
            if (member.hasDefaultValue) {
              translator.compileBootstrapValue(member.fieldDefaultValue,
                                               typeBuilder, slot.initDefaultValue());
2107
              slot.setHadExplicitDefault(true);
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
            } else {
              translator.compileDefaultDefaultValue(typeBuilder, slot.initDefaultValue());
            }
          } else {
            translator.compileDefaultDefaultValue(typeBuilder, slot.initDefaultValue());
          }

          int lgSize = -1;
          switch (typeBuilder.which()) {
            case schema::Type::VOID: lgSize = -1; break;
            case schema::Type::BOOL: lgSize = 0; break;
            case schema::Type::INT8: lgSize = 3; break;
            case schema::Type::INT16: lgSize = 4; break;
            case schema::Type::INT32: lgSize = 5; break;
            case schema::Type::INT64: lgSize = 6; break;
            case schema::Type::UINT8: lgSize = 3; break;
            case schema::Type::UINT16: lgSize = 4; break;
            case schema::Type::UINT32: lgSize = 5; break;
            case schema::Type::UINT64: lgSize = 6; break;
            case schema::Type::FLOAT32: lgSize = 5; break;
            case schema::Type::FLOAT64: lgSize = 6; break;

            case schema::Type::TEXT: lgSize = -2; break;
            case schema::Type::DATA: lgSize = -2; break;
            case schema::Type::LIST: lgSize = -2; break;
            case schema::Type::ENUM: lgSize = 4; break;
            case schema::Type::STRUCT: lgSize = -2; break;
            case schema::Type::INTERFACE: lgSize = -2; break;
2136
            case schema::Type::ANY_POINTER: lgSize = -2; break;
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198
          }

          if (lgSize == -2) {
            // pointer
            slot.setOffset(member.fieldScope->addPointer());
          } else if (lgSize == -1) {
            // void
            member.fieldScope->addVoid();
            slot.setOffset(0);
          } else {
            slot.setOffset(member.fieldScope->addData(lgSize));
          }
          break;
        }

        case Declaration::UNION:
          if (!member.unionScope->addDiscriminant()) {
            errorReporter.addErrorOn(member.declId.getOrdinal(),
                "Union ordinal, if specified, must be greater than no more than one of its "
                "member ordinals (i.e. there can only be one field retroactively unionized).");
          }
          break;

        case Declaration::GROUP:
          KJ_FAIL_ASSERT("Groups don't have ordinals.");
          break;

        default:
          KJ_FAIL_ASSERT("Unexpected member type.");
          break;
      }
    }

    // OK, we should have built all the members.  Now go through and make sure the discriminant
    // offsets have been copied over to the schemas and annotations have been applied.
    root.finishGroup();
    for (auto member: allMembers) {
      kj::StringPtr targetsFlagName;
      if (member->isParam) {
        targetsFlagName = "targetsParam";
      } else {
        switch (member->declKind) {
          case Declaration::FIELD:
            targetsFlagName = "targetsField";
            break;

          case Declaration::UNION:
            member->finishGroup();
            targetsFlagName = "targetsUnion";
            break;

          case Declaration::GROUP:
            member->finishGroup();
            targetsFlagName = "targetsGroup";
            break;

          default:
            KJ_FAIL_ASSERT("Unexpected member type.");
            break;
        }
      }

2199
      member->getSchema().adoptAnnotations(translator.compileAnnotationApplications(
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
          member->declAnnotations, targetsFlagName));
    }

    // And fill in the sizes.
    structBuilder.setDataWordCount(layout.getTop().dataWordCount);
    structBuilder.setPointerCount(layout.getTop().pointerCount);
    structBuilder.setPreferredListEncoding(schema::ElementSize::INLINE_COMPOSITE);

    for (auto& group: translator.groups) {
      auto groupBuilder = group.get().getStruct();
      groupBuilder.setDataWordCount(structBuilder.getDataWordCount());
      groupBuilder.setPointerCount(structBuilder.getPointerCount());
      groupBuilder.setPreferredListEncoding(structBuilder.getPreferredListEncoding());
    }
  }
Kenton Varda's avatar
Kenton Varda committed
2215 2216
};

2217
void NodeTranslator::compileStruct(Void decl, List<Declaration>::Reader members,
Kenton Varda's avatar
Kenton Varda committed
2218
                                   schema::Node::Builder builder) {
2219
  StructTranslator(*this, noImplicitParams()).translate(decl, members, builder);
Kenton Varda's avatar
Kenton Varda committed
2220 2221 2222 2223
}

// -------------------------------------------------------------------

Kenton Varda's avatar
Kenton Varda committed
2224
static kj::String expressionString(Expression::Reader name);
2225 2226 2227

void NodeTranslator::compileInterface(Declaration::Interface::Reader decl,
                                      List<Declaration>::Reader members,
Kenton Varda's avatar
Kenton Varda committed
2228
                                      schema::Node::Builder builder) {
2229 2230
  auto interfaceBuilder = builder.initInterface();

2231 2232 2233 2234
  auto superclassesDecl = decl.getSuperclasses();
  auto superclassesBuilder = interfaceBuilder.initSuperclasses(superclassesDecl.size());
  for (uint i: kj::indices(superclassesDecl)) {
    auto superclass = superclassesDecl[i];
Kenton Varda's avatar
Kenton Varda committed
2235

2236
    KJ_IF_MAYBE(decl, compileDeclExpression(superclass, noImplicitParams())) {
Kenton Varda's avatar
Kenton Varda committed
2237 2238
      KJ_IF_MAYBE(kind, decl->getKind()) {
        if (*kind == Declaration::INTERFACE) {
2239
          auto s = superclassesBuilder[i];
2240
          s.setId(decl->getIdAndFillBrand([&]() { return s.initBrand(); }));
Kenton Varda's avatar
Kenton Varda committed
2241 2242 2243 2244
        } else {
          decl->addError(errorReporter, kj::str(
            "'", decl->toString(), "' is not an interface."));
        }
2245
      } else {
Kenton Varda's avatar
Kenton Varda committed
2246 2247 2248 2249
        // A variable?
        decl->addError(errorReporter, kj::str(
            "'", decl->toString(), "' is an unbound generic parameter. Currently we don't support "
            "extending these."));
2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
      }
    }
  }

  // maps ordinal -> (code order, declaration)
  std::multimap<uint, std::pair<uint, Declaration::Reader>> methods;

  uint codeOrder = 0;
  for (auto member: members) {
    if (member.isMethod()) {
      methods.insert(
          std::make_pair(member.getId().getOrdinal().getValue(),
                         std::make_pair(codeOrder++, member)));
    }
  }

  auto list = interfaceBuilder.initMethods(methods.size());
  uint i = 0;
  DuplicateOrdinalDetector dupDetector(errorReporter);

  for (auto& entry: methods) {
    uint codeOrder = entry.second.first;
    Declaration::Reader methodDecl = entry.second.second;
    auto methodReader = methodDecl.getMethod();

2275 2276 2277
    auto ordinalDecl = methodDecl.getId().getOrdinal();
    dupDetector.check(ordinalDecl);
    uint16_t ordinal = ordinalDecl.getValue();
2278 2279 2280 2281 2282

    auto methodBuilder = list[i++];
    methodBuilder.setName(methodDecl.getName().getValue());
    methodBuilder.setCodeOrder(codeOrder);

2283 2284 2285 2286 2287 2288
    auto implicits = methodDecl.getParameters();
    auto implicitsBuilder = methodBuilder.initImplicitParameters(implicits.size());
    for (auto i: kj::indices(implicits)) {
      implicitsBuilder[i].setName(implicits[i].getName());
    }

2289
    methodBuilder.setParamStructType(compileParamList(
2290 2291
        methodDecl.getName().getValue(), ordinal, false,
        methodReader.getParams(), implicits,
2292
        [&]() { return methodBuilder.initParamBrand(); }));
2293

2294
    auto results = methodReader.getResults();
Kenton Varda's avatar
Kenton Varda committed
2295
    Declaration::ParamList::Reader resultList;
2296
    if (results.isExplicit()) {
Kenton Varda's avatar
Kenton Varda committed
2297
      resultList = results.getExplicit();
2298
    } else {
Kenton Varda's avatar
Kenton Varda committed
2299 2300 2301
      // We just stick with `resultList` uninitialized, which is equivalent to the default
      // instance. This works because `namedList` is the default kind of ParamList, and it will
      // default to an empty list.
2302
    }
Kenton Varda's avatar
Kenton Varda committed
2303
    methodBuilder.setResultStructType(compileParamList(
2304 2305
        methodDecl.getName().getValue(), ordinal, true,
        resultList, implicits,
2306
        [&]() { return methodBuilder.initResultBrand(); }));
2307 2308 2309 2310

    methodBuilder.adoptAnnotations(compileAnnotationApplications(
        methodDecl.getAnnotations(), "targetsMethod"));
  }
Kenton Varda's avatar
Kenton Varda committed
2311 2312
}

2313
template <typename InitBrandFunc>
2314 2315
uint64_t NodeTranslator::compileParamList(
    kj::StringPtr methodName, uint16_t ordinal, bool isResults,
2316
    Declaration::ParamList::Reader paramList,
2317
    typename List<Declaration::BrandParameter>::Reader implicitParams,
2318
    InitBrandFunc&& initBrand) {
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
  switch (paramList.which()) {
    case Declaration::ParamList::NAMED_LIST: {
      auto newStruct = orphanage.newOrphan<schema::Node>();
      auto builder = newStruct.get();
      auto parent = wipNode.getReader();

      kj::String typeName = kj::str(methodName, isResults ? "$Results" : "$Params");

      builder.setId(generateMethodParamsId(parent.getId(), ordinal, isResults));
      builder.setDisplayName(kj::str(parent.getDisplayName(), '.', typeName));
      builder.setDisplayNamePrefixLength(builder.getDisplayName().size() - typeName.size());
2330
      builder.setIsGeneric(parent.getIsGeneric() || implicitParams.size() > 0);
2331 2332 2333 2334
      builder.setScopeId(0);  // detached struct type

      builder.initStruct();

2335 2336 2337 2338 2339 2340
      // Note that the struct we create here has a brand parameter list mirrioring the method's
      // implicit parameter list. Of course, fields inside the struct using the method's implicit
      // params as types actually need to refer to them as regular params, so we create an
      // ImplicitParams with a scopeId here.
      StructTranslator(*this, ImplicitParams { builder.getId(), implicitParams })
          .translate(paramList.getNamedList(), builder);
2341 2342
      uint64_t id = builder.getId();
      paramStructs.add(kj::mv(newStruct));
2343

2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
      auto brand = localBrand->push(builder.getId(), implicitParams.size());

      if (implicitParams.size() > 0) {
        auto implicitDecls = kj::heapArrayBuilder<BrandedDecl>(implicitParams.size());
        auto implicitBuilder = builder.initParameters(implicitParams.size());

        for (auto i: kj::indices(implicitParams)) {
          auto param = implicitParams[i];
          implicitDecls.add(BrandedDecl::implicitMethodParam(i));
          implicitBuilder[i].setName(param.getName());
        }

        brand->setParams(implicitDecls.finish(), Declaration::STRUCT, Expression::Reader());
      }

      brand->compile(initBrand);
2360 2361 2362
      return id;
    }
    case Declaration::ParamList::TYPE:
2363 2364
      KJ_IF_MAYBE(target, compileDeclExpression(
          paramList.getType(), ImplicitParams { 0, implicitParams })) {
Kenton Varda's avatar
Kenton Varda committed
2365 2366
        KJ_IF_MAYBE(kind, target->getKind()) {
          if (*kind == Declaration::STRUCT) {
2367
            return target->getIdAndFillBrand(kj::fwd<InitBrandFunc>(initBrand));
Kenton Varda's avatar
Kenton Varda committed
2368 2369 2370 2371 2372
          } else {
            errorReporter.addErrorOn(
                paramList.getType(),
                kj::str("'", expressionString(paramList.getType()), "' is not a struct type."));
          }
2373
        } else {
Kenton Varda's avatar
Kenton Varda committed
2374 2375 2376 2377 2378
          // A variable?
          target->addError(errorReporter,
              "Cannot use generic parameter as whole input or output of a method. Instead, "
              "use a parameter/result list containing a field with this type.");
          return 0;
2379 2380 2381 2382 2383 2384 2385
        }
      }
      return 0;
  }
  KJ_UNREACHABLE;
}

Kenton Varda's avatar
Kenton Varda committed
2386 2387
// -------------------------------------------------------------------

Kenton Varda's avatar
Kenton Varda committed
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
static const char HEXDIGITS[] = "0123456789abcdef";

static kj::StringTree stringLiteral(kj::StringPtr chars) {
  // TODO(cleanup): This code keeps coming up. Put somewhere common?

  kj::Vector<char> escaped(chars.size());

  for (char c: chars) {
    switch (c) {
      case '\a': escaped.addAll(kj::StringPtr("\\a")); break;
      case '\b': escaped.addAll(kj::StringPtr("\\b")); break;
      case '\f': escaped.addAll(kj::StringPtr("\\f")); break;
      case '\n': escaped.addAll(kj::StringPtr("\\n")); break;
      case '\r': escaped.addAll(kj::StringPtr("\\r")); break;
      case '\t': escaped.addAll(kj::StringPtr("\\t")); break;
      case '\v': escaped.addAll(kj::StringPtr("\\v")); break;
      case '\'': escaped.addAll(kj::StringPtr("\\\'")); break;
      case '\"': escaped.addAll(kj::StringPtr("\\\"")); break;
      case '\\': escaped.addAll(kj::StringPtr("\\\\")); break;
      default:
        if (c < 0x20) {
          escaped.add('\\');
          escaped.add('x');
          uint8_t c2 = c;
          escaped.add(HEXDIGITS[c2 / 16]);
          escaped.add(HEXDIGITS[c2 % 16]);
        } else {
          escaped.add(c);
        }
        break;
    }
  }
  return kj::strTree('"', escaped, '"');
}

static kj::StringTree binaryLiteral(Data::Reader data) {
  kj::Vector<char> escaped(data.size() * 3);
Kenton Varda's avatar
Kenton Varda committed
2425

Kenton Varda's avatar
Kenton Varda committed
2426 2427 2428 2429
  for (byte b: data) {
    escaped.add(HEXDIGITS[b % 16]);
    escaped.add(HEXDIGITS[b / 16]);
    escaped.add(' ');
Kenton Varda's avatar
Kenton Varda committed
2430 2431
  }

Kenton Varda's avatar
Kenton Varda committed
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
  escaped.removeLast();
  return kj::strTree("0x\"", escaped, '"');
}

static kj::StringTree expressionStringTree(Expression::Reader exp);

static kj::StringTree tupleLiteral(List<Expression::Param>::Reader params) {
  auto parts = kj::heapArrayBuilder<kj::StringTree>(params.size());
  for (auto param: params) {
    auto part = expressionStringTree(param.getValue());
    if (param.isNamed()) {
      part = kj::strTree(param.getNamed().getValue(), " = ", kj::mv(part));
Kenton Varda's avatar
Kenton Varda committed
2444
    }
Kenton Varda's avatar
Kenton Varda committed
2445
    parts.add(kj::mv(part));
Kenton Varda's avatar
Kenton Varda committed
2446
  }
Kenton Varda's avatar
Kenton Varda committed
2447
  return kj::strTree("( ", kj::StringTree(parts.finish(), ", "), " )");
Kenton Varda's avatar
Kenton Varda committed
2448 2449
}

Kenton Varda's avatar
Kenton Varda committed
2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469
static kj::StringTree expressionStringTree(Expression::Reader exp) {
  switch (exp.which()) {
    case Expression::UNKNOWN:
      return kj::strTree("<parse error>");
    case Expression::POSITIVE_INT:
      return kj::strTree(exp.getPositiveInt());
    case Expression::NEGATIVE_INT:
      return kj::strTree('-', exp.getNegativeInt());
    case Expression::FLOAT:
      return kj::strTree(exp.getFloat());
    case Expression::STRING:
      return stringLiteral(exp.getString());
    case Expression::BINARY:
      return binaryLiteral(exp.getBinary());
    case Expression::RELATIVE_NAME:
      return kj::strTree(exp.getRelativeName().getValue());
    case Expression::ABSOLUTE_NAME:
      return kj::strTree('.', exp.getAbsoluteName().getValue());
    case Expression::IMPORT:
      return kj::strTree("import ", stringLiteral(exp.getImport().getValue()));
2470 2471
    case Expression::EMBED:
      return kj::strTree("embed ", stringLiteral(exp.getEmbed().getValue()));
Kenton Varda's avatar
Kenton Varda committed
2472 2473 2474 2475 2476 2477 2478 2479 2480

    case Expression::LIST: {
      auto list = exp.getList();
      auto parts = kj::heapArrayBuilder<kj::StringTree>(list.size());
      for (auto element: list) {
        parts.add(expressionStringTree(element));
      }
      return kj::strTree("[ ", kj::StringTree(parts.finish(), ", "), " ]");
    }
Kenton Varda's avatar
Kenton Varda committed
2481

Kenton Varda's avatar
Kenton Varda committed
2482 2483
    case Expression::TUPLE:
      return tupleLiteral(exp.getTuple());
Kenton Varda's avatar
Kenton Varda committed
2484

Kenton Varda's avatar
Kenton Varda committed
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
    case Expression::APPLICATION: {
      auto app = exp.getApplication();
      return kj::strTree(expressionStringTree(app.getFunction()),
                         '(', tupleLiteral(app.getParams()), ')');
    }

    case Expression::MEMBER: {
      auto member = exp.getMember();
      return kj::strTree(expressionStringTree(member.getParent()), '.',
                         member.getName().getValue());
    }
  }

  KJ_UNREACHABLE;
}

static kj::String expressionString(Expression::Reader name) {
  return expressionStringTree(name).flatten();
}

// -------------------------------------------------------------------

2507
kj::Maybe<NodeTranslator::BrandedDecl>
2508 2509 2510
NodeTranslator::compileDeclExpression(
    Expression::Reader source, ImplicitParams implicitMethodParams) {
  return localBrand->compileDeclExpression(source, resolver, implicitMethodParams);
Kenton Varda's avatar
Kenton Varda committed
2511
}
Kenton Varda's avatar
Kenton Varda committed
2512

2513 2514 2515 2516
/* static */ kj::Maybe<NodeTranslator::Resolver::ResolveResult> NodeTranslator::compileDecl(
    uint64_t scopeId, uint scopeParameterCount, Resolver& resolver, ErrorReporter& errorReporter,
    Expression::Reader expression, schema::Brand::Builder brandBuilder) {
  auto scope = kj::refcounted<BrandScope>(errorReporter, scopeId, scopeParameterCount, resolver);
2517
  KJ_IF_MAYBE(decl, scope->compileDeclExpression(expression, resolver, noImplicitParams())) {
2518
    return decl->asResolveResult(scope->getScopeId(), brandBuilder);
Kenton Varda's avatar
Kenton Varda committed
2519
  } else {
2520
    return nullptr;
Kenton Varda's avatar
Kenton Varda committed
2521 2522 2523
  }
}

2524 2525 2526
bool NodeTranslator::compileType(Expression::Reader source, schema::Type::Builder target,
                                 ImplicitParams implicitMethodParams) {
  KJ_IF_MAYBE(decl, compileDeclExpression(source, implicitMethodParams)) {
2527
    return decl->compileAsType(errorReporter, target);
Kenton Varda's avatar
Kenton Varda committed
2528
  } else {
2529
    return false;
Kenton Varda's avatar
Kenton Varda committed
2530 2531 2532 2533 2534 2535
  }
}

// -------------------------------------------------------------------

void NodeTranslator::compileDefaultDefaultValue(
Kenton Varda's avatar
Kenton Varda committed
2536
    schema::Type::Reader type, schema::Value::Builder target) {
Kenton Varda's avatar
Kenton Varda committed
2537
  switch (type.which()) {
Kenton Varda's avatar
Kenton Varda committed
2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551
    case schema::Type::VOID: target.setVoid(); break;
    case schema::Type::BOOL: target.setBool(false); break;
    case schema::Type::INT8: target.setInt8(0); break;
    case schema::Type::INT16: target.setInt16(0); break;
    case schema::Type::INT32: target.setInt32(0); break;
    case schema::Type::INT64: target.setInt64(0); break;
    case schema::Type::UINT8: target.setUint8(0); break;
    case schema::Type::UINT16: target.setUint16(0); break;
    case schema::Type::UINT32: target.setUint32(0); break;
    case schema::Type::UINT64: target.setUint64(0); break;
    case schema::Type::FLOAT32: target.setFloat32(0); break;
    case schema::Type::FLOAT64: target.setFloat64(0); break;
    case schema::Type::ENUM: target.setEnum(0); break;
    case schema::Type::INTERFACE: target.setInterface(); break;
Kenton Varda's avatar
Kenton Varda committed
2552

2553
    // Bit of a hack:  For Text/Data, we adopt a null orphan, which sets the field to null.
Kenton Varda's avatar
Kenton Varda committed
2554
    // TODO(cleanup):  Create a cleaner way to do this.
Kenton Varda's avatar
Kenton Varda committed
2555 2556
    case schema::Type::TEXT: target.adoptText(Orphan<Text>()); break;
    case schema::Type::DATA: target.adoptData(Orphan<Data>()); break;
2557 2558
    case schema::Type::STRUCT: target.initStruct(); break;
    case schema::Type::LIST: target.initList(); break;
2559
    case schema::Type::ANY_POINTER: target.initAnyPointer(); break;
Kenton Varda's avatar
Kenton Varda committed
2560
  }
Kenton Varda's avatar
Kenton Varda committed
2561 2562
}

2563 2564 2565
void NodeTranslator::compileBootstrapValue(
    Expression::Reader source, schema::Type::Reader type, schema::Value::Builder target,
    Schema typeScope) {
2566 2567 2568 2569
  // Start by filling in a default default value so that if for whatever reason we don't end up
  // initializing the value, this won't cause schema validation to fail.
  compileDefaultDefaultValue(type, target);

Kenton Varda's avatar
Kenton Varda committed
2570
  switch (type.which()) {
Kenton Varda's avatar
Kenton Varda committed
2571 2572 2573
    case schema::Type::LIST:
    case schema::Type::STRUCT:
    case schema::Type::INTERFACE:
2574
    case schema::Type::ANY_POINTER:
2575
      unfinishedValues.add(UnfinishedValue { source, type, typeScope, target });
2576 2577
      break;

Kenton Varda's avatar
Kenton Varda committed
2578
    default:
2579
      // Primitive value.
2580
      compileValue(source, type, typeScope, target, true);
Kenton Varda's avatar
Kenton Varda committed
2581 2582
      break;
  }
2583 2584
}

Kenton Varda's avatar
Kenton Varda committed
2585
void NodeTranslator::compileValue(Expression::Reader source, schema::Type::Reader type,
2586 2587
                                  Schema typeScope, schema::Value::Builder target,
                                  bool isBootstrap) {
2588 2589 2590 2591 2592
  class ResolverGlue: public ValueTranslator::Resolver {
  public:
    inline ResolverGlue(NodeTranslator& translator, bool isBootstrap)
        : translator(translator), isBootstrap(isBootstrap) {}

Kenton Varda's avatar
Kenton Varda committed
2593
    kj::Maybe<DynamicValue::Reader> resolveConstant(Expression::Reader name) override {
2594 2595 2596
      return translator.readConstant(name, isBootstrap);
    }

2597 2598 2599 2600
    kj::Maybe<kj::Array<const byte>> readEmbed(LocatedText::Reader filename) override {
      return translator.readEmbed(filename);
    }

2601 2602 2603 2604 2605 2606 2607 2608
  private:
    NodeTranslator& translator;
    bool isBootstrap;
  };

  ResolverGlue glue(*this, isBootstrap);
  ValueTranslator valueTranslator(glue, errorReporter, orphanage);

2609 2610 2611 2612 2613 2614 2615 2616 2617 2618
  KJ_IF_MAYBE(typeSchema, resolver.resolveBootstrapType(type, typeScope)) {
    kj::StringPtr fieldName = Schema::from<schema::Type>()
        .getUnionFields()[static_cast<uint>(typeSchema->which())].getProto().getName();

    KJ_IF_MAYBE(value, valueTranslator.compileValue(source, *typeSchema)) {
      if (typeSchema->isEnum()) {
        target.setEnum(value->getReader().as<DynamicEnum>().getRaw());
      } else {
        toDynamic(target).adopt(fieldName, kj::mv(*value));
      }
2619 2620 2621 2622
    }
  }
}

2623
kj::Maybe<Orphan<DynamicValue>> ValueTranslator::compileValue(Expression::Reader src, Type type) {
2624
  Orphan<DynamicValue> result = compileValueInner(src, type);
2625 2626 2627 2628 2629 2630 2631 2632 2633

  switch (result.getType()) {
    case DynamicValue::UNKNOWN:
      // Error already reported.
      return nullptr;

    case DynamicValue::VOID:
      if (type.isVoid()) {
        return kj::mv(result);
2634 2635
      }
      break;
2636 2637 2638 2639

    case DynamicValue::BOOL:
      if (type.isBool()) {
        return kj::mv(result);
2640 2641
      }
      break;
2642 2643 2644 2645 2646 2647

    case DynamicValue::INT: {
      int64_t value = result.getReader().as<int64_t>();
      if (value < 0) {
        int64_t minValue = 1;
        switch (type.which()) {
2648 2649 2650 2651 2652 2653 2654 2655
          case schema::Type::INT8: minValue = (int8_t)kj::minValue; break;
          case schema::Type::INT16: minValue = (int16_t)kj::minValue; break;
          case schema::Type::INT32: minValue = (int32_t)kj::minValue; break;
          case schema::Type::INT64: minValue = (int64_t)kj::minValue; break;
          case schema::Type::UINT8: minValue = (uint8_t)kj::minValue; break;
          case schema::Type::UINT16: minValue = (uint16_t)kj::minValue; break;
          case schema::Type::UINT32: minValue = (uint32_t)kj::minValue; break;
          case schema::Type::UINT64: minValue = (uint64_t)kj::minValue; break;
2656 2657 2658 2659

          case schema::Type::FLOAT32:
          case schema::Type::FLOAT64:
            // Any integer is acceptable.
2660
            minValue = (int64_t)kj::minValue;
2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679
            break;

          default: break;
        }
        if (minValue == 1) break;

        if (value < minValue) {
          errorReporter.addErrorOn(src, "Integer value out of range.");
          result = minValue;
        }
        return kj::mv(result);
      }

      // No break -- value is positive, so we can just go on to the uint case below.
    }

    case DynamicValue::UINT: {
      uint64_t maxValue = 0;
      switch (type.which()) {
2680 2681 2682 2683 2684 2685 2686 2687
        case schema::Type::INT8: maxValue = (int8_t)kj::maxValue; break;
        case schema::Type::INT16: maxValue = (int16_t)kj::maxValue; break;
        case schema::Type::INT32: maxValue = (int32_t)kj::maxValue; break;
        case schema::Type::INT64: maxValue = (int64_t)kj::maxValue; break;
        case schema::Type::UINT8: maxValue = (uint8_t)kj::maxValue; break;
        case schema::Type::UINT16: maxValue = (uint16_t)kj::maxValue; break;
        case schema::Type::UINT32: maxValue = (uint32_t)kj::maxValue; break;
        case schema::Type::UINT64: maxValue = (uint64_t)kj::maxValue; break;
2688 2689 2690 2691

        case schema::Type::FLOAT32:
        case schema::Type::FLOAT64:
          // Any integer is acceptable.
2692
          maxValue = (uint64_t)kj::maxValue;
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
          break;

        default: break;
      }
      if (maxValue == 0) break;

      if (result.getReader().as<uint64_t>() > maxValue) {
        errorReporter.addErrorOn(src, "Integer value out of range.");
        result = maxValue;
      }
      return kj::mv(result);
    }

    case DynamicValue::FLOAT:
      if (type.isFloat32() || type.isFloat64()) {
        return kj::mv(result);
2709 2710
      }
      break;
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721

    case DynamicValue::TEXT:
      if (type.isText()) {
        return kj::mv(result);
      }
      break;

    case DynamicValue::DATA:
      if (type.isData()) {
        return kj::mv(result);
      }
2722
      break;
2723

2724 2725
    case DynamicValue::LIST:
      if (type.isList()) {
2726 2727
        if (result.getReader().as<DynamicList>().getSchema() == type.asList()) {
          return kj::mv(result);
2728
        }
2729 2730 2731 2732 2733 2734 2735 2736 2737
      } else if (type.isAnyPointer()) {
        switch (type.whichAnyPointerKind()) {
          case schema::Type::AnyPointer::Unconstrained::ANY_KIND:
          case schema::Type::AnyPointer::Unconstrained::LIST:
            return kj::mv(result);
          case schema::Type::AnyPointer::Unconstrained::STRUCT:
          case schema::Type::AnyPointer::Unconstrained::CAPABILITY:
            break;
        }
2738 2739 2740 2741 2742
      }
      break;

    case DynamicValue::ENUM:
      if (type.isEnum()) {
2743 2744
        if (result.getReader().as<DynamicEnum>().getSchema() == type.asEnum()) {
          return kj::mv(result);
2745 2746 2747 2748 2749 2750
        }
      }
      break;

    case DynamicValue::STRUCT:
      if (type.isStruct()) {
2751 2752
        if (result.getReader().as<DynamicStruct>().getSchema() == type.asStruct()) {
          return kj::mv(result);
2753
        }
2754 2755 2756 2757 2758 2759 2760 2761 2762
      } else if (type.isAnyPointer()) {
        switch (type.whichAnyPointerKind()) {
          case schema::Type::AnyPointer::Unconstrained::ANY_KIND:
          case schema::Type::AnyPointer::Unconstrained::STRUCT:
            return kj::mv(result);
          case schema::Type::AnyPointer::Unconstrained::LIST:
          case schema::Type::AnyPointer::Unconstrained::CAPABILITY:
            break;
        }
2763 2764 2765
      }
      break;

2766
    case DynamicValue::CAPABILITY:
2767 2768
      KJ_FAIL_ASSERT("Interfaces can't have literal values.");

2769 2770
    case DynamicValue::ANY_POINTER:
      KJ_FAIL_ASSERT("AnyPointers can't have literal values.");
2771
  }
2772 2773 2774

  errorReporter.addErrorOn(src, kj::str("Type mismatch; expected ", makeTypeName(type), "."));
  return nullptr;
2775
}
Kenton Varda's avatar
Kenton Varda committed
2776

2777
Orphan<DynamicValue> ValueTranslator::compileValueInner(Expression::Reader src, Type type) {
2778
  switch (src.which()) {
Kenton Varda's avatar
Kenton Varda committed
2779 2780 2781 2782 2783 2784 2785
    case Expression::RELATIVE_NAME: {
      auto name = src.getRelativeName();

      // The name is just a bare identifier.  It may be a literal value or an enumerant.
      kj::StringPtr id = name.getValue();

      if (type.isEnum()) {
2786 2787
        KJ_IF_MAYBE(enumerant, type.asEnum().findEnumerantByName(id)) {
          return DynamicEnum(*enumerant);
Kenton Varda's avatar
Kenton Varda committed
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800
        }
      } else {
        // Interpret known constant values.
        if (id == "void") {
          return VOID;
        } else if (id == "true") {
          return true;
        } else if (id == "false") {
          return false;
        } else if (id == "nan") {
          return kj::nan();
        } else if (id == "inf") {
          return kj::inf();
2801 2802
        }
      }
Kenton Varda's avatar
Kenton Varda committed
2803

Kenton Varda's avatar
Kenton Varda committed
2804 2805
      // Apparently not a literal. Try resolving it.
      KJ_IF_MAYBE(constValue, resolver.resolveConstant(src)) {
2806
        return orphanage.newOrphanCopy(*constValue);
Kenton Varda's avatar
Kenton Varda committed
2807 2808
      } else {
        return nullptr;
Kenton Varda's avatar
Kenton Varda committed
2809 2810
      }
    }
Kenton Varda's avatar
Kenton Varda committed
2811

Kenton Varda's avatar
Kenton Varda committed
2812 2813 2814 2815 2816 2817 2818 2819 2820 2821
    case Expression::ABSOLUTE_NAME:
    case Expression::IMPORT:
    case Expression::APPLICATION:
    case Expression::MEMBER:
      KJ_IF_MAYBE(constValue, resolver.resolveConstant(src)) {
        return orphanage.newOrphanCopy(*constValue);
      } else {
        return nullptr;
      }

2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
    case Expression::EMBED:
      KJ_IF_MAYBE(data, resolver.readEmbed(src.getEmbed())) {
        switch (type.which()) {
          case schema::Type::TEXT: {
            // Sadly, we need to make a copy to add the NUL terminator.
            auto text = orphanage.newOrphan<Text>(data->size());
            memcpy(text.get().begin(), data->begin(), data->size());
            return kj::mv(text);
          }
          case schema::Type::DATA:
            // TODO(perf): It would arguably be neat to use orphanage.referenceExternalData(),
            //   since typically the data is mmap()ed and this would avoid forcing a large file
            //   to become memory-resident. However, we'd have to figure out who should own the
            //   Array<byte>. Also, we'd have to deal with the possibility of misaligned data --
            //   though arguably in that case we know it's not mmap()ed so whatever. One more
            //   thing: it would be neat to be able to reference text blobs this way too, if only
            //   we could rely on the assumption that as long as the data doesn't end on a page
            //   boundary, it will be zero-padded, thus giving us our NUL terminator (4095/4096 of
            //   the time), but this seems to require documenting constraints on the underlying
            //   file-reading interfaces. Hm.
            return orphanage.newOrphanCopy(Data::Reader(*data));
          case schema::Type::STRUCT: {
            // We will almost certainly
            if (data->size() % sizeof(word) != 0) {
              errorReporter.addErrorOn(src,
                  "Embedded file is not a valid Cap'n Proto message.");
              return nullptr;
            }
            kj::Array<word> copy;
            kj::ArrayPtr<const word> words;
            if (reinterpret_cast<uintptr_t>(data->begin()) % sizeof(void*) == 0) {
              // Hooray, data is aligned.
              words = kj::ArrayPtr<const word>(
                  reinterpret_cast<const word*>(data->begin()),
                  data->size() / sizeof(word));
            } else {
              // Ugh, data not aligned. Make a copy.
              copy = kj::heapArray<word>(data->size() / sizeof(word));
              memcpy(copy.begin(), data->begin(), data->size());
              words = copy;
            }
            ReaderOptions options;
            options.traversalLimitInWords = kj::maxValue;
            options.nestingLimit = kj::maxValue;
            FlatArrayMessageReader reader(words, options);
            return orphanage.newOrphanCopy(reader.getRoot<DynamicStruct>(type.asStruct()));
          }
          default:
            errorReporter.addErrorOn(src,
                "Embeds can only be used when Text, Data, or a struct is expected.");
            return nullptr;
        }
      } else {
        return nullptr;
      }

Kenton Varda's avatar
Kenton Varda committed
2878
    case Expression::POSITIVE_INT:
2879
      return src.getPositiveInt();
Kenton Varda's avatar
Kenton Varda committed
2880

Kenton Varda's avatar
Kenton Varda committed
2881
    case Expression::NEGATIVE_INT: {
2882
      uint64_t nValue = src.getNegativeInt();
2883
      if (nValue > ((uint64_t)kj::maxValue >> 1) + 1) {
2884
        errorReporter.addErrorOn(src, "Integer is too big to be negative.");
2885
        return nullptr;
2886
      } else {
2887
        return kj::implicitCast<int64_t>(-nValue);
Kenton Varda's avatar
Kenton Varda committed
2888
      }
2889
    }
Kenton Varda's avatar
Kenton Varda committed
2890

Kenton Varda's avatar
Kenton Varda committed
2891
    case Expression::FLOAT:
2892
      return src.getFloat();
2893
      break;
Kenton Varda's avatar
Kenton Varda committed
2894

Kenton Varda's avatar
Kenton Varda committed
2895
    case Expression::STRING:
2896 2897
      if (type.isData()) {
        Text::Reader text = src.getString();
2898
        return orphanage.newOrphanCopy(Data::Reader(text.asBytes()));
2899 2900 2901
      } else {
        return orphanage.newOrphanCopy(src.getString());
      }
Kenton Varda's avatar
Kenton Varda committed
2902 2903
      break;

Kenton Varda's avatar
Kenton Varda committed
2904
    case Expression::BINARY:
Jason Choy's avatar
Jason Choy committed
2905 2906 2907 2908 2909 2910
      if (!type.isData()) {
        errorReporter.addErrorOn(src, kj::str("Type mismatch; expected ", makeTypeName(type), "."));
        return nullptr;
      }
      return orphanage.newOrphanCopy(src.getBinary());

Kenton Varda's avatar
Kenton Varda committed
2911
    case Expression::LIST: {
2912
      if (!type.isList()) {
2913
        errorReporter.addErrorOn(src, kj::str("Type mismatch; expected ", makeTypeName(type), "."));
2914 2915
        return nullptr;
      }
2916 2917 2918 2919 2920 2921 2922 2923
      auto listSchema = type.asList();
      Type elementType = listSchema.getElementType();
      auto srcList = src.getList();
      Orphan<DynamicList> result = orphanage.newOrphan(listSchema, srcList.size());
      auto dstList = result.get();
      for (uint i = 0; i < srcList.size(); i++) {
        KJ_IF_MAYBE(value, compileValue(srcList[i], elementType)) {
          dstList.adopt(i, kj::mv(*value));
2924
        }
Kenton Varda's avatar
Kenton Varda committed
2925
      }
2926
      return kj::mv(result);
Kenton Varda's avatar
Kenton Varda committed
2927 2928
    }

Kenton Varda's avatar
Kenton Varda committed
2929
    case Expression::TUPLE: {
2930
      if (!type.isStruct()) {
2931
        errorReporter.addErrorOn(src, kj::str("Type mismatch; expected ", makeTypeName(type), "."));
2932 2933
        return nullptr;
      }
2934 2935 2936 2937
      auto structSchema = type.asStruct();
      Orphan<DynamicStruct> result = orphanage.newOrphan(structSchema);
      fillStructValue(result.get(), src.getTuple());
      return kj::mv(result);
Kenton Varda's avatar
Kenton Varda committed
2938 2939
    }

Kenton Varda's avatar
Kenton Varda committed
2940
    case Expression::UNKNOWN:
Kenton Varda's avatar
Kenton Varda committed
2941
      // Ignore earlier error.
2942 2943 2944 2945 2946 2947
      return nullptr;
  }

  KJ_UNREACHABLE;
}

2948
void ValueTranslator::fillStructValue(DynamicStruct::Builder builder,
Kenton Varda's avatar
Kenton Varda committed
2949
                                      List<Expression::Param>::Reader assignments) {
2950
  for (auto assignment: assignments) {
Kenton Varda's avatar
Kenton Varda committed
2951 2952 2953 2954 2955 2956 2957 2958
    if (assignment.isNamed()) {
      auto fieldName = assignment.getNamed();
      KJ_IF_MAYBE(field, builder.getSchema().findFieldByName(fieldName.getValue())) {
        auto fieldProto = field->getProto();
        auto value = assignment.getValue();

        switch (fieldProto.which()) {
          case schema::Field::SLOT:
2959
            KJ_IF_MAYBE(compiledValue, compileValue(value, field->getType())) {
Kenton Varda's avatar
Kenton Varda committed
2960 2961 2962
              builder.adopt(*field, kj::mv(*compiledValue));
            }
            break;
2963

Kenton Varda's avatar
Kenton Varda committed
2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974
          case schema::Field::GROUP:
            if (value.isTuple()) {
              fillStructValue(builder.init(*field).as<DynamicStruct>(), value.getTuple());
            } else {
              errorReporter.addErrorOn(value, "Type mismatch; expected group.");
            }
            break;
        }
      } else {
        errorReporter.addErrorOn(fieldName, kj::str(
            "Struct has no field named '", fieldName.getValue(), "'."));
2975 2976
      }
    } else {
Kenton Varda's avatar
Kenton Varda committed
2977
      errorReporter.addErrorOn(assignment.getValue(), kj::str("Missing field name."));
2978 2979 2980 2981
    }
  }
}

2982 2983 2984
kj::String ValueTranslator::makeNodeName(Schema schema) {
  schema::Node::Reader proto = schema.getProto();
  return kj::str(proto.getDisplayName().slice(proto.getDisplayNamePrefixLength()));
2985 2986
}

2987
kj::String ValueTranslator::makeTypeName(Type type) {
2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
  switch (type.which()) {
    case schema::Type::VOID: return kj::str("Void");
    case schema::Type::BOOL: return kj::str("Bool");
    case schema::Type::INT8: return kj::str("Int8");
    case schema::Type::INT16: return kj::str("Int16");
    case schema::Type::INT32: return kj::str("Int32");
    case schema::Type::INT64: return kj::str("Int64");
    case schema::Type::UINT8: return kj::str("UInt8");
    case schema::Type::UINT16: return kj::str("UInt16");
    case schema::Type::UINT32: return kj::str("UInt32");
    case schema::Type::UINT64: return kj::str("UInt64");
    case schema::Type::FLOAT32: return kj::str("Float32");
    case schema::Type::FLOAT64: return kj::str("Float64");
    case schema::Type::TEXT: return kj::str("Text");
    case schema::Type::DATA: return kj::str("Data");
3003
    case schema::Type::LIST:
3004 3005 3006 3007
      return kj::str("List(", makeTypeName(type.asList().getElementType()), ")");
    case schema::Type::ENUM: return makeNodeName(type.asEnum());
    case schema::Type::STRUCT: return makeNodeName(type.asStruct());
    case schema::Type::INTERFACE: return makeNodeName(type.asInterface());
3008
    case schema::Type::ANY_POINTER: return kj::str("AnyPointer");
Kenton Varda's avatar
Kenton Varda committed
3009
  }
3010
  KJ_UNREACHABLE;
Kenton Varda's avatar
Kenton Varda committed
3011 3012
}

3013
kj::Maybe<DynamicValue::Reader> NodeTranslator::readConstant(
Kenton Varda's avatar
Kenton Varda committed
3014
    Expression::Reader source, bool isBootstrap) {
3015
  // Look up the constant decl.
3016
  NodeTranslator::BrandedDecl constDecl = nullptr;
3017
  KJ_IF_MAYBE(decl, compileDeclExpression(source, noImplicitParams())) {
3018 3019 3020 3021 3022
    constDecl = *decl;
  } else {
    // Lookup will have reported an error.
    return nullptr;
  }
3023

3024 3025 3026 3027 3028 3029
  // Is it a constant?
  if(constDecl.getKind().orDefault(Declaration::FILE) != Declaration::CONST) {
    errorReporter.addErrorOn(source,
        kj::str("'", expressionString(source), "' does not refer to a constant."));
    return nullptr;
  }
3030

3031
  // Extract the ID and brand.
3032
  MallocMessageBuilder builder(256);
3033
  auto constBrand = builder.getRoot<schema::Brand>();
3034
  uint64_t id = constDecl.getIdAndFillBrand([&]() { return constBrand; });
3035

3036 3037
  // Look up the schema -- we'll need this to compile the constant's type.
  Schema constSchema;
3038
  KJ_IF_MAYBE(s, resolver.resolveBootstrapSchema(id, constBrand)) {
3039 3040 3041 3042 3043
    constSchema = *s;
  } else {
    // The constant's schema is broken for reasons already reported.
    return nullptr;
  }
3044

3045 3046 3047 3048 3049 3050 3051 3052
  // If we're bootstrapping, then we know we're expecting a primitive value, so if the
  // constant turns out to be non-primitive, we'll error out anyway.  If we're not
  // bootstrapping, we may be compiling a non-primitive value and so we need the final
  // version of the constant to make sure its value is filled in.
  schema::Node::Reader proto = constSchema.getProto();
  if (!isBootstrap) {
    KJ_IF_MAYBE(finalProto, resolver.resolveFinalSchema(id)) {
      proto = *finalProto;
3053
    } else {
3054
      // The constant's final schema is broken for reasons already reported.
3055
      return nullptr;
3056 3057 3058
    }
  }

3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
  auto constReader = proto.getConst();
  auto dynamicConst = toDynamic(constReader.getValue());
  auto constValue = dynamicConst.get(KJ_ASSERT_NONNULL(dynamicConst.which()));

  if (constValue.getType() == DynamicValue::ANY_POINTER) {
    // We need to assign an appropriate schema to this pointer.
    AnyPointer::Reader objValue = constValue.as<AnyPointer>();

    auto constType = constSchema.asConst().getType();
    switch (constType.which()) {
      case schema::Type::STRUCT:
        constValue = objValue.getAs<DynamicStruct>(constType.asStruct());
        break;
      case schema::Type::LIST:
        constValue = objValue.getAs<DynamicList>(constType.asList());
        break;
      case schema::Type::ANY_POINTER:
        // Fine as-is.
        break;
      default:
        KJ_FAIL_ASSERT("Unrecognized AnyPointer-typed member of schema::Value.");
        break;
    }
  }

  if (source.isRelativeName()) {
    // A fully unqualified identifier looks like it might refer to a constant visible in the
    // current scope, but if that's really what the user wanted, we want them to use a
    // qualified name to make it more obvious.  Report an error.
    KJ_IF_MAYBE(scope, resolver.resolveBootstrapSchema(proto.getScopeId(),
3089
                                                       schema::Brand::Reader())) {
3090 3091 3092 3093
      auto scopeReader = scope->getProto();
      kj::StringPtr parent;
      if (scopeReader.isFile()) {
        parent = "";
3094
      } else {
3095
        parent = scopeReader.getDisplayName().slice(scopeReader.getDisplayNamePrefixLength());
3096
      }
3097
      kj::StringPtr id = source.getRelativeName().getValue();
3098

3099 3100 3101 3102 3103 3104
      errorReporter.addErrorOn(source, kj::str(
          "Constant names must be qualified to avoid confusion.  Please replace '",
          expressionString(source), "' with '", parent, ".", id,
          "', if that's what you intended."));
    }
  }
3105

3106
  return constValue;
3107 3108
}

3109 3110 3111 3112 3113 3114 3115 3116 3117 3118
kj::Maybe<kj::Array<const byte>> NodeTranslator::readEmbed(LocatedText::Reader filename) {
  KJ_IF_MAYBE(data, resolver.readEmbed(filename.getValue())) {
    return kj::mv(*data);
  } else {
    errorReporter.addErrorOn(filename,
        kj::str("Couldn't read file for embed: ", filename.getValue()));
    return nullptr;
  }
}

Kenton Varda's avatar
Kenton Varda committed
3119
Orphan<List<schema::Annotation>> NodeTranslator::compileAnnotationApplications(
Kenton Varda's avatar
Kenton Varda committed
3120 3121
    List<Declaration::AnnotationApplication>::Reader annotations,
    kj::StringPtr targetsFlagName) {
3122
  if (annotations.size() == 0 || !compileAnnotations) {
3123
    // Return null.
Kenton Varda's avatar
Kenton Varda committed
3124
    return Orphan<List<schema::Annotation>>();
3125 3126
  }

Kenton Varda's avatar
Kenton Varda committed
3127
  auto result = orphanage.newOrphan<List<schema::Annotation>>(annotations.size());
3128 3129 3130 3131
  auto builder = result.get();

  for (uint i = 0; i < annotations.size(); i++) {
    Declaration::AnnotationApplication::Reader annotation = annotations[i];
Kenton Varda's avatar
Kenton Varda committed
3132
    schema::Annotation::Builder annotationBuilder = builder[i];
3133 3134

    // Set the annotation's value to void in case we fail to produce something better below.
Kenton Varda's avatar
Kenton Varda committed
3135
    annotationBuilder.initValue().setVoid();
3136

3137
    auto name = annotation.getName();
3138
    KJ_IF_MAYBE(decl, compileDeclExpression(name, noImplicitParams())) {
Kenton Varda's avatar
Kenton Varda committed
3139 3140 3141 3142 3143
      KJ_IF_MAYBE(kind, decl->getKind()) {
        if (*kind != Declaration::ANNOTATION) {
          errorReporter.addErrorOn(name, kj::str(
              "'", expressionString(name), "' is not an annotation."));
        } else {
3144
          annotationBuilder.setId(decl->getIdAndFillBrand(
3145
              [&]() { return annotationBuilder.initBrand(); }));
Kenton Varda's avatar
Kenton Varda committed
3146
          KJ_IF_MAYBE(annotationSchema,
3147
                      resolver.resolveBootstrapSchema(annotationBuilder.getId(),
3148
                                                      annotationBuilder.getBrand())) {
Kenton Varda's avatar
Kenton Varda committed
3149 3150 3151 3152 3153
            auto node = annotationSchema->getProto().getAnnotation();
            if (!toDynamic(node).get(targetsFlagName).as<bool>()) {
              errorReporter.addErrorOn(name, kj::str(
                  "'", expressionString(name), "' cannot be applied to this kind of declaration."));
            }
3154

Kenton Varda's avatar
Kenton Varda committed
3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170
            // Interpret the value.
            auto value = annotation.getValue();
            switch (value.which()) {
              case Declaration::AnnotationApplication::Value::NONE:
                // No value, i.e. void.
                if (node.getType().isVoid()) {
                  annotationBuilder.getValue().setVoid();
                } else {
                  errorReporter.addErrorOn(name, kj::str(
                      "'", expressionString(name), "' requires a value."));
                  compileDefaultDefaultValue(node.getType(), annotationBuilder.getValue());
                }
                break;

              case Declaration::AnnotationApplication::Value::EXPRESSION:
                compileBootstrapValue(value.getExpression(), node.getType(),
3171 3172
                                      annotationBuilder.getValue(),
                                      *annotationSchema);
Kenton Varda's avatar
Kenton Varda committed
3173 3174
                break;
            }
3175
          }
3176
        }
Kenton Varda's avatar
Kenton Varda committed
3177 3178 3179
      } else if (*kind != Declaration::ANNOTATION) {
        errorReporter.addErrorOn(name, kj::str(
            "'", expressionString(name), "' is not an annotation."));
3180 3181 3182
      }
    }
  }
Kenton Varda's avatar
Kenton Varda committed
3183

3184
  return result;
Kenton Varda's avatar
Kenton Varda committed
3185 3186 3187 3188
}

}  // namespace compiler
}  // namespace capnp