node-translator.c++ 120 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
#include <kj/debug.h>
#include <kj/arena.h>
27
#include <kj/encoding.h>
Kenton Varda's avatar
Kenton Varda committed
28 29
#include <set>
#include <map>
30
#include <stdlib.h>
Kenton Varda's avatar
Kenton Varda committed
31 32 33 34

namespace capnp {
namespace compiler {

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

Kenton Varda's avatar
Kenton Varda committed
39 40 41 42 43 44 45
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
46
    inline HoleSet(): holes{0, 0, 0, 0, 0, 0} {}
Kenton Varda's avatar
Kenton Varda committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

    // 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
70
    UIntType holes[6];
Kenton Varda's avatar
Kenton Varda committed
71 72 73 74 75 76 77
    // 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) {
78
      // 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
79 80 81
      // 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).

82
      if (lgSize >= kj::size(holes)) {
83 84
        return nullptr;
      } else if (holes[lgSize] != 0) {
Kenton Varda's avatar
Kenton Varda committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        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,
107
                       UIntType limitLgSize = sizeof(HoleSet::holes) / sizeof(HoleSet::holes[0])) {
Kenton Varda's avatar
Kenton Varda committed
108 109 110 111
      // 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.

112
      KJ_DREQUIRE(limitLgSize <= kj::size(holes));
Kenton Varda's avatar
Kenton Varda committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

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

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

    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.
165
      for (uint i = kj::size(holes); i > 0; i--) {
Kenton Varda's avatar
Kenton Varda committed
166 167 168 169 170 171
        if (holes[i - 1] != 1) {
          return i;
        }
      }
      return 0;
    }
Kenton Varda's avatar
Kenton Varda committed
172 173 174 175 176
  };

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

177
    virtual void addVoid() = 0;
Kenton Varda's avatar
Kenton Varda committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    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;

193 194
    void addVoid() override {}

Kenton Varda's avatar
Kenton Varda committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    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;
237
    kj::Maybe<uint> discriminantOffset;
Kenton Varda's avatar
Kenton Varda committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    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());
    }

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

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

274
  struct Group final: public StructOrGroup {
Kenton Varda's avatar
Kenton Varda committed
275 276 277 278 279 280 281 282 283
  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,
284
        // to reduce fragmentation.  Returns the size of the hole, if found.
Kenton Varda's avatar
Kenton Varda committed
285 286 287

        if (!isUsed) {
          // The location is effectively one big hole.
288 289 290 291 292
          if (lgSize <= location.lgSize) {
            return location.lgSize;
          } else {
            return nullptr;
          }
Kenton Varda's avatar
Kenton Varda committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
        } 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;
365
            return location.offset << (location.lgSize - lgSize);
Kenton Varda's avatar
Kenton Varda committed
366 367 368 369 370
          } else {
            return nullptr;
          }
        } else {
          uint newSize = kj::max(lgSizeUsed, lgSize) + 1;
371
          if (tryExpandUsage(group, location, newSize, true)) {
372
            uint result = KJ_ASSERT_NONNULL(holes.tryAllocate(lgSize));
373 374
            uint locationOffset = location.offset << (location.lgSize - lgSize);
            return locationOffset + result;
Kenton Varda's avatar
Kenton Varda committed
375 376 377 378 379 380 381 382 383 384
          } 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.
385
          return tryExpandUsage(group, location, oldLgSize + expansionFactor, false);
Kenton Varda's avatar
Kenton Varda committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
        } 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.

408 409
      bool tryExpandUsage(Group& group, Union::DataLocation& location, uint desiredUsage,
                          bool newHoles) {
Kenton Varda's avatar
Kenton Varda committed
410 411 412 413 414 415 416 417
        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.
418 419
        if (newHoles) {
          holes.addHolesAtEnd(lgSizeUsed, 1, desiredUsage);
420
        } else if (shouldDetectIssue344()) {
421 422 423 424 425 426 427 428 429 430 431
          // 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
432 433 434 435 436 437 438 439 440 441 442 443 444 445
        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.

446 447 448
    bool hasMembers = false;

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

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

458 459 460 461 462 463 464 465 466 467
    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
468
    uint addData(uint lgSize) override {
469
      addMember();
470

471
      uint bestSize = kj::maxValue;
Kenton Varda's avatar
Kenton Varda committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
      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 {
510
      addMember();
511

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

    bool tryExpandData(uint oldLgSize, uint oldOffset, uint expansionFactor) override {
521
      bool mustFail = false;
Kenton Varda's avatar
Kenton Varda committed
522 523 524 525
      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.
526 527 528 529 530 531 532 533 534

        // 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.
535 536 537 538 539
        if (shouldDetectIssue344()) {
          mustFail = true;
        } else {
          return false;
        }
Kenton Varda's avatar
Kenton Varda committed
540 541 542 543 544 545 546 547 548 549 550 551 552
      }

      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.
553 554 555 556 557 558
          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
559 560 561 562 563 564 565 566
        }
      }

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

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

private:
  Top top;
};

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

575
class NodeTranslator::BrandedDecl {
Kenton Varda's avatar
Kenton Varda committed
576
  // Represents a declaration possibly with generic parameter bindings.
577 578 579
  //
  // 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
580 581

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

594 595 596 597 598 599
  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());
  }

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

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

606 607 608 609
  // 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.

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

613
  kj::Maybe<BrandedDecl> getMember(kj::StringPtr memberName, Expression::Reader subSource);
Kenton Varda's avatar
Kenton Varda committed
614 615 616 617 618
  // 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.

619
  template <typename InitBrandFunc>
620
  uint64_t getIdAndFillBrand(InitBrandFunc&& initBrand);
621 622 623
  // 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
624 625 626
  //
  // It is an error to call this when `getKind()` returns null.

627
  kj::Maybe<BrandedDecl&> getListParam();
Kenton Varda's avatar
Kenton Varda committed
628 629 630 631 632 633 634 635
  // 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.

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

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

643 644 645 646
  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
647 648 649 650
  kj::String toString();
  kj::String toDebugString();

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

656
class NodeTranslator::BrandScope: public kj::Refcounted {
657
  // Tracks the brand parameter bindings affecting the current scope. For example, if we are
David Renshaw's avatar
David Renshaw committed
658
  // interpreting the type expression "Foo(Text).Bar", we would start with the current scopes
659 660 661 662
  // 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
663
  // TODO(cleanup): This is too complicated to live here. We should refactor this class and
664 665
  //   BrandedDecl out into their own file, independent of NodeTranslator.

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

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

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

688 689
  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
690 691
  }

692 693
  kj::Maybe<kj::Own<BrandScope>> setParams(
      kj::Array<BrandedDecl> params, Declaration::Which genericType, Expression::Reader source) {
Kenton Varda's avatar
Kenton Varda committed
694
    if (this->params.size() != 0) {
695 696 697
      errorReporter.addErrorOn(source, "Double-application of generic parameters.");
      return nullptr;
    } else if (params.size() > leafParamCount) {
698 699 700 701 702
      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
703
      return nullptr;
704
    } else if (params.size() < leafParamCount) {
705
      errorReporter.addErrorOn(source, "Not enough generic parameters.");
Kenton Varda's avatar
Kenton Varda committed
706 707
      return nullptr;
    } else {
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
      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;
            }
          }
        }
      }

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

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

745 746 747
  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
748 749 750
    if (scopeId == leafId) {
      if (index < params.size()) {
        return params[index];
751
      } else if (inherited) {
Kenton Varda's avatar
Kenton Varda committed
752
        return nullptr;
753 754 755 756 757 758
      } 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
759 760
      }
    } else KJ_IF_MAYBE(p, parent) {
761
      return p->get()->lookupParameter(resolver, scopeId, index);
Kenton Varda's avatar
Kenton Varda committed
762
    } else {
763
      KJ_FAIL_REQUIRE("scope is not a parent");
Kenton Varda's avatar
Kenton Varda committed
764 765 766
    }
  }

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

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

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

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

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

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

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

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

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

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

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

890
kj::Maybe<NodeTranslator::BrandedDecl> NodeTranslator::BrandedDecl::getMember(
891 892
    kj::StringPtr memberName, Expression::Reader subSource) {
  if (body.is<Resolver::ResolvedParameter>()) {
Kenton Varda's avatar
Kenton Varda committed
893
    return nullptr;
894 895
  } 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
896 897 898 899 900
  } else {
    return nullptr;
  }
}

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

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

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

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

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

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

931
NodeTranslator::Resolver::ResolvedParameter NodeTranslator::BrandedDecl::asVariable() {
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 960
  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();
961 962 963 964 965 966 967

        KJ_IF_MAYBE(param, getListParam()) {
          if (!param->compileAsType(errorReporter, elementType)) {
            return false;
          }
        } else {
          addError(errorReporter, "'List' requires exactly one parameter.");
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
          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.  :)");
1000
        // fallthrough
1001
      case Declaration::BUILTIN_ANY_POINTER:
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
        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();
1012
        return true;
Kenton Varda's avatar
Kenton Varda committed
1013

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
      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();
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
    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;
    }
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
  }
}

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
1061 1062 1063 1064
}

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

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

1069
kj::String NodeTranslator::BrandedDecl::toDebugString() {
1070 1071
  if (body.is<Resolver::ResolvedParameter>()) {
    auto variable = body.get<Resolver::ResolvedParameter>();
1072
    return kj::str("variable(", variable.id, ", ", variable.index, ")");
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 1231
  } 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());
          }
        }
1232 1233 1234

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

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

  KJ_UNREACHABLE;
1242 1243 1244
}

kj::Maybe<NodeTranslator::BrandedDecl> NodeTranslator::BrandScope::compileDeclExpression(
1245 1246
    Expression::Reader source, Resolver& resolver,
    ImplicitParams implicitMethodParams) {
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
  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:
1259
    case Expression::EMBED:
1260 1261 1262 1263 1264
      errorReporter.addErrorOn(source, "Expected name.");
      return nullptr;

    case Expression::RELATIVE_NAME: {
      auto name = source.getRelativeName();
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
      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)) {
1281 1282
        return interpretResolve(resolver, *r, source);
      } else {
1283
        errorReporter.addErrorOn(name, kj::str("Not defined: ", nameValue));
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
        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())) {
1301
        // Import is always a root scope, so create a fresh BrandScope.
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
        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();
1312
      KJ_IF_MAYBE(decl, compileDeclExpression(app.getFunction(), resolver, implicitMethodParams)) {
1313 1314 1315 1316 1317 1318 1319 1320 1321
        // 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.");
          }

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

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

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

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

Kenton Varda's avatar
Kenton Varda committed
1386
NodeTranslator::~NodeTranslator() noexcept(false) {}
1387

Kenton Varda's avatar
Kenton Varda committed
1388
NodeTranslator::NodeSet NodeTranslator::getBootstrapNode() {
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
  auto sourceInfos = kj::heapArrayBuilder<schema::Node::SourceInfo::Reader>(
      1 + groups.size() + paramStructs.size());
  sourceInfos.add(sourceInfo.getReader());
  for (auto& group: groups) {
    sourceInfos.add(group.sourceInfo.getReader());
  }
  for (auto& paramStruct: paramStructs) {
    sourceInfos.add(paramStruct.sourceInfo.getReader());
  }

1399 1400 1401 1402
  auto nodeReader = wipNode.getReader();
  if (nodeReader.isInterface()) {
    return NodeSet {
      nodeReader,
1403 1404
      KJ_MAP(g, paramStructs) { return g.node.getReader(); },
      sourceInfos.finish()
1405 1406 1407 1408
    };
  } else {
    return NodeSet {
      nodeReader,
1409 1410
      KJ_MAP(g, groups) { return g.node.getReader(); },
      sourceInfos.finish()
1411 1412
    };
  }
Kenton Varda's avatar
Kenton Varda committed
1413 1414 1415
}

NodeTranslator::NodeSet NodeTranslator::finish() {
Kenton Varda's avatar
Kenton Varda committed
1416 1417 1418 1419
  // 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];
1420
    compileValue(value.source, value.type, value.typeScope, value.target, false);
Kenton Varda's avatar
Kenton Varda committed
1421 1422
  }

Kenton Varda's avatar
Kenton Varda committed
1423
  return getBootstrapNode();
Kenton Varda's avatar
Kenton Varda committed
1424 1425
}

1426 1427
class NodeTranslator::DuplicateNameDetector {
public:
1428
  inline explicit DuplicateNameDetector(ErrorReporter& errorReporter)
1429
      : errorReporter(errorReporter) {}
1430
  void check(List<Declaration>::Reader nestedDecls, Declaration::Which parentKind);
1431 1432

private:
1433
  ErrorReporter& errorReporter;
1434 1435 1436
  std::map<kj::StringPtr, LocatedText::Reader> names;
};

Kenton Varda's avatar
Kenton Varda committed
1437
void NodeTranslator::compileNode(Declaration::Reader decl, schema::Node::Builder builder) {
1438
  DuplicateNameDetector(errorReporter)
1439
      .check(decl.getNestedDecls(), decl.which());
Kenton Varda's avatar
Kenton Varda committed
1440

Kenton Varda's avatar
Kenton Varda committed
1441 1442 1443 1444 1445 1446 1447 1448
  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());
    }
  }

1449 1450
  builder.setIsGeneric(localBrand->isGeneric());

Kenton Varda's avatar
Kenton Varda committed
1451 1452
  kj::StringPtr targetsFlagName;

1453 1454
  switch (decl.which()) {
    case Declaration::FILE:
Kenton Varda's avatar
Kenton Varda committed
1455
      targetsFlagName = "targetsFile";
Kenton Varda's avatar
Kenton Varda committed
1456
      break;
1457 1458
    case Declaration::CONST:
      compileConst(decl.getConst(), builder.initConst());
Kenton Varda's avatar
Kenton Varda committed
1459
      targetsFlagName = "targetsConst";
Kenton Varda's avatar
Kenton Varda committed
1460
      break;
1461 1462
    case Declaration::ANNOTATION:
      compileAnnotation(decl.getAnnotation(), builder.initAnnotation());
Kenton Varda's avatar
Kenton Varda committed
1463
      targetsFlagName = "targetsAnnotation";
Kenton Varda's avatar
Kenton Varda committed
1464
      break;
1465 1466
    case Declaration::ENUM:
      compileEnum(decl.getEnum(), decl.getNestedDecls(), builder);
Kenton Varda's avatar
Kenton Varda committed
1467
      targetsFlagName = "targetsEnum";
Kenton Varda's avatar
Kenton Varda committed
1468
      break;
1469 1470
    case Declaration::STRUCT:
      compileStruct(decl.getStruct(), decl.getNestedDecls(), builder);
Kenton Varda's avatar
Kenton Varda committed
1471
      targetsFlagName = "targetsStruct";
Kenton Varda's avatar
Kenton Varda committed
1472
      break;
1473 1474
    case Declaration::INTERFACE:
      compileInterface(decl.getInterface(), decl.getNestedDecls(), builder);
Kenton Varda's avatar
Kenton Varda committed
1475
      targetsFlagName = "targetsInterface";
Kenton Varda's avatar
Kenton Varda committed
1476 1477 1478 1479 1480 1481 1482
      break;

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

Kenton Varda's avatar
Kenton Varda committed
1483
  builder.adoptAnnotations(compileAnnotationApplications(decl.getAnnotations(), targetsFlagName));
1484

1485 1486 1487 1488
  auto di = sourceInfo.get();
  di.setId(wipNode.getReader().getId());
  if (decl.hasDocComment()) {
    di.setDocComment(decl.getDocComment());
1489
  }
Kenton Varda's avatar
Kenton Varda committed
1490 1491
}

1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
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;
  }
}

1508
void NodeTranslator::DuplicateNameDetector::check(
1509
    List<Declaration>::Reader nestedDecls, Declaration::Which parentKind) {
Kenton Varda's avatar
Kenton Varda committed
1510 1511 1512 1513 1514 1515
  for (auto decl: nestedDecls) {
    {
      auto name = decl.getName();
      auto nameText = name.getValue();
      auto insertResult = names.insert(std::make_pair(nameText, name));
      if (!insertResult.second) {
1516
        if (nameText.size() == 0 && decl.isUnion()) {
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
          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
1527
      }
Kenton Varda's avatar
Kenton Varda committed
1528 1529

      switch (decl.which()) {
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
        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
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
        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
1583 1584
    }

1585 1586 1587 1588 1589 1590 1591
    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
1592
        switch (parentKind) {
1593 1594 1595
          case Declaration::FILE:
          case Declaration::STRUCT:
          case Declaration::INTERFACE:
Kenton Varda's avatar
Kenton Varda committed
1596 1597 1598 1599 1600 1601 1602 1603
            // OK.
            break;
          default:
            errorReporter.addErrorOn(decl, "This kind of declaration doesn't belong here.");
            break;
        }
        break;

1604 1605
      case Declaration::ENUMERANT:
        if (parentKind != Declaration::ENUM) {
Kenton Varda's avatar
Kenton Varda committed
1606 1607 1608
          errorReporter.addErrorOn(decl, "Enumerants can only appear in enums.");
        }
        break;
1609 1610
      case Declaration::METHOD:
        if (parentKind != Declaration::INTERFACE) {
Kenton Varda's avatar
Kenton Varda committed
1611 1612 1613
          errorReporter.addErrorOn(decl, "Methods can only appear in interfaces.");
        }
        break;
1614 1615 1616
      case Declaration::FIELD:
      case Declaration::UNION:
      case Declaration::GROUP:
Kenton Varda's avatar
Kenton Varda committed
1617
        switch (parentKind) {
1618 1619 1620
          case Declaration::STRUCT:
          case Declaration::UNION:
          case Declaration::GROUP:
Kenton Varda's avatar
Kenton Varda committed
1621 1622 1623 1624 1625 1626
            // OK.
            break;
          default:
            errorReporter.addErrorOn(decl, "This declaration can only appear in structs.");
            break;
        }
1627 1628 1629 1630 1631

        // 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.
1632
          check(decl.getNestedDecls(), decl.which());
1633 1634 1635
        } else {
          // Children are in their own scope.
          DuplicateNameDetector(errorReporter)
1636
              .check(decl.getNestedDecls(), decl.which());
1637 1638
        }

Kenton Varda's avatar
Kenton Varda committed
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
        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
1649
                                  schema::Node::Const::Builder builder) {
Kenton Varda's avatar
Kenton Varda committed
1650
  auto typeBuilder = builder.initType();
1651
  if (compileType(decl.getType(), typeBuilder, noImplicitParams())) {
Kenton Varda's avatar
Kenton Varda committed
1652 1653 1654 1655 1656
    compileBootstrapValue(decl.getValue(), typeBuilder.asReader(), builder.initValue());
  }
}

void NodeTranslator::compileAnnotation(Declaration::Annotation::Reader decl,
Kenton Varda's avatar
Kenton Varda committed
1657
                                       schema::Node::Annotation::Builder builder) {
1658
  compileType(decl.getType(), builder.initType(), noImplicitParams());
Kenton Varda's avatar
Kenton Varda committed
1659 1660 1661 1662

  // Dynamically copy over the values of all of the "targets" members.
  DynamicStruct::Reader src = decl;
  DynamicStruct::Builder dst = builder;
1663 1664 1665 1666 1667
  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
1668 1669 1670 1671 1672 1673
    }
  }
}

class NodeTranslator::DuplicateOrdinalDetector {
public:
1674
  DuplicateOrdinalDetector(ErrorReporter& errorReporter): errorReporter(errorReporter) {}
Kenton Varda's avatar
Kenton Varda committed
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688

  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."));
1689
      expectedOrdinal = ordinal.getValue() + 1;
Kenton Varda's avatar
Kenton Varda committed
1690 1691 1692 1693 1694 1695 1696
    } else {
      ++expectedOrdinal;
      lastOrdinalLocation = ordinal;
    }
  }

private:
1697
  ErrorReporter& errorReporter;
Kenton Varda's avatar
Kenton Varda committed
1698 1699 1700 1701
  uint expectedOrdinal = 0;
  kj::Maybe<LocatedInteger::Reader> lastOrdinalLocation;
};

1702
void NodeTranslator::compileEnum(Void decl,
Kenton Varda's avatar
Kenton Varda committed
1703
                                 List<Declaration>::Reader members,
Kenton Varda's avatar
Kenton Varda committed
1704
                                 schema::Node::Builder builder) {
Kenton Varda's avatar
Kenton Varda committed
1705 1706 1707 1708 1709
  // maps ordinal -> (code order, declaration)
  std::multimap<uint, std::pair<uint, Declaration::Reader>> enumerants;

  uint codeOrder = 0;
  for (auto member: members) {
1710
    if (member.isEnumerant()) {
Kenton Varda's avatar
Kenton Varda committed
1711 1712 1713 1714 1715 1716
      enumerants.insert(
          std::make_pair(member.getId().getOrdinal().getValue(),
                         std::make_pair(codeOrder++, member)));
    }
  }

1717
  auto list = builder.initEnum().initEnumerants(enumerants.size());
1718
  auto sourceInfoList = sourceInfo.get().initMembers(enumerants.size());
Kenton Varda's avatar
Kenton Varda committed
1719 1720 1721 1722 1723 1724 1725 1726 1727
  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());

1728 1729 1730 1731
    if (enumerantDecl.hasDocComment()) {
      sourceInfoList[i].setDocComment(enumerantDecl.getDocComment());
    }

1732
    auto enumerantBuilder = list[i++];
Kenton Varda's avatar
Kenton Varda committed
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
    enumerantBuilder.setName(enumerantDecl.getName().getValue());
    enumerantBuilder.setCodeOrder(codeOrder);
    enumerantBuilder.adoptAnnotations(compileAnnotationApplications(
        enumerantDecl.getAnnotations(), "targetsEnumerant"));
  }
}

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

class NodeTranslator::StructTranslator {
public:
1744 1745 1746
  explicit StructTranslator(NodeTranslator& translator, ImplicitParams implicitMethodParams)
      : translator(translator), errorReporter(translator.errorReporter),
        implicitMethodParams(implicitMethodParams) {}
Kenton Varda's avatar
Kenton Varda committed
1747 1748
  KJ_DISALLOW_COPY(StructTranslator);

1749 1750
  void translate(Void decl, List<Declaration>::Reader members, schema::Node::Builder builder,
                 schema::Node::SourceInfo::Builder sourceInfo) {
Kenton Varda's avatar
Kenton Varda committed
1751
    // Build the member-info-by-ordinal map.
1752
    MemberInfo root(builder, sourceInfo);
Kenton Varda's avatar
Kenton Varda committed
1753
    traverseTopOrGroup(members, root, layout.getTop());
1754 1755
    translateInternal(root, builder);
  }
Kenton Varda's avatar
Kenton Varda committed
1756

1757 1758
  void translate(List<Declaration::Param>::Reader params, schema::Node::Builder builder,
                 schema::Node::SourceInfo::Builder sourceInfo) {
1759
    // Build a struct from a method param / result list.
1760
    MemberInfo root(builder, sourceInfo);
1761 1762
    traverseParams(params, root, layout.getTop());
    translateInternal(root, builder);
Kenton Varda's avatar
Kenton Varda committed
1763 1764 1765 1766
  }

private:
  NodeTranslator& translator;
1767
  ErrorReporter& errorReporter;
1768
  ImplicitParams implicitMethodParams;
Kenton Varda's avatar
Kenton Varda committed
1769 1770 1771
  StructLayout layout;
  kj::Arena arena;

1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
  struct NodeSourceInfoBuilderPair {
    schema::Node::Builder node;
    schema::Node::SourceInfo::Builder sourceInfo;
  };

  struct FieldSourceInfoBuilderPair {
    schema::Field::Builder field;
    schema::Node::SourceInfo::Member::Builder sourceInfo;
  };

Kenton Varda's avatar
Kenton Varda committed
1782 1783 1784 1785 1786 1787 1788
  struct MemberInfo {
    MemberInfo* parent;
    // The MemberInfo for the parent scope.

    uint codeOrder;
    // Code order within the parent.

1789 1790 1791
    uint index = 0;
    // Index within the parent.

Kenton Varda's avatar
Kenton Varda committed
1792 1793 1794
    uint childCount = 0;
    // Number of children this member has.

1795 1796 1797
    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
1798

Kenton Varda's avatar
Kenton Varda committed
1799 1800 1801 1802 1803 1804 1805
    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.

1806 1807 1808 1809 1810
    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
1811 1812
    Expression::Reader fieldType;               // if declKind == FIELD
    Expression::Reader fieldDefaultValue;       // if declKind == FIELD && hasDefaultValue
1813 1814 1815 1816 1817
    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
1818

1819
    kj::Maybe<Text::Reader> docComment = nullptr;
1820

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

Kenton Varda's avatar
Kenton Varda committed
1824
    schema::Node::Builder node;
1825
    schema::Node::SourceInfo::Builder sourceInfo;
Kenton Varda's avatar
Kenton Varda committed
1826
    // If it's a group, or the top-level struct.
1827

Kenton Varda's avatar
Kenton Varda committed
1828 1829 1830 1831 1832 1833
    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
1834 1835 1836 1837
      // 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
1838 1839
    };

1840 1841 1842 1843
    inline explicit MemberInfo(schema::Node::Builder node,
                               schema::Node::SourceInfo::Builder sourceInfo)
        : parent(nullptr), codeOrder(0), isInUnion(false), node(node), sourceInfo(sourceInfo),
          unionScope(nullptr) {}
Kenton Varda's avatar
Kenton Varda committed
1844 1845
    inline MemberInfo(MemberInfo& parent, uint codeOrder,
                      const Declaration::Reader& decl,
Kenton Varda's avatar
Kenton Varda committed
1846 1847 1848
                      StructLayout::StructOrGroup& fieldScope,
                      bool isInUnion)
        : parent(&parent), codeOrder(codeOrder), isInUnion(isInUnion),
1849 1850 1851
          name(decl.getName().getValue()), declId(decl.getId()), declKind(Declaration::FIELD),
          declAnnotations(decl.getAnnotations()),
          startByte(decl.getStartByte()), endByte(decl.getEndByte()),
1852
          node(nullptr), sourceInfo(nullptr), fieldScope(&fieldScope) {
1853 1854 1855 1856 1857 1858 1859
      KJ_REQUIRE(decl.which() == Declaration::FIELD);
      auto fieldDecl = decl.getField();
      fieldType = fieldDecl.getType();
      if (fieldDecl.getDefaultValue().isValue()) {
        hasDefaultValue = true;
        fieldDefaultValue = fieldDecl.getDefaultValue().getValue();
      }
Kenton Varda's avatar
Kenton Varda committed
1860
      if (decl.hasDocComment()) {
1861
        docComment = decl.getDocComment();
Kenton Varda's avatar
Kenton Varda committed
1862
      }
1863 1864 1865 1866 1867 1868 1869 1870 1871
    }
    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()),
1872
          node(nullptr), sourceInfo(nullptr), fieldScope(&fieldScope) {
1873 1874 1875 1876 1877 1878
      fieldType = decl.getType();
      if (decl.getDefaultValue().isValue()) {
        hasDefaultValue = true;
        fieldDefaultValue = decl.getDefaultValue().getValue();
      }
    }
Kenton Varda's avatar
Kenton Varda committed
1879
    inline MemberInfo(MemberInfo& parent, uint codeOrder,
Kenton Varda's avatar
Kenton Varda committed
1880
                      const Declaration::Reader& decl,
1881
                      NodeSourceInfoBuilderPair builderPair,
Kenton Varda's avatar
Kenton Varda committed
1882 1883
                      bool isInUnion)
        : parent(&parent), codeOrder(codeOrder), isInUnion(isInUnion),
1884 1885 1886
          name(decl.getName().getValue()), declId(decl.getId()), declKind(decl.which()),
          declAnnotations(decl.getAnnotations()),
          startByte(decl.getStartByte()), endByte(decl.getEndByte()),
1887
          node(builderPair.node), sourceInfo(builderPair.sourceInfo), unionScope(nullptr) {
1888
      KJ_REQUIRE(decl.which() != Declaration::FIELD);
Kenton Varda's avatar
Kenton Varda committed
1889
      if (decl.hasDocComment()) {
1890
        docComment = decl.getDocComment();
Kenton Varda's avatar
Kenton Varda committed
1891
      }
1892
    }
Kenton Varda's avatar
Kenton Varda committed
1893

Kenton Varda's avatar
Kenton Varda committed
1894
    schema::Field::Builder getSchema() {
1895 1896 1897
      KJ_IF_MAYBE(result, schema) {
        return *result;
      } else {
1898
        index = parent->childInitializedCount;
1899 1900
        auto builderPair = parent->addMemberSchema();
        auto builder = builderPair.field;
Kenton Varda's avatar
Kenton Varda committed
1901 1902 1903
        if (isInUnion) {
          builder.setDiscriminantValue(parent->unionDiscriminantCount++);
        }
1904
        builder.setName(name);
Kenton Varda's avatar
Kenton Varda committed
1905
        builder.setCodeOrder(codeOrder);
1906 1907 1908 1909 1910

        KJ_IF_MAYBE(dc, docComment) {
          builderPair.sourceInfo.setDocComment(*dc);
        }

1911 1912 1913 1914 1915
        schema = builder;
        return builder;
      }
    }

1916
    FieldSourceInfoBuilderPair addMemberSchema() {
Kenton Varda's avatar
Kenton Varda committed
1917 1918 1919
      // 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
1920
      KJ_REQUIRE(childInitializedCount < childCount);
Kenton Varda's avatar
Kenton Varda committed
1921

Kenton Varda's avatar
Kenton Varda committed
1922 1923
      auto structNode = node.getStruct();
      if (!structNode.hasFields()) {
Kenton Varda's avatar
Kenton Varda committed
1924 1925 1926
        if (parent != nullptr) {
          getSchema();  // Make sure field exists in parent once the first child is added.
        }
1927 1928 1929 1930 1931 1932
        FieldSourceInfoBuilderPair result {
          structNode.initFields(childCount)[childInitializedCount],
          sourceInfo.initMembers(childCount)[childInitializedCount]
        };
        ++childInitializedCount;
        return result;
Kenton Varda's avatar
Kenton Varda committed
1933
      } else {
1934 1935 1936 1937 1938 1939
        FieldSourceInfoBuilderPair result {
          structNode.getFields()[childInitializedCount],
          sourceInfo.getMembers()[childInitializedCount]
        };
        ++childInitializedCount;
        return result;
Kenton Varda's avatar
Kenton Varda committed
1940 1941 1942
      }
    }

Kenton Varda's avatar
Kenton Varda committed
1943
    void finishGroup() {
Kenton Varda's avatar
Kenton Varda committed
1944 1945 1946 1947 1948
      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
1949
      }
Kenton Varda's avatar
Kenton Varda committed
1950 1951 1952 1953

      if (parent != nullptr) {
        uint64_t groupId = generateGroupId(parent->node.getId(), index);
        node.setId(groupId);
1954
        node.setScopeId(parent->node.getId());
1955
        getSchema().initGroup().setTypeId(groupId);
1956 1957 1958 1959 1960

        sourceInfo.setId(groupId);
        KJ_IF_MAYBE(dc, docComment) {
          sourceInfo.setDocComment(*dc);
        }
Kenton Varda's avatar
Kenton Varda committed
1961
      }
Kenton Varda's avatar
Kenton Varda committed
1962 1963 1964 1965
    }
  };

  std::multimap<uint, MemberInfo*> membersByOrdinal;
Kenton Varda's avatar
Kenton Varda committed
1966 1967
  // 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
1968

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

1972 1973
  void traverseUnion(const Declaration::Reader& decl,
                     List<Declaration>::Reader members, MemberInfo& parent,
Kenton Varda's avatar
Kenton Varda committed
1974
                     StructLayout::Union& layout, uint& codeOrder) {
Kenton Varda's avatar
Kenton Varda committed
1975
    if (members.size() < 2) {
1976
      errorReporter.addErrorOn(decl, "Union must have at least two members.");
Kenton Varda's avatar
Kenton Varda committed
1977 1978 1979
    }

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

1983 1984
      switch (member.which()) {
        case Declaration::FIELD: {
Kenton Varda's avatar
Kenton Varda committed
1985 1986
          parent.childCount++;
          // For layout purposes, pretend this field is enclosed in a one-member group.
Kenton Varda's avatar
Kenton Varda committed
1987
          StructLayout::Group& singletonGroup =
Kenton Varda's avatar
Kenton Varda committed
1988 1989 1990 1991
              arena.allocate<StructLayout::Group>(layout);
          memberInfo = &arena.allocate<MemberInfo>(parent, codeOrder++, member, singletonGroup,
                                                   true);
          allMembers.add(memberInfo);
Kenton Varda's avatar
Kenton Varda committed
1992 1993 1994 1995
          ordinal = member.getId().getOrdinal().getValue();
          break;
        }

1996
        case Declaration::UNION:
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
          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;
2014
            traverseUnion(member, member.getNestedDecls(), *memberInfo, unionLayout, subCodeOrder);
2015 2016 2017 2018
            if (member.getId().isOrdinal()) {
              ordinal = member.getId().getOrdinal().getValue();
            }
          }
Kenton Varda's avatar
Kenton Varda committed
2019 2020
          break;

2021
        case Declaration::GROUP: {
Kenton Varda's avatar
Kenton Varda committed
2022 2023 2024 2025 2026 2027 2028 2029
          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
2030 2031 2032 2033 2034 2035 2036 2037
          break;
        }

        default:
          // Ignore others.
          break;
      }

Kenton Varda's avatar
Kenton Varda committed
2038 2039
      KJ_IF_MAYBE(o, ordinal) {
        membersByOrdinal.insert(std::make_pair(*o, memberInfo));
Kenton Varda's avatar
Kenton Varda committed
2040 2041 2042 2043
      }
    }
  }

Kenton Varda's avatar
Kenton Varda committed
2044 2045 2046
  void traverseGroup(List<Declaration>::Reader members, MemberInfo& parent,
                     StructLayout::StructOrGroup& layout) {
    if (members.size() < 1) {
2047 2048
      errorReporter.addError(parent.startByte, parent.endByte,
                             "Group must have at least one member.");
Kenton Varda's avatar
Kenton Varda committed
2049 2050
    }

Kenton Varda's avatar
Kenton Varda committed
2051
    traverseTopOrGroup(members, parent, layout);
2052 2053
  }

Kenton Varda's avatar
Kenton Varda committed
2054 2055
  void traverseTopOrGroup(List<Declaration>::Reader members, MemberInfo& parent,
                          StructLayout::StructOrGroup& layout) {
2056 2057
    uint codeOrder = 0;

Kenton Varda's avatar
Kenton Varda committed
2058
    for (auto member: members) {
Kenton Varda's avatar
Kenton Varda committed
2059
      kj::Maybe<uint> ordinal;
Kenton Varda's avatar
Kenton Varda committed
2060 2061
      MemberInfo* memberInfo = nullptr;

2062 2063
      switch (member.which()) {
        case Declaration::FIELD: {
Kenton Varda's avatar
Kenton Varda committed
2064
          parent.childCount++;
Kenton Varda's avatar
Kenton Varda committed
2065
          memberInfo = &arena.allocate<MemberInfo>(
Kenton Varda's avatar
Kenton Varda committed
2066 2067
              parent, codeOrder++, member, layout, false);
          allMembers.add(memberInfo);
2068
          ordinal = member.getId().getOrdinal().getValue();
Kenton Varda's avatar
Kenton Varda committed
2069 2070 2071
          break;
        }

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

Kenton Varda's avatar
Kenton Varda committed
2075 2076
          uint independentSubCodeOrder = 0;
          uint* subCodeOrder = &independentSubCodeOrder;
Kenton Varda's avatar
Kenton Varda committed
2077 2078
          if (member.getName().getValue() == "") {
            memberInfo = &parent;
Kenton Varda's avatar
Kenton Varda committed
2079
            subCodeOrder = &codeOrder;
Kenton Varda's avatar
Kenton Varda committed
2080 2081 2082 2083 2084 2085 2086 2087 2088
          } else {
            parent.childCount++;
            memberInfo = &arena.allocate<MemberInfo>(
                parent, codeOrder++, member,
                newGroupNode(parent.node, member.getName().getValue()),
                false);
            allMembers.add(memberInfo);
          }
          memberInfo->unionScope = &unionLayout;
2089
          traverseUnion(member, member.getNestedDecls(), *memberInfo, unionLayout, *subCodeOrder);
2090
          if (member.getId().isOrdinal()) {
Kenton Varda's avatar
Kenton Varda committed
2091 2092 2093 2094 2095
            ordinal = member.getId().getOrdinal().getValue();
          }
          break;
        }

2096
        case Declaration::GROUP:
Kenton Varda's avatar
Kenton Varda committed
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
          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
2109 2110 2111 2112 2113 2114 2115
          break;

        default:
          // Ignore others.
          break;
      }

Kenton Varda's avatar
Kenton Varda committed
2116 2117
      KJ_IF_MAYBE(o, ordinal) {
        membersByOrdinal.insert(std::make_pair(*o, memberInfo));
Kenton Varda's avatar
Kenton Varda committed
2118 2119
      }
    }
Kenton Varda's avatar
Kenton Varda committed
2120 2121
  }

2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
  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));
    }
  }

2133 2134 2135 2136 2137 2138 2139
  NodeSourceInfoBuilderPair newGroupNode(schema::Node::Reader parent, kj::StringPtr name) {
    AuxNode aux {
      translator.orphanage.newOrphan<schema::Node>(),
      translator.orphanage.newOrphan<schema::Node::SourceInfo>()
    };
    auto node = aux.node.get();
    auto sourceInfo = aux.sourceInfo.get();
Kenton Varda's avatar
Kenton Varda committed
2140

2141
    // We'll set the ID and scope ID later.
Kenton Varda's avatar
Kenton Varda committed
2142 2143
    node.setDisplayName(kj::str(parent.getDisplayName(), '.', name));
    node.setDisplayNamePrefixLength(node.getDisplayName().size() - name.size());
2144
    node.setIsGeneric(parent.getIsGeneric());
Kenton Varda's avatar
Kenton Varda committed
2145 2146 2147 2148
    node.initStruct().setIsGroup(true);

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

2149 2150
    translator.groups.add(kj::mv(aux));
    return { node, sourceInfo };
Kenton Varda's avatar
Kenton Varda committed
2151
  }
2152 2153 2154 2155 2156 2157 2158 2159 2160

  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;

2161 2162 2163 2164
      // Make sure the exceptions added relating to
      // https://github.com/sandstorm-io/capnproto/issues/344 identify the affected field.
      KJ_CONTEXT(member.name);

2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
      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();
2176
          if (translator.compileType(member.fieldType, typeBuilder, implicitMethodParams)) {
2177
            if (member.hasDefaultValue) {
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
              if (member.isParam &&
                  member.fieldDefaultValue.isRelativeName() &&
                  member.fieldDefaultValue.getRelativeName().getValue() == "null") {
                // special case: parameter set null
                switch (typeBuilder.which()) {
                  case schema::Type::TEXT:
                  case schema::Type::DATA:
                  case schema::Type::LIST:
                  case schema::Type::STRUCT:
                  case schema::Type::INTERFACE:
                  case schema::Type::ANY_POINTER:
                    break;
                  default:
                    errorReporter.addErrorOn(member.fieldDefaultValue.getRelativeName(),
                        "Only pointer parameters can declare their default as 'null'.");
                    break;
                }
                translator.compileDefaultDefaultValue(typeBuilder, slot.initDefaultValue());
              } else {
                translator.compileBootstrapValue(member.fieldDefaultValue,
                                                 typeBuilder, slot.initDefaultValue());
              }
2200
              slot.setHadExplicitDefault(true);
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
            } 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;
2229
            case schema::Type::ANY_POINTER: lgSize = -2; break;
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 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 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291
          }

          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;
        }
      }

2292
      member->getSchema().adoptAnnotations(translator.compileAnnotationApplications(
2293 2294 2295 2296 2297 2298 2299 2300 2301
          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) {
2302
      auto groupBuilder = group.node.get().getStruct();
2303 2304 2305 2306 2307
      groupBuilder.setDataWordCount(structBuilder.getDataWordCount());
      groupBuilder.setPointerCount(structBuilder.getPointerCount());
      groupBuilder.setPreferredListEncoding(structBuilder.getPreferredListEncoding());
    }
  }
Kenton Varda's avatar
Kenton Varda committed
2308 2309
};

2310
void NodeTranslator::compileStruct(Void decl, List<Declaration>::Reader members,
Kenton Varda's avatar
Kenton Varda committed
2311
                                   schema::Node::Builder builder) {
2312
  StructTranslator(*this, noImplicitParams()).translate(decl, members, builder, sourceInfo.get());
Kenton Varda's avatar
Kenton Varda committed
2313 2314 2315 2316
}

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

Kenton Varda's avatar
Kenton Varda committed
2317
static kj::String expressionString(Expression::Reader name);
2318 2319 2320

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

2324 2325 2326 2327
  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
2328

2329
    KJ_IF_MAYBE(decl, compileDeclExpression(superclass, noImplicitParams())) {
Kenton Varda's avatar
Kenton Varda committed
2330 2331
      KJ_IF_MAYBE(kind, decl->getKind()) {
        if (*kind == Declaration::INTERFACE) {
2332
          auto s = superclassesBuilder[i];
2333
          s.setId(decl->getIdAndFillBrand([&]() { return s.initBrand(); }));
Kenton Varda's avatar
Kenton Varda committed
2334 2335 2336 2337
        } else {
          decl->addError(errorReporter, kj::str(
            "'", decl->toString(), "' is not an interface."));
        }
2338
      } else {
Kenton Varda's avatar
Kenton Varda committed
2339 2340 2341 2342
        // A variable?
        decl->addError(errorReporter, kj::str(
            "'", decl->toString(), "' is an unbound generic parameter. Currently we don't support "
            "extending these."));
2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
      }
    }
  }

  // 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());
2360
  auto sourceInfoList = sourceInfo.get().initMembers(methods.size());
2361 2362 2363 2364 2365 2366 2367 2368
  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();

2369 2370 2371
    auto ordinalDecl = methodDecl.getId().getOrdinal();
    dupDetector.check(ordinalDecl);
    uint16_t ordinal = ordinalDecl.getValue();
2372

2373 2374 2375 2376
    if (methodDecl.hasDocComment()) {
      sourceInfoList[i].setDocComment(methodDecl.getDocComment());
    }

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

2381 2382 2383 2384 2385 2386
    auto implicits = methodDecl.getParameters();
    auto implicitsBuilder = methodBuilder.initImplicitParameters(implicits.size());
    for (auto i: kj::indices(implicits)) {
      implicitsBuilder[i].setName(implicits[i].getName());
    }

2387
    methodBuilder.setParamStructType(compileParamList(
2388 2389
        methodDecl.getName().getValue(), ordinal, false,
        methodReader.getParams(), implicits,
2390
        [&]() { return methodBuilder.initParamBrand(); }));
2391

2392
    auto results = methodReader.getResults();
Kenton Varda's avatar
Kenton Varda committed
2393
    Declaration::ParamList::Reader resultList;
2394
    if (results.isExplicit()) {
Kenton Varda's avatar
Kenton Varda committed
2395
      resultList = results.getExplicit();
2396
    } else {
Kenton Varda's avatar
Kenton Varda committed
2397 2398 2399
      // 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.
2400
    }
Kenton Varda's avatar
Kenton Varda committed
2401
    methodBuilder.setResultStructType(compileParamList(
2402 2403
        methodDecl.getName().getValue(), ordinal, true,
        resultList, implicits,
2404
        [&]() { return methodBuilder.initResultBrand(); }));
2405 2406 2407 2408

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

2411
template <typename InitBrandFunc>
2412 2413
uint64_t NodeTranslator::compileParamList(
    kj::StringPtr methodName, uint16_t ordinal, bool isResults,
2414
    Declaration::ParamList::Reader paramList,
2415
    typename List<Declaration::BrandParameter>::Reader implicitParams,
2416
    InitBrandFunc&& initBrand) {
2417 2418 2419
  switch (paramList.which()) {
    case Declaration::ParamList::NAMED_LIST: {
      auto newStruct = orphanage.newOrphan<schema::Node>();
2420
      auto newSourceInfo = orphanage.newOrphan<schema::Node::SourceInfo>();
2421 2422 2423 2424 2425 2426 2427 2428
      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());
2429
      builder.setIsGeneric(parent.getIsGeneric() || implicitParams.size() > 0);
2430 2431 2432 2433
      builder.setScopeId(0);  // detached struct type

      builder.initStruct();

2434 2435 2436 2437 2438
      // 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 })
2439
          .translate(paramList.getNamedList(), builder, newSourceInfo.get());
2440
      uint64_t id = builder.getId();
2441
      paramStructs.add(AuxNode { kj::mv(newStruct), kj::mv(newSourceInfo) });
2442

2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458
      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);
2459 2460 2461
      return id;
    }
    case Declaration::ParamList::TYPE:
2462 2463
      KJ_IF_MAYBE(target, compileDeclExpression(
          paramList.getType(), ImplicitParams { 0, implicitParams })) {
Kenton Varda's avatar
Kenton Varda committed
2464 2465
        KJ_IF_MAYBE(kind, target->getKind()) {
          if (*kind == Declaration::STRUCT) {
2466
            return target->getIdAndFillBrand(kj::fwd<InitBrandFunc>(initBrand));
Kenton Varda's avatar
Kenton Varda committed
2467 2468 2469 2470 2471
          } else {
            errorReporter.addErrorOn(
                paramList.getType(),
                kj::str("'", expressionString(paramList.getType()), "' is not a struct type."));
          }
2472
        } else {
Kenton Varda's avatar
Kenton Varda committed
2473 2474 2475 2476 2477
          // 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;
2478 2479 2480 2481 2482 2483 2484
        }
      }
      return 0;
  }
  KJ_UNREACHABLE;
}

Kenton Varda's avatar
Kenton Varda committed
2485 2486
// -------------------------------------------------------------------

Kenton Varda's avatar
Kenton Varda committed
2487 2488 2489
static const char HEXDIGITS[] = "0123456789abcdef";

static kj::StringTree stringLiteral(kj::StringPtr chars) {
2490
  return kj::strTree('"', kj::encodeCEscape(chars), '"');
Kenton Varda's avatar
Kenton Varda committed
2491 2492 2493 2494
}

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

Kenton Varda's avatar
Kenton Varda committed
2496 2497 2498 2499
  for (byte b: data) {
    escaped.add(HEXDIGITS[b % 16]);
    escaped.add(HEXDIGITS[b / 16]);
    escaped.add(' ');
Kenton Varda's avatar
Kenton Varda committed
2500 2501
  }

Kenton Varda's avatar
Kenton Varda committed
2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513
  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
2514
    }
Kenton Varda's avatar
Kenton Varda committed
2515
    parts.add(kj::mv(part));
Kenton Varda's avatar
Kenton Varda committed
2516
  }
Kenton Varda's avatar
Kenton Varda committed
2517
  return kj::strTree("( ", kj::StringTree(parts.finish(), ", "), " )");
Kenton Varda's avatar
Kenton Varda committed
2518 2519
}

Kenton Varda's avatar
Kenton Varda committed
2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
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()));
2540 2541
    case Expression::EMBED:
      return kj::strTree("embed ", stringLiteral(exp.getEmbed().getValue()));
Kenton Varda's avatar
Kenton Varda committed
2542 2543 2544 2545 2546 2547 2548 2549 2550

    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
2551

Kenton Varda's avatar
Kenton Varda committed
2552 2553
    case Expression::TUPLE:
      return tupleLiteral(exp.getTuple());
Kenton Varda's avatar
Kenton Varda committed
2554

Kenton Varda's avatar
Kenton Varda committed
2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576
    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();
}

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

2577
kj::Maybe<NodeTranslator::BrandedDecl>
2578 2579 2580
NodeTranslator::compileDeclExpression(
    Expression::Reader source, ImplicitParams implicitMethodParams) {
  return localBrand->compileDeclExpression(source, resolver, implicitMethodParams);
Kenton Varda's avatar
Kenton Varda committed
2581
}
Kenton Varda's avatar
Kenton Varda committed
2582

2583 2584 2585 2586
/* 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);
2587
  KJ_IF_MAYBE(decl, scope->compileDeclExpression(expression, resolver, noImplicitParams())) {
2588
    return decl->asResolveResult(scope->getScopeId(), brandBuilder);
Kenton Varda's avatar
Kenton Varda committed
2589
  } else {
2590
    return nullptr;
Kenton Varda's avatar
Kenton Varda committed
2591 2592 2593
  }
}

2594 2595 2596
bool NodeTranslator::compileType(Expression::Reader source, schema::Type::Builder target,
                                 ImplicitParams implicitMethodParams) {
  KJ_IF_MAYBE(decl, compileDeclExpression(source, implicitMethodParams)) {
2597
    return decl->compileAsType(errorReporter, target);
Kenton Varda's avatar
Kenton Varda committed
2598
  } else {
2599
    return false;
Kenton Varda's avatar
Kenton Varda committed
2600 2601 2602 2603 2604 2605
  }
}

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

void NodeTranslator::compileDefaultDefaultValue(
Kenton Varda's avatar
Kenton Varda committed
2606
    schema::Type::Reader type, schema::Value::Builder target) {
Kenton Varda's avatar
Kenton Varda committed
2607
  switch (type.which()) {
Kenton Varda's avatar
Kenton Varda committed
2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
    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
2622

2623
    // 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
2624
    // TODO(cleanup):  Create a cleaner way to do this.
Kenton Varda's avatar
Kenton Varda committed
2625 2626
    case schema::Type::TEXT: target.adoptText(Orphan<Text>()); break;
    case schema::Type::DATA: target.adoptData(Orphan<Data>()); break;
2627 2628
    case schema::Type::STRUCT: target.initStruct(); break;
    case schema::Type::LIST: target.initList(); break;
2629
    case schema::Type::ANY_POINTER: target.initAnyPointer(); break;
Kenton Varda's avatar
Kenton Varda committed
2630
  }
Kenton Varda's avatar
Kenton Varda committed
2631 2632
}

2633 2634 2635
void NodeTranslator::compileBootstrapValue(
    Expression::Reader source, schema::Type::Reader type, schema::Value::Builder target,
    Schema typeScope) {
2636 2637 2638 2639
  // 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
2640
  switch (type.which()) {
Kenton Varda's avatar
Kenton Varda committed
2641 2642 2643
    case schema::Type::LIST:
    case schema::Type::STRUCT:
    case schema::Type::INTERFACE:
2644
    case schema::Type::ANY_POINTER:
2645
      unfinishedValues.add(UnfinishedValue { source, type, typeScope, target });
2646 2647
      break;

Kenton Varda's avatar
Kenton Varda committed
2648
    default:
2649
      // Primitive value.
2650
      compileValue(source, type, typeScope, target, true);
Kenton Varda's avatar
Kenton Varda committed
2651 2652
      break;
  }
2653 2654
}

Kenton Varda's avatar
Kenton Varda committed
2655
void NodeTranslator::compileValue(Expression::Reader source, schema::Type::Reader type,
2656 2657
                                  Schema typeScope, schema::Value::Builder target,
                                  bool isBootstrap) {
2658 2659 2660 2661 2662
  class ResolverGlue: public ValueTranslator::Resolver {
  public:
    inline ResolverGlue(NodeTranslator& translator, bool isBootstrap)
        : translator(translator), isBootstrap(isBootstrap) {}

Kenton Varda's avatar
Kenton Varda committed
2663
    kj::Maybe<DynamicValue::Reader> resolveConstant(Expression::Reader name) override {
2664 2665 2666
      return translator.readConstant(name, isBootstrap);
    }

2667 2668 2669 2670
    kj::Maybe<kj::Array<const byte>> readEmbed(LocatedText::Reader filename) override {
      return translator.readEmbed(filename);
    }

2671 2672 2673 2674 2675 2676 2677 2678
  private:
    NodeTranslator& translator;
    bool isBootstrap;
  };

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

2679 2680 2681 2682 2683 2684 2685 2686 2687 2688
  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));
      }
2689 2690 2691 2692
    }
  }
}

2693
kj::Maybe<Orphan<DynamicValue>> ValueTranslator::compileValue(Expression::Reader src, Type type) {
2694
  Orphan<DynamicValue> result = compileValueInner(src, type);
2695 2696 2697 2698 2699 2700 2701 2702 2703

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

    case DynamicValue::VOID:
      if (type.isVoid()) {
        return kj::mv(result);
2704 2705
      }
      break;
2706 2707 2708 2709

    case DynamicValue::BOOL:
      if (type.isBool()) {
        return kj::mv(result);
2710 2711
      }
      break;
2712 2713 2714 2715 2716 2717

    case DynamicValue::INT: {
      int64_t value = result.getReader().as<int64_t>();
      if (value < 0) {
        int64_t minValue = 1;
        switch (type.which()) {
2718 2719 2720 2721 2722 2723 2724 2725
          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;
2726 2727 2728 2729

          case schema::Type::FLOAT32:
          case schema::Type::FLOAT64:
            // Any integer is acceptable.
2730
            minValue = (int64_t)kj::minValue;
2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743
            break;

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

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

2744
    } // fallthrough -- value is positive, so we can just go on to the uint case below.
2745 2746 2747 2748

    case DynamicValue::UINT: {
      uint64_t maxValue = 0;
      switch (type.which()) {
2749 2750 2751 2752 2753 2754 2755 2756
        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;
2757 2758 2759 2760

        case schema::Type::FLOAT32:
        case schema::Type::FLOAT64:
          // Any integer is acceptable.
2761
          maxValue = (uint64_t)kj::maxValue;
2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777
          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);
2778 2779
      }
      break;
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790

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

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

2793 2794
    case DynamicValue::LIST:
      if (type.isList()) {
2795 2796
        if (result.getReader().as<DynamicList>().getSchema() == type.asList()) {
          return kj::mv(result);
2797
        }
2798 2799 2800 2801 2802 2803 2804 2805 2806
      } 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;
        }
2807 2808 2809 2810 2811
      }
      break;

    case DynamicValue::ENUM:
      if (type.isEnum()) {
2812 2813
        if (result.getReader().as<DynamicEnum>().getSchema() == type.asEnum()) {
          return kj::mv(result);
2814 2815 2816 2817 2818 2819
        }
      }
      break;

    case DynamicValue::STRUCT:
      if (type.isStruct()) {
2820 2821
        if (result.getReader().as<DynamicStruct>().getSchema() == type.asStruct()) {
          return kj::mv(result);
2822
        }
2823 2824 2825 2826 2827 2828 2829 2830 2831
      } 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;
        }
2832 2833 2834
      }
      break;

2835
    case DynamicValue::CAPABILITY:
2836 2837
      KJ_FAIL_ASSERT("Interfaces can't have literal values.");

2838 2839
    case DynamicValue::ANY_POINTER:
      KJ_FAIL_ASSERT("AnyPointers can't have literal values.");
2840
  }
2841 2842 2843

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

2846
Orphan<DynamicValue> ValueTranslator::compileValueInner(Expression::Reader src, Type type) {
2847
  switch (src.which()) {
Kenton Varda's avatar
Kenton Varda committed
2848 2849 2850 2851 2852 2853 2854
    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()) {
2855 2856
        KJ_IF_MAYBE(enumerant, type.asEnum().findEnumerantByName(id)) {
          return DynamicEnum(*enumerant);
Kenton Varda's avatar
Kenton Varda committed
2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869
        }
      } 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();
2870 2871
        }
      }
Kenton Varda's avatar
Kenton Varda committed
2872

Kenton Varda's avatar
Kenton Varda committed
2873 2874
      // Apparently not a literal. Try resolving it.
      KJ_IF_MAYBE(constValue, resolver.resolveConstant(src)) {
2875
        return orphanage.newOrphanCopy(*constValue);
Kenton Varda's avatar
Kenton Varda committed
2876 2877
      } else {
        return nullptr;
Kenton Varda's avatar
Kenton Varda committed
2878 2879
      }
    }
Kenton Varda's avatar
Kenton Varda committed
2880

Kenton Varda's avatar
Kenton Varda committed
2881 2882 2883 2884 2885 2886 2887 2888 2889 2890
    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;
      }

2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946
    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
2947
    case Expression::POSITIVE_INT:
2948
      return src.getPositiveInt();
Kenton Varda's avatar
Kenton Varda committed
2949

Kenton Varda's avatar
Kenton Varda committed
2950
    case Expression::NEGATIVE_INT: {
2951
      uint64_t nValue = src.getNegativeInt();
2952
      if (nValue > ((uint64_t)kj::maxValue >> 1) + 1) {
2953
        errorReporter.addErrorOn(src, "Integer is too big to be negative.");
2954
        return nullptr;
2955
      } else {
2956
        return kj::implicitCast<int64_t>(-nValue);
Kenton Varda's avatar
Kenton Varda committed
2957
      }
2958
    }
Kenton Varda's avatar
Kenton Varda committed
2959

Kenton Varda's avatar
Kenton Varda committed
2960
    case Expression::FLOAT:
2961
      return src.getFloat();
2962
      break;
Kenton Varda's avatar
Kenton Varda committed
2963

Kenton Varda's avatar
Kenton Varda committed
2964
    case Expression::STRING:
2965 2966
      if (type.isData()) {
        Text::Reader text = src.getString();
2967
        return orphanage.newOrphanCopy(Data::Reader(text.asBytes()));
2968 2969 2970
      } else {
        return orphanage.newOrphanCopy(src.getString());
      }
Kenton Varda's avatar
Kenton Varda committed
2971 2972
      break;

Kenton Varda's avatar
Kenton Varda committed
2973
    case Expression::BINARY:
Jason Choy's avatar
Jason Choy committed
2974 2975 2976 2977 2978 2979
      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
2980
    case Expression::LIST: {
2981
      if (!type.isList()) {
2982
        errorReporter.addErrorOn(src, kj::str("Type mismatch; expected ", makeTypeName(type), "."));
2983 2984
        return nullptr;
      }
2985 2986 2987 2988 2989 2990 2991 2992
      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));
2993
        }
Kenton Varda's avatar
Kenton Varda committed
2994
      }
2995
      return kj::mv(result);
Kenton Varda's avatar
Kenton Varda committed
2996 2997
    }

Kenton Varda's avatar
Kenton Varda committed
2998
    case Expression::TUPLE: {
2999
      if (!type.isStruct()) {
3000
        errorReporter.addErrorOn(src, kj::str("Type mismatch; expected ", makeTypeName(type), "."));
3001 3002
        return nullptr;
      }
3003 3004 3005 3006
      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
3007 3008
    }

Kenton Varda's avatar
Kenton Varda committed
3009
    case Expression::UNKNOWN:
Kenton Varda's avatar
Kenton Varda committed
3010
      // Ignore earlier error.
3011 3012 3013 3014 3015 3016
      return nullptr;
  }

  KJ_UNREACHABLE;
}

3017
void ValueTranslator::fillStructValue(DynamicStruct::Builder builder,
Kenton Varda's avatar
Kenton Varda committed
3018
                                      List<Expression::Param>::Reader assignments) {
3019
  for (auto assignment: assignments) {
Kenton Varda's avatar
Kenton Varda committed
3020 3021 3022 3023 3024 3025 3026 3027
    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:
3028
            KJ_IF_MAYBE(compiledValue, compileValue(value, field->getType())) {
Kenton Varda's avatar
Kenton Varda committed
3029 3030 3031
              builder.adopt(*field, kj::mv(*compiledValue));
            }
            break;
3032

Kenton Varda's avatar
Kenton Varda committed
3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043
          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(), "'."));
3044 3045
      }
    } else {
Kenton Varda's avatar
Kenton Varda committed
3046
      errorReporter.addErrorOn(assignment.getValue(), kj::str("Missing field name."));
3047 3048 3049 3050
    }
  }
}

3051 3052 3053
kj::String ValueTranslator::makeNodeName(Schema schema) {
  schema::Node::Reader proto = schema.getProto();
  return kj::str(proto.getDisplayName().slice(proto.getDisplayNamePrefixLength()));
3054 3055
}

3056
kj::String ValueTranslator::makeTypeName(Type type) {
3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071
  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");
3072
    case schema::Type::LIST:
3073 3074 3075 3076
      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());
3077
    case schema::Type::ANY_POINTER: return kj::str("AnyPointer");
Kenton Varda's avatar
Kenton Varda committed
3078
  }
3079
  KJ_UNREACHABLE;
Kenton Varda's avatar
Kenton Varda committed
3080 3081
}

3082
kj::Maybe<DynamicValue::Reader> NodeTranslator::readConstant(
Kenton Varda's avatar
Kenton Varda committed
3083
    Expression::Reader source, bool isBootstrap) {
3084
  // Look up the constant decl.
3085
  NodeTranslator::BrandedDecl constDecl = nullptr;
3086
  KJ_IF_MAYBE(decl, compileDeclExpression(source, noImplicitParams())) {
3087 3088 3089 3090 3091
    constDecl = *decl;
  } else {
    // Lookup will have reported an error.
    return nullptr;
  }
3092

3093 3094 3095 3096 3097 3098
  // 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;
  }
3099

3100
  // Extract the ID and brand.
3101
  MallocMessageBuilder builder(256);
3102
  auto constBrand = builder.getRoot<schema::Brand>();
3103
  uint64_t id = constDecl.getIdAndFillBrand([&]() { return constBrand; });
3104

3105 3106
  // Look up the schema -- we'll need this to compile the constant's type.
  Schema constSchema;
3107
  KJ_IF_MAYBE(s, resolver.resolveBootstrapSchema(id, constBrand)) {
3108 3109 3110 3111 3112
    constSchema = *s;
  } else {
    // The constant's schema is broken for reasons already reported.
    return nullptr;
  }
3113

3114 3115 3116 3117 3118 3119 3120 3121
  // 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;
3122
    } else {
3123
      // The constant's final schema is broken for reasons already reported.
3124
      return nullptr;
3125 3126 3127
    }
  }

3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157
  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(),
3158
                                                       schema::Brand::Reader())) {
3159 3160 3161 3162
      auto scopeReader = scope->getProto();
      kj::StringPtr parent;
      if (scopeReader.isFile()) {
        parent = "";
3163
      } else {
3164
        parent = scopeReader.getDisplayName().slice(scopeReader.getDisplayNamePrefixLength());
3165
      }
3166
      kj::StringPtr id = source.getRelativeName().getValue();
3167

3168 3169 3170 3171 3172 3173
      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."));
    }
  }
3174

3175
  return constValue;
3176 3177
}

3178 3179 3180 3181 3182 3183 3184 3185 3186 3187
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
3188
Orphan<List<schema::Annotation>> NodeTranslator::compileAnnotationApplications(
Kenton Varda's avatar
Kenton Varda committed
3189 3190
    List<Declaration::AnnotationApplication>::Reader annotations,
    kj::StringPtr targetsFlagName) {
3191
  if (annotations.size() == 0 || !compileAnnotations) {
3192
    // Return null.
Kenton Varda's avatar
Kenton Varda committed
3193
    return Orphan<List<schema::Annotation>>();
3194 3195
  }

Kenton Varda's avatar
Kenton Varda committed
3196
  auto result = orphanage.newOrphan<List<schema::Annotation>>(annotations.size());
3197 3198 3199 3200
  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
3201
    schema::Annotation::Builder annotationBuilder = builder[i];
3202 3203

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

3206
    auto name = annotation.getName();
3207
    KJ_IF_MAYBE(decl, compileDeclExpression(name, noImplicitParams())) {
Kenton Varda's avatar
Kenton Varda committed
3208 3209 3210 3211 3212
      KJ_IF_MAYBE(kind, decl->getKind()) {
        if (*kind != Declaration::ANNOTATION) {
          errorReporter.addErrorOn(name, kj::str(
              "'", expressionString(name), "' is not an annotation."));
        } else {
3213
          annotationBuilder.setId(decl->getIdAndFillBrand(
3214
              [&]() { return annotationBuilder.initBrand(); }));
Kenton Varda's avatar
Kenton Varda committed
3215
          KJ_IF_MAYBE(annotationSchema,
3216
                      resolver.resolveBootstrapSchema(annotationBuilder.getId(),
3217
                                                      annotationBuilder.getBrand())) {
Kenton Varda's avatar
Kenton Varda committed
3218 3219 3220 3221 3222
            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."));
            }
3223

Kenton Varda's avatar
Kenton Varda committed
3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239
            // 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(),
3240 3241
                                      annotationBuilder.getValue(),
                                      *annotationSchema);
Kenton Varda's avatar
Kenton Varda committed
3242 3243
                break;
            }
3244
          }
3245
        }
Kenton Varda's avatar
Kenton Varda committed
3246 3247 3248
      } else if (*kind != Declaration::ANNOTATION) {
        errorReporter.addErrorOn(name, kj::str(
            "'", expressionString(name), "' is not an annotation."));
3249 3250 3251
      }
    }
  }
Kenton Varda's avatar
Kenton Varda committed
3252

3253
  return result;
Kenton Varda's avatar
Kenton Varda committed
3254 3255 3256 3257
}

}  // namespace compiler
}  // namespace capnp