test.capnp 27.6 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:
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:
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.
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.
21

22 23
@0xd508eebdc2dc42b8;

24 25
using Cxx = import "c++.capnp";

26 27
# Use a namespace likely to cause trouble if the generated code doesn't use fully-qualified
# names for stuff in the capnproto namespace.
28
$Cxx.namespace("capnproto_test::capnp::test");
29

30 31 32 33 34 35 36 37 38 39 40
enum TestEnum {
  foo @0;
  bar @1;
  baz @2;
  qux @3;
  quux @4;
  corge @5;
  grault @6;
  garply @7;
}

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
struct TestAllTypes {
  voidField      @0  : Void;
  boolField      @1  : Bool;
  int8Field      @2  : Int8;
  int16Field     @3  : Int16;
  int32Field     @4  : Int32;
  int64Field     @5  : Int64;
  uInt8Field     @6  : UInt8;
  uInt16Field    @7  : UInt16;
  uInt32Field    @8  : UInt32;
  uInt64Field    @9  : UInt64;
  float32Field   @10 : Float32;
  float64Field   @11 : Float64;
  textField      @12 : Text;
  dataField      @13 : Data;
  structField    @14 : TestAllTypes;
57
  enumField      @15 : TestEnum;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  interfaceField @16 : Void;  # TODO

  voidList      @17 : List(Void);
  boolList      @18 : List(Bool);
  int8List      @19 : List(Int8);
  int16List     @20 : List(Int16);
  int32List     @21 : List(Int32);
  int64List     @22 : List(Int64);
  uInt8List     @23 : List(UInt8);
  uInt16List    @24 : List(UInt16);
  uInt32List    @25 : List(UInt32);
  uInt64List    @26 : List(UInt64);
  float32List   @27 : List(Float32);
  float64List   @28 : List(Float64);
  textList      @29 : List(Text);
  dataList      @30 : List(Data);
  structList    @31 : List(TestAllTypes);
75
  enumList      @32 : List(TestEnum);
76
  interfaceList @33 : List(Void);  # TODO
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
}

struct TestDefaults {
  voidField      @0  : Void    = void;
  boolField      @1  : Bool    = true;
  int8Field      @2  : Int8    = -123;
  int16Field     @3  : Int16   = -12345;
  int32Field     @4  : Int32   = -12345678;
  int64Field     @5  : Int64   = -123456789012345;
  uInt8Field     @6  : UInt8   = 234;
  uInt16Field    @7  : UInt16  = 45678;
  uInt32Field    @8  : UInt32  = 3456789012;
  uInt64Field    @9  : UInt64  = 12345678901234567890;
  float32Field   @10 : Float32 = 1234.5;
  float64Field   @11 : Float64 = -123e45;
  textField      @12 : Text    = "foo";
93
  dataField      @13 : Data    = 0x"62 61 72"; # "bar"
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  structField    @14 : TestAllTypes = (
      voidField      = void,
      boolField      = true,
      int8Field      = -12,
      int16Field     = 3456,
      int32Field     = -78901234,
      int64Field     = 56789012345678,
      uInt8Field     = 90,
      uInt16Field    = 1234,
      uInt32Field    = 56789012,
      uInt64Field    = 345678901234567890,
      float32Field   = -1.25e-10,
      float64Field   = 345,
      textField      = "baz",
      dataField      = "qux",
      structField    = (
          textField = "nested",
          structField = (textField = "really nested")),
112
      enumField      = baz,
113 114 115 116 117 118
      # interfaceField can't have a default

      voidList      = [void, void, void],
      boolList      = [false, true, false, true, true],
      int8List      = [12, -34, -0x80, 0x7f],
      int16List     = [1234, -5678, -0x8000, 0x7fff],
119
      int32List     = [12345678, -90123456, -0x80000000, 0x7fffffff],
120 121 122 123 124 125 126 127 128 129
      int64List     = [123456789012345, -678901234567890, -0x8000000000000000, 0x7fffffffffffffff],
      uInt8List     = [12, 34, 0, 0xff],
      uInt16List    = [1234, 5678, 0, 0xffff],
      uInt32List    = [12345678, 90123456, 0, 0xffffffff],
      uInt64List    = [123456789012345, 678901234567890, 0, 0xffffffffffffffff],
      float32List   = [0, 1234567, 1e37, -1e37, 1e-37, -1e-37],
      float64List   = [0, 123456789012345, 1e306, -1e306, 1e-306, -1e-306],
      textList      = ["quux", "corge", "grault"],
      dataList      = ["garply", "waldo", "fred"],
      structList    = [
130 131
          (textField = "x " "structlist"
                       " 1"),
132
          (textField = "x structlist 2"),
133 134
          (textField = "x structlist 3")],
      enumList      = [qux, bar, grault]
135 136
      # interfaceList can't have a default
      );
137
  enumField      @15 : TestEnum = corge;
138 139 140 141 142 143 144 145 146 147 148 149
  interfaceField @16 : Void;  # TODO

  voidList      @17 : List(Void)    = [void, void, void, void, void, void];
  boolList      @18 : List(Bool)    = [true, false, false, true];
  int8List      @19 : List(Int8)    = [111, -111];
  int16List     @20 : List(Int16)   = [11111, -11111];
  int32List     @21 : List(Int32)   = [111111111, -111111111];
  int64List     @22 : List(Int64)   = [1111111111111111111, -1111111111111111111];
  uInt8List     @23 : List(UInt8)   = [111, 222] ;
  uInt16List    @24 : List(UInt16)  = [33333, 44444];
  uInt32List    @25 : List(UInt32)  = [3333333333];
  uInt64List    @26 : List(UInt64)  = [11111111111111111111];
150 151
  float32List   @27 : List(Float32) = [5555.5, inf, -inf, nan];
  float64List   @28 : List(Float64) = [7777.75, inf, -inf, nan];
152 153 154 155 156 157
  textList      @29 : List(Text)    = ["plugh", "xyzzy", "thud"];
  dataList      @30 : List(Data)    = ["oops", "exhausted", "rfc3092"];
  structList    @31 : List(TestAllTypes) = [
      (textField = "structlist 1"),
      (textField = "structlist 2"),
      (textField = "structlist 3")];
158
  enumList      @32 : List(TestEnum) = [foo, garply];
159 160
  interfaceList @33 : List(Void);  # TODO
}
Kenton Varda's avatar
Kenton Varda committed
161

162 163
struct TestAnyPointer {
  anyPointerField @0 :AnyPointer;
164

165
  # Do not add any other fields here!  Some tests rely on anyPointerField being the last pointer
166
  # in the struct.
167 168
}

169 170 171 172 173 174
struct TestAnyOthers {
  anyStructField @0 :AnyStruct;
  anyListField @1 :AnyList;
  capabilityField @2 :Capability;
}

175 176 177 178 179 180 181 182 183 184 185 186
struct TestOutOfOrder {
  foo @3 :Text;
  bar @2 :Text;
  baz @8 :Text;
  qux @0 :Text;
  quux @6 :Text;
  corge @4 :Text;
  grault @1 :Text;
  garply @7 :Text;
  waldo @5 :Text;
}

Kenton Varda's avatar
Kenton Varda committed
187
struct TestUnion {
188
  union0 @0! :union {
189 190 191 192 193 194 195 196
    # Pack union 0 under ideal conditions: there is no unused padding space prior to it.
    u0f0s0  @4: Void;
    u0f0s1  @5: Bool;
    u0f0s8  @6: Int8;
    u0f0s16 @7: Int16;
    u0f0s32 @8: Int32;
    u0f0s64 @9: Int64;
    u0f0sp  @10: Text;
Kenton Varda's avatar
Kenton Varda committed
197

198 199 200 201 202 203 204 205 206
    # Pack more stuff into union0 -- should go in same space.
    u0f1s0  @11: Void;
    u0f1s1  @12: Bool;
    u0f1s8  @13: Int8;
    u0f1s16 @14: Int16;
    u0f1s32 @15: Int32;
    u0f1s64 @16: Int64;
    u0f1sp  @17: Text;
  }
Kenton Varda's avatar
Kenton Varda committed
207 208 209 210

  # Pack one bit in order to make pathological situation for union1.
  bit0 @18: Bool;

211
  union1 @1! :union {
212 213 214 215 216 217 218 219 220 221 222 223 224 225
    # Pack pathologically bad case.  Each field takes up new space.
    u1f0s0  @19: Void;
    u1f0s1  @20: Bool;
    u1f1s1  @21: Bool;
    u1f0s8  @22: Int8;
    u1f1s8  @23: Int8;
    u1f0s16 @24: Int16;
    u1f1s16 @25: Int16;
    u1f0s32 @26: Int32;
    u1f1s32 @27: Int32;
    u1f0s64 @28: Int64;
    u1f1s64 @29: Int64;
    u1f0sp  @30: Text;
    u1f1sp  @31: Text;
Kenton Varda's avatar
Kenton Varda committed
226

227
    # Pack more stuff into union1 -- each should go into the same space as corresponding u1f0s*.
228 229 230 231 232 233 234 235
    u1f2s0  @32: Void;
    u1f2s1  @33: Bool;
    u1f2s8  @34: Int8;
    u1f2s16 @35: Int16;
    u1f2s32 @36: Int32;
    u1f2s64 @37: Int64;
    u1f2sp  @38: Text;
  }
Kenton Varda's avatar
Kenton Varda committed
236 237 238 239 240 241 242 243 244 245 246 247

  # Fill in the rest of that bitfield from earlier.
  bit2 @39: Bool;
  bit3 @40: Bool;
  bit4 @41: Bool;
  bit5 @42: Bool;
  bit6 @43: Bool;
  bit7 @44: Bool;

  # Interleave two unions to be really annoying.
  # Also declare in reverse order to make sure union discriminant values are sorted by field number
  # and not by declaration order.
248
  union2 @2! :union {
249 250 251 252 253 254 255
    u2f0s64 @54: Int64;
    u2f0s32 @52: Int32;
    u2f0s16 @50: Int16;
    u2f0s8 @47: Int8;
    u2f0s1 @45: Bool;
  }

256
  union3 @3! :union {
257 258 259 260 261 262
    u3f0s64 @55: Int64;
    u3f0s32 @53: Int32;
    u3f0s16 @51: Int16;
    u3f0s8 @48: Int8;
    u3f0s1 @46: Bool;
  }
Kenton Varda's avatar
Kenton Varda committed
263 264

  byte0 @49: UInt8;
Kenton Varda's avatar
Kenton Varda committed
265
}
266

267 268 269 270 271 272 273 274 275 276 277 278 279
struct TestUnnamedUnion {
  before @0 :Text;

  union {
    foo @1 :UInt16;
    bar @3 :UInt32;
  }

  middle @2 :UInt16;

  after @4 :Text;
}

280 281 282 283 284 285 286 287 288 289 290
struct TestUnionInUnion {
  # There is no reason to ever do this.
  outer :union {
    inner :union {
      foo @0 :Int32;
      bar @1 :Int32;
    }
    baz @2 :Int32;
  }
}

Kenton Varda's avatar
Kenton Varda committed
291
struct TestGroups {
292 293
  groups :union {
    foo :group {
294 295 296
      corge @0 :Int32;
      grault @2 :Int64;
      garply @8 :Text;
Kenton Varda's avatar
Kenton Varda committed
297
    }
298
    bar :group {
299 300 301
      corge @3 :Int32;
      grault @4 :Text;
      garply @5 :Int64;
Kenton Varda's avatar
Kenton Varda committed
302
    }
303
    baz :group {
304 305 306
      corge @1 :Int32;
      grault @6 :Text;
      garply @7 :Text;
Kenton Varda's avatar
Kenton Varda committed
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
struct TestInterleavedGroups {
  group1 :group {
    foo @0 :UInt32;
    bar @2 :UInt64;
    union {
      qux @4 :UInt16;
      corge :group {
        grault @6 :UInt64;
        garply @8 :UInt16;
        plugh @14 :Text;
        xyzzy @16 :Text;
      }

      fred @12 :Text;
    }

    waldo @10 :Text;
  }

  group2 :group {
    foo @1 :UInt32;
    bar @3 :UInt64;
    union {
      qux @5 :UInt16;
      corge :group {
        grault @7 :UInt64;
        garply @9 :UInt16;
        plugh @15 :Text;
        xyzzy @17 :Text;
      }

      fred @13 :Text;
    }

    waldo @11 :Text;
  }
}

349 350
struct TestUnionDefaults {
  s16s8s64s8Set @0 :TestUnion =
351 352
      (union0 = (u0f0s16 = 321), union1 = (u1f0s8 = 123), union2 = (u2f0s64 = 12345678901234567),
       union3 = (u3f0s8 = 55));
353
  s0sps1s32Set @1 :TestUnion =
354 355
      (union0 = (u0f1s0 = void), union1 = (u1f0sp = "foo"), union2 = (u2f0s1 = true),
       union3 = (u3f0s32 = 12345678));
356

357 358
  unnamed1 @2 :TestUnnamedUnion = (foo = 123);
  unnamed2 @3 :TestUnnamedUnion = (bar = 321, before = "foo", after = "bar");
359
}
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

struct TestNestedTypes {
  enum NestedEnum {
    foo @0;
    bar @1;
  }

  struct NestedStruct {
    enum NestedEnum {
      baz @0;
      qux @1;
      quux @2;
    }

    outerNestedEnum @0 :TestNestedTypes.NestedEnum = bar;
    innerNestedEnum @1 :NestedEnum = quux;
  }

  nestedStruct @0 :NestedStruct;

  outerNestedEnum @1 :NestedEnum = bar;
  innerNestedEnum @2 :NestedStruct.NestedEnum = quux;
}
383 384 385 386 387 388 389 390

struct TestUsing {
  using OuterNestedEnum = TestNestedTypes.NestedEnum;
  using TestNestedTypes.NestedStruct.NestedEnum;

  outerNestedEnum @1 :OuterNestedEnum = bar;
  innerNestedEnum @0 :NestedEnum = quux;
}
Kenton Varda's avatar
Kenton Varda committed
391

392
struct TestLists {
Kenton Varda's avatar
Kenton Varda committed
393 394
  # Small structs, when encoded as list, will be encoded as primitive lists rather than struct
  # lists, to save space.
395 396 397 398 399 400 401 402
  struct Struct0  { f @0 :Void; }
  struct Struct1  { f @0 :Bool; }
  struct Struct8  { f @0 :UInt8; }
  struct Struct16 { f @0 :UInt16; }
  struct Struct32 { f @0 :UInt32; }
  struct Struct64 { f @0 :UInt64; }
  struct StructP  { f @0 :Text; }

403 404 405 406 407 408 409 410 411
  # Versions of the above which cannot be encoded as primitive lists.
  struct Struct0c  { f @0 :Void; pad @1 :Text; }
  struct Struct1c  { f @0 :Bool; pad @1 :Text; }
  struct Struct8c  { f @0 :UInt8; pad @1 :Text; }
  struct Struct16c { f @0 :UInt16; pad @1 :Text; }
  struct Struct32c { f @0 :UInt32; pad @1 :Text; }
  struct Struct64c { f @0 :UInt64; pad @1 :Text; }
  struct StructPc  { f @0 :Text; pad @1 :UInt64; }

412 413 414 415 416 417 418
  list0  @0 :List(Struct0);
  list1  @1 :List(Struct1);
  list8  @2 :List(Struct8);
  list16 @3 :List(Struct16);
  list32 @4 :List(Struct32);
  list64 @5 :List(Struct64);
  listP  @6 :List(StructP);
419

420 421 422
  int32ListList @7 :List(List(Int32));
  textListList @8 :List(List(Text));
  structListList @9 :List(List(TestAllTypes));
423 424
}

425 426 427 428 429 430
struct TestFieldZeroIsBit {
  bit @0 :Bool;
  secondBit @1 :Bool = true;
  thirdField @2 :UInt8 = 123;
}

431 432
struct TestListDefaults {
  lists @0 :TestLists = (
433
      list0  = [(f = void), (f = void)],
434
      list1  = [(f = true), (f = false), (f = true), (f = true)],
435 436 437 438
      list8  = [(f = 123), (f = 45)],
      list16 = [(f = 12345), (f = 6789)],
      list32 = [(f = 123456789), (f = 234567890)],
      list64 = [(f = 1234567890123456), (f = 2345678901234567)],
439
      listP  = [(f = "foo"), (f = "bar")],
440 441
      int32ListList = [[1, 2, 3], [4, 5], [12341234]],
      textListList = [["foo", "bar"], ["baz"], ["qux", "corge"]],
442
      structListList = [[(int32Field = 123), (int32Field = 456)], [(int32Field = 789)]]);
Kenton Varda's avatar
Kenton Varda committed
443
}
444 445

struct TestLateUnion {
446 447
  # Test what happens if the unions are not the first ordinals in the struct.  At one point this
  # was broken for the dynamic API.
448 449 450 451 452

  foo @0 :Int32;
  bar @1 :Text;
  baz @2 :Int16;

453
  theUnion @3! :union {
454 455 456 457
    qux @4 :Text;
    corge @5 :List(Int32);
    grault @6 :Float32;
  }
Kenton Varda's avatar
Kenton Varda committed
458

459
  anotherUnion @7! :union {
Kenton Varda's avatar
Kenton Varda committed
460 461 462 463
    qux @8 :Text;
    corge @9 :List(Int32);
    grault @10 :Float32;
  }
464
}
465 466 467

struct TestOldVersion {
  # A subset of TestNewVersion.
468
  old1 @0 :Int64;
469 470 471 472 473 474
  old2 @1 :Text;
  old3 @2 :TestOldVersion;
}

struct TestNewVersion {
  # A superset of TestOldVersion.
475
  old1 @0 :Int64;
476 477 478 479 480
  old2 @1 :Text;
  old3 @2 :TestNewVersion;
  new1 @3 :Int64 = 987;
  new2 @4 :Text = "baz";
}
481

482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
struct TestOldUnionVersion {
  union {
    a @0 :Void;
    b @1 :UInt64;
  }
}

struct TestNewUnionVersion {
  union {
    a :union {
      a0 @0 :Void;
      a1 @2 :UInt64;
    }
    b @1 :UInt64;
  }
}

499
struct TestStructUnion {
500
  un @0! :union {
501
    struct @1 :SomeStruct;
502
    object @2 :TestAnyPointer;
503
  }
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

  struct SomeStruct {
    someText @0 :Text;
    moreText @1 :Text;
  }
}

struct TestPrintInlineStructs {
  someText @0 :Text;

  structList @1 :List(InlineStruct);
  struct InlineStruct {
    int32Field @0 :Int32;
    textField @1 :Text;
  }
519
}
520

521 522 523 524
struct TestWholeFloatDefault {
  # At one point, these failed to compile in C++ because it would produce literals like "123f",
  # which is not valid; it needs to be "123.0f".
  field @0 :Float32 = 123;
525
  bigField @1 :Float32 = 2e30;
526
  const constant :Float32 = 456;
527
  const bigConstant :Float32 = 4e30;
528 529
}

Kenton Varda's avatar
Kenton Varda committed
530 531 532 533
struct TestGenerics(Foo, Bar) {
  foo @0 :Foo;
  rev @1 :TestGenerics(Bar, Foo);

534 535 536 537 538 539 540
  union {
    uv @2:Void;
    ug :group {
      ugfoo @3:Int32;
    }
  }

541 542 543
  list @4 :List(Inner);
  # At one time this failed to compile with MSVC due to poor expression SFINAE support.

Kenton Varda's avatar
Kenton Varda committed
544 545 546 547 548 549 550 551
  struct Inner {
    foo @0 :Foo;
    bar @1 :Bar;
  }

  struct Inner2(Baz) {
    bar @0 :Bar;
    baz @1 :Baz;
552 553
    innerBound @2 :Inner;
    innerUnbound @3 :TestGenerics.Inner;
554 555 556 557 558 559

    struct DeepNest(Qux) {
      foo @0 :Foo;
      bar @1 :Bar;
      baz @2 :Baz;
      qux @3 :Qux;
560 561 562 563 564

      interface DeepNestInterface(Quux) {
        # At one time this failed to compile.
        call @0 () -> ();
      }
565
    }
Kenton Varda's avatar
Kenton Varda committed
566 567 568
  }

  interface Interface(Qux) {
569
    call @0 Inner2(Text) -> (qux :Qux, gen :TestGenerics(TestAllTypes, TestAnyPointer));
Kenton Varda's avatar
Kenton Varda committed
570
  }
571 572

  annotation ann(struct) :Foo;
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

  using AliasFoo = Foo;
  using AliasInner = Inner;
  using AliasInner2 = Inner2;
  using AliasInner2Text = Inner2(Text);
  using AliasRev = TestGenerics(Bar, Foo);

  struct UseAliases {
    foo @0 :AliasFoo;
    inner @1 :AliasInner;
    inner2 @2 :AliasInner2;
    inner2Bind @3 :AliasInner2(Text);
    inner2Text @4 :AliasInner2Text;
    revFoo @5 :AliasRev.AliasFoo;
  }
588 589 590 591
}

struct TestGenericsWrapper(Foo, Bar) {
  value @0 :TestGenerics(Foo, Bar);
Kenton Varda's avatar
Kenton Varda committed
592 593
}

594 595 596 597
struct TestGenericsWrapper2 {
  value @0 :TestGenericsWrapper(Text, TestAllTypes);
}

598 599 600 601
interface TestImplicitMethodParams {
  call @0 [T, U] (foo :T, bar :U) -> TestGenerics(T, U);
}

602 603 604 605
interface TestImplicitMethodParamsInGeneric(V) {
  call @0 [T, U] (foo :T, bar :U) -> TestGenerics(T, U);
}

Kenton Varda's avatar
Kenton Varda committed
606 607 608 609 610 611 612 613 614
struct TestGenericsUnion(Foo, Bar) {
  # At one point this failed to compile.

  union {
    foo @0 :Foo;
    bar @1 :Bar;
  }
}

615 616 617 618
struct TestUseGenerics $TestGenerics(Text, Data).ann("foo") {
  basic @0 :TestGenerics(TestAllTypes, TestAnyPointer);
  inner @1 :TestGenerics(TestAllTypes, TestAnyPointer).Inner;
  inner2 @2 :TestGenerics(TestAllTypes, TestAnyPointer).Inner2(Text);
Kenton Varda's avatar
Kenton Varda committed
619 620
  unspecified @3 :TestGenerics;
  unspecifiedInner @4 :TestGenerics.Inner2(Text);
621
  wrapper @8 :TestGenericsWrapper(TestAllTypes, TestAnyPointer);
622
  cap @18 :TestGenerics(TestInterface, Text);
623
  genericCap @19 :TestGenerics(TestAllTypes, List(UInt32)).Interface(Data);
624 625 626 627 628 629 630 631 632 633

  default @5 :TestGenerics(TestAllTypes, Text) =
      (foo = (int16Field = 123), rev = (foo = "text", rev = (foo = (int16Field = 321))));
  defaultInner @6 :TestGenerics(TestAllTypes, Text).Inner =
      (foo = (int16Field = 123), bar = "text");
  defaultUser @7 :TestUseGenerics = (basic = (foo = (int16Field = 123)));
  defaultWrapper @9 :TestGenericsWrapper(Text, TestAllTypes) =
      (value = (foo = "text", rev = (foo = (int16Field = 321))));
  defaultWrapper2 @10 :TestGenericsWrapper2 =
      (value = (value = (foo = "text", rev = (foo = (int16Field = 321)))));
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652

  aliasFoo @11 :TestGenerics(TestAllTypes, TestAnyPointer).AliasFoo = (int16Field = 123);
  aliasInner @12 :TestGenerics(TestAllTypes, TestAnyPointer).AliasInner
      = (foo = (int16Field = 123));
  aliasInner2 @13 :TestGenerics(TestAllTypes, TestAnyPointer).AliasInner2
      = (innerBound = (foo = (int16Field = 123)));
  aliasInner2Bind @14 :TestGenerics(TestAllTypes, TestAnyPointer).AliasInner2(List(UInt32))
      = (baz = [12, 34], innerBound = (foo = (int16Field = 123)));
  aliasInner2Text @15 :TestGenerics(TestAllTypes, TestAnyPointer).AliasInner2Text
      = (baz = "text", innerBound = (foo = (int16Field = 123)));
  aliasRev @16 :TestGenerics(TestAnyPointer, Text).AliasRev.AliasFoo = "text";

  useAliases @17 :TestGenerics(TestAllTypes, List(UInt32)).UseAliases = (
      foo = (int16Field = 123),
      inner = (foo = (int16Field = 123)),
      inner2 = (innerBound = (foo = (int16Field = 123))),
      inner2Bind = (baz = "text", innerBound = (foo = (int16Field = 123))),
      inner2Text = (baz = "text", innerBound = (foo = (int16Field = 123))),
      revFoo = [12, 34, 56]);
Kenton Varda's avatar
Kenton Varda committed
653 654
}

655 656
struct TestEmptyStruct {}

657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
struct TestConstants {
  const voidConst      :Void    = void;
  const boolConst      :Bool    = true;
  const int8Const      :Int8    = -123;
  const int16Const     :Int16   = -12345;
  const int32Const     :Int32   = -12345678;
  const int64Const     :Int64   = -123456789012345;
  const uint8Const     :UInt8   = 234;
  const uint16Const    :UInt16  = 45678;
  const uint32Const    :UInt32  = 3456789012;
  const uint64Const    :UInt64  = 12345678901234567890;
  const float32Const   :Float32 = 1234.5;
  const float64Const   :Float64 = -123e45;
  const textConst      :Text    = "foo";
  const dataConst      :Data    = "bar";
  const structConst    :TestAllTypes = (
      voidField      = void,
      boolField      = true,
      int8Field      = -12,
      int16Field     = 3456,
      int32Field     = -78901234,
      int64Field     = 56789012345678,
      uInt8Field     = 90,
      uInt16Field    = 1234,
      uInt32Field    = 56789012,
      uInt64Field    = 345678901234567890,
      float32Field   = -1.25e-10,
      float64Field   = 345,
      textField      = "baz",
      dataField      = "qux",
      structField    = (
          textField = "nested",
          structField = (textField = "really nested")),
      enumField      = baz,
      # interfaceField can't have a default

      voidList      = [void, void, void],
      boolList      = [false, true, false, true, true],
      int8List      = [12, -34, -0x80, 0x7f],
      int16List     = [1234, -5678, -0x8000, 0x7fff],
      int32List     = [12345678, -90123456, -0x80000000, 0x7fffffff],
      int64List     = [123456789012345, -678901234567890, -0x8000000000000000, 0x7fffffffffffffff],
      uInt8List     = [12, 34, 0, 0xff],
      uInt16List    = [1234, 5678, 0, 0xffff],
      uInt32List    = [12345678, 90123456, 0, 0xffffffff],
      uInt64List    = [123456789012345, 678901234567890, 0, 0xffffffffffffffff],
      float32List   = [0, 1234567, 1e37, -1e37, 1e-37, -1e-37],
      float64List   = [0, 123456789012345, 1e306, -1e306, 1e-306, -1e-306],
      textList      = ["quux", "corge", "grault"],
      dataList      = ["garply", "waldo", "fred"],
      structList    = [
708 709
          (textField = "x " "structlist"
                       " 1"),
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
          (textField = "x structlist 2"),
          (textField = "x structlist 3")],
      enumList      = [qux, bar, grault]
      # interfaceList can't have a default
      );
  const enumConst      :TestEnum = corge;

  const voidListConst      :List(Void)    = [void, void, void, void, void, void];
  const boolListConst      :List(Bool)    = [true, false, false, true];
  const int8ListConst      :List(Int8)    = [111, -111];
  const int16ListConst     :List(Int16)   = [11111, -11111];
  const int32ListConst     :List(Int32)   = [111111111, -111111111];
  const int64ListConst     :List(Int64)   = [1111111111111111111, -1111111111111111111];
  const uint8ListConst     :List(UInt8)   = [111, 222] ;
  const uint16ListConst    :List(UInt16)  = [33333, 44444];
  const uint32ListConst    :List(UInt32)  = [3333333333];
  const uint64ListConst    :List(UInt64)  = [11111111111111111111];
  const float32ListConst   :List(Float32) = [5555.5, inf, -inf, nan];
  const float64ListConst   :List(Float64) = [7777.75, inf, -inf, nan];
  const textListConst      :List(Text)    = ["plugh", "xyzzy", "thud"];
  const dataListConst      :List(Data)    = ["oops", "exhausted", "rfc3092"];
  const structListConst    :List(TestAllTypes) = [
      (textField = "structlist 1"),
      (textField = "structlist 2"),
      (textField = "structlist 3")];
  const enumListConst      :List(TestEnum) = [foo, garply];
}

const globalInt :UInt32 = 12345;
const globalText :Text = "foobar";
const globalStruct :TestAllTypes = (int32Field = 54321);
741
const globalPrintableStruct :TestPrintInlineStructs = (someText = "foo");
742 743 744 745 746 747
const derivedConstant :TestAllTypes = (
    uInt32Field = .globalInt,
    textField = TestConstants.textConst,
    structField = TestConstants.structConst,
    int16List = TestConstants.int16ListConst,
    structList = TestConstants.structListConst);
748

749 750 751
const genericConstant :TestGenerics(TestAllTypes, Text) =
    (foo = (int16Field = 123), rev = (foo = "text", rev = (foo = (int16Field = 321))));

752 753 754 755
const embeddedData :Data = embed "testdata/packed";
const embeddedText :Text = embed "testdata/short.txt";
const embeddedStruct :TestAllTypes = embed "testdata/binary";

756 757
const nonAsciiText :Text = "♫ é ✓";

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
struct TestAnyPointerConstants {
  anyKindAsStruct @0 :AnyPointer;
  anyStructAsStruct @1 :AnyStruct;
  anyKindAsList @2 :AnyPointer;
  anyListAsList @3 :AnyList;

}

const anyPointerConstants :TestAnyPointerConstants = (
  anyKindAsStruct = TestConstants.structConst,
  anyStructAsStruct = TestConstants.structConst,
  anyKindAsList = TestConstants.int32ListConst,
  anyListAsList = TestConstants.int32ListConst,
);

773
interface TestInterface {
774
  foo @0 (i :UInt32, j :Bool) -> (x :Text);
775
  bar @1 () -> ();
776 777 778 779 780
  baz @2 (s: TestAllTypes);
}

interface TestExtends extends(TestInterface) {
  qux @0 ();
781 782
  corge @1 TestAllTypes -> ();
  grault @2 () -> TestAllTypes;
783
}
784

785 786
interface TestExtends2 extends(TestExtends) {}

787
interface TestPipeline {
788
  getCap @0 (n: UInt32, inCap :TestInterface) -> (s: Text, outBox :Box);
789
  testPointers @1 (cap :TestInterface, obj :AnyPointer, list :List(TestInterface)) -> ();
790
  getAnyCap @2 (n: UInt32, inCap :Capability) -> (s: Text, outBox :AnyBox);
791 792 793 794

  struct Box {
    cap @0 :TestInterface;
  }
795 796 797
  struct AnyBox {
    cap @0 :Capability;
  }
798
}
799

800
interface TestCallOrder {
Kenton Varda's avatar
Kenton Varda committed
801
  getCallSequence @0 (expected: UInt32) -> (n: UInt32);
802
  # First call returns 0, next returns 1, ...
Kenton Varda's avatar
Kenton Varda committed
803 804
  #
  # The input `expected` is ignored but useful for disambiguating debug logs.
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
}

interface TestTailCallee {
  struct TailResult {
    i @0 :UInt32;
    t @1 :Text;
    c @2 :TestCallOrder;
  }

  foo @0 (i :Int32, t :Text) -> TailResult;
}

interface TestTailCaller {
  foo @0 (i :Int32, callee :TestTailCallee) -> TestTailCallee.TailResult;
}

821 822
interface TestHandle {}

823 824 825 826 827 828 829 830
interface TestMoreStuff extends(TestCallOrder) {
  # Catch-all type that contains lots of testing methods.

  callFoo @0 (cap :TestInterface) -> (s: Text);
  # Call `cap.foo()`, check the result, and return "bar".

  callFooWhenResolved @1 (cap :TestInterface) -> (s: Text);
  # Like callFoo but waits for `cap` to resolve first.
831 832 833 834 835 836 837 838 839 840 841 842

  neverReturn @2 (cap :TestInterface) -> (capCopy :TestInterface);
  # Doesn't return.  You should cancel it.

  hold @3 (cap :TestInterface) -> ();
  # Returns immediately but holds on to the capability.

  callHeld @4 () -> (s: Text);
  # Calls the capability previously held using `hold` (and keeps holding it).

  getHeld @5 () -> (cap :TestInterface);
  # Returns the capability previously held using `hold` (and keeps holding it).
Kenton Varda's avatar
Kenton Varda committed
843 844 845

  echo @6 (cap :TestCallOrder) -> (cap :TestCallOrder);
  # Just returns the input cap.
846

847
  expectCancel @7 (cap :TestInterface) -> ();
848
  # evalLater()-loops forever, holding `cap`.  Must be canceled.
849 850

  methodWithDefaults @8 (a :Text, b :UInt32 = 123, c :Text = "foo") -> (d :Text, e :Text = "bar");
851

852 853
  methodWithNullDefault @12 (a :Text, b :TestInterface = null);

854 855 856
  getHandle @9 () -> (handle :TestHandle);
  # Get a new handle. Tests have an out-of-band way to check the current number of live handles, so
  # this can be used to test garbage collection.
857 858 859

  getNull @10 () -> (nullCap :TestMoreStuff);
  # Always returns a null capability.
860 861 862

  getEnormousString @11 () -> (str :Text);
  # Attempts to return an 100MB string. Should always fail.
863 864
}

865 866 867 868 869 870
interface TestMembrane {
  makeThing @0 () -> (thing :Thing);
  callPassThrough @1 (thing :Thing, tailCall :Bool) -> Result;
  callIntercept @2 (thing :Thing, tailCall :Bool) -> Result;
  loopback @3 (thing :Thing) -> (thing :Thing);

871 872
  waitForever @4 ();

873 874 875 876 877 878 879 880 881 882
  interface Thing {
    passThrough @0 () -> Result;
    intercept @1 () -> Result;
  }

  struct Result {
    text @0 :Text;
  }
}

883 884 885 886 887
struct TestContainMembrane {
  cap @0 :TestMembrane.Thing;
  list @1 :List(TestMembrane.Thing);
}

888 889 890 891 892 893 894 895
struct TestTransferCap {
  list @0 :List(Element);
  struct Element {
    text @0 :Text;
    cap @1 :TestInterface;
  }
}

896 897 898 899 900 901 902
interface TestKeywordMethods {
  delete @0 ();
  class @1 ();
  void @2 ();
  return @3 ();
}

903 904 905 906
interface TestAuthenticatedBootstrap(VatId) {
  getCallerId @0 () -> (caller :VatId);
}

907 908 909 910 911
struct TestSturdyRef {
  hostId @0 :TestSturdyRefHostId;
  objectId @1 :AnyPointer;
}

912
struct TestSturdyRefHostId {
913
  host @0 :Text;
914 915 916 917
}

struct TestSturdyRefObjectId {
  tag @0 :Tag;
918 919
  enum Tag {
    testInterface @0;
920 921
    testExtends @1;
    testPipeline @2;
922 923
    testTailCallee @3;
    testTailCaller @4;
924
    testMoreStuff @5;
925 926 927 928 929 930
  }
}

struct TestProvisionId {}
struct TestRecipientId {}
struct TestThirdPartyCapId {}
931
struct TestJoinResult {}
932 933 934 935 936 937 938 939 940 941 942 943 944 945

struct TestNameAnnotation $Cxx.name("RenamedStruct") {
  union {
    badFieldName @0 :Bool $Cxx.name("goodFieldName");
    bar @1 :Int8;
  }

  enum BadlyNamedEnum $Cxx.name("RenamedEnum") {
    foo @0;
    bar @1;
    baz @2 $Cxx.name("qux");
  }

  anotherBadFieldName @2 :BadlyNamedEnum $Cxx.name("anotherGoodFieldName");
946 947 948 949

  struct NestedStruct $Cxx.name("RenamedNestedStruct") {
    badNestedFieldName @0 :Bool $Cxx.name("goodNestedFieldName");
    anotherBadNestedFieldName @1 :NestedStruct $Cxx.name("anotherGoodNestedFieldName");
950 951 952 953 954 955

    enum DeeplyNestedEnum $Cxx.name("RenamedDeeplyNestedEnum") {
      quux @0;
      corge @1;
      grault @2 $Cxx.name("garply");
    }
956 957
  }

958 959 960 961 962
  badlyNamedUnion :union $Cxx.name("renamedUnion") {
    badlyNamedGroup :group $Cxx.name("renamedGroup") {
      foo @3 :Void;
      bar @4 :Void;
    }
963 964
    baz @5 :NestedStruct $Cxx.name("qux");
  }
965
}
966 967 968 969

interface TestNameAnnotationInterface $Cxx.name("RenamedInterface") {
  badlyNamedMethod @0 (badlyNamedParam :UInt8 $Cxx.name("renamedParam")) $Cxx.name("renamedMethod");
}