node-translator.c++ 121 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>
31
#include <capnp/stream.capnp.h>
Kenton Varda's avatar
Kenton Varda committed
32 33 34 35

namespace capnp {
namespace compiler {

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

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

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

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

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

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

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

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

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

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

194 195
    void addVoid() override {}

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

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

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

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

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

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

447 448 449
    bool hasMembers = false;

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

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

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

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

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

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

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

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

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

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

private:
  Top top;
};

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        return true;
      }

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

      case Declaration::BUILTIN_OBJECT:
        addError(errorReporter,
            "As of Cap'n Proto 0.4, 'Object' has been renamed to 'AnyPointer'.  Sorry for the "
            "inconvenience, and thanks for being an early adopter.  :)");
1001
        // fallthrough
1002
      case Declaration::BUILTIN_ANY_POINTER:
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
        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();
1013
        return true;
Kenton Varda's avatar
Kenton Varda committed
1014

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

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

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

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

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

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

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

  KJ_UNREACHABLE;
1243 1244 1245
}

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

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

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

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

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

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

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

Kenton Varda's avatar
Kenton Varda committed
1389
NodeTranslator::NodeSet NodeTranslator::getBootstrapNode() {
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
  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());
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      switch (decl.which()) {
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
        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
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 1583
        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
1584 1585
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
  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
1783 1784 1785 1786 1787 1788 1789
  struct MemberInfo {
    MemberInfo* parent;
    // The MemberInfo for the parent scope.

    uint codeOrder;
    // Code order within the parent.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        default:
          // Ignore others.
          break;
      }

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

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

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

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

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

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

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

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

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

        default:
          // Ignore others.
          break;
      }

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

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

2134 2135 2136 2137 2138 2139 2140
  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
2141

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

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

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

  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;

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

2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
      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();
2177
          if (translator.compileType(member.fieldType, typeBuilder, implicitMethodParams)) {
2178
            if (member.hasDefaultValue) {
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
              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());
              }
2201
              slot.setHadExplicitDefault(true);
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 2229
            } 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;
2230
            case schema::Type::ANY_POINTER: lgSize = -2; break;
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 2292
          }

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

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

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

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

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

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

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

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

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

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

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

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

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

2388 2389 2390 2391
    auto params = methodReader.getParams();
    if (params.isStream()) {
      errorReporter.addErrorOn(params, "'stream' can only appear after '->', not before.");
    }
2392
    methodBuilder.setParamStructType(compileParamList(
2393
        methodDecl.getName().getValue(), ordinal, false,
2394
        params, implicits,
2395
        [&]() { return methodBuilder.initParamBrand(); }));
2396

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

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

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

      builder.initStruct();

2439 2440 2441 2442 2443
      // 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 })
2444
          .translate(paramList.getNamedList(), builder, newSourceInfo.get());
2445
      uint64_t id = builder.getId();
2446
      paramStructs.add(AuxNode { kj::mv(newStruct), kj::mv(newSourceInfo) });
2447

2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463
      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);
2464 2465 2466
      return id;
    }
    case Declaration::ParamList::TYPE:
2467 2468
      KJ_IF_MAYBE(target, compileDeclExpression(
          paramList.getType(), ImplicitParams { 0, implicitParams })) {
Kenton Varda's avatar
Kenton Varda committed
2469 2470
        KJ_IF_MAYBE(kind, target->getKind()) {
          if (*kind == Declaration::STRUCT) {
2471
            return target->getIdAndFillBrand(kj::fwd<InitBrandFunc>(initBrand));
Kenton Varda's avatar
Kenton Varda committed
2472 2473 2474 2475 2476
          } else {
            errorReporter.addErrorOn(
                paramList.getType(),
                kj::str("'", expressionString(paramList.getType()), "' is not a struct type."));
          }
2477
        } else {
Kenton Varda's avatar
Kenton Varda committed
2478 2479 2480 2481 2482
          // 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;
2483 2484 2485
        }
      }
      return 0;
2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499
    case Declaration::ParamList::STREAM:
      KJ_IF_MAYBE(streamCapnp, resolver.resolveImport("/capnp/stream.capnp")) {
        if (streamCapnp->resolver->resolveMember("StreamResult") == nullptr) {
          errorReporter.addErrorOn(paramList,
              "The version of '/capnp/stream.capnp' found in your import path does not appear "
              "to be the official one; it is missing the declaration of StreamResult.");
        }
      } else {
        errorReporter.addErrorOn(paramList,
            "A method declaration uses streaming, but '/capnp/stream.capnp' is not found "
            "in the import path. This is a standard file that should always be installed "
            "with the Cap'n Proto compiler.");
      }
      return typeId<StreamResult>();
2500 2501 2502 2503
  }
  KJ_UNREACHABLE;
}

Kenton Varda's avatar
Kenton Varda committed
2504 2505
// -------------------------------------------------------------------

Kenton Varda's avatar
Kenton Varda committed
2506 2507 2508
static const char HEXDIGITS[] = "0123456789abcdef";

static kj::StringTree stringLiteral(kj::StringPtr chars) {
2509
  return kj::strTree('"', kj::encodeCEscape(chars), '"');
Kenton Varda's avatar
Kenton Varda committed
2510 2511 2512 2513
}

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

Kenton Varda's avatar
Kenton Varda committed
2515 2516 2517 2518
  for (byte b: data) {
    escaped.add(HEXDIGITS[b % 16]);
    escaped.add(HEXDIGITS[b / 16]);
    escaped.add(' ');
Kenton Varda's avatar
Kenton Varda committed
2519 2520
  }

Kenton Varda's avatar
Kenton Varda committed
2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
  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
2533
    }
Kenton Varda's avatar
Kenton Varda committed
2534
    parts.add(kj::mv(part));
Kenton Varda's avatar
Kenton Varda committed
2535
  }
Kenton Varda's avatar
Kenton Varda committed
2536
  return kj::strTree("( ", kj::StringTree(parts.finish(), ", "), " )");
Kenton Varda's avatar
Kenton Varda committed
2537 2538
}

Kenton Varda's avatar
Kenton Varda committed
2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558
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()));
2559 2560
    case Expression::EMBED:
      return kj::strTree("embed ", stringLiteral(exp.getEmbed().getValue()));
Kenton Varda's avatar
Kenton Varda committed
2561 2562 2563 2564 2565 2566 2567 2568 2569

    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
2570

Kenton Varda's avatar
Kenton Varda committed
2571 2572
    case Expression::TUPLE:
      return tupleLiteral(exp.getTuple());
Kenton Varda's avatar
Kenton Varda committed
2573

Kenton Varda's avatar
Kenton Varda committed
2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595
    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();
}

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

2596
kj::Maybe<NodeTranslator::BrandedDecl>
2597 2598 2599
NodeTranslator::compileDeclExpression(
    Expression::Reader source, ImplicitParams implicitMethodParams) {
  return localBrand->compileDeclExpression(source, resolver, implicitMethodParams);
Kenton Varda's avatar
Kenton Varda committed
2600
}
Kenton Varda's avatar
Kenton Varda committed
2601

2602 2603 2604 2605
/* 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);
2606
  KJ_IF_MAYBE(decl, scope->compileDeclExpression(expression, resolver, noImplicitParams())) {
2607
    return decl->asResolveResult(scope->getScopeId(), brandBuilder);
Kenton Varda's avatar
Kenton Varda committed
2608
  } else {
2609
    return nullptr;
Kenton Varda's avatar
Kenton Varda committed
2610 2611 2612
  }
}

2613 2614 2615
bool NodeTranslator::compileType(Expression::Reader source, schema::Type::Builder target,
                                 ImplicitParams implicitMethodParams) {
  KJ_IF_MAYBE(decl, compileDeclExpression(source, implicitMethodParams)) {
2616
    return decl->compileAsType(errorReporter, target);
Kenton Varda's avatar
Kenton Varda committed
2617
  } else {
2618
    return false;
Kenton Varda's avatar
Kenton Varda committed
2619 2620 2621 2622 2623 2624
  }
}

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

void NodeTranslator::compileDefaultDefaultValue(
Kenton Varda's avatar
Kenton Varda committed
2625
    schema::Type::Reader type, schema::Value::Builder target) {
Kenton Varda's avatar
Kenton Varda committed
2626
  switch (type.which()) {
Kenton Varda's avatar
Kenton Varda committed
2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640
    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
2641

2642
    // 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
2643
    // TODO(cleanup):  Create a cleaner way to do this.
Kenton Varda's avatar
Kenton Varda committed
2644 2645
    case schema::Type::TEXT: target.adoptText(Orphan<Text>()); break;
    case schema::Type::DATA: target.adoptData(Orphan<Data>()); break;
2646 2647
    case schema::Type::STRUCT: target.initStruct(); break;
    case schema::Type::LIST: target.initList(); break;
2648
    case schema::Type::ANY_POINTER: target.initAnyPointer(); break;
Kenton Varda's avatar
Kenton Varda committed
2649
  }
Kenton Varda's avatar
Kenton Varda committed
2650 2651
}

2652 2653 2654
void NodeTranslator::compileBootstrapValue(
    Expression::Reader source, schema::Type::Reader type, schema::Value::Builder target,
    Schema typeScope) {
2655 2656 2657 2658
  // 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
2659
  switch (type.which()) {
Kenton Varda's avatar
Kenton Varda committed
2660 2661 2662
    case schema::Type::LIST:
    case schema::Type::STRUCT:
    case schema::Type::INTERFACE:
2663
    case schema::Type::ANY_POINTER:
2664
      unfinishedValues.add(UnfinishedValue { source, type, typeScope, target });
2665 2666
      break;

Kenton Varda's avatar
Kenton Varda committed
2667
    default:
2668
      // Primitive value.
2669
      compileValue(source, type, typeScope, target, true);
Kenton Varda's avatar
Kenton Varda committed
2670 2671
      break;
  }
2672 2673
}

Kenton Varda's avatar
Kenton Varda committed
2674
void NodeTranslator::compileValue(Expression::Reader source, schema::Type::Reader type,
2675 2676
                                  Schema typeScope, schema::Value::Builder target,
                                  bool isBootstrap) {
2677 2678 2679 2680 2681
  class ResolverGlue: public ValueTranslator::Resolver {
  public:
    inline ResolverGlue(NodeTranslator& translator, bool isBootstrap)
        : translator(translator), isBootstrap(isBootstrap) {}

Kenton Varda's avatar
Kenton Varda committed
2682
    kj::Maybe<DynamicValue::Reader> resolveConstant(Expression::Reader name) override {
2683 2684 2685
      return translator.readConstant(name, isBootstrap);
    }

2686 2687 2688 2689
    kj::Maybe<kj::Array<const byte>> readEmbed(LocatedText::Reader filename) override {
      return translator.readEmbed(filename);
    }

2690 2691 2692 2693 2694 2695 2696 2697
  private:
    NodeTranslator& translator;
    bool isBootstrap;
  };

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

2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
  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));
      }
2708 2709 2710 2711
    }
  }
}

2712
kj::Maybe<Orphan<DynamicValue>> ValueTranslator::compileValue(Expression::Reader src, Type type) {
2713
  Orphan<DynamicValue> result = compileValueInner(src, type);
2714 2715 2716 2717 2718 2719 2720 2721 2722

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

    case DynamicValue::VOID:
      if (type.isVoid()) {
        return kj::mv(result);
2723 2724
      }
      break;
2725 2726 2727 2728

    case DynamicValue::BOOL:
      if (type.isBool()) {
        return kj::mv(result);
2729 2730
      }
      break;
2731 2732 2733 2734 2735 2736

    case DynamicValue::INT: {
      int64_t value = result.getReader().as<int64_t>();
      if (value < 0) {
        int64_t minValue = 1;
        switch (type.which()) {
2737 2738 2739 2740 2741 2742 2743 2744
          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;
2745 2746 2747 2748

          case schema::Type::FLOAT32:
          case schema::Type::FLOAT64:
            // Any integer is acceptable.
2749
            minValue = (int64_t)kj::minValue;
2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762
            break;

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

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

2763
    } // fallthrough -- value is positive, so we can just go on to the uint case below.
2764 2765 2766 2767

    case DynamicValue::UINT: {
      uint64_t maxValue = 0;
      switch (type.which()) {
2768 2769 2770 2771 2772 2773 2774 2775
        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;
2776 2777 2778 2779

        case schema::Type::FLOAT32:
        case schema::Type::FLOAT64:
          // Any integer is acceptable.
2780
          maxValue = (uint64_t)kj::maxValue;
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
          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);
2797 2798
      }
      break;
2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809

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

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

2812 2813
    case DynamicValue::LIST:
      if (type.isList()) {
2814 2815
        if (result.getReader().as<DynamicList>().getSchema() == type.asList()) {
          return kj::mv(result);
2816
        }
2817 2818 2819 2820 2821 2822 2823 2824 2825
      } 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;
        }
2826 2827 2828 2829 2830
      }
      break;

    case DynamicValue::ENUM:
      if (type.isEnum()) {
2831 2832
        if (result.getReader().as<DynamicEnum>().getSchema() == type.asEnum()) {
          return kj::mv(result);
2833 2834 2835 2836 2837 2838
        }
      }
      break;

    case DynamicValue::STRUCT:
      if (type.isStruct()) {
2839 2840
        if (result.getReader().as<DynamicStruct>().getSchema() == type.asStruct()) {
          return kj::mv(result);
2841
        }
2842 2843 2844 2845 2846 2847 2848 2849 2850
      } 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;
        }
2851 2852 2853
      }
      break;

2854
    case DynamicValue::CAPABILITY:
2855 2856
      KJ_FAIL_ASSERT("Interfaces can't have literal values.");

2857 2858
    case DynamicValue::ANY_POINTER:
      KJ_FAIL_ASSERT("AnyPointers can't have literal values.");
2859
  }
2860 2861 2862

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

2865
Orphan<DynamicValue> ValueTranslator::compileValueInner(Expression::Reader src, Type type) {
2866
  switch (src.which()) {
Kenton Varda's avatar
Kenton Varda committed
2867 2868 2869 2870 2871 2872 2873
    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()) {
2874 2875
        KJ_IF_MAYBE(enumerant, type.asEnum().findEnumerantByName(id)) {
          return DynamicEnum(*enumerant);
Kenton Varda's avatar
Kenton Varda committed
2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888
        }
      } 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();
2889 2890
        }
      }
Kenton Varda's avatar
Kenton Varda committed
2891

Kenton Varda's avatar
Kenton Varda committed
2892 2893
      // Apparently not a literal. Try resolving it.
      KJ_IF_MAYBE(constValue, resolver.resolveConstant(src)) {
2894
        return orphanage.newOrphanCopy(*constValue);
Kenton Varda's avatar
Kenton Varda committed
2895 2896
      } else {
        return nullptr;
Kenton Varda's avatar
Kenton Varda committed
2897 2898
      }
    }
Kenton Varda's avatar
Kenton Varda committed
2899

Kenton Varda's avatar
Kenton Varda committed
2900 2901 2902 2903 2904 2905 2906 2907 2908 2909
    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;
      }

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 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
    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
2966
    case Expression::POSITIVE_INT:
2967
      return src.getPositiveInt();
Kenton Varda's avatar
Kenton Varda committed
2968

Kenton Varda's avatar
Kenton Varda committed
2969
    case Expression::NEGATIVE_INT: {
2970
      uint64_t nValue = src.getNegativeInt();
2971
      if (nValue > ((uint64_t)kj::maxValue >> 1) + 1) {
2972
        errorReporter.addErrorOn(src, "Integer is too big to be negative.");
2973
        return nullptr;
2974
      } else {
2975
        return kj::implicitCast<int64_t>(-nValue);
Kenton Varda's avatar
Kenton Varda committed
2976
      }
2977
    }
Kenton Varda's avatar
Kenton Varda committed
2978

Kenton Varda's avatar
Kenton Varda committed
2979
    case Expression::FLOAT:
2980
      return src.getFloat();
2981
      break;
Kenton Varda's avatar
Kenton Varda committed
2982

Kenton Varda's avatar
Kenton Varda committed
2983
    case Expression::STRING:
2984 2985
      if (type.isData()) {
        Text::Reader text = src.getString();
2986
        return orphanage.newOrphanCopy(Data::Reader(text.asBytes()));
2987 2988 2989
      } else {
        return orphanage.newOrphanCopy(src.getString());
      }
Kenton Varda's avatar
Kenton Varda committed
2990 2991
      break;

Kenton Varda's avatar
Kenton Varda committed
2992
    case Expression::BINARY:
Jason Choy's avatar
Jason Choy committed
2993 2994 2995 2996 2997 2998
      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
2999
    case Expression::LIST: {
3000
      if (!type.isList()) {
3001
        errorReporter.addErrorOn(src, kj::str("Type mismatch; expected ", makeTypeName(type), "."));
3002 3003
        return nullptr;
      }
3004 3005 3006 3007 3008 3009 3010 3011
      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));
3012
        }
Kenton Varda's avatar
Kenton Varda committed
3013
      }
3014
      return kj::mv(result);
Kenton Varda's avatar
Kenton Varda committed
3015 3016
    }

Kenton Varda's avatar
Kenton Varda committed
3017
    case Expression::TUPLE: {
3018
      if (!type.isStruct()) {
3019
        errorReporter.addErrorOn(src, kj::str("Type mismatch; expected ", makeTypeName(type), "."));
3020 3021
        return nullptr;
      }
3022 3023 3024 3025
      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
3026 3027
    }

Kenton Varda's avatar
Kenton Varda committed
3028
    case Expression::UNKNOWN:
Kenton Varda's avatar
Kenton Varda committed
3029
      // Ignore earlier error.
3030 3031 3032 3033 3034 3035
      return nullptr;
  }

  KJ_UNREACHABLE;
}

3036
void ValueTranslator::fillStructValue(DynamicStruct::Builder builder,
Kenton Varda's avatar
Kenton Varda committed
3037
                                      List<Expression::Param>::Reader assignments) {
3038
  for (auto assignment: assignments) {
Kenton Varda's avatar
Kenton Varda committed
3039 3040 3041 3042 3043 3044 3045 3046
    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:
3047
            KJ_IF_MAYBE(compiledValue, compileValue(value, field->getType())) {
Kenton Varda's avatar
Kenton Varda committed
3048 3049 3050
              builder.adopt(*field, kj::mv(*compiledValue));
            }
            break;
3051

Kenton Varda's avatar
Kenton Varda committed
3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062
          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(), "'."));
3063 3064
      }
    } else {
Kenton Varda's avatar
Kenton Varda committed
3065
      errorReporter.addErrorOn(assignment.getValue(), kj::str("Missing field name."));
3066 3067 3068 3069
    }
  }
}

3070 3071 3072
kj::String ValueTranslator::makeNodeName(Schema schema) {
  schema::Node::Reader proto = schema.getProto();
  return kj::str(proto.getDisplayName().slice(proto.getDisplayNamePrefixLength()));
3073 3074
}

3075
kj::String ValueTranslator::makeTypeName(Type type) {
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090
  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");
3091
    case schema::Type::LIST:
3092 3093 3094 3095
      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());
3096
    case schema::Type::ANY_POINTER: return kj::str("AnyPointer");
Kenton Varda's avatar
Kenton Varda committed
3097
  }
3098
  KJ_UNREACHABLE;
Kenton Varda's avatar
Kenton Varda committed
3099 3100
}

3101
kj::Maybe<DynamicValue::Reader> NodeTranslator::readConstant(
Kenton Varda's avatar
Kenton Varda committed
3102
    Expression::Reader source, bool isBootstrap) {
3103
  // Look up the constant decl.
3104
  NodeTranslator::BrandedDecl constDecl = nullptr;
3105
  KJ_IF_MAYBE(decl, compileDeclExpression(source, noImplicitParams())) {
3106 3107 3108 3109 3110
    constDecl = *decl;
  } else {
    // Lookup will have reported an error.
    return nullptr;
  }
3111

3112 3113 3114 3115 3116 3117
  // 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;
  }
3118

3119
  // Extract the ID and brand.
3120
  MallocMessageBuilder builder(256);
3121
  auto constBrand = builder.getRoot<schema::Brand>();
3122
  uint64_t id = constDecl.getIdAndFillBrand([&]() { return constBrand; });
3123

3124 3125
  // Look up the schema -- we'll need this to compile the constant's type.
  Schema constSchema;
3126
  KJ_IF_MAYBE(s, resolver.resolveBootstrapSchema(id, constBrand)) {
3127 3128 3129 3130 3131
    constSchema = *s;
  } else {
    // The constant's schema is broken for reasons already reported.
    return nullptr;
  }
3132

3133 3134 3135 3136 3137 3138 3139 3140
  // 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;
3141
    } else {
3142
      // The constant's final schema is broken for reasons already reported.
3143
      return nullptr;
3144 3145 3146
    }
  }

3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176
  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(),
3177
                                                       schema::Brand::Reader())) {
3178 3179 3180 3181
      auto scopeReader = scope->getProto();
      kj::StringPtr parent;
      if (scopeReader.isFile()) {
        parent = "";
3182
      } else {
3183
        parent = scopeReader.getDisplayName().slice(scopeReader.getDisplayNamePrefixLength());
3184
      }
3185
      kj::StringPtr id = source.getRelativeName().getValue();
3186

3187 3188 3189 3190 3191 3192
      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."));
    }
  }
3193

3194
  return constValue;
3195 3196
}

3197 3198 3199 3200 3201 3202 3203 3204 3205 3206
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
3207
Orphan<List<schema::Annotation>> NodeTranslator::compileAnnotationApplications(
Kenton Varda's avatar
Kenton Varda committed
3208 3209
    List<Declaration::AnnotationApplication>::Reader annotations,
    kj::StringPtr targetsFlagName) {
3210
  if (annotations.size() == 0 || !compileAnnotations) {
3211
    // Return null.
Kenton Varda's avatar
Kenton Varda committed
3212
    return Orphan<List<schema::Annotation>>();
3213 3214
  }

Kenton Varda's avatar
Kenton Varda committed
3215
  auto result = orphanage.newOrphan<List<schema::Annotation>>(annotations.size());
3216 3217 3218 3219
  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
3220
    schema::Annotation::Builder annotationBuilder = builder[i];
3221 3222

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

3225
    auto name = annotation.getName();
3226
    KJ_IF_MAYBE(decl, compileDeclExpression(name, noImplicitParams())) {
Kenton Varda's avatar
Kenton Varda committed
3227 3228 3229 3230 3231
      KJ_IF_MAYBE(kind, decl->getKind()) {
        if (*kind != Declaration::ANNOTATION) {
          errorReporter.addErrorOn(name, kj::str(
              "'", expressionString(name), "' is not an annotation."));
        } else {
3232
          annotationBuilder.setId(decl->getIdAndFillBrand(
3233
              [&]() { return annotationBuilder.initBrand(); }));
Kenton Varda's avatar
Kenton Varda committed
3234
          KJ_IF_MAYBE(annotationSchema,
3235
                      resolver.resolveBootstrapSchema(annotationBuilder.getId(),
3236
                                                      annotationBuilder.getBrand())) {
Kenton Varda's avatar
Kenton Varda committed
3237 3238 3239 3240 3241
            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."));
            }
3242

Kenton Varda's avatar
Kenton Varda committed
3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258
            // 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(),
3259 3260
                                      annotationBuilder.getValue(),
                                      *annotationSchema);
Kenton Varda's avatar
Kenton Varda committed
3261 3262
                break;
            }
3263
          }
3264
        }
Kenton Varda's avatar
Kenton Varda committed
3265 3266 3267
      } else if (*kind != Declaration::ANNOTATION) {
        errorReporter.addErrorOn(name, kj::str(
            "'", expressionString(name), "' is not an annotation."));
3268 3269 3270
      }
    }
  }
Kenton Varda's avatar
Kenton Varda committed
3271

3272
  return result;
Kenton Varda's avatar
Kenton Varda committed
3273 3274 3275 3276
}

}  // namespace compiler
}  // namespace capnp